Added locale setting before calling dpkg to fix "link to" parsing on non-C locales
[midnight-commander.git] / lib / widget / widget-common.c
blobe1490a0e47140b10d6b421b79ca4286754fcc49f
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 widget-common.c
30 * \brief Source: shared stuff of widgets
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/color.h"
42 #include "lib/skin.h"
43 #include "lib/strutil.h"
44 #include "lib/widget.h"
46 /*** global variables ****************************************************************************/
48 /*** file scope macro definitions ****************************************************************/
50 /*** file scope type declarations ****************************************************************/
52 /*** file scope variables ************************************************************************/
54 /*** file scope functions ************************************************************************/
56 /* --------------------------------------------------------------------------------------------- */
57 /*** public functions ****************************************************************************/
58 /* --------------------------------------------------------------------------------------------- */
60 struct hotkey_t
61 parse_hotkey (const char *text)
63 hotkey_t result;
64 const char *cp, *p;
66 if (text == NULL)
67 text = "";
69 /* search for '&', that is not on the of text */
70 cp = strchr (text, '&');
71 if (cp != NULL && cp[1] != '\0')
73 result.start = g_strndup (text, cp - text);
75 /* skip '&' */
76 cp++;
77 p = str_cget_next_char (cp);
78 result.hotkey = g_strndup (cp, p - cp);
80 cp = p;
81 result.end = g_strdup (cp);
83 else
85 result.start = g_strdup (text);
86 result.hotkey = NULL;
87 result.end = NULL;
90 return result;
93 /* --------------------------------------------------------------------------------------------- */
95 void
96 release_hotkey (const hotkey_t hotkey)
98 g_free (hotkey.start);
99 g_free (hotkey.hotkey);
100 g_free (hotkey.end);
103 /* --------------------------------------------------------------------------------------------- */
106 hotkey_width (const hotkey_t hotkey)
108 int result;
110 result = str_term_width1 (hotkey.start);
111 result += (hotkey.hotkey != NULL) ? str_term_width1 (hotkey.hotkey) : 0;
112 result += (hotkey.end != NULL) ? str_term_width1 (hotkey.end) : 0;
113 return result;
116 /* --------------------------------------------------------------------------------------------- */
118 void
119 hotkey_draw (Widget * w, const hotkey_t hotkey, gboolean focused)
121 widget_selectcolor (w, focused, FALSE);
122 tty_print_string (hotkey.start);
124 if (hotkey.hotkey != NULL)
126 widget_selectcolor (w, focused, TRUE);
127 tty_print_string (hotkey.hotkey);
128 widget_selectcolor (w, focused, FALSE);
131 if (hotkey.end != NULL)
132 tty_print_string (hotkey.end);
135 /* --------------------------------------------------------------------------------------------- */
137 void
138 init_widget (Widget * w, int y, int x, int lines, int cols,
139 callback_fn callback, mouse_h mouse_handler)
141 w->x = x;
142 w->y = y;
143 w->cols = cols;
144 w->lines = lines;
145 w->callback = callback;
146 w->mouse = mouse_handler;
147 w->owner = NULL;
149 /* Almost all widgets want to put the cursor in a suitable place */
150 w->options = W_WANT_CURSOR;
153 /* --------------------------------------------------------------------------------------------- */
155 /* Default callback for widgets */
156 cb_ret_t
157 default_proc (widget_msg_t msg, int parm)
159 (void) parm;
161 switch (msg)
163 case WIDGET_INIT:
164 case WIDGET_FOCUS:
165 case WIDGET_UNFOCUS:
166 case WIDGET_DRAW:
167 case WIDGET_DESTROY:
168 case WIDGET_CURSOR:
169 case WIDGET_IDLE:
170 return MSG_HANDLED;
172 default:
173 return MSG_NOT_HANDLED;
177 /* --------------------------------------------------------------------------------------------- */
179 void
180 widget_set_size (Widget * widget, int y, int x, int lines, int cols)
182 widget->x = x;
183 widget->y = y;
184 widget->cols = cols;
185 widget->lines = lines;
186 send_message (widget, WIDGET_RESIZED, 0 /* unused */ );
189 /* --------------------------------------------------------------------------------------------- */
191 void
192 widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey)
194 Dlg_head *h = w->owner;
195 int color;
197 if ((w->options & W_DISABLED) != 0)
198 color = DISABLED_COLOR;
199 else if (hotkey)
201 if (focused)
202 color = h->color[DLG_COLOR_HOT_FOCUS];
203 else
204 color = h->color[DLG_COLOR_HOT_NORMAL];
206 else
208 if (focused)
209 color = h->color[DLG_COLOR_FOCUS];
210 else
211 color = h->color[DLG_COLOR_NORMAL];
214 tty_setcolor (color);
217 /* --------------------------------------------------------------------------------------------- */
219 void
220 widget_erase (Widget * w)
222 if (w != NULL)
223 tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
226 /* --------------------------------------------------------------------------------------------- */