(dlg_overlap): rename to widget_overlapped()
[midnight-commander.git] / lib / widget / widget-common.c
blobab489cec58dd69b3a85b341779020dec76a3faa5
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, 2012, 2013
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, 2011, 2012, 2013
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 widget-common.c
33 * \brief Source: shared stuff of widgets
36 #include <config.h>
38 #include <stdlib.h>
39 #include <string.h>
41 #include "lib/global.h"
43 #include "lib/tty/tty.h"
44 #include "lib/tty/color.h"
45 #include "lib/skin.h"
46 #include "lib/strutil.h"
47 #include "lib/widget.h"
49 /*** global variables ****************************************************************************/
51 /*** file scope macro definitions ****************************************************************/
53 /*** file scope type declarations ****************************************************************/
55 /*** file scope variables ************************************************************************/
57 /*** file scope functions ************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
60 /*** public functions ****************************************************************************/
61 /* --------------------------------------------------------------------------------------------- */
63 struct hotkey_t
64 parse_hotkey (const char *text)
66 hotkey_t result;
67 const char *cp, *p;
69 if (text == NULL)
70 text = "";
72 /* search for '&', that is not on the of text */
73 cp = strchr (text, '&');
74 if (cp != NULL && cp[1] != '\0')
76 result.start = g_strndup (text, cp - text);
78 /* skip '&' */
79 cp++;
80 p = str_cget_next_char (cp);
81 result.hotkey = g_strndup (cp, p - cp);
83 cp = p;
84 result.end = g_strdup (cp);
86 else
88 result.start = g_strdup (text);
89 result.hotkey = NULL;
90 result.end = NULL;
93 return result;
96 /* --------------------------------------------------------------------------------------------- */
98 void
99 release_hotkey (const hotkey_t hotkey)
101 g_free (hotkey.start);
102 g_free (hotkey.hotkey);
103 g_free (hotkey.end);
106 /* --------------------------------------------------------------------------------------------- */
109 hotkey_width (const hotkey_t hotkey)
111 int result;
113 result = str_term_width1 (hotkey.start);
114 result += (hotkey.hotkey != NULL) ? str_term_width1 (hotkey.hotkey) : 0;
115 result += (hotkey.end != NULL) ? str_term_width1 (hotkey.end) : 0;
116 return result;
119 /* --------------------------------------------------------------------------------------------- */
121 void
122 hotkey_draw (Widget * w, const hotkey_t hotkey, gboolean focused)
124 widget_selectcolor (w, focused, FALSE);
125 tty_print_string (hotkey.start);
127 if (hotkey.hotkey != NULL)
129 widget_selectcolor (w, focused, TRUE);
130 tty_print_string (hotkey.hotkey);
131 widget_selectcolor (w, focused, FALSE);
134 if (hotkey.end != NULL)
135 tty_print_string (hotkey.end);
138 /* --------------------------------------------------------------------------------------------- */
140 void
141 init_widget (Widget * w, int y, int x, int lines, int cols,
142 widget_cb_fn callback, mouse_h mouse_handler)
144 w->x = x;
145 w->y = y;
146 w->cols = cols;
147 w->lines = lines;
148 w->callback = callback;
149 w->mouse = mouse_handler;
150 w->set_options = widget_default_set_options_callback;
151 w->owner = NULL;
153 /* Almost all widgets want to put the cursor in a suitable place */
154 w->options = W_WANT_CURSOR;
157 /* --------------------------------------------------------------------------------------------- */
159 /* Default callback for widgets */
160 cb_ret_t
161 widget_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
163 (void) w;
164 (void) sender;
165 (void) parm;
166 (void) data;
168 switch (msg)
170 case MSG_INIT:
171 case MSG_FOCUS:
172 case MSG_UNFOCUS:
173 case MSG_DRAW:
174 case MSG_DESTROY:
175 case MSG_CURSOR:
176 case MSG_IDLE:
177 return MSG_HANDLED;
179 default:
180 return MSG_NOT_HANDLED;
184 /* --------------------------------------------------------------------------------------------- */
187 * Callback for applying new options to widget.
189 * @param w widget
190 * @param options options set
191 * @param enable TRUE if specified options should be added, FALSE if options should be removed
193 void
194 widget_default_set_options_callback (Widget * w, widget_options_t options, gboolean enable)
196 if (enable)
197 w->options |= options;
198 else
199 w->options &= ~options;
201 if (w->owner != NULL && (options & W_DISABLED) != 0)
202 send_message (w, NULL, MSG_DRAW, 0, NULL);
205 /* --------------------------------------------------------------------------------------------- */
208 * Apply new options to widget.
210 * @param w widget
211 * @param options options set
212 * @param enable TRUE if specified options should be added, FALSE if options should be removed
214 void
215 widget_set_options (Widget * w, widget_options_t options, gboolean enable)
217 w->set_options (w, options, enable);
220 /* --------------------------------------------------------------------------------------------- */
222 void
223 widget_set_size (Widget * widget, int y, int x, int lines, int cols)
225 widget->x = x;
226 widget->y = y;
227 widget->cols = cols;
228 widget->lines = lines;
229 send_message (widget, NULL, MSG_RESIZE, 0, NULL);
232 /* --------------------------------------------------------------------------------------------- */
234 void
235 widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey)
237 WDialog *h = w->owner;
238 int color;
240 if ((w->options & W_DISABLED) != 0)
241 color = DISABLED_COLOR;
242 else if (hotkey)
244 if (focused)
245 color = h->color[DLG_COLOR_HOT_FOCUS];
246 else
247 color = h->color[DLG_COLOR_HOT_NORMAL];
249 else
251 if (focused)
252 color = h->color[DLG_COLOR_FOCUS];
253 else
254 color = h->color[DLG_COLOR_NORMAL];
257 tty_setcolor (color);
260 /* --------------------------------------------------------------------------------------------- */
262 void
263 widget_erase (Widget * w)
265 if (w != NULL)
266 tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
269 /* --------------------------------------------------------------------------------------------- */
271 * Check whether widget is active or not.
272 * @param w the widget
274 * @return TRUE if the widget is active, FALSE otherwise
277 gboolean
278 widget_is_active (const void *w)
280 return (w == WIDGET (w)->owner->current->data);
283 /* --------------------------------------------------------------------------------------------- */
285 void
286 widget_redraw (Widget * w)
288 if (w != NULL)
290 WDialog *h = w->owner;
292 if (h != NULL && h->state == DLG_ACTIVE)
293 w->callback (w, NULL, MSG_DRAW, 0, NULL);
297 /* --------------------------------------------------------------------------------------------- */
299 * Replace widget in the dialog.
301 * @param old_w old widget that need to be replaced
302 * @param new_w new widget that will replace @old_w
305 void
306 widget_replace (Widget * old_w, Widget * new_w)
308 WDialog *h = old_w->owner;
309 gboolean should_focus = FALSE;
311 if (h->widgets == NULL)
312 return;
314 if (h->current == NULL)
315 h->current = h->widgets;
317 if (old_w == h->current->data)
318 should_focus = TRUE;
320 new_w->owner = h;
321 new_w->id = old_w->id;
323 if (should_focus)
324 h->current->data = new_w;
325 else
326 g_list_find (h->widgets, old_w)->data = new_w;
328 send_message (old_w, NULL, MSG_DESTROY, 0, NULL);
329 send_message (new_w, NULL, MSG_INIT, 0, NULL);
331 if (should_focus)
332 dlg_select_widget (new_w);
334 widget_redraw (new_w);
337 /* --------------------------------------------------------------------------------------------- */
339 * Check whether two widgets are overlapped or not.
340 * @param a 1st widget
341 * @param b 2nd widget
343 * @return TRUE if widgets are overlapped, FALSE otherwise.
346 gboolean
347 widget_overlapped (const Widget * a, const Widget * b)
349 return !((b->x >= a->x + a->cols)
350 || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
353 /* --------------------------------------------------------------------------------------------- */
354 /* get mouse pointer location within widget */
356 Gpm_Event
357 mouse_get_local (const Gpm_Event * global, const Widget * w)
359 Gpm_Event local;
361 local.buttons = global->buttons;
362 local.x = global->x - w->x;
363 local.y = global->y - w->y;
364 local.type = global->type;
366 return local;
369 /* --------------------------------------------------------------------------------------------- */
371 gboolean
372 mouse_global_in_widget (const Gpm_Event * event, const Widget * w)
374 return (event->x > w->x) && (event->y > w->y) && (event->x <= w->x + w->cols)
375 && (event->y <= w->y + w->lines);
378 /* --------------------------------------------------------------------------------------------- */