1 /* Widgets for the Midnight Commander
3 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
6 Authors: 1994, 1995 Radek Doulik
7 1994, 1995 Miguel de Icaza
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 * \brief Source: widgets
41 #include <sys/types.h>
45 #include "../src/tty/tty.h"
46 #include "../src/skin/skin.h"
47 #include "../src/tty/mouse.h"
48 #include "../src/tty/key.h" /* XCTRL and ALT macros */
50 #include "../src/mcconfig/mcconfig.h" /* for history loading and saving */
56 #include "../src/event/event.h"
58 #include "cmddef.h" /* CK_ cmd name const */
59 #include "keybind.h" /* global_key_map_t */
61 #define HISTORY_FILE_NAME ".mc/history"
63 const global_key_map_t
*input_map
;
66 widget_selectcolor (Widget
*w
, gboolean focused
, gboolean hotkey
)
68 Dlg_head
*h
= w
->parent
;
73 : DLG_HOT_NORMALC (h
))
80 parse_hotkey (const char *text
)
82 struct hotkey_t result
;
85 /* search for '&', that is not on the of text */
86 cp
= strchr (text
, '&');
87 if (cp
!= NULL
&& cp
[1] != '\0') {
88 result
.start
= g_strndup (text
, cp
- text
);
92 p
= str_cget_next_char (cp
);
93 result
.hotkey
= g_strndup (cp
, p
- cp
);
96 result
.end
= g_strdup (cp
);
98 result
.start
= g_strdup (text
);
106 release_hotkey (const struct hotkey_t hotkey
)
108 g_free (hotkey
.start
);
109 g_free (hotkey
.hotkey
);
114 hotkey_width (const struct hotkey_t hotkey
)
118 result
= str_term_width1 (hotkey
.start
);
119 result
+= (hotkey
.hotkey
!= NULL
) ? str_term_width1 (hotkey
.hotkey
) : 0;
120 result
+= (hotkey
.end
!= NULL
) ? str_term_width1 (hotkey
.end
) : 0;
125 draw_hotkey (Widget
*w
, const struct hotkey_t hotkey
, gboolean focused
)
127 widget_selectcolor (w
, focused
, FALSE
);
128 tty_print_string (hotkey
.start
);
130 if (hotkey
.hotkey
!= NULL
) {
131 widget_selectcolor (w
, focused
, TRUE
);
132 tty_print_string (hotkey
.hotkey
);
133 widget_selectcolor (w
, focused
, FALSE
);
136 if (hotkey
.end
!= NULL
)
137 tty_print_string (hotkey
.end
);
141 /* Default callback for widgets */
143 default_proc (widget_msg_t msg
, int parm
)
158 return MSG_NOT_HANDLED
;
162 static int button_event (Gpm_Event
*event
, void *);
167 button_callback (Widget
*w
, widget_msg_t msg
, int parm
)
169 WButton
*b
= (WButton
*) w
;
172 Dlg_head
*h
= b
->widget
.parent
;
177 * Don't let the default button steal Enter from the current
178 * button. This is a workaround for the flawed event model
179 * when hotkeys are sent to all widgets before the key is
180 * handled by the current widget.
182 if (parm
== '\n' && h
->current
== &b
->widget
) {
183 button_callback (w
, WIDGET_KEY
, ' ');
187 if (parm
== '\n' && b
->flags
== DEFPUSH_BUTTON
) {
188 button_callback (w
, WIDGET_KEY
, ' ');
192 if (b
->text
.hotkey
!= NULL
) {
193 if (g_ascii_tolower ((gchar
)b
->text
.hotkey
[0]) ==
194 g_ascii_tolower ((gchar
)parm
)) {
195 button_callback (w
, WIDGET_KEY
, ' ');
199 return MSG_NOT_HANDLED
;
202 if (parm
!= ' ' && parm
!= '\n')
203 return MSG_NOT_HANDLED
;
206 stop
= (*b
->callback
) (b
->action
);
207 if (!b
->callback
|| stop
) {
208 h
->ret_value
= b
->action
;
229 widget_move (&b
->widget
, 0, b
->hotpos
+ off
);
235 if (msg
== WIDGET_UNFOCUS
)
237 else if (msg
== WIDGET_FOCUS
)
240 widget_selectcolor (w
, b
->selected
, FALSE
);
241 widget_move (w
, 0, 0);
245 tty_print_string ("[< ");
248 tty_print_string ("[ ");
251 tty_print_string ("[");
258 draw_hotkey (w
, b
->text
, b
->selected
);
262 tty_print_string (" >]");
265 tty_print_string (" ]");
268 tty_print_string ("]");
274 release_hotkey (b
->text
);
278 return default_proc (msg
, parm
);
283 button_event (Gpm_Event
*event
, void *data
)
287 if (event
->type
& (GPM_DOWN
|GPM_UP
)){
288 Dlg_head
*h
=b
->widget
.parent
;
289 dlg_select_widget (b
);
290 if (event
->type
& GPM_UP
){
291 button_callback ((Widget
*) data
, WIDGET_KEY
, ' ');
292 (*h
->callback
) (h
, DLG_POST_KEY
, ' ');
300 button_get_len (const WButton
*b
)
302 int ret
= hotkey_width (b
->text
);
321 button_new (int y
, int x
, int action
, int flags
, const char *text
,
324 WButton
*b
= g_new (WButton
, 1);
328 b
->text
= parse_hotkey (text
);
330 init_widget (&b
->widget
, y
, x
, 1, button_get_len (b
),
331 button_callback
, button_event
);
334 b
->callback
= callback
;
335 widget_want_hotkey (b
->widget
, 1);
336 b
->hotpos
= (b
->text
.hotkey
!= NULL
) ? str_term_width1 (b
->text
.start
) : -1;
342 button_get_text (const WButton
*b
)
344 if (b
->text
.hotkey
!= NULL
)
345 return g_strconcat (b
->text
.start
, "&", b
->text
.hotkey
,
348 return g_strdup (b
->text
.start
);
352 button_set_text (WButton
*b
, const char *text
)
354 release_hotkey (b
->text
);
355 b
->text
= parse_hotkey (text
);
356 b
->widget
.cols
= button_get_len (b
);
357 dlg_redraw (b
->widget
.parent
);
361 /* Radio button widget */
362 static int radio_event (Gpm_Event
*event
, void *);
365 radio_callback (Widget
*w
, widget_msg_t msg
, int parm
)
367 WRadio
*r
= (WRadio
*) w
;
369 Dlg_head
*h
= r
->widget
.parent
;
374 int i
, lp
= g_ascii_tolower ((gchar
)parm
);
376 for (i
= 0; i
< r
->count
; i
++) {
377 if (r
->texts
[i
].hotkey
!= NULL
) {
378 int c
= g_ascii_tolower ((gchar
)r
->texts
[i
].hotkey
[0]);
385 radio_callback (w
, WIDGET_KEY
, ' ');
390 return MSG_NOT_HANDLED
;
396 (*h
->callback
) (h
, DLG_ACTION
, 0);
397 radio_callback (w
, WIDGET_FOCUS
, ' ');
406 return MSG_NOT_HANDLED
;
410 if (r
->count
- 1 > r
->pos
) {
415 return MSG_NOT_HANDLED
;
418 (*h
->callback
) (h
, DLG_ACTION
, 0);
419 radio_callback (w
, WIDGET_FOCUS
, ' ');
420 widget_move (&r
->widget
, r
->pos
, 1);
426 for (i
= 0; i
< r
->count
; i
++) {
427 const gboolean focused
= (i
== r
->pos
&& msg
== WIDGET_FOCUS
);
428 widget_selectcolor (w
, focused
, FALSE
);
429 widget_move (&r
->widget
, i
, 0);
430 tty_print_string ((r
->sel
== i
) ? "(*) " : "( ) ");
431 draw_hotkey (w
, r
->texts
[i
], focused
);
436 for (i
= 0; i
< r
->count
; i
++) {
437 release_hotkey (r
->texts
[i
]);
443 return default_proc (msg
, parm
);
448 radio_event (Gpm_Event
*event
, void *data
)
453 if (event
->type
& (GPM_DOWN
|GPM_UP
)){
454 Dlg_head
*h
= r
->widget
.parent
;
456 r
->pos
= event
->y
- 1;
457 dlg_select_widget (r
);
458 if (event
->type
& GPM_UP
){
459 radio_callback (w
, WIDGET_KEY
, ' ');
460 radio_callback (w
, WIDGET_FOCUS
, 0);
461 (*h
->callback
) (h
, DLG_POST_KEY
, ' ');
469 radio_new (int y
, int x
, int count
, const char **texts
)
471 WRadio
*result
= g_new (WRadio
, 1);
474 /* Compute the longest string */
475 result
->texts
= g_new (struct hotkey_t
, count
);
478 for (i
= 0; i
< count
; i
++){
479 result
->texts
[i
] = parse_hotkey (texts
[i
]);
480 m
= hotkey_width (result
->texts
[i
]);
485 init_widget (&result
->widget
, y
, x
, count
, max
, radio_callback
, radio_event
);
489 result
->count
= count
;
490 widget_want_hotkey (result
->widget
, 1);
496 /* Checkbutton widget */
498 static int check_event (Gpm_Event
*event
, void *);
501 check_callback (Widget
*w
, widget_msg_t msg
, int parm
)
503 WCheck
*c
= (WCheck
*) w
;
504 Dlg_head
*h
= c
->widget
.parent
;
508 if (c
->text
.hotkey
!= NULL
) {
509 if (g_ascii_tolower ((gchar
)c
->text
.hotkey
[0]) ==
510 g_ascii_tolower ((gchar
)parm
)) {
512 check_callback (w
, WIDGET_KEY
, ' '); /* make action */
516 return MSG_NOT_HANDLED
;
520 return MSG_NOT_HANDLED
;
522 c
->state
^= C_CHANGE
;
523 (*h
->callback
) (h
, DLG_ACTION
, 0);
524 check_callback (w
, WIDGET_FOCUS
, ' ');
528 widget_move (&c
->widget
, 0, 1);
534 widget_selectcolor (w
, msg
== WIDGET_FOCUS
, FALSE
);
535 widget_move (&c
->widget
, 0, 0);
536 tty_print_string ((c
->state
& C_BOOL
) ? "[x] " : "[ ] ");
537 draw_hotkey (w
, c
->text
, msg
== WIDGET_FOCUS
);
541 release_hotkey (c
->text
);
545 return default_proc (msg
, parm
);
550 check_event (Gpm_Event
*event
, void *data
)
555 if (event
->type
& (GPM_DOWN
|GPM_UP
)){
556 Dlg_head
*h
= c
->widget
.parent
;
558 dlg_select_widget (c
);
559 if (event
->type
& GPM_UP
){
560 check_callback (w
, WIDGET_KEY
, ' ');
561 check_callback (w
, WIDGET_FOCUS
, 0);
562 (*h
->callback
) (h
, DLG_POST_KEY
, ' ');
570 check_new (int y
, int x
, int state
, const char *text
)
572 WCheck
*c
= g_new (WCheck
, 1);
574 c
->text
= parse_hotkey (text
);
576 init_widget (&c
->widget
, y
, x
, 1, hotkey_width (c
->text
),
577 check_callback
, check_event
);
578 c
->state
= state
? C_BOOL
: 0;
579 widget_want_hotkey (c
->widget
, 1);
588 label_callback (Widget
*w
, widget_msg_t msg
, int parm
)
590 WLabel
*l
= (WLabel
*) w
;
591 Dlg_head
*h
= l
->widget
.parent
;
597 /* We don't want to get the focus */
599 return MSG_NOT_HANDLED
;
603 char *p
= l
->text
, *q
, c
= 0;
610 tty_setcolor (DEFAULT_COLOR
);
612 tty_setcolor (DLG_NORMALC (h
));
615 q
= strchr (p
, '\n');
621 widget_move (&l
->widget
, y
, 0);
622 tty_print_string (str_fit_to_term (p
, l
->widget
.cols
, J_LEFT
));
638 return default_proc (msg
, parm
);
643 label_set_text (WLabel
*label
, const char *text
)
645 int newcols
= label
->widget
.cols
;
648 if (label
->text
&& text
&& !strcmp (label
->text
, text
))
649 return; /* Flickering is not nice */
651 g_free (label
->text
);
654 label
->text
= g_strdup (text
);
655 if (label
->auto_adjust_cols
) {
656 str_msg_term_size (text
, &newlines
, &newcols
);
657 if (newcols
> label
->widget
.cols
)
658 label
->widget
.cols
= newcols
;
659 if (newlines
> label
->widget
.lines
)
660 label
->widget
.lines
= newlines
;
662 } else label
->text
= NULL
;
664 if (label
->widget
.parent
)
665 label_callback ((Widget
*) label
, WIDGET_DRAW
, 0);
667 if (newcols
< label
->widget
.cols
)
668 label
->widget
.cols
= newcols
;
672 label_new (int y
, int x
, const char *text
)
679 str_msg_term_size (text
, &lines
, &cols
);
681 l
= g_new (WLabel
, 1);
682 init_widget (&l
->widget
, y
, x
, lines
, cols
, label_callback
, NULL
);
683 l
->text
= (text
!= NULL
) ? g_strdup (text
) : NULL
;
684 l
->auto_adjust_cols
= 1;
686 widget_want_cursor (l
->widget
, 0);
691 /* Gauge widget (progress indicator) */
692 /* Currently width is hardcoded here for text mode */
696 gauge_callback (Widget
*w
, widget_msg_t msg
, int parm
)
698 WGauge
*g
= (WGauge
*) w
;
699 Dlg_head
*h
= g
->widget
.parent
;
701 if (msg
== WIDGET_INIT
)
704 /* We don't want to get the focus */
705 if (msg
== WIDGET_FOCUS
)
706 return MSG_NOT_HANDLED
;
708 if (msg
== WIDGET_DRAW
){
709 widget_move (&g
->widget
, 0, 0);
710 tty_setcolor (DLG_NORMALC (h
));
712 tty_printf ("%*s", gauge_len
, "");
714 int percentage
, columns
;
715 long total
= g
->max
, done
= g
->current
;
717 if (total
<= 0 || done
< 0) {
723 while (total
> 65535) {
727 percentage
= (200 * done
/ total
+ 1) / 2;
728 columns
= (2 * (gauge_len
- 7) * done
/ total
+ 1) / 2;
729 tty_print_char ('[');
730 tty_setcolor (GAUGE_COLOR
);
731 tty_printf ("%*s", (int) columns
, "");
732 tty_setcolor (DLG_NORMALC (h
));
733 tty_printf ("%*s] %3d%%", (int)(gauge_len
- 7 - columns
), "", (int) percentage
);
738 return default_proc (msg
, parm
);
742 gauge_set_value (WGauge
*g
, int max
, int current
)
744 if (g
->current
== current
&& g
->max
== max
)
745 return; /* Do not flicker */
747 max
= 1; /* I do not like division by zero :) */
749 g
->current
= current
;
751 gauge_callback ((Widget
*) g
, WIDGET_DRAW
, 0);
755 gauge_show (WGauge
*g
, int shown
)
757 if (g
->shown
== shown
)
760 gauge_callback ((Widget
*) g
, WIDGET_DRAW
, 0);
764 gauge_new (int y
, int x
, int shown
, int max
, int current
)
766 WGauge
*g
= g_new (WGauge
, 1);
768 init_widget (&g
->widget
, y
, x
, 1, gauge_len
, gauge_callback
, NULL
);
771 max
= 1; /* I do not like division by zero :) */
773 g
->current
= current
;
774 widget_want_cursor (g
->widget
, 0);
781 /* {{{ history button */
783 #define LARGE_HISTORY_BUTTON 1
785 #ifdef LARGE_HISTORY_BUTTON
786 # define HISTORY_BUTTON_WIDTH 3
788 # define HISTORY_BUTTON_WIDTH 1
791 #define should_show_history_button(in) \
792 (in->history && in->field_width > HISTORY_BUTTON_WIDTH * 2 + 1 && in->widget.parent)
794 static void draw_history_button (WInput
* in
)
797 c
= in
->history
->next
? (in
->history
->prev
? '|' : 'v') : '^';
798 widget_move (&in
->widget
, 0, in
->field_width
- HISTORY_BUTTON_WIDTH
);
799 #ifdef LARGE_HISTORY_BUTTON
802 h
= in
->widget
.parent
;
803 tty_setcolor (NORMAL_COLOR
);
804 tty_print_string ("[ ]");
805 /* Too distracting: tty_setcolor (MARKED_COLOR); */
806 widget_move (&in
->widget
, 0, in
->field_width
- HISTORY_BUTTON_WIDTH
+ 1);
810 tty_setcolor (MARKED_COLOR
);
815 /* }}} history button */
818 /* Input widgets now have a global kill ring */
819 /* Pointer to killed data */
820 static char *kill_buffer
= 0;
823 update_input (WInput
*in
, int clear_first
)
827 int buf_len
= str_length (in
->buffer
);
831 if (should_show_history_button (in
))
832 has_history
= HISTORY_BUTTON_WIDTH
;
834 if (in
->disable_update
)
837 pw
= str_term_width2 (in
->buffer
, in
->point
);
839 /* Make the point visible */
840 if ((pw
< in
->term_first_shown
) ||
841 (pw
>= in
->term_first_shown
+ in
->field_width
- has_history
)) {
843 in
->term_first_shown
= pw
- (in
->field_width
/ 3);
844 if (in
->term_first_shown
< 0)
845 in
->term_first_shown
= 0;
848 /* Adjust the mark */
849 if (in
->mark
> buf_len
)
853 draw_history_button (in
);
855 tty_setcolor (in
->color
);
857 widget_move (&in
->widget
, 0, 0);
859 if (!in
->is_password
) {
860 tty_print_string (str_term_substring (in
->buffer
, in
->term_first_shown
,
861 in
->field_width
- has_history
));
864 for (i
= -in
->term_first_shown
; i
< in
->field_width
- has_history
; i
++){
866 tty_print_char ((cp
[0] != '\0') ? '*' : ' ');
868 if (cp
[0] != '\0') str_cnext_char (&cp
);
877 winput_set_origin (WInput
*in
, int x
, int field_width
)
880 in
->field_width
= in
->widget
.cols
= field_width
;
881 update_input (in
, 0);
884 /* {{{ history saving and loading */
886 int num_history_items_recorded
= 60;
889 This loads and saves the history of an input line to and from the
890 widget. It is called with the widgets history name on creation of the
891 widget, and returns the GList list. It stores histories in the file
892 ~/.mc/history in using the profile code.
894 If def_text is passed as INPUT_LAST_TEXT (to the input_new()
895 function) then input_new assigns the default text to be the last text
896 entered, or "" if not found.
900 history_get (const char *input_name
)
910 if (!num_history_items_recorded
) /* this is how to disable */
912 if (!input_name
|| !*input_name
)
915 profile
= concat_dir_and_file (home_dir
, HISTORY_FILE_NAME
);
916 cfg
= mc_config_init (profile
);
918 /* get number of keys */
919 keys
= mc_config_get_keys (cfg
, input_name
, &keys_num
);
922 for (i
= 0; i
< keys_num
; i
++) {
923 char key_name
[BUF_TINY
];
924 g_snprintf (key_name
, sizeof (key_name
), "%lu", (unsigned long)i
);
925 this_entry
= mc_config_get_string (cfg
, input_name
, key_name
, "");
927 if (this_entry
&& *this_entry
)
928 hist
= list_append_unique (hist
, this_entry
);
931 mc_config_deinit (cfg
);
934 /* return pointer to the last entry in the list */
935 return g_list_last (hist
);
939 history_put (const char *input_name
, GList
*h
)
954 if (!num_history_items_recorded
) /* this is how to disable */
957 profile
= concat_dir_and_file (home_dir
, HISTORY_FILE_NAME
);
959 if ((i
= open (profile
, O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
)) != -1)
962 /* Make sure the history is only readable by the user */
963 if (chmod (profile
, S_IRUSR
| S_IWUSR
) == -1 && errno
!= ENOENT
) {
968 /* go to end of list */
971 /* go back 60 places */
972 for (i
= 0; i
< num_history_items_recorded
- 1 && h
->prev
; i
++)
973 h
= g_list_previous (h
);
975 cfg
= mc_config_init(profile
);
978 mc_config_del_group(cfg
,input_name
);
980 /* dump histories into profile */
981 for (i
= 0; h
; h
= g_list_next (h
)) {
984 text
= (char *) h
->data
;
986 /* We shouldn't have null entries, but let's be sure */
988 char key_name
[BUF_TINY
];
989 g_snprintf (key_name
, sizeof (key_name
), "%d", i
++);
990 mc_config_set_string(cfg
,input_name
, key_name
, text
);
994 mc_config_save_file (cfg
);
995 mc_config_deinit(cfg
);
999 /* }}} history saving and loading */
1002 /* {{{ history display */
1007 return _(" History ");
1011 listbox_fwd (WListbox
*l
)
1013 if (l
->current
!= l
->list
->prev
)
1014 listbox_select_entry (l
, l
->current
->next
);
1016 listbox_select_first (l
);
1026 dlg_hist_reposition (Dlg_head
*dlg_head
)
1028 dlg_hist_data
*data
;
1029 int x
= 0, y
, he
, wi
;
1032 if ((dlg_head
== NULL
)
1033 || (dlg_head
->data
== NULL
))
1034 return MSG_NOT_HANDLED
;
1036 data
= (dlg_hist_data
*) dlg_head
->data
;
1038 y
= data
->widget
->y
;
1039 he
= data
->count
+ 2;
1041 if (he
<= y
|| y
> (LINES
- 6)) {
1042 he
= min (he
, y
- 1);
1046 he
= min (he
, LINES
- y
);
1049 if (data
->widget
->x
> 2)
1050 x
= data
->widget
->x
- 2;
1052 wi
= data
->maxlen
+ 4;
1054 if ((wi
+ x
) > COLS
) {
1055 wi
= min (wi
, COLS
);
1059 dlg_set_position (dlg_head
, y
, x
, y
+ he
, x
+ wi
);
1065 dlg_hist_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
1069 return dlg_hist_reposition (h
);
1072 return default_dlg_callback (h
, msg
, parm
);
1077 show_hist (GList
*history
, Widget
*widget
)
1080 size_t maxlen
, i
, count
= 0;
1082 Dlg_head
*query_dlg
;
1083 WListbox
*query_list
;
1084 dlg_hist_data hist_data
;
1086 if (history
== NULL
)
1089 maxlen
= str_term_width1 (i18n_htitle ());
1091 z
= g_list_first (history
);
1094 i
= str_term_width1 ((char *) hi
->data
);
1095 maxlen
= max (maxlen
, i
);
1097 hi
= g_list_next (hi
);
1100 hist_data
.maxlen
= maxlen
;
1101 hist_data
.widget
= widget
;
1102 hist_data
.count
= count
;
1105 create_dlg (0, 0, 4, 4, dialog_colors
, dlg_hist_callback
,
1106 "[History-query]", i18n_htitle (), DLG_COMPACT
);
1107 query_dlg
->data
= &hist_data
;
1109 query_list
= listbox_new (1, 1, 2, 2, NULL
);
1111 /* this call makes list stick to all sides of dialog, effectively make
1112 it be resized with dialog */
1113 add_widget_autopos (query_dlg
, query_list
, WPOS_KEEP_ALL
);
1115 /* to avoid diplicating of (calculating sizes in two places)
1116 code, call dlg_hist_callback function here, to set dialog and
1118 The main idea - create 4x4 dialog and add 2x2 list in
1119 center of it, and let dialog function resize it to needed
1121 dlg_hist_callback (query_dlg
, DLG_RESIZE
, 0);
1123 if (query_dlg
->y
< widget
->y
) {
1127 listbox_add_item (query_list
, LISTBOX_APPEND_AT_END
,
1128 0, (char *) hi
->data
, NULL
);
1129 hi
= g_list_next (hi
);
1131 listbox_select_last (query_list
);
1133 /* traverse backwards */
1134 hi
= g_list_last (history
);
1136 listbox_add_item (query_list
, LISTBOX_APPEND_AT_END
,
1137 0, (char *) hi
->data
, NULL
);
1138 hi
= g_list_previous (hi
);
1142 if (run_dlg (query_dlg
) != B_CANCEL
) {
1143 listbox_get_current (query_list
, &q
, NULL
);
1147 destroy_dlg (query_dlg
);
1152 do_show_hist (WInput
*in
)
1155 r
= show_hist (in
->history
, &in
->widget
);
1157 assign_text (in
, r
);
1162 /* }}} history display */
1165 input_destroy (WInput
*in
)
1168 fprintf (stderr
, "Internal error: null Input *\n");
1176 if (!in
->is_password
) /* don't save passwords ;-) */
1177 history_put (in
->history_name
, in
->history
);
1179 in
->history
= g_list_first (in
->history
);
1180 g_list_foreach (in
->history
, (GFunc
) g_free
, NULL
);
1181 g_list_free (in
->history
);
1184 g_free (in
->buffer
);
1185 free_completions (in
);
1186 g_free (in
->history_name
);
1190 input_disable_update (WInput
*in
)
1192 in
->disable_update
++;
1196 input_enable_update (WInput
*in
)
1198 in
->disable_update
--;
1199 update_input (in
, 0);
1202 #define ELEMENTS(a) (sizeof(a)/sizeof(a[0]))
1205 push_history (WInput
*in
, const char *text
)
1208 /* input widget where urls with passwords are entered without any
1210 static const char *password_input_fields
[] = {
1211 N_(" Link to a remote machine "),
1212 N_(" FTP to machine "),
1213 N_(" SMB link to machine ")
1221 for (i
= 0; i
< ELEMENTS (password_input_fields
); i
++)
1222 password_input_fields
[i
] = _(password_input_fields
[i
]);
1225 for (p
= text
; *p
== ' ' || *p
== '\t'; p
++);
1230 /* Avoid duplicated entries */
1231 in
->history
= g_list_last (in
->history
);
1232 if (!strcmp ((char *) in
->history
->data
, text
))
1236 t
= g_strdup (text
);
1238 if (in
->history_name
) {
1239 p
= in
->history_name
+ 3;
1240 for (i
= 0; i
< ELEMENTS (password_input_fields
); i
++)
1241 if (strcmp (p
, password_input_fields
[i
]) == 0)
1243 if (i
< ELEMENTS (password_input_fields
))
1244 strip_password (t
, 0);
1246 strip_password (t
, 1);
1249 in
->history
= list_append_unique (in
->history
, t
);
1257 /* Cleans the input line and adds the current text to the history */
1259 new_input (WInput
*in
)
1262 push_history (in
, in
->buffer
);
1264 in
->buffer
[0] = '\0';
1268 free_completions (in
);
1269 update_input (in
, 0);
1273 move_buffer_backward (WInput
*in
, int start
, int end
)
1276 int str_len
= str_length (in
->buffer
);
1277 if (start
>= str_len
|| end
> str_len
+ 1) return;
1279 pos
= str_offset_to_pos (in
->buffer
, start
);
1280 len
= str_offset_to_pos (in
->buffer
, end
) - pos
;
1282 for (i
= pos
; in
->buffer
[i
+ len
- 1]; i
++)
1283 in
->buffer
[i
] = in
->buffer
[i
+ len
];
1287 insert_char (WInput
*in
, int c_code
)
1293 return MSG_NOT_HANDLED
;
1295 if (in
->charpoint
>= MB_LEN_MAX
)
1298 in
->charbuf
[in
->charpoint
] = c_code
;
1301 res
= str_is_valid_char (in
->charbuf
, in
->charpoint
);
1304 in
->charpoint
= 0; /* broken multibyte char, skip */
1309 if (strlen (in
->buffer
) + 1 + in
->charpoint
>= in
->current_max_size
){
1310 /* Expand the buffer */
1311 size_t new_length
= in
->current_max_size
+
1312 in
->field_width
+ in
->charpoint
;
1313 char *narea
= g_try_renew (char, in
->buffer
, new_length
);
1316 in
->current_max_size
= new_length
;
1320 if (strlen (in
->buffer
) + in
->charpoint
< in
->current_max_size
) {
1321 /* bytes from begin */
1322 size_t ins_point
= str_offset_to_pos (in
->buffer
, in
->point
);
1324 size_t rest_bytes
= strlen (in
->buffer
+ ins_point
);
1326 for (i
= rest_bytes
+ 1; i
> 0; i
--)
1327 in
->buffer
[ins_point
+ i
+ in
->charpoint
- 1] =
1328 in
->buffer
[ins_point
+ i
- 1];
1330 memcpy(in
->buffer
+ ins_point
, in
->charbuf
, in
->charpoint
);
1339 beginning_of_line (WInput
*in
)
1346 end_of_line (WInput
*in
)
1348 in
->point
= str_length (in
->buffer
);
1353 backward_char (WInput
*in
)
1355 const char *act
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
1357 if (in
->point
> 0) {
1358 in
->point
-= str_cprev_noncomb_char (&act
, in
->buffer
);
1364 forward_char (WInput
*in
)
1366 const char *act
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
1367 if (act
[0] != '\0') {
1368 in
->point
+= str_cnext_noncomb_char (&act
);
1374 forward_word (WInput
* in
)
1376 const char *p
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
1378 while (p
[0] != '\0' && (str_isspace (p
) || str_ispunct (p
))) {
1379 str_cnext_char (&p
);
1382 while (p
[0] != '\0' && !str_isspace (p
) && !str_ispunct (p
)) {
1383 str_cnext_char (&p
);
1389 backward_word (WInput
*in
)
1391 const char *p
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
1393 while ((p
!= in
->buffer
) && (p
[0] == '\0')) {
1398 while ((p
!= in
->buffer
) && (str_isspace (p
) || str_ispunct (p
))) {
1399 str_cprev_char (&p
);
1402 while ((p
!= in
->buffer
) && !str_isspace (p
) && !str_ispunct (p
)) {
1403 str_cprev_char (&p
);
1409 key_left (WInput
*in
)
1415 key_ctrl_left (WInput
*in
)
1421 key_right (WInput
*in
)
1427 key_ctrl_right (WInput
*in
)
1432 backward_delete (WInput
*in
)
1434 const char *act
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
1440 start
= in
->point
- str_cprev_noncomb_char (&act
, in
->buffer
);
1441 move_buffer_backward(in
, start
, in
->point
);
1448 delete_char (WInput
*in
)
1450 const char *act
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
1451 int end
= in
->point
;
1453 end
+= str_cnext_noncomb_char (&act
);
1455 move_buffer_backward(in
, in
->point
, end
);
1461 copy_region (WInput
*in
, int x_first
, int x_last
)
1463 int first
= min (x_first
, x_last
);
1464 int last
= max (x_first
, x_last
);
1469 g_free (kill_buffer
);
1471 first
= str_offset_to_pos (in
->buffer
, first
);
1472 last
= str_offset_to_pos (in
->buffer
, last
);
1474 kill_buffer
= g_strndup(in
->buffer
+ first
, last
- first
);
1478 delete_region (WInput
*in
, int x_first
, int x_last
)
1480 int first
= min (x_first
, x_last
);
1481 int last
= max (x_first
, x_last
);
1486 last
= str_offset_to_pos (in
->buffer
, last
);
1487 first
= str_offset_to_pos (in
->buffer
, first
);
1488 len
= strlen (&in
->buffer
[last
]) + 1;
1489 memmove (&in
->buffer
[first
], &in
->buffer
[last
], len
);
1495 kill_word (WInput
*in
)
1497 int old_point
= in
->point
;
1501 new_point
= in
->point
;
1502 in
->point
= old_point
;
1504 copy_region (in
, old_point
, new_point
);
1505 delete_region (in
, old_point
, new_point
);
1512 back_kill_word (WInput
*in
)
1514 int old_point
= in
->point
;
1518 new_point
= in
->point
;
1519 in
->point
= old_point
;
1521 copy_region (in
, old_point
, new_point
);
1522 delete_region (in
, old_point
, new_point
);
1527 set_mark (WInput
*in
)
1529 in
->mark
= in
->point
;
1533 kill_save (WInput
*in
)
1535 copy_region (in
, in
->mark
, in
->point
);
1539 kill_region (WInput
*in
)
1542 delete_region (in
, in
->point
, in
->mark
);
1553 for (p
= kill_buffer
; *p
; p
++)
1554 insert_char (in
, *p
);
1559 kill_line (WInput
*in
)
1561 int chp
= str_offset_to_pos (in
->buffer
, in
->point
);
1562 g_free (kill_buffer
);
1563 kill_buffer
= g_strdup (&in
->buffer
[chp
]);
1564 in
->buffer
[chp
] = '\0';
1569 assign_text (WInput
*in
, const char *text
)
1571 free_completions (in
);
1572 g_free (in
->buffer
);
1573 in
->buffer
= g_strdup (text
); /* was in->buffer->text */
1574 in
->current_max_size
= strlen (in
->buffer
) + 1;
1575 in
->point
= str_length (in
->buffer
);
1582 hist_prev (WInput
*in
)
1587 if (in
->need_push
) {
1588 switch (push_history (in
, in
->buffer
)) {
1590 in
->history
= g_list_previous (in
->history
);
1593 if (in
->history
->prev
)
1594 in
->history
= g_list_previous (in
->history
);
1599 } else if (in
->history
->prev
)
1600 in
->history
= g_list_previous (in
->history
);
1603 assign_text (in
, (char *) in
->history
->data
);
1608 hist_next (WInput
*in
)
1610 if (in
->need_push
) {
1611 switch (push_history (in
, in
->buffer
)) {
1613 assign_text (in
, "");
1623 if (!in
->history
->next
) {
1624 assign_text (in
, "");
1628 in
->history
= g_list_next (in
->history
);
1629 assign_text (in
, (char *) in
->history
->data
);
1634 port_region_marked_for_delete (WInput
*in
)
1636 in
->buffer
[0] = '\0';
1643 input_execute_cmd (WInput
*in
, int command
)
1645 cb_ret_t res
= MSG_HANDLED
;
1649 beginning_of_line (in
);
1654 case CK_InputMoveLeft
:
1657 case CK_InputWordLeft
:
1660 case CK_InputMoveRight
:
1663 case CK_InputWordRight
:
1664 key_ctrl_right (in
);
1666 case CK_InputBackwardChar
:
1669 case CK_InputBackwardWord
:
1672 case CK_InputForwardChar
:
1675 case CK_InputForwardWord
:
1678 case CK_InputBackwardDelete
:
1679 backward_delete (in
);
1681 case CK_InputDeleteChar
:
1684 case CK_InputKillWord
:
1687 case CK_InputBackwardKillWord
:
1688 back_kill_word (in
);
1690 case CK_InputSetMark
:
1693 case CK_InputKillRegion
:
1696 case CK_InputKillSave
:
1702 case CK_InputKillLine
:
1705 case CK_InputHistoryPrev
:
1708 case CK_InputHistoryNext
:
1711 case CK_InputHistoryShow
:
1714 case CK_InputComplete
:
1718 res
= MSG_NOT_HANDLED
;
1724 /* This function is a test for a special input key used in complete.c */
1725 /* Returns 0 if it is not a special key, 1 if it is a non-complete key
1726 and 2 if it is a complete key */
1728 is_in_input_map (WInput
*in
, int key
)
1732 for (i
= 0; input_map
[i
].key
; i
++) {
1733 if (key
== input_map
[i
].key
) {
1734 input_execute_cmd (in
, input_map
[i
].command
);
1735 if (input_map
[i
].command
== CK_InputComplete
)
1745 handle_char (WInput
*in
, int key
)
1750 v
= MSG_NOT_HANDLED
;
1753 free_completions (in
);
1754 v
= insert_char (in
, key
);
1755 update_input (in
, 1);
1760 for (i
= 0; input_map
[i
].key
; i
++) {
1761 if (key
== input_map
[i
].key
) {
1762 if (input_map
[i
].command
!= CK_InputComplete
) {
1763 free_completions (in
);
1764 input_execute_cmd (in
, input_map
[i
].command
);
1765 update_input (in
, 1);
1771 if (input_map
[i
].command
== 0) {
1773 return MSG_NOT_HANDLED
;
1775 port_region_marked_for_delete (in
);
1776 free_completions (in
);
1777 v
= insert_char (in
, key
);
1779 update_input (in
, 1);
1783 /* Inserts text in input line */
1785 stuff (WInput
*in
, const char *text
, int insert_extra_space
)
1787 input_disable_update (in
);
1789 handle_char (in
, *text
++);
1790 if (insert_extra_space
)
1791 handle_char (in
, ' ');
1792 input_enable_update (in
);
1793 update_input (in
, 1);
1797 input_set_point (WInput
*in
, int pos
)
1799 int max_pos
= str_length (in
->buffer
);
1803 if (pos
!= in
->point
)
1804 free_completions (in
);
1807 update_input (in
, 1);
1811 input_callback (Widget
*w
, widget_msg_t msg
, int parm
)
1813 WInput
*in
= (WInput
*) w
;
1818 if (parm
== XCTRL ('q')) {
1820 v
= handle_char (in
, ascii_alpha_to_cntrl (tty_getch ()));
1825 /* Keys we want others to handle */
1826 if (parm
== KEY_UP
|| parm
== KEY_DOWN
|| parm
== ESC_CHAR
1827 || parm
== KEY_F (10) || parm
== XCTRL ('g') || parm
== '\n')
1828 return MSG_NOT_HANDLED
;
1830 /* When pasting multiline text, insert literal Enter */
1831 if ((parm
& ~KEY_M_MASK
) == '\n') {
1833 v
= handle_char (in
, '\n');
1838 return handle_char (in
, parm
);
1840 case WIDGET_COMMAND
:
1841 return input_execute_cmd (in
, parm
);
1844 case WIDGET_UNFOCUS
:
1846 update_input (in
, 0);
1850 widget_move (&in
->widget
, 0, str_term_width2 (in
->buffer
, in
->point
)
1851 - in
->term_first_shown
);
1854 case WIDGET_DESTROY
:
1859 return default_proc (msg
, parm
);
1864 input_event (Gpm_Event
* event
, void *data
)
1868 if (event
->type
& (GPM_DOWN
| GPM_DRAG
)) {
1869 dlg_select_widget (in
);
1871 if (event
->x
>= in
->field_width
- HISTORY_BUTTON_WIDTH
+ 1
1872 && should_show_history_button (in
)) {
1875 in
->point
= str_length (in
->buffer
);
1876 if (event
->x
+ in
->term_first_shown
- 1 <
1877 str_term_width1 (in
->buffer
))
1879 in
->point
= str_column_to_pos (in
->buffer
, event
->x
1880 + in
->term_first_shown
- 1);
1883 update_input (in
, 1);
1889 input_new (int y
, int x
, int color
, int width
, const char *def_text
,
1890 const char *histname
, INPUT_COMPLETE_FLAGS completion_flags
)
1892 WInput
*in
= g_new (WInput
, 1);
1893 int initial_buffer_len
;
1895 init_widget (&in
->widget
, y
, x
, 1, width
, input_callback
, input_event
);
1899 in
->history_name
= 0;
1902 in
->history_name
= g_strdup (histname
);
1903 in
->history
= history_get (histname
);
1907 if (def_text
== NULL
)
1910 if (def_text
== INPUT_LAST_TEXT
) {
1913 if (in
->history
->data
)
1914 def_text
= (char *) in
->history
->data
;
1916 initial_buffer_len
= 1 + max ((size_t) width
, strlen (def_text
));
1917 in
->widget
.options
|= W_IS_INPUT
;
1918 in
->completions
= NULL
;
1919 in
->completion_flags
= completion_flags
;
1920 in
->current_max_size
= initial_buffer_len
;
1921 in
->buffer
= g_new (char, initial_buffer_len
);
1923 in
->field_width
= width
;
1925 in
->term_first_shown
= 0;
1926 in
->disable_update
= 0;
1929 in
->is_password
= 0;
1931 strcpy (in
->buffer
, def_text
);
1932 in
->point
= str_length (in
->buffer
);
1937 /* Listbox widget */
1939 /* Should draw the scrollbar, but currently draws only
1940 * indications that there is more information
1942 static int listbox_cdiff (WLEntry
*s
, WLEntry
*e
);
1945 listbox_drawscroll (WListbox
*l
)
1949 int max_line
= l
->widget
.lines
- 1;
1951 /* Are we at the top? */
1952 widget_move (&l
->widget
, 0, l
->widget
.cols
);
1953 if (l
->list
== l
->top
)
1954 tty_print_one_vline ();
1956 tty_print_char ('^');
1958 /* Are we at the bottom? */
1959 widget_move (&l
->widget
, max_line
, l
->widget
.cols
);
1960 top
= listbox_cdiff (l
->list
, l
->top
);
1961 if ((top
+ l
->widget
.lines
== l
->count
) || l
->widget
.lines
>= l
->count
)
1962 tty_print_one_vline ();
1964 tty_print_char ('v');
1966 /* Now draw the nice relative pointer */
1968 line
= 1+ ((l
->pos
* (l
->widget
.lines
- 2)) / l
->count
);
1970 for (i
= 1; i
< max_line
; i
++){
1971 widget_move (&l
->widget
, i
, l
->widget
.cols
);
1973 tty_print_one_vline ();
1975 tty_print_char ('*');
1980 listbox_draw (WListbox
*l
, gboolean focused
)
1982 const Dlg_head
*h
= l
->widget
.parent
;
1983 const int normalc
= DLG_NORMALC (h
);
1984 int selc
= focused
? DLG_HOT_FOCUSC (h
) : DLG_FOCUSC (h
);
1991 for (e
= l
->top
, i
= 0; i
< l
->widget
.lines
; i
++) {
1992 /* Display the entry */
1993 if (e
== l
->current
&& sel_line
== -1) {
1995 tty_setcolor (selc
);
1997 tty_setcolor (normalc
);
1999 widget_move (&l
->widget
, i
, 1);
2001 if ((i
> 0 && e
== l
->list
) || !l
->list
)
2007 tty_print_string (str_fit_to_term (text
, l
->widget
.cols
- 2, J_LEFT_FIT
));
2009 l
->cursor_y
= sel_line
;
2011 if (l
->scrollbar
&& l
->count
> l
->widget
.lines
) {
2012 tty_setcolor (normalc
);
2013 listbox_drawscroll (l
);
2017 /* Returns the number of items between s and e,
2018 must be on the same linked list */
2020 listbox_cdiff (WLEntry
*s
, WLEntry
*e
)
2024 for (count
= 0; s
!= e
; count
++)
2030 listbox_check_hotkey (WListbox
*l
, int key
)
2042 /* If we didn't find anything, return */
2043 if (i
&& e
== l
->list
)
2046 if (e
->hotkey
== key
)
2054 /* Selects the last entry and scrolls the list to the bottom */
2056 listbox_select_last (WListbox
*l
)
2059 l
->current
= l
->top
= l
->list
->prev
;
2060 for (i
= min (l
->widget
.lines
, l
->count
) - 1; i
; i
--)
2061 l
->top
= l
->top
->prev
;
2062 l
->pos
= l
->count
- 1;
2065 /* Selects the first entry and scrolls the list to the top */
2067 listbox_select_first (WListbox
*l
)
2069 l
->current
= l
->top
= l
->list
;
2074 listbox_remove_list (WListbox
*l
)
2083 while (l
->count
--) {
2089 l
->pos
= l
->count
= 0;
2090 l
->list
= l
->top
= l
->current
= 0;
2094 * bor 30.10.96: added force flag to remove *last* entry as well
2095 * bor 30.10.96: corrected selection bug if last entry was removed
2099 listbox_remove_current (WListbox
*l
, int force
)
2103 /* Ok, note: this won't allow for emtpy lists */
2104 if (!force
&& (!l
->count
|| l
->count
== 1))
2111 l
->current
->next
->prev
= l
->current
->prev
;
2112 l
->current
->prev
->next
= l
->current
->next
;
2113 if (p
->next
== l
->list
) {
2114 l
->current
= p
->prev
;
2118 l
->current
= p
->next
;
2121 l
->list
= l
->top
= p
->next
;
2124 l
->list
= l
->top
= l
->current
= 0;
2131 /* Makes *e the selected entry (sets current and pos) */
2133 listbox_select_entry (WListbox
*l
, WLEntry
*dest
)
2142 for (pos
= 0, e
= l
->list
; pos
< l
->count
; e
= e
->next
, pos
++){
2150 while (listbox_cdiff (l
->top
, l
->current
) >= l
->widget
.lines
)
2151 l
->top
= l
->top
->next
;
2153 l
->top
= l
->current
;
2159 /* If we are unable to find it, set decent values */
2160 l
->current
= l
->top
= l
->list
;
2164 /* Selects from base the pos element */
2166 listbox_select_pos (WListbox
*l
, WLEntry
*base
, int pos
)
2168 WLEntry
*last
= l
->list
->prev
;
2181 listbox_back (WListbox
*l
)
2184 listbox_select_entry (l
, l
->current
->prev
);
2186 listbox_select_last (l
);
2189 /* Return MSG_HANDLED if we want a redraw */
2191 listbox_key (WListbox
*l
, int key
)
2195 cb_ret_t j
= MSG_NOT_HANDLED
;
2197 /* focus on listbox item N by '0'..'9' keys */
2198 if (key
>= '0' && key
<= '9') {
2199 int oldpos
= l
->pos
;
2200 listbox_select_by_number(l
, key
- '0');
2202 /* need scroll to item? */
2203 if (abs(oldpos
- l
->pos
) > l
->widget
.lines
)
2204 l
->top
= l
->current
;
2210 return MSG_NOT_HANDLED
;
2216 listbox_select_first (l
);
2222 listbox_select_last (l
);
2237 for (i
= 0; ((i
< l
->widget
.lines
- 1)
2238 && (l
->current
!= l
->list
->prev
)); i
++) {
2246 for (i
= 0; ((i
< l
->widget
.lines
- 1)
2247 && (l
->current
!= l
->list
)); i
++) {
2253 return MSG_NOT_HANDLED
;
2257 listbox_destroy (WListbox
*l
)
2259 WLEntry
*n
, *p
= l
->list
;
2262 for (i
= 0; i
< l
->count
; i
++){
2271 listbox_callback (Widget
*w
, widget_msg_t msg
, int parm
)
2273 WListbox
*l
= (WListbox
*) w
;
2274 Dlg_head
*h
= l
->widget
.parent
;
2283 e
= listbox_check_hotkey (l
, parm
);
2287 listbox_select_entry (l
, e
);
2289 (*h
->callback
) (h
, DLG_ACTION
, l
->pos
);
2292 action
= (*l
->cback
) (l
);
2294 action
= LISTBOX_DONE
;
2296 if (action
== LISTBOX_DONE
) {
2297 h
->ret_value
= B_ENTER
;
2302 return MSG_NOT_HANDLED
;
2305 ret_code
= listbox_key (l
, parm
);
2306 if (ret_code
!= MSG_NOT_HANDLED
) {
2307 listbox_draw (l
, TRUE
);
2308 (*h
->callback
) (h
, DLG_ACTION
, l
->pos
);
2313 widget_move (&l
->widget
, l
->cursor_y
, 0);
2314 (*h
->callback
) (h
, DLG_ACTION
, l
->pos
);
2318 case WIDGET_UNFOCUS
:
2320 listbox_draw (l
, msg
!= WIDGET_UNFOCUS
);
2323 case WIDGET_DESTROY
:
2324 listbox_destroy (l
);
2327 case WIDGET_RESIZED
:
2331 return default_proc (msg
, parm
);
2336 listbox_event (Gpm_Event
*event
, void *data
)
2341 Dlg_head
*h
= l
->widget
.parent
;
2344 if (event
->type
& GPM_DOWN
)
2345 dlg_select_widget (l
);
2347 if (l
->list
== NULL
)
2350 if (event
->type
& (GPM_DOWN
| GPM_DRAG
)) {
2351 int ret
= MOU_REPEAT
;
2353 if (event
->x
< 0 || event
->x
> l
->widget
.cols
)
2357 for (i
= -event
->y
; i
>= 0; i
--)
2359 else if (event
->y
> l
->widget
.lines
)
2360 for (i
= event
->y
- l
->widget
.lines
; i
> 0; i
--)
2362 else if (event
->buttons
& GPM_B_UP
) {
2365 } else if (event
->buttons
& GPM_B_DOWN
) {
2369 listbox_select_entry (l
,
2370 listbox_select_pos (l
, l
->top
,
2373 /* We need to refresh ourselves since the dialog manager doesn't */
2374 /* know about this event */
2375 listbox_draw (l
, TRUE
);
2380 if ((event
->type
& (GPM_DOUBLE
| GPM_UP
)) == (GPM_UP
| GPM_DOUBLE
)) {
2383 if (event
->x
< 0 || event
->x
>= l
->widget
.cols
2384 || event
->y
< 1 || event
->y
> l
->widget
.lines
)
2387 dlg_select_widget (l
);
2388 listbox_select_entry (l
,
2389 listbox_select_pos (l
, l
->top
,
2393 action
= (*l
->cback
) (l
);
2395 action
= LISTBOX_DONE
;
2397 if (action
== LISTBOX_DONE
) {
2398 h
->ret_value
= B_ENTER
;
2407 listbox_new (int y
, int x
, int height
, int width
, lcback callback
)
2409 WListbox
*l
= g_new (WListbox
, 1);
2414 init_widget (&l
->widget
, y
, x
, height
, width
,
2415 listbox_callback
, listbox_event
);
2417 l
->list
= l
->top
= l
->current
= 0;
2420 l
->cback
= callback
;
2421 l
->allow_duplicates
= 1;
2422 l
->scrollbar
= !tty_is_slow ();
2423 widget_want_hotkey (l
->widget
, 1);
2428 /* Listbox item adding function. They still lack a lot of functionality */
2430 /* 1.11.96 bor: added pos argument to control placement of new entry */
2432 listbox_append_item (WListbox
*l
, WLEntry
*e
, enum append_pos pos
)
2440 } else if (pos
== LISTBOX_APPEND_AT_END
) {
2442 e
->prev
= l
->list
->prev
;
2443 l
->list
->prev
->next
= e
;
2445 } else if (pos
== LISTBOX_APPEND_BEFORE
){
2446 e
->next
= l
->current
;
2447 e
->prev
= l
->current
->prev
;
2448 l
->current
->prev
->next
= e
;
2449 l
->current
->prev
= e
;
2450 if (l
->list
== l
->current
) { /* move list one position down */
2454 } else if (pos
== LISTBOX_APPEND_AFTER
) {
2455 e
->prev
= l
->current
;
2456 e
->next
= l
->current
->next
;
2457 l
->current
->next
->prev
= e
;
2458 l
->current
->next
= e
;
2459 } else if (pos
== LISTBOX_APPEND_SORTED
) {
2460 WLEntry
*w
= l
->list
;
2462 while (w
->next
!= l
->list
&& strcmp (e
->text
, w
->text
) > 0)
2464 if (w
->next
== l
->list
) {
2480 listbox_add_item (WListbox
*l
, enum append_pos pos
, int hotkey
,
2481 const char *text
, void *data
)
2488 if (!l
->allow_duplicates
)
2489 if (listbox_search_text (l
, text
))
2492 entry
= g_new (WLEntry
, 1);
2493 entry
->text
= g_strdup (text
);
2495 entry
->hotkey
= hotkey
;
2497 listbox_append_item (l
, entry
, pos
);
2502 /* Selects the nth entry in the listbox */
2504 listbox_select_by_number (WListbox
*l
, int n
)
2506 if (l
->list
!= NULL
)
2507 listbox_select_entry (l
, listbox_select_pos (l
, l
->list
, n
));
2511 listbox_search_text (WListbox
*l
, const char *text
)
2520 if(!strcmp (e
->text
, text
))
2523 } while (e
!=l
->list
);
2528 /* Returns the current string text as well as the associated extra data */
2530 listbox_get_current (WListbox
*l
, char **string
, char **extra
)
2536 if (string
&& l
->current
)
2537 *string
= l
->current
->text
;
2538 if (extra
&& l
->current
)
2539 *extra
= l
->current
->data
;
2542 /* returns TRUE if a function has been called, FALSE otherwise. */
2544 buttonbar_call (WButtonBar
*bb
, int i
)
2546 switch (bb
->labels
[i
].tag
) {
2550 bb
->labels
[i
].u
.fn_void ();
2553 bb
->labels
[i
].u
.fn_ptr (bb
->labels
[i
].data
);
2559 /* calculate width of one button, width is never lesser than 7 */
2561 buttonbat_get_button_width ()
2563 int result
= COLS
/ BUTTONBAR_LABELS_NUM
;
2564 return (result
>= 7) ? result
: 7;
2569 buttonbar_callback (Widget
*w
, widget_msg_t msg
, int parm
)
2571 WButtonBar
*bb
= (WButtonBar
*) w
;
2577 return MSG_NOT_HANDLED
;
2580 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++)
2581 if (parm
== KEY_F (i
+ 1) && buttonbar_call (bb
, i
))
2583 return MSG_NOT_HANDLED
;
2587 widget_move (&bb
->widget
, 0, 0);
2588 tty_setcolor (DEFAULT_COLOR
);
2589 bb
->btn_width
= buttonbat_get_button_width ();
2590 tty_printf ("%-*s", bb
->widget
.cols
, "");
2592 for (i
= 0; i
< COLS
/ bb
->btn_width
&& i
< BUTTONBAR_LABELS_NUM
; i
++) {
2593 widget_move (&bb
->widget
, 0, i
* bb
->btn_width
);
2594 tty_setcolor (DEFAULT_COLOR
);
2595 tty_printf ("%2d", i
+ 1);
2596 tty_setcolor (SELECTED_COLOR
);
2597 text
= (bb
->labels
[i
].text
!= NULL
) ? bb
->labels
[i
].text
: "";
2598 tty_print_string (str_fit_to_term (text
, bb
->btn_width
- 2, J_CENTER_LEFT
));
2603 case WIDGET_DESTROY
:
2604 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++)
2605 g_free (bb
->labels
[i
].text
);
2609 return default_proc (msg
, parm
);
2614 buttonbar_event (Gpm_Event
*event
, void *data
)
2616 WButtonBar
*bb
= data
;
2619 if (!(event
->type
& GPM_UP
))
2623 button
= (event
->x
- 1) / bb
->btn_width
;
2624 if (button
< BUTTONBAR_LABELS_NUM
)
2625 buttonbar_call (bb
, button
);
2630 buttonbar_new (int visible
)
2635 bb
= g_new0 (WButtonBar
, 1);
2637 init_widget (&bb
->widget
, LINES
- 1, 0, 1, COLS
,
2638 buttonbar_callback
, buttonbar_event
);
2639 bb
->widget
.pos_flags
= WPOS_KEEP_HORZ
| WPOS_KEEP_BOTTOM
;
2640 bb
->visible
= visible
;
2641 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++){
2642 bb
->labels
[i
].text
= NULL
;
2643 bb
->labels
[i
].tag
= BBFUNC_NONE
;
2645 widget_want_hotkey (bb
->widget
, 1);
2646 widget_want_cursor (bb
->widget
, 0);
2647 bb
->btn_width
= buttonbat_get_button_width ();
2653 set_label_text (WButtonBar
* bb
, int index
, const char *text
)
2655 g_free (bb
->labels
[index
- 1].text
);
2657 bb
->labels
[index
- 1].text
= g_strdup (text
);
2660 /* Find ButtonBar widget in the dialog */
2662 find_buttonbar (Dlg_head
*h
)
2666 bb
= (WButtonBar
*) find_widget_type (h
, buttonbar_callback
);
2671 buttonbar_clear_label (Dlg_head
*h
, int idx
)
2673 WButtonBar
*bb
= find_buttonbar (h
);
2678 set_label_text (bb
, idx
, "");
2679 bb
->labels
[idx
- 1].tag
= BBFUNC_NONE
;
2683 buttonbar_set_label_data (Dlg_head
*h
, int idx
, const char *text
,
2684 buttonbarfn cback
, void *data
)
2686 WButtonBar
*bb
= find_buttonbar (h
);
2691 assert (cback
!= (buttonbarfn
) 0);
2692 set_label_text (bb
, idx
, text
);
2693 bb
->labels
[idx
- 1].tag
= BBFUNC_PTR
;
2694 bb
->labels
[idx
- 1].u
.fn_ptr
= cback
;
2695 bb
->labels
[idx
- 1].data
= data
;
2699 buttonbar_set_label (Dlg_head
*h
, int idx
, const char *text
, voidfn cback
)
2701 WButtonBar
*bb
= find_buttonbar (h
);
2706 assert (cback
!= (voidfn
) 0);
2707 set_label_text (bb
, idx
, text
);
2708 bb
->labels
[idx
- 1].tag
= BBFUNC_VOID
;
2709 bb
->labels
[idx
- 1].u
.fn_void
= cback
;
2713 buttonbar_set_visible (WButtonBar
*bb
, gboolean visible
)
2715 bb
->visible
= visible
;
2719 buttonbar_redraw (Dlg_head
*h
)
2721 WButtonBar
*bb
= find_buttonbar (h
);
2726 send_message ((Widget
*) bb
, WIDGET_DRAW
, 0);
2730 groupbox_callback (Widget
*w
, widget_msg_t msg
, int parm
)
2732 WGroupbox
*g
= (WGroupbox
*) w
;
2739 return MSG_NOT_HANDLED
;
2742 tty_setcolor (COLOR_NORMAL
);
2743 draw_box (g
->widget
.parent
, g
->widget
.y
- g
->widget
.parent
->y
,
2744 g
->widget
.x
- g
->widget
.parent
->x
, g
->widget
.lines
,
2747 tty_setcolor (COLOR_HOT_NORMAL
);
2748 dlg_move (g
->widget
.parent
, g
->widget
.y
- g
->widget
.parent
->y
,
2749 g
->widget
.x
- g
->widget
.parent
->x
+ 1);
2750 tty_print_string (g
->title
);
2753 case WIDGET_DESTROY
:
2758 return default_proc (msg
, parm
);
2763 groupbox_new (int y
, int x
, int height
, int width
, const char *title
)
2765 WGroupbox
*g
= g_new (WGroupbox
, 1);
2767 init_widget (&g
->widget
, y
, x
, height
, width
, groupbox_callback
, NULL
);
2769 g
->widget
.options
&= ~W_WANT_CURSOR
;
2770 widget_want_hotkey (g
->widget
, 0);
2772 /* Strip existing spaces, add one space before and after the title */
2775 t
= g_strstrip (g_strdup (title
));
2776 g
->title
= g_strconcat (" ", t
, " ", (char *) NULL
);