Unification of widget and dialog callback functions.
[midnight-commander.git] / lib / widget / buttonbar.c
blobc2903a9fda2dea0ee90fcd1757aae924b554f30d
1 /*
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.
8 Authors:
9 Radek Doulik, 1994, 1995
10 Miguel de Icaza, 1994, 1995
11 Jakub Jelinek, 1995
12 Andrej Borsenkow, 1996
13 Norbert Warmuth, 1997
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/>.
32 /** \file buttonbar.c
33 * \brief Source: WButtonBar widget
36 #include <config.h>
38 #include <stdlib.h>
39 #include <string.h>
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 */
46 #include "lib/skin.h"
47 #include "lib/strutil.h"
48 #include "lib/util.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 */
64 static void
65 buttonbar_init_button_positions (WButtonBar * bb)
67 int i;
68 int pos = 0;
70 if (COLS < BUTTONBAR_LABELS_NUM * 7)
72 for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
74 if (pos + 7 <= COLS)
75 pos += 7;
77 bb->labels[i].end_coord = pos;
80 else
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. */
85 int dv, md;
87 dv = COLS / BUTTONBAR_LABELS_NUM;
88 md = COLS % BUTTONBAR_LABELS_NUM;
90 for (i = 0; i < BUTTONBAR_LABELS_NUM / 2; i++)
92 pos += dv;
93 if (BUTTONBAR_LABELS_NUM / 2 - 1 - i < md / 2)
94 pos++;
96 bb->labels[i].end_coord = pos;
99 for (; i < BUTTONBAR_LABELS_NUM; i++)
101 pos += dv;
102 if (BUTTONBAR_LABELS_NUM - 1 - i < (md + 1) / 2)
103 pos++;
105 bb->labels[i].end_coord = pos;
110 /* --------------------------------------------------------------------------------------------- */
112 /* return width of one button */
113 static int
114 buttonbar_get_button_width (const WButtonBar * bb, int i)
116 if (i == 0)
117 return bb->labels[0].end_coord;
118 return bb->labels[i].end_coord - bb->labels[i - 1].end_coord;
121 /* --------------------------------------------------------------------------------------------- */
123 static int
124 buttonbar_get_button_by_x_coord (const WButtonBar * bb, int x)
126 int i;
128 for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
129 if (bb->labels[i].end_coord > x)
130 return i;
132 return (-1);
135 /* --------------------------------------------------------------------------------------------- */
137 static void
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. */
147 static gboolean
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 = w->owner->callback (w->owner, w, DLG_ACTION,
155 bb->labels[i].command, bb->labels[i].receiver);
156 return ret;
159 /* --------------------------------------------------------------------------------------------- */
161 static cb_ret_t
162 buttonbar_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
164 WButtonBar *bb = (WButtonBar *) w;
165 int i;
166 const char *text;
168 switch (msg)
170 case WIDGET_FOCUS:
171 return MSG_NOT_HANDLED;
173 case WIDGET_HOTKEY:
174 for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
175 if (parm == KEY_F (i + 1) && buttonbar_call (bb, i))
176 return MSG_HANDLED;
177 return MSG_NOT_HANDLED;
179 case WIDGET_DRAW:
180 if (bb->visible)
182 buttonbar_init_button_positions (bb);
183 widget_move (w, 0, 0);
184 tty_setcolor (DEFAULT_COLOR);
185 tty_printf ("%-*s", w->cols, "");
186 widget_move (w, 0, 0);
188 for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
190 int width;
192 width = buttonbar_get_button_width (bb, i);
193 if (width <= 0)
194 break;
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));
204 return MSG_HANDLED;
206 case WIDGET_DESTROY:
207 for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
208 g_free (bb->labels[i].text);
209 return MSG_HANDLED;
211 default:
212 return default_widget_callback (sender, msg, parm, data);
216 /* --------------------------------------------------------------------------------------------- */
218 static int
219 buttonbar_event (Gpm_Event * event, void *data)
221 Widget *w = WIDGET (data);
223 if (!mouse_global_in_widget (event, w))
224 return MOU_UNHANDLED;
226 if ((event->type & GPM_UP) != 0)
228 WButtonBar *bb = (WButtonBar *) data;
229 Gpm_Event local;
230 int button;
232 local = mouse_get_local (event, w);
233 button = buttonbar_get_button_by_x_coord (bb, local.x - 1);
234 if (button >= 0)
235 buttonbar_call (bb, button);
238 return MOU_NORMAL;
241 /* --------------------------------------------------------------------------------------------- */
242 /*** public functions ****************************************************************************/
243 /* --------------------------------------------------------------------------------------------- */
245 WButtonBar *
246 buttonbar_new (gboolean visible)
248 WButtonBar *bb;
249 Widget *w;
251 bb = g_new0 (WButtonBar, 1);
252 w = WIDGET (bb);
253 init_widget (w, LINES - 1, 0, 1, COLS, buttonbar_callback, buttonbar_event);
255 w->pos_flags = WPOS_KEEP_HORZ | WPOS_KEEP_BOTTOM;
256 bb->visible = visible;
257 widget_want_hotkey (w, TRUE);
258 widget_want_cursor (w, FALSE);
260 return bb;
263 /* --------------------------------------------------------------------------------------------- */
265 void
266 buttonbar_set_label (WButtonBar * bb, int idx, const char *text,
267 const struct global_keymap_t *keymap, const Widget * receiver)
269 if ((bb != NULL) && (idx >= 1) && (idx <= BUTTONBAR_LABELS_NUM))
271 unsigned long command = CK_IgnoreKey;
273 if (keymap != NULL)
274 command = keybind_lookup_keymap_command (keymap, KEY_F (idx));
276 if ((text == NULL) || (text[0] == '\0'))
277 set_label_text (bb, idx, "");
278 else
279 set_label_text (bb, idx, text);
281 bb->labels[idx - 1].command = command;
282 bb->labels[idx - 1].receiver = WIDGET (receiver);
286 /* --------------------------------------------------------------------------------------------- */
288 /* Find ButtonBar widget in the dialog */
289 WButtonBar *
290 find_buttonbar (const Dlg_head * h)
292 return (WButtonBar *) find_widget_type (h, buttonbar_callback);