2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2016
5 Free Software Foundation, Inc.
8 Radek Doulik, 1994, 1995
9 Miguel de Icaza, 1994, 1995
11 Andrej Borsenkow, 1996
13 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013, 2016
15 This file is part of the Midnight Commander.
17 The Midnight Commander is free software: you can redistribute it
18 and/or modify it under the terms of the GNU General Public License as
19 published by the Free Software Foundation, either version 3 of the License,
20 or (at your option) any later version.
22 The Midnight Commander is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 * \brief Source: WButtonBar widget
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/key.h" /* XCTRL and ALT macros */
45 #include "lib/strutil.h"
47 #include "lib/keybind.h" /* global_keymap_t */
48 #include "lib/widget.h"
50 /*** global variables ****************************************************************************/
52 /*** file scope macro definitions ****************************************************************/
54 /*** file scope type declarations ****************************************************************/
56 /*** file scope variables ************************************************************************/
58 /*** file scope functions ************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
61 /* calculate positions of buttons; width is never less than 7 */
63 buttonbar_init_button_positions (WButtonBar
* bb
)
68 if (COLS
< BUTTONBAR_LABELS_NUM
* 7)
70 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++)
75 bb
->labels
[i
].end_coord
= pos
;
80 /* Distribute the extra width in a way that the middle vertical line
81 (between F5 and F6) aligns with the two panels. The extra width
82 is distributed in this order: F10, F5, F9, F4, ..., F6, F1. */
85 dv
= COLS
/ BUTTONBAR_LABELS_NUM
;
86 md
= COLS
% BUTTONBAR_LABELS_NUM
;
88 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
/ 2; i
++)
91 if (BUTTONBAR_LABELS_NUM
/ 2 - 1 - i
< md
/ 2)
94 bb
->labels
[i
].end_coord
= pos
;
97 for (; i
< BUTTONBAR_LABELS_NUM
; i
++)
100 if (BUTTONBAR_LABELS_NUM
- 1 - i
< (md
+ 1) / 2)
103 bb
->labels
[i
].end_coord
= pos
;
108 /* --------------------------------------------------------------------------------------------- */
110 /* return width of one button */
112 buttonbar_get_button_width (const WButtonBar
* bb
, int i
)
115 return bb
->labels
[0].end_coord
;
116 return bb
->labels
[i
].end_coord
- bb
->labels
[i
- 1].end_coord
;
119 /* --------------------------------------------------------------------------------------------- */
122 buttonbar_get_button_by_x_coord (const WButtonBar
* bb
, int x
)
126 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++)
127 if (bb
->labels
[i
].end_coord
> x
)
133 /* --------------------------------------------------------------------------------------------- */
136 set_label_text (WButtonBar
* bb
, int idx
, const char *text
)
138 g_free (bb
->labels
[idx
- 1].text
);
139 bb
->labels
[idx
- 1].text
= g_strdup (text
);
142 /* --------------------------------------------------------------------------------------------- */
144 /* returns TRUE if a function has been called, FALSE otherwise. */
146 buttonbar_call (WButtonBar
* bb
, int i
)
148 cb_ret_t ret
= MSG_NOT_HANDLED
;
149 Widget
*w
= WIDGET (bb
);
152 target
= (bb
->labels
[i
].receiver
!= NULL
) ? bb
->labels
[i
].receiver
: WIDGET (w
->owner
);
154 if ((bb
!= NULL
) && (bb
->labels
[i
].command
!= CK_IgnoreKey
))
155 ret
= send_message (target
, w
, MSG_ACTION
, bb
->labels
[i
].command
, NULL
);
159 /* --------------------------------------------------------------------------------------------- */
162 buttonbar_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
164 WButtonBar
*bb
= BUTTONBAR (w
);
170 return MSG_NOT_HANDLED
;
173 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++)
174 if (parm
== KEY_F (i
+ 1) && buttonbar_call (bb
, i
))
176 return MSG_NOT_HANDLED
;
181 buttonbar_init_button_positions (bb
);
182 widget_move (w
, 0, 0);
183 tty_setcolor (DEFAULT_COLOR
);
184 tty_printf ("%-*s", w
->cols
, "");
185 widget_move (w
, 0, 0);
187 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++)
192 width
= buttonbar_get_button_width (bb
, i
);
196 tty_setcolor (BUTTONBAR_HOTKEY_COLOR
);
197 tty_printf ("%2d", i
+ 1);
199 tty_setcolor (BUTTONBAR_BUTTON_COLOR
);
200 text
= (bb
->labels
[i
].text
!= NULL
) ? bb
->labels
[i
].text
: "";
201 tty_print_string (str_fit_to_term (text
, width
- 2, J_LEFT_FIT
));
207 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++)
208 g_free (bb
->labels
[i
].text
);
212 return widget_default_callback (w
, sender
, msg
, parm
, data
);
216 /* --------------------------------------------------------------------------------------------- */
219 buttonbar_mouse_callback (Widget
* w
, mouse_msg_t msg
, mouse_event_t
* event
)
223 case MSG_MOUSE_CLICK
:
225 WButtonBar
*bb
= BUTTONBAR (w
);
228 button
= buttonbar_get_button_by_x_coord (bb
, event
->x
);
230 buttonbar_call (bb
, button
);
239 /* --------------------------------------------------------------------------------------------- */
240 /*** public functions ****************************************************************************/
241 /* --------------------------------------------------------------------------------------------- */
244 buttonbar_new (gboolean visible
)
249 bb
= g_new0 (WButtonBar
, 1);
251 widget_init (w
, LINES
- 1, 0, 1, COLS
, buttonbar_callback
, buttonbar_mouse_callback
);
253 w
->pos_flags
= WPOS_KEEP_HORZ
| WPOS_KEEP_BOTTOM
;
254 bb
->visible
= visible
;
255 widget_want_hotkey (w
, TRUE
);
256 widget_want_cursor (w
, FALSE
);
261 /* --------------------------------------------------------------------------------------------- */
264 buttonbar_set_label (WButtonBar
* bb
, int idx
, const char *text
, const global_keymap_t
* keymap
,
267 if ((bb
!= NULL
) && (idx
>= 1) && (idx
<= BUTTONBAR_LABELS_NUM
))
269 long command
= CK_IgnoreKey
;
272 command
= keybind_lookup_keymap_command (keymap
, KEY_F (idx
));
274 if ((text
== NULL
) || (text
[0] == '\0'))
275 set_label_text (bb
, idx
, "");
277 set_label_text (bb
, idx
, text
);
279 bb
->labels
[idx
- 1].command
= command
;
280 bb
->labels
[idx
- 1].receiver
= WIDGET (receiver
);
284 /* --------------------------------------------------------------------------------------------- */
286 /* Find ButtonBar widget in the dialog */
288 find_buttonbar (const WDialog
* h
)
290 return BUTTONBAR (find_widget_type (h
, buttonbar_callback
));