2 Widgets for the Midnight Commander
4 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
5 2004, 2005, 2006, 2007, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
9 Radek Doulik, 1994, 1995
10 Miguel de Icaza, 1994, 1995
12 Andrej Borsenkow, 1996
14 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
16 This file is part of the Midnight Commander.
18 The Midnight Commander is free software: you can redistribute it
19 and/or modify it under the terms of the GNU General Public License as
20 published by the Free Software Foundation, either version 3 of the License,
21 or (at your option) any later version.
23 The Midnight Commander is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program. If not, see <http://www.gnu.org/licenses/>.
33 * \brief Source: WButtonBar widget
41 #include "lib/global.h"
43 #include "lib/tty/tty.h"
44 #include "lib/tty/mouse.h"
45 #include "lib/tty/key.h" /* XCTRL and ALT macros */
47 #include "lib/strutil.h"
49 #include "lib/keybind.h" /* global_keymap_t */
50 #include "lib/widget.h"
52 /*** global variables ****************************************************************************/
54 /*** file scope macro definitions ****************************************************************/
56 /*** file scope type declarations ****************************************************************/
58 /*** file scope variables ************************************************************************/
60 /*** file scope functions ************************************************************************/
61 /* --------------------------------------------------------------------------------------------- */
63 /* calculate positions of buttons; width is never less than 7 */
65 buttonbar_init_button_positions (WButtonBar
* bb
)
70 if (COLS
< BUTTONBAR_LABELS_NUM
* 7)
72 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++)
77 bb
->labels
[i
].end_coord
= pos
;
82 /* Distribute the extra width in a way that the middle vertical line
83 (between F5 and F6) aligns with the two panels. The extra width
84 is distributed in this order: F10, F5, F9, F4, ..., F6, F1. */
87 dv
= COLS
/ BUTTONBAR_LABELS_NUM
;
88 md
= COLS
% BUTTONBAR_LABELS_NUM
;
90 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
/ 2; i
++)
93 if (BUTTONBAR_LABELS_NUM
/ 2 - 1 - i
< md
/ 2)
96 bb
->labels
[i
].end_coord
= pos
;
99 for (; i
< BUTTONBAR_LABELS_NUM
; i
++)
102 if (BUTTONBAR_LABELS_NUM
- 1 - i
< (md
+ 1) / 2)
105 bb
->labels
[i
].end_coord
= pos
;
110 /* --------------------------------------------------------------------------------------------- */
112 /* return width of one button */
114 buttonbar_get_button_width (const WButtonBar
* bb
, int i
)
117 return bb
->labels
[0].end_coord
;
118 return bb
->labels
[i
].end_coord
- bb
->labels
[i
- 1].end_coord
;
121 /* --------------------------------------------------------------------------------------------- */
124 buttonbar_get_button_by_x_coord (const WButtonBar
* bb
, int x
)
128 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++)
129 if (bb
->labels
[i
].end_coord
> x
)
135 /* --------------------------------------------------------------------------------------------- */
138 set_label_text (WButtonBar
* bb
, int idx
, const char *text
)
140 g_free (bb
->labels
[idx
- 1].text
);
141 bb
->labels
[idx
- 1].text
= g_strdup (text
);
144 /* --------------------------------------------------------------------------------------------- */
146 /* returns TRUE if a function has been called, FALSE otherwise. */
148 buttonbar_call (WButtonBar
* bb
, int i
)
150 cb_ret_t ret
= MSG_NOT_HANDLED
;
151 Widget
*w
= WIDGET (bb
);
153 if ((bb
!= NULL
) && (bb
->labels
[i
].command
!= CK_IgnoreKey
))
154 ret
= send_message (w
->owner
, w
, MSG_ACTION
, bb
->labels
[i
].command
, bb
->labels
[i
].receiver
);
158 /* --------------------------------------------------------------------------------------------- */
161 buttonbar_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
163 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
++)
191 width
= buttonbar_get_button_width (bb
, i
);
195 tty_setcolor (BUTTONBAR_HOTKEY_COLOR
);
196 tty_printf ("%2d", i
+ 1);
198 tty_setcolor (BUTTONBAR_BUTTON_COLOR
);
199 text
= (bb
->labels
[i
].text
!= NULL
) ? bb
->labels
[i
].text
: "";
200 tty_print_string (str_fit_to_term (text
, width
- 2, J_LEFT_FIT
));
206 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++)
207 g_free (bb
->labels
[i
].text
);
211 return widget_default_callback (w
, sender
, msg
, parm
, data
);
215 /* --------------------------------------------------------------------------------------------- */
218 buttonbar_event (Gpm_Event
* event
, void *data
)
220 Widget
*w
= WIDGET (data
);
222 if (!mouse_global_in_widget (event
, w
))
223 return MOU_UNHANDLED
;
225 if ((event
->type
& GPM_UP
) != 0)
227 WButtonBar
*bb
= BUTTONBAR (data
);
231 local
= mouse_get_local (event
, w
);
232 button
= buttonbar_get_button_by_x_coord (bb
, local
.x
- 1);
234 buttonbar_call (bb
, button
);
240 /* --------------------------------------------------------------------------------------------- */
241 /*** public functions ****************************************************************************/
242 /* --------------------------------------------------------------------------------------------- */
245 buttonbar_new (gboolean visible
)
250 bb
= g_new0 (WButtonBar
, 1);
252 init_widget (w
, LINES
- 1, 0, 1, COLS
, buttonbar_callback
, buttonbar_event
);
254 w
->pos_flags
= WPOS_KEEP_HORZ
| WPOS_KEEP_BOTTOM
;
255 bb
->visible
= visible
;
256 widget_want_hotkey (w
, TRUE
);
257 widget_want_cursor (w
, FALSE
);
262 /* --------------------------------------------------------------------------------------------- */
265 buttonbar_set_label (WButtonBar
* bb
, int idx
, const char *text
,
266 const struct global_keymap_t
*keymap
, const Widget
* receiver
)
268 if ((bb
!= NULL
) && (idx
>= 1) && (idx
<= BUTTONBAR_LABELS_NUM
))
270 unsigned long command
= CK_IgnoreKey
;
273 command
= keybind_lookup_keymap_command (keymap
, KEY_F (idx
));
275 if ((text
== NULL
) || (text
[0] == '\0'))
276 set_label_text (bb
, idx
, "");
278 set_label_text (bb
, idx
, text
);
280 bb
->labels
[idx
- 1].command
= command
;
281 bb
->labels
[idx
- 1].receiver
= WIDGET (receiver
);
285 /* --------------------------------------------------------------------------------------------- */
287 /* Find ButtonBar widget in the dialog */
289 find_buttonbar (const WDialog
* h
)
291 return BUTTONBAR (find_widget_type (h
, buttonbar_callback
));