Added locale setting before calling dpkg to fix "link to" parsing on non-C locales
[midnight-commander.git] / lib / widget / buttonbar.c
blob8cbc2aa8401e4167483557f891d5efc2d8ad95e5
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
8 1995 Jakub Jelinek
9 1996 Andrej Borsenkow
10 1997 Norbert Warmuth
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.
29 /** \file buttonbar.c
30 * \brief Source: WButtonBar widget
33 #include <config.h>
35 #include <stdlib.h>
36 #include <string.h>
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 */
43 #include "lib/skin.h"
44 #include "lib/strutil.h"
45 #include "lib/util.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 */
61 static void
62 buttonbar_init_button_positions (WButtonBar * bb)
64 int i;
65 int pos = 0;
67 if (COLS < BUTTONBAR_LABELS_NUM * 7)
69 for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
71 if (pos + 7 <= COLS)
72 pos += 7;
74 bb->labels[i].end_coord = pos;
77 else
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. */
82 int dv, md;
84 dv = COLS / BUTTONBAR_LABELS_NUM;
85 md = COLS % BUTTONBAR_LABELS_NUM;
87 for (i = 0; i < BUTTONBAR_LABELS_NUM / 2; i++)
89 pos += dv;
90 if (BUTTONBAR_LABELS_NUM / 2 - 1 - i < md / 2)
91 pos++;
93 bb->labels[i].end_coord = pos;
96 for (; i < BUTTONBAR_LABELS_NUM; i++)
98 pos += dv;
99 if (BUTTONBAR_LABELS_NUM - 1 - i < (md + 1) / 2)
100 pos++;
102 bb->labels[i].end_coord = pos;
107 /* --------------------------------------------------------------------------------------------- */
109 /* return width of one button */
110 static int
111 buttonbar_get_button_width (const WButtonBar * bb, int i)
113 if (i == 0)
114 return bb->labels[0].end_coord;
115 return bb->labels[i].end_coord - bb->labels[i - 1].end_coord;
118 /* --------------------------------------------------------------------------------------------- */
120 static int
121 buttonbar_get_button_by_x_coord (const WButtonBar * bb, int x)
123 int i;
125 for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
126 if (bb->labels[i].end_coord > x)
127 return i;
129 return (-1);
132 /* --------------------------------------------------------------------------------------------- */
134 static void
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. */
144 static gboolean
145 buttonbar_call (WButtonBar * bb, int i)
147 cb_ret_t ret = MSG_NOT_HANDLED;
149 if ((bb != NULL) && (bb->labels[i].command != CK_IgnoreKey))
150 ret = bb->widget.owner->callback (bb->widget.owner,
151 (Widget *) bb, DLG_ACTION,
152 bb->labels[i].command, bb->labels[i].receiver);
153 return ret;
156 /* --------------------------------------------------------------------------------------------- */
158 static cb_ret_t
159 buttonbar_callback (Widget * w, widget_msg_t msg, int parm)
161 WButtonBar *bb = (WButtonBar *) w;
162 int i;
163 const char *text;
165 switch (msg)
167 case WIDGET_FOCUS:
168 return MSG_NOT_HANDLED;
170 case WIDGET_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 WIDGET_DRAW:
177 if (bb->visible)
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++)
187 int width;
189 width = buttonbar_get_button_width (bb, i);
190 if (width <= 0)
191 break;
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));
201 return MSG_HANDLED;
203 case WIDGET_DESTROY:
204 for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
205 g_free (bb->labels[i].text);
206 return MSG_HANDLED;
208 default:
209 return default_proc (msg, parm);
213 /* --------------------------------------------------------------------------------------------- */
215 static int
216 buttonbar_event (Gpm_Event * event, void *data)
218 WButtonBar *bb = data;
219 int button;
221 if (!(event->type & GPM_UP))
222 return MOU_NORMAL;
223 if (event->y == 2)
224 return MOU_NORMAL;
225 button = buttonbar_get_button_by_x_coord (bb, event->x - 1);
226 if (button >= 0)
227 buttonbar_call (bb, button);
228 return MOU_NORMAL;
231 /* --------------------------------------------------------------------------------------------- */
232 /*** public functions ****************************************************************************/
233 /* --------------------------------------------------------------------------------------------- */
235 WButtonBar *
236 buttonbar_new (gboolean visible)
238 WButtonBar *bb;
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);
248 return bb;
251 /* --------------------------------------------------------------------------------------------- */
253 void
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_IgnoreKey;
261 if (keymap != NULL)
262 command = keybind_lookup_keymap_command (keymap, KEY_F (idx));
264 if ((text == NULL) || (text[0] == '\0'))
265 set_label_text (bb, idx, "");
266 else
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 */
277 WButtonBar *
278 find_buttonbar (const Dlg_head * h)
280 return (WButtonBar *) find_widget_type (h, buttonbar_callback);