By default, WOP_WANT_HOTKEY option is off.
[midnight-commander.git] / lib / widget / button.c
blob36b0ab100a237d0f79393b1a1203c2c0e3156ece
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2016
5 Free Software Foundation, Inc.
7 Authors:
8 Radek Doulik, 1994, 1995
9 Miguel de Icaza, 1994, 1995
10 Jakub Jelinek, 1995
11 Andrej Borsenkow, 1996
12 Norbert Warmuth, 1997
13 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013, 2016
15 This file is part of the Midnight Commander.
17 The Midnight Commander is free software: you can redistribute it
18 and/or modify it under the terms of the GNU General Public License as
19 published by the Free Software Foundation, either version 3 of the License,
20 or (at your option) any later version.
22 The Midnight Commander is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 /** \file button.c
32 * \brief Source: WButton widget
35 #include <config.h>
37 #include <stdlib.h>
39 #include "lib/global.h"
41 #include "lib/tty/tty.h"
42 #include "lib/strutil.h"
43 #include "lib/widget.h"
45 /*** global variables ****************************************************************************/
47 /*** file scope macro definitions ****************************************************************/
49 /*** file scope type declarations ****************************************************************/
51 /*** file scope variables ************************************************************************/
53 /*** file scope functions ************************************************************************/
55 static cb_ret_t
56 button_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
58 WButton *b = BUTTON (w);
59 WDialog *h = w->owner;
60 int off = 0;
62 switch (msg)
64 case MSG_HOTKEY:
66 * Don't let the default button steal Enter from the current
67 * button. This is a workaround for the flawed event model
68 * when hotkeys are sent to all widgets before the key is
69 * handled by the current widget.
71 if (parm == '\n' && WIDGET (h->current->data) == WIDGET (b))
73 send_message (w, sender, MSG_KEY, ' ', data);
74 return MSG_HANDLED;
77 if (parm == '\n' && b->flags == DEFPUSH_BUTTON)
79 send_message (w, sender, MSG_KEY, ' ', data);
80 return MSG_HANDLED;
83 if (b->text.hotkey != NULL && g_ascii_tolower ((gchar) b->text.hotkey[0]) == parm)
85 send_message (w, sender, MSG_KEY, ' ', data);
86 return MSG_HANDLED;
88 return MSG_NOT_HANDLED;
90 case MSG_KEY:
91 if (parm != ' ' && parm != '\n')
92 return MSG_NOT_HANDLED;
94 h->ret_value = b->action;
95 if (b->callback == NULL || b->callback (b, b->action) != 0)
96 dlg_stop (h);
98 return MSG_HANDLED;
100 case MSG_CURSOR:
101 switch (b->flags)
103 case DEFPUSH_BUTTON:
104 off = 3;
105 break;
106 case NORMAL_BUTTON:
107 off = 2;
108 break;
109 case NARROW_BUTTON:
110 off = 1;
111 break;
112 case HIDDEN_BUTTON:
113 default:
114 off = 0;
115 break;
117 widget_move (w, 0, b->hotpos + off);
118 return MSG_HANDLED;
120 case MSG_UNFOCUS:
121 case MSG_FOCUS:
122 case MSG_DRAW:
123 if (msg == MSG_UNFOCUS)
124 b->selected = FALSE;
125 else if (msg == MSG_FOCUS)
126 b->selected = TRUE;
128 widget_selectcolor (w, b->selected, FALSE);
129 widget_move (w, 0, 0);
131 switch (b->flags)
133 case DEFPUSH_BUTTON:
134 tty_print_string ("[< ");
135 break;
136 case NORMAL_BUTTON:
137 tty_print_string ("[ ");
138 break;
139 case NARROW_BUTTON:
140 tty_print_string ("[");
141 break;
142 case HIDDEN_BUTTON:
143 default:
144 return MSG_HANDLED;
147 hotkey_draw (w, b->text, b->selected);
149 switch (b->flags)
151 case DEFPUSH_BUTTON:
152 tty_print_string (" >]");
153 break;
154 case NORMAL_BUTTON:
155 tty_print_string (" ]");
156 break;
157 case NARROW_BUTTON:
158 tty_print_string ("]");
159 break;
160 default:
161 break;
163 return MSG_HANDLED;
165 case MSG_DESTROY:
166 release_hotkey (b->text);
167 return MSG_HANDLED;
169 default:
170 return widget_default_callback (w, sender, msg, parm, data);
174 /* --------------------------------------------------------------------------------------------- */
176 static void
177 button_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
179 (void) event;
181 switch (msg)
183 case MSG_MOUSE_DOWN:
184 dlg_select_widget (w);
185 break;
187 case MSG_MOUSE_CLICK:
188 send_message (w, NULL, MSG_KEY, ' ', NULL);
189 send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
190 break;
192 default:
193 break;
197 /* --------------------------------------------------------------------------------------------- */
198 /*** public functions ****************************************************************************/
199 /* --------------------------------------------------------------------------------------------- */
201 WButton *
202 button_new (int y, int x, int action, button_flags_t flags, const char *text, bcback_fn callback)
204 WButton *b;
205 Widget *w;
207 b = g_new (WButton, 1);
208 w = WIDGET (b);
210 b->action = action;
211 b->flags = flags;
212 b->text = parse_hotkey (text);
213 widget_init (w, y, x, 1, button_get_len (b), button_callback, button_mouse_callback);
214 b->selected = FALSE;
215 b->callback = callback;
216 widget_want_cursor (w, TRUE);
217 widget_want_hotkey (w, TRUE);
218 b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
220 return b;
223 /* --------------------------------------------------------------------------------------------- */
225 char *
226 button_get_text (const WButton * b)
228 if (b->text.hotkey != NULL)
229 return g_strconcat (b->text.start, "&", b->text.hotkey, b->text.end, (char *) NULL);
230 return g_strdup (b->text.start);
233 /* --------------------------------------------------------------------------------------------- */
235 void
236 button_set_text (WButton * b, const char *text)
238 Widget *w = WIDGET (b);
240 release_hotkey (b->text);
241 b->text = parse_hotkey (text);
242 b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
243 w->cols = button_get_len (b);
244 widget_redraw (w);
247 /* --------------------------------------------------------------------------------------------- */
250 button_get_len (const WButton * b)
252 int ret = hotkey_width (b->text);
254 switch (b->flags)
256 case DEFPUSH_BUTTON:
257 ret += 6;
258 break;
259 case NORMAL_BUTTON:
260 ret += 4;
261 break;
262 case NARROW_BUTTON:
263 ret += 2;
264 break;
265 case HIDDEN_BUTTON:
266 default:
267 return 0;
270 return ret;
273 /* --------------------------------------------------------------------------------------------- */