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.
24 * \brief Header: dialog box features module
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 */
43 typedef struct Widget Widget
;
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 */
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.
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 */
94 /* Flags for create_dlg */
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 */
109 DLG_ACTIVE
= 0, /* Dialog is visible and active */
110 DLG_SUSPENDED
= 1, /* Dialog is suspended */
111 DLG_CLOSED
= 2 /* Dialog is closed */
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 */
133 DLG_COLOR_HOT_NORMAL
,
139 typedef int dlg_colors_t
[DLG_COLOR_COUNT
];
143 /* Set by the user */
144 gboolean modal
; /* type of dialog: modal or not */
145 dlg_flags_t flags
; /* User flags */
146 const char *help_ctx
; /* Name of the help entry */
147 dlg_colors_t color
; /* Color set. Unused in viewer and editor */
148 char *title
; /* Title of the dialog */
150 /* Set and received by the user */
151 int ret_value
; /* Result of run_dlg() */
154 int x
, y
; /* Position relative to screen origin */
155 int cols
, lines
; /* Width and height of the window */
159 gboolean fullscreen
; /* Parents dialogs don't need refresh */
160 gboolean winch_pending
; /* SIGWINCH signal has been got. Resize dialog after rise */
161 int mouse_status
; /* For the autorepeat status of the mouse */
163 /* Internal variables */
164 GList
*widgets
; /* widgets list */
165 GList
*current
; /* Curently active widget */
166 void *data
; /* Data can be passed to dialog */
169 dlg_shortcut_str get_shortcut
; /* Shortcut string */
170 dlg_title_str get_title
; /* useless for modal dialogs */
172 struct Dlg_head
*parent
; /* Parent dialog */
175 /* Color styles for normal and error dialogs */
176 extern dlg_colors_t dialog_colors
;
177 extern dlg_colors_t alarm_colors
;
179 /* Widget callback */
180 typedef cb_ret_t (*callback_fn
) (Widget
* widget
, widget_msg_t msg
, int parm
);
185 W_WANT_HOTKEY
= (1 << 1),
186 W_WANT_CURSOR
= (1 << 2),
187 W_WANT_IDLE
= (1 << 3),
188 W_IS_INPUT
= (1 << 4),
189 W_DISABLED
= (1 << 5) /* Widget cannot be selected */
192 /* Flags for widget repositioning on dialog resize */
195 WPOS_KEEP_LEFT
= (1 << 0), /* keep widget distance to left border of dialog */
196 WPOS_KEEP_RIGHT
= (1 << 1), /* keep widget distance to right border of dialog */
197 WPOS_KEEP_TOP
= (1 << 2), /* keep widget distance to top border of dialog */
198 WPOS_KEEP_BOTTOM
= (1 << 3), /* keep widget distance to bottom border of dialog */
199 WPOS_KEEP_HORZ
= WPOS_KEEP_LEFT
| WPOS_KEEP_RIGHT
,
200 WPOS_KEEP_VERT
= WPOS_KEEP_TOP
| WPOS_KEEP_BOTTOM
,
201 WPOS_KEEP_ALL
= WPOS_KEEP_HORZ
| WPOS_KEEP_VERT
202 } widget_pos_flags_t
;
204 /* Every Widget must have this as its first element */
209 widget_options_t options
;
210 widget_pos_flags_t pos_flags
; /* repositioning flags */
211 unsigned int id
; /* Number of the widget, starting with 0 */
212 callback_fn callback
;
214 struct Dlg_head
*owner
;
217 /* draw box in window */
218 void draw_box (Dlg_head
* h
, int y
, int x
, int ys
, int xs
, gboolean single
);
220 /* Creates a dialog head */
221 Dlg_head
*create_dlg (gboolean modal
, int y1
, int x1
, int lines
, int cols
,
222 const int *colors
, dlg_cb_fn callback
,
223 const char *help_ctx
, const char *title
, dlg_flags_t flags
);
225 void dlg_set_default_colors (void);
227 int add_widget_autopos (Dlg_head
* dest
, void *w
, widget_pos_flags_t pos_flags
);
228 int add_widget (Dlg_head
* dest
, void *w
);
230 /* sets size of dialog, leaving positioning to automatic mehtods
231 according to dialog flags */
232 void dlg_set_size (Dlg_head
* h
, int lines
, int cols
);
233 /* this function allows to set dialog position */
234 void dlg_set_position (Dlg_head
* h
, int y1
, int x1
, int y2
, int x2
);
236 void init_dlg (Dlg_head
* h
);
237 int run_dlg (Dlg_head
* d
);
238 void destroy_dlg (Dlg_head
* h
);
240 void dlg_run_done (Dlg_head
* h
);
241 void dlg_process_event (Dlg_head
* h
, int key
, Gpm_Event
* event
);
243 char *dlg_get_title (const Dlg_head
*h
, size_t len
);
245 /* To activate/deactivate the idle message generation */
246 void set_idle_proc (Dlg_head
* d
, int enable
);
248 void dlg_redraw (Dlg_head
* h
);
250 void widget_set_size (Widget
* widget
, int y
, int x
, int lines
, int cols
);
252 void dlg_broadcast_msg (Dlg_head
* h
, widget_msg_t message
, gboolean reverse
);
254 void init_widget (Widget
* w
, int y
, int x
, int lines
, int cols
,
255 callback_fn callback
, mouse_h mouse_handler
);
257 /* Default callback for dialogs */
258 cb_ret_t
default_dlg_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
);
260 /* Default paint routine for dialogs */
261 void common_dialog_repaint (Dlg_head
*h
);
263 #define widget_move(w, _y, _x) tty_gotoyx (((Widget *)(w))->y + _y, ((Widget *)(w))->x + _x)
264 #define dlg_move(h, _y, _x) tty_gotoyx (((Dlg_head *)(h))->y + _y, ((Dlg_head *)(h))->x + _x)
266 extern GList
*top_dlg
;
268 /* A hook list for idle events */
269 extern Hook
*idle_hook
;
271 static inline cb_ret_t
272 send_message (Widget
* w
, widget_msg_t msg
, int parm
)
274 return w
->callback (w
, msg
, parm
);
277 /* Return 1 if the widget is active, 0 otherwise */
279 dlg_widget_active (void *w
)
281 Widget
*w1
= (Widget
*) w
;
282 return ((Widget
*) w1
->owner
->current
->data
== w1
);
285 static inline unsigned int
286 dlg_get_current_widget_id (const Dlg_head
* h
)
288 return ((Widget
*) h
->current
->data
)->id
;
291 void dlg_replace_widget (Widget
* old
, Widget
* new);
292 int dlg_overlap (Widget
* a
, Widget
* b
);
293 void widget_erase (Widget
*);
294 void dlg_erase (Dlg_head
* h
);
295 void dlg_stop (Dlg_head
* h
);
297 /* Widget selection */
298 void dlg_select_widget (void *widget
);
299 void dlg_one_up (Dlg_head
* h
);
300 void dlg_one_down (Dlg_head
* h
);
301 int dlg_focus (Dlg_head
* h
);
302 Widget
*find_widget_type (const Dlg_head
* h
, callback_fn callback
);
303 Widget
*dlg_find_by_id (const Dlg_head
* h
, unsigned int id
);
304 void dlg_select_by_id (const Dlg_head
* h
, unsigned int id
);
306 /* Redraw all dialogs */
307 void do_refresh (void);
309 /* Sets/clear the specified flag in the options field */
310 #define widget_option(w,f,i) \
311 w.options = ((i) ? ((w).options | (f)) : ((w).options & (~(f))))
313 #define widget_want_cursor(w,i) widget_option((w), W_WANT_CURSOR, (i))
314 #define widget_want_hotkey(w,i) widget_option((w), W_WANT_HOTKEY, (i))
315 #define widget_disable(w,i) widget_option((w), W_DISABLED, (i))
317 /* Used in load_prompt() */
318 void update_cursor (Dlg_head
* h
);
320 #endif /* MC_DIALOG_H */