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 "../src/tty/mouse.h"
27 #include "util.h" /* Hook */
29 /* Common return values */
38 WIDGET_INIT
, /* Initialize widget */
39 WIDGET_FOCUS
, /* Draw widget in focused state */
40 WIDGET_UNFOCUS
, /* Draw widget in unfocused state */
41 WIDGET_DRAW
, /* Sent to widget to draw themselves */
42 WIDGET_KEY
, /* Sent to widgets on key press */
43 WIDGET_HOTKEY
, /* Sent to widget to catch preprocess key */
44 WIDGET_DESTROY
, /* Sent to widget at destruction time */
45 WIDGET_CURSOR
, /* Sent to widget to position the cursor */
46 WIDGET_IDLE
, /* Sent to widgets with options & W_WANT_IDLE*/
47 WIDGET_RESIZED
/* Sent after a widget has been resized */
55 /* Widgets are expected to answer to the following messages:
57 WIDGET_FOCUS: 1 if the accept the focus, 0 if they do not.
58 WIDGET_UNFOCUS: 1 if they accept to release the focus, 0 if they don't.
59 WIDGET_KEY: 1 if they actually used the key, 0 if not.
60 WIDGET_HOTKEY: 1 if they actually used the key, 0 if not.
65 DLG_KEY
, /* Key before sending to widget */
66 DLG_INIT
, /* Initialize dialog */
67 DLG_END
, /* Shut down dialog */
68 DLG_ACTION
, /* State of check- and radioboxes has changed
69 * and listbox current entry has changed */
70 DLG_DRAW
, /* Draw dialog on screen */
71 DLG_FOCUS
, /* A widget has got focus */
72 DLG_UNFOCUS
, /* A widget has been unfocused */
73 DLG_RESIZE
, /* Window size has changed */
74 DLG_POST_KEY
, /* The key has been handled */
75 DLG_IDLE
, /* The idle state is active */
76 DLG_UNHANDLED_KEY
, /* Key that no widget handled */
77 DLG_HOTKEY_HANDLED
, /* A widget has got the hotkey */
78 DLG_VALIDATE
/* Dialog is to be closed */
84 typedef cb_ret_t (*dlg_cb_fn
)(struct Dlg_head
*h
, dlg_msg_t msg
, int parm
);
86 /* Dialog color constants */
87 #define DLG_COLOR_NUM 4
88 #define DLG_NORMALC(h) ((h)->color[0])
89 #define DLG_FOCUSC(h) ((h)->color[1])
90 #define DLG_HOT_NORMALC(h) ((h)->color[2])
91 #define DLG_HOT_FOCUSC(h) ((h)->color[3])
93 typedef struct Dlg_head
{
95 int flags
; /* User flags */
96 const char *help_ctx
; /* Name of the help entry */
97 int *color
; /* Color set. Unused in viewer and editor */
98 /*notconst*/ char *title
; /* Title of the dialog */
100 /* Set and received by the user */
101 int ret_value
; /* Result of run_dlg() */
104 int x
, y
; /* Position relative to screen origin */
105 int cols
, lines
; /* Width and height of the window */
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 void *data
; /* data can be passed to dialog */
117 struct Dlg_head
*parent
; /* Parent dialog */
120 /* Color styles for normal and error dialogs */
121 extern int dialog_colors
[4];
122 extern int alarm_colors
[4];
124 typedef struct Widget Widget
;
126 /* Widget callback */
127 typedef cb_ret_t (*callback_fn
) (Widget
*widget
, widget_msg_t msg
, int parm
);
131 W_WANT_HOTKEY
= (1 << 1),
132 W_WANT_CURSOR
= (1 << 2),
133 W_WANT_IDLE
= (1 << 3),
134 W_IS_INPUT
= (1 << 4)
137 /* Flags for widget repositioning on dialog resize */
139 WPOS_KEEP_LEFT
= (1 << 0), /* keep widget distance to left border of dialog */
140 WPOS_KEEP_RIGHT
= (1 << 1), /* keep widget distance to right border of dialog */
141 WPOS_KEEP_TOP
= (1 << 2), /* keep widget distance to top border of dialog */
142 WPOS_KEEP_BOTTOM
= (1 << 3), /* keep widget distance to bottom border of dialog */
143 WPOS_KEEP_HORZ
= WPOS_KEEP_LEFT
| WPOS_KEEP_RIGHT
,
144 WPOS_KEEP_VERT
= WPOS_KEEP_TOP
| WPOS_KEEP_BOTTOM
,
145 WPOS_KEEP_ALL
= WPOS_KEEP_HORZ
| WPOS_KEEP_VERT
146 } widget_pos_flags_t
;
148 /* Every Widget must have this as its first element */
152 widget_options_t options
;
153 widget_pos_flags_t pos_flags
; /* repositioning flags */
154 int dlg_id
; /* Number of the widget, starting with 0 */
157 callback_fn callback
;
159 struct Dlg_head
*parent
;
162 /* draw box in window */
163 void draw_box (Dlg_head
*h
, int y
, int x
, int ys
, int xs
);
165 /* Flags for create_dlg: */
166 #define DLG_REVERSE (1 << 5) /* Tab order is opposite to the add order */
167 #define DLG_WANT_TAB (1 << 4) /* Should the tab key be sent to the dialog? */
168 #define DLG_WANT_IDLE (1 << 3) /* Dialog wants idle events */
169 #define DLG_COMPACT (1 << 2) /* Suppress spaces around the frame */
170 #define DLG_TRYUP (1 << 1) /* Try to move two lines up the dialog */
171 #define DLG_CENTER (1 << 0) /* Center the dialog */
172 #define DLG_NONE (000000) /* No options */
174 /* Creates a dialog head */
175 Dlg_head
*create_dlg (int y1
, int x1
, int lines
, int cols
,
176 const int *color_set
, dlg_cb_fn callback
,
177 const char *help_ctx
, const char *title
, int flags
);
179 void dlg_set_default_colors (void);
181 int add_widget_autopos (Dlg_head
*dest
, void *w
, widget_pos_flags_t pos_flags
);
182 int add_widget (Dlg_head
*dest
, void *w
);
184 /* sets size of dialog, leaving positioning to automatic mehtods
185 according to dialog flags */
186 void dlg_set_size (Dlg_head
*h
, int lines
, int cols
);
187 /* this function allows to set dialog position */
188 void dlg_set_position (Dlg_head
*h
, int y1
, int x1
, int y2
, int x2
);
191 int run_dlg (Dlg_head
*d
);
193 void dlg_run_done (Dlg_head
*h
);
194 void dlg_process_event (Dlg_head
*h
, int key
, Gpm_Event
*event
);
195 void init_dlg (Dlg_head
*h
);
197 /* To activate/deactivate the idle message generation */
198 void set_idle_proc (Dlg_head
*d
, int enable
);
200 void dlg_redraw (Dlg_head
*h
);
201 void destroy_dlg (Dlg_head
*h
);
203 void widget_set_size (Widget
*widget
, int y
, int x
, int lines
, int cols
);
205 void dlg_broadcast_msg (Dlg_head
*h
, widget_msg_t message
, int reverse
);
207 void init_widget (Widget
*w
, int y
, int x
, int lines
, int cols
,
208 callback_fn callback
, mouse_h mouse_handler
);
210 /* Default callback for dialogs */
211 cb_ret_t
default_dlg_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
);
213 /* Default paint routine for dialogs */
214 void common_dialog_repaint (struct Dlg_head
*h
);
216 #define widget_move(w, _y, _x) tty_gotoyx (((Widget *)(w))->y + _y, ((Widget *)(w))->x + _x)
217 #define dlg_move(h, _y, _x) tty_gotoyx (((Dlg_head *)(h))->y + _y, ((Dlg_head *)(h))->x + _x)
219 extern Dlg_head
*current_dlg
;
221 /* A hook list for idle events */
222 extern Hook
*idle_hook
;
224 static inline cb_ret_t
225 send_message (Widget
*w
, widget_msg_t msg
, int parm
)
227 return (*(w
->callback
)) (w
, msg
, parm
);
230 /* Return 1 if the widget is active, 0 otherwise */
232 dlg_widget_active (void *w
)
234 Widget
*w1
= (Widget
*) w
;
235 return (w1
->parent
->current
== w1
);
238 void dlg_replace_widget (Widget
*old
, Widget
*new);
239 int dlg_overlap (Widget
*a
, Widget
*b
);
240 void widget_erase (Widget
*);
241 void dlg_erase (Dlg_head
*h
);
242 void dlg_stop (Dlg_head
*h
);
244 /* Widget selection */
245 void dlg_select_widget (void *widget
);
246 void dlg_one_up (Dlg_head
*h
);
247 void dlg_one_down (Dlg_head
*h
);
248 int dlg_focus (Dlg_head
*h
);
249 Widget
*find_widget_type (Dlg_head
*h
, callback_fn callback
);
250 void dlg_select_by_id (Dlg_head
*h
, int id
);
252 /* Redraw all dialogs */
253 void do_refresh (void);
255 /* Sets/clear the specified flag in the options field */
256 #define widget_option(w,f,i) \
257 w.options = ((i) ? (w.options | (f)) : (w.options & (~(f))))
259 #define widget_want_cursor(w,i) widget_option(w, W_WANT_CURSOR, i)
260 #define widget_want_hotkey(w,i) widget_option(w, W_WANT_HOTKEY, i)
262 /* Used in load_prompt() */
263 void update_cursor (Dlg_head
*h
);
265 #endif /* MC_DIALOG_H */