patches by Rostislav Beneš: mc-03-button

text of button stored in hotkey_t instead of char*, modified achown.c, because
access directly button text, change in drawing button adn handling hotkey
This commit is contained in:
Slava Zanko 2008-12-29 00:50:42 +02:00
parent 55be4b67cb
commit 9810380e9a
3 changed files with 54 additions and 54 deletions

View File

@ -139,9 +139,9 @@ static void set_perm_by_flags (char *s, int f_p)
static void update_permissions (void) static void update_permissions (void)
{ {
set_perm_by_flags (b_att[0]->text, 0); set_perm_by_flags (b_att[0]->text.start, 0);
set_perm_by_flags (b_att[1]->text, 3); set_perm_by_flags (b_att[1]->text.start, 3);
set_perm_by_flags (b_att[2]->text, 6); set_perm_by_flags (b_att[2]->text.start, 6);
} }
static mode_t get_perm (char *s, int base) static mode_t get_perm (char *s, int base)
@ -484,7 +484,7 @@ advanced_chown_callback (Dlg_head *h, dlg_msg_t msg, int parm)
if (f_pos > 2) if (f_pos > 2)
break; break;
flag_pos = f_pos * 3 + i; /* (strchr(ch_perm,parm)-ch_perm); */ flag_pos = f_pos * 3 + i; /* (strchr(ch_perm,parm)-ch_perm); */
if (((WButton *) h->current)->text[(flag_pos % 3)] == if (((WButton *) h->current)->text.start[(flag_pos % 3)] ==
'-') '-')
ch_flags[flag_pos] = '+'; ch_flags[flag_pos] = '+';
else else

View File

@ -132,7 +132,6 @@ static cb_ret_t
button_callback (Widget *w, widget_msg_t msg, int parm) button_callback (Widget *w, widget_msg_t msg, int parm)
{ {
WButton *b = (WButton *) w; WButton *b = (WButton *) w;
char buf[BUF_SMALL];
int stop = 0; int stop = 0;
int off = 0; int off = 0;
Dlg_head *h = b->widget.parent; Dlg_head *h = b->widget.parent;
@ -155,11 +154,14 @@ button_callback (Widget *w, widget_msg_t msg, int parm)
return MSG_HANDLED; return MSG_HANDLED;
} }
if (b->hotkey == tolower (parm)) { if (b->text.hotkey != NULL) {
if (g_ascii_tolower ((gchar)b->text.hotkey[0]) ==
g_ascii_tolower ((gchar)parm)) {
button_callback (w, WIDGET_KEY, ' '); button_callback (w, WIDGET_KEY, ' ');
return MSG_HANDLED; return MSG_HANDLED;
} }
}
return MSG_NOT_HANDLED; return MSG_NOT_HANDLED;
case WIDGET_KEY: case WIDGET_KEY:
@ -201,40 +203,52 @@ button_callback (Widget *w, widget_msg_t msg, int parm)
else if (msg == WIDGET_FOCUS) else if (msg == WIDGET_FOCUS)
b->selected = 1; b->selected = 1;
widget_selectcolor (w, b->selected, FALSE);
widget_move (w, 0, 0);
switch (b->flags) { switch (b->flags) {
case DEFPUSH_BUTTON: case DEFPUSH_BUTTON:
g_snprintf (buf, sizeof (buf), "[< %s >]", b->text); addstr ("[< ");
off = 3;
break; break;
case NORMAL_BUTTON: case NORMAL_BUTTON:
g_snprintf (buf, sizeof (buf), "[ %s ]", b->text); addstr ("[ ");
off = 2;
break; break;
case NARROW_BUTTON: case NARROW_BUTTON:
g_snprintf (buf, sizeof (buf), "[%s]", b->text); addstr ("[");
off = 1;
break; break;
case HIDDEN_BUTTON: case HIDDEN_BUTTON:
default: default:
buf[0] = '\0'; return MSG_HANDLED;
off = 0;
break;
} }
addstr (str_term_form (b->text.start));
if (b->text.hotkey != NULL) {
widget_selectcolor (w, b->selected, TRUE);
addstr (str_term_form (b->text.hotkey));
widget_selectcolor (w, b->selected, FALSE); widget_selectcolor (w, b->selected, FALSE);
widget_move (w, 0, 0); }
addstr (buf); if (b->text.end != NULL) {
addstr (str_term_form (b->text.end));
}
if (b->hotpos >= 0) { switch (b->flags) {
widget_selectcolor (w, b->selected, TRUE); case DEFPUSH_BUTTON:
widget_move (w, 0, b->hotpos + off); addstr (" >]");
addch ((unsigned char) b->text[b->hotpos]); break;
case NORMAL_BUTTON:
addstr (" ]");
break;
case NARROW_BUTTON:
addstr ("]");
break;
} }
return MSG_HANDLED; return MSG_HANDLED;
case WIDGET_DESTROY: case WIDGET_DESTROY:
g_free (b->text); release_hotkey (b->text);
return MSG_HANDLED; return MSG_HANDLED;
default: default:
@ -260,10 +274,10 @@ button_event (Gpm_Event *event, void *data)
} }
static int static int
button_len (const char *text, unsigned int flags) button_len (const struct hotkey_t text, unsigned int flags)
{ {
int ret = strlen (text); int ret = hotkey_width (text);
switch (flags){ switch (flags) {
case DEFPUSH_BUTTON: case DEFPUSH_BUTTON:
ret += 6; ret += 6;
break; break;
@ -280,57 +294,43 @@ button_len (const char *text, unsigned int flags)
return ret; return ret;
} }
/*
* Locate the hotkey and remove it from the button text. Assuming that
* the button text is g_malloc()ed, we can safely change and shorten it.
*/
static void
button_scan_hotkey (WButton *b)
{
char *cp = strchr (b->text, '&');
if (cp != NULL && cp[1] != '\0') {
g_strlcpy (cp, cp + 1, strlen (cp));
b->hotkey = tolower ((unsigned char) *cp);
b->hotpos = cp - b->text;
}
}
WButton * WButton *
button_new (int y, int x, int action, int flags, const char *text, button_new (int y, int x, int action, int flags, const char *text,
bcback callback) bcback callback)
{ {
WButton *b = g_new (WButton, 1); WButton *b = g_new (WButton, 1);
init_widget (&b->widget, y, x, 1, button_len (text, flags), b->text = parse_hotkey (text);
init_widget (&b->widget, y, x, 1, button_len (b->text, flags),
button_callback, button_event); button_callback, button_event);
b->action = action; b->action = action;
b->flags = flags; b->flags = flags;
b->selected = 0; b->selected = 0;
b->text = g_strdup (text);
b->callback = callback; b->callback = callback;
widget_want_hotkey (b->widget, 1); widget_want_hotkey (b->widget, 1);
b->hotkey = 0; b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
b->hotpos = -1;
button_scan_hotkey(b);
return b; return b;
} }
const char * const char *
button_get_text (WButton *b) button_get_text (WButton *b)
{ {
return b->text; if (b->text.hotkey != NULL)
return g_strconcat (b->text.start, "&", b->text.hotkey,
b->text.end, NULL);
else
return g_strdup (b->text.start);
} }
void void
button_set_text (WButton *b, const char *text) button_set_text (WButton *b, const char *text)
{ {
g_free (b->text); release_hotkey (b->text);
b->text = g_strdup (text); b->text = parse_hotkey (text);
b->widget.cols = button_len (text, b->flags); b->widget.cols = button_len (b->text, b->flags);
button_scan_hotkey(b);
dlg_redraw (b->widget.parent); dlg_redraw (b->widget.parent);
} }

View File

@ -39,8 +39,7 @@ typedef struct WButton {
#define NORMAL_BUTTON 2 #define NORMAL_BUTTON 2
#define DEFPUSH_BUTTON 3 #define DEFPUSH_BUTTON 3
unsigned int flags; /* button flags */ unsigned int flags; /* button flags */
char *text; /* text of button */ struct hotkey_t text; /* text of button, contain hotkey too */
int hotkey; /* hot KEY */
int hotpos; /* offset hot KEY char in text */ int hotpos; /* offset hot KEY char in text */
bcback callback; /* Callback function */ bcback callback; /* Callback function */
} WButton; } WButton;
@ -177,6 +176,7 @@ void gauge_set_value (WGauge *g, int max, int current);
void gauge_show (WGauge *g, int shown); void gauge_show (WGauge *g, int shown);
/* Buttons */ /* Buttons */
/* return copy of button text */
const char *button_get_text (WButton *b); const char *button_get_text (WButton *b);
void button_set_text (WButton *b, const char *text); void button_set_text (WButton *b, const char *text);