From 7b16dbf41f305024218d419a153838ca8cbf0395 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Sun, 16 Sep 2012 21:06:30 +0400 Subject: [PATCH] src/learch.c: cleanup. Signed-off-by: Andrew Borodin --- src/learn.c | 177 +++++++++++++++++++++++++++--------------------------------- 1 file changed, 79 insertions(+), 98 deletions(-) diff --git a/src/learn.c b/src/learn.c index 9d4f7614a..dff8c809f 100644 --- a/src/learn.c +++ b/src/learn.c @@ -2,11 +2,12 @@ Learn keys Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, - 2007, 2009, 2011 + 2007, 2009, 2011, 2012 The Free Software Foundation, Inc. Written by: Jakub Jelinek, 1995 + Andrew Borodin , 2012 This file is part of the Midnight Commander. @@ -30,27 +31,18 @@ #include -#include -#include #include -#include - -#include -#include -#include #include "lib/global.h" #include "lib/tty/tty.h" #include "lib/tty/key.h" -#include "lib/mcconfig.h" /* Save profile */ +#include "lib/mcconfig.h" #include "lib/strescape.h" #include "lib/strutil.h" #include "lib/util.h" /* convert_controls() */ #include "lib/widget.h" -#include "src/filemanager/layout.h" /* mc_refresh() */ - #include "setup.h" #include "learn.h" @@ -59,9 +51,7 @@ /*** file scope macro definitions ****************************************************************/ #define UX 4 -#define UY 3 - -#define BY UY + 17 +#define UY 2 #define ROWS 13 #define COLSHIFT 23 @@ -74,33 +64,19 @@ typedef struct { Widget *button; Widget *label; - int ok; + gboolean ok; char *sequence; -} learnkey; +} learnkey_t; /*** file scope variables ************************************************************************/ -static struct -{ - int ret_cmd, flags, y, x; - const char *text; -} learn_but[BUTTONS] = -{ - /* *INDENT-OFF */ - { - B_CANCEL, NORMAL_BUTTON, 0, 39, N_("&Cancel")}, - { - B_ENTER, DEFPUSH_BUTTON, 0, 25, N_("&Save")} - /* *INDENT-ON */ -}; - static Dlg_head *learn_dlg; +static const char *learn_title = N_("Learn keys"); -static learnkey *learnkeys = NULL; +static learnkey_t *learnkeys = NULL; static int learn_total; static int learnok; -static int learnchanged; -static const char *learn_title = N_("Learn keys"); +static gboolean learnchanged = FALSE; /*** file scope functions ************************************************************************/ /* --------------------------------------------------------------------------------------------- */ @@ -126,20 +102,20 @@ learn_button (WButton * button, int action) g_free (learnkeys[action - B_USER].sequence); learnkeys[action - B_USER].sequence = NULL; } - seq = learn_key (); - if (seq) + seq = learn_key (); + if (seq != NULL) { /* Esc hides the dialog and do not allow definitions of * regular characters */ gboolean seq_ok = FALSE; - if (*seq && strcmp (seq, "\\e") && strcmp (seq, "\\e\\e") - && strcmp (seq, "^m") && strcmp (seq, "^i") && (seq[1] || (*seq < ' ' || *seq > '~'))) + if (*seq != '\0' && strcmp (seq, "\\e") != 0 && strcmp (seq, "\\e\\e") != 0 + && strcmp (seq, "^m") != 0 && strcmp (seq, "^i") != 0 + && (seq[1] != '\0' || *seq < ' ' || *seq > '~')) { - - learnchanged = 1; + learnchanged = TRUE; learnkeys[action - B_USER].sequence = seq; seq = convert_controls (seq); seq_ok = define_sequence (key_name_conv_tab[action - B_USER].code, seq, MCKEY_NOACTION); @@ -153,14 +129,16 @@ learn_button (WButton * button, int action) dlg_run_done (d); destroy_dlg (d); + dlg_select_widget (learnkeys[action - B_USER].button); + return 0; /* Do not kill learn_dlg */ } /* --------------------------------------------------------------------------------------------- */ -static int -learn_move (int right) +static gboolean +learn_move (gboolean right) { int i, totalcols; @@ -177,7 +155,7 @@ learn_move (int right) } else { - if (i / ROWS) + if (i / ROWS != 0) i -= ROWS; else if (i + (totalcols - 1) * ROWS >= learn_total) i += (totalcols - 2) * ROWS; @@ -185,14 +163,15 @@ learn_move (int right) i += (totalcols - 1) * ROWS; } dlg_select_widget (learnkeys[i].button); - return 1; + return TRUE; } - return 0; + + return FALSE; } /* --------------------------------------------------------------------------------------------- */ -static int +static gboolean learn_check_key (int c) { int i; @@ -205,7 +184,7 @@ learn_check_key (int c) dlg_select_widget (learnkeys[i].button); /* TRANSLATORS: This label appears near learned keys. Keep it short. */ label_set_text ((WLabel *) learnkeys[i].label, _("OK")); - learnkeys[i].ok = 1; + learnkeys[i].ok = TRUE; learnok++; if (learnok >= learn_total) { @@ -228,30 +207,28 @@ learn_check_key (int c) } dlg_stop (learn_dlg); } - return 1; + return TRUE; } + switch (c) { case KEY_LEFT: case 'h': - return learn_move (0); + return learn_move (FALSE); case KEY_RIGHT: case 'l': - return learn_move (1); + return learn_move (TRUE); case 'j': dlg_one_down (learn_dlg); - return 1; + return TRUE; case 'k': dlg_one_up (learn_dlg); - return 1; + return TRUE; } /* Prevent from disappearing if a non-defined sequence is pressed and contains a button hotkey. Only recognize hotkeys with ALT. */ - if (c < 255 && g_ascii_isalnum (c)) - return 1; - - return 0; + return (c < 255 && g_ascii_isalnum (c)); } /* --------------------------------------------------------------------------------------------- */ @@ -261,12 +238,8 @@ learn_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *da { switch (msg) { - case DLG_DRAW: - common_dialog_repaint (h); - return MSG_HANDLED; - case DLG_KEY: - return learn_check_key (parm); + return learn_check_key (parm) ? MSG_HANDLED : MSG_NOT_HANDLED; default: return default_dlg_callback (h, sender, msg, parm, data); @@ -278,58 +251,73 @@ learn_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *da static void init_learn (void) { + const int dlg_width = 78; + const int dlg_height = 23; + + struct + { + int ret_cmd, flags, y, x; + const char *text; + } learn_but[BUTTONS] = + { + /* *INDENT-OFF* */ + {B_CANCEL, NORMAL_BUTTON, 0, 39, N_("&Cancel")}, + {B_ENTER, DEFPUSH_BUTTON, 0, 25, N_("&Save")} + /* *INDENT-ON* */ + }; + int x, y, i, j; const key_code_name_t *key; char buffer[BUF_TINY]; #ifdef ENABLE_NLS - static int i18n_flag = 0; + static gboolean i18n_flag = FALSE; if (!i18n_flag) { - learn_but[0].text = _(learn_but[0].text); - learn_but[0].x = 78 / 2 + 4; - - learn_but[1].text = _(learn_but[1].text); - learn_but[1].x = 78 / 2 - (str_term_width1 (learn_but[1].text) + 9); - learn_title = _(learn_title); - i18n_flag = 1; + i18n_flag = TRUE; } + + learn_but[0].text = _(learn_but[0].text); + learn_but[0].x = dlg_width / 2 + 4; + + learn_but[1].text = _(learn_but[1].text); + learn_but[1].x = dlg_width / 2 - (str_term_width1 (learn_but[1].text) + 9); #endif /* ENABLE_NLS */ do_refresh (); learn_dlg = - create_dlg (TRUE, 0, 0, 23, 78, dialog_colors, learn_callback, NULL, + create_dlg (TRUE, 0, 0, dlg_height, dlg_width, dialog_colors, learn_callback, NULL, "[Learn keys]", learn_title, DLG_CENTER | DLG_REVERSE); for (i = 0; i < BUTTONS; i++) add_widget (learn_dlg, - button_new (BY + learn_but[i].y, learn_but[i].x, + button_new (dlg_height - 3 + learn_but[i].y, learn_but[i].x, learn_but[i].ret_cmd, learn_but[i].flags, _(learn_but[i].text), 0)); x = UX; y = UY; for (key = key_name_conv_tab, j = 0; - key->name != NULL && strcmp (key->name, "kpleft"); key++, j++) + key->name != NULL && strcmp (key->name, "kpleft") != 0; key++, j++) ; - learnkeys = g_new (learnkey, j); + learnkeys = g_new (learnkey_t, j); x += ((j - 1) / ROWS) * COLSHIFT; y += (j - 1) % ROWS; learn_total = j; learnok = 0; - learnchanged = 0; + learnchanged = FALSE; for (i = j - 1, key = key_name_conv_tab + j - 1; i >= 0; i--, key--) { - learnkeys[i].ok = 0; + learnkeys[i].ok = FALSE; learnkeys[i].sequence = NULL; g_snprintf (buffer, sizeof (buffer), "%-16s", _(key->longname)); learnkeys[i].button = - WIDGET (button_new (y, x, B_USER + i, NARROW_BUTTON, buffer, learn_button)); + WIDGET (button_new (y, x, B_USER + i, NARROW_BUTTON, buffer, learn_button)); add_widget (learn_dlg, learnkeys[i].button); learnkeys[i].label = WIDGET (label_new (y, x + 19, "")); add_widget (learn_dlg, learnkeys[i].label); - if (i % 13) + if (i % 13 != 0) y--; else { @@ -337,15 +325,12 @@ init_learn (void) y = UY + ROWS - 1; } } + add_widget (learn_dlg, label_new (UY + 14, 5, - _("Press all the keys mentioned here. After you have done it, check"))); - add_widget (learn_dlg, - label_new (UY + 15, 5, - _("which keys are not marked with OK. Press space on the missing"))); - add_widget (learn_dlg, - label_new (UY + 16, 5, - _("key, or click with the mouse to define it. Move around with Tab."))); + _("Press all the keys mentioned here. After you have done it, check\n" + "which keys are not marked with OK. Press space on the missing\n" + "key, or click with the mouse to define it. Move around with Tab."))); } /* --------------------------------------------------------------------------------------------- */ @@ -363,24 +348,23 @@ static void learn_save (void) { int i; - int profile_changed = 0; - char *section = g_strconcat ("terminal:", getenv ("TERM"), (char *) NULL); - char *esc_str; + char *section; + gboolean profile_changed = FALSE; + + section = g_strconcat ("terminal:", getenv ("TERM"), (char *) NULL); for (i = 0; i < learn_total; i++) - { if (learnkeys[i].sequence != NULL) { - profile_changed = 1; + char *esc_str; esc_str = strutils_escape (learnkeys[i].sequence, -1, ";\\", TRUE); - mc_config_set_string_raw_value (mc_main_config, section, key_name_conv_tab[i].name, esc_str); - g_free (esc_str); + + profile_changed = TRUE; } - } /* On the one hand no good idea to save the complete setup but * without 'Auto save setup' the new key-definitions will not be @@ -403,6 +387,7 @@ learn_keys (void) { int save_old_esc_mode = old_esc_mode; gboolean save_alternate_plus_minus = mc_global.tty.alternate_plus_minus; + int result; /* old_esc_mode cannot work in learn keys dialog */ old_esc_mode = 0; @@ -412,9 +397,9 @@ learn_keys (void) correct_key_code */ mc_global.tty.alternate_plus_minus = TRUE; application_keypad_mode (); - init_learn (); - run_dlg (learn_dlg); + init_learn (); + result = run_dlg (learn_dlg); old_esc_mode = save_old_esc_mode; mc_global.tty.alternate_plus_minus = save_alternate_plus_minus; @@ -422,12 +407,8 @@ learn_keys (void) if (!mc_global.tty.alternate_plus_minus) numeric_keypad_mode (); - switch (learn_dlg->ret_value) - { - case B_ENTER: + if (result == B_ENTER) learn_save (); - break; - } learn_done (); } -- 2.11.4.GIT