1 /* Widgets for the Midnight Commander
3 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
6 Authors: 1994, 1995 Radek Doulik
7 1994, 1995 Miguel de Icaza
11 2009, 2010 Andrew Borodin
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 * \brief Source: WButtonBar widget
38 #include "lib/global.h"
40 #include "lib/tty/tty.h"
41 #include "lib/tty/mouse.h"
42 #include "lib/tty/key.h" /* XCTRL and ALT macros */
44 #include "lib/strutil.h"
46 #include "lib/keybind.h" /* global_keymap_t */
47 #include "lib/widget.h"
49 /*** global variables ****************************************************************************/
51 /*** file scope macro definitions ****************************************************************/
53 /*** file scope type declarations ****************************************************************/
55 /*** file scope variables ************************************************************************/
57 /*** file scope functions ************************************************************************/
58 /* --------------------------------------------------------------------------------------------- */
60 /* calculate positions of buttons; width is never less than 7 */
62 buttonbar_init_button_positions (WButtonBar
* bb
)
67 if (COLS
< BUTTONBAR_LABELS_NUM
* 7)
69 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++)
74 bb
->labels
[i
].end_coord
= pos
;
79 /* Distribute the extra width in a way that the middle vertical line
80 (between F5 and F6) aligns with the two panels. The extra width
81 is distributed in this order: F10, F5, F9, F4, ..., F6, F1. */
84 dv
= COLS
/ BUTTONBAR_LABELS_NUM
;
85 md
= COLS
% BUTTONBAR_LABELS_NUM
;
87 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
/ 2; i
++)
90 if (BUTTONBAR_LABELS_NUM
/ 2 - 1 - i
< md
/ 2)
93 bb
->labels
[i
].end_coord
= pos
;
96 for (; i
< BUTTONBAR_LABELS_NUM
; i
++)
99 if (BUTTONBAR_LABELS_NUM
- 1 - i
< (md
+ 1) / 2)
102 bb
->labels
[i
].end_coord
= pos
;
107 /* --------------------------------------------------------------------------------------------- */
109 /* return width of one button */
111 buttonbar_get_button_width (const WButtonBar
* bb
, int i
)
114 return bb
->labels
[0].end_coord
;
115 return bb
->labels
[i
].end_coord
- bb
->labels
[i
- 1].end_coord
;
118 /* --------------------------------------------------------------------------------------------- */
121 buttonbar_get_button_by_x_coord (const WButtonBar
* bb
, int x
)
125 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++)
126 if (bb
->labels
[i
].end_coord
> x
)
132 /* --------------------------------------------------------------------------------------------- */
135 set_label_text (WButtonBar
* bb
, int idx
, const char *text
)
137 g_free (bb
->labels
[idx
- 1].text
);
138 bb
->labels
[idx
- 1].text
= g_strdup (text
);
141 /* --------------------------------------------------------------------------------------------- */
143 /* returns TRUE if a function has been called, FALSE otherwise. */
145 buttonbar_call (WButtonBar
* bb
, int i
)
147 cb_ret_t ret
= MSG_NOT_HANDLED
;
149 if ((bb
!= NULL
) && (bb
->labels
[i
].command
!= CK_Ignore_Key
))
150 ret
= bb
->widget
.owner
->callback (bb
->widget
.owner
,
151 (Widget
*) bb
, DLG_ACTION
,
152 bb
->labels
[i
].command
, bb
->labels
[i
].receiver
);
156 /* --------------------------------------------------------------------------------------------- */
159 buttonbar_callback (Widget
* w
, widget_msg_t msg
, int parm
)
161 WButtonBar
*bb
= (WButtonBar
*) w
;
168 return MSG_NOT_HANDLED
;
171 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++)
172 if (parm
== KEY_F (i
+ 1) && buttonbar_call (bb
, i
))
174 return MSG_NOT_HANDLED
;
179 buttonbar_init_button_positions (bb
);
180 widget_move (&bb
->widget
, 0, 0);
181 tty_setcolor (DEFAULT_COLOR
);
182 tty_printf ("%-*s", bb
->widget
.cols
, "");
183 widget_move (&bb
->widget
, 0, 0);
185 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++)
189 width
= buttonbar_get_button_width (bb
, i
);
193 tty_setcolor (BUTTONBAR_HOTKEY_COLOR
);
194 tty_printf ("%2d", i
+ 1);
196 tty_setcolor (BUTTONBAR_BUTTON_COLOR
);
197 text
= (bb
->labels
[i
].text
!= NULL
) ? bb
->labels
[i
].text
: "";
198 tty_print_string (str_fit_to_term (text
, width
- 2, J_LEFT_FIT
));
204 for (i
= 0; i
< BUTTONBAR_LABELS_NUM
; i
++)
205 g_free (bb
->labels
[i
].text
);
209 return default_proc (msg
, parm
);
213 /* --------------------------------------------------------------------------------------------- */
216 buttonbar_event (Gpm_Event
* event
, void *data
)
218 WButtonBar
*bb
= data
;
221 if (!(event
->type
& GPM_UP
))
225 button
= buttonbar_get_button_by_x_coord (bb
, event
->x
- 1);
227 buttonbar_call (bb
, button
);
231 /* --------------------------------------------------------------------------------------------- */
232 /*** public functions ****************************************************************************/
233 /* --------------------------------------------------------------------------------------------- */
236 buttonbar_new (gboolean visible
)
240 bb
= g_new0 (WButtonBar
, 1);
242 init_widget (&bb
->widget
, LINES
- 1, 0, 1, COLS
, buttonbar_callback
, buttonbar_event
);
243 bb
->widget
.pos_flags
= WPOS_KEEP_HORZ
| WPOS_KEEP_BOTTOM
;
244 bb
->visible
= visible
;
245 widget_want_hotkey (bb
->widget
, 1);
246 widget_want_cursor (bb
->widget
, 0);
251 /* --------------------------------------------------------------------------------------------- */
254 buttonbar_set_label (WButtonBar
* bb
, int idx
, const char *text
,
255 const struct global_keymap_t
*keymap
, const Widget
* receiver
)
257 if ((bb
!= NULL
) && (idx
>= 1) && (idx
<= BUTTONBAR_LABELS_NUM
))
259 unsigned long command
= CK_Ignore_Key
;
262 command
= keybind_lookup_keymap_command (keymap
, KEY_F (idx
));
264 if ((text
== NULL
) || (text
[0] == '\0'))
265 set_label_text (bb
, idx
, "");
267 set_label_text (bb
, idx
, text
);
269 bb
->labels
[idx
- 1].command
= command
;
270 bb
->labels
[idx
- 1].receiver
= (Widget
*) receiver
;
274 /* --------------------------------------------------------------------------------------------- */
276 /* Find ButtonBar widget in the dialog */
278 find_buttonbar (const Dlg_head
* h
)
280 return (WButtonBar
*) find_widget_type (h
, buttonbar_callback
);