Refactoring of widget's position and size calculation.
[midnight-commander.git] / lib / widget / dialog.c
blob2f8c05e4c84f08f7afef93283b75a6d2a9f39489
1 /*
2 Dialog box features module for the Midnight Commander
4 Copyright (C) 1994-2014
5 Free Software Foundation, Inc.
7 This file is part of the Midnight Commander.
9 The Midnight Commander is free software: you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation, either version 3 of the License,
12 or (at your option) any later version.
14 The Midnight Commander is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /** \file dialog.c
24 * \brief Source: dialog box features module
27 #include <config.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
37 #include "lib/global.h"
39 #include "lib/tty/tty.h"
40 #include "lib/skin.h"
41 #include "lib/tty/key.h"
42 #include "lib/strutil.h"
43 #include "lib/fileloc.h" /* MC_HISTORY_FILE */
44 #include "lib/event.h" /* mc_event_raise() */
46 #include "lib/widget.h"
47 #include "lib/widget/mouse.h"
49 /*** global variables ****************************************************************************/
51 /* Color styles for normal and error dialogs */
52 dlg_colors_t dialog_colors;
53 dlg_colors_t alarm_colors;
54 dlg_colors_t listbox_colors;
56 /* Primitive way to check if the the current dialog is our dialog */
57 /* This is needed by async routines like load_prompt */
58 GList *top_dlg = NULL;
60 /* A hook list for idle events */
61 hook_t *idle_hook = NULL;
63 /* If set then dialogs just clean the screen when refreshing, else */
64 /* they do a complete refresh, refreshing all the parts of the program */
65 int fast_refresh = 0;
67 /* left click outside of dialog closes it */
68 int mouse_close_dialog = 0;
70 const global_keymap_t *dialog_map = NULL;
72 /*** file scope macro definitions ****************************************************************/
74 /*** file scope type declarations ****************************************************************/
76 /** What to do if the requested widget doesn't take focus */
77 typedef enum
79 SELECT_NEXT, /* go the the next widget */
80 SELECT_PREV, /* go the the previous widget */
81 SELECT_EXACT /* use current widget */
82 } select_dir_t;
84 /* Control widget positions in dialog */
85 typedef struct
87 int shift_x;
88 int scale_x;
89 int shift_y;
90 int scale_y;
91 } widget_shift_scale_t;
93 /*** file scope variables ************************************************************************/
95 /*** file scope functions ************************************************************************/
97 static GList *
98 dlg_widget_next (WDialog * h, GList * l)
100 GList *next;
102 next = g_list_next (l);
103 if (next == NULL)
104 next = h->widgets;
106 return next;
109 /* --------------------------------------------------------------------------------------------- */
111 static GList *
112 dlg_widget_prev (WDialog * h, GList * l)
114 GList *prev;
116 prev = g_list_previous (l);
117 if (prev == NULL)
118 prev = g_list_last (h->widgets);
120 return prev;
123 /* --------------------------------------------------------------------------------------------- */
125 * broadcast a message to all the widgets in a dialog that have
126 * the options set to flags. If flags is zero, the message is sent
127 * to all widgets.
130 static void
131 dlg_broadcast_msg_to (WDialog * h, widget_msg_t msg, gboolean reverse, widget_options_t flags)
133 GList *p, *first;
135 if (h->widgets == NULL)
136 return;
138 if (h->current == NULL)
139 h->current = h->widgets;
141 if (reverse)
142 p = dlg_widget_prev (h, h->current);
143 else
144 p = dlg_widget_next (h, h->current);
146 first = p;
150 Widget *w = WIDGET (p->data);
152 if (reverse)
153 p = dlg_widget_prev (h, p);
154 else
155 p = dlg_widget_next (h, p);
157 if ((flags == 0) || ((flags & w->options) != 0))
158 send_message (w, NULL, msg, 0, NULL);
160 while (first != p);
163 /* --------------------------------------------------------------------------------------------- */
166 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
168 static void
169 dlg_read_history (WDialog * h)
171 char *profile;
172 ev_history_load_save_t event_data;
174 if (num_history_items_recorded == 0) /* this is how to disable */
175 return;
177 profile = mc_config_get_full_path (MC_HISTORY_FILE);
178 event_data.cfg = mc_config_init (profile, TRUE);
179 event_data.receiver = NULL;
181 /* create all histories in dialog */
182 mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data);
184 mc_config_deinit (event_data.cfg);
185 g_free (profile);
188 /* --------------------------------------------------------------------------------------------- */
190 static gboolean
191 dlg_unfocus (WDialog * h)
193 /* we can unfocus disabled widget */
194 if (h->current != NULL)
196 Widget *wh = WIDGET (h);
198 if (widget_get_state (wh, WST_CONSTRUCT) || widget_get_state (wh, WST_ACTIVE))
200 Widget *current = WIDGET (h->current->data);
202 if (send_message (current, NULL, MSG_UNFOCUS, 0, NULL) == MSG_HANDLED)
204 send_message (h, current, MSG_UNFOCUS, 0, NULL);
205 return TRUE;
211 return FALSE;
214 /* --------------------------------------------------------------------------------------------- */
216 static int
217 dlg_find_widget_callback (const void *a, const void *b)
219 const Widget *w = CONST_WIDGET (a);
220 const widget_cb_fn f = b;
222 return (w->callback == f) ? 0 : 1;
225 /* --------------------------------------------------------------------------------------------- */
228 * Put widget on top or bottom of Z-order.
230 static void
231 dlg_reorder_widgets (GList * l, gboolean set_top)
233 WDialog *h = WIDGET (l->data)->owner;
235 h->widgets = g_list_remove_link (h->widgets, l);
236 if (set_top)
237 h->widgets = g_list_concat (h->widgets, l);
238 else
239 h->widgets = g_list_concat (l, h->widgets);
242 /* --------------------------------------------------------------------------------------------- */
244 * Try to select another widget. If forward is set, follow tab order.
245 * Otherwise go to the previous widget.
248 static void
249 do_select_widget (WDialog * h, GList * w, select_dir_t dir)
251 Widget *w0 = WIDGET (h->current->data);
253 if (!dlg_unfocus (h))
254 return;
256 h->current = w;
260 if (dlg_focus (h))
261 break;
263 switch (dir)
265 case SELECT_EXACT:
266 h->current = g_list_find (h->widgets, w0);
267 if (dlg_focus (h))
268 return;
269 /* try find another widget that can take focus */
270 dir = SELECT_NEXT;
271 /* fallthrough */
272 case SELECT_NEXT:
273 h->current = dlg_widget_next (h, h->current);
274 break;
275 case SELECT_PREV:
276 h->current = dlg_widget_prev (h, h->current);
277 break;
278 default:
279 break;
282 while (h->current != w);
284 if (widget_get_options (WIDGET (h->current->data), WOP_TOP_SELECT))
285 dlg_reorder_widgets (h->current, TRUE);
287 if (widget_overlapped (w0, WIDGET (h->current->data)))
289 send_message (h->current->data, NULL, MSG_DRAW, 0, NULL);
290 send_message (h->current->data, NULL, MSG_FOCUS, 0, NULL);
294 /* --------------------------------------------------------------------------------------------- */
296 static void
297 refresh_cmd (void)
299 #ifdef HAVE_SLANG
300 tty_touch_screen ();
301 mc_refresh ();
302 #else
303 /* Use this if the refreshes fail */
304 clr_scr ();
305 repaint_screen ();
306 #endif /* HAVE_SLANG */
309 /* --------------------------------------------------------------------------------------------- */
311 static cb_ret_t
312 dlg_execute_cmd (WDialog * h, long command)
314 cb_ret_t ret = MSG_HANDLED;
315 switch (command)
317 case CK_Ok:
318 h->ret_value = B_ENTER;
319 dlg_stop (h);
320 break;
321 case CK_Cancel:
322 h->ret_value = B_CANCEL;
323 dlg_stop (h);
324 break;
326 case CK_Up:
327 case CK_Left:
328 dlg_one_up (h);
329 break;
330 case CK_Down:
331 case CK_Right:
332 dlg_one_down (h);
333 break;
335 case CK_Help:
337 ev_help_t event_data = { NULL, h->help_ctx };
338 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
340 break;
342 case CK_Suspend:
343 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
344 refresh_cmd ();
345 break;
346 case CK_Refresh:
347 refresh_cmd ();
348 break;
350 case CK_ScreenList:
351 if (!widget_get_state (WIDGET (h), WST_MODAL))
352 dialog_switch_list ();
353 else
354 ret = MSG_NOT_HANDLED;
355 break;
356 case CK_ScreenNext:
357 if (!widget_get_state (WIDGET (h), WST_MODAL))
358 dialog_switch_next ();
359 else
360 ret = MSG_NOT_HANDLED;
361 break;
362 case CK_ScreenPrev:
363 if (!widget_get_state (WIDGET (h), WST_MODAL))
364 dialog_switch_prev ();
365 else
366 ret = MSG_NOT_HANDLED;
367 break;
369 default:
370 ret = MSG_NOT_HANDLED;
373 return ret;
376 /* --------------------------------------------------------------------------------------------- */
378 static cb_ret_t
379 dlg_handle_key (WDialog * h, int d_key)
381 long command;
383 command = keybind_lookup_keymap_command (dialog_map, d_key);
385 if (command == CK_IgnoreKey)
386 return MSG_NOT_HANDLED;
388 if (send_message (h, NULL, MSG_ACTION, command, NULL) == MSG_HANDLED
389 || dlg_execute_cmd (h, command) == MSG_HANDLED)
390 return MSG_HANDLED;
392 return MSG_NOT_HANDLED;
395 /* --------------------------------------------------------------------------------------------- */
397 * This is the low-level mouse handler.
398 * It receives a Gpm_Event event and translates it into a higher level protocol.
400 static int
401 dlg_mouse_translator (Gpm_Event * event, Widget * w)
403 mouse_event_t me;
405 me = mouse_translate_event (w, event);
407 return mouse_process_event (w, &me);
410 /* --------------------------------------------------------------------------------------------- */
412 static int
413 dlg_mouse_event (WDialog * h, Gpm_Event * event)
415 Widget *wh = WIDGET (h);
417 GList *p;
419 /* close the dialog by mouse left click out of dialog area */
420 if (mouse_close_dialog && (wh->pos_flags & WPOS_FULLSCREEN) == 0
421 && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0)
422 && !mouse_global_in_widget (event, wh))
424 h->ret_value = B_CANCEL;
425 dlg_stop (h);
426 return MOU_NORMAL;
429 if (wh->mouse_callback != NULL)
431 int mou;
433 mou = dlg_mouse_translator (event, wh);
434 if (mou != MOU_UNHANDLED)
435 return mou;
438 /* send the event to widgets in reverse Z-order */
439 p = g_list_last (h->widgets);
442 Widget *w = WIDGET (p->data);
444 if (!widget_get_state (w, WST_DISABLED) && w->mouse_callback != NULL)
446 /* put global cursor position to the widget */
447 int ret;
449 ret = dlg_mouse_translator (event, w);
450 if (ret != MOU_UNHANDLED)
451 return ret;
454 p = g_list_previous (p);
456 while (p != NULL);
458 return MOU_UNHANDLED;
461 /* --------------------------------------------------------------------------------------------- */
463 static cb_ret_t
464 dlg_try_hotkey (WDialog * h, int d_key)
466 GList *hot_cur;
467 Widget *current;
468 cb_ret_t handled;
469 int c;
471 if (h->widgets == NULL)
472 return MSG_NOT_HANDLED;
474 if (h->current == NULL)
475 h->current = h->widgets;
478 * Explanation: we don't send letter hotkeys to other widgets if
479 * the currently selected widget is an input line
482 current = WIDGET (h->current->data);
484 if (widget_get_state (current, WST_DISABLED))
485 return MSG_NOT_HANDLED;
487 if (widget_get_options (current, WOP_IS_INPUT))
489 /* skip ascii control characters, anything else can valid character in
490 * some encoding */
491 if (d_key >= 32 && d_key < 256)
492 return MSG_NOT_HANDLED;
495 /* If it's an alt key, send the message */
496 c = d_key & ~ALT (0);
497 if (d_key & ALT (0) && g_ascii_isalpha (c))
498 d_key = g_ascii_tolower (c);
500 handled = MSG_NOT_HANDLED;
501 if (widget_get_options (current, WOP_WANT_HOTKEY))
502 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
504 /* If not used, send hotkey to other widgets */
505 if (handled == MSG_HANDLED)
506 return MSG_HANDLED;
508 hot_cur = dlg_widget_next (h, h->current);
510 /* send it to all widgets */
511 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
513 current = WIDGET (hot_cur->data);
515 if (widget_get_options (current, WOP_WANT_HOTKEY)
516 && !widget_get_state (current, WST_DISABLED))
517 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
519 if (handled == MSG_NOT_HANDLED)
520 hot_cur = dlg_widget_next (h, hot_cur);
523 if (handled == MSG_HANDLED)
524 do_select_widget (h, hot_cur, SELECT_EXACT);
526 return handled;
529 /* --------------------------------------------------------------------------------------------- */
531 static void
532 dlg_key_event (WDialog * h, int d_key)
534 cb_ret_t handled;
536 if (h->widgets == NULL)
537 return;
539 if (h->current == NULL)
540 h->current = h->widgets;
542 /* TAB used to cycle */
543 if (!widget_get_options (WIDGET (h), WOP_WANT_TAB))
545 if (d_key == '\t')
547 dlg_one_down (h);
548 return;
550 else if ((d_key & ~(KEY_M_SHIFT | KEY_M_CTRL)) == '\t')
552 dlg_one_up (h);
553 return;
557 /* first can dlg_callback handle the key */
558 handled = send_message (h, NULL, MSG_KEY, d_key, NULL);
560 /* next try the hotkey */
561 if (handled == MSG_NOT_HANDLED)
562 handled = dlg_try_hotkey (h, d_key);
564 if (handled == MSG_HANDLED)
565 send_message (h, NULL, MSG_HOTKEY_HANDLED, 0, NULL);
566 else
567 /* not used - then try widget_callback */
568 handled = send_message (h->current->data, NULL, MSG_KEY, d_key, NULL);
570 /* not used- try to use the unhandled case */
571 if (handled == MSG_NOT_HANDLED)
572 handled = send_message (h, NULL, MSG_UNHANDLED_KEY, d_key, NULL);
574 if (handled == MSG_NOT_HANDLED)
575 handled = dlg_handle_key (h, d_key);
577 (void) handled;
578 send_message (h, NULL, MSG_POST_KEY, d_key, NULL);
581 /* --------------------------------------------------------------------------------------------- */
583 static void
584 frontend_dlg_run (WDialog * h)
586 Widget *wh = WIDGET (h);
587 Gpm_Event event;
589 event.x = -1;
591 /* close opened editors, viewers, etc */
592 if (!widget_get_state (wh, WST_MODAL) && mc_global.midnight_shutdown)
594 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
595 return;
598 while (widget_get_state (wh, WST_ACTIVE))
600 int d_key;
602 if (mc_global.tty.winch_flag != 0)
603 dialog_change_screen_size ();
605 if (is_idle ())
607 if (idle_hook)
608 execute_hooks (idle_hook);
610 while (widget_get_state (wh, WST_IDLE) && is_idle ())
611 send_message (wh, NULL, MSG_IDLE, 0, NULL);
613 /* Allow terminating the dialog from the idle handler */
614 if (!widget_get_state (wh, WST_ACTIVE))
615 break;
618 update_cursor (h);
620 /* Clear interrupt flag */
621 tty_got_interrupt ();
622 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
624 dlg_process_event (h, d_key, &event);
626 if (widget_get_state (wh, WST_CLOSED))
627 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
631 /* --------------------------------------------------------------------------------------------- */
633 static int
634 dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
636 const Widget *w = CONST_WIDGET (a);
637 unsigned long id = GPOINTER_TO_UINT (b);
639 return w->id == id ? 0 : 1;
642 /* --------------------------------------------------------------------------------------------- */
643 static void
644 dlg_widget_set_position (gpointer data, gpointer user_data)
646 /* there are, mainly, 2 generally possible situations:
647 * 1. control sticks to one side - it should be moved
648 * 2. control sticks to two sides of one direction - it should be sized
651 Widget *c = WIDGET (data);
652 Widget *wh = WIDGET (c->owner);
653 const widget_shift_scale_t *wss = (const widget_shift_scale_t *) user_data;
654 int x = c->x;
655 int y = c->y;
656 int cols = c->cols;
657 int lines = c->lines;
659 if ((c->pos_flags & WPOS_CENTER_HORZ) != 0)
660 x = wh->x + (wh->cols - c->cols) / 2;
661 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0 && (c->pos_flags & WPOS_KEEP_RIGHT) != 0)
663 x += wss->shift_x;
664 cols += wss->scale_x;
666 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0)
667 x += wss->shift_x;
668 else if ((c->pos_flags & WPOS_KEEP_RIGHT) != 0)
669 x += wss->shift_x + wss->scale_x;
671 if ((c->pos_flags & WPOS_CENTER_VERT) != 0)
672 y = wh->y + (wh->lines - c->lines) / 2;
673 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0 && (c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
675 y += wss->shift_y;
676 lines += wss->scale_y;
678 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0)
679 y += wss->shift_y;
680 else if ((c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
681 y += wss->shift_y + wss->scale_y;
683 widget_set_size (c, y, x, lines, cols);
686 /* --------------------------------------------------------------------------------------------- */
688 static void
689 dlg_adjust_position (const WDialog * h, int *y, int *x, int *lines, int *cols)
691 const Widget *w = CONST_WIDGET (h);
693 if ((w->pos_flags & WPOS_FULLSCREEN) != 0)
695 *y = 0;
696 *x = 0;
697 *lines = LINES;
698 *cols = COLS;
700 else
702 if ((w->pos_flags & WPOS_CENTER_HORZ) != 0)
703 *x = (COLS - *cols) / 2;
704 else
705 *x = w->x;
707 if ((w->pos_flags & WPOS_CENTER_VERT) != 0)
708 *y = (LINES - *lines) / 2;
709 else
710 *y = w->y;
712 if ((w->pos_flags & WPOS_TRYUP) != 0)
714 if (*y > 3)
715 *y -= 2;
716 else if (*y == 3)
717 *y = 2;
722 /* --------------------------------------------------------------------------------------------- */
723 /*** public functions ****************************************************************************/
724 /* --------------------------------------------------------------------------------------------- */
726 /** Clean the dialog area, draw the frame and the title */
727 void
728 dlg_default_repaint (WDialog * h)
730 Widget *wh = WIDGET (h);
732 int space;
734 if (!widget_get_state (wh, WST_ACTIVE))
735 return;
737 space = h->compact ? 0 : 1;
739 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
740 dlg_erase (h);
741 tty_draw_box (wh->y + space, wh->x + space, wh->lines - 2 * space, wh->cols - 2 * space, FALSE);
743 if (h->title != NULL)
745 tty_setcolor (h->color[DLG_COLOR_TITLE]);
746 widget_move (h, space, (wh->cols - str_term_width1 (h->title)) / 2);
747 tty_print_string (h->title);
751 /* --------------------------------------------------------------------------------------------- */
752 /** this function allows to set dialog position */
754 void
755 dlg_set_position (WDialog * h, int y, int x, int lines, int cols)
757 Widget *wh = WIDGET (h);
758 widget_shift_scale_t wss;
760 /* save old positions, will be used to reposition childs */
761 int ox, oy, oc, ol;
763 /* save old positions, will be used to reposition childs */
764 ox = wh->x;
765 oy = wh->y;
766 oc = wh->cols;
767 ol = wh->lines;
769 wh->x = x;
770 wh->y = y;
771 wh->lines = lines;
772 wh->cols = cols;
774 /* dialog is empty */
775 if (h->widgets == NULL)
776 return;
778 if (h->current == NULL)
779 h->current = h->widgets;
781 /* values by which controls should be moved */
782 wss.shift_x = wh->x - ox;
783 wss.scale_x = wh->cols - oc;
784 wss.shift_y = wh->y - oy;
785 wss.scale_y = wh->lines - ol;
787 if (wss.shift_x != 0 || wss.shift_y != 0 || wss.scale_x != 0 || wss.scale_y != 0)
788 g_list_foreach (h->widgets, dlg_widget_set_position, &wss);
791 /* --------------------------------------------------------------------------------------------- */
792 /** Set dialog size and position */
794 void
795 dlg_set_size (WDialog * h, int lines, int cols)
797 int x = 0, y = 0;
799 dlg_adjust_position (h, &y, &x, &lines, &cols);
800 dlg_set_position (h, y, x, lines, cols);
803 /* --------------------------------------------------------------------------------------------- */
804 /** Default dialog callback */
806 cb_ret_t
807 dlg_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
809 WDialog *h = DIALOG (w);
811 (void) sender;
812 (void) parm;
813 (void) data;
815 switch (msg)
817 case MSG_DRAW:
818 if (h->color != NULL)
820 dlg_default_repaint (h);
821 return MSG_HANDLED;
823 return MSG_NOT_HANDLED;
825 case MSG_IDLE:
826 /* we don't want endless loop */
827 widget_idle (w, FALSE);
828 return MSG_HANDLED;
830 case MSG_RESIZE:
831 /* this is default resizing mechanism */
832 /* the main idea of this code is to resize dialog
833 according to flags (if any of flags require automatic
834 resizing, like WPOS_CENTER, end after that reposition
835 controls in dialog according to flags of widget) */
836 dlg_set_size (h, w->lines, w->cols);
837 return MSG_HANDLED;
839 default:
840 break;
843 return MSG_NOT_HANDLED;
846 /* --------------------------------------------------------------------------------------------- */
848 WDialog *
849 dlg_create (gboolean modal, int y1, int x1, int lines, int cols, widget_pos_flags_t pos_flags,
850 gboolean compact, const int *colors, widget_cb_fn callback,
851 widget_mouse_cb_fn mouse_callback, const char *help_ctx, const char *title)
853 WDialog *new_d;
854 Widget *w;
856 new_d = g_new0 (WDialog, 1);
857 w = WIDGET (new_d);
858 w->pos_flags = pos_flags; /* required for dlg_adjust_position() */
859 dlg_adjust_position (new_d, &y1, &x1, &lines, &cols);
860 widget_init (w, y1, x1, lines, cols, (callback != NULL) ? callback : dlg_default_callback,
861 mouse_callback);
862 w->pos_flags = pos_flags; /* restore after widget_init() */
863 w->options |= WOP_TOP_SELECT;
865 w->state |= WST_CONSTRUCT;
866 if (modal)
867 w->state |= WST_MODAL;
869 new_d->color = colors;
870 new_d->help_ctx = help_ctx;
871 new_d->compact = compact;
872 new_d->data = NULL;
874 new_d->mouse_status = MOU_UNHANDLED;
876 /* Strip existing spaces, add one space before and after the title */
877 if (title != NULL && *title != '\0')
879 char *t;
881 t = g_strstrip (g_strdup (title));
882 if (*t != '\0')
883 new_d->title = g_strdup_printf (" %s ", t);
884 g_free (t);
887 /* unique name of event group for this dialog */
888 new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
890 return new_d;
893 /* --------------------------------------------------------------------------------------------- */
895 void
896 dlg_set_default_colors (void)
898 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
899 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
900 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
901 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
902 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
904 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
905 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
906 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
907 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
908 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
910 listbox_colors[DLG_COLOR_NORMAL] = PMENU_ENTRY_COLOR;
911 listbox_colors[DLG_COLOR_FOCUS] = PMENU_SELECTED_COLOR;
912 listbox_colors[DLG_COLOR_HOT_NORMAL] = PMENU_ENTRY_COLOR;
913 listbox_colors[DLG_COLOR_HOT_FOCUS] = PMENU_SELECTED_COLOR;
914 listbox_colors[DLG_COLOR_TITLE] = PMENU_TITLE_COLOR;
917 /* --------------------------------------------------------------------------------------------- */
919 void
920 dlg_erase (WDialog * h)
922 Widget *wh = WIDGET (h);
924 if (wh != NULL && widget_get_state (wh, WST_ACTIVE))
925 tty_fill_region (wh->y, wh->x, wh->lines, wh->cols, ' ');
928 /* --------------------------------------------------------------------------------------------- */
930 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
933 unsigned long
934 add_widget_autopos (WDialog * h, void *w, widget_pos_flags_t pos_flags, const void *before)
936 Widget *wh = WIDGET (h);
937 Widget *widget;
939 /* Don't accept 0 widgets */
940 if (w == NULL)
941 abort ();
943 widget = WIDGET (w);
945 if ((pos_flags & WPOS_CENTER_HORZ) != 0)
946 widget->x = (wh->cols - widget->cols) / 2;
947 widget->x += wh->x;
949 if ((pos_flags & WPOS_CENTER_VERT) != 0)
950 widget->y = (wh->lines - widget->lines) / 2;
951 widget->y += wh->y;
953 widget->owner = h;
954 widget->pos_flags = pos_flags;
955 widget->id = h->widget_id++;
957 if (h->widgets == NULL || before == NULL)
959 h->widgets = g_list_append (h->widgets, widget);
960 h->current = g_list_last (h->widgets);
962 else
964 GList *b;
966 b = g_list_find (h->widgets, before);
968 /* don't accept widget not from dialog. This shouldn't happen */
969 if (b == NULL)
970 abort ();
972 b = g_list_next (b);
973 h->widgets = g_list_insert_before (h->widgets, b, widget);
974 if (b != NULL)
975 h->current = g_list_previous (b);
976 else
977 h->current = g_list_last (h->widgets);
980 /* widget has been added in runtime */
981 if (widget_get_state (wh, WST_ACTIVE))
983 send_message (widget, NULL, MSG_INIT, 0, NULL);
984 send_message (widget, NULL, MSG_DRAW, 0, NULL);
985 send_message (widget, NULL, MSG_FOCUS, 0, NULL);
988 return widget->id;
991 /* --------------------------------------------------------------------------------------------- */
992 /** wrapper to simply add lefttop positioned controls */
994 unsigned long
995 add_widget (WDialog * h, void *w)
997 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT,
998 h->current != NULL ? h->current->data : NULL);
1001 /* --------------------------------------------------------------------------------------------- */
1003 unsigned long
1004 add_widget_before (WDialog * h, void *w, void *before)
1006 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT, before);
1009 /* --------------------------------------------------------------------------------------------- */
1011 /** delete widget from dialog */
1012 void
1013 del_widget (void *w)
1015 WDialog *h;
1016 GList *d;
1018 /* Don't accept NULL widget. This shouldn't happen */
1019 if (w == NULL)
1020 abort ();
1022 h = WIDGET (w)->owner;
1024 d = g_list_find (h->widgets, w);
1025 if (d == h->current)
1026 h->current = dlg_widget_next (h, d);
1028 h->widgets = g_list_remove_link (h->widgets, d);
1029 send_message (d->data, NULL, MSG_DESTROY, 0, NULL);
1030 g_free (d->data);
1031 g_list_free_1 (d);
1033 /* widget has been deleted in runtime */
1034 if (widget_get_state (WIDGET (h), WST_ACTIVE))
1036 dlg_redraw (h);
1037 dlg_focus (h);
1041 /* --------------------------------------------------------------------------------------------- */
1043 void
1044 do_refresh (void)
1046 GList *d = top_dlg;
1048 if (fast_refresh)
1050 if ((d != NULL) && (d->data != NULL))
1051 dlg_redraw (DIALOG (d->data));
1053 else
1055 /* Search first fullscreen dialog */
1056 for (; d != NULL; d = g_list_next (d))
1057 if (d->data != NULL && (WIDGET (d->data)->pos_flags & WPOS_FULLSCREEN) != 0)
1058 break;
1059 /* back to top dialog */
1060 for (; d != NULL; d = g_list_previous (d))
1061 if (d->data != NULL)
1062 dlg_redraw (DIALOG (d->data));
1066 /* --------------------------------------------------------------------------------------------- */
1067 /** broadcast a message to all the widgets in a dialog */
1069 void
1070 dlg_broadcast_msg (WDialog * h, widget_msg_t msg)
1072 dlg_broadcast_msg_to (h, msg, FALSE, 0);
1075 /* --------------------------------------------------------------------------------------------- */
1077 gboolean
1078 dlg_focus (WDialog * h)
1080 /* cannot focus disabled widget */
1081 if (h->current != NULL)
1083 Widget *wh = WIDGET (h);
1085 if (widget_get_state (wh, WST_CONSTRUCT) || widget_get_state (wh, WST_ACTIVE))
1087 Widget *current = WIDGET (h->current->data);
1089 if (!widget_get_state (current, WST_DISABLED)
1090 && (send_message (current, NULL, MSG_FOCUS, 0, NULL) == MSG_HANDLED))
1092 send_message (h, current, MSG_FOCUS, 0, NULL);
1093 return TRUE;
1098 return FALSE;
1101 /* --------------------------------------------------------------------------------------------- */
1102 /** Find the widget with the given callback in the dialog h */
1104 Widget *
1105 find_widget_type (const WDialog * h, widget_cb_fn callback)
1107 GList *w;
1109 w = g_list_find_custom (h->widgets, (gconstpointer) callback, dlg_find_widget_callback);
1111 return (w == NULL) ? NULL : WIDGET (w->data);
1114 /* --------------------------------------------------------------------------------------------- */
1115 /** Find the widget with the given id */
1117 Widget *
1118 dlg_find_by_id (const WDialog * h, unsigned long id)
1120 GList *w;
1122 w = g_list_find_custom (h->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
1123 return w != NULL ? WIDGET (w->data) : NULL;
1126 /* --------------------------------------------------------------------------------------------- */
1127 /** Find the widget with the given id in the dialog h and select it */
1129 void
1130 dlg_select_by_id (const WDialog * h, unsigned long id)
1132 Widget *w;
1134 w = dlg_find_by_id (h, id);
1135 if (w != NULL)
1136 dlg_select_widget (w);
1139 /* --------------------------------------------------------------------------------------------- */
1141 * Try to select widget in the dialog.
1144 void
1145 dlg_select_widget (void *w)
1147 Widget *widget = WIDGET (w);
1148 WDialog *h = widget->owner;
1150 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
1153 /* --------------------------------------------------------------------------------------------- */
1155 * Set widget at bottom of widget list.
1158 void
1159 dlg_set_bottom_widget (void *w)
1161 Widget *widget = WIDGET (w);
1162 WDialog *h = widget->owner;
1164 dlg_reorder_widgets (g_list_find (h->widgets, widget), FALSE);
1167 /* --------------------------------------------------------------------------------------------- */
1168 /** Try to select previous widget in the tab order */
1170 void
1171 dlg_one_up (WDialog * h)
1173 if (h->widgets != NULL)
1174 do_select_widget (h, dlg_widget_prev (h, h->current), SELECT_PREV);
1177 /* --------------------------------------------------------------------------------------------- */
1178 /** Try to select next widget in the tab order */
1180 void
1181 dlg_one_down (WDialog * h)
1183 if (h->widgets != NULL)
1184 do_select_widget (h, dlg_widget_next (h, h->current), SELECT_NEXT);
1187 /* --------------------------------------------------------------------------------------------- */
1189 void
1190 update_cursor (WDialog * h)
1192 GList *p = h->current;
1194 if (p != NULL && widget_get_state (WIDGET (h), WST_ACTIVE))
1196 Widget *w;
1198 w = WIDGET (p->data);
1200 if (!widget_get_state (w, WST_DISABLED) && widget_get_options (w, WOP_WANT_CURSOR))
1201 send_message (w, NULL, MSG_CURSOR, 0, NULL);
1202 else
1205 p = dlg_widget_next (h, p);
1206 if (p == h->current)
1207 break;
1209 w = WIDGET (p->data);
1211 if (!widget_get_state (w, WST_DISABLED) && widget_get_options (w, WOP_WANT_CURSOR)
1212 && send_message (w, NULL, MSG_CURSOR, 0, NULL) == MSG_HANDLED)
1213 break;
1215 while (TRUE);
1219 /* --------------------------------------------------------------------------------------------- */
1221 * Redraw the widgets in reverse order, leaving the current widget
1222 * as the last one
1225 void
1226 dlg_redraw (WDialog * h)
1228 if (!widget_get_state (WIDGET (h), WST_ACTIVE))
1229 return;
1231 if (h->winch_pending)
1233 h->winch_pending = FALSE;
1234 send_message (h, NULL, MSG_RESIZE, 0, NULL);
1237 send_message (h, NULL, MSG_DRAW, 0, NULL);
1238 dlg_broadcast_msg (h, MSG_DRAW);
1239 update_cursor (h);
1242 /* --------------------------------------------------------------------------------------------- */
1244 void
1245 dlg_stop (WDialog * h)
1247 widget_set_state (WIDGET (h), WST_CLOSED, TRUE);
1250 /* --------------------------------------------------------------------------------------------- */
1251 /** Init the process */
1253 void
1254 dlg_init (WDialog * h)
1256 Widget *wh = WIDGET (h);
1258 if (top_dlg != NULL && widget_get_state (WIDGET (top_dlg->data), WST_MODAL))
1259 widget_set_state (wh, WST_MODAL, TRUE);
1261 /* add dialog to the stack */
1262 top_dlg = g_list_prepend (top_dlg, h);
1264 /* Initialize dialog manager and widgets */
1265 if (widget_get_state (wh, WST_CONSTRUCT))
1267 if (!widget_get_state (wh, WST_MODAL))
1268 dialog_switch_add (h);
1270 send_message (h, NULL, MSG_INIT, 0, NULL);
1271 dlg_broadcast_msg (h, MSG_INIT);
1272 dlg_read_history (h);
1275 widget_set_state (wh, WST_ACTIVE, TRUE);
1277 /* first send MSG_DRAW to dialog itself and all widgets... */
1278 dlg_redraw (h);
1280 /* ...then send MSG_FOCUS to select the first widget that can take focus */
1281 while (h->current != NULL && !dlg_focus (h))
1282 h->current = dlg_widget_next (h, h->current);
1285 h->ret_value = 0;
1288 /* --------------------------------------------------------------------------------------------- */
1290 void
1291 dlg_process_event (WDialog * h, int key, Gpm_Event * event)
1293 if (key == EV_NONE)
1295 if (tty_got_interrupt ())
1296 if (send_message (h, NULL, MSG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
1297 dlg_execute_cmd (h, CK_Cancel);
1299 else if (key == EV_MOUSE)
1300 h->mouse_status = dlg_mouse_event (h, event);
1301 else
1302 dlg_key_event (h, key);
1305 /* --------------------------------------------------------------------------------------------- */
1306 /** Shutdown the dlg_run */
1308 void
1309 dlg_run_done (WDialog * h)
1311 top_dlg = g_list_remove (top_dlg, h);
1313 if (widget_get_state (WIDGET (h), WST_CLOSED))
1315 send_message (h, h->current->data, MSG_END, 0, NULL);
1316 if (!widget_get_state (WIDGET (h), WST_MODAL))
1317 dialog_switch_remove (h);
1321 /* --------------------------------------------------------------------------------------------- */
1323 * Standard run dialog routine
1324 * We have to keep this routine small so that we can duplicate it's
1325 * behavior on complex routines like the file routines, this way,
1326 * they can call the dlg_process_event without rewriting all the code
1330 dlg_run (WDialog * h)
1332 dlg_init (h);
1333 frontend_dlg_run (h);
1334 dlg_run_done (h);
1335 return h->ret_value;
1338 /* --------------------------------------------------------------------------------------------- */
1340 void
1341 dlg_destroy (WDialog * h)
1343 /* if some widgets have history, save all history at one moment here */
1344 dlg_save_history (h);
1345 dlg_broadcast_msg (h, MSG_DESTROY);
1346 g_list_free_full (h->widgets, g_free);
1347 mc_event_group_del (h->event_group);
1348 g_free (h->event_group);
1349 g_free (h->title);
1350 g_free (h);
1352 do_refresh ();
1355 /* --------------------------------------------------------------------------------------------- */
1358 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1360 void
1361 dlg_save_history (WDialog * h)
1363 char *profile;
1364 int i;
1366 if (num_history_items_recorded == 0) /* this is how to disable */
1367 return;
1369 profile = mc_config_get_full_path (MC_HISTORY_FILE);
1370 i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
1371 if (i != -1)
1372 close (i);
1374 /* Make sure the history is only readable by the user */
1375 if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
1377 ev_history_load_save_t event_data;
1379 event_data.cfg = mc_config_init (profile, FALSE);
1380 event_data.receiver = NULL;
1382 /* get all histories in dialog */
1383 mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
1385 mc_config_save_file (event_data.cfg, NULL);
1386 mc_config_deinit (event_data.cfg);
1389 g_free (profile);
1392 /* --------------------------------------------------------------------------------------------- */
1394 char *
1395 dlg_get_title (const WDialog * h, size_t len)
1397 char *t;
1399 if (h == NULL)
1400 abort ();
1402 if (h->get_title != NULL)
1403 t = h->get_title (h, len);
1404 else
1405 t = g_strdup ("");
1407 return t;
1410 /* --------------------------------------------------------------------------------------------- */