root/lib/widget/button.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. button_default_callback
  2. button_mouse_default_callback
  3. button_new
  4. button_get_text
  5. button_set_text
  6. button_get_len

   1 /*
   2    Widgets for the Midnight Commander
   3 
   4    Copyright (C) 1994-2019
   5    Free Software Foundation, Inc.
   6 
   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, 2016
  14 
  15    This file is part of the Midnight Commander.
  16 
  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.
  21 
  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.
  26 
  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/>.
  29  */
  30 
  31 /** \file button.c
  32  *  \brief Source: WButton widget
  33  */
  34 
  35 #include <config.h>
  36 
  37 #include <stdlib.h>
  38 
  39 #include "lib/global.h"
  40 
  41 #include "lib/tty/tty.h"
  42 #include "lib/strutil.h"
  43 #include "lib/widget.h"
  44 
  45 /*** global variables ****************************************************************************/
  46 
  47 /*** file scope macro definitions ****************************************************************/
  48 
  49 /*** file scope type declarations ****************************************************************/
  50 
  51 /*** file scope variables ************************************************************************/
  52 
  53 /* --------------------------------------------------------------------------------------------- */
  54 /*** file scope functions ************************************************************************/
  55 /* --------------------------------------------------------------------------------------------- */
  56 
  57 /* --------------------------------------------------------------------------------------------- */
  58 /*** public functions ****************************************************************************/
  59 /* --------------------------------------------------------------------------------------------- */
  60 
  61 cb_ret_t
  62 button_default_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  63 {
  64     WButton *b = BUTTON (w);
  65     WDialog *h = w->owner;
  66     int off = 0;
  67 
  68     switch (msg)
  69     {
  70     case MSG_HOTKEY:
  71         /*
  72          * Don't let the default button steal Enter from the current
  73          * button.  This is a workaround for the flawed event model
  74          * when hotkeys are sent to all widgets before the key is
  75          * handled by the current widget.
  76          */
  77         if (parm == '\n' && WIDGET (h->current->data) == w)
  78         {
  79             send_message (w, sender, MSG_KEY, ' ', data);
  80             return MSG_HANDLED;
  81         }
  82 
  83         if (parm == '\n' && b->flags == DEFPUSH_BUTTON)
  84         {
  85             send_message (w, sender, MSG_KEY, ' ', data);
  86             return MSG_HANDLED;
  87         }
  88 
  89         if (b->text.hotkey != NULL && g_ascii_tolower ((gchar) b->text.hotkey[0]) == parm)
  90         {
  91             send_message (w, sender, MSG_KEY, ' ', data);
  92             return MSG_HANDLED;
  93         }
  94         return MSG_NOT_HANDLED;
  95 
  96     case MSG_KEY:
  97         if (parm != ' ' && parm != '\n')
  98             return MSG_NOT_HANDLED;
  99 
 100         h->ret_value = b->action;
 101         if (b->callback == NULL || b->callback (b, b->action) != 0)
 102             dlg_stop (h);
 103 
 104         return MSG_HANDLED;
 105 
 106     case MSG_CURSOR:
 107         switch (b->flags)
 108         {
 109         case DEFPUSH_BUTTON:
 110             off = 3;
 111             break;
 112         case NORMAL_BUTTON:
 113             off = 2;
 114             break;
 115         case NARROW_BUTTON:
 116             off = 1;
 117             break;
 118         case HIDDEN_BUTTON:
 119         default:
 120             off = 0;
 121             break;
 122         }
 123         widget_move (w, 0, b->hotpos + off);
 124         return MSG_HANDLED;
 125 
 126     case MSG_DRAW:
 127         {
 128             gboolean focused;
 129 
 130             focused = widget_get_state (w, WST_FOCUSED);
 131 
 132             widget_selectcolor (w, focused, FALSE);
 133             widget_move (w, 0, 0);
 134 
 135             switch (b->flags)
 136             {
 137             case DEFPUSH_BUTTON:
 138                 tty_print_string ("[< ");
 139                 break;
 140             case NORMAL_BUTTON:
 141                 tty_print_string ("[ ");
 142                 break;
 143             case NARROW_BUTTON:
 144                 tty_print_string ("[");
 145                 break;
 146             case HIDDEN_BUTTON:
 147             default:
 148                 return MSG_HANDLED;
 149             }
 150 
 151             hotkey_draw (w, b->text, focused);
 152 
 153             switch (b->flags)
 154             {
 155             case DEFPUSH_BUTTON:
 156                 tty_print_string (" >]");
 157                 break;
 158             case NORMAL_BUTTON:
 159                 tty_print_string (" ]");
 160                 break;
 161             case NARROW_BUTTON:
 162                 tty_print_string ("]");
 163                 break;
 164             default:
 165                 break;
 166             }
 167 
 168             return MSG_HANDLED;
 169         }
 170 
 171     case MSG_DESTROY:
 172         release_hotkey (b->text);
 173         return MSG_HANDLED;
 174 
 175     default:
 176         return widget_default_callback (w, sender, msg, parm, data);
 177     }
 178 }
 179 
 180 /* --------------------------------------------------------------------------------------------- */
 181 
 182 void
 183 button_mouse_default_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
     /* [previous][next][first][last][top][bottom][index][help]  */
 184 {
 185     (void) event;
 186 
 187     switch (msg)
 188     {
 189     case MSG_MOUSE_DOWN:
 190         widget_select (w);
 191         break;
 192 
 193     case MSG_MOUSE_CLICK:
 194         send_message (w, NULL, MSG_KEY, ' ', NULL);
 195         send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
 196         break;
 197 
 198     default:
 199         break;
 200     }
 201 }
 202 
 203 /* --------------------------------------------------------------------------------------------- */
 204 
 205 WButton *
 206 button_new (int y, int x, int action, button_flags_t flags, const char *text, bcback_fn callback)
     /* [previous][next][first][last][top][bottom][index][help]  */
 207 {
 208     WButton *b;
 209     Widget *w;
 210 
 211     b = g_new (WButton, 1);
 212     w = WIDGET (b);
 213 
 214     b->action = action;
 215     b->flags = flags;
 216     b->text = parse_hotkey (text);
 217     widget_init (w, y, x, 1, button_get_len (b), button_default_callback,
 218                  button_mouse_default_callback);
 219     w->options |= WOP_SELECTABLE | WOP_WANT_CURSOR | WOP_WANT_HOTKEY;
 220     b->callback = callback;
 221     b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
 222 
 223     return b;
 224 }
 225 
 226 /* --------------------------------------------------------------------------------------------- */
 227 
 228 char *
 229 button_get_text (const WButton * b)
     /* [previous][next][first][last][top][bottom][index][help]  */
 230 {
 231     if (b->text.hotkey != NULL)
 232         return g_strconcat (b->text.start, "&", b->text.hotkey, b->text.end, (char *) NULL);
 233     return g_strdup (b->text.start);
 234 }
 235 
 236 /* --------------------------------------------------------------------------------------------- */
 237 
 238 void
 239 button_set_text (WButton * b, const char *text)
     /* [previous][next][first][last][top][bottom][index][help]  */
 240 {
 241     Widget *w = WIDGET (b);
 242 
 243     release_hotkey (b->text);
 244     b->text = parse_hotkey (text);
 245     b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
 246     w->cols = button_get_len (b);
 247     widget_redraw (w);
 248 }
 249 
 250 /* --------------------------------------------------------------------------------------------- */
 251 
 252 int
 253 button_get_len (const WButton * b)
     /* [previous][next][first][last][top][bottom][index][help]  */
 254 {
 255     int ret = hotkey_width (b->text);
 256 
 257     switch (b->flags)
 258     {
 259     case DEFPUSH_BUTTON:
 260         ret += 6;
 261         break;
 262     case NORMAL_BUTTON:
 263         ret += 4;
 264         break;
 265     case NARROW_BUTTON:
 266         ret += 2;
 267         break;
 268     case HIDDEN_BUTTON:
 269     default:
 270         return 0;
 271     }
 272 
 273     return ret;
 274 }
 275 
 276 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */