Rename Dlg_head to WDialog.
[midnight-commander.git] / lib / widget / widget-common.c
blob6d281c378066407be8276884653a38df436b278b
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 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 * sender, widget_msg_t msg, int parm, void *data)
163 (void) sender;
164 (void) parm;
165 (void) data;
167 switch (msg)
169 case WIDGET_INIT:
170 case WIDGET_FOCUS:
171 case WIDGET_UNFOCUS:
172 case WIDGET_DRAW:
173 case WIDGET_DESTROY:
174 case WIDGET_CURSOR:
175 case WIDGET_IDLE:
176 return MSG_HANDLED;
178 default:
179 return MSG_NOT_HANDLED;
183 /* --------------------------------------------------------------------------------------------- */
186 * Callback for applying new options to widget.
188 * @param w widget
189 * @param options options set
190 * @param enable TRUE if specified options should be added, FALSE if options should be removed
192 void
193 widget_default_set_options_callback (Widget *w, widget_options_t options, gboolean enable)
195 if (enable)
196 w->options |= options;
197 else
198 w->options &= ~options;
200 if (w->owner != NULL && (options & W_DISABLED) != 0)
201 send_message (w, NULL, WIDGET_DRAW, 0, NULL);
204 /* --------------------------------------------------------------------------------------------- */
207 * Apply new options to widget.
209 * @param w widget
210 * @param options options set
211 * @param enable TRUE if specified options should be added, FALSE if options should be removed
213 void
214 widget_set_options (Widget *w, widget_options_t options, gboolean enable)
216 w->set_options (w, options, enable);
219 /* --------------------------------------------------------------------------------------------- */
221 void
222 widget_set_size (Widget * widget, int y, int x, int lines, int cols)
224 widget->x = x;
225 widget->y = y;
226 widget->cols = cols;
227 widget->lines = lines;
228 send_message (widget, NULL, WIDGET_RESIZED, 0, NULL);
231 /* --------------------------------------------------------------------------------------------- */
233 void
234 widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey)
236 WDialog *h = w->owner;
237 int color;
239 if ((w->options & W_DISABLED) != 0)
240 color = DISABLED_COLOR;
241 else if (hotkey)
243 if (focused)
244 color = h->color[DLG_COLOR_HOT_FOCUS];
245 else
246 color = h->color[DLG_COLOR_HOT_NORMAL];
248 else
250 if (focused)
251 color = h->color[DLG_COLOR_FOCUS];
252 else
253 color = h->color[DLG_COLOR_NORMAL];
256 tty_setcolor (color);
259 /* --------------------------------------------------------------------------------------------- */
261 void
262 widget_erase (Widget * w)
264 if (w != NULL)
265 tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
268 /* --------------------------------------------------------------------------------------------- */
270 /* get mouse pointer location within widget */
271 Gpm_Event
272 mouse_get_local (const Gpm_Event * global, const Widget * w)
274 Gpm_Event local;
276 local.buttons = global->buttons;
277 local.x = global->x - w->x;
278 local.y = global->y - w->y;
279 local.type = global->type;
281 return local;
284 /* --------------------------------------------------------------------------------------------- */
286 gboolean
287 mouse_global_in_widget (const Gpm_Event * event, const Widget * w)
289 return (event->x > w->x) && (event->y > w->y) && (event->x <= w->x + w->cols)
290 && (event->y <= w->y + w->lines);
293 /* --------------------------------------------------------------------------------------------- */