File: gui_spinbutton.c

package info (click to toggle)
lbreakout2 2.5.2-2.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 6,040 kB
  • ctags: 2,374
  • sloc: ansic: 20,911; sh: 2,891; makefile: 374
file content (256 lines) | stat: -rw-r--r-- 9,772 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/***************************************************************************
                          gui_spinbutton.c  -  description
                             -------------------
    begin                : Sat Oct 19 2002
    copyright            : (C) 2002 by Michael Speck
    email                : kulkanie@gmx.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <stdlib.h>
#include <string.h>
#include "gui_widget.h"
#include "gui_spinbutton.h"
#include "gui_button.h"
#include "gui_edit.h"

extern GuiTheme *gui_theme;
extern SDL_Surface *stk_display;

/*
====================================================================
LOCALS
====================================================================
*/

/*
====================================================================
Default event handler
====================================================================
*/
static void default_event_handler( 
    GuiWidget *widget, GuiEvent *event )
{
    switch ( event->type ) {
        case GUI_DESTROY:
            break;
        case GUI_DRAW:
            break;
    }
}

/*
====================================================================
Handle edit and restrict input to digits and minus. Check range.
The GUI_CHANGED event from the edit is blocked if an invalid
character was added.
====================================================================
*/
static void gui_spinbutton_edit_handler(
    GuiWidget *widget, GuiEvent *event )
{
    char aux[GUI_SPINBUTTON_LENGTH + 1];
    int old_value;
    if ( event->type == GUI_CHANGED ) {
        old_value = widget->parent->spec.spinbutton.value;
        gui_edit_get_text( widget, aux, 
            GUI_SPINBUTTON_LENGTH + 1, 0, -1 );
        widget->parent->spec.spinbutton.value = atoi( aux );
        /* check if we hit upper limit */
        if ( widget->parent->spec.spinbutton.value > 
             widget->parent->spec.spinbutton.max ) {
            widget->parent->spec.spinbutton.value = 
                widget->parent->spec.spinbutton.max;
            snprintf( aux, GUI_SPINBUTTON_LENGTH + 1,
                "%i", widget->parent->spec.spinbutton.value );
            gui_edit_set_text( widget, aux );
        }
        if ( old_value != widget->parent->spec.spinbutton.value )
            gui_widget_call_user_event_handler( 
                widget->parent, event );
    }
    else
        gui_widget_call_user_event_handler( 
            widget->parent, event );
}
/*
====================================================================
Handle increase/decrease buttons.
====================================================================
*/
static void gui_spinbutton_inc_handler(
    GuiWidget *widget, GuiEvent *event )
{
    if ( event->type == GUI_CLICKED )
    if ( widget->parent->spec.spinbutton.value < 
         widget->parent->spec.spinbutton.max ) {
        widget->parent->spec.spinbutton.value += 
            widget->parent->spec.spinbutton.step;
        if ( widget->parent->spec.spinbutton.value > 
             widget->parent->spec.spinbutton.max )
            widget->parent->spec.spinbutton.value = 
                widget->parent->spec.spinbutton.max;
        gui_spinbutton_set_value( widget->parent, 
            widget->parent->spec.spinbutton.value );
        gui_widget_call_user_event_handler( 
            widget->parent, 
            gui_event_get_simple( GUI_CHANGED ) );
    }
    gui_widget_call_user_event_handler( widget->parent, event );
}
static void gui_spinbutton_dec_handler(
    GuiWidget *widget, GuiEvent *event )
{
    if ( event->type == GUI_CLICKED )
    if ( widget->parent->spec.spinbutton.value > 
         widget->parent->spec.spinbutton.min ) {
        widget->parent->spec.spinbutton.value -= 
            widget->parent->spec.spinbutton.step;
        if ( widget->parent->spec.spinbutton.value <
             widget->parent->spec.spinbutton.min )
            widget->parent->spec.spinbutton.value = 
                widget->parent->spec.spinbutton.min;
        gui_spinbutton_set_value( widget->parent, 
            widget->parent->spec.spinbutton.value );
        gui_widget_call_user_event_handler( 
            widget->parent, 
            gui_event_get_simple( GUI_CHANGED ) );
    }
    gui_widget_call_user_event_handler( widget->parent, event );
}

/*
====================================================================
PUBLICS
====================================================================
*/

/*
====================================================================
Create a spinbutton which is an alphanumerical edit with an 
increase and a decrease button both the size of height x height/2
at the right end of the edit.
====================================================================
*/
GuiWidget* gui_spinbutton_create( 
    GuiWidget *parent, int x, int y, int width, int height, 
    void (*user_event_handler)(GuiWidget*,GuiEvent*),
    int min, int max, int step, int value )
{
    GuiWidget *widget = gui_widget_create(
        parent, GUI_SPINBUTTON, x, y, width, height, 
        default_event_handler, user_event_handler );
    /* events */
    gui_widget_enable_event( widget, GUI_CHANGED );
    /* build empty surface */
    widget->surface = stk_surface_create( 
        SDL_SWSURFACE, width, height );
    /* set range */
    widget->spec.spinbutton.min = min;
    widget->spec.spinbutton.max = max;
    widget->spec.spinbutton.step = step;
    /* build edit */
    widget->spec.spinbutton.edit = gui_edit_create( 
        widget, 0, 0, width - height, height, 
        gui_spinbutton_edit_handler, 2, 0, 
        GUI_SPINBUTTON_LENGTH, "undefined" );
    gui_edit_set_filter( 
        widget->spec.spinbutton.edit, GUI_EDIT_NUMERICAL );
    gui_widget_disable_event( 
        widget->spec.spinbutton.edit, GUI_FOCUS_IN );
    gui_widget_disable_event( 
        widget->spec.spinbutton.edit, GUI_FOCUS_OUT );
    /* by default edit can't be edited */
    widget->spec.spinbutton.edit->active = 0;
    /* build buttons */
    widget->spec.spinbutton.inc = gui_button_create(
        widget, width - height, 0, height, height / 2,
        gui_spinbutton_inc_handler,
        gui_theme->spinbutton_arrows, 0,0,
        gui_theme->spinbutton_arrow_size,
        gui_theme->spinbutton_arrow_size, 0 );
    gui_widget_disable_event( 
        widget->spec.spinbutton.inc, GUI_FOCUS_IN );
    gui_widget_disable_event( 
        widget->spec.spinbutton.inc, GUI_FOCUS_OUT );
    widget->spec.spinbutton.dec = gui_button_create(
        widget, width - height, height/2, height, height / 2,
        gui_spinbutton_dec_handler,
        gui_theme->spinbutton_arrows, 
        gui_theme->spinbutton_arrow_size,0,
        gui_theme->spinbutton_arrow_size,
        gui_theme->spinbutton_arrow_size, 0 );
    gui_widget_disable_event( 
        widget->spec.spinbutton.dec, GUI_FOCUS_IN );
    gui_widget_disable_event( 
        widget->spec.spinbutton.dec, GUI_FOCUS_OUT );
    /* set value */
    gui_spinbutton_set_value( widget, value );
    /* done */
    return widget;
}

/*
====================================================================
Get/set spinbutton's value. Get returns False if this was no 
spinbutton.
====================================================================
*/
void gui_spinbutton_set_value( GuiWidget *widget, int value )
{
    char aux[GUI_SPINBUTTON_LENGTH + 1];
    if ( widget->type == GUI_SPINBUTTON ) {
        widget->spec.spinbutton.value = value;
        snprintf( aux, GUI_SPINBUTTON_LENGTH + 1, "%i", value );
        gui_edit_set_text( widget->spec.spinbutton.edit, aux );
    }
}
int gui_spinbutton_get_value( GuiWidget *widget, int *value )
{
    if ( widget->type != GUI_SPINBUTTON )
        return 0;
    *value = widget->spec.spinbutton.value;
    if ( *value < widget->spec.spinbutton.min )
        *value = widget->spec.spinbutton.min;
    if ( *value > widget->spec.spinbutton.max )
        *value = widget->spec.spinbutton.max;
    return 1;
}
/*
====================================================================
Set the range of the spinbutton. Stores refresh rects if visible.
-1 for any value means to keep the current setting.
====================================================================
*/
int gui_spinbutton_set_range( 
    GuiWidget *widget, int min, int max, int step )
{
    if ( widget->type != GUI_SPINBUTTON )
        return 0;
    if ( min != -1 ) widget->spec.spinbutton.min = min;
    if ( max != -1 ) widget->spec.spinbutton.max = max;
    if ( step != -1 ) widget->spec.spinbutton.step = step;
    return 1;
}

/*
====================================================================
(De)Activate edit of spinbutton. If the entered value is greater
than the maximum it is set to maximum. If it's less than minimum
it is _not_ reset (else it would be impossible to enter new 
values) However, if the value is queried by 
gui_spinbutton_get_value() it is checked for its range.
====================================================================
*/
void gui_spinbutton_enable_edit( GuiWidget *widget, int enable )
{
    widget->spec.spinbutton.edit->active = enable;
}