Merge branch '72_preserve_attributes'
[midnight-commander.git] / src / dialog.h
blob919e8665799238f55eeb1440ae54d0c62987f770
1 /* Dialog box features module for the Midnight Commander
2 Copyright (C) 1994, 1995 Radek Doulik, Miguel de Icaza
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 /** \file dialog.h
20 * \brief Header: dialog box features module
23 #ifndef MC_DIALOG_H
24 #define MC_DIALOG_H
26 #include "mouse.h"
27 #include "util.h" /* Hook */
29 /* Color constants */
30 #define DLG_NORMALC(h) ((h)->color[0])
31 #define DLG_FOCUSC(h) ((h)->color[1])
32 #define DLG_HOT_NORMALC(h) ((h)->color[2])
33 #define DLG_HOT_FOCUSC(h) ((h)->color[3])
35 /* Common return values */
36 #define B_EXIT 0
37 #define B_CANCEL 1
38 #define B_ENTER 2
39 #define B_HELP 3
40 #define B_USER 100
42 /* Widget messages */
43 typedef enum {
44 WIDGET_INIT, /* Initialize widget */
45 WIDGET_FOCUS, /* Draw widget in focused state */
46 WIDGET_UNFOCUS, /* Draw widget in unfocused state */
47 WIDGET_DRAW, /* Sent to widget to draw themselves */
48 WIDGET_KEY, /* Sent to widgets on key press */
49 WIDGET_HOTKEY, /* Sent to widget to catch preprocess key */
50 WIDGET_DESTROY, /* Sent to widget at destruction time */
51 WIDGET_CURSOR, /* Sent to widget to position the cursor */
52 WIDGET_IDLE, /* Sent to widgets with options & W_WANT_IDLE*/
53 WIDGET_RESIZED /* Sent after a widget has been resized */
54 } widget_msg_t;
56 typedef enum {
57 MSG_NOT_HANDLED = 0,
58 MSG_HANDLED = 1
59 } cb_ret_t;
61 /* Widgets are expected to answer to the following messages:
63 WIDGET_FOCUS: 1 if the accept the focus, 0 if they do not.
64 WIDGET_UNFOCUS: 1 if they accept to release the focus, 0 if they don't.
65 WIDGET_KEY: 1 if they actually used the key, 0 if not.
66 WIDGET_HOTKEY: 1 if they actually used the key, 0 if not.
69 /* Dialog messages */
70 typedef enum {
71 DLG_KEY, /* Key before sending to widget */
72 DLG_INIT, /* Initialize dialog */
73 DLG_END, /* Shut down dialog */
74 DLG_ACTION, /* State of check- and radioboxes has changed
75 * and listbox current entry has changed */
76 DLG_DRAW, /* Draw dialog on screen */
77 DLG_FOCUS, /* A widget has got focus */
78 DLG_UNFOCUS, /* A widget has been unfocused */
79 DLG_RESIZE, /* Window size has changed */
80 DLG_POST_KEY, /* The key has been handled */
81 DLG_IDLE, /* The idle state is active */
82 DLG_UNHANDLED_KEY, /* Key that no widget handled */
83 DLG_HOTKEY_HANDLED, /* A widget has got the hotkey */
84 DLG_VALIDATE /* Dialog is to be closed */
85 } dlg_msg_t;
88 /* Dialog callback */
89 struct Dlg_head;
90 typedef cb_ret_t (*dlg_cb_fn)(struct Dlg_head *h, dlg_msg_t msg, int parm);
92 typedef struct Dlg_head {
94 /* Set by the user */
95 int flags; /* User flags */
96 const char *help_ctx; /* Name of the help entry */
97 const int *color; /* Color set */
98 /*notconst*/ char *title; /* Title of the dialog */
100 /* Set and received by the user */
101 int ret_value; /* Result of run_dlg() */
103 /* Geometry */
104 int x, y; /* Position relative to screen origin */
105 int cols, lines; /* Width and height of the window */
107 /* Internal flags */
108 unsigned int running:1; /* The dialog is currently active */
109 unsigned int fullscreen:1; /* Parents dialogs don't need refresh */
110 int mouse_status; /* For the autorepeat status of the mouse */
112 /* Internal variables */
113 int count; /* Number of widgets */
114 struct Widget *current; /* Curently active widget */
115 dlg_cb_fn callback;
116 struct Dlg_head *parent; /* Parent dialog */
118 } Dlg_head;
121 typedef struct Widget Widget;
123 /* Widget callback */
124 typedef cb_ret_t (*callback_fn) (Widget *widget, widget_msg_t msg, int parm);
126 /* Every Widget must have this as its first element */
127 struct Widget {
128 int x, y;
129 int cols, lines;
131 #define W_WANT_HOTKEY (1 << 1)
132 #define W_WANT_CURSOR (1 << 2)
133 #define W_WANT_IDLE (1 << 3)
134 #define W_IS_INPUT (1 << 4)
135 int options;
136 int dlg_id; /* Number of the widget, starting with 0 */
137 struct Widget *next;
138 struct Widget *prev;
139 callback_fn callback;
140 mouse_h mouse;
141 struct Dlg_head *parent;
144 /* draw box in window */
145 void draw_box (Dlg_head *h, int y, int x, int ys, int xs);
147 /* doubled line if possible */
148 void draw_double_box (Dlg_head *h, int y, int x, int ys, int xs);
150 /* Flags for create_dlg: */
151 #define DLG_REVERSE (1 << 5) /* Tab order is opposite to the add order */
152 #define DLG_WANT_TAB (1 << 4) /* Should the tab key be sent to the dialog? */
153 #define DLG_WANT_IDLE (1 << 3) /* Dialog wants idle events */
154 #define DLG_COMPACT (1 << 2) /* Suppress spaces around the frame */
155 #define DLG_TRYUP (1 << 1) /* Try to move two lines up the dialog */
156 #define DLG_CENTER (1 << 0) /* Center the dialog */
157 #define DLG_NONE (000000) /* No options */
159 /* Creates a dialog head */
160 Dlg_head *create_dlg (int y1, int x1, int lines, int cols,
161 const int *color_set, dlg_cb_fn callback,
162 const char *help_ctx, const char *title, int flags);
163 int add_widget (Dlg_head *dest, void *Widget);
165 /* Runs dialog d */
166 int run_dlg (Dlg_head *d);
168 void dlg_run_done (Dlg_head *h);
169 void dlg_process_event (Dlg_head *h, int key, Gpm_Event *event);
170 void init_dlg (Dlg_head *h);
172 /* To activate/deactivate the idle message generation */
173 void set_idle_proc (Dlg_head *d, int enable);
175 void dlg_redraw (Dlg_head *h);
176 void destroy_dlg (Dlg_head *h);
178 void widget_set_size (Widget *widget, int x1, int y1, int x2, int y2);
180 void dlg_broadcast_msg (Dlg_head *h, widget_msg_t message, int reverse);
182 void init_widget (Widget *w, int y, int x, int lines, int cols,
183 callback_fn callback, mouse_h mouse_handler);
185 /* Default callback for dialogs */
186 cb_ret_t default_dlg_callback (Dlg_head *h, dlg_msg_t msg, int parm);
188 /* Default callback for widgets */
189 cb_ret_t default_proc (widget_msg_t msg, int parm);
191 /* Default paint routine for dialogs */
192 void common_dialog_repaint (struct Dlg_head *h);
194 #define widget_move(w, _y, _x) move(((Widget *)(w))->y + _y, \
195 ((Widget *)(w))->x + _x)
196 #define dlg_move(h, _y, _x) move(((Dlg_head *)(h))->y + _y, \
197 ((Dlg_head *)(h))->x + _x)
199 extern Dlg_head *current_dlg;
201 /* A hook list for idle events */
202 extern Hook *idle_hook;
204 static inline cb_ret_t
205 send_message (Widget *w, widget_msg_t msg, int parm)
207 return (*(w->callback)) (w, msg, parm);
210 /* Return 1 if the widget is active, 0 otherwise */
211 static inline int
212 dlg_widget_active (void *w)
214 Widget *w1 = (Widget *) w;
215 return (w1->parent->current == w1);
218 void dlg_replace_widget (Widget *old, Widget *new);
219 int dlg_overlap (Widget *a, Widget *b);
220 void widget_erase (Widget *);
221 void dlg_erase (Dlg_head *h);
222 void dlg_stop (Dlg_head *h);
224 /* Widget selection */
225 void dlg_select_widget (void *widget);
226 void dlg_one_up (Dlg_head *h);
227 void dlg_one_down (Dlg_head *h);
228 int dlg_focus (Dlg_head *h);
229 Widget *find_widget_type (Dlg_head *h, callback_fn callback);
230 void dlg_select_by_id (Dlg_head *h, int id);
232 /* Redraw all dialogs */
233 void do_refresh (void);
235 /* Sets/clear the specified flag in the options field */
236 #define widget_option(w,f,i) \
237 w.options = ((i) ? (w.options | (f)) : (w.options & (~(f))))
239 #define widget_want_cursor(w,i) widget_option(w, W_WANT_CURSOR, i)
240 #define widget_want_hotkey(w,i) widget_option(w, W_WANT_HOTKEY, i)
242 /* Used in load_prompt() */
243 void update_cursor (Dlg_head *h);
245 #endif /* MC_DIALOG_H */