Move widget add/del API from WDialog to WGroup.
[midnight-commander.git] / lib / widget / dialog.c
blobaebd3e210a334826d48206b9134a326648f8c82c
1 /*
2 Dialog box features module for the Midnight Commander
4 Copyright (C) 1994-2020
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() */
45 #include "lib/util.h" /* MC_PTR_FREE */
46 #include "lib/mcconfig.h" /* num_history_items_recorded */
48 #include "lib/widget.h"
49 #include "lib/widget/mouse.h"
51 /*** global variables ****************************************************************************/
53 /* Color styles for normal and error dialogs */
54 dlg_colors_t dialog_colors;
55 dlg_colors_t alarm_colors;
56 dlg_colors_t listbox_colors;
58 /* Primitive way to check if the the current dialog is our dialog */
59 /* This is needed by async routines like load_prompt */
60 GList *top_dlg = NULL;
62 /* A hook list for idle events */
63 hook_t *idle_hook = NULL;
65 /* If set then dialogs just clean the screen when refreshing, else */
66 /* they do a complete refresh, refreshing all the parts of the program */
67 gboolean fast_refresh = FALSE;
69 /* left click outside of dialog closes it */
70 gboolean mouse_close_dialog = FALSE;
72 const global_keymap_t *dialog_map = NULL;
74 /*** file scope macro definitions ****************************************************************/
76 /*** file scope type declarations ****************************************************************/
78 /* Control widget positions in dialog */
79 typedef struct
81 int shift_x;
82 int scale_x;
83 int shift_y;
84 int scale_y;
85 } widget_shift_scale_t;
87 /*** file scope variables ************************************************************************/
89 /* --------------------------------------------------------------------------------------------- */
90 /*** file scope functions ************************************************************************/
91 /* --------------------------------------------------------------------------------------------- */
93 /**
94 * broadcast a message to all the widgets in a dialog that have
95 * the options set to flags. If flags is zero, the message is sent
96 * to all widgets.
99 static void
100 dlg_broadcast_msg_to (WDialog * h, widget_msg_t msg, gboolean reverse, widget_options_t flags)
102 GList *p, *first;
103 WGroup *g = GROUP (h);
105 if (g->widgets == NULL)
106 return;
108 if (g->current == NULL)
109 g->current = g->widgets;
111 if (reverse)
112 p = group_get_widget_prev_of (g->current);
113 else
114 p = group_get_widget_next_of (g->current);
116 first = p;
120 Widget *w = WIDGET (p->data);
122 if (reverse)
123 p = group_get_widget_prev_of (p);
124 else
125 p = group_get_widget_next_of (p);
127 if ((flags == 0) || ((flags & w->options) != 0))
128 send_message (w, NULL, msg, 0, NULL);
130 while (first != p);
133 /* --------------------------------------------------------------------------------------------- */
136 * Read histories from the ${XDG_CACHE_HOME}/mc/history file
138 static void
139 dlg_read_history (WDialog * h)
141 char *profile;
142 ev_history_load_save_t event_data;
144 if (num_history_items_recorded == 0) /* this is how to disable */
145 return;
147 profile = mc_config_get_full_path (MC_HISTORY_FILE);
148 event_data.cfg = mc_config_init (profile, TRUE);
149 event_data.receiver = NULL;
151 /* create all histories in dialog */
152 mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data);
154 mc_config_deinit (event_data.cfg);
155 g_free (profile);
158 /* --------------------------------------------------------------------------------------------- */
160 static int
161 dlg_find_widget_callback (const void *a, const void *b)
163 const Widget *w = CONST_WIDGET (a);
164 const widget_cb_fn f = b;
166 return (w->callback == f) ? 0 : 1;
169 /* --------------------------------------------------------------------------------------------- */
171 static void
172 refresh_cmd (void)
174 #ifdef HAVE_SLANG
175 tty_touch_screen ();
176 mc_refresh ();
177 #else
178 /* Use this if the refreshes fail */
179 clr_scr ();
180 repaint_screen ();
181 #endif /* HAVE_SLANG */
184 /* --------------------------------------------------------------------------------------------- */
186 static cb_ret_t
187 dlg_execute_cmd (WDialog * h, long command)
189 WGroup *g = GROUP (h);
190 cb_ret_t ret = MSG_HANDLED;
192 if (send_message (h, NULL, MSG_ACTION, command, NULL) == MSG_HANDLED)
193 return MSG_HANDLED;
195 switch (command)
197 case CK_Ok:
198 h->ret_value = B_ENTER;
199 dlg_stop (h);
200 break;
201 case CK_Cancel:
202 h->ret_value = B_CANCEL;
203 dlg_stop (h);
204 break;
206 case CK_Up:
207 case CK_Left:
208 group_select_prev_widget (g);
209 break;
210 case CK_Down:
211 case CK_Right:
212 group_select_next_widget (g);
213 break;
215 case CK_Help:
217 ev_help_t event_data = { NULL, h->help_ctx };
218 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
220 break;
222 case CK_Suspend:
223 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
224 refresh_cmd ();
225 break;
226 case CK_Refresh:
227 refresh_cmd ();
228 break;
230 case CK_ScreenList:
231 if (!widget_get_state (WIDGET (h), WST_MODAL))
232 dialog_switch_list ();
233 else
234 ret = MSG_NOT_HANDLED;
235 break;
236 case CK_ScreenNext:
237 if (!widget_get_state (WIDGET (h), WST_MODAL))
238 dialog_switch_next ();
239 else
240 ret = MSG_NOT_HANDLED;
241 break;
242 case CK_ScreenPrev:
243 if (!widget_get_state (WIDGET (h), WST_MODAL))
244 dialog_switch_prev ();
245 else
246 ret = MSG_NOT_HANDLED;
247 break;
249 default:
250 ret = MSG_NOT_HANDLED;
253 return ret;
256 /* --------------------------------------------------------------------------------------------- */
258 static cb_ret_t
259 dlg_handle_key (WDialog * h, int d_key)
261 long command;
263 command = keybind_lookup_keymap_command (dialog_map, d_key);
264 if (command != CK_IgnoreKey)
265 return dlg_execute_cmd (h, command);
267 return MSG_NOT_HANDLED;
270 /* --------------------------------------------------------------------------------------------- */
272 * This is the low-level mouse handler.
273 * It receives a Gpm_Event event and translates it into a higher level protocol.
275 static int
276 dlg_mouse_translator (Gpm_Event * event, Widget * w)
278 mouse_event_t me;
280 me = mouse_translate_event (w, event);
282 return mouse_process_event (w, &me);
285 /* --------------------------------------------------------------------------------------------- */
287 static int
288 dlg_mouse_event (WDialog * h, Gpm_Event * event)
290 Widget *wh = WIDGET (h);
292 GList *p;
294 /* close the dialog by mouse left click out of dialog area */
295 if (mouse_close_dialog && (wh->pos_flags & WPOS_FULLSCREEN) == 0
296 && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0)
297 && !mouse_global_in_widget (event, wh))
299 h->ret_value = B_CANCEL;
300 dlg_stop (h);
301 return MOU_NORMAL;
304 if (wh->mouse_callback != NULL)
306 int mou;
308 mou = dlg_mouse_translator (event, wh);
309 if (mou != MOU_UNHANDLED)
310 return mou;
313 if (GROUP (h)->widgets == NULL)
314 return MOU_UNHANDLED;
316 /* send the event to widgets in reverse Z-order */
317 p = g_list_last (GROUP (h)->widgets);
320 Widget *w = WIDGET (p->data);
322 if (!widget_get_state (w, WST_DISABLED) && w->mouse_callback != NULL)
324 /* put global cursor position to the widget */
325 int ret;
327 ret = dlg_mouse_translator (event, w);
328 if (ret != MOU_UNHANDLED)
329 return ret;
332 p = g_list_previous (p);
334 while (p != NULL);
336 return MOU_UNHANDLED;
339 /* --------------------------------------------------------------------------------------------- */
341 static cb_ret_t
342 dlg_try_hotkey (WDialog * h, int d_key)
344 WGroup *g = GROUP (h);
345 GList *hot_cur;
346 Widget *current;
347 cb_ret_t handled;
348 int c;
350 if (g->widgets == NULL)
351 return MSG_NOT_HANDLED;
353 if (g->current == NULL)
354 g->current = g->widgets;
357 * Explanation: we don't send letter hotkeys to other widgets if
358 * the currently selected widget is an input line
361 current = WIDGET (g->current->data);
363 if (widget_get_state (current, WST_DISABLED))
364 return MSG_NOT_HANDLED;
366 if (widget_get_options (current, WOP_IS_INPUT))
368 /* skip ascii control characters, anything else can valid character in
369 * some encoding */
370 if (d_key >= 32 && d_key < 256)
371 return MSG_NOT_HANDLED;
374 /* If it's an alt key, send the message */
375 c = d_key & ~ALT (0);
376 if (d_key & ALT (0) && g_ascii_isalpha (c))
377 d_key = g_ascii_tolower (c);
379 handled = MSG_NOT_HANDLED;
380 if (widget_get_options (current, WOP_WANT_HOTKEY))
381 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
383 /* If not used, send hotkey to other widgets */
384 if (handled == MSG_HANDLED)
385 return MSG_HANDLED;
387 hot_cur = group_get_widget_next_of (g->current);
389 /* send it to all widgets */
390 while (g->current != hot_cur && handled == MSG_NOT_HANDLED)
392 current = WIDGET (hot_cur->data);
394 if (widget_get_options (current, WOP_WANT_HOTKEY)
395 && !widget_get_state (current, WST_DISABLED))
396 handled = send_message (current, NULL, MSG_HOTKEY, d_key, NULL);
398 if (handled == MSG_NOT_HANDLED)
399 hot_cur = group_get_widget_next_of (hot_cur);
402 if (handled == MSG_HANDLED)
403 widget_select (WIDGET (hot_cur->data));
405 return handled;
408 /* --------------------------------------------------------------------------------------------- */
410 static void
411 dlg_key_event (WDialog * h, int d_key)
413 WGroup *g = GROUP (h);
414 cb_ret_t handled;
416 if (g->widgets == NULL)
417 return;
419 if (g->current == NULL)
420 g->current = g->widgets;
422 /* TAB used to cycle */
423 if (!widget_get_options (WIDGET (g), WOP_WANT_TAB))
425 if (d_key == '\t')
427 group_select_next_widget (g);
428 return;
430 else if ((d_key & ~(KEY_M_SHIFT | KEY_M_CTRL)) == '\t')
432 group_select_prev_widget (g);
433 return;
437 /* first can dlg_callback handle the key */
438 handled = send_message (h, NULL, MSG_KEY, d_key, NULL);
440 /* next try the hotkey */
441 if (handled == MSG_NOT_HANDLED)
442 handled = dlg_try_hotkey (h, d_key);
444 if (handled == MSG_HANDLED)
445 send_message (h, NULL, MSG_HOTKEY_HANDLED, 0, NULL);
446 else
447 /* not used - then try widget_callback */
448 handled = send_message (g->current->data, NULL, MSG_KEY, d_key, NULL);
450 /* not used- try to use the unhandled case */
451 if (handled == MSG_NOT_HANDLED)
452 handled = send_message (h, NULL, MSG_UNHANDLED_KEY, d_key, NULL);
454 if (handled == MSG_NOT_HANDLED)
455 handled = dlg_handle_key (h, d_key);
457 (void) handled;
458 send_message (h, NULL, MSG_POST_KEY, d_key, NULL);
461 /* --------------------------------------------------------------------------------------------- */
463 static void
464 frontend_dlg_run (WDialog * h)
466 Widget *wh = WIDGET (h);
467 Gpm_Event event;
469 event.x = -1;
471 /* close opened editors, viewers, etc */
472 if (!widget_get_state (wh, WST_MODAL) && mc_global.midnight_shutdown)
474 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
475 return;
478 while (widget_get_state (wh, WST_ACTIVE))
480 int d_key;
482 if (tty_got_winch ())
483 dialog_change_screen_size ();
485 if (is_idle ())
487 if (idle_hook)
488 execute_hooks (idle_hook);
490 while (widget_get_state (wh, WST_IDLE) && is_idle ())
491 send_message (wh, NULL, MSG_IDLE, 0, NULL);
493 /* Allow terminating the dialog from the idle handler */
494 if (!widget_get_state (wh, WST_ACTIVE))
495 break;
498 update_cursor (h);
500 /* Clear interrupt flag */
501 tty_got_interrupt ();
502 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
504 dlg_process_event (h, d_key, &event);
506 if (widget_get_state (wh, WST_CLOSED))
507 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
511 /* --------------------------------------------------------------------------------------------- */
513 static int
514 dlg_find_widget_by_id (gconstpointer a, gconstpointer b)
516 const Widget *w = CONST_WIDGET (a);
517 unsigned long id = GPOINTER_TO_UINT (b);
519 return w->id == id ? 0 : 1;
522 /* --------------------------------------------------------------------------------------------- */
523 static void
524 dlg_widget_set_position (gpointer data, gpointer user_data)
526 /* there are, mainly, 2 generally possible situations:
527 * 1. control sticks to one side - it should be moved
528 * 2. control sticks to two sides of one direction - it should be sized
531 Widget *c = WIDGET (data);
532 Widget *wh = WIDGET (c->owner);
533 const widget_shift_scale_t *wss = (const widget_shift_scale_t *) user_data;
534 int x = c->x;
535 int y = c->y;
536 int cols = c->cols;
537 int lines = c->lines;
539 if ((c->pos_flags & WPOS_CENTER_HORZ) != 0)
540 x = wh->x + (wh->cols - c->cols) / 2;
541 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0 && (c->pos_flags & WPOS_KEEP_RIGHT) != 0)
543 x += wss->shift_x;
544 cols += wss->scale_x;
546 else if ((c->pos_flags & WPOS_KEEP_LEFT) != 0)
547 x += wss->shift_x;
548 else if ((c->pos_flags & WPOS_KEEP_RIGHT) != 0)
549 x += wss->shift_x + wss->scale_x;
551 if ((c->pos_flags & WPOS_CENTER_VERT) != 0)
552 y = wh->y + (wh->lines - c->lines) / 2;
553 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0 && (c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
555 y += wss->shift_y;
556 lines += wss->scale_y;
558 else if ((c->pos_flags & WPOS_KEEP_TOP) != 0)
559 y += wss->shift_y;
560 else if ((c->pos_flags & WPOS_KEEP_BOTTOM) != 0)
561 y += wss->shift_y + wss->scale_y;
563 widget_set_size (c, y, x, lines, cols);
566 /* --------------------------------------------------------------------------------------------- */
567 /*** public functions ****************************************************************************/
568 /* --------------------------------------------------------------------------------------------- */
570 /** Clean the dialog area, draw the frame and the title */
571 void
572 dlg_default_repaint (WDialog * h)
574 Widget *wh = WIDGET (h);
576 int space;
578 if (!widget_get_state (wh, WST_ACTIVE))
579 return;
581 space = h->compact ? 0 : 1;
583 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
584 dlg_erase (h);
585 tty_draw_box (wh->y + space, wh->x + space, wh->lines - 2 * space, wh->cols - 2 * space, FALSE);
587 if (h->title != NULL)
589 /* TODO: truncate long title */
590 tty_setcolor (h->color[DLG_COLOR_TITLE]);
591 widget_gotoyx (h, space, (wh->cols - str_term_width1 (h->title)) / 2);
592 tty_print_string (h->title);
596 /* --------------------------------------------------------------------------------------------- */
597 /** this function allows to set dialog position */
599 void
600 dlg_set_position (WDialog * h, int y, int x, int lines, int cols)
602 WGroup *g = GROUP (h);
603 Widget *wh = WIDGET (h);
604 widget_shift_scale_t wss;
606 /* save old positions, will be used to reposition childs */
607 int ox, oy, oc, ol;
609 /* save old positions, will be used to reposition childs */
610 ox = wh->x;
611 oy = wh->y;
612 oc = wh->cols;
613 ol = wh->lines;
615 wh->x = x;
616 wh->y = y;
617 wh->lines = lines;
618 wh->cols = cols;
620 /* dialog is empty */
621 if (g->widgets == NULL)
622 return;
624 if (g->current == NULL)
625 g->current = g->widgets;
627 /* values by which controls should be moved */
628 wss.shift_x = wh->x - ox;
629 wss.scale_x = wh->cols - oc;
630 wss.shift_y = wh->y - oy;
631 wss.scale_y = wh->lines - ol;
633 if (wss.shift_x != 0 || wss.shift_y != 0 || wss.scale_x != 0 || wss.scale_y != 0)
634 g_list_foreach (g->widgets, dlg_widget_set_position, &wss);
637 /* --------------------------------------------------------------------------------------------- */
638 /** Set dialog size and position */
640 void
641 dlg_set_size (WDialog * h, int lines, int cols)
643 int x = 0, y = 0;
645 widget_adjust_position (WIDGET (h)->pos_flags, &y, &x, &lines, &cols);
646 dlg_set_position (h, y, x, lines, cols);
649 /* --------------------------------------------------------------------------------------------- */
650 /** Default dialog callback */
652 cb_ret_t
653 dlg_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
655 WDialog *h = DIALOG (w);
657 (void) sender;
658 (void) parm;
659 (void) data;
661 switch (msg)
663 case MSG_DRAW:
664 if (h->color != NULL)
666 dlg_default_repaint (h);
667 return MSG_HANDLED;
669 return MSG_NOT_HANDLED;
671 case MSG_IDLE:
672 /* we don't want endless loop */
673 widget_idle (w, FALSE);
674 return MSG_HANDLED;
676 case MSG_RESIZE:
677 /* this is default resizing mechanism */
678 /* the main idea of this code is to resize dialog
679 according to flags (if any of flags require automatic
680 resizing, like WPOS_CENTER, end after that reposition
681 controls in dialog according to flags of widget) */
682 dlg_set_size (h, w->lines, w->cols);
683 return MSG_HANDLED;
685 default:
686 break;
689 return MSG_NOT_HANDLED;
692 /* --------------------------------------------------------------------------------------------- */
694 WDialog *
695 dlg_create (gboolean modal, int y1, int x1, int lines, int cols, widget_pos_flags_t pos_flags,
696 gboolean compact, const int *colors, widget_cb_fn callback,
697 widget_mouse_cb_fn mouse_callback, const char *help_ctx, const char *title)
699 WDialog *new_d;
700 Widget *w;
702 new_d = g_new0 (WDialog, 1);
703 w = WIDGET (new_d);
704 widget_adjust_position (pos_flags, &y1, &x1, &lines, &cols);
705 widget_init (w, y1, x1, lines, cols, (callback != NULL) ? callback : dlg_default_callback,
706 mouse_callback);
707 w->pos_flags = pos_flags;
708 w->options |= WOP_SELECTABLE | WOP_TOP_SELECT;
710 w->state |= WST_CONSTRUCT | WST_FOCUSED;
711 if (modal)
712 w->state |= WST_MODAL;
714 new_d->color = colors;
715 new_d->help_ctx = help_ctx;
716 new_d->compact = compact;
717 new_d->data = NULL;
719 new_d->mouse_status = MOU_UNHANDLED;
721 dlg_set_title (new_d, title);
723 /* unique name of event group for this dialog */
724 new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
726 return new_d;
729 /* --------------------------------------------------------------------------------------------- */
731 void
732 dlg_set_default_colors (void)
734 dialog_colors[DLG_COLOR_NORMAL] = COLOR_NORMAL;
735 dialog_colors[DLG_COLOR_FOCUS] = COLOR_FOCUS;
736 dialog_colors[DLG_COLOR_HOT_NORMAL] = COLOR_HOT_NORMAL;
737 dialog_colors[DLG_COLOR_HOT_FOCUS] = COLOR_HOT_FOCUS;
738 dialog_colors[DLG_COLOR_TITLE] = COLOR_TITLE;
740 alarm_colors[DLG_COLOR_NORMAL] = ERROR_COLOR;
741 alarm_colors[DLG_COLOR_FOCUS] = ERROR_FOCUS;
742 alarm_colors[DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL;
743 alarm_colors[DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS;
744 alarm_colors[DLG_COLOR_TITLE] = ERROR_TITLE;
746 listbox_colors[DLG_COLOR_NORMAL] = PMENU_ENTRY_COLOR;
747 listbox_colors[DLG_COLOR_FOCUS] = PMENU_SELECTED_COLOR;
748 listbox_colors[DLG_COLOR_HOT_NORMAL] = PMENU_ENTRY_COLOR;
749 listbox_colors[DLG_COLOR_HOT_FOCUS] = PMENU_SELECTED_COLOR;
750 listbox_colors[DLG_COLOR_TITLE] = PMENU_TITLE_COLOR;
753 /* --------------------------------------------------------------------------------------------- */
755 void
756 dlg_erase (WDialog * h)
758 Widget *wh = WIDGET (h);
760 if (wh != NULL && widget_get_state (wh, WST_ACTIVE))
761 tty_fill_region (wh->y, wh->x, wh->lines, wh->cols, ' ');
764 /* --------------------------------------------------------------------------------------------- */
766 void
767 do_refresh (void)
769 GList *d = top_dlg;
771 if (fast_refresh)
773 if (d != NULL)
774 dlg_draw (DIALOG (d->data));
776 else
778 /* Search first fullscreen dialog */
779 for (; d != NULL; d = g_list_next (d))
780 if ((WIDGET (d->data)->pos_flags & WPOS_FULLSCREEN) != 0)
781 break;
782 /* back to top dialog */
783 for (; d != NULL; d = g_list_previous (d))
784 dlg_draw (DIALOG (d->data));
788 /* --------------------------------------------------------------------------------------------- */
789 /** broadcast a message to all the widgets in a dialog */
791 void
792 dlg_broadcast_msg (WDialog * h, widget_msg_t msg)
794 dlg_broadcast_msg_to (h, msg, FALSE, 0);
797 /* --------------------------------------------------------------------------------------------- */
798 /** Find the widget with the given callback in the dialog h */
800 Widget *
801 find_widget_type (const WDialog * h, widget_cb_fn callback)
803 GList *w;
805 w = g_list_find_custom (CONST_GROUP (h)->widgets, (gconstpointer) callback,
806 dlg_find_widget_callback);
808 return (w == NULL) ? NULL : WIDGET (w->data);
811 /* --------------------------------------------------------------------------------------------- */
813 GList *
814 dlg_find (const WDialog * h, const Widget * w)
816 const WGroup *g = CONST_GROUP (h);
818 return (w->owner == NULL || w->owner != g) ? NULL : g_list_find (g->widgets, w);
821 /* --------------------------------------------------------------------------------------------- */
822 /** Find the widget with the given id */
824 Widget *
825 dlg_find_by_id (const WDialog * h, unsigned long id)
827 GList *w;
829 w = g_list_find_custom (CONST_GROUP (h)->widgets, GUINT_TO_POINTER (id), dlg_find_widget_by_id);
830 return w != NULL ? WIDGET (w->data) : NULL;
833 /* --------------------------------------------------------------------------------------------- */
835 void
836 update_cursor (WDialog * h)
838 WGroup *g = GROUP (h);
839 GList *p = g->current;
841 if (p != NULL && widget_get_state (WIDGET (h), WST_ACTIVE))
843 Widget *w = WIDGET (p->data);
845 if (!widget_get_state (w, WST_DISABLED) && widget_get_options (w, WOP_WANT_CURSOR))
846 send_message (w, NULL, MSG_CURSOR, 0, NULL);
847 else
850 p = group_get_widget_next_of (p);
851 if (p == g->current)
852 break;
854 w = WIDGET (p->data);
856 if (!widget_get_state (w, WST_DISABLED) && widget_get_options (w, WOP_WANT_CURSOR)
857 && send_message (w, NULL, MSG_CURSOR, 0, NULL) == MSG_HANDLED)
858 break;
860 while (TRUE);
864 /* --------------------------------------------------------------------------------------------- */
866 * Redraw the widgets in reverse order, leaving the current widget
867 * as the last one
870 void
871 dlg_draw (WDialog * h)
873 WGroup *g = GROUP (h);
875 if (!widget_get_state (WIDGET (h), WST_ACTIVE))
876 return;
878 if (g->winch_pending)
880 g->winch_pending = FALSE;
881 send_message (h, NULL, MSG_RESIZE, 0, NULL);
884 send_message (h, NULL, MSG_DRAW, 0, NULL);
885 dlg_broadcast_msg (h, MSG_DRAW);
886 update_cursor (h);
889 /* --------------------------------------------------------------------------------------------- */
891 void
892 dlg_stop (WDialog * h)
894 widget_set_state (WIDGET (h), WST_CLOSED, TRUE);
897 /* --------------------------------------------------------------------------------------------- */
898 /** Init the process */
900 void
901 dlg_init (WDialog * h)
903 WGroup *g = GROUP (h);
904 Widget *wh = WIDGET (h);
906 if (top_dlg != NULL && widget_get_state (WIDGET (top_dlg->data), WST_MODAL))
907 widget_set_state (wh, WST_MODAL, TRUE);
909 /* add dialog to the stack */
910 top_dlg = g_list_prepend (top_dlg, h);
912 /* Initialize dialog manager and widgets */
913 if (widget_get_state (wh, WST_CONSTRUCT))
915 if (!widget_get_state (wh, WST_MODAL))
916 dialog_switch_add (h);
918 send_message (h, NULL, MSG_INIT, 0, NULL);
919 dlg_broadcast_msg (h, MSG_INIT);
920 dlg_read_history (h);
923 /* Select the first widget that takes focus */
924 while (g->current != NULL && !widget_get_options (WIDGET (g->current->data), WOP_SELECTABLE)
925 && !widget_get_state (WIDGET (g->current->data), WST_DISABLED))
926 group_set_current_widget_next (g);
928 widget_set_state (wh, WST_ACTIVE, TRUE);
929 dlg_draw (h);
930 /* focus found widget */
931 if (g->current != NULL)
932 widget_set_state (WIDGET (g->current->data), WST_FOCUSED, TRUE);
934 h->ret_value = 0;
937 /* --------------------------------------------------------------------------------------------- */
939 void
940 dlg_process_event (WDialog * h, int key, Gpm_Event * event)
942 switch (key)
944 case EV_NONE:
945 if (tty_got_interrupt ())
946 dlg_execute_cmd (h, CK_Cancel);
947 break;
949 case EV_MOUSE:
950 h->mouse_status = dlg_mouse_event (h, event);
951 break;
953 default:
954 dlg_key_event (h, key);
955 break;
959 /* --------------------------------------------------------------------------------------------- */
960 /** Shutdown the dlg_run */
962 void
963 dlg_run_done (WDialog * h)
965 top_dlg = g_list_remove (top_dlg, h);
967 if (widget_get_state (WIDGET (h), WST_CLOSED))
969 send_message (h, GROUP (h)->current == NULL ? NULL : WIDGET (GROUP (h)->current->data),
970 MSG_END, 0, NULL);
971 if (!widget_get_state (WIDGET (h), WST_MODAL))
972 dialog_switch_remove (h);
976 /* --------------------------------------------------------------------------------------------- */
978 * Standard run dialog routine
979 * We have to keep this routine small so that we can duplicate it's
980 * behavior on complex routines like the file routines, this way,
981 * they can call the dlg_process_event without rewriting all the code
985 dlg_run (WDialog * h)
987 dlg_init (h);
988 frontend_dlg_run (h);
989 dlg_run_done (h);
990 return h->ret_value;
993 /* --------------------------------------------------------------------------------------------- */
995 void
996 dlg_destroy (WDialog * h)
998 WGroup *g = GROUP (h);
1000 /* if some widgets have history, save all history at one moment here */
1001 dlg_save_history (h);
1002 g_list_foreach (g->widgets, (GFunc) widget_destroy, NULL);
1003 g_list_free (g->widgets);
1004 mc_event_group_del (h->event_group);
1005 g_free (h->event_group);
1006 g_free (h->title);
1007 g_free (h);
1009 do_refresh ();
1012 /* --------------------------------------------------------------------------------------------- */
1015 * Write history to the ${XDG_CACHE_HOME}/mc/history file
1017 void
1018 dlg_save_history (WDialog * h)
1020 char *profile;
1021 int i;
1023 if (num_history_items_recorded == 0) /* this is how to disable */
1024 return;
1026 profile = mc_config_get_full_path (MC_HISTORY_FILE);
1027 i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
1028 if (i != -1)
1029 close (i);
1031 /* Make sure the history is only readable by the user */
1032 if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
1034 ev_history_load_save_t event_data;
1036 event_data.cfg = mc_config_init (profile, FALSE);
1037 event_data.receiver = NULL;
1039 /* get all histories in dialog */
1040 mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data);
1042 mc_config_save_file (event_data.cfg, NULL);
1043 mc_config_deinit (event_data.cfg);
1046 g_free (profile);
1049 /* --------------------------------------------------------------------------------------------- */
1051 void
1052 dlg_set_title (WDialog * h, const char *title)
1054 MC_PTR_FREE (h->title);
1056 /* Strip existing spaces, add one space before and after the title */
1057 if (title != NULL && title[0] != '\0')
1059 char *t;
1061 t = g_strstrip (g_strdup (title));
1062 if (t[0] != '\0')
1063 h->title = g_strdup_printf (" %s ", t);
1064 g_free (t);
1068 /* --------------------------------------------------------------------------------------------- */
1070 char *
1071 dlg_get_title (const WDialog * h, size_t len)
1073 char *t;
1075 if (h == NULL)
1076 abort ();
1078 if (h->get_title != NULL)
1079 t = h->get_title (h, len);
1080 else
1081 t = g_strdup ("");
1083 return t;
1086 /* --------------------------------------------------------------------------------------------- */