0c1b5fc9055ed028aa9bcfeaa9772c129a660da0
[midnight-commander.git] / lib / widget / button.c
blob0c1b5fc9055ed028aa9bcfeaa9772c129a660da0
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
5 2004, 2005, 2006, 2007, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 Authors:
9 Radek Doulik, 1994, 1995
10 Miguel de Icaza, 1994, 1995
11 Jakub Jelinek, 1995
12 Andrej Borsenkow, 1996
13 Norbert Warmuth, 1997
14 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
16 This file is part of the Midnight Commander.
18 The Midnight Commander is free software: you can redistribute it
19 and/or modify it under the terms of the GNU General Public License as
20 published by the Free Software Foundation, either version 3 of the License,
21 or (at your option) any later version.
23 The Midnight Commander is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 /** \file button.c
33 * \brief Source: WButton widget
36 #include <config.h>
38 #include <stdlib.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/mouse.h"
44 #include "lib/strutil.h"
45 #include "lib/widget.h"
47 /*** global variables ****************************************************************************/
49 /*** file scope macro definitions ****************************************************************/
51 /*** file scope type declarations ****************************************************************/
53 /*** file scope variables ************************************************************************/
55 /*** file scope functions ************************************************************************/
57 static cb_ret_t
58 button_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
60 WButton *b = BUTTON (w);
61 WDialog *h = w->owner;
62 int stop = 0;
63 int off = 0;
65 switch (msg)
67 case MSG_HOTKEY:
69 * Don't let the default button steal Enter from the current
70 * button. This is a workaround for the flawed event model
71 * when hotkeys are sent to all widgets before the key is
72 * handled by the current widget.
74 if (parm == '\n' && WIDGET (h->current->data) == WIDGET (b))
76 send_message (w, sender, MSG_KEY, ' ', data);
77 return MSG_HANDLED;
80 if (parm == '\n' && b->flags == DEFPUSH_BUTTON)
82 send_message (w, sender, MSG_KEY, ' ', data);
83 return MSG_HANDLED;
86 if (b->text.hotkey != NULL && g_ascii_tolower ((gchar) b->text.hotkey[0]) == parm)
88 send_message (w, sender, MSG_KEY, ' ', data);
89 return MSG_HANDLED;
91 return MSG_NOT_HANDLED;
93 case MSG_KEY:
94 if (parm != ' ' && parm != '\n')
95 return MSG_NOT_HANDLED;
97 if (b->callback != NULL)
98 stop = b->callback (b, b->action);
99 if (b->callback == NULL || stop != 0)
101 h->ret_value = b->action;
102 dlg_stop (h);
104 return MSG_HANDLED;
106 case MSG_CURSOR:
107 switch (b->flags)
109 case DEFPUSH_BUTTON:
110 off = 3;
111 break;
112 case NORMAL_BUTTON:
113 off = 2;
114 break;
115 case NARROW_BUTTON:
116 off = 1;
117 break;
118 case HIDDEN_BUTTON:
119 default:
120 off = 0;
121 break;
123 widget_move (w, 0, b->hotpos + off);
124 return MSG_HANDLED;
126 case MSG_UNFOCUS:
127 case MSG_FOCUS:
128 case MSG_DRAW:
129 if (msg == MSG_UNFOCUS)
130 b->selected = FALSE;
131 else if (msg == MSG_FOCUS)
132 b->selected = TRUE;
134 widget_selectcolor (w, b->selected, FALSE);
135 widget_move (w, 0, 0);
137 switch (b->flags)
139 case DEFPUSH_BUTTON:
140 tty_print_string ("[< ");
141 break;
142 case NORMAL_BUTTON:
143 tty_print_string ("[ ");
144 break;
145 case NARROW_BUTTON:
146 tty_print_string ("[");
147 break;
148 case HIDDEN_BUTTON:
149 default:
150 return MSG_HANDLED;
153 hotkey_draw (w, b->text, b->selected);
155 switch (b->flags)
157 case DEFPUSH_BUTTON:
158 tty_print_string (" >]");
159 break;
160 case NORMAL_BUTTON:
161 tty_print_string (" ]");
162 break;
163 case NARROW_BUTTON:
164 tty_print_string ("]");
165 break;
166 default:
167 break;
169 return MSG_HANDLED;
171 case MSG_DESTROY:
172 release_hotkey (b->text);
173 return MSG_HANDLED;
175 default:
176 return widget_default_callback (w, sender, msg, parm, data);
180 /* --------------------------------------------------------------------------------------------- */
182 static int
183 button_event (Gpm_Event * event, void *data)
185 Widget *w = WIDGET (data);
187 if (!mouse_global_in_widget (event, w))
188 return MOU_UNHANDLED;
190 if ((event->type & (GPM_DOWN | GPM_UP)) != 0)
192 dlg_select_widget (w);
193 if ((event->type & GPM_UP) != 0)
195 send_message (w, NULL, MSG_KEY, ' ', NULL);
196 send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
200 return MOU_NORMAL;
203 /* --------------------------------------------------------------------------------------------- */
204 /*** public functions ****************************************************************************/
205 /* --------------------------------------------------------------------------------------------- */
207 WButton *
208 button_new (int y, int x, int action, button_flags_t flags, const char *text, bcback_fn callback)
210 WButton *b;
211 Widget *w;
213 b = g_new (WButton, 1);
214 w = WIDGET (b);
216 b->action = action;
217 b->flags = flags;
218 b->text = parse_hotkey (text);
219 init_widget (w, y, x, 1, button_get_len (b), button_callback, button_event);
220 b->selected = FALSE;
221 b->callback = callback;
222 widget_want_hotkey (w, TRUE);
223 b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
225 return b;
228 /* --------------------------------------------------------------------------------------------- */
230 const char *
231 button_get_text (const WButton * b)
233 if (b->text.hotkey != NULL)
234 return g_strconcat (b->text.start, "&", b->text.hotkey, b->text.end, (char *) NULL);
235 return g_strdup (b->text.start);
238 /* --------------------------------------------------------------------------------------------- */
240 void
241 button_set_text (WButton * b, const char *text)
243 Widget *w = WIDGET (b);
245 release_hotkey (b->text);
246 b->text = parse_hotkey (text);
247 w->cols = button_get_len (b);
248 if (w->owner != NULL)
249 send_message (w, NULL, MSG_DRAW, 0, NULL);
252 /* --------------------------------------------------------------------------------------------- */
255 button_get_len (const WButton * b)
257 int ret = hotkey_width (b->text);
259 switch (b->flags)
261 case DEFPUSH_BUTTON:
262 ret += 6;
263 break;
264 case NORMAL_BUTTON:
265 ret += 4;
266 break;
267 case NARROW_BUTTON:
268 ret += 2;
269 break;
270 case HIDDEN_BUTTON:
271 default:
272 return 0;
275 return ret;
278 /* --------------------------------------------------------------------------------------------- */