summaryrefslogtreecommitdiffstats
path: root/src/selcodepage.c
blob: c8a3cdca89e9160447a9658aa80bb7b61eedb9e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
   User interface for charset selection.

   Copyright (C) 2001 Walery Studennikov <despair@sama.ru>

   Copyright (C) 2011-2023
   Free Software Foundation, Inc.

   Written by:
   Walery Studennikov <despair@sama.ru>, 2001

   This file is part of the Midnight Commander.

   The Midnight Commander is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   The Midnight Commander is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file selcodepage.c
 *  \brief Source: user %interface for charset %selection
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>

#include "lib/global.h"
#include "lib/widget.h"
#include "lib/charsets.h"

#include "setup.h"

#include "selcodepage.h"

/*** global variables ****************************************************************************/

/*** file scope macro definitions ****************************************************************/

#define ENTRY_LEN 30

/*** file scope type declarations ****************************************************************/

/*** forward declarations (file scope functions) *************************************************/

/*** file scope variables ************************************************************************/

/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */

static unsigned char
get_hotkey (int n)
{
    return (n <= 9) ? '0' + n : 'a' + n - 10;
}

/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */

/* Return value:
 *   -2 (SELECT_CHARSET_CANCEL)       : Cancel
 *   -1 (SELECT_CHARSET_OTHER_8BIT)   : "Other 8 bit"    if seldisplay == TRUE
 *   -1 (SELECT_CHARSET_NO_TRANSLATE) : "No translation" if seldisplay == FALSE
 *   >= 0                             : charset number
 */
int
select_charset (int center_y, int center_x, int current_charset, gboolean seldisplay)
{
    Listbox *listbox;
    size_t i;
    int listbox_result;
    char buffer[255];

    /* Create listbox */
    listbox =
        listbox_window_centered_new (center_y, center_x, codepages->len + 1, ENTRY_LEN + 2,
                                     _("Choose codepage"), "[Codepages Translation]");

    if (!seldisplay)
        LISTBOX_APPEND_TEXT (listbox, '-', _("-  < No translation >"), NULL, FALSE);

    /* insert all the items found */
    for (i = 0; i < codepages->len; i++)
    {
        const char *name = ((codepage_desc *) g_ptr_array_index (codepages, i))->name;
        g_snprintf (buffer, sizeof (buffer), "%c  %s", get_hotkey (i), name);
        LISTBOX_APPEND_TEXT (listbox, get_hotkey (i), buffer, NULL, FALSE);
    }
    if (seldisplay)
    {
        unsigned char hotkey = get_hotkey (codepages->len);
        g_snprintf (buffer, sizeof (buffer), "%c  %s", hotkey, _("Other 8 bit"));
        LISTBOX_APPEND_TEXT (listbox, hotkey, buffer, NULL, FALSE);
    }

    /* Select the default entry */
    i = (seldisplay)
        ? ((current_charset < 0) ? codepages->len : (size_t) current_charset)
        : ((size_t) current_charset + 1);

    listbox_set_current (listbox->list, i);

    listbox_result = listbox_run (listbox);

    if (listbox_result < 0)
    {
        /* Cancel dialog */
        return SELECT_CHARSET_CANCEL;
    }
    else
    {
        /* some charset has been selected */
        if (seldisplay)
        {
            /* charset list is finished with "Other 8 bit" item */
            return (listbox_result >= (int) codepages->len)
                ? SELECT_CHARSET_OTHER_8BIT : listbox_result;
        }
        else
        {
            /* charset list is began with "-  < No translation >" item */
            return (listbox_result - 1);
        }
    }
}

/* --------------------------------------------------------------------------------------------- */

/** Set codepage */
gboolean
do_set_codepage (int codepage)
{
    char *errmsg;
    gboolean ret;

    mc_global.source_codepage = codepage;
    errmsg = init_translation_table (codepage == SELECT_CHARSET_NO_TRANSLATE ?
                                     mc_global.display_codepage : mc_global.source_codepage,
                                     mc_global.display_codepage);
    ret = errmsg == NULL;

    if (!ret)
    {
        message (D_ERROR, MSG_ERROR, "%s", errmsg);
        g_free (errmsg);
    }

    return ret;
}

/* --------------------------------------------------------------------------------------------- */

/** Show menu selecting codepage */
gboolean
do_select_codepage (void)
{
    int r;

    r = select_charset (-1, -1, default_source_codepage, FALSE);
    if (r == SELECT_CHARSET_CANCEL)
        return FALSE;

    default_source_codepage = r;
    return do_set_codepage (default_source_codepage);
}

/* --------------------------------------------------------------------------------------------- */