root/lib/widget/radio.c

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

DEFINITIONS

This source file includes following definitions.
  1. radio_execute_cmd
  2. radio_key
  3. radio_callback
  4. radio_mouse_callback
  5. radio_new

   1 /*
   2    Widgets for the Midnight Commander
   3 
   4    Copyright (C) 1994-2020
   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 radio.c
  32  *  \brief Source: WRadui widget (radiobuttons)
  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/widget.h"
  43 
  44 /*** global variables ****************************************************************************/
  45 
  46 const global_keymap_t *radio_map = NULL;
  47 
  48 /*** file scope macro definitions ****************************************************************/
  49 
  50 /*** file scope type declarations ****************************************************************/
  51 
  52 /*** file scope variables ************************************************************************/
  53 
  54 /* --------------------------------------------------------------------------------------------- */
  55 /*** file scope functions ************************************************************************/
  56 /* --------------------------------------------------------------------------------------------- */
  57 
  58 static cb_ret_t
  59 radio_execute_cmd (WRadio * r, long command)
     /* [previous][next][first][last][top][bottom][index][help]  */
  60 {
  61     cb_ret_t ret = MSG_HANDLED;
  62     Widget *w = WIDGET (r);
  63 
  64     switch (command)
  65     {
  66     case CK_Up:
  67     case CK_Top:
  68         if (r->pos == 0)
  69             return MSG_NOT_HANDLED;
  70 
  71         if (command == CK_Top)
  72             r->pos = 0;
  73         else
  74             r->pos--;
  75         widget_draw (w);
  76         return MSG_HANDLED;
  77 
  78     case CK_Down:
  79     case CK_Bottom:
  80         if (r->pos == r->count - 1)
  81             return MSG_NOT_HANDLED;
  82 
  83         if (command == CK_Bottom)
  84             r->pos = r->count - 1;
  85         else
  86             r->pos++;
  87         widget_draw (w);
  88         return MSG_HANDLED;
  89 
  90     case CK_Select:
  91         r->sel = r->pos;
  92         widget_set_state (w, WST_FOCUSED, TRUE);        /* Also draws the widget */
  93         send_message (w->owner, w, MSG_NOTIFY, 0, NULL);
  94         return MSG_HANDLED;
  95 
  96     default:
  97         ret = MSG_NOT_HANDLED;
  98         break;
  99     }
 100 
 101     return ret;
 102 }
 103 
 104 /* --------------------------------------------------------------------------------------------- */
 105 
 106 /* Return MSG_HANDLED if we want a redraw */
 107 static cb_ret_t
 108 radio_key (WRadio * r, int key)
     /* [previous][next][first][last][top][bottom][index][help]  */
 109 {
 110     long command;
 111 
 112     command = widget_lookup_key (WIDGET (r), key);
 113     if (command == CK_IgnoreKey)
 114         return MSG_NOT_HANDLED;
 115     return radio_execute_cmd (r, command);
 116 }
 117 
 118 /* --------------------------------------------------------------------------------------------- */
 119 
 120 static cb_ret_t
 121 radio_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 122 {
 123     WRadio *r = RADIO (w);
 124     int i;
 125 
 126     switch (msg)
 127     {
 128     case MSG_HOTKEY:
 129         for (i = 0; i < r->count; i++)
 130         {
 131             if (r->texts[i].hotkey != NULL)
 132             {
 133                 int c;
 134 
 135                 c = g_ascii_tolower ((gchar) r->texts[i].hotkey[0]);
 136                 if (c != parm)
 137                     continue;
 138                 r->pos = i;
 139 
 140                 /* Take action */
 141                 send_message (w, sender, MSG_ACTION, CK_Select, data);
 142                 return MSG_HANDLED;
 143             }
 144         }
 145         return MSG_NOT_HANDLED;
 146 
 147     case MSG_KEY:
 148         return radio_key (r, parm);
 149 
 150     case MSG_ACTION:
 151         return radio_execute_cmd (r, parm);
 152 
 153     case MSG_CURSOR:
 154         widget_gotoyx (r, r->pos, 1);
 155         return MSG_HANDLED;
 156 
 157     case MSG_DRAW:
 158         {
 159             gboolean focused;
 160 
 161             focused = widget_get_state (w, WST_FOCUSED);
 162 
 163             for (i = 0; i < r->count; i++)
 164             {
 165                 widget_selectcolor (w, i == r->pos && focused, FALSE);
 166                 widget_gotoyx (w, i, 0);
 167                 tty_draw_hline (w->y + i, w->x, ' ', w->cols);
 168                 tty_print_string ((r->sel == i) ? "(*) " : "( ) ");
 169                 hotkey_draw (w, r->texts[i], i == r->pos && focused);
 170             }
 171 
 172             return MSG_HANDLED;
 173         }
 174 
 175     case MSG_DESTROY:
 176         for (i = 0; i < r->count; i++)
 177             hotkey_free (r->texts[i]);
 178         g_free (r->texts);
 179         return MSG_HANDLED;
 180 
 181     default:
 182         return widget_default_callback (w, sender, msg, parm, data);
 183     }
 184 }
 185 
 186 /* --------------------------------------------------------------------------------------------- */
 187 
 188 static void
 189 radio_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
     /* [previous][next][first][last][top][bottom][index][help]  */
 190 {
 191     switch (msg)
 192     {
 193     case MSG_MOUSE_DOWN:
 194         RADIO (w)->pos = event->y;
 195         widget_select (w);
 196         break;
 197 
 198     case MSG_MOUSE_CLICK:
 199         RADIO (w)->pos = event->y;
 200         send_message (w, NULL, MSG_ACTION, CK_Select, NULL);
 201         send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
 202         break;
 203 
 204     default:
 205         break;
 206     }
 207 }
 208 
 209 /* --------------------------------------------------------------------------------------------- */
 210 /*** public functions ****************************************************************************/
 211 /* --------------------------------------------------------------------------------------------- */
 212 
 213 WRadio *
 214 radio_new (int y, int x, int count, const char **texts)
     /* [previous][next][first][last][top][bottom][index][help]  */
 215 {
 216     WRadio *r;
 217     Widget *w;
 218     int i, wmax = 0;
 219 
 220     r = g_new (WRadio, 1);
 221     w = WIDGET (r);
 222 
 223     /* Compute the longest string */
 224     r->texts = g_new (hotkey_t, count);
 225 
 226     for (i = 0; i < count; i++)
 227     {
 228         int width;
 229 
 230         r->texts[i] = hotkey_new (texts[i]);
 231         width = hotkey_width (r->texts[i]);
 232         wmax = MAX (width, wmax);
 233     }
 234 
 235     /* 4 is width of "(*) " */
 236     widget_init (w, y, x, count, 4 + wmax, radio_callback, radio_mouse_callback);
 237     w->options |= WOP_SELECTABLE | WOP_WANT_CURSOR | WOP_WANT_HOTKEY;
 238     w->keymap = radio_map;
 239 
 240     r->pos = 0;
 241     r->sel = 0;
 242     r->count = count;
 243 
 244     return r;
 245 }
 246 
 247 /* --------------------------------------------------------------------------------------------- */

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