ab632993d33e394d873ebfc4ac1efa5578347e94
[midnight-commander.git] / lib / widget / dialog.c
blobab632993d33e394d873ebfc4ac1efa5578347e94
1 /*
2 Dialog box features module for the Midnight Commander
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2007, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /** \file dialog.c
25 * \brief Source: dialog box features module
28 #include <config.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h> /* open() */
39 #include "lib/global.h"
41 #include "lib/tty/tty.h"
42 #include "lib/skin.h"
43 #include "lib/tty/key.h"
44 #include "lib/strutil.h"
45 #include "lib/widget.h"
46 #include "lib/fileloc.h" /* MC_HISTORY_FILE */
47 #include "lib/event.h" /* mc_event_raise() */
49 /*** global variables ****************************************************************************/
51 /* Color styles for normal and error dialogs */
52 dlg_colors_t dialog_colors;
53 dlg_colors_t alarm_colors;
55 /* Primitive way to check if the the current dialog is our dialog */
56 /* This is needed by async routines like load_prompt */
57 GList *top_dlg = NULL;
59 /* A hook list for idle events */
60 hook_t *idle_hook = NULL;
62 /* If set then dialogs just clean the screen when refreshing, else */
63 /* they do a complete refresh, refreshing all the parts of the program */
64 int fast_refresh = 0;
66 /* left click outside of dialog closes it */
67 int mouse_close_dialog = 0;
69 const global_keymap_t *dialog_map = NULL;
71 /*** file scope macro definitions ****************************************************************/
73 /*** file scope type declarations ****************************************************************/
75 /** What to do if the requested widget doesn't take focus */
76 typedef enum
78 SELECT_NEXT, /* go the the next widget */
79 SELECT_PREV, /* go the the previous widget */
80 SELECT_EXACT /* use current widget */
81 } select_dir_t;
83 /*** file scope variables ************************************************************************/
85 /*** file scope functions ************************************************************************/
87 static GList *
88 dlg_widget_next (WDialog * h, GList * l)
90 GList *next;
92 next = g_list_next (l);
93 if (next == NULL)
94 next = h->widgets;
96 return next;
99 /* --------------------------------------------------------------------------------------------- */
101 static GList *
102 dlg_widget_prev (WDialog * h, GList * l)
104 GList *prev;
106 prev = g_list_previous (l);
107 if (prev == NULL)
108 prev = g_list_last (h->widgets);
110 return prev;
113 /* --------------------------------------------------------------------------------------------- */
115 * broadcast a message to all the widgets in a dialog that have
116 * the options set to flags. If flags is zero, the message is sent
117 * to all widgets.
120 static void
121 dlg_broadcast_msg_to (WDialog * h, widget_msg_t msg, gboolean reverse, int flags)
123 GList *p, *first;
125 if (h->widgets == NULL)
126 return;
128 if (h->current == NULL)
129 h->current = h->widgets;
131 if (reverse)
132 p = dlg_widget_prev (h, h->current);
133 else
134 p = dlg_widget_next (h, h->current);
136 first = p;
140 Widget *w = WIDGET (p->data);
142 if (reverse)
143 p = dlg_widget_prev (h, p);
144 else
145 p = dlg_widget_next (h, p);
147 if ((flags == 0) || ((flags & w->options) != 0))
148 send_message (w, NULL, msg, 0, NULL);
150 while (first != p);
153 /* --------------------------------------------------------------------------------------------- */
156 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
158 static void
159 dlg_read_history (WDialog * h)
161 char *profile;
162 ev_history_load_save_t event_data;
164 if (num_history_items_recorded == 0) /* this is how to disable */
165 return;
167 profile = mc_config_get_full_path (MC_HISTORY_FILE);
168 event_data.cfg = mc_config_init (profile, TRUE);
169 event_data.receiver = NULL;
171 /* create all histories in dialog */
172 mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data);
174 mc_config_deinit (event_data.cfg);
175 g_free (profile);
178 /* --------------------------------------------------------------------------------------------- */
180 static gboolean
181 dlg_unfocus (WDialog * h)
183 /* we can unfocus disabled widget */
184 if ((h->current != NULL) && (h->state == DLG_CONSTRUCT || h->state == DLG_ACTIVE))
186 Widget *current = WIDGET (h->current->data);
188 if (send_message (current, NULL, MSG_UNFOCUS, 0, NULL) == MSG_HANDLED)
190 send_message (h, current, MSG_UNFOCUS, 0, NULL);
191 return TRUE;
195 return FALSE;
198 /* --------------------------------------------------------------------------------------------- */
200 static int
201 dlg_find_widget_callback (const void *a, const void *b)
203 const Widget *w = WIDGET (a);
204 widget_cb_fn f = (widget_cb_fn) b;
206 return (w->callback == f) ? 0 : 1;
209 /* --------------------------------------------------------------------------------------------- */
211 * Try to select another widget. If forward is set, follow tab order.
212 * Otherwise go to the previous widget.
215 static void
216 do_select_widget (WDialog * h, GList * w, select_dir_t dir)
218 Widget *w0 = WIDGET (h->current->data);
220 if (!dlg_unfocus (h))
221 return;
223 h->current = w;
227 if (dlg_focus (h))
228 break;
230 switch (dir)
232 case SELECT_EXACT:
233 h->current = g_list_find (h->widgets, w0);
234 if (dlg_focus (h))
235 return;
236 /* try find another widget that can take focus */
237 dir = SELECT_NEXT;
238 /* fallthrough */
239 case SELECT_NEXT:
240 h->current = dlg_widget_next (h, h->current);
241 break;
242 case SELECT_PREV:
243 h->current = dlg_widget_prev (h, h->current);
244 break;
247 while (h->current != w /* && (WIDGET (h->current->data)->options & W_DISABLED) == 0 */ );
249 if (dlg_overlap (w0, WIDGET (h->current->data)))
251 send_message (h->current->data, NULL, MSG_DRAW, 0, NULL);
252 send_message (h->current->data, NULL, MSG_FOCUS, 0, NULL);
256 /* --------------------------------------------------------------------------------------------- */
258 static void
259 refresh_cmd (void)
261 #ifdef HAVE_SLANG
262 tty_touch_screen ();
263 mc_refresh ();
264 #else
265 /* Use this if the refreshes fail */
266 clr_scr ();
267 repaint_screen ();
268 #endif /* HAVE_SLANG */
271 /* --------------------------------------------------------------------------------------------- */
273 static cb_ret_t
274 dlg_execute_cmd (WDialog * h, unsigned long command)
276 cb_ret_t ret = MSG_HANDLED;
277 switch (command)
279 case CK_Ok:
280 h->ret_value = B_ENTER;
281 dlg_stop (h);
282 break;
283 case CK_Cancel:
284 h->ret_value = B_CANCEL;
285 dlg_stop (h);
286 break;
288 case CK_Up:
289 case CK_Left:
290 dlg_one_up (h);
291 break;
292 case CK_Down:
293 case CK_Right:
294 dlg_one_down (h);
295 break;
297 case CK_Help:
299 ev_help_t event_data = { NULL, h->help_ctx };
300 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
302 break;
304 case CK_Suspend:
305 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
306 refresh_cmd ();
307 break;
308 case CK_Refresh:
309 refresh_cmd ();
310 break;
312 case CK_ScreenList:
313 if (!h->modal)
314 dialog_switch_list ();
315 else
316 ret = MSG_NOT_HANDLED;
317 break;
318 case CK_ScreenNext:
319 if (!h->modal)
320 dialog_switch_next ();
321 else
322 ret = MSG_NOT_HANDLED;
323 break;
324 case CK_ScreenPrev:
325 if (!h->modal)
326 dialog_switch_prev ();
327 else
328 ret = MSG_NOT_HANDLED;
329 break;
331 default:
332 ret = MSG_NOT_HANDLED;
335 return ret;
338 /* --------------------------------------------------------------------------------------------- */
340 static cb_ret_t
341 dlg_handle_key (WDialog * h, int d_key)
343 unsigned long command;
345 command = keybind_lookup_keymap_command (dialog_map, d_key);
347 if (command == CK_IgnoreKey)
348 return MSG_NOT_HANDLED;
350 if (send_message (h, NULL, MSG_ACTION, command, NULL) == MSG_HANDLED
351 || dlg_execute_cmd (h, command) == MSG_HANDLED)
352 return MSG_HANDLED;
354 return MSG_NOT_HANDLED;
357 /* --------------------------------------------------------------------------------------------- */
359 static int
360 dlg_mouse_event (WDialog * h, Gpm_Event * event)
362 Widget *wh = WIDGET (h);
364 GList *item;
365 GList *starting_widget = h->current;
366 int x = event->x;
367 int y = event->y;
369 /* close the dialog by mouse click out of dialog area */
370 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0) /* left click */
371 && !((x > wh->x) && (x <= wh->x + wh->cols) && (y > wh->y) && (y <= wh->y + wh->lines)))
373 h->ret_value = B_CANCEL;
374 dlg_stop (h);
375 return MOU_NORMAL;
378 if (wh->mouse != NULL)
380 int mou;
382 mou = wh->mouse (event, wh);
383 if (mou != MOU_UNHANDLED)
384 return mou;
387 item = starting_widget;
390 Widget *widget = WIDGET (item->data);
392 item = dlg_widget_prev (h, item);
394 if ((widget->options & W_DISABLED) == 0 && widget->mouse != NULL)
396 /* put global cursor position to the widget */
397 int ret;
399 ret = widget->mouse (event, widget);
400 if (ret != MOU_UNHANDLED)
401 return ret;
404 while (item != starting_widget);
406 return MOU_UNHANDLED;
409 /* --------------------------------------------------------------------------------------------- */
411 static cb_ret_t
412 dlg_try_hotkey (WDialog * h, int d_key)
414 GList *hot_cur;
415 Widget *current;
416 cb_ret_t handled;
417 int c;
419 if (h->widgets == NULL)
420 return MSG_NOT_HANDLED;
422 if (h->current == NULL)
423 h->current = h->widgets;
426 * Explanation: we don't send letter hotkeys to other widgets if
427 * the currently selected widget is an input line
430 current = WIDGET (h->current->data);
432 if ((current->options & W_DISABLED) != 0)
433 return MSG_NOT_HANDLED;
435 if (current->options & W_IS_INPUT)
437 /* skip ascii control characters, anything else can valid character in
438 * some encoding */
439 if (d_key >= 32 && d_key < 256)
440 return MSG_NOT_HANDLED;
443 /* If it's an alt key, send the message */
444 c = d_key & ~ALT (0);
445 if (d_key & ALT (0) && g_ascii_isalpha (c))
446 d_key = g_ascii_tolower (c);
448 handled = MSG_NOT_HANDLED;
449 if ((current->options & W_WANT_HOTKEY) != 0)
450 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
452 /* If not used, send hotkey to other widgets */
453 if (handled == MSG_HANDLED)
454 return MSG_HANDLED;
456 hot_cur = dlg_widget_next (h, h->current);
458 /* send it to all widgets */
459 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
461 current = WIDGET (hot_cur->data);
463 if ((current->options & W_WANT_HOTKEY) != 0 && (current->options & W_DISABLED) == 0)
464 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
466 if (handled == MSG_NOT_HANDLED)
467 hot_cur = dlg_widget_next (h, hot_cur);
470 if (handled == MSG_HANDLED)
471 do_select_widget (h, hot_cur, SELECT_EXACT);
473 return handled;
476 /* --------------------------------------------------------------------------------------------- */
478 static void
479 dlg_key_event (WDialog * h, int d_key)
481 cb_ret_t handled;
483 if (h->widgets == NULL)
484 return;
486 if (h->current == NULL)
487 h->current = h->widgets;
489 /* TAB used to cycle */
490 if ((h->flags & DLG_WANT_TAB) == 0)
492 if (d_key == '\t')
494 dlg_one_down (h);
495 return;
497 else if (d_key == KEY_BTAB)
499 dlg_one_up (h);
500 return;
504 /* first can dlg_callback handle the key */
505 handled = send_message (h, NULL, MSG_KEY, d_key, NULL);
507 /* next try the hotkey */
508 if (handled == MSG_NOT_HANDLED)
509 handled = dlg_try_hotkey (h, d_key);
511 if (handled == MSG_HANDLED)
512 send_message (h, NULL, MSG_HOTKEY_HANDLED, 0, NULL);
513 else
514 /* not used - then try widget_callback */
515 handled = send_message (h->current->data, NULL, MSG_KEY, d_key, NULL);
517 /* not used- try to use the unhandled case */
518 if (handled == MSG_NOT_HANDLED)
519 handled = send_message (h, NULL, MSG_UNHANDLED_KEY, d_key, NULL);
521 if (handled == MSG_NOT_HANDLED)
522 handled = dlg_handle_key (h, d_key);
524 send_message (h, NULL, MSG_POST_KEY, d_key, NULL);
527 /* --------------------------------------------------------------------------------------------- */
529 static void
530 frontend_run_dlg (WDialog * h)
532 int d_key;
533 Gpm_Event event;
535 event.x = -1;
537 /* close opened editors, viewers, etc */
538 if (!h->modal && mc_global.midnight_shutdown)
540 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
541 return;
544 while (h->state == DLG_ACTIVE)
546 if (mc_global.tty.winch_flag != 0)
547 dialog_change_screen_size ();
549 if (is_idle ())
551 if (idle_hook)
552 execute_hooks (idle_hook);
554 while ((WIDGET (h)->options & W_WANT_IDLE) != 0 && is_idle ())
555 send_message (h, NULL, MSG_IDLE, 0, NULL);
557 /* Allow terminating the dialog from the idle handler */
558 if (h->state != DLG_ACTIVE)
559 break;
562 update_cursor (h);
564 /* Clear interrupt flag */
565 tty_got_interrupt ();
566 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
568 dlg_process_event (h, d_key, &event);
570 if (h->state == DLG_CLOSED)
571 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
575 /* --------------------------------------------------------------------------------------------- */
577 static int
578 dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
580 Widget *w = WIDGET (a);
581 unsigned long id = GPOINTER_TO_UINT (b);
583 return w->id == id ? 0 : 1;
586 /* --------------------------------------------------------------------------------------------- */
587 /*** public functions ****************************************************************************/
588 /* --------------------------------------------------------------------------------------------- */
590 /** draw box in window */
591 void
592 draw_box (const WDialog * h, int y, int x, int ys, int xs, gboolean single)
594 const Widget *wh = WIDGET (h);
596 tty_draw_box (wh->y + y, wh->x + x, ys, xs, single);
599 /* --------------------------------------------------------------------------------------------- */
601 /** Clean the dialog area, draw the frame and the title */
602 void
603 dlg_default_repaint (WDialog * h)
605 Widget *wh = WIDGET (h);
607 int space;
609 if (h->state != DLG_ACTIVE)
610 return;
612 space = (h->flags & DLG_COMPACT) ? 0 : 1;
614 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
615 dlg_erase (h);
616 draw_box (h, space, space, wh->lines - 2 * space, wh->cols - 2 * space, FALSE);
618 if (h->title != NULL)
620 tty_setcolor (h->color[DLG_COLOR_TITLE]);
621 widget_move (h, space, (wh->cols - str_term_width1 (h->title)) / 2);
622 tty_print_string (h->title);
626 /* --------------------------------------------------------------------------------------------- */
627 /** this function allows to set dialog position */
629 void
630 dlg_set_position (WDialog * h, int y1, int x1, int y2, int x2)
632 Widget *wh = WIDGET (h);
634 /* save old positions, will be used to reposition childs */
635 int ox, oy, oc, ol;
636 int shift_x, shift_y, scale_x, scale_y;
638 /* save old positions, will be used to reposition childs */
639 ox = wh->x;
640 oy = wh->y;
641 oc = wh->cols;
642 ol = wh->lines;
644 wh->x = x1;
645 wh->y = y1;
646 wh->lines = y2 - y1;
647 wh->cols = x2 - x1;
649 /* dialog is empty */
650 if (h->widgets == NULL)
651 return;
653 if (h->current == NULL)
654 h->current = h->widgets;
656 /* values by which controls should be moved */
657 shift_x = wh->x - ox;
658 shift_y = wh->y - oy;
659 scale_x = wh->cols - oc;
660 scale_y = wh->lines - ol;
662 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
664 GList *w;
666 for (w = h->widgets; w != NULL; w = g_list_next (w))
668 /* there are, mainly, 2 generally possible
669 situations:
671 1. control sticks to one side - it
672 should be moved
674 2. control sticks to two sides of
675 one direction - it should be sized */
677 Widget *c = WIDGET (w->data);
678 int x = c->x;
679 int y = c->y;
680 int cols = c->cols;
681 int lines = c->lines;
683 if ((c->pos_flags & WPOS_CENTER_HORZ) != 0)
684 x = wh->x + (wh->cols - c->cols) / 2;
685 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0 && (c->pos_flags & WPOS_KEEP_RIGHT) != 0)
687 x += shift_x;
688 cols += scale_x;
690 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0)
691 x += shift_x;
692 else if ((c->pos_flags & WPOS_KEEP_RIGHT) != 0)
693 x += shift_x + scale_x;
695 if ((c->pos_flags & WPOS_CENTER_VERT) != 0)
696 y = wh->y + (wh->lines - c->lines) / 2;
697 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0 && (c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
699 y += shift_y;
700 lines += scale_y;
702 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0)
703 y += shift_y;
704 else if ((c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
705 y += shift_y + scale_y;
707 widget_set_size (c, y, x, lines, cols);
712 /* --------------------------------------------------------------------------------------------- */
713 /** this function sets only size, leaving positioning to automatic methods */
715 void
716 dlg_set_size (WDialog * h, int lines, int cols)
718 int x = WIDGET (h)->x;
719 int y = WIDGET (h)->y;
721 if (h->flags & DLG_CENTER)
723 y = (LINES - lines) / 2;
724 x = (COLS - cols) / 2;
727 if ((h->flags & DLG_TRYUP) && (y > 3))
728 y -= 2;
730 dlg_set_position (h, y, x, y + lines, x + cols);
733 /* --------------------------------------------------------------------------------------------- */
734 /** Default dialog callback */
736 cb_ret_t
737 dlg_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
739 WDialog *h = DIALOG (w);
741 (void) sender;
742 (void) parm;
743 (void) data;
745 switch (msg)
747 case MSG_DRAW:
748 if (h->color != NULL)
750 dlg_default_repaint (h);
751 return MSG_HANDLED;
753 return MSG_NOT_HANDLED;
755 case MSG_IDLE:
756 dlg_broadcast_msg_to (h, MSG_IDLE, FALSE, W_WANT_IDLE);
757 return MSG_HANDLED;
759 case MSG_RESIZE:
760 /* this is default resizing mechanism */
761 /* the main idea of this code is to resize dialog
762 according to flags (if any of flags require automatic
763 resizing, like DLG_CENTER, end after that reposition
764 controls in dialog according to flags of widget) */
765 dlg_set_size (h, WIDGET (h)->lines, WIDGET (h)->cols);
766 return MSG_HANDLED;
768 default:
769 break;
772 return MSG_NOT_HANDLED;
775 /* --------------------------------------------------------------------------------------------- */
777 WDialog *
778 create_dlg (gboolean modal, int y1, int x1, int lines, int cols,
779 const int *colors, widget_cb_fn callback, mouse_h mouse_handler,
780 const char *help_ctx, const char *title, dlg_flags_t flags)
782 WDialog *new_d;
783 Widget *w;
785 new_d = g_new0 (WDialog, 1);
786 w = WIDGET (new_d);
787 init_widget (w, y1, x1, lines, cols, (callback != NULL) ? callback : dlg_default_callback, mouse_handler);
788 widget_want_cursor (w, FALSE);
790 new_d->state = DLG_CONSTRUCT;
791 new_d->modal = modal;
792 if (colors != NULL)
793 memmove (new_d->color, colors, sizeof (dlg_colors_t));
794 new_d->help_ctx = help_ctx;
795 new_d->flags = flags;
796 new_d->data = NULL;
798 dlg_set_size (new_d, lines, cols);
799 new_d->fullscreen = (w->x == 0 && w->y == 0 && w->cols == COLS && w->lines == LINES);
801 new_d->mouse_status = MOU_UNHANDLED;
803 /* Strip existing spaces, add one space before and after the title */
804 if (title != NULL && *title != '\0')
806 char *t;
808 t = g_strstrip (g_strdup (title));
809 if (*t != '\0')
810 new_d->title = g_strdup_printf (" %s ", t);
811 g_free (t);
814 /* unique name got event group for this dialog */
815 new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
817 return new_d;
820 /* --------------------------------------------------------------------------------------------- */
822 void
823 dlg_set_default_colors (void)
825 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
826 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
827 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
828 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
829 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
831 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
832 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
833 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
834 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
835 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
838 /* --------------------------------------------------------------------------------------------- */
840 void
841 dlg_erase (WDialog * h)
843 if ((h != NULL) && (h->state == DLG_ACTIVE))
845 Widget *wh = WIDGET (h);
847 tty_fill_region (wh->y, wh->x, wh->lines, wh->cols, ' ');
851 /* --------------------------------------------------------------------------------------------- */
853 * Insert widget to dialog before requested widget. Make the widget current. Return widget ID.
856 unsigned long
857 add_widget_autopos (WDialog * h, void *w, widget_pos_flags_t pos_flags, const void *before)
859 Widget *wh = WIDGET (h);
860 Widget *widget = WIDGET (w);
862 /* Don't accept 0 widgets */
863 if (w == NULL)
864 abort ();
866 if ((pos_flags & WPOS_CENTER_HORZ) != 0)
867 widget->x = (wh->cols - widget->cols) / 2;
868 widget->x += wh->x;
870 if ((pos_flags & WPOS_CENTER_VERT) != 0)
871 widget->y = (wh->lines - widget->lines) / 2;
872 widget->y += wh->y;
874 widget->owner = h;
875 widget->pos_flags = pos_flags;
876 widget->id = h->widget_id++;
878 if (h->widgets == NULL || before == NULL)
880 h->widgets = g_list_append (h->widgets, widget);
881 h->current = g_list_last (h->widgets);
883 else
885 GList *b;
887 b = g_list_find (h->widgets, before);
889 /* don't accept widget not from dialog. This shouldn't happen */
890 if (b == NULL)
891 abort ();
893 b = g_list_next (b);
894 h->widgets = g_list_insert_before (h->widgets, b, widget);
895 if (b != NULL)
896 h->current = g_list_previous (b);
897 else
898 h->current = g_list_last (h->widgets);
901 /* widget has been added in runtime */
902 if (h->state == DLG_ACTIVE)
904 send_message (widget, NULL, MSG_INIT, 0, NULL);
905 send_message (widget, NULL, MSG_DRAW, 0, NULL);
906 send_message (widget, NULL, MSG_FOCUS, 0, NULL);
909 return widget->id;
912 /* --------------------------------------------------------------------------------------------- */
913 /** wrapper to simply add lefttop positioned controls */
915 unsigned long
916 add_widget (WDialog * h, void *w)
918 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT,
919 h->current != NULL ? h->current->data : NULL);
922 /* --------------------------------------------------------------------------------------------- */
924 unsigned long
925 add_widget_before (WDialog * h, void *w, void *before)
927 return add_widget_autopos (h, w, WPOS_KEEP_DEFAULT, before);
930 /* --------------------------------------------------------------------------------------------- */
932 /** delete widget from dialog */
933 void
934 del_widget (void *w)
936 WDialog *h = WIDGET (w)->owner;
937 GList *d;
939 /* Don't accept NULL widget. This shouldn't happen */
940 if (w == NULL)
941 abort ();
943 d = g_list_find (h->widgets, w);
944 if (d == h->current)
945 h->current = dlg_widget_next (h, d);
947 h->widgets = g_list_remove_link (h->widgets, d);
948 send_message (d->data, NULL, MSG_DESTROY, 0, NULL);
949 g_list_free_1 (d);
951 /* widget has been deleted in runtime */
952 if (h->state == DLG_ACTIVE)
954 dlg_redraw (h);
955 dlg_focus (h);
959 /* --------------------------------------------------------------------------------------------- */
961 void
962 do_refresh (void)
964 GList *d = top_dlg;
966 if (fast_refresh)
968 if ((d != NULL) && (d->data != NULL))
969 dlg_redraw (DIALOG (d->data));
971 else
973 /* Search first fullscreen dialog */
974 for (; d != NULL; d = g_list_next (d))
975 if (d->data != NULL && DIALOG (d->data)->fullscreen)
976 break;
977 /* back to top dialog */
978 for (; d != NULL; d = g_list_previous (d))
979 if (d->data != NULL)
980 dlg_redraw (DIALOG (d->data));
984 /* --------------------------------------------------------------------------------------------- */
985 /** broadcast a message to all the widgets in a dialog */
987 void
988 dlg_broadcast_msg (WDialog * h, widget_msg_t msg)
990 dlg_broadcast_msg_to (h, msg, FALSE, 0);
993 /* --------------------------------------------------------------------------------------------- */
995 gboolean
996 dlg_focus (WDialog * h)
998 /* cannot focus disabled widget */
999 if ((h->current != NULL) && (h->state == DLG_CONSTRUCT || h->state == DLG_ACTIVE))
1001 Widget *current = WIDGET (h->current->data);
1003 if (((current->options & W_DISABLED) == 0)
1004 && (send_message (current, NULL, MSG_FOCUS, 0, NULL) == MSG_HANDLED))
1006 send_message (h, current, MSG_FOCUS, 0, NULL);
1007 return TRUE;
1011 return FALSE;
1014 /* --------------------------------------------------------------------------------------------- */
1015 /** Return true if the windows overlap */
1018 dlg_overlap (Widget * a, Widget * b)
1020 return !((b->x >= a->x + a->cols)
1021 || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines));
1025 /* --------------------------------------------------------------------------------------------- */
1026 /** Find the widget with the given callback in the dialog h */
1028 Widget *
1029 find_widget_type (const WDialog * h, widget_cb_fn callback)
1031 GList *w;
1033 w = g_list_find_custom (h->widgets, callback, dlg_find_widget_callback);
1035 return (w == NULL) ? NULL : WIDGET (w->data);
1038 /* --------------------------------------------------------------------------------------------- */
1039 /** Find the widget with the given id */
1041 Widget *
1042 dlg_find_by_id (const WDialog * h, unsigned long id)
1044 GList *w;
1046 w = g_list_find_custom (h->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
1047 return w != NULL ? WIDGET (w->data) : NULL;
1050 /* --------------------------------------------------------------------------------------------- */
1051 /** Find the widget with the given id in the dialog h and select it */
1053 void
1054 dlg_select_by_id (const WDialog * h, unsigned long id)
1056 Widget *w;
1058 w = dlg_find_by_id (h, id);
1059 if (w != NULL)
1060 dlg_select_widget (w);
1063 /* --------------------------------------------------------------------------------------------- */
1065 * Try to select widget in the dialog.
1068 void
1069 dlg_select_widget (void *w)
1071 Widget *widget = WIDGET (w);
1072 WDialog *h = widget->owner;
1074 do_select_widget (h, g_list_find (h->widgets, widget), SELECT_EXACT);
1077 /* --------------------------------------------------------------------------------------------- */
1080 * Set widget at top of widget list and make it current.
1083 void
1084 dlg_set_top_widget (void *w)
1086 Widget *widget = WIDGET (w);
1087 WDialog *h = widget->owner;
1088 GList *l;
1090 l = g_list_find (h->widgets, w);
1091 if (l == NULL)
1092 abort (); /* widget is not in dialog, this should not happen */
1094 /* unfocus prevoius widget and focus current one before widget reordering */
1095 if (h->state == DLG_ACTIVE)
1096 do_select_widget (h, l, SELECT_EXACT);
1098 /* widget reordering */
1099 h->widgets = g_list_remove_link (h->widgets, l);
1100 h->widgets = g_list_concat (h->widgets, l);
1101 h->current = l;
1104 /* --------------------------------------------------------------------------------------------- */
1105 /** Try to select previous widget in the tab order */
1107 void
1108 dlg_one_up (WDialog * h)
1110 if (h->widgets != NULL)
1111 do_select_widget (h, dlg_widget_prev (h, h->current), SELECT_PREV);
1114 /* --------------------------------------------------------------------------------------------- */
1115 /** Try to select next widget in the tab order */
1117 void
1118 dlg_one_down (WDialog * h)
1120 if (h->widgets != NULL)
1121 do_select_widget (h, dlg_widget_next (h, h->current), SELECT_NEXT);
1124 /* --------------------------------------------------------------------------------------------- */
1126 void
1127 update_cursor (WDialog * h)
1129 GList *p = h->current;
1131 if ((p != NULL) && (h->state == DLG_ACTIVE))
1133 Widget *w;
1135 w = WIDGET (p->data);
1137 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1138 send_message (w, NULL, MSG_CURSOR, 0, NULL);
1139 else
1142 p = dlg_widget_next (h, p);
1143 if (p == h->current)
1144 break;
1146 w = WIDGET (p->data);
1148 if (((w->options & W_DISABLED) == 0) && ((w->options & W_WANT_CURSOR) != 0))
1149 if (send_message (w, NULL, MSG_CURSOR, 0, NULL) == MSG_HANDLED)
1150 break;
1152 while (TRUE);
1156 /* --------------------------------------------------------------------------------------------- */
1158 * Redraw the widgets in reverse order, leaving the current widget
1159 * as the last one
1162 void
1163 dlg_redraw (WDialog * h)
1165 if (h->state != DLG_ACTIVE)
1166 return;
1168 if (h->winch_pending)
1170 h->winch_pending = FALSE;
1171 send_message (h, NULL, MSG_RESIZE, 0, NULL);
1174 send_message (h, NULL, MSG_DRAW, 0, NULL);
1175 dlg_broadcast_msg (h, MSG_DRAW);
1176 update_cursor (h);
1179 /* --------------------------------------------------------------------------------------------- */
1181 void
1182 dlg_stop (WDialog * h)
1184 h->state = DLG_CLOSED;
1187 /* --------------------------------------------------------------------------------------------- */
1188 /** Init the process */
1190 void
1191 init_dlg (WDialog * h)
1193 if (top_dlg != NULL && DIALOG (top_dlg->data)->modal)
1194 h->modal = TRUE;
1196 /* add dialog to the stack */
1197 top_dlg = g_list_prepend (top_dlg, h);
1199 /* Initialize dialog manager and widgets */
1200 if (h->state == DLG_CONSTRUCT)
1202 if (!h->modal)
1203 dialog_switch_add (h);
1205 send_message (h, NULL, MSG_INIT, 0, NULL);
1206 dlg_broadcast_msg (h, MSG_INIT);
1207 dlg_read_history (h);
1210 h->state = DLG_ACTIVE;
1212 /* Select the first widget that takes focus */
1213 while (h->current != NULL && !dlg_focus (h))
1214 h->current = dlg_widget_next (h, h->current);
1216 dlg_redraw (h);
1218 h->ret_value = 0;
1221 /* --------------------------------------------------------------------------------------------- */
1223 void
1224 dlg_process_event (WDialog * h, int key, Gpm_Event * event)
1226 if (key == EV_NONE)
1228 if (tty_got_interrupt ())
1229 if (send_message (h, NULL, MSG_ACTION, CK_Cancel, NULL) != MSG_HANDLED)
1230 dlg_execute_cmd (h, CK_Cancel);
1232 return;
1235 if (key == EV_MOUSE)
1236 h->mouse_status = dlg_mouse_event (h, event);
1237 else
1238 dlg_key_event (h, key);
1241 /* --------------------------------------------------------------------------------------------- */
1242 /** Shutdown the run_dlg */
1244 void
1245 dlg_run_done (WDialog * h)
1247 top_dlg = g_list_remove (top_dlg, h);
1249 if (h->state == DLG_CLOSED)
1251 send_message (h, h->current->data, MSG_END, 0, NULL);
1252 if (!h->modal)
1253 dialog_switch_remove (h);
1257 /* --------------------------------------------------------------------------------------------- */
1259 * Standard run dialog routine
1260 * We have to keep this routine small so that we can duplicate it's
1261 * behavior on complex routines like the file routines, this way,
1262 * they can call the dlg_process_event without rewriting all the code
1266 run_dlg (WDialog * h)
1268 init_dlg (h);
1269 frontend_run_dlg (h);
1270 dlg_run_done (h);
1271 return h->ret_value;
1274 /* --------------------------------------------------------------------------------------------- */
1276 void
1277 destroy_dlg (WDialog * h)
1279 /* if some widgets have history, save all history at one moment here */
1280 dlg_save_history (h);
1281 dlg_broadcast_msg (h, MSG_DESTROY);
1282 g_list_foreach (h->widgets, (GFunc) g_free, NULL);
1283 g_list_free (h->widgets);
1284 mc_event_group_del (h->event_group);
1285 g_free (h->event_group);
1286 g_free (h->title);
1287 g_free (h);
1289 do_refresh ();
1292 /* --------------------------------------------------------------------------------------------- */
1295 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1297 void
1298 dlg_save_history (WDialog * h)
1300 char *profile;
1301 int i;
1303 if (num_history_items_recorded == 0) /* this is how to disable */
1304 return;
1306 profile = mc_config_get_full_path (MC_HISTORY_FILE);
1307 i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
1308 if (i != -1)
1309 close (i);
1311 /* Make sure the history is only readable by the user */
1312 if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
1314 ev_history_load_save_t event_data;
1316 event_data.cfg = mc_config_init (profile, FALSE);
1317 event_data.receiver = NULL;
1319 /* get all histories in dialog */
1320 mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
1322 mc_config_save_file (event_data.cfg, NULL);
1323 mc_config_deinit (event_data.cfg);
1326 g_free (profile);
1329 /* --------------------------------------------------------------------------------------------- */
1331 char *
1332 dlg_get_title (const WDialog * h, size_t len)
1334 char *t;
1336 if (h == NULL)
1337 abort ();
1339 if (h->get_title != NULL)
1340 t = h->get_title (h, len);
1341 else
1342 t = g_strdup ("");
1344 return t;
1347 /* --------------------------------------------------------------------------------------------- */
1348 /** Replace widget old_w for widget new_w in the dialog */
1350 void
1351 dlg_replace_widget (Widget * old_w, Widget * new_w)
1353 WDialog *h = old_w->owner;
1354 gboolean should_focus = FALSE;
1356 if (h->widgets == NULL)
1357 return;
1359 if (h->current == NULL)
1360 h->current = h->widgets;
1362 if (old_w == h->current->data)
1363 should_focus = TRUE;
1365 new_w->owner = h;
1366 new_w->id = old_w->id;
1368 if (should_focus)
1369 h->current->data = new_w;
1370 else
1371 g_list_find (h->widgets, old_w)->data = new_w;
1373 send_message (old_w, NULL, MSG_DESTROY, 0, NULL);
1374 send_message (new_w, NULL, MSG_INIT, 0, NULL);
1376 if (should_focus)
1377 dlg_select_widget (new_w);
1379 if (new_w->owner->state == DLG_ACTIVE)
1380 send_message (new_w, NULL, MSG_DRAW, 0, NULL);
1383 /* --------------------------------------------------------------------------------------------- */