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 */
49 #include "../src/mcconfig/mcconfig.h" /* for history loading and saving */
51 #include "../vfs/vfs.h"
58 #include "cmddef.h" /* CK_ cmd name const */
59 #include "keybind.h" /* global_keymap_t */
61 #include "panel.h" /* current_panel */
63 const global_keymap_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
, &b
->widget
, DLG_POST_KEY
, ' ', NULL
);
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
,
346 b
->text
.end
, (char *) NULL
);
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 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
, w
, DLG_ACTION
, 0, NULL
);
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
, w
, DLG_ACTION
, 0, NULL
);
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
, w
, DLG_POST_KEY
, ' ', NULL
);
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
, w
, DLG_ACTION
, 0, NULL
);
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
, w
, DLG_POST_KEY
, ' ', NULL
);
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);
585 save_text_to_clip_file (const char *text
)
590 fname
= g_build_filename (home_dir
, EDIT_CLIP_FILE
, NULL
);
591 file
= mc_open (fname
, O_CREAT
| O_WRONLY
| O_TRUNC
,
592 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
598 mc_write (file
, (char *) text
, strlen (text
));
604 load_text_from_clip_file (char **text
)
609 gboolean first
= TRUE
;
611 fname
= g_build_filename (home_dir
, EDIT_CLIP_FILE
, NULL
);
612 f
= fopen (fname
, "r");
620 while (fgets (buf
, sizeof (buf
), f
)) {
625 if (buf
[len
- 1] == '\n')
630 *text
= g_strdup (buf
);
632 /* remove \n on EOL */
635 tmp
= g_strconcat (*text
, " ", buf
, (char *) NULL
);
644 return (*text
!= NULL
);
648 panel_save_curent_file_to_clip_file (void)
652 if (current_panel
->marked
== 0)
653 res
= save_text_to_clip_file (selection (current_panel
)->fname
);
656 gboolean first
= TRUE
;
659 for (i
= 0; i
< current_panel
->count
; i
++)
660 if (current_panel
->dir
.list
[i
].f
.marked
!= 0) { /* Skip the unmarked ones */
662 flist
= g_strdup (current_panel
->dir
.list
[i
].fname
);
665 /* Add empty lines after the file */
668 tmp
= g_strconcat (flist
, "\n", current_panel
->dir
.list
[i
].fname
, (char *) NULL
);
675 res
= save_text_to_clip_file (flist
);
686 label_callback (Widget
*w
, widget_msg_t msg
, int parm
)
688 WLabel
*l
= (WLabel
*) w
;
689 Dlg_head
*h
= l
->widget
.parent
;
695 /* We don't want to get the focus */
697 return MSG_NOT_HANDLED
;
701 char *p
= l
->text
, *q
, c
= 0;
708 tty_setcolor (DEFAULT_COLOR
);
710 tty_setcolor (DLG_NORMALC (h
));
713 q
= strchr (p
, '\n');
719 widget_move (&l
->widget
, y
, 0);
720 tty_print_string (str_fit_to_term (p
, l
->widget
.cols
, J_LEFT
));
736 return default_proc (msg
, parm
);
741 label_set_text (WLabel
*label
, const char *text
)
743 int newcols
= label
->widget
.cols
;
746 if (label
->text
&& text
&& !strcmp (label
->text
, text
))
747 return; /* Flickering is not nice */
749 g_free (label
->text
);
752 label
->text
= g_strdup (text
);
753 if (label
->auto_adjust_cols
) {
754 str_msg_term_size (text
, &newlines
, &newcols
);
755 if (newcols
> label
->widget
.cols
)
756 label
->widget
.cols
= newcols
;
757 if (newlines
> label
->widget
.lines
)
758 label
->widget
.lines
= newlines
;
760 } else label
->text
= NULL
;
762 if (label
->widget
.parent
)
763 label_callback ((Widget
*) label
, WIDGET_DRAW
, 0);
765 if (newcols
< label
->widget
.cols
)
766 label
->widget
.cols
= newcols
;
770 label_new (int y
, int x
, const char *text
)
777 str_msg_term_size (text
, &lines
, &cols
);
779 l
= g_new (WLabel
, 1);
780 init_widget (&l
->widget
, y
, x
, lines
, cols
, label_callback
, NULL
);
781 l
->text
= (text
!= NULL
) ? g_strdup (text
) : NULL
;
782 l
->auto_adjust_cols
= 1;
784 widget_want_cursor (l
->widget
, 0);
789 /* Gauge widget (progress indicator) */
790 /* Currently width is hardcoded here for text mode */
794 gauge_callback (Widget
*w
, widget_msg_t msg
, int parm
)
796 WGauge
*g
= (WGauge
*) w
;
797 Dlg_head
*h
= g
->widget
.parent
;
799 if (msg
== WIDGET_INIT
)
802 /* We don't want to get the focus */
803 if (msg
== WIDGET_FOCUS
)
804 return MSG_NOT_HANDLED
;
806 if (msg
== WIDGET_DRAW
){
807 widget_move (&g
->widget
, 0, 0);
808 tty_setcolor (DLG_NORMALC (h
));
810 tty_printf ("%*s", gauge_len
, "");
812 int percentage
, columns
;
813 long total
= g
->max
, done
= g
->current
;
815 if (total
<= 0 || done
< 0) {
821 while (total
> 65535) {
825 percentage
= (200 * done
/ total
+ 1) / 2;
826 columns
= (2 * (gauge_len
- 7) * done
/ total
+ 1) / 2;
827 tty_print_char ('[');
828 tty_setcolor (GAUGE_COLOR
);
829 tty_printf ("%*s", (int) columns
, "");
830 tty_setcolor (DLG_NORMALC (h
));
831 tty_printf ("%*s] %3d%%", (int)(gauge_len
- 7 - columns
), "", (int) percentage
);
836 return default_proc (msg
, parm
);
840 gauge_set_value (WGauge
*g
, int max
, int current
)
842 if (g
->current
== current
&& g
->max
== max
)
843 return; /* Do not flicker */
845 max
= 1; /* I do not like division by zero :) */
847 g
->current
= current
;
849 gauge_callback ((Widget
*) g
, WIDGET_DRAW
, 0);
853 gauge_show (WGauge
*g
, int shown
)
855 if (g
->shown
== shown
)
858 gauge_callback ((Widget
*) g
, WIDGET_DRAW
, 0);
862 gauge_new (int y
, int x
, int shown
, int max
, int current
)
864 WGauge
*g
= g_new (WGauge
, 1);
866 init_widget (&g
->widget
, y
, x
, 1, gauge_len
, gauge_callback
, NULL
);
869 max
= 1; /* I do not like division by zero :) */
871 g
->current
= current
;
872 widget_want_cursor (g
->widget
, 0);
879 /* {{{ history button */
881 #define LARGE_HISTORY_BUTTON 1
883 #ifdef LARGE_HISTORY_BUTTON
884 # define HISTORY_BUTTON_WIDTH 3
886 # define HISTORY_BUTTON_WIDTH 1
889 #define should_show_history_button(in) \
890 (in->history && in->field_width > HISTORY_BUTTON_WIDTH * 2 + 1 && in->widget.parent)
892 static void draw_history_button (WInput
* in
)
895 c
= in
->history
->next
? (in
->history
->prev
? '|' : 'v') : '^';
896 widget_move (&in
->widget
, 0, in
->field_width
- HISTORY_BUTTON_WIDTH
);
897 #ifdef LARGE_HISTORY_BUTTON
900 h
= in
->widget
.parent
;
901 tty_setcolor (NORMAL_COLOR
);
902 tty_print_string ("[ ]");
903 /* Too distracting: tty_setcolor (MARKED_COLOR); */
904 widget_move (&in
->widget
, 0, in
->field_width
- HISTORY_BUTTON_WIDTH
+ 1);
908 tty_setcolor (MARKED_COLOR
);
913 /* }}} history button */
916 /* Input widgets now have a global kill ring */
917 /* Pointer to killed data */
918 static char *kill_buffer
= 0;
921 update_input (WInput
*in
, int clear_first
)
925 int buf_len
= str_length (in
->buffer
);
929 if (should_show_history_button (in
))
930 has_history
= HISTORY_BUTTON_WIDTH
;
932 if (in
->disable_update
)
935 pw
= str_term_width2 (in
->buffer
, in
->point
);
937 /* Make the point visible */
938 if ((pw
< in
->term_first_shown
) ||
939 (pw
>= in
->term_first_shown
+ in
->field_width
- has_history
)) {
941 in
->term_first_shown
= pw
- (in
->field_width
/ 3);
942 if (in
->term_first_shown
< 0)
943 in
->term_first_shown
= 0;
946 /* Adjust the mark */
947 if (in
->mark
> buf_len
)
951 draw_history_button (in
);
953 tty_setcolor (in
->color
);
955 widget_move (&in
->widget
, 0, 0);
957 if (!in
->is_password
) {
958 tty_print_string (str_term_substring (in
->buffer
, in
->term_first_shown
,
959 in
->field_width
- has_history
));
962 for (i
= -in
->term_first_shown
; i
< in
->field_width
- has_history
; i
++){
964 tty_print_char ((cp
[0] != '\0') ? '*' : ' ');
966 if (cp
[0] != '\0') str_cnext_char (&cp
);
975 winput_set_origin (WInput
*in
, int x
, int field_width
)
978 in
->field_width
= in
->widget
.cols
= field_width
;
979 update_input (in
, 0);
982 /* {{{ history saving and loading */
984 int num_history_items_recorded
= 60;
987 This loads and saves the history of an input line to and from the
988 widget. It is called with the widgets history name on creation of the
989 widget, and returns the GList list. It stores histories in the file
990 ~/.mc/history in using the profile code.
992 If def_text is passed as INPUT_LAST_TEXT (to the input_new()
993 function) then input_new assigns the default text to be the last text
994 entered, or "" if not found.
998 history_get (const char *input_name
)
1005 size_t keys_num
= 0;
1008 if (!num_history_items_recorded
) /* this is how to disable */
1010 if (!input_name
|| !*input_name
)
1013 profile
= g_build_filename (home_dir
, MC_USERCONF_DIR
, MC_HISTORY_FILE
, NULL
);
1014 cfg
= mc_config_init (profile
);
1016 /* get number of keys */
1017 keys
= mc_config_get_keys (cfg
, input_name
, &keys_num
);
1020 for (i
= 0; i
< keys_num
; i
++) {
1021 char key_name
[BUF_TINY
];
1022 g_snprintf (key_name
, sizeof (key_name
), "%lu", (unsigned long)i
);
1023 this_entry
= mc_config_get_string (cfg
, input_name
, key_name
, "");
1025 if (this_entry
!= NULL
)
1026 hist
= list_append_unique (hist
, this_entry
);
1029 mc_config_deinit (cfg
);
1032 /* return pointer to the last entry in the list */
1033 return g_list_last (hist
);
1037 history_put (const char *input_name
, GList
*h
)
1052 if (!num_history_items_recorded
) /* this is how to disable */
1055 profile
= g_build_filename (home_dir
, MC_USERCONF_DIR
, MC_HISTORY_FILE
, NULL
);
1057 if ((i
= open (profile
, O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
)) != -1)
1060 /* Make sure the history is only readable by the user */
1061 if (chmod (profile
, S_IRUSR
| S_IWUSR
) == -1 && errno
!= ENOENT
) {
1066 /* go to end of list */
1067 h
= g_list_last (h
);
1069 /* go back 60 places */
1070 for (i
= 0; i
< num_history_items_recorded
- 1 && h
->prev
; i
++)
1071 h
= g_list_previous (h
);
1073 cfg
= mc_config_init(profile
);
1076 mc_config_del_group(cfg
,input_name
);
1078 /* dump histories into profile */
1079 for (i
= 0; h
; h
= g_list_next (h
)) {
1080 char *text
= (char *) h
->data
;
1082 /* We shouldn't have null entries, but let's be sure */
1084 char key_name
[BUF_TINY
];
1085 g_snprintf (key_name
, sizeof (key_name
), "%d", i
++);
1086 mc_config_set_string(cfg
,input_name
, key_name
, text
);
1090 mc_config_save_file (cfg
, NULL
);
1091 mc_config_deinit(cfg
);
1095 /* }}} history saving and loading */
1098 /* {{{ history display */
1103 return _(" History ");
1107 listbox_fwd (WListbox
*l
)
1109 if (l
->current
!= l
->list
->prev
)
1110 listbox_select_entry (l
, l
->current
->next
);
1112 listbox_select_first (l
);
1122 dlg_hist_reposition (Dlg_head
*dlg_head
)
1124 dlg_hist_data
*data
;
1125 int x
= 0, y
, he
, wi
;
1128 if ((dlg_head
== NULL
)
1129 || (dlg_head
->data
== NULL
))
1130 return MSG_NOT_HANDLED
;
1132 data
= (dlg_hist_data
*) dlg_head
->data
;
1134 y
= data
->widget
->y
;
1135 he
= data
->count
+ 2;
1137 if (he
<= y
|| y
> (LINES
- 6)) {
1138 he
= min (he
, y
- 1);
1142 he
= min (he
, LINES
- y
);
1145 if (data
->widget
->x
> 2)
1146 x
= data
->widget
->x
- 2;
1148 wi
= data
->maxlen
+ 4;
1150 if ((wi
+ x
) > COLS
) {
1151 wi
= min (wi
, COLS
);
1155 dlg_set_position (dlg_head
, y
, x
, y
+ he
, x
+ wi
);
1161 dlg_hist_callback (Dlg_head
*h
, Widget
*sender
,
1162 dlg_msg_t msg
, int parm
, void *data
)
1166 return dlg_hist_reposition (h
);
1169 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
1174 show_hist (GList
*history
, Widget
*widget
)
1177 size_t maxlen
, i
, count
= 0;
1179 Dlg_head
*query_dlg
;
1180 WListbox
*query_list
;
1181 dlg_hist_data hist_data
;
1183 if (history
== NULL
)
1186 maxlen
= str_term_width1 (i18n_htitle ());
1188 z
= g_list_first (history
);
1191 i
= str_term_width1 ((char *) hi
->data
);
1192 maxlen
= max (maxlen
, i
);
1194 hi
= g_list_next (hi
);
1197 hist_data
.maxlen
= maxlen
;
1198 hist_data
.widget
= widget
;
1199 hist_data
.count
= count
;
1202 create_dlg (0, 0, 4, 4, dialog_colors
, dlg_hist_callback
,
1203 "[History-query]", i18n_htitle (), DLG_COMPACT
);
1204 query_dlg
->data
= &hist_data
;
1206 query_list
= listbox_new (1, 1, 2, 2, NULL
);
1208 /* this call makes list stick to all sides of dialog, effectively make
1209 it be resized with dialog */
1210 add_widget_autopos (query_dlg
, query_list
, WPOS_KEEP_ALL
);
1212 /* to avoid diplicating of (calculating sizes in two places)
1213 code, call dlg_hist_callback function here, to set dialog and
1215 The main idea - create 4x4 dialog and add 2x2 list in
1216 center of it, and let dialog function resize it to needed
1218 dlg_hist_callback (query_dlg
, NULL
, DLG_RESIZE
, 0, NULL
);
1220 if (query_dlg
->y
< widget
->y
) {
1224 listbox_add_item (query_list
, LISTBOX_APPEND_AT_END
,
1225 0, (char *) hi
->data
, NULL
);
1226 hi
= g_list_next (hi
);
1228 listbox_select_last (query_list
);
1230 /* traverse backwards */
1231 hi
= g_list_last (history
);
1233 listbox_add_item (query_list
, LISTBOX_APPEND_AT_END
,
1234 0, (char *) hi
->data
, NULL
);
1235 hi
= g_list_previous (hi
);
1239 if (run_dlg (query_dlg
) != B_CANCEL
) {
1240 listbox_get_current (query_list
, &q
, NULL
);
1244 destroy_dlg (query_dlg
);
1249 do_show_hist (WInput
*in
)
1252 r
= show_hist (in
->history
, &in
->widget
);
1254 assign_text (in
, r
);
1259 /* }}} history display */
1262 input_destroy (WInput
*in
)
1265 fprintf (stderr
, "Internal error: null Input *\n");
1272 if (!in
->is_password
) /* don't save passwords ;-) */
1273 history_put (in
->history_name
, in
->history
);
1275 in
->history
= g_list_first (in
->history
);
1276 g_list_foreach (in
->history
, (GFunc
) g_free
, NULL
);
1277 g_list_free (in
->history
);
1280 g_free (in
->buffer
);
1281 free_completions (in
);
1282 g_free (in
->history_name
);
1286 input_disable_update (WInput
*in
)
1288 in
->disable_update
++;
1292 input_enable_update (WInput
*in
)
1294 in
->disable_update
--;
1295 update_input (in
, 0);
1300 push_history (WInput
*in
, const char *text
)
1302 /* input widget where urls with passwords are entered without any
1304 const char *password_input_fields
[] = {
1305 N_(" Link to a remote machine "),
1306 N_(" FTP to machine "),
1307 N_(" SMB link to machine ")
1309 const size_t ELEMENTS
= (sizeof (password_input_fields
) /
1310 sizeof (password_input_fields
[0]));
1320 for (i
= 0; i
< ELEMENTS
; i
++)
1321 password_input_fields
[i
] = _(password_input_fields
[i
]);
1324 t
= g_strstrip (g_strdup (text
));
1327 t
= g_strdup (empty
? "" : text
);
1329 if (in
->history_name
!= NULL
) {
1330 const char *p
= in
->history_name
+ 3;
1332 for (i
= 0; i
< ELEMENTS
; i
++)
1333 if (strcmp (p
, password_input_fields
[i
]) == 0)
1336 strip_password (t
, i
>= ELEMENTS
);
1339 in
->history
= list_append_unique (in
->history
, t
);
1343 /* Cleans the input line and adds the current text to the history */
1345 new_input (WInput
*in
)
1347 push_history (in
, in
->buffer
);
1349 in
->buffer
[0] = '\0';
1353 free_completions (in
);
1354 update_input (in
, 0);
1358 move_buffer_backward (WInput
*in
, int start
, int end
)
1361 int str_len
= str_length (in
->buffer
);
1362 if (start
>= str_len
|| end
> str_len
+ 1) return;
1364 pos
= str_offset_to_pos (in
->buffer
, start
);
1365 len
= str_offset_to_pos (in
->buffer
, end
) - pos
;
1367 for (i
= pos
; in
->buffer
[i
+ len
- 1]; i
++)
1368 in
->buffer
[i
] = in
->buffer
[i
+ len
];
1372 insert_char (WInput
*in
, int c_code
)
1378 return MSG_NOT_HANDLED
;
1380 if (in
->charpoint
>= MB_LEN_MAX
)
1383 in
->charbuf
[in
->charpoint
] = c_code
;
1386 res
= str_is_valid_char (in
->charbuf
, in
->charpoint
);
1389 in
->charpoint
= 0; /* broken multibyte char, skip */
1394 if (strlen (in
->buffer
) + 1 + in
->charpoint
>= in
->current_max_size
){
1395 /* Expand the buffer */
1396 size_t new_length
= in
->current_max_size
+
1397 in
->field_width
+ in
->charpoint
;
1398 char *narea
= g_try_renew (char, in
->buffer
, new_length
);
1401 in
->current_max_size
= new_length
;
1405 if (strlen (in
->buffer
) + in
->charpoint
< in
->current_max_size
) {
1406 /* bytes from begin */
1407 size_t ins_point
= str_offset_to_pos (in
->buffer
, in
->point
);
1409 size_t rest_bytes
= strlen (in
->buffer
+ ins_point
);
1411 for (i
= rest_bytes
+ 1; i
> 0; i
--)
1412 in
->buffer
[ins_point
+ i
+ in
->charpoint
- 1] =
1413 in
->buffer
[ins_point
+ i
- 1];
1415 memcpy(in
->buffer
+ ins_point
, in
->charbuf
, in
->charpoint
);
1424 beginning_of_line (WInput
*in
)
1431 end_of_line (WInput
*in
)
1433 in
->point
= str_length (in
->buffer
);
1438 backward_char (WInput
*in
)
1440 const char *act
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
1442 if (in
->point
> 0) {
1443 in
->point
-= str_cprev_noncomb_char (&act
, in
->buffer
);
1449 forward_char (WInput
*in
)
1451 const char *act
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
1452 if (act
[0] != '\0') {
1453 in
->point
+= str_cnext_noncomb_char (&act
);
1459 forward_word (WInput
* in
)
1461 const char *p
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
1463 while (p
[0] != '\0' && (str_isspace (p
) || str_ispunct (p
))) {
1464 str_cnext_char (&p
);
1467 while (p
[0] != '\0' && !str_isspace (p
) && !str_ispunct (p
)) {
1468 str_cnext_char (&p
);
1474 backward_word (WInput
*in
)
1480 p
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
1481 (p
!= in
->buffer
) && (p
[0] == '\0');
1482 str_cprev_char (&p
), in
->point
--
1485 while (p
!= in
->buffer
) {
1487 str_cprev_char (&p
);
1488 if (!str_isspace (p
) && !str_ispunct (p
)) {
1494 while (p
!= in
->buffer
) {
1495 str_cprev_char (&p
);
1496 if (str_isspace (p
) || str_ispunct (p
))
1504 key_left (WInput
*in
)
1510 key_ctrl_left (WInput
*in
)
1516 key_right (WInput
*in
)
1522 key_ctrl_right (WInput
*in
)
1527 backward_delete (WInput
*in
)
1529 const char *act
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
1535 start
= in
->point
- str_cprev_noncomb_char (&act
, in
->buffer
);
1536 move_buffer_backward(in
, start
, in
->point
);
1543 delete_char (WInput
*in
)
1545 const char *act
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
1546 int end
= in
->point
;
1548 end
+= str_cnext_noncomb_char (&act
);
1550 move_buffer_backward(in
, in
->point
, end
);
1556 copy_region (WInput
*in
, int x_first
, int x_last
)
1558 int first
= min (x_first
, x_last
);
1559 int last
= max (x_first
, x_last
);
1561 if (last
== first
) {
1562 /* Copy selected files to clipboard */
1563 panel_save_curent_file_to_clip_file ();
1567 g_free (kill_buffer
);
1569 first
= str_offset_to_pos (in
->buffer
, first
);
1570 last
= str_offset_to_pos (in
->buffer
, last
);
1572 kill_buffer
= g_strndup(in
->buffer
+ first
, last
- first
);
1573 save_text_to_clip_file (kill_buffer
);
1577 delete_region (WInput
*in
, int x_first
, int x_last
)
1579 int first
= min (x_first
, x_last
);
1580 int last
= max (x_first
, x_last
);
1584 if (in
->mark
> first
)
1586 last
= str_offset_to_pos (in
->buffer
, last
);
1587 first
= str_offset_to_pos (in
->buffer
, first
);
1588 len
= strlen (&in
->buffer
[last
]) + 1;
1589 memmove (&in
->buffer
[first
], &in
->buffer
[last
], len
);
1595 kill_word (WInput
*in
)
1597 int old_point
= in
->point
;
1601 new_point
= in
->point
;
1602 in
->point
= old_point
;
1604 copy_region (in
, old_point
, new_point
);
1605 delete_region (in
, old_point
, new_point
);
1612 back_kill_word (WInput
*in
)
1614 int old_point
= in
->point
;
1618 new_point
= in
->point
;
1619 in
->point
= old_point
;
1621 copy_region (in
, old_point
, new_point
);
1622 delete_region (in
, old_point
, new_point
);
1627 set_mark (WInput
*in
)
1629 in
->mark
= in
->point
;
1633 kill_save (WInput
*in
)
1635 copy_region (in
, in
->mark
, in
->point
);
1639 kill_region (WInput
*in
)
1642 delete_region (in
, in
->point
, in
->mark
);
1646 clear_region (WInput
*in
)
1648 delete_region (in
, in
->point
, in
->mark
);
1659 for (p
= kill_buffer
; *p
; p
++)
1660 insert_char (in
, *p
);
1665 kill_line (WInput
*in
)
1667 int chp
= str_offset_to_pos (in
->buffer
, in
->point
);
1668 g_free (kill_buffer
);
1669 kill_buffer
= g_strdup (&in
->buffer
[chp
]);
1670 in
->buffer
[chp
] = '\0';
1675 ins_from_clip (WInput
*in
)
1679 if (load_text_from_clip_file (&p
)) {
1682 for (pp
= p
; *pp
!= '\0'; pp
++)
1683 insert_char (in
, *pp
);
1690 assign_text (WInput
*in
, const char *text
)
1692 free_completions (in
);
1693 g_free (in
->buffer
);
1694 in
->buffer
= g_strdup (text
); /* was in->buffer->text */
1695 in
->current_max_size
= strlen (in
->buffer
) + 1;
1696 in
->point
= str_length (in
->buffer
);
1703 hist_prev (WInput
*in
)
1708 if (in
->need_push
) {
1709 push_history (in
, in
->buffer
);
1710 in
->history
= g_list_previous (in
->history
);
1711 } else if (in
->history
->prev
)
1712 in
->history
= g_list_previous (in
->history
);
1715 assign_text (in
, (char *) in
->history
->data
);
1720 hist_next (WInput
*in
)
1722 if (in
->need_push
) {
1723 push_history (in
, in
->buffer
);
1724 assign_text (in
, "");
1731 if (!in
->history
->next
) {
1732 assign_text (in
, "");
1736 in
->history
= g_list_next (in
->history
);
1737 assign_text (in
, (char *) in
->history
->data
);
1742 port_region_marked_for_delete (WInput
*in
)
1744 in
->buffer
[0] = '\0';
1751 input_execute_cmd (WInput
*in
, unsigned long command
)
1753 cb_ret_t res
= MSG_HANDLED
;
1757 beginning_of_line (in
);
1762 case CK_InputMoveLeft
:
1765 case CK_InputWordLeft
:
1768 case CK_InputMoveRight
:
1771 case CK_InputWordRight
:
1772 key_ctrl_right (in
);
1774 case CK_InputBackwardChar
:
1777 case CK_InputBackwardWord
:
1780 case CK_InputForwardChar
:
1783 case CK_InputForwardWord
:
1786 case CK_InputBackwardDelete
:
1787 backward_delete (in
);
1789 case CK_InputDeleteChar
:
1792 case CK_InputKillWord
:
1795 case CK_InputBackwardKillWord
:
1796 back_kill_word (in
);
1798 case CK_InputSetMark
:
1801 case CK_InputKillRegion
:
1804 case CK_InputClearLine
:
1807 case CK_InputKillSave
:
1816 case CK_InputKillLine
:
1819 case CK_InputHistoryPrev
:
1822 case CK_InputHistoryNext
:
1825 case CK_InputHistoryShow
:
1828 case CK_InputComplete
:
1832 res
= MSG_NOT_HANDLED
;
1838 /* This function is a test for a special input key used in complete.c */
1839 /* Returns 0 if it is not a special key, 1 if it is a non-complete key
1840 and 2 if it is a complete key */
1842 is_in_input_map (WInput
*in
, int key
)
1845 for (i
= 0; input_map
[i
].key
!= 0; i
++)
1846 if (key
== input_map
[i
].key
) {
1847 input_execute_cmd (in
, input_map
[i
].command
);
1848 return (input_map
[i
].command
== CK_InputComplete
) ? 2 : 1;
1854 handle_char (WInput
*in
, int key
)
1859 v
= MSG_NOT_HANDLED
;
1862 free_completions (in
);
1863 v
= insert_char (in
, key
);
1864 update_input (in
, 1);
1868 for (i
= 0; input_map
[i
].key
; i
++) {
1869 if (key
== input_map
[i
].key
) {
1870 if (input_map
[i
].command
!= CK_InputComplete
)
1871 free_completions (in
);
1872 input_execute_cmd (in
, input_map
[i
].command
);
1873 update_input (in
, 1);
1878 if (input_map
[i
].command
== 0) {
1880 return MSG_NOT_HANDLED
;
1882 port_region_marked_for_delete (in
);
1883 free_completions (in
);
1884 v
= insert_char (in
, key
);
1886 update_input (in
, 1);
1890 /* Inserts text in input line */
1892 stuff (WInput
*in
, const char *text
, int insert_extra_space
)
1894 input_disable_update (in
);
1895 while (*text
!= '\0')
1896 handle_char (in
, (unsigned char) *text
++); /* unsigned extension char->int */
1897 if (insert_extra_space
)
1898 handle_char (in
, ' ');
1899 input_enable_update (in
);
1900 update_input (in
, 1);
1904 input_set_point (WInput
*in
, int pos
)
1906 int max_pos
= str_length (in
->buffer
);
1910 if (pos
!= in
->point
)
1911 free_completions (in
);
1914 update_input (in
, 1);
1918 input_callback (Widget
*w
, widget_msg_t msg
, int parm
)
1920 WInput
*in
= (WInput
*) w
;
1925 if (parm
== XCTRL ('q')) {
1927 v
= handle_char (in
, ascii_alpha_to_cntrl (tty_getch ()));
1932 /* Keys we want others to handle */
1933 if (parm
== KEY_UP
|| parm
== KEY_DOWN
|| parm
== ESC_CHAR
1934 || parm
== KEY_F (10) || parm
== '\n')
1935 return MSG_NOT_HANDLED
;
1937 /* When pasting multiline text, insert literal Enter */
1938 if ((parm
& ~KEY_M_MASK
) == '\n') {
1940 v
= handle_char (in
, '\n');
1945 return handle_char (in
, parm
);
1947 case WIDGET_COMMAND
:
1948 return input_execute_cmd (in
, parm
);
1951 case WIDGET_UNFOCUS
:
1953 update_input (in
, 0);
1957 widget_move (&in
->widget
, 0, str_term_width2 (in
->buffer
, in
->point
)
1958 - in
->term_first_shown
);
1961 case WIDGET_DESTROY
:
1966 return default_proc (msg
, parm
);
1971 input_event (Gpm_Event
* event
, void *data
)
1975 if (event
->type
& (GPM_DOWN
| GPM_DRAG
)) {
1976 dlg_select_widget (in
);
1978 if (event
->x
>= in
->field_width
- HISTORY_BUTTON_WIDTH
+ 1
1979 && should_show_history_button (in
)) {
1982 in
->point
= str_length (in
->buffer
);
1983 if (event
->x
+ in
->term_first_shown
- 1 <
1984 str_term_width1 (in
->buffer
))
1986 in
->point
= str_column_to_pos (in
->buffer
, event
->x
1987 + in
->term_first_shown
- 1);
1990 update_input (in
, 1);
1996 input_new (int y
, int x
, int color
, int width
, const char *def_text
,
1997 const char *histname
, INPUT_COMPLETE_FLAGS completion_flags
)
1999 WInput
*in
= g_new (WInput
, 1);
2000 int initial_buffer_len
;
2002 init_widget (&in
->widget
, y
, x
, 1, width
, input_callback
, input_event
);
2006 in
->history_name
= 0;
2009 in
->history_name
= g_strdup (histname
);
2010 in
->history
= history_get (histname
);
2014 if (def_text
== NULL
)
2017 if (def_text
== INPUT_LAST_TEXT
) {
2020 if (in
->history
->data
)
2021 def_text
= (char *) in
->history
->data
;
2023 initial_buffer_len
= 1 + max ((size_t) width
, strlen (def_text
));
2024 in
->widget
.options
|= W_IS_INPUT
;
2025 in
->completions
= NULL
;
2026 in
->completion_flags
= completion_flags
;
2027 in
->current_max_size
= initial_buffer_len
;
2028 in
->buffer
= g_new (char, initial_buffer_len
);
2030 in
->field_width
= width
;
2032 in
->term_first_shown
= 0;
2033 in
->disable_update
= 0;
2036 in
->is_password
= 0;
2038 strcpy (in
->buffer
, def_text
);
2039 in
->point
= str_length (in
->buffer
);
2044 /* Listbox widget */
2046 /* Should draw the scrollbar, but currently draws only
2047 * indications that there is more information
2049 static int listbox_cdiff (WLEntry
*s
, WLEntry
*e
);
2052 listbox_drawscroll (WListbox
*l
)
2056 int max_line
= l
->widget
.lines
- 1;
2058 /* Are we at the top? */
2059 widget_move (&l
->widget
, 0, l
->widget
.cols
);
2060 if (l
->list
== l
->top
)
2061 tty_print_one_vline ();
2063 tty_print_char ('^');
2065 /* Are we at the bottom? */
2066 widget_move (&l
->widget
, max_line
, l
->widget
.cols
);
2067 top
= listbox_cdiff (l
->list
, l
->top
);
2068 if ((top
+ l
->widget
.lines
== l
->count
) || l
->widget
.lines
>= l
->count
)
2069 tty_print_one_vline ();
2071 tty_print_char ('v');
2073 /* Now draw the nice relative pointer */
2075 line
= 1+ ((l
->pos
* (l
->widget
.lines
- 2)) / l
->count
);
2077 for (i
= 1; i
< max_line
; i
++){
2078 widget_move (&l
->widget
, i
, l
->widget
.cols
);
2080 tty_print_one_vline ();
2082 tty_print_char ('*');
2087 listbox_draw (WListbox
*l
, gboolean focused
)
2089 const Dlg_head
*h
= l
->widget
.parent
;
2090 const int normalc
= DLG_NORMALC (h
);
2091 int selc
= focused
? DLG_HOT_FOCUSC (h
) : DLG_FOCUSC (h
);
2098 for (e
= l
->top
, i
= 0; i
< l
->widget
.lines
; i
++) {
2099 /* Display the entry */
2100 if (e
== l
->current
&& sel_line
== -1) {
2102 tty_setcolor (selc
);
2104 tty_setcolor (normalc
);
2106 widget_move (&l
->widget
, i
, 1);
2108 if ((i
> 0 && e
== l
->list
) || !l
->list
)
2114 tty_print_string (str_fit_to_term (text
, l
->widget
.cols
- 2, J_LEFT_FIT
));
2116 l
->cursor_y
= sel_line
;
2118 if (l
->scrollbar
&& l
->count
> l
->widget
.lines
) {
2119 tty_setcolor (normalc
);
2120 listbox_drawscroll (l
);
2124 /* Returns the number of items between s and e,
2125 must be on the same linked list */
2127 listbox_cdiff (WLEntry
*s
, WLEntry
*e
)
2131 for (count
= 0; s
!= e
; count
++)
2137 listbox_check_hotkey (WListbox
*l
, int key
)
2149 /* If we didn't find anything, return */
2150 if (i
&& e
== l
->list
)
2153 if (e
->hotkey
== key
)
2161 /* Selects the last entry and scrolls the list to the bottom */
2163 listbox_select_last (WListbox
*l
)
2166 l
->current
= l
->top
= l
->list
->prev
;
2167 for (i
= min (l
->widget
.lines
, l
->count
) - 1; i
; i
--)
2168 l
->top
= l
->top
->prev
;
2169 l
->pos
= l
->count
- 1;
2172 /* Selects the first entry and scrolls the list to the top */
2174 listbox_select_first (WListbox
*l
)
2176 l
->current
= l
->top
= l
->list
;
2181 listbox_remove_list (WListbox
*l
)
2190 while (l
->count
--) {
2196 l
->pos
= l
->count
= 0;
2197 l
->list
= l
->top
= l
->current
= 0;
2201 * bor 30.10.96: added force flag to remove *last* entry as well
2202 * bor 30.10.96: corrected selection bug if last entry was removed
2206 listbox_remove_current (WListbox
*l
, int force
)
2210 /* Ok, note: this won't allow for emtpy lists */
2211 if (!force
&& (!l
->count
|| l
->count
== 1))
2218 l
->current
->next
->prev
= l
->current
->prev
;
2219 l
->current
->prev
->next
= l
->current
->next
;
2220 if (p
->next
== l
->list
) {
2221 l
->current
= p
->prev
;
2225 l
->current
= p
->next
;
2228 l
->list
= l
->top
= p
->next
;
2231 l
->list
= l
->top
= l
->current
= 0;
2238 /* Makes *e the selected entry (sets current and pos) */
2240 listbox_select_entry (WListbox
*l
, WLEntry
*dest
)
2249 for (pos
= 0, e
= l
->list
; pos
< l
->count
; e
= e
->next
, pos
++){
2257 while (listbox_cdiff (l
->top
, l
->current
) >= l
->widget
.lines
)
2258 l
->top
= l
->top
->next
;
2260 l
->top
= l
->current
;
2266 /* If we are unable to find it, set decent values */
2267 l
->current
= l
->top
= l
->list
;
2271 /* Selects from base the pos element */
2273 listbox_select_pos (WListbox
*l
, WLEntry
*base
, int pos
)
2275 WLEntry
*last
= l
->list
->prev
;
2288 listbox_back (WListbox
*l
)
2291 listbox_select_entry (l
, l
->current
->prev
);
2293 listbox_select_last (l
);
2296 /* Return MSG_HANDLED if we want a redraw */
2298 listbox_key (WListbox
*l
, int key
)
2302 cb_ret_t j
= MSG_NOT_HANDLED
;
2304 /* focus on listbox item N by '0'..'9' keys */
2305 if (key
>= '0' && key
<= '9') {
2306 int oldpos
= l
->pos
;
2307 listbox_select_by_number(l
, key
- '0');
2309 /* need scroll to item? */
2310 if (abs(oldpos
- l
->pos
) > l
->widget
.lines
)
2311 l
->top
= l
->current
;
2317 return MSG_NOT_HANDLED
;
2323 listbox_select_first (l
);
2329 listbox_select_last (l
);
2344 for (i
= 0; ((i
< l
->widget
.lines
- 1)
2345 && (l
->current
!= l
->list
->prev
)); i
++) {
2353 for (i
= 0; ((i
< l
->widget
.lines
- 1)
2354 && (l
->current
!= l
->list
)); i
++) {
2360 return MSG_NOT_HANDLED
;
2364 listbox_destroy (WListbox
*l
)
2366 WLEntry
*n
, *p
= l
->list
;
2369 for (i
= 0; i
< l
->count
; i
++){
2378 listbox_callback (Widget
*w
, widget_msg_t msg
, int parm
)
2380 WListbox
*l
= (WListbox
*) w
;
2381 Dlg_head
*h
= l
->widget
.parent
;
2390 e
= listbox_check_hotkey (l
, parm
);
2394 listbox_select_entry (l
, e
);
2395 h
->callback (h
, w
, DLG_ACTION
, l
->pos
, NULL
);
2398 action
= (*l
->cback
) (l
);
2400 action
= LISTBOX_DONE
;
2402 if (action
== LISTBOX_DONE
) {
2403 h
->ret_value
= B_ENTER
;
2408 return MSG_NOT_HANDLED
;
2411 ret_code
= listbox_key (l
, parm
);
2412 if (ret_code
!= MSG_NOT_HANDLED
) {
2413 listbox_draw (l
, TRUE
);
2414 h
->callback (h
, w
, DLG_ACTION
, l
->pos
, NULL
);
2419 widget_move (&l
->widget
, l
->cursor_y
, 0);
2420 h
->callback (h
, w
, DLG_ACTION
, l
->pos
, NULL
);
2424 case WIDGET_UNFOCUS
:
2426 listbox_draw (l
, msg
!= WIDGET_UNFOCUS
);
2429 case WIDGET_DESTROY
:
2430 listbox_destroy (l
);
2433 case WIDGET_RESIZED
:
2437 return default_proc (msg
, parm
);
2442 listbox_event (Gpm_Event
*event
, void *data
)
2447 Dlg_head
*h
= l
->widget
.parent
;
2450 if (event
->type
& GPM_DOWN
)
2451 dlg_select_widget (l
);
2453 if (l
->list
== NULL
)
2456 if (event
->type
& (GPM_DOWN
| GPM_DRAG
)) {
2457 int ret
= MOU_REPEAT
;
2459 if (event
->x
< 0 || event
->x
> l
->widget
.cols
)
2463 for (i
= -event
->y
; i
>= 0; i
--)
2465 else if (event
->y
> l
->widget
.lines
)
2466 for (i
= event
->y
- l
->widget
.lines
; i
> 0; i
--)
2468 else if (event
->buttons
& GPM_B_UP
) {
2471 } else if (event
->buttons
& GPM_B_DOWN
) {
2475 listbox_select_entry (l
,
2476 listbox_select_pos (l
, l
->top
,
2479 /* We need to refresh ourselves since the dialog manager doesn't */
2480 /* know about this event */
2481 listbox_draw (l
, TRUE
);
2486 if ((event
->type
& (GPM_DOUBLE
| GPM_UP
)) == (GPM_UP
| GPM_DOUBLE
)) {
2489 if (event
->x
< 0 || event
->x
>= l
->widget
.cols
2490 || event
->y
< 1 || event
->y
> l
->widget
.lines
)
2493 dlg_select_widget (l
);
2494 listbox_select_entry (l
,
2495 listbox_select_pos (l
, l
->top
,
2499 action
= (*l
->cback
) (l
);
2501 action
= LISTBOX_DONE
;
2503 if (action
== LISTBOX_DONE
) {
2504 h
->ret_value
= B_ENTER
;
2513 listbox_new (int y
, int x
, int height
, int width
, lcback callback
)
2515 WListbox
*l
= g_new (WListbox
, 1);
2520 init_widget (&l
->widget
, y
, x
, height
, width
,
2521 listbox_callback
, listbox_event
);
2523 l
->list
= l
->top
= l
->current
= 0;
2526 l
->cback
= callback
;
2527 l
->allow_duplicates
= 1;
2528 l
->scrollbar
= !tty_is_slow ();
2529 widget_want_hotkey (l
->widget
, 1);
2534 /* Listbox item adding function. They still lack a lot of functionality */
2536 /* 1.11.96 bor: added pos argument to control placement of new entry */
2538 listbox_append_item (WListbox
*l
, WLEntry
*e
, enum append_pos pos
)
2546 } else if (pos
== LISTBOX_APPEND_AT_END
) {
2548 e
->prev
= l
->list
->prev
;
2549 l
->list
->prev
->next
= e
;
2551 } else if (pos
== LISTBOX_APPEND_BEFORE
){
2552 e
->next
= l
->current
;
2553 e
->prev
= l
->current
->prev
;
2554 l
->current
->prev
->next
= e
;
2555 l
->current
->prev
= e
;
2556 if (l
->list
== l
->current
) { /* move list one position down */
2560 } else if (pos
== LISTBOX_APPEND_AFTER
) {
2561 e
->prev
= l
->current
;
2562 e
->next
= l
->current
->next
;
2563 l
->current
->next
->prev
= e
;
2564 l
->current
->next
= e
;
2565 } else if (pos
== LISTBOX_APPEND_SORTED
) {
2566 WLEntry
*w
= l
->list
;
2568 while (w
->next
!= l
->list
&& strcmp (e
->text
, w
->text
) > 0)
2570 if (w
->next
== l
->list
) {
2586 listbox_add_item (WListbox
*l
, enum append_pos pos
, int hotkey
,
2587 const char *text
, void *data
)
2594 if (!l
->allow_duplicates
)
2595 if (listbox_search_text (l
, text
))
2598 entry
= g_new (WLEntry
, 1);
2599 entry
->text
= g_strdup (text
);
2601 entry
->hotkey
= hotkey
;
2603 listbox_append_item (l
, entry
, pos
);
2608 /* Selects the nth entry in the listbox */
2610 listbox_select_by_number (WListbox
*l
, int n
)
2612 if (l
->list
!= NULL
)
2613 listbox_select_entry (l
, listbox_select_pos (l
, l
->list
, n
));
2617 listbox_search_text (WListbox
*l
, const char *text
)
2626 if(!strcmp (e
->text
, text
))
2629 } while (e
!=l
->list
);
2634 /* Returns the current string text as well as the associated extra data */
2636 listbox_get_current (WListbox
*l
, char **string
, char **extra
)
2642 if (string
&& l
->current
)
2643 *string
= l
->current
->text
;
2644 if (extra
&& l
->current
)
2645 *extra
= l
->current
->data
;
2648 /* returns TRUE if a function has been called, FALSE otherwise. */
2650 buttonbar_call (WButtonBar
*bb
, int i
)
2652 cb_ret_t ret
= MSG_NOT_HANDLED
;
2655 ret
= bb
->widget
.parent
->callback (bb
->widget
.parent
,
2656 (Widget
*) bb
, DLG_ACTION
,
2657 bb
->labels
[i
].command
,
2658 bb
->labels
[i
].receiver
);
2662 /* calculate width of one button, width is never lesser than 7 */
2664 buttonbat_get_button_width (void)
2666 int result
= COLS
/ BUTTONBAR_LABELS_NUM
;
2667 return (result
>= 7) ? result
: 7;
2671 buttonbar_callback (Widget
*w
, widget_msg_t msg
, int parm
)
2673 WButtonBar
*bb
= (WButtonBar
*) w
;
2679 return MSG_NOT_HANDLED
;
2682 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++)
2683 if (parm
== KEY_F (i
+ 1) && buttonbar_call (bb
, i
))
2685 return MSG_NOT_HANDLED
;
2690 int count_free_positions
;
2692 widget_move (&bb
->widget
, 0, 0);
2693 tty_setcolor (DEFAULT_COLOR
);
2694 bb
->btn_width
= buttonbat_get_button_width ();
2695 tty_printf ("%-*s", bb
->widget
.cols
, "");
2696 count_free_positions
= COLS
- bb
->btn_width
* BUTTONBAR_LABELS_NUM
;
2698 for (i
= 0; i
< COLS
/ bb
->btn_width
&& i
< BUTTONBAR_LABELS_NUM
; i
++) {
2699 widget_move (&bb
->widget
, 0, (i
* bb
->btn_width
) + offset
);
2700 tty_setcolor (BUTTONBAR_HOTKEY_COLOR
);
2701 tty_printf ("%2d", i
+ 1);
2702 tty_setcolor (BUTTONBAR_BUTTON_COLOR
);
2703 text
= (bb
->labels
[i
].text
!= NULL
) ? bb
->labels
[i
].text
: "";
2704 tty_print_string (str_fit_to_term (
2706 bb
->btn_width
- 2 + (int)(offset
< count_free_positions
),
2709 if (count_free_positions
!= 0 && offset
< count_free_positions
)
2715 case WIDGET_DESTROY
:
2716 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++)
2717 g_free (bb
->labels
[i
].text
);
2721 return default_proc (msg
, parm
);
2726 buttonbar_event (Gpm_Event
*event
, void *data
)
2728 WButtonBar
*bb
= data
;
2731 if (!(event
->type
& GPM_UP
))
2735 button
= (event
->x
- 1) * BUTTONBAR_LABELS_NUM
/ COLS
;
2736 if (button
< BUTTONBAR_LABELS_NUM
)
2737 buttonbar_call (bb
, button
);
2742 buttonbar_new (gboolean visible
)
2746 bb
= g_new0 (WButtonBar
, 1);
2748 init_widget (&bb
->widget
, LINES
- 1, 0, 1, COLS
,
2749 buttonbar_callback
, buttonbar_event
);
2750 bb
->widget
.pos_flags
= WPOS_KEEP_HORZ
| WPOS_KEEP_BOTTOM
;
2751 bb
->visible
= visible
;
2752 widget_want_hotkey (bb
->widget
, 1);
2753 widget_want_cursor (bb
->widget
, 0);
2754 bb
->btn_width
= buttonbat_get_button_width ();
2760 set_label_text (WButtonBar
*bb
, int lc_index
, const char *text
)
2762 g_free (bb
->labels
[lc_index
- 1].text
);
2763 bb
->labels
[lc_index
- 1].text
= g_strdup (text
);
2766 /* Find ButtonBar widget in the dialog */
2768 find_buttonbar (const Dlg_head
*h
)
2770 return (WButtonBar
*) find_widget_type (h
, buttonbar_callback
);
2774 buttonbar_set_label (WButtonBar
*bb
, int idx
, const char *text
,
2775 const struct global_keymap_t
*keymap
, const Widget
*receiver
)
2777 if ((bb
!= NULL
) && (idx
>= 1) && (idx
<= BUTTONBAR_LABELS_NUM
)) {
2778 unsigned long command
= CK_Ignore_Key
;
2781 command
= lookup_keymap_command (keymap
, KEY_F (idx
));
2783 if ((text
== NULL
) || (text
[0] == '\0'))
2784 set_label_text (bb
, idx
, "");
2786 set_label_text (bb
, idx
, text
);
2788 bb
->labels
[idx
- 1].command
= command
;
2789 bb
->labels
[idx
- 1].receiver
= (Widget
*) receiver
;
2794 buttonbar_set_visible (WButtonBar
*bb
, gboolean visible
)
2796 bb
->visible
= visible
;
2800 buttonbar_redraw (WButtonBar
*bb
)
2803 send_message ((Widget
*) bb
, WIDGET_DRAW
, 0);
2807 groupbox_callback (Widget
*w
, widget_msg_t msg
, int parm
)
2809 WGroupbox
*g
= (WGroupbox
*) w
;
2816 return MSG_NOT_HANDLED
;
2819 tty_setcolor (COLOR_NORMAL
);
2820 draw_box (g
->widget
.parent
, g
->widget
.y
- g
->widget
.parent
->y
,
2821 g
->widget
.x
- g
->widget
.parent
->x
, g
->widget
.lines
,
2824 tty_setcolor (COLOR_HOT_NORMAL
);
2825 dlg_move (g
->widget
.parent
, g
->widget
.y
- g
->widget
.parent
->y
,
2826 g
->widget
.x
- g
->widget
.parent
->x
+ 1);
2827 tty_print_string (g
->title
);
2830 case WIDGET_DESTROY
:
2835 return default_proc (msg
, parm
);
2840 groupbox_new (int y
, int x
, int height
, int width
, const char *title
)
2842 WGroupbox
*g
= g_new (WGroupbox
, 1);
2844 init_widget (&g
->widget
, y
, x
, height
, width
, groupbox_callback
, NULL
);
2846 g
->widget
.options
&= ~W_WANT_CURSOR
;
2847 widget_want_hotkey (g
->widget
, 0);
2849 /* Strip existing spaces, add one space before and after the title */
2852 t
= g_strstrip (g_strdup (title
));
2853 g
->title
= g_strconcat (" ", t
, " ", (char *) NULL
);