root/lib/widget/radio.c

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

DEFINITIONS

This source file includes following definitions.
  1. radio_callback
  2. radio_mouse_callback
  3. radio_new

   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 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 /*** file scope macro definitions ****************************************************************/
  47 
  48 /*** file scope type declarations ****************************************************************/
  49 
  50 /*** file scope variables ************************************************************************/
  51 
  52 /*** file scope functions ************************************************************************/
  53 
  54 static cb_ret_t
  55 radio_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  56 {
  57     WRadio *r = RADIO (w);
  58     int i;
  59 
  60     switch (msg)
  61     {
  62     case MSG_HOTKEY:
  63         for (i = 0; i < r->count; i++)
  64         {
  65             if (r->texts[i].hotkey != NULL)
  66             {
  67                 int c;
  68 
  69                 c = g_ascii_tolower ((gchar) r->texts[i].hotkey[0]);
  70                 if (c != parm)
  71                     continue;
  72                 r->pos = i;
  73 
  74                 /* Take action */
  75                 send_message (w, sender, MSG_KEY, ' ', data);
  76                 return MSG_HANDLED;
  77             }
  78         }
  79         return MSG_NOT_HANDLED;
  80 
  81     case MSG_KEY:
  82         switch (parm)
  83         {
  84         case ' ':
  85             r->sel = r->pos;
  86             widget_set_state (w, WST_FOCUSED, TRUE);    /* Also draws the widget. */
  87             send_message (w->owner, w, MSG_NOTIFY, 0, NULL);
  88             return MSG_HANDLED;
  89 
  90         case KEY_UP:
  91         case KEY_LEFT:
  92             if (r->pos > 0)
  93             {
  94                 r->pos--;
  95                 widget_redraw (w);
  96                 return MSG_HANDLED;
  97             }
  98             return MSG_NOT_HANDLED;
  99 
 100         case KEY_DOWN:
 101         case KEY_RIGHT:
 102             if (r->count - 1 > r->pos)
 103             {
 104                 r->pos++;
 105                 widget_redraw (w);
 106                 return MSG_HANDLED;
 107             }
 108             return MSG_NOT_HANDLED;
 109 
 110         default:
 111             return MSG_NOT_HANDLED;
 112         }
 113 
 114     case MSG_CURSOR:
 115         widget_move (r, r->pos, 1);
 116         return MSG_HANDLED;
 117 
 118     case MSG_DRAW:
 119         {
 120             gboolean focused;
 121 
 122             focused = widget_get_state (w, WST_FOCUSED);
 123 
 124             for (i = 0; i < r->count; i++)
 125             {
 126                 widget_selectcolor (w, i == r->pos && focused, FALSE);
 127                 widget_move (w, i, 0);
 128                 tty_draw_hline (w->y + i, w->x, ' ', w->cols);
 129                 tty_print_string ((r->sel == i) ? "(*) " : "( ) ");
 130                 hotkey_draw (w, r->texts[i], i == r->pos && focused);
 131             }
 132 
 133             return MSG_HANDLED;
 134         }
 135 
 136     case MSG_DESTROY:
 137         for (i = 0; i < r->count; i++)
 138             release_hotkey (r->texts[i]);
 139         g_free (r->texts);
 140         return MSG_HANDLED;
 141 
 142     default:
 143         return widget_default_callback (w, sender, msg, parm, data);
 144     }
 145 }
 146 
 147 /* --------------------------------------------------------------------------------------------- */
 148 
 149 static void
 150 radio_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
     /* [previous][next][first][last][top][bottom][index][help]  */
 151 {
 152     switch (msg)
 153     {
 154     case MSG_MOUSE_DOWN:
 155         RADIO (w)->pos = event->y;
 156         widget_select (w);
 157         break;
 158 
 159     case MSG_MOUSE_CLICK:
 160         RADIO (w)->pos = event->y;
 161         send_message (w, NULL, MSG_KEY, ' ', NULL);
 162         send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
 163         break;
 164 
 165     default:
 166         break;
 167     }
 168 }
 169 
 170 /* --------------------------------------------------------------------------------------------- */
 171 /*** public functions ****************************************************************************/
 172 /* --------------------------------------------------------------------------------------------- */
 173 
 174 WRadio *
 175 radio_new (int y, int x, int count, const char **texts)
     /* [previous][next][first][last][top][bottom][index][help]  */
 176 {
 177     WRadio *r;
 178     Widget *w;
 179     int i, wmax = 0;
 180 
 181     r = g_new (WRadio, 1);
 182     w = WIDGET (r);
 183 
 184     /* Compute the longest string */
 185     r->texts = g_new (hotkey_t, count);
 186 
 187     for (i = 0; i < count; i++)
 188     {
 189         int width;
 190 
 191         r->texts[i] = parse_hotkey (texts[i]);
 192         width = hotkey_width (r->texts[i]);
 193         wmax = MAX (width, wmax);
 194     }
 195 
 196     /* 4 is width of "(*) " */
 197     widget_init (w, y, x, count, 4 + wmax, radio_callback, radio_mouse_callback);
 198     w->options |= WOP_SELECTABLE | WOP_WANT_CURSOR | WOP_WANT_HOTKEY;
 199     r->pos = 0;
 200     r->sel = 0;
 201     r->count = count;
 202 
 203     return r;
 204 }
 205 
 206 /* --------------------------------------------------------------------------------------------- */

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