Merge branch '2123_crash_while_copy'
[midnight-commander.git] / src / dialog.h
blobcefa5e48ca670757ee040fb115d33efb4a23866c
1 /* Dialog box features module for the Midnight Commander
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2009, 2010 Free Software Foundation
5 Authors: 1994, 1995 Radek Doulik, Miguel de Icaza
6 2009, 2010 Andrew Borodin
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 /** \file dialog.h
24 * \brief Header: dialog box features module
27 #ifndef MC_DIALOG_H
28 #define MC_DIALOG_H
30 #include <sys/types.h> /* size_t */
32 #include "lib/global.h"
33 #include "lib/tty/mouse.h"
34 #include "lib/util.h" /* Hook */
36 /* Common return values */
37 #define B_EXIT 0
38 #define B_CANCEL 1
39 #define B_ENTER 2
40 #define B_HELP 3
41 #define B_USER 100
43 typedef struct Widget Widget;
45 /* Widget messages */
46 typedef enum
48 WIDGET_INIT, /* Initialize widget */
49 WIDGET_FOCUS, /* Draw widget in focused state */
50 WIDGET_UNFOCUS, /* Draw widget in unfocused state */
51 WIDGET_DRAW, /* Sent to widget to draw themselves */
52 WIDGET_KEY, /* Sent to widgets on key press */
53 WIDGET_HOTKEY, /* Sent to widget to catch preprocess key */
54 WIDGET_COMMAND, /* Send to widget to handle command */
55 WIDGET_DESTROY, /* Sent to widget at destruction time */
56 WIDGET_CURSOR, /* Sent to widget to position the cursor */
57 WIDGET_IDLE, /* Sent to widgets with options & W_WANT_IDLE */
58 WIDGET_RESIZED /* Sent after a widget has been resized */
59 } widget_msg_t;
61 typedef enum
63 MSG_NOT_HANDLED = 0,
64 MSG_HANDLED = 1
65 } cb_ret_t;
67 /* Widgets are expected to answer to the following messages:
69 WIDGET_FOCUS: 1 if the accept the focus, 0 if they do not.
70 WIDGET_UNFOCUS: 1 if they accept to release the focus, 0 if they don't.
71 WIDGET_KEY: 1 if they actually used the key, 0 if not.
72 WIDGET_HOTKEY: 1 if they actually used the key, 0 if not.
75 /* Dialog messages */
76 typedef enum
78 DLG_INIT = 0, /* Initialize dialog */
79 DLG_IDLE = 1, /* The idle state is active */
80 DLG_DRAW = 2, /* Draw dialog on screen */
81 DLG_FOCUS = 3, /* A widget has got focus */
82 DLG_UNFOCUS = 4, /* A widget has been unfocused */
83 DLG_RESIZE = 5, /* Window size has changed */
84 DLG_KEY = 6, /* Key before sending to widget */
85 DLG_HOTKEY_HANDLED = 7, /* A widget has got the hotkey */
86 DLG_POST_KEY = 8, /* The key has been handled */
87 DLG_UNHANDLED_KEY = 9, /* Key that no widget handled */
88 DLG_ACTION = 10, /* State of check- and radioboxes has changed
89 * and listbox current entry has changed */
90 DLG_VALIDATE = 11, /* Dialog is to be closed */
91 DLG_END = 12 /* Shut down dialog */
92 } dlg_msg_t;
94 /* Flags for create_dlg */
95 typedef enum
97 DLG_REVERSE = (1 << 5), /* Tab order is opposite to the add order */
98 DLG_WANT_TAB = (1 << 4), /* Should the tab key be sent to the dialog? */
99 DLG_WANT_IDLE = (1 << 3), /* Dialog wants idle events */
100 DLG_COMPACT = (1 << 2), /* Suppress spaces around the frame */
101 DLG_TRYUP = (1 << 1), /* Try to move two lines up the dialog */
102 DLG_CENTER = (1 << 0), /* Center the dialog */
103 DLG_NONE = 0 /* No options */
104 } dlg_flags_t;
106 /* Dialog state */
107 typedef enum
109 DLG_ACTIVE = 0, /* Dialog is visible and active */
110 DLG_SUSPENDED = 1, /* Dialog is suspended */
111 DLG_CLOSED = 2 /* Dialog is closed */
112 } dlg_state_t;
114 /* Dialog callback */
115 typedef struct Dlg_head Dlg_head;
116 typedef cb_ret_t (*dlg_cb_fn) (Dlg_head * h, Widget * sender,
117 dlg_msg_t msg, int parm, void *data);
119 /* menu command execution */
120 typedef cb_ret_t (*menu_exec_fn) (int command);
122 /* get string representation of shortcut assigned with command */
123 /* as menu is a widget of dialog, ask dialog about shortcut string */
124 typedef char *(*dlg_shortcut_str) (unsigned long command);
126 /* get dialog name to show in dialog list */
127 typedef char *(*dlg_title_str) (const Dlg_head * h, size_t len);
129 /* Dialog color constants */
130 #define DLG_COLOR_NUM 4
131 #define DLG_NORMALC(h) ((h)->color[0])
132 #define DLG_FOCUSC(h) ((h)->color[1])
133 #define DLG_HOT_NORMALC(h) ((h)->color[2])
134 #define DLG_HOT_FOCUSC(h) ((h)->color[3])
136 struct Dlg_head
138 /* Set by the user */
139 gboolean modal; /* type of dialog: modal or not */
140 dlg_flags_t flags; /* User flags */
141 const char *help_ctx; /* Name of the help entry */
142 int *color; /* Color set. Unused in viewer and editor */
143 char *title; /* Title of the dialog */
145 /* Set and received by the user */
146 int ret_value; /* Result of run_dlg() */
148 /* Geometry */
149 int x, y; /* Position relative to screen origin */
150 int cols, lines; /* Width and height of the window */
152 /* Internal flags */
153 dlg_state_t state;
154 gboolean fullscreen; /* Parents dialogs don't need refresh */
155 gboolean winch_pending; /* SIGWINCH signal has been got. Resize dialog after rise */
156 int mouse_status; /* For the autorepeat status of the mouse */
158 /* Internal variables */
159 GList *widgets; /* widgets list */
160 GList *current; /* Curently active widget */
161 void *data; /* Data can be passed to dialog */
163 dlg_cb_fn callback;
164 dlg_shortcut_str get_shortcut; /* Shortcut string */
165 dlg_title_str get_title; /* useless for modal dialogs */
167 struct Dlg_head *parent; /* Parent dialog */
170 /* Color styles for normal and error dialogs */
171 extern int dialog_colors[4];
172 extern int alarm_colors[4];
174 /* Widget callback */
175 typedef cb_ret_t (*callback_fn) (Widget * widget, widget_msg_t msg, int parm);
177 /* widget options */
178 typedef enum
180 W_WANT_HOTKEY = (1 << 1),
181 W_WANT_CURSOR = (1 << 2),
182 W_WANT_IDLE = (1 << 3),
183 W_IS_INPUT = (1 << 4)
184 } widget_options_t;
186 /* Flags for widget repositioning on dialog resize */
187 typedef enum
189 WPOS_KEEP_LEFT = (1 << 0), /* keep widget distance to left border of dialog */
190 WPOS_KEEP_RIGHT = (1 << 1), /* keep widget distance to right border of dialog */
191 WPOS_KEEP_TOP = (1 << 2), /* keep widget distance to top border of dialog */
192 WPOS_KEEP_BOTTOM = (1 << 3), /* keep widget distance to bottom border of dialog */
193 WPOS_KEEP_HORZ = WPOS_KEEP_LEFT | WPOS_KEEP_RIGHT,
194 WPOS_KEEP_VERT = WPOS_KEEP_TOP | WPOS_KEEP_BOTTOM,
195 WPOS_KEEP_ALL = WPOS_KEEP_HORZ | WPOS_KEEP_VERT
196 } widget_pos_flags_t;
198 /* Every Widget must have this as its first element */
199 struct Widget
201 int x, y;
202 int cols, lines;
203 widget_options_t options;
204 widget_pos_flags_t pos_flags; /* repositioning flags */
205 unsigned int id; /* Number of the widget, starting with 0 */
206 callback_fn callback;
207 mouse_h mouse;
208 struct Dlg_head *owner;
211 /* draw box in window */
212 void draw_box (Dlg_head * h, int y, int x, int ys, int xs, gboolean single);
214 /* Creates a dialog head */
215 Dlg_head *create_dlg (gboolean modal, int y1, int x1, int lines, int cols,
216 const int *colors, dlg_cb_fn callback,
217 const char *help_ctx, const char *title, dlg_flags_t flags);
219 void dlg_set_default_colors (void);
221 int add_widget_autopos (Dlg_head * dest, void *w, widget_pos_flags_t pos_flags);
222 int add_widget (Dlg_head * dest, void *w);
224 /* sets size of dialog, leaving positioning to automatic mehtods
225 according to dialog flags */
226 void dlg_set_size (Dlg_head * h, int lines, int cols);
227 /* this function allows to set dialog position */
228 void dlg_set_position (Dlg_head * h, int y1, int x1, int y2, int x2);
230 void init_dlg (Dlg_head * h);
231 int run_dlg (Dlg_head * d);
232 void destroy_dlg (Dlg_head * h);
234 void dlg_run_done (Dlg_head * h);
235 void dlg_process_event (Dlg_head * h, int key, Gpm_Event * event);
237 char *dlg_get_title (const Dlg_head *h, size_t len);
239 /* To activate/deactivate the idle message generation */
240 void set_idle_proc (Dlg_head * d, int enable);
242 void dlg_redraw (Dlg_head * h);
244 void widget_set_size (Widget * widget, int y, int x, int lines, int cols);
246 void dlg_broadcast_msg (Dlg_head * h, widget_msg_t message, gboolean reverse);
248 void init_widget (Widget * w, int y, int x, int lines, int cols,
249 callback_fn callback, mouse_h mouse_handler);
251 /* Default callback for dialogs */
252 cb_ret_t default_dlg_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data);
254 /* Default paint routine for dialogs */
255 void common_dialog_repaint (struct Dlg_head *h);
257 #define widget_move(w, _y, _x) tty_gotoyx (((Widget *)(w))->y + _y, ((Widget *)(w))->x + _x)
258 #define dlg_move(h, _y, _x) tty_gotoyx (((Dlg_head *)(h))->y + _y, ((Dlg_head *)(h))->x + _x)
260 extern GList *top_dlg;
262 /* A hook list for idle events */
263 extern Hook *idle_hook;
265 static inline cb_ret_t
266 send_message (Widget * w, widget_msg_t msg, int parm)
268 return w->callback (w, msg, parm);
271 /* Return 1 if the widget is active, 0 otherwise */
272 static inline int
273 dlg_widget_active (void *w)
275 Widget *w1 = (Widget *) w;
276 return ((Widget *) w1->owner->current->data == w1);
279 static inline unsigned int
280 dlg_get_current_widget_id (const Dlg_head * h)
282 return ((Widget *) h->current->data)->id;
285 void dlg_replace_widget (Widget * old, Widget * new);
286 int dlg_overlap (Widget * a, Widget * b);
287 void widget_erase (Widget *);
288 void dlg_erase (Dlg_head * h);
289 void dlg_stop (Dlg_head * h);
291 /* Widget selection */
292 void dlg_select_widget (void *widget);
293 void dlg_one_up (Dlg_head * h);
294 void dlg_one_down (Dlg_head * h);
295 int dlg_focus (Dlg_head * h);
296 Widget *find_widget_type (const Dlg_head * h, callback_fn callback);
297 void dlg_select_by_id (const Dlg_head * h, unsigned int id);
299 /* Redraw all dialogs */
300 void do_refresh (void);
302 /* Sets/clear the specified flag in the options field */
303 #define widget_option(w,f,i) \
304 w.options = ((i) ? (w.options | (f)) : (w.options & (~(f))))
306 #define widget_want_cursor(w,i) widget_option(w, W_WANT_CURSOR, i)
307 #define widget_want_hotkey(w,i) widget_option(w, W_WANT_HOTKEY, i)
309 /* Used in load_prompt() */
310 void update_cursor (Dlg_head * h);
312 #endif /* MC_DIALOG_H */