2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2016
5 Free Software Foundation, Inc.
8 Radek Doulik, 1994, 1995
9 Miguel de Icaza, 1994, 1995
11 Andrej Borsenkow, 1996
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
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/color.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 /* --------------------------------------------------------------------------------------------- */
63 parse_hotkey (const char *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
);
79 p
= str_cget_next_char (cp
);
80 result
.hotkey
= g_strndup (cp
, p
- cp
);
83 result
.end
= g_strdup (cp
);
87 result
.start
= g_strdup (text
);
95 /* --------------------------------------------------------------------------------------------- */
98 release_hotkey (const hotkey_t hotkey
)
100 g_free (hotkey
.start
);
101 g_free (hotkey
.hotkey
);
105 /* --------------------------------------------------------------------------------------------- */
108 hotkey_width (const hotkey_t hotkey
)
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;
118 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
140 widget_init (Widget
* w
, int y
, int x
, int lines
, int cols
,
141 widget_cb_fn callback
, widget_mouse_cb_fn mouse_callback
)
147 w
->pos_flags
= WPOS_KEEP_DEFAULT
;
148 w
->callback
= callback
;
149 w
->mouse_callback
= mouse_callback
;
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 /* Almost all widgets want to put the cursor in a suitable place */
157 w
->options
= WOP_WANT_CURSOR
;
158 w
->state
= WST_DEFAULT
;
161 /* --------------------------------------------------------------------------------------------- */
163 /* Default callback for widgets */
165 widget_default_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
186 return MSG_NOT_HANDLED
;
190 /* --------------------------------------------------------------------------------------------- */
193 * Apply new options to widget.
196 * @param options widget option flags to modify. Several flags per call can be modified.
197 * @param enable TRUE if specified options should be added, FALSE if options should be removed
200 widget_set_options (Widget
* w
, widget_options_t options
, gboolean enable
)
203 w
->options
|= options
;
205 w
->options
&= ~options
;
208 /* --------------------------------------------------------------------------------------------- */
211 * Modify state of widget.
214 * @param state widget state flag to modify
215 * @param enable specifies whether to turn the flag on (TRUE) or off (FALSE).
216 * Only one flag per call can be modified.
217 * @return TRUE if set was handled successfully, FALSE otherwise
220 widget_set_state (Widget
* w
, widget_state_t state
, gboolean enable
)
232 if ((state
& WST_CONSTRUCT
) != 0)
233 w
->state
&= ~(WST_ACTIVE
| WST_SUSPENDED
| WST_CLOSED
);
234 else if ((state
& WST_ACTIVE
) != 0)
235 w
->state
&= ~(WST_CONSTRUCT
| WST_SUSPENDED
| WST_CLOSED
);
236 else if ((state
& WST_SUSPENDED
) != 0)
237 w
->state
&= ~(WST_CONSTRUCT
| WST_ACTIVE
| WST_CLOSED
);
238 else if ((state
& WST_CLOSED
) != 0)
239 w
->state
&= ~(WST_CONSTRUCT
| WST_ACTIVE
| WST_SUSPENDED
);
242 if (w
->owner
== NULL
)
248 if (send_message (w
, NULL
, enable
? MSG_DISABLE
: MSG_ENABLE
, 0, NULL
) != MSG_HANDLED
)
251 send_message (w
, NULL
, MSG_DRAW
, 0, NULL
);
261 /* --------------------------------------------------------------------------------------------- */
264 widget_set_size (Widget
* widget
, int y
, int x
, int lines
, int cols
)
269 widget
->lines
= lines
;
270 send_message (widget
, NULL
, MSG_RESIZE
, 0, NULL
);
273 /* --------------------------------------------------------------------------------------------- */
276 widget_selectcolor (Widget
* w
, gboolean focused
, gboolean hotkey
)
278 WDialog
*h
= w
->owner
;
281 if (widget_get_state (w
, WST_DISABLED
))
282 color
= DISABLED_COLOR
;
286 color
= h
->color
[DLG_COLOR_HOT_FOCUS
];
288 color
= h
->color
[DLG_COLOR_HOT_NORMAL
];
293 color
= h
->color
[DLG_COLOR_FOCUS
];
295 color
= h
->color
[DLG_COLOR_NORMAL
];
298 tty_setcolor (color
);
301 /* --------------------------------------------------------------------------------------------- */
304 widget_erase (Widget
* w
)
307 tty_fill_region (w
->y
, w
->x
, w
->lines
, w
->cols
, ' ');
310 /* --------------------------------------------------------------------------------------------- */
312 * Check whether widget is active or not.
313 * @param w the widget
315 * @return TRUE if the widget is active, FALSE otherwise
319 widget_is_active (const void *w
)
321 return (w
== CONST_WIDGET (w
)->owner
->current
->data
);
324 /* --------------------------------------------------------------------------------------------- */
327 widget_redraw (Widget
* w
)
331 WDialog
*h
= w
->owner
;
333 if (h
!= NULL
&& widget_get_state (WIDGET (h
), WST_ACTIVE
))
334 w
->callback (w
, NULL
, MSG_DRAW
, 0, NULL
);
338 /* --------------------------------------------------------------------------------------------- */
340 * Replace widget in the dialog.
342 * @param old_w old widget that need to be replaced
343 * @param new_w new widget that will replace @old_w
347 widget_replace (Widget
* old_w
, Widget
* new_w
)
349 WDialog
*h
= old_w
->owner
;
350 gboolean should_focus
= FALSE
;
352 if (h
->widgets
== NULL
)
355 if (h
->current
== NULL
)
356 h
->current
= h
->widgets
;
358 if (old_w
== h
->current
->data
)
362 new_w
->id
= old_w
->id
;
365 h
->current
->data
= new_w
;
367 g_list_find (h
->widgets
, old_w
)->data
= new_w
;
369 send_message (old_w
, NULL
, MSG_DESTROY
, 0, NULL
);
370 send_message (new_w
, NULL
, MSG_INIT
, 0, NULL
);
373 dlg_select_widget (new_w
);
375 widget_redraw (new_w
);
378 /* --------------------------------------------------------------------------------------------- */
380 * Check whether two widgets are overlapped or not.
381 * @param a 1st widget
382 * @param b 2nd widget
384 * @return TRUE if widgets are overlapped, FALSE otherwise.
388 widget_overlapped (const Widget
* a
, const Widget
* b
)
390 return !((b
->x
>= a
->x
+ a
->cols
)
391 || (a
->x
>= b
->x
+ b
->cols
) || (b
->y
>= a
->y
+ a
->lines
) || (a
->y
>= b
->y
+ b
->lines
));
394 /* --------------------------------------------------------------------------------------------- */
395 /* get mouse pointer location within widget */
398 mouse_get_local (const Gpm_Event
* global
, const Widget
* w
)
402 local
.buttons
= global
->buttons
;
409 local
.x
= global
->x
- w
->x
;
410 local
.y
= global
->y
- w
->y
;
411 local
.type
= global
->type
;
416 /* --------------------------------------------------------------------------------------------- */
419 mouse_global_in_widget (const Gpm_Event
* event
, const Widget
* w
)
421 return (event
->x
> w
->x
) && (event
->y
> w
->y
) && (event
->x
<= w
->x
+ w
->cols
)
422 && (event
->y
<= w
->y
+ w
->lines
);
425 /* --------------------------------------------------------------------------------------------- */