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-2024
   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-2022
  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     WGroup *g = w->owner;
  66     WDialog *h = DIALOG (g);
  67     int off = 0;
  68 
  69     switch (msg)
  70     {
  71     case MSG_HOTKEY:
  72         /*
  73          * Don't let the default button steal Enter from the current
  74          * button.  This is a workaround for the flawed event model
  75          * when hotkeys are sent to all widgets before the key is
  76          * handled by the current widget.
  77          */
  78         if (parm == '\n' && WIDGET (g->current->data) == w)
  79         {
  80             send_message (w, sender, MSG_KEY, ' ', data);
  81             return MSG_HANDLED;
  82         }
  83 
  84         if (parm == '\n' && b->flags == DEFPUSH_BUTTON)
  85         {
  86             send_message (w, sender, MSG_KEY, ' ', data);
  87             return MSG_HANDLED;
  88         }
  89 
  90         if (b->text.hotkey != NULL && g_ascii_tolower ((gchar) b->text.hotkey[0]) == parm)
  91         {
  92             send_message (w, sender, MSG_KEY, ' ', data);
  93             return MSG_HANDLED;
  94         }
  95         return MSG_NOT_HANDLED;
  96 
  97     case MSG_KEY:
  98         if (parm != ' ' && parm != '\n')
  99             return MSG_NOT_HANDLED;
 100 
 101         h->ret_value = b->action;
 102         if (b->callback == NULL || b->callback (b, b->action) != 0)
 103             dlg_close (h);
 104 
 105         return MSG_HANDLED;
 106 
 107     case MSG_CURSOR:
 108         switch (b->flags)
 109         {
 110         case DEFPUSH_BUTTON:
 111             off = 3;
 112             break;
 113         case NORMAL_BUTTON:
 114             off = 2;
 115             break;
 116         case NARROW_BUTTON:
 117             off = 1;
 118             break;
 119         case HIDDEN_BUTTON:
 120         default:
 121             off = 0;
 122             break;
 123         }
 124         widget_gotoyx (w, 0, b->hotpos + off);
 125         return MSG_HANDLED;
 126 
 127     case MSG_DRAW:
 128         {
 129             gboolean focused;
 130 
 131             focused = widget_get_state (w, WST_FOCUSED);
 132 
 133             widget_selectcolor (w, focused, FALSE);
 134             widget_gotoyx (w, 0, 0);
 135 
 136             switch (b->flags)
 137             {
 138             case DEFPUSH_BUTTON:
 139                 tty_print_string ("[< ");
 140                 break;
 141             case NORMAL_BUTTON:
 142                 tty_print_string ("[ ");
 143                 break;
 144             case NARROW_BUTTON:
 145                 tty_print_string ("[");
 146                 break;
 147             case HIDDEN_BUTTON:
 148             default:
 149                 return MSG_HANDLED;
 150             }
 151 
 152             hotkey_draw (w, b->text, focused);
 153 
 154             switch (b->flags)
 155             {
 156             case DEFPUSH_BUTTON:
 157                 tty_print_string (" >]");
 158                 break;
 159             case NORMAL_BUTTON:
 160                 tty_print_string (" ]");
 161                 break;
 162             case NARROW_BUTTON:
 163                 tty_print_string ("]");
 164                 break;
 165             default:
 166                 break;
 167             }
 168 
 169             return MSG_HANDLED;
 170         }
 171 
 172     case MSG_DESTROY:
 173         hotkey_free (b->text);
 174         return MSG_HANDLED;
 175 
 176     default:
 177         return widget_default_callback (w, sender, msg, parm, data);
 178     }
 179 }
 180 
 181 /* --------------------------------------------------------------------------------------------- */
 182 
 183 void
 184 button_mouse_default_callback (Widget *w, mouse_msg_t msg, mouse_event_t *event)
     /* [previous][next][first][last][top][bottom][index][help]  */
 185 {
 186     (void) event;
 187 
 188     switch (msg)
 189     {
 190     case MSG_MOUSE_DOWN:
 191         widget_select (w);
 192         break;
 193 
 194     case MSG_MOUSE_CLICK:
 195         send_message (w, NULL, MSG_KEY, ' ', NULL);
 196         send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
 197         break;
 198 
 199     default:
 200         break;
 201     }
 202 }
 203 
 204 /* --------------------------------------------------------------------------------------------- */
 205 
 206 WButton *
 207 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]  */
 208 {
 209     WRect r = { y, x, 1, 1 };
 210     WButton *b;
 211     Widget *w;
 212 
 213     b = g_new (WButton, 1);
 214     w = WIDGET (b);
 215 
 216     b->action = action;
 217     b->flags = flags;
 218     b->text = hotkey_new (text);
 219     r.cols = button_get_len (b);
 220     widget_init (w, &r, button_default_callback, button_mouse_default_callback);
 221     w->options |= WOP_SELECTABLE | WOP_WANT_CURSOR | WOP_WANT_HOTKEY;
 222     b->callback = callback;
 223     b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
 224 
 225     return b;
 226 }
 227 
 228 /* --------------------------------------------------------------------------------------------- */
 229 
 230 char *
 231 button_get_text (const WButton *b)
     /* [previous][next][first][last][top][bottom][index][help]  */
 232 {
 233     return hotkey_get_text (b->text);
 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     hotkey_t hk;
 243 
 244     hk = hotkey_new (text);
 245     if (hotkey_equal (b->text, hk))
 246     {
 247         hotkey_free (hk);
 248         return;
 249     }
 250 
 251     hotkey_free (b->text);
 252     b->text = hk;
 253     b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
 254     w->rect.cols = button_get_len (b);
 255     widget_draw (w);
 256 }
 257 
 258 /* --------------------------------------------------------------------------------------------- */
 259 
 260 int
 261 button_get_len (const WButton *b)
     /* [previous][next][first][last][top][bottom][index][help]  */
 262 {
 263     int ret = hotkey_width (b->text);
 264 
 265     switch (b->flags)
 266     {
 267     case DEFPUSH_BUTTON:
 268         ret += 6;
 269         break;
 270     case NORMAL_BUTTON:
 271         ret += 4;
 272         break;
 273     case NARROW_BUTTON:
 274         ret += 2;
 275         break;
 276     case HIDDEN_BUTTON:
 277     default:
 278         return 0;
 279     }
 280 
 281     return ret;
 282 }
 283 
 284 /* --------------------------------------------------------------------------------------------- */

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