summaryrefslogtreecommitdiff
path: root/src/selcodepage.c
blob: 8abce047f14cabefce386944ed7b357b461c7384 (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
/*
   User interface for charset selection.

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

   Copyright (C) 2011-2025
   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 <https://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_NO_TRANSLATE) : "No translation"
 *   >= 0                             : charset number
 */
int
select_charset (const int center_y, const int center_x, const int current_charset)
{
    Listbox *listbox;

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

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

    for (guint i = 0; i < codepages->len; i++)
    {
        char buffer[BUF_SMALL];
        const char *name = get_codepage_name (i);

        g_snprintf (buffer, sizeof (buffer), "%c  %s", get_hotkey (i), name);
        LISTBOX_APPEND_TEXT (listbox, get_hotkey (i), buffer, NULL, FALSE);
    }

    listbox_set_current (listbox->list, current_charset + 1);

    const int listbox_result = listbox_run (listbox);

    return listbox_result < 0 ? SELECT_CHARSET_CANCEL : listbox_result - 1;
}

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

/** Set codepage */
gboolean
do_set_codepage (const 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)
{
    const int r = select_charset (-1, -1, default_source_codepage);

    if (r == SELECT_CHARSET_CANCEL)
        return FALSE;

    default_source_codepage = r;
    return do_set_codepage (default_source_codepage);
}

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