Ticket #3571: high-level mouse API.
[midnight-commander.git] / lib / widget / widget-common.c
blobee8a208f6faa552b9111d6fe1d137cc35fe14712
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, mouse_h mouse_handler)
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 = mouse_handler;
150 w->set_options = widget_default_set_options_callback;
151 w->owner = NULL;
152 w->Mouse.callback = NULL; /* it will be overriden in set_easy_mouse_callback() */
153 w->Mouse.capture = FALSE;
154 w->Mouse.forced_capture = FALSE;
156 /* Almost all widgets want to put the cursor in a suitable place */
157 w->options = W_WANT_CURSOR;
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_DRAW:
177 case MSG_DESTROY:
178 case MSG_CURSOR:
179 case MSG_IDLE:
180 return MSG_HANDLED;
182 default:
183 return MSG_NOT_HANDLED;
187 /* --------------------------------------------------------------------------------------------- */
190 * Callback for applying new options to widget.
192 * @param w widget
193 * @param options options set
194 * @param enable TRUE if specified options should be added, FALSE if options should be removed
196 void
197 widget_default_set_options_callback (Widget * w, widget_options_t options, gboolean enable)
199 if (enable)
200 w->options |= options;
201 else
202 w->options &= ~options;
204 if (w->owner != NULL && (options & W_DISABLED) != 0)
205 send_message (w, NULL, MSG_DRAW, 0, NULL);
208 /* --------------------------------------------------------------------------------------------- */
211 * Apply new options to widget.
213 * @param w widget
214 * @param options options set
215 * @param enable TRUE if specified options should be added, FALSE if options should be removed
217 void
218 widget_set_options (Widget * w, widget_options_t options, gboolean enable)
220 w->set_options (w, options, enable);
223 /* --------------------------------------------------------------------------------------------- */
225 void
226 widget_set_size (Widget * widget, int y, int x, int lines, int cols)
228 widget->x = x;
229 widget->y = y;
230 widget->cols = cols;
231 widget->lines = lines;
232 send_message (widget, NULL, MSG_RESIZE, 0, NULL);
235 /* --------------------------------------------------------------------------------------------- */
237 void
238 widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey)
240 WDialog *h = w->owner;
241 int color;
243 if ((w->options & W_DISABLED) != 0)
244 color = DISABLED_COLOR;
245 else if (hotkey)
247 if (focused)
248 color = h->color[DLG_COLOR_HOT_FOCUS];
249 else
250 color = h->color[DLG_COLOR_HOT_NORMAL];
252 else
254 if (focused)
255 color = h->color[DLG_COLOR_FOCUS];
256 else
257 color = h->color[DLG_COLOR_NORMAL];
260 tty_setcolor (color);
263 /* --------------------------------------------------------------------------------------------- */
265 void
266 widget_erase (Widget * w)
268 if (w != NULL)
269 tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
272 /* --------------------------------------------------------------------------------------------- */
274 * Check whether widget is active or not.
275 * @param w the widget
277 * @return TRUE if the widget is active, FALSE otherwise
280 gboolean
281 widget_is_active (const void *w)
283 return (w == WIDGET (w)->owner->current->data);
286 /* --------------------------------------------------------------------------------------------- */
288 void
289 widget_redraw (Widget * w)
291 if (w != NULL)
293 WDialog *h = w->owner;
295 if (h != NULL && h->state == DLG_ACTIVE)
296 w->callback (w, NULL, MSG_DRAW, 0, NULL);
300 /* --------------------------------------------------------------------------------------------- */
302 * Replace widget in the dialog.
304 * @param old_w old widget that need to be replaced
305 * @param new_w new widget that will replace @old_w
308 void
309 widget_replace (Widget * old_w, Widget * new_w)
311 WDialog *h = old_w->owner;
312 gboolean should_focus = FALSE;
314 if (h->widgets == NULL)
315 return;
317 if (h->current == NULL)
318 h->current = h->widgets;
320 if (old_w == h->current->data)
321 should_focus = TRUE;
323 new_w->owner = h;
324 new_w->id = old_w->id;
326 if (should_focus)
327 h->current->data = new_w;
328 else
329 g_list_find (h->widgets, old_w)->data = new_w;
331 send_message (old_w, NULL, MSG_DESTROY, 0, NULL);
332 send_message (new_w, NULL, MSG_INIT, 0, NULL);
334 if (should_focus)
335 dlg_select_widget (new_w);
337 widget_redraw (new_w);
340 /* --------------------------------------------------------------------------------------------- */
342 * Check whether two widgets are overlapped or not.
343 * @param a 1st widget
344 * @param b 2nd widget
346 * @return TRUE if widgets are overlapped, FALSE otherwise.
349 gboolean
350 widget_overlapped (const Widget * a, const Widget * b)
352 return !((b->x >= a->x + a->cols)
353 || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
356 /* --------------------------------------------------------------------------------------------- */
357 /* get mouse pointer location within widget */
359 Gpm_Event
360 mouse_get_local (const Gpm_Event * global, const Widget * w)
362 Gpm_Event local;
364 local.buttons = global->buttons;
365 #ifdef HAVE_LIBGPM
366 local.modifiers = 0;
367 #endif
368 local.x = global->x - w->x;
369 local.y = global->y - w->y;
370 local.type = global->type;
372 return local;
375 /* --------------------------------------------------------------------------------------------- */
377 gboolean
378 mouse_global_in_widget (const Gpm_Event * event, const Widget * w)
380 return (event->x > w->x) && (event->y > w->y) && (event->x <= w->x + w->cols)
381 && (event->y <= w->y + w->lines);
384 /* --------------------------------------------------------------------------------------------- */