(widget_set_state): return cb_ret_t instead of gboolean.
[midnight-commander.git] / lib / widget / widget-common.c
blob3382e962a682e02f78caf6e98ea63d7dee2c5036
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, 2011, 2012, 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 widget-common.c
32 * \brief Source: shared stuff of widgets
35 #include <config.h>
37 #include <stdlib.h>
38 #include <string.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/color.h"
44 #include "lib/skin.h"
45 #include "lib/strutil.h"
46 #include "lib/widget.h"
48 /*** global variables ****************************************************************************/
50 /*** file scope macro definitions ****************************************************************/
52 /*** file scope type declarations ****************************************************************/
54 /*** file scope variables ************************************************************************/
56 /*** file scope functions ************************************************************************/
58 /* --------------------------------------------------------------------------------------------- */
59 /*** public functions ****************************************************************************/
60 /* --------------------------------------------------------------------------------------------- */
62 struct hotkey_t
63 parse_hotkey (const char *text)
65 hotkey_t result;
66 const char *cp, *p;
68 if (text == NULL)
69 text = "";
71 /* search for '&', that is not on the of text */
72 cp = strchr (text, '&');
73 if (cp != NULL && cp[1] != '\0')
75 result.start = g_strndup (text, cp - text);
77 /* skip '&' */
78 cp++;
79 p = str_cget_next_char (cp);
80 result.hotkey = g_strndup (cp, p - cp);
82 cp = p;
83 result.end = g_strdup (cp);
85 else
87 result.start = g_strdup (text);
88 result.hotkey = NULL;
89 result.end = NULL;
92 return result;
95 /* --------------------------------------------------------------------------------------------- */
97 void
98 release_hotkey (const hotkey_t hotkey)
100 g_free (hotkey.start);
101 g_free (hotkey.hotkey);
102 g_free (hotkey.end);
105 /* --------------------------------------------------------------------------------------------- */
108 hotkey_width (const hotkey_t hotkey)
110 int result;
112 result = str_term_width1 (hotkey.start);
113 result += (hotkey.hotkey != NULL) ? str_term_width1 (hotkey.hotkey) : 0;
114 result += (hotkey.end != NULL) ? str_term_width1 (hotkey.end) : 0;
115 return result;
118 /* --------------------------------------------------------------------------------------------- */
120 void
121 hotkey_draw (Widget * w, const hotkey_t hotkey, gboolean focused)
123 widget_selectcolor (w, focused, FALSE);
124 tty_print_string (hotkey.start);
126 if (hotkey.hotkey != NULL)
128 widget_selectcolor (w, focused, TRUE);
129 tty_print_string (hotkey.hotkey);
130 widget_selectcolor (w, focused, FALSE);
133 if (hotkey.end != NULL)
134 tty_print_string (hotkey.end);
137 /* --------------------------------------------------------------------------------------------- */
139 void
140 widget_init (Widget * w, int y, int x, int lines, int cols,
141 widget_cb_fn callback, widget_mouse_cb_fn mouse_callback)
143 w->x = x;
144 w->y = y;
145 w->cols = cols;
146 w->lines = lines;
147 w->pos_flags = WPOS_KEEP_DEFAULT;
148 w->callback = callback;
149 w->mouse_callback = mouse_callback;
150 w->owner = NULL;
151 w->mouse.forced_capture = FALSE;
152 w->mouse.capture = FALSE;
153 w->mouse.last_msg = MSG_MOUSE_NONE;
154 w->mouse.last_buttons_down = 0;
156 w->options = WOP_DEFAULT;
157 w->state = WST_DEFAULT;
160 /* --------------------------------------------------------------------------------------------- */
162 /* Default callback for widgets */
163 cb_ret_t
164 widget_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
166 (void) w;
167 (void) sender;
168 (void) parm;
169 (void) data;
171 switch (msg)
173 case MSG_INIT:
174 case MSG_FOCUS:
175 case MSG_UNFOCUS:
176 case MSG_ENABLE:
177 case MSG_DISABLE:
178 case MSG_DRAW:
179 case MSG_DESTROY:
180 case MSG_CURSOR:
181 case MSG_IDLE:
182 return MSG_HANDLED;
184 default:
185 return MSG_NOT_HANDLED;
189 /* --------------------------------------------------------------------------------------------- */
192 * Apply new options to widget.
194 * @param w widget
195 * @param options widget option flags to modify. Several flags per call can be modified.
196 * @param enable TRUE if specified options should be added, FALSE if options should be removed
198 void
199 widget_set_options (Widget * w, widget_options_t options, gboolean enable)
201 if (enable)
202 w->options |= options;
203 else
204 w->options &= ~options;
207 /* --------------------------------------------------------------------------------------------- */
210 * Modify state of widget.
212 * @param w widget
213 * @param state widget state flag to modify
214 * @param enable specifies whether to turn the flag on (TRUE) or off (FALSE).
215 * Only one flag per call can be modified.
216 * @return MSG_HANDLED if set was handled successfully, MSG_NOT_HANDLED otherwise.
218 cb_ret_t
219 widget_set_state (Widget * w, widget_state_t state, gboolean enable)
221 gboolean ret = MSG_HANDLED;
223 if (enable)
224 w->state |= state;
225 else
226 w->state &= ~state;
228 if (enable)
230 /* exclusive bits */
231 if ((state & WST_CONSTRUCT) != 0)
232 w->state &= ~(WST_ACTIVE | WST_SUSPENDED | WST_CLOSED);
233 else if ((state & WST_ACTIVE) != 0)
234 w->state &= ~(WST_CONSTRUCT | WST_SUSPENDED | WST_CLOSED);
235 else if ((state & WST_SUSPENDED) != 0)
236 w->state &= ~(WST_CONSTRUCT | WST_ACTIVE | WST_CLOSED);
237 else if ((state & WST_CLOSED) != 0)
238 w->state &= ~(WST_CONSTRUCT | WST_ACTIVE | WST_SUSPENDED);
241 if (w->owner == NULL)
242 return MSG_NOT_HANDLED;
244 switch (state)
246 case WST_DISABLED:
247 ret = send_message (w, NULL, enable ? MSG_DISABLE : MSG_ENABLE, 0, NULL);
248 if (ret == MSG_HANDLED)
249 ret = send_message (w, NULL, MSG_DRAW, 0, NULL);
250 break;
252 default:
253 break;
256 return ret;
259 /* --------------------------------------------------------------------------------------------- */
261 void
262 widget_set_size (Widget * widget, int y, int x, int lines, int cols)
264 widget->x = x;
265 widget->y = y;
266 widget->cols = cols;
267 widget->lines = lines;
268 send_message (widget, NULL, MSG_RESIZE, 0, NULL);
271 /* --------------------------------------------------------------------------------------------- */
273 void
274 widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey)
276 WDialog *h = w->owner;
277 int color;
279 if (widget_get_state (w, WST_DISABLED))
280 color = DISABLED_COLOR;
281 else if (hotkey)
283 if (focused)
284 color = h->color[DLG_COLOR_HOT_FOCUS];
285 else
286 color = h->color[DLG_COLOR_HOT_NORMAL];
288 else
290 if (focused)
291 color = h->color[DLG_COLOR_FOCUS];
292 else
293 color = h->color[DLG_COLOR_NORMAL];
296 tty_setcolor (color);
299 /* --------------------------------------------------------------------------------------------- */
301 void
302 widget_erase (Widget * w)
304 if (w != NULL)
305 tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
308 /* --------------------------------------------------------------------------------------------- */
310 * Check whether widget is active or not.
311 * @param w the widget
313 * @return TRUE if the widget is active, FALSE otherwise
316 gboolean
317 widget_is_active (const void *w)
319 return (w == CONST_WIDGET (w)->owner->current->data);
322 /* --------------------------------------------------------------------------------------------- */
324 void
325 widget_redraw (Widget * w)
327 if (w != NULL)
329 WDialog *h = w->owner;
331 if (h != NULL && widget_get_state (WIDGET (h), WST_ACTIVE))
332 w->callback (w, NULL, MSG_DRAW, 0, NULL);
336 /* --------------------------------------------------------------------------------------------- */
338 * Replace widget in the dialog.
340 * @param old_w old widget that need to be replaced
341 * @param new_w new widget that will replace @old_w
344 void
345 widget_replace (Widget * old_w, Widget * new_w)
347 WDialog *h = old_w->owner;
348 gboolean should_focus = FALSE;
350 if (h->widgets == NULL)
351 return;
353 if (h->current == NULL)
354 h->current = h->widgets;
356 if (old_w == h->current->data)
357 should_focus = TRUE;
359 new_w->owner = h;
360 new_w->id = old_w->id;
362 if (should_focus)
363 h->current->data = new_w;
364 else
365 g_list_find (h->widgets, old_w)->data = new_w;
367 send_message (old_w, NULL, MSG_DESTROY, 0, NULL);
368 send_message (new_w, NULL, MSG_INIT, 0, NULL);
370 if (should_focus)
371 dlg_select_widget (new_w);
373 widget_redraw (new_w);
376 /* --------------------------------------------------------------------------------------------- */
378 * Check whether two widgets are overlapped or not.
379 * @param a 1st widget
380 * @param b 2nd widget
382 * @return TRUE if widgets are overlapped, FALSE otherwise.
385 gboolean
386 widget_overlapped (const Widget * a, const Widget * b)
388 return !((b->x >= a->x + a->cols)
389 || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
392 /* --------------------------------------------------------------------------------------------- */
393 /* get mouse pointer location within widget */
395 Gpm_Event
396 mouse_get_local (const Gpm_Event * global, const Widget * w)
398 Gpm_Event local;
400 local.buttons = global->buttons;
401 #ifdef HAVE_LIBGPM
402 local.clicks = 0;
403 local.margin = 0;
404 local.modifiers = 0;
405 local.vc = 0;
406 #endif
407 local.x = global->x - w->x;
408 local.y = global->y - w->y;
409 local.type = global->type;
411 return local;
414 /* --------------------------------------------------------------------------------------------- */
416 gboolean
417 mouse_global_in_widget (const Gpm_Event * event, const Widget * w)
419 return (event->x > w->x) && (event->y > w->y) && (event->x <= w->x + w->cols)
420 && (event->y <= w->y + w->lines);
423 /* --------------------------------------------------------------------------------------------- */