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.
20 * \brief Header: dialog box features module
26 #include "lib/tty/mouse.h"
28 /* Common return values */
35 typedef struct Widget Widget
;
40 WIDGET_INIT
, /* Initialize widget */
41 WIDGET_FOCUS
, /* Draw widget in focused state */
42 WIDGET_UNFOCUS
, /* Draw widget in unfocused state */
43 WIDGET_DRAW
, /* Sent to widget to draw themselves */
44 WIDGET_KEY
, /* Sent to widgets on key press */
45 WIDGET_HOTKEY
, /* Sent to widget to catch preprocess key */
46 WIDGET_COMMAND
, /* Send to widget to handle command */
47 WIDGET_DESTROY
, /* Sent to widget at destruction time */
48 WIDGET_CURSOR
, /* Sent to widget to position the cursor */
49 WIDGET_IDLE
, /* Sent to widgets with options & W_WANT_IDLE */
50 WIDGET_RESIZED
/* Sent after a widget has been resized */
59 /* Widgets are expected to answer to the following messages:
61 WIDGET_FOCUS: 1 if the accept the focus, 0 if they do not.
62 WIDGET_UNFOCUS: 1 if they accept to release the focus, 0 if they don't.
63 WIDGET_KEY: 1 if they actually used the key, 0 if not.
64 WIDGET_HOTKEY: 1 if they actually used the key, 0 if not.
70 DLG_INIT
= 0, /* Initialize dialog */
71 DLG_IDLE
= 1, /* The idle state is active */
72 DLG_DRAW
= 2, /* Draw dialog on screen */
73 DLG_FOCUS
= 3, /* A widget has got focus */
74 DLG_UNFOCUS
= 4, /* A widget has been unfocused */
75 DLG_RESIZE
= 5, /* Window size has changed */
76 DLG_KEY
= 6, /* Key before sending to widget */
77 DLG_HOTKEY_HANDLED
= 7, /* A widget has got the hotkey */
78 DLG_POST_KEY
= 8, /* The key has been handled */
79 DLG_UNHANDLED_KEY
= 9, /* Key that no widget handled */
80 DLG_ACTION
= 10, /* State of check- and radioboxes has changed
81 * and listbox current entry has changed */
82 DLG_VALIDATE
= 11, /* Dialog is to be closed */
83 DLG_END
= 12 /* Shut down dialog */
86 /* Flags for create_dlg */
89 DLG_REVERSE
= (1 << 5), /* Tab order is opposite to the add order */
90 DLG_WANT_TAB
= (1 << 4), /* Should the tab key be sent to the dialog? */
91 DLG_WANT_IDLE
= (1 << 3), /* Dialog wants idle events */
92 DLG_COMPACT
= (1 << 2), /* Suppress spaces around the frame */
93 DLG_TRYUP
= (1 << 1), /* Try to move two lines up the dialog */
94 DLG_CENTER
= (1 << 0), /* Center the dialog */
95 DLG_NONE
= 0 /* No options */
99 typedef struct Dlg_head Dlg_head
;
100 typedef cb_ret_t (*dlg_cb_fn
) (struct Dlg_head
* h
, Widget
* sender
,
101 dlg_msg_t msg
, int parm
, void *data
);
103 /* menu command execution */
104 typedef cb_ret_t (*menu_exec_fn
) (int command
);
106 /* get string representation of shortcut assigned with command */
107 /* as menu is a widget of dialog, ask dialog about shortcut string */
108 typedef char *(*dlg_shortcut_str
) (unsigned long command
);
110 /* Dialog color constants */
111 #define DLG_COLOR_NUM 4
112 #define DLG_NORMALC(h) ((h)->color[0])
113 #define DLG_FOCUSC(h) ((h)->color[1])
114 #define DLG_HOT_NORMALC(h) ((h)->color[2])
115 #define DLG_HOT_FOCUSC(h) ((h)->color[3])
119 /* Set by the user */
120 dlg_flags_t flags
; /* User flags */
121 const char *help_ctx
; /* Name of the help entry */
122 int *color
; /* Color set. Unused in viewer and editor */
123 char *title
; /* Title of the dialog */
125 /* Set and received by the user */
126 int ret_value
; /* Result of run_dlg() */
129 int x
, y
; /* Position relative to screen origin */
130 int cols
, lines
; /* Width and height of the window */
133 unsigned int running
:1; /* The dialog is currently active */
134 unsigned int fullscreen
:1; /* Parents dialogs don't need refresh */
135 int mouse_status
; /* For the autorepeat status of the mouse */
137 /* Internal variables */
138 int count
; /* Number of widgets */
139 struct Widget
*current
; /* Curently active widget */
140 void *data
; /* Data can be passed to dialog */
142 dlg_shortcut_str get_shortcut
; /* Shortcut string */
143 struct Dlg_head
*parent
; /* Parent dialog */
146 /* Color styles for normal and error dialogs */
147 extern int dialog_colors
[4];
148 extern int alarm_colors
[4];
151 /* Widget callback */
152 typedef cb_ret_t (*callback_fn
) (Widget
* widget
, widget_msg_t msg
, int parm
);
157 W_WANT_HOTKEY
= (1 << 1),
158 W_WANT_CURSOR
= (1 << 2),
159 W_WANT_IDLE
= (1 << 3),
160 W_IS_INPUT
= (1 << 4)
163 /* Flags for widget repositioning on dialog resize */
166 WPOS_KEEP_LEFT
= (1 << 0), /* keep widget distance to left border of dialog */
167 WPOS_KEEP_RIGHT
= (1 << 1), /* keep widget distance to right border of dialog */
168 WPOS_KEEP_TOP
= (1 << 2), /* keep widget distance to top border of dialog */
169 WPOS_KEEP_BOTTOM
= (1 << 3), /* keep widget distance to bottom border of dialog */
170 WPOS_KEEP_HORZ
= WPOS_KEEP_LEFT
| WPOS_KEEP_RIGHT
,
171 WPOS_KEEP_VERT
= WPOS_KEEP_TOP
| WPOS_KEEP_BOTTOM
,
172 WPOS_KEEP_ALL
= WPOS_KEEP_HORZ
| WPOS_KEEP_VERT
173 } widget_pos_flags_t
;
175 /* Every Widget must have this as its first element */
180 widget_options_t options
;
181 widget_pos_flags_t pos_flags
; /* repositioning flags */
182 int dlg_id
; /* Number of the widget, starting with 0 */
185 callback_fn callback
;
187 struct Dlg_head
*parent
;
190 /* draw box in window */
191 void draw_box (Dlg_head
* h
, int y
, int x
, int ys
, int xs
, gboolean single
);
193 /* Creates a dialog head */
194 Dlg_head
*create_dlg (int y1
, int x1
, int lines
, int cols
,
195 const int *colors
, dlg_cb_fn callback
,
196 const char *help_ctx
, const char *title
, dlg_flags_t flags
);
198 void dlg_set_default_colors (void);
200 int add_widget_autopos (Dlg_head
* dest
, void *w
, widget_pos_flags_t pos_flags
);
201 int add_widget (Dlg_head
* dest
, void *w
);
203 /* sets size of dialog, leaving positioning to automatic mehtods
204 according to dialog flags */
205 void dlg_set_size (Dlg_head
* h
, int lines
, int cols
);
206 /* this function allows to set dialog position */
207 void dlg_set_position (Dlg_head
* h
, int y1
, int x1
, int y2
, int x2
);
210 int run_dlg (Dlg_head
* d
);
212 void dlg_run_done (Dlg_head
* h
);
213 void dlg_process_event (Dlg_head
* h
, int key
, Gpm_Event
* event
);
214 void init_dlg (Dlg_head
* h
);
216 /* To activate/deactivate the idle message generation */
217 void set_idle_proc (Dlg_head
* d
, int enable
);
219 void dlg_redraw (Dlg_head
* h
);
220 void destroy_dlg (Dlg_head
* h
);
222 void widget_set_size (Widget
* widget
, int y
, int x
, int lines
, int cols
);
224 void dlg_broadcast_msg (Dlg_head
* h
, widget_msg_t message
, int reverse
);
226 void init_widget (Widget
* w
, int y
, int x
, int lines
, int cols
,
227 callback_fn callback
, mouse_h mouse_handler
);
229 /* Default callback for dialogs */
230 cb_ret_t
default_dlg_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
);
232 /* Default paint routine for dialogs */
233 void common_dialog_repaint (struct Dlg_head
*h
);
235 #define widget_move(w, _y, _x) tty_gotoyx (((Widget *)(w))->y + _y, ((Widget *)(w))->x + _x)
236 #define dlg_move(h, _y, _x) tty_gotoyx (((Dlg_head *)(h))->y + _y, ((Dlg_head *)(h))->x + _x)
238 extern Dlg_head
*current_dlg
;
240 /* A hook list for idle events */
241 extern Hook
*idle_hook
;
243 static inline cb_ret_t
244 send_message (Widget
* w
, widget_msg_t msg
, int parm
)
246 return (*(w
->callback
)) (w
, msg
, parm
);
249 /* Return 1 if the widget is active, 0 otherwise */
251 dlg_widget_active (void *w
)
253 Widget
*w1
= (Widget
*) w
;
254 return (w1
->parent
->current
== w1
);
257 void dlg_replace_widget (Widget
* old
, Widget
* new);
258 int dlg_overlap (Widget
* a
, Widget
* b
);
259 void widget_erase (Widget
*);
260 void dlg_erase (Dlg_head
* h
);
261 void dlg_stop (Dlg_head
* h
);
263 /* Widget selection */
264 void dlg_select_widget (void *widget
);
265 void dlg_one_up (Dlg_head
* h
);
266 void dlg_one_down (Dlg_head
* h
);
267 int dlg_focus (Dlg_head
* h
);
268 Widget
*find_widget_type (const Dlg_head
* h
, callback_fn callback
);
269 void dlg_select_by_id (const Dlg_head
* h
, int id
);
271 /* Redraw all dialogs */
272 void do_refresh (void);
274 /* Sets/clear the specified flag in the options field */
275 #define widget_option(w,f,i) \
276 w.options = ((i) ? (w.options | (f)) : (w.options & (~(f))))
278 #define widget_want_cursor(w,i) widget_option(w, W_WANT_CURSOR, i)
279 #define widget_want_hotkey(w,i) widget_option(w, W_WANT_HOTKEY, i)
281 /* Used in load_prompt() */
282 void update_cursor (Dlg_head
* h
);
284 #endif /* MC_DIALOG_H */