3 /* {{{ Copyright Notice */
5 /* Widget based utility functions.
6 Copyright (C) 1994, 1995 the Free Software Foundation
8 Authors: 1994, 1995, 1996 Miguel de Icaza
9 1994, 1995 Radek Doulik
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
47 #include "key.h" /* For mi_getch() */
48 #include "dialog.h" /* For do_refresh() */
49 #include "complete.h" /* INPUT_COMPLETE_CD */
53 /* {{{ Common dialog callback */
56 dialog_repaint (struct Dlg_head
*h
, int back
, int title_fore
)
61 draw_box (h
, 1, 1, h
->lines
- 2, h
->cols
- 2);
64 dlg_move (h
, 1, (h
->cols
-strlen (h
->title
))/2);
71 common_dialog_repaint (struct Dlg_head
*h
)
73 dialog_repaint (h
, COLOR_NORMAL
, COLOR_HOT_NORMAL
);
77 common_dialog_callback (struct Dlg_head
*h
, int id
, int msg
)
80 common_dialog_repaint (h
);
85 /* {{{ Listbox utility functions */
88 #define listbox_refresh(h)
90 #define listbox_refresh(h) common_dialog_repaint(h)
93 static int listbox_callback (Dlg_head
*h
, int id
, int msg
)
103 Listbox
*create_listbox_window (int cols
, int lines
, char *title
, char *help
)
106 Listbox
*listbox
= g_new (Listbox
, 1);
107 char* cancel_string
= _("&Cancel");
110 lines
= (lines
> LINES
-6) ? LINES
- 6 : lines
;
112 if (title
&& (cols
< (len
= strlen(title
) + 2)))
115 /* no &, but 4 spaces around button for brackets and such */
116 if (cols
< (len
= strlen(cancel_string
) + 3))
119 cols
= cols
> COLS
-6 ? COLS
-6 : cols
;
121 /* I'm not sure if this -2 is safe, should test it */
122 xpos
= (COLS
-cols
)/2;
123 ypos
= (LINES
-lines
)/2 - 2;
125 /* Create components */
126 listbox
->dlg
= create_dlg (ypos
, xpos
, lines
+6, cols
+4, dialog_colors
,
127 listbox_callback
, help
, "listbox", DLG_CENTER
|DLG_GRID
);
128 x_set_dialog_title (listbox
->dlg
, title
);
130 listbox
->list
= listbox_new (2, 2, cols
, lines
, listbox_finish
, 0, "li");
132 add_widget (listbox
->dlg
,
133 button_new (lines
+3, (cols
/2 + 2) - len
/2,
134 B_CANCEL
, NORMAL_BUTTON
, cancel_string
, 0, 0, "c"));
135 add_widget (listbox
->dlg
, listbox
->list
);
136 listbox_refresh(listbox
->dlg
);
140 /* Returns the number of the item selected */
141 int run_listbox (Listbox
*l
)
146 if (l
->dlg
->ret_value
== B_CANCEL
)
150 destroy_dlg (l
->dlg
);
158 /* {{{ Query Dialog functions */
165 static int query_callback (struct Dlg_head
*h
, int Id
, int Msg
)
168 struct text_struct
*info
;
170 info
= (struct text_struct
*) h
->data
;
174 /* designate window */
177 draw_box (h
, 1, 1, h
->lines
-2, h
->cols
-2);
178 attrset (HOT_NORMALC
);
179 dlg_move (h
, 1, (h
->cols
-strlen (info
->header
))/2);
180 addstr (info
->header
);
188 Dlg_head
*last_query_dlg
;
190 static int sel_pos
= 0;
192 /* Used to ask questions to the user */
193 int query_dialog (char *header
, char *text
, int flags
, int count
, ...)
203 static int query_colors
[4];
204 static struct text_struct pass
;
206 static char *buttonnames
[10];
209 /* set dialog colors */
210 query_colors
[0] = (flags
& D_ERROR
) ? ERROR_COLOR
: Q_UNSELECTED_COLOR
;
211 query_colors
[1] = (flags
& D_ERROR
) ? REVERSE_COLOR
: Q_SELECTED_COLOR
;
212 query_colors
[2] = (flags
& D_ERROR
) ? ERROR_COLOR
: COLOR_HOT_NORMAL
;
213 query_colors
[3] = (flags
& D_ERROR
) ? COLOR_HOT_NORMAL
: COLOR_HOT_FOCUS
;
215 if (header
== MSG_ERROR
)
216 header
= _(" Error ");
219 va_start (ap
, count
);
220 for (i
= 0; i
< count
; i
++)
222 char* cp
= va_arg (ap
, char *);
223 win_len
+= strlen (cp
) + 6;
224 if (strchr (cp
, '&') != NULL
)
230 /* count coordinates */
231 cols
= 6 + max (win_len
, max (strlen (header
), msglen (text
, &lines
)));
232 lines
+= 4 + (count
> 0 ? 2 : 0);
233 xpos
= COLS
/2 - cols
/2;
234 ypos
= LINES
/3 - (lines
-3)/2;
235 pass
.header
= header
;
239 query_dlg
= create_dlg (ypos
, xpos
, lines
, cols
, query_colors
,
240 query_callback
, "[QueryBox]", "query", DLG_NONE
);
241 x_set_dialog_title (query_dlg
, header
);
243 /* The data we need to pass to the callback */
244 query_dlg
->cols
= cols
;
245 query_dlg
->lines
= lines
;
246 query_dlg
->data
= &pass
;
248 query_dlg
->direction
= DIR_BACKWARD
;
252 cols
= (cols
-win_len
-2)/2+2;
253 va_start (ap
, count
);
254 for (i
= 0; i
< count
; i
++){
255 cur_name
= va_arg (ap
, char *);
256 xpos
= strlen (cur_name
)+6;
257 if (strchr(cur_name
, '&') != NULL
)
259 add_widget (query_dlg
, button_new
260 (lines
-3, cols
, B_USER
+i
, NORMAL_BUTTON
, cur_name
,
264 query_dlg
->initfocus
= query_dlg
->current
;
268 add_widget (query_dlg
, label_new (2, 3, text
, NULL
));
270 /* run dialog and make result */
272 switch (query_dlg
->ret_value
){
276 result
= query_dlg
->ret_value
-B_USER
;
279 /* free used memory */
280 destroy_dlg (query_dlg
);
283 add_widget (query_dlg
, button_new(0, 0, B_EXIT
, NORMAL_BUTTON
,
284 _("&Ok"), 0, 0, NULL
));
286 add_widget (query_dlg
, label_new (2, 3, text
, NULL
));
288 destroy_dlg (query_dlg
);
290 add_widget (query_dlg
, label_new (2, 3, text
, NULL
));
291 add_widget (query_dlg
, button_new(0, 0, 0, HIDDEN_BUTTON
, "-", 0, 0, NULL
));
293 last_query_dlg
= query_dlg
;
299 void query_set_sel (int new_sel
)
306 /* {{{ The message function */
308 /* To show nice messages to the users */
309 Dlg_head
*message (int error
, char *header
, const char *text
, ...)
315 /* Setup the display information */
316 strcpy (buffer
, "\n");
317 va_start (args
, text
);
318 g_vsnprintf (&buffer
[1], sizeof (buffer
) - 1, text
, args
);
319 strcat (buffer
, "\n");
322 query_dialog (header
, buffer
, error
, 0);
326 if (!(error
& D_INSERT
)){
337 /* {{{ The chooser routines */
339 static int remove_callback (int i
, void *data
)
341 Chooser
*c
= (Chooser
*) data
;
343 listbox_remove_current (c
->listbox
, 0);
345 dlg_select_widget (c
->dialog
, c
->listbox
);
346 dlg_select_nth_widget (c
->dialog
, 0);
348 /* Return: do not abort dialog */
352 Chooser
*new_chooser (int lines
, int cols
, char *help
, int flags
)
357 c
=g_new (Chooser
, 1);
358 c
->dialog
= create_dlg (0, 0, lines
, cols
, dialog_colors
, common_dialog_callback
,
359 help
, "chooser", DLG_CENTER
| DLG_GRID
);
361 c
->dialog
->lines
= lines
;
362 c
->dialog
->cols
= cols
;
364 button_lines
= flags
& CHOOSE_EDITABLE
? 3 : 0;
366 c
->listbox
= listbox_new (1, 1, cols
-2, lines
-button_lines
,
367 listbox_finish
, 0, "listbox");
370 add_widget (c
->dialog
, button_new (lines
-button_lines
+1,
371 20, B_ENTER
, DEFPUSH_BUTTON
, _("&Remove"),
372 remove_callback
, c
, "button-remove"));
373 add_widget (c
->dialog
, button_new (lines
-button_lines
+1,
374 4, B_CANCEL
, NORMAL_BUTTON
, _("&Cancel"),
375 0, 0, "button-cancel"));
377 add_widget (c
->dialog
, c
->listbox
);
381 int run_chooser (Chooser
*c
)
384 return c
->dialog
->ret_value
;
387 void destroy_chooser (Chooser
*c
)
389 destroy_dlg (c
->dialog
);
394 /* {{{ Quick dialog routines */
398 static int quick_callback (struct Dlg_head
*h
, int id
, int Msg
)
402 attrset (COLOR_NORMAL
);
404 draw_box (h
, 1, 1, h
->lines
-2, h
->cols
-2);
406 attrset (COLOR_HOT_NORMAL
);
407 dlg_move (h
, 1,((h
->cols
-strlen (h
->data
))/2));
412 h
->ret_value
= B_ENTER
;
420 #define I18N(x) (do_int && *x ? (x = _(x)): x)
422 int quick_dialog_skip (QuickDialog
*qd
, int nskip
)
438 qd
->title
= _(qd
->title
);
443 dd
= create_dlg (0, 0, qd
->ylen
, qd
->xlen
, dialog_colors
, quick_callback
,
444 qd
->help
, qd
->class, DLG_CENTER
| DLG_TRYUP
| DLG_GRID
);
446 dd
= create_dlg (qd
->ypos
, qd
->xpos
, qd
->ylen
, qd
->xlen
, dialog_colors
,
448 qd
->help
, qd
->class, DLG_GRID
);
450 x_set_dialog_title (dd
, qd
->title
);
452 /* We pass this to the callback */
454 dd
->lines
= qd
->ylen
;
455 dd
->data
= qd
->title
;
457 for (qw
= qd
->widgets
; qw
->widget_type
; qw
++){
458 xpos
= (qd
->xlen
* qw
->relative_x
)/qw
->x_divisions
;
459 ypos
= (qd
->ylen
* qw
->relative_y
)/qw
->y_divisions
;
461 switch (qw
->widget_type
){
463 widget
= check_new (ypos
, xpos
, *qw
->result
, I18N (qw
->text
), qw
->tkname
);
467 r
= radio_new (ypos
, xpos
, qw
->hotkey_pos
, qw
->str_result
, 1, qw
->tkname
);
468 r
->pos
= r
->sel
= qw
->value
;
473 widget
= button_new (ypos
, xpos
, qw
->value
, (qw
->value
==B_ENTER
) ? DEFPUSH_BUTTON
: NORMAL_BUTTON
,
474 I18N (qw
->text
), 0, 0, qw
->tkname
);
477 /* We use the hotkey pos as the field length */
479 input
= input_new (ypos
, xpos
, INPUT_COLOR
,
480 qw
->hotkey_pos
, qw
->text
, qw
->tkname
);
481 input
->is_password
= qw
->value
== 1;
484 input
->completion_flags
|= INPUT_COMPLETE_CD
;
489 widget
= label_new (ypos
, xpos
, I18N(qw
->text
), qw
->tkname
);
494 fprintf (stderr
, "QuickWidget: unknown widget type\n");
497 qw
->the_widget
= widget
;
498 add_widget (dd
, widget
);
502 dd
->current
= dd
->current
->next
;
506 /* Get the data if we found something interesting */
507 if (dd
->ret_value
!= B_CANCEL
){
508 for (qw
= qd
->widgets
; qw
->widget_type
; qw
++){
509 switch (qw
->widget_type
){
511 *qw
->result
= ((WCheck
*) qw
->the_widget
)->state
& C_BOOL
;
515 *qw
->result
= ((WRadio
*) qw
->the_widget
)->sel
;
519 *qw
->str_result
= g_strdup (((WInput
*) qw
->the_widget
)->buffer
);
524 return_val
= dd
->ret_value
;
530 int quick_dialog (QuickDialog
*qd
)
532 return quick_dialog_skip (qd
, 0);
537 /* {{{ Input routines */
539 #define INPUT_INDEX 2
540 char *real_input_dialog_help (char *header
, char *text
, char *help
, char *def_text
)
542 QuickDialog Quick_input
;
543 QuickWidget quick_widgets
[] = {
544 { quick_button
, 6, 10, 1, 0, N_("&Cancel"), 0, B_CANCEL
, 0, 0,
546 { quick_button
, 3, 10, 1, 0, N_("&Ok"), 0, B_ENTER
, 0, 0,
548 { quick_input
, 4, 80, 0, 0, "", 58, 0, 0, 0, 0 },
549 { quick_label
, 3, 80, 2, 0, "", 0, 0, 0, 0, "label" },
556 char tk_name
[64] = "inp|";
558 /* we need a unique name for tkname because widget.c:history_tool()
559 needs a unique name for each dialog - using the header is ideal */
561 strncpy (tk_name
+ 3, header
, 60);
563 quick_widgets
[2].tkname
= tk_name
;
565 len
= max (strlen (header
), msglen (text
, &lines
)) + 4;
568 /* Translators should take care as "Password" or its translations
569 are used to identify password boxes and hide characters with "*" */
570 my_str
= _("Password:");
571 if (strncmp (text
, my_str
, strlen (my_str
)-1) == 0){
572 quick_widgets
[INPUT_INDEX
].value
= 1;
575 quick_widgets
[INPUT_INDEX
].value
= 0;
580 * An attempt to place buttons symmetrically, based on actual i18n
581 * length of the string. It looks nicer with i18n (IMO) - alex
583 quick_widgets
[0].relative_x
= len
/2 + 4;
584 quick_widgets
[1].relative_x
=
585 len
/2 - (strlen (_(quick_widgets
[1].text
)) + 9);
586 quick_widgets
[0].x_divisions
= quick_widgets
[1].x_divisions
= len
;
587 #endif /* ENABLE_NLS */
589 Quick_input
.xlen
= len
;
590 Quick_input
.xpos
= -1;
591 Quick_input
.title
= header
;
592 Quick_input
.help
= help
;
593 Quick_input
.class = "quick_input";
594 Quick_input
.i18n
= 0;
595 quick_widgets
[INPUT_INDEX
+1].text
= text
;
596 quick_widgets
[INPUT_INDEX
].text
= def_text
;
598 for (i
= 0; i
< 4; i
++)
599 quick_widgets
[i
].y_divisions
= lines
+6;
600 Quick_input
.ylen
= lines
+ 6;
602 for (i
= 0; i
< 3; i
++)
603 quick_widgets
[i
].relative_y
+= 2 + lines
;
605 quick_widgets
[INPUT_INDEX
].str_result
= &my_str
;
607 Quick_input
.widgets
= quick_widgets
;
608 if (quick_dialog (&Quick_input
) != B_CANCEL
){
609 return *(quick_widgets
[INPUT_INDEX
].str_result
);
615 char *input_dialog (char *header
, char *text
, char *def_text
)
617 return input_dialog_help (header
, text
, "[Input Line Keys]", def_text
);
620 char *input_expand_dialog (char *header
, char *text
, char *def_text
)
625 result
= input_dialog (header
, text
, def_text
);
627 expanded
= tilde_expand (result
);
640 Cause emacs to enter folding mode for this file: