Remove unneeded `struct` keyword for typedef'd structs
[midnight-commander.git] / lib / widget / button.c
blobfe5be570e1274ee3f17fe17c0666c2fbd391699e
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
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/tty/mouse.h"
43 #include "lib/strutil.h"
44 #include "lib/widget.h"
46 /*** global variables ****************************************************************************/
48 /*** file scope macro definitions ****************************************************************/
50 /*** file scope type declarations ****************************************************************/
52 /*** file scope variables ************************************************************************/
54 /*** file scope functions ************************************************************************/
56 static cb_ret_t
57 button_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
59 WButton *b = BUTTON (w);
60 WDialog *h = w->owner;
61 int off = 0;
63 switch (msg)
65 case MSG_HOTKEY:
67 * Don't let the default button steal Enter from the current
68 * button. This is a workaround for the flawed event model
69 * when hotkeys are sent to all widgets before the key is
70 * handled by the current widget.
72 if (parm == '\n' && WIDGET (h->current->data) == WIDGET (b))
74 send_message (w, sender, MSG_KEY, ' ', data);
75 return MSG_HANDLED;
78 if (parm == '\n' && b->flags == DEFPUSH_BUTTON)
80 send_message (w, sender, MSG_KEY, ' ', data);
81 return MSG_HANDLED;
84 if (b->text.hotkey != NULL && g_ascii_tolower ((gchar) b->text.hotkey[0]) == parm)
86 send_message (w, sender, MSG_KEY, ' ', data);
87 return MSG_HANDLED;
89 return MSG_NOT_HANDLED;
91 case MSG_KEY:
92 if (parm != ' ' && parm != '\n')
93 return MSG_NOT_HANDLED;
95 h->ret_value = b->action;
96 if (b->callback == NULL || b->callback (b, b->action) != 0)
97 dlg_stop (h);
99 return MSG_HANDLED;
101 case MSG_CURSOR:
102 switch (b->flags)
104 case DEFPUSH_BUTTON:
105 off = 3;
106 break;
107 case NORMAL_BUTTON:
108 off = 2;
109 break;
110 case NARROW_BUTTON:
111 off = 1;
112 break;
113 case HIDDEN_BUTTON:
114 default:
115 off = 0;
116 break;
118 widget_move (w, 0, b->hotpos + off);
119 return MSG_HANDLED;
121 case MSG_UNFOCUS:
122 case MSG_FOCUS:
123 case MSG_DRAW:
124 if (msg == MSG_UNFOCUS)
125 b->selected = FALSE;
126 else if (msg == MSG_FOCUS)
127 b->selected = TRUE;
129 widget_selectcolor (w, b->selected, FALSE);
130 widget_move (w, 0, 0);
132 switch (b->flags)
134 case DEFPUSH_BUTTON:
135 tty_print_string ("[< ");
136 break;
137 case NORMAL_BUTTON:
138 tty_print_string ("[ ");
139 break;
140 case NARROW_BUTTON:
141 tty_print_string ("[");
142 break;
143 case HIDDEN_BUTTON:
144 default:
145 return MSG_HANDLED;
148 hotkey_draw (w, b->text, b->selected);
150 switch (b->flags)
152 case DEFPUSH_BUTTON:
153 tty_print_string (" >]");
154 break;
155 case NORMAL_BUTTON:
156 tty_print_string (" ]");
157 break;
158 case NARROW_BUTTON:
159 tty_print_string ("]");
160 break;
161 default:
162 break;
164 return MSG_HANDLED;
166 case MSG_DESTROY:
167 release_hotkey (b->text);
168 return MSG_HANDLED;
170 default:
171 return widget_default_callback (w, sender, msg, parm, data);
175 /* --------------------------------------------------------------------------------------------- */
177 static int
178 button_event (Gpm_Event * event, void *data)
180 Widget *w = WIDGET (data);
182 if (!mouse_global_in_widget (event, w))
183 return MOU_UNHANDLED;
185 if ((event->type & (GPM_DOWN | GPM_UP)) != 0)
187 dlg_select_widget (w);
188 if ((event->type & GPM_UP) != 0)
190 send_message (w, NULL, MSG_KEY, ' ', NULL);
191 send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
195 return MOU_NORMAL;
198 /* --------------------------------------------------------------------------------------------- */
199 /*** public functions ****************************************************************************/
200 /* --------------------------------------------------------------------------------------------- */
202 WButton *
203 button_new (int y, int x, int action, button_flags_t flags, const char *text, bcback_fn callback)
205 WButton *b;
206 Widget *w;
208 b = g_new (WButton, 1);
209 w = WIDGET (b);
211 b->action = action;
212 b->flags = flags;
213 b->text = parse_hotkey (text);
214 widget_init (w, y, x, 1, button_get_len (b), button_callback, button_event);
215 b->selected = FALSE;
216 b->callback = callback;
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 /* --------------------------------------------------------------------------------------------- */