Removed hardcoded hotkeys in dialog.c Replaced keymap's initialization from main...
[midnight-commander.git] / src / dialog.c
blob96b687c3bf8842d0cd90f1d8787c2cc196a05fa6
1 /* Dialog box features module for the Midnight Commander
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2007 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 /** \file dialog.c
21 * \brief Source: dialog box features module
24 #include <config.h>
26 #include <ctype.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include "lib/global.h"
34 #include "lib/tty/tty.h"
35 #include "lib/skin.h"
36 #include "lib/tty/mouse.h"
37 #include "lib/tty/key.h"
38 #include "lib/strutil.h"
40 #include "help.h" /* interactive_display() */
41 #include "dialog.h"
42 #include "layout.h"
43 #include "execute.h" /* suspend_cmd() */
44 #include "cmddef.h"
45 #include "keybind.h"
46 #include "main.h" /* fast_refresh */
47 #include "setup.h" /* mouse_close_dialog */
49 /* Color styles for normal and error dialogs */
50 int dialog_colors[4];
51 int alarm_colors[4];
53 /* Primitive way to check if the the current dialog is our dialog */
54 /* This is needed by async routines like load_prompt */
55 Dlg_head *current_dlg = 0;
57 /* A hook list for idle events */
58 Hook *idle_hook = 0;
60 /* left click outside of dialog closes it */
61 int mouse_close_dialog = 0;
63 static void dlg_broadcast_msg_to (Dlg_head * h, widget_msg_t message, int reverse, int flags);
65 /* draw box in window */
66 void
67 draw_box (Dlg_head * h, int y, int x, int ys, int xs, gboolean single)
69 tty_draw_box (h->y + y, h->x + x, ys, xs, single);
72 void
73 widget_erase (Widget * w)
75 tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
78 void
79 dlg_erase (Dlg_head * h)
81 if (h != NULL)
82 tty_fill_region (h->y, h->x, h->lines, h->cols, ' ');
85 void
86 init_widget (Widget * w, int y, int x, int lines, int cols,
87 callback_fn callback, mouse_h mouse_handler)
89 w->x = x;
90 w->y = y;
91 w->cols = cols;
92 w->lines = lines;
93 w->callback = callback;
94 w->mouse = mouse_handler;
95 w->parent = 0;
97 /* Almost all widgets want to put the cursor in a suitable place */
98 w->options = W_WANT_CURSOR;
101 /* Clean the dialog area, draw the frame and the title */
102 void
103 common_dialog_repaint (struct Dlg_head *h)
105 int space;
107 space = (h->flags & DLG_COMPACT) ? 0 : 1;
109 tty_setcolor (DLG_NORMALC (h));
110 dlg_erase (h);
111 draw_box (h, space, space, h->lines - 2 * space, h->cols - 2 * space, FALSE);
113 if (h->title)
115 tty_setcolor (DLG_HOT_NORMALC (h));
116 dlg_move (h, space, (h->cols - str_term_width1 (h->title)) / 2);
117 tty_print_string (h->title);
121 /* this function allows to set dialog position */
122 void
123 dlg_set_position (Dlg_head * h, int y1, int x1, int y2, int x2)
125 /* save old positions, will be used to reposition childs */
126 int ox, oy, oc, ol;
127 int shift_x, shift_y, scale_x, scale_y;
129 /* save old positions, will be used to reposition childs */
130 ox = h->x;
131 oy = h->y;
132 oc = h->cols;
133 ol = h->lines;
135 h->x = x1;
136 h->y = y1;
137 h->lines = y2 - y1;
138 h->cols = x2 - x1;
140 /* values by which controls should be moved */
141 shift_x = h->x - ox;
142 shift_y = h->y - oy;
143 scale_x = h->cols - oc;
144 scale_y = h->lines - ol;
146 if (h->current == NULL)
147 return;
149 if ((shift_x != 0) || (shift_y != 0) || (scale_x != 0) || (scale_y != 0))
151 Widget *c = h->current;
155 /* there are, mainly, 2 generally possible
156 situations:
158 1. control sticks to one side - it
159 should be moved
161 2. control sticks to two sides of
162 one direction - it should be sized */
164 int x = c->x;
165 int y = c->y;
166 int cols = c->cols;
167 int lines = c->lines;
169 if ((c->pos_flags & WPOS_KEEP_LEFT) && (c->pos_flags & WPOS_KEEP_RIGHT))
171 x += shift_x;
172 cols += scale_x;
174 else if (c->pos_flags & WPOS_KEEP_LEFT)
175 x += shift_x;
176 else if (c->pos_flags & WPOS_KEEP_RIGHT)
177 x += shift_x + scale_x;
179 if ((c->pos_flags & WPOS_KEEP_TOP) && (c->pos_flags & WPOS_KEEP_BOTTOM))
181 y += shift_y;
182 lines += scale_y;
184 else if (c->pos_flags & WPOS_KEEP_TOP)
185 y += shift_y;
186 else if (c->pos_flags & WPOS_KEEP_BOTTOM)
187 y += shift_y + scale_y;
189 widget_set_size (c, y, x, lines, cols);
191 c = c->next;
193 while (h->current != c);
197 /* this function sets only size, leaving positioning to automatic methods */
198 void
199 dlg_set_size (Dlg_head * h, int lines, int cols)
201 int x = h->x;
202 int y = h->y;
204 if (h->flags & DLG_CENTER)
206 y = (LINES - lines) / 2;
207 x = (COLS - cols) / 2;
210 if ((h->flags & DLG_TRYUP) && (y > 3))
211 y -= 2;
213 dlg_set_position (h, y, x, y + lines, x + cols);
216 /* Default dialog callback */
217 cb_ret_t
218 default_dlg_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
220 (void) sender;
221 (void) parm;
222 (void) data;
224 switch (msg)
226 case DLG_DRAW:
227 if (h->color != NULL)
229 common_dialog_repaint (h);
230 return MSG_HANDLED;
232 return MSG_NOT_HANDLED;
234 case DLG_IDLE:
235 dlg_broadcast_msg_to (h, WIDGET_IDLE, 0, W_WANT_IDLE);
236 return MSG_HANDLED;
238 case DLG_RESIZE:
239 /* this is default resizing mechanism */
240 /* the main idea of this code is to resize dialog
241 according to flags (if any of flags require automatic
242 resizing, like DLG_CENTER, end after that reposition
243 controls in dialog according to flags of widget) */
244 dlg_set_size (h, h->lines, h->cols);
245 return MSG_HANDLED;
247 default:
248 break;
251 return MSG_NOT_HANDLED;
254 Dlg_head *
255 create_dlg (int y1, int x1, int lines, int cols, const int *colors,
256 dlg_cb_fn callback, const char *help_ctx, const char *title, int flags)
258 Dlg_head *new_d;
260 new_d = g_new0 (Dlg_head, 1);
261 if (colors != NULL)
263 new_d->color = g_new (int, DLG_COLOR_NUM);
264 memmove (new_d->color, colors, sizeof (int) * DLG_COLOR_NUM);
266 new_d->help_ctx = help_ctx;
267 new_d->callback = (callback != NULL) ? callback : default_dlg_callback;
268 new_d->x = x1;
269 new_d->y = y1;
270 new_d->flags = flags;
271 new_d->data = NULL;
273 dlg_set_size (new_d, lines, cols);
275 /* Strip existing spaces, add one space before and after the title */
276 if (title)
278 char *t;
280 t = g_strstrip (g_strdup (title));
281 if (*t != '\0')
282 new_d->title = g_strdup_printf (" %s ", t);
283 g_free (t);
286 return new_d;
289 void
290 dlg_set_default_colors (void)
292 dialog_colors[0] = COLOR_NORMAL;
293 dialog_colors[1] = COLOR_FOCUS;
294 dialog_colors[2] = COLOR_HOT_NORMAL;
295 dialog_colors[3] = COLOR_HOT_FOCUS;
297 alarm_colors[0] = ERROR_COLOR;
298 alarm_colors[1] = REVERSE_COLOR;
299 alarm_colors[2] = ERROR_HOT_NORMAL;
300 alarm_colors[3] = ERROR_HOT_FOCUS;
303 void
304 set_idle_proc (Dlg_head * d, int enable)
306 if (enable)
307 d->flags |= DLG_WANT_IDLE;
308 else
309 d->flags &= ~DLG_WANT_IDLE;
313 * Insert widget to dialog before current widget. For dialogs populated
314 * from the bottom, make the widget current. Return widget number.
317 add_widget_autopos (Dlg_head * h, void *w, widget_pos_flags_t pos_flags)
319 Widget *widget = (Widget *) w;
321 /* Don't accept 0 widgets, and running dialogs */
322 if (!widget || h->running)
323 abort ();
325 widget->x += h->x;
326 widget->y += h->y;
327 widget->parent = h;
328 widget->dlg_id = h->count++;
329 widget->pos_flags = pos_flags;
331 if (h->current)
333 widget->next = h->current;
334 widget->prev = h->current->prev;
335 h->current->prev->next = widget;
336 h->current->prev = widget;
338 else
340 widget->prev = widget;
341 widget->next = widget;
344 if ((h->flags & DLG_REVERSE) || !h->current)
345 h->current = widget;
347 return widget->dlg_id;
350 /* wrapper to simply add lefttop positioned controls */
352 add_widget (Dlg_head * h, void *w)
354 return add_widget_autopos (h, w, WPOS_KEEP_LEFT | WPOS_KEEP_TOP);
357 static void
358 do_complete_refresh (Dlg_head * dlg)
360 if (!dlg->fullscreen && dlg->parent)
361 do_complete_refresh (dlg->parent);
363 dlg_redraw (dlg);
366 void
367 do_refresh (void)
369 if (!current_dlg)
370 return;
372 if (fast_refresh)
373 dlg_redraw (current_dlg);
374 else
375 do_complete_refresh (current_dlg);
378 /* broadcast a message to all the widgets in a dialog that have
379 * the options set to flags. If flags is zero, the message is sent
380 * to all widgets.
382 static void
383 dlg_broadcast_msg_to (Dlg_head * h, widget_msg_t message, int reverse, int flags)
385 Widget *p, *first, *wi;
387 if (!h->current)
388 return;
390 if (reverse)
391 first = p = h->current->prev;
392 else
393 first = p = h->current->next;
397 wi = p;
398 if (reverse)
399 p = p->prev;
400 else
401 p = p->next;
402 if (flags == 0 || (flags & wi->options))
403 send_message (wi, message, 0);
405 while (first != p);
408 /* broadcast a message to all the widgets in a dialog */
409 void
410 dlg_broadcast_msg (Dlg_head * h, widget_msg_t message, int reverse)
412 dlg_broadcast_msg_to (h, message, reverse, 0);
416 dlg_focus (Dlg_head * h)
418 if ((h->current != NULL) && (send_message (h->current, WIDGET_FOCUS, 0) == MSG_HANDLED))
420 h->callback (h, h->current, DLG_FOCUS, 0, NULL);
421 return 1;
423 return 0;
426 static int
427 dlg_unfocus (Dlg_head * h)
429 if ((h->current != NULL) && (send_message (h->current, WIDGET_UNFOCUS, 0) == MSG_HANDLED))
431 h->callback (h, h->current, DLG_UNFOCUS, 0, NULL);
432 return 1;
434 return 0;
438 /* Return true if the windows overlap */
440 dlg_overlap (Widget * a, Widget * b)
442 if ((b->x >= a->x + a->cols)
443 || (a->x >= b->x + b->cols) || (b->y >= a->y + a->lines) || (a->y >= b->y + b->lines))
444 return 0;
445 return 1;
449 /* Find the widget with the given callback in the dialog h */
450 Widget *
451 find_widget_type (const Dlg_head * h, callback_fn callback)
453 Widget *w = NULL;
455 if ((h != NULL) && (h->current != NULL))
457 int i;
458 Widget *item;
460 for (i = 0, item = h->current; i < h->count; i++, item = item->next)
462 if (item->callback == callback)
464 w = item;
465 break;
470 return w;
473 /* Find the widget with the given dialog id in the dialog h and select it */
474 void
475 dlg_select_by_id (const Dlg_head * h, int id)
477 Widget *w, *w_found;
479 if (!h->current)
480 return;
482 w = h->current;
483 w_found = NULL;
487 if (w->dlg_id == id)
489 w_found = w;
490 break;
492 w = w->next;
494 while (w != h->current);
496 if (w_found)
497 dlg_select_widget (w_found);
501 /* What to do if the requested widget doesn't take focus */
502 typedef enum
504 SELECT_NEXT, /* go the the next widget */
505 SELECT_PREV, /* go the the previous widget */
506 SELECT_EXACT /* use current widget */
507 } select_dir_t;
510 * Try to select another widget. If forward is set, follow tab order.
511 * Otherwise go to the previous widget.
513 static void
514 do_select_widget (Dlg_head * h, Widget * w, select_dir_t dir)
516 Widget *w0 = h->current;
518 if (!dlg_unfocus (h))
519 return;
521 h->current = w;
524 if (dlg_focus (h))
525 break;
527 switch (dir)
529 case SELECT_NEXT:
530 h->current = h->current->next;
531 break;
532 case SELECT_PREV:
533 h->current = h->current->prev;
534 break;
535 case SELECT_EXACT:
536 h->current = w0;
537 dlg_focus (h);
538 return;
541 while (h->current != w);
543 if (dlg_overlap (w0, h->current))
545 send_message (h->current, WIDGET_DRAW, 0);
546 send_message (h->current, WIDGET_FOCUS, 0);
552 * Try to select widget in the dialog.
554 void
555 dlg_select_widget (void *w)
557 do_select_widget (((Widget *) w)->parent, w, SELECT_NEXT);
561 /* Try to select previous widget in the tab order */
562 void
563 dlg_one_up (Dlg_head * h)
565 if (h->current)
566 do_select_widget (h, h->current->prev, SELECT_PREV);
570 /* Try to select next widget in the tab order */
571 void
572 dlg_one_down (Dlg_head * h)
574 if (h->current)
575 do_select_widget (h, h->current->next, SELECT_NEXT);
579 void
580 update_cursor (Dlg_head * h)
582 Widget *p = h->current;
584 if (p != NULL)
586 if (p->options & W_WANT_CURSOR)
587 send_message (p, WIDGET_CURSOR, 0);
588 else
589 while ((p = p->next) != h->current)
590 if (p->options & W_WANT_CURSOR)
591 if (send_message (p, WIDGET_CURSOR, 0) == MSG_HANDLED)
592 break;
596 /* Redraw the widgets in reverse order, leaving the current widget
597 * as the last one
599 void
600 dlg_redraw (Dlg_head * h)
602 h->callback (h, NULL, DLG_DRAW, 0, NULL);
603 dlg_broadcast_msg (h, WIDGET_DRAW, 1);
604 update_cursor (h);
607 void
608 dlg_stop (Dlg_head * h)
610 h->running = 0;
613 static void
614 refresh_cmd (void)
616 #ifdef HAVE_SLANG
617 tty_touch_screen ();
618 mc_refresh ();
619 #else
620 /* Use this if the refreshes fail */
621 clr_scr ();
622 repaint_screen ();
623 #endif /* HAVE_SLANG */
626 static cb_ret_t
627 dlg_execute_cmd (Dlg_head * h, unsigned long command)
629 cb_ret_t ret = MSG_HANDLED;
630 switch (command)
632 case CK_DialogOK:
633 h->ret_value = B_ENTER;
634 dlg_stop (h);
635 break;
636 case CK_DialogCancel:
637 h->ret_value = B_CANCEL;
638 dlg_stop (h);
639 break;
640 case CK_DialogPrevItem:
641 dlg_one_up (h);
642 break;
643 case CK_DialogNextItem:
644 dlg_one_down (h);
645 break;
646 case CK_DialogHelp:
647 interactive_display (NULL, h->help_ctx);
648 do_refresh ();
649 break;
650 case CK_DialogSuspend:
651 suspend_cmd ();
652 refresh_cmd ();
653 break;
654 case CK_DialogRefresh:
655 refresh_cmd ();
656 break;
657 default:
658 ret = MSG_NOT_HANDLED;
661 return ret;
664 static cb_ret_t
665 dlg_handle_key (Dlg_head * h, int d_key)
667 unsigned long command;
668 command = lookup_keymap_command (dialog_map, d_key);
669 if ((command == CK_Ignore_Key) || (dlg_execute_cmd (h, command) == MSG_NOT_HANDLED))
670 return MSG_NOT_HANDLED;
671 else
672 return MSG_HANDLED;
675 static cb_ret_t
676 dlg_try_hotkey (Dlg_head * h, int d_key)
678 Widget *hot_cur;
679 cb_ret_t handled;
680 int c;
682 if (h->current == NULL)
683 return MSG_NOT_HANDLED;
686 * Explanation: we don't send letter hotkeys to other widgets if
687 * the currently selected widget is an input line
690 if (h->current->options & W_IS_INPUT)
692 /* skip ascii control characters, anything else can valid character in
693 * some encoding */
694 if (d_key >= 32 && d_key < 256)
695 return MSG_NOT_HANDLED;
698 /* If it's an alt key, send the message */
699 c = d_key & ~ALT (0);
700 if (d_key & ALT (0) && g_ascii_isalpha (c))
701 d_key = g_ascii_tolower (c);
703 handled = MSG_NOT_HANDLED;
704 if (h->current->options & W_WANT_HOTKEY)
705 handled = send_message (h->current, WIDGET_HOTKEY, d_key);
707 /* If not used, send hotkey to other widgets */
708 if (handled == MSG_HANDLED)
709 return MSG_HANDLED;
711 hot_cur = h->current->next;
713 /* send it to all widgets */
714 while (h->current != hot_cur && handled == MSG_NOT_HANDLED)
716 if (hot_cur->options & W_WANT_HOTKEY)
717 handled = send_message (hot_cur, WIDGET_HOTKEY, d_key);
719 if (handled == MSG_NOT_HANDLED)
720 hot_cur = hot_cur->next;
723 if (handled == MSG_HANDLED)
724 do_select_widget (h, hot_cur, SELECT_EXACT);
726 return handled;
729 static void
730 dlg_key_event (Dlg_head * h, int d_key)
732 cb_ret_t handled;
734 if (h->current == NULL)
735 return;
737 /* TAB used to cycle */
738 if (!(h->flags & DLG_WANT_TAB))
740 if (d_key == '\t')
742 dlg_one_down (h);
743 return;
745 else if (d_key == KEY_BTAB)
747 dlg_one_up (h);
748 return;
752 /* first can dlg_callback handle the key */
753 handled = h->callback (h, NULL, DLG_KEY, d_key, NULL);
755 /* next try the hotkey */
756 if (handled == MSG_NOT_HANDLED)
757 handled = dlg_try_hotkey (h, d_key);
759 if (handled == MSG_HANDLED)
760 h->callback (h, NULL, DLG_HOTKEY_HANDLED, 0, NULL);
761 else
762 /* not used - then try widget_callback */
763 handled = send_message (h->current, WIDGET_KEY, d_key);
765 /* not used- try to use the unhandled case */
766 if (handled == MSG_NOT_HANDLED)
767 handled = h->callback (h, NULL, DLG_UNHANDLED_KEY, d_key, NULL);
769 if (handled == MSG_NOT_HANDLED)
770 handled = dlg_handle_key (h, d_key);
772 h->callback (h, NULL, DLG_POST_KEY, d_key, NULL);
775 static int
776 dlg_mouse_event (Dlg_head * h, Gpm_Event * event)
778 Widget *item;
779 Widget *starting_widget = h->current;
780 Gpm_Event new_event;
781 int x = event->x;
782 int y = event->y;
784 /* close the dialog by mouse click out of dialog area */
785 if (mouse_close_dialog && !h->fullscreen && ((event->buttons & GPM_B_LEFT) != 0) && ((event->type & GPM_DOWN) != 0) /* left click */
786 && !((x > h->x) && (x <= h->x + h->cols) && (y > h->y) && (y <= h->y + h->lines)))
788 h->ret_value = B_CANCEL;
789 dlg_stop (h);
790 return MOU_NORMAL;
793 item = starting_widget;
796 Widget *widget;
798 widget = item;
799 item = item->next;
801 if ((x > widget->x) && (x <= widget->x + widget->cols)
802 && (y > widget->y) && (y <= widget->y + widget->lines))
804 new_event = *event;
805 new_event.x -= widget->x;
806 new_event.y -= widget->y;
808 if (widget->mouse != NULL)
809 return widget->mouse (&new_event, widget);
812 while (item != starting_widget);
814 return MOU_NORMAL;
817 /* Run dialog routines */
819 /* Init the process */
820 void
821 init_dlg (Dlg_head * h)
823 /* Initialize dialog manager and widgets */
824 h->callback (h, NULL, DLG_INIT, 0, NULL);
825 dlg_broadcast_msg (h, WIDGET_INIT, 0);
827 if (h->x == 0 && h->y == 0 && h->cols == COLS && h->lines == LINES)
828 h->fullscreen = 1;
830 h->parent = current_dlg;
831 current_dlg = h;
833 /* Initialize the mouse status */
834 h->mouse_status = MOU_NORMAL;
836 /* Select the first widget that takes focus */
837 while (!dlg_focus (h) && h->current)
838 h->current = h->current->next;
840 /* Redraw the screen */
841 dlg_redraw (h);
843 h->ret_value = 0;
844 h->running = 1;
847 /* Shutdown the run_dlg */
848 void
849 dlg_run_done (Dlg_head * h)
851 if (h->current != NULL)
852 h->callback (h, h->current, DLG_END, 0, NULL);
854 current_dlg = h->parent;
857 void
858 dlg_process_event (Dlg_head * h, int key, Gpm_Event * event)
860 if (key == EV_NONE)
862 if (tty_got_interrupt ())
863 dlg_execute_cmd (h, CK_DialogCancel);
865 return;
868 if (key == EV_MOUSE)
869 h->mouse_status = dlg_mouse_event (h, event);
870 else
871 dlg_key_event (h, key);
874 static void
875 frontend_run_dlg (Dlg_head * h)
877 int d_key;
878 Gpm_Event event;
880 event.x = -1;
881 while (h->running)
883 if (winch_flag)
884 change_screen_size ();
886 if (is_idle ())
888 if (idle_hook)
889 execute_hooks (idle_hook);
891 while ((h->flags & DLG_WANT_IDLE) && is_idle ())
892 h->callback (h, NULL, DLG_IDLE, 0, NULL);
894 /* Allow terminating the dialog from the idle handler */
895 if (!h->running)
896 break;
899 update_cursor (h);
901 /* Clear interrupt flag */
902 tty_got_interrupt ();
903 d_key = tty_get_event (&event, h->mouse_status == MOU_REPEAT, TRUE);
905 dlg_process_event (h, d_key, &event);
907 if (!h->running)
908 h->callback (h, NULL, DLG_VALIDATE, 0, NULL);
912 /* Standard run dialog routine
913 * We have to keep this routine small so that we can duplicate it's
914 * behavior on complex routines like the file routines, this way,
915 * they can call the dlg_process_event without rewriting all the code
918 run_dlg (Dlg_head * h)
920 init_dlg (h);
921 frontend_run_dlg (h);
922 dlg_run_done (h);
923 return h->ret_value;
926 void
927 destroy_dlg (Dlg_head * h)
929 int i;
930 Widget *c;
932 dlg_broadcast_msg (h, WIDGET_DESTROY, 0);
933 c = h->current;
934 for (i = 0; i < h->count; i++)
936 c = c->next;
937 g_free (h->current);
938 h->current = c;
940 g_free (h->color);
941 g_free (h->title);
942 g_free (h);
944 do_refresh ();
947 void
948 widget_set_size (Widget * widget, int y, int x, int lines, int cols)
950 widget->x = x;
951 widget->y = y;
952 widget->cols = cols;
953 widget->lines = lines;
954 send_message (widget, WIDGET_RESIZED, 0 /* unused */ );
957 /* Replace widget old_w for widget new_w in the dialog */
958 void
959 dlg_replace_widget (Widget * old_w, Widget * new_w)
961 Dlg_head *h = old_w->parent;
962 int should_focus = 0;
964 if (!h->current)
965 return;
967 if (old_w == h->current)
968 should_focus = 1;
970 new_w->parent = h;
971 new_w->dlg_id = old_w->dlg_id;
973 if (old_w == old_w->next)
975 /* just one widget */
976 new_w->prev = new_w;
977 new_w->next = new_w;
979 else
981 new_w->prev = old_w->prev;
982 new_w->next = old_w->next;
983 old_w->prev->next = new_w;
984 old_w->next->prev = new_w;
987 if (should_focus)
988 h->current = new_w;
990 send_message (old_w, WIDGET_DESTROY, 0);
991 send_message (new_w, WIDGET_INIT, 0);
993 if (should_focus)
994 dlg_select_widget (new_w);
996 send_message (new_w, WIDGET_DRAW, 0);