panel.h: remove unused variable declaration.
[midnight-commander.git] / lib / widget / buttonbar.c
bloba97772db024569f7d566b4911f70d3bad6fbd433
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2015
5 Free Software Foundation, Inc.
7 Authors:
8 Radek Doulik, 1994, 1995
9 Miguel de Icaza, 1994, 1995
10 Jakub Jelinek, 1995
11 Andrej Borsenkow, 1996
12 Norbert Warmuth, 1997
13 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013
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/>.
31 /** \file buttonbar.c
32 * \brief Source: WButtonBar widget
35 #include <config.h>
37 #include <stdlib.h>
38 #include <string.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/mouse.h"
44 #include "lib/tty/key.h" /* XCTRL and ALT macros */
45 #include "lib/skin.h"
46 #include "lib/strutil.h"
47 #include "lib/util.h"
48 #include "lib/keybind.h" /* global_keymap_t */
49 #include "lib/widget.h"
51 /*** global variables ****************************************************************************/
53 /*** file scope macro definitions ****************************************************************/
55 /*** file scope type declarations ****************************************************************/
57 /*** file scope variables ************************************************************************/
59 /*** file scope functions ************************************************************************/
60 /* --------------------------------------------------------------------------------------------- */
62 /* calculate positions of buttons; width is never less than 7 */
63 static void
64 buttonbar_init_button_positions (WButtonBar * bb)
66 int i;
67 int pos = 0;
69 if (COLS < BUTTONBAR_LABELS_NUM * 7)
71 for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
73 if (pos + 7 <= COLS)
74 pos += 7;
76 bb->labels[i].end_coord = pos;
79 else
81 /* Distribute the extra width in a way that the middle vertical line
82 (between F5 and F6) aligns with the two panels. The extra width
83 is distributed in this order: F10, F5, F9, F4, ..., F6, F1. */
84 int dv, md;
86 dv = COLS / BUTTONBAR_LABELS_NUM;
87 md = COLS % BUTTONBAR_LABELS_NUM;
89 for (i = 0; i < BUTTONBAR_LABELS_NUM / 2; i++)
91 pos += dv;
92 if (BUTTONBAR_LABELS_NUM / 2 - 1 - i < md / 2)
93 pos++;
95 bb->labels[i].end_coord = pos;
98 for (; i < BUTTONBAR_LABELS_NUM; i++)
100 pos += dv;
101 if (BUTTONBAR_LABELS_NUM - 1 - i < (md + 1) / 2)
102 pos++;
104 bb->labels[i].end_coord = pos;
109 /* --------------------------------------------------------------------------------------------- */
111 /* return width of one button */
112 static int
113 buttonbar_get_button_width (const WButtonBar * bb, int i)
115 if (i == 0)
116 return bb->labels[0].end_coord;
117 return bb->labels[i].end_coord - bb->labels[i - 1].end_coord;
120 /* --------------------------------------------------------------------------------------------- */
122 static int
123 buttonbar_get_button_by_x_coord (const WButtonBar * bb, int x)
125 int i;
127 for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
128 if (bb->labels[i].end_coord > x)
129 return i;
131 return (-1);
134 /* --------------------------------------------------------------------------------------------- */
136 static void
137 set_label_text (WButtonBar * bb, int idx, const char *text)
139 g_free (bb->labels[idx - 1].text);
140 bb->labels[idx - 1].text = g_strdup (text);
143 /* --------------------------------------------------------------------------------------------- */
145 /* returns TRUE if a function has been called, FALSE otherwise. */
146 static gboolean
147 buttonbar_call (WButtonBar * bb, int i)
149 cb_ret_t ret = MSG_NOT_HANDLED;
150 Widget *w = WIDGET (bb);
152 if ((bb != NULL) && (bb->labels[i].command != CK_IgnoreKey))
153 ret = send_message (w->owner, w, MSG_ACTION, bb->labels[i].command, bb->labels[i].receiver);
154 return ret;
157 /* --------------------------------------------------------------------------------------------- */
159 static cb_ret_t
160 buttonbar_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
162 WButtonBar *bb = BUTTONBAR (w);
163 int i;
165 switch (msg)
167 case MSG_FOCUS:
168 return MSG_NOT_HANDLED;
170 case MSG_HOTKEY:
171 for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
172 if (parm == KEY_F (i + 1) && buttonbar_call (bb, i))
173 return MSG_HANDLED;
174 return MSG_NOT_HANDLED;
176 case MSG_DRAW:
177 if (bb->visible)
179 buttonbar_init_button_positions (bb);
180 widget_move (w, 0, 0);
181 tty_setcolor (DEFAULT_COLOR);
182 tty_printf ("%-*s", w->cols, "");
183 widget_move (w, 0, 0);
185 for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
187 int width;
188 const char *text;
190 width = buttonbar_get_button_width (bb, i);
191 if (width <= 0)
192 break;
194 tty_setcolor (BUTTONBAR_HOTKEY_COLOR);
195 tty_printf ("%2d", i + 1);
197 tty_setcolor (BUTTONBAR_BUTTON_COLOR);
198 text = (bb->labels[i].text != NULL) ? bb->labels[i].text : "";
199 tty_print_string (str_fit_to_term (text, width - 2, J_LEFT_FIT));
202 return MSG_HANDLED;
204 case MSG_DESTROY:
205 for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
206 g_free (bb->labels[i].text);
207 return MSG_HANDLED;
209 default:
210 return widget_default_callback (w, sender, msg, parm, data);
214 /* --------------------------------------------------------------------------------------------- */
216 static int
217 buttonbar_event (Gpm_Event * event, void *data)
219 Widget *w = WIDGET (data);
221 if (!mouse_global_in_widget (event, w))
222 return MOU_UNHANDLED;
224 if ((event->type & GPM_UP) != 0)
226 WButtonBar *bb = BUTTONBAR (data);
227 Gpm_Event local;
228 int button;
230 local = mouse_get_local (event, w);
231 button = buttonbar_get_button_by_x_coord (bb, local.x - 1);
232 if (button >= 0)
233 buttonbar_call (bb, button);
236 return MOU_NORMAL;
239 /* --------------------------------------------------------------------------------------------- */
240 /*** public functions ****************************************************************************/
241 /* --------------------------------------------------------------------------------------------- */
243 WButtonBar *
244 buttonbar_new (gboolean visible)
246 WButtonBar *bb;
247 Widget *w;
249 bb = g_new0 (WButtonBar, 1);
250 w = WIDGET (bb);
251 widget_init (w, LINES - 1, 0, 1, COLS, buttonbar_callback, buttonbar_event);
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);
258 return bb;
261 /* --------------------------------------------------------------------------------------------- */
263 void
264 buttonbar_set_label (WButtonBar * bb, int idx, const char *text,
265 const struct global_keymap_t *keymap, const Widget * receiver)
267 if ((bb != NULL) && (idx >= 1) && (idx <= BUTTONBAR_LABELS_NUM))
269 unsigned long command = CK_IgnoreKey;
271 if (keymap != NULL)
272 command = keybind_lookup_keymap_command (keymap, KEY_F (idx));
274 if ((text == NULL) || (text[0] == '\0'))
275 set_label_text (bb, idx, "");
276 else
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 */
287 WButtonBar *
288 find_buttonbar (const WDialog * h)
290 return BUTTONBAR (find_widget_type (h, buttonbar_callback));