File: gui_progressbar.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 (186 lines) | stat: -rw-r--r-- 6,510 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
/***************************************************************************
                          gui_progressbar.c  -  description
                             -------------------
    begin                : Wed Oct 16 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 "gui_widget.h"
#include "gui_progressbar.h"

extern GuiTheme *gui_theme;
extern SDL_Surface *stk_display;

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

/*
====================================================================
Default event handler
====================================================================
*/
static char progressbar_hint[32];
static void default_event_handler( 
    GuiWidget *widget, GuiEvent *event )
{
    switch ( event->type ) {
        case GUI_DESTROY:
            break;
        case GUI_DRAW:
            /* display surface */
            stk_surface_blit( 
                widget->surface, 0,0,-1,-1, 
                stk_display, 
                widget->screen_region.x,
                widget->screen_region.y );
            /* add progress */
            stk_surface_apply_wallpaper( stk_display,
                widget->screen_region.x + widget->border,
                widget->screen_region.y + widget->border,
                widget->spec.progressbar.length,
                widget->screen_region.h - (widget->border<<1),
                widget->spec.progressbar.wallpaper, -1 );
            /* if focused add info */
            if ( widget->focused ) {
                gui_theme->progressbar_font->align = 
                    STK_FONT_ALIGN_CENTER_X | 
                    STK_FONT_ALIGN_CENTER_Y;
                snprintf( progressbar_hint, 32, "%i/%i",
                          widget->spec.progressbar.value,
                          widget->spec.progressbar.max );
                stk_font_write( gui_theme->progressbar_font, 
                    stk_display, 
                    widget->screen_region.x + 
                    widget->screen_region.w/2,
                    widget->screen_region.y + 
                    widget->screen_region.h/2,
                    -1, progressbar_hint );
            }
            break;
        case GUI_FOCUS_IN:
        case GUI_FOCUS_OUT:
            gui_widget_draw( widget );
            break;
    }
}

/*
====================================================================
Adjust length of the beam and set correct beam wallpaper.
(no update)
====================================================================
*/
static void gui_progressbar_adjust( GuiWidget *widget )
{
    int ratio = 100 * widget->spec.progressbar.value / 
                widget->spec.progressbar.max;
    /* wallpaper */
    if ( ratio > 60 )
        widget->spec.progressbar.wallpaper = 
            gui_theme->progress_high;
    else
        if ( ratio > 25 )
            widget->spec.progressbar.wallpaper = 
                gui_theme->progress_medium;
        else
            widget->spec.progressbar.wallpaper =
                gui_theme->progress_low;
    /* length */
    widget->spec.progressbar.length = 
        (widget->parent_region.w - (widget->border<<1)) *
        widget->spec.progressbar.value / widget->spec.progressbar.max;
}

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

/*
====================================================================
Create a framed progress bar.
====================================================================
*/
GuiWidget *gui_progressbar_create( 
    GuiWidget *parent, int x, int y, int width, int height,
    void (*user_event_handler)(GuiWidget*,GuiEvent*),
    int max, int value )
{
    GuiWidget *widget = gui_widget_create( 
        parent, GUI_PROGRESSBAR, x, y, width, height, 
        default_event_handler, user_event_handler );
    /* create surface, wallpaper and frame it */
    widget->surface = stk_surface_create( 
        SDL_SWSURFACE, width, height );
    SDL_SetColorKey( widget->surface, 0,0 );
    stk_surface_apply_wallpaper( widget->surface, 0,0,-1,-1,
        gui_theme->widget_wallpaper, -1 );
    widget->border = stk_surface_apply_frame(
        widget->surface, 0, 0, -1, -1, 
        gui_theme->widget_frame );
    /* size w/o border */
    widget->width -= 2 * widget->border;
    widget->height -= 2 * widget->border;
    /* set value */
    widget->spec.progressbar.max = max;
    gui_progressbar_set_value( widget, value );
    /* done */
    return widget;
}

/*
====================================================================
Get/Set value and limit of progressbar.
====================================================================
*/
void gui_progressbar_set_value( 
    GuiWidget *widget, int value )
{
    if ( widget->type != GUI_PROGRESSBAR ) return;
    /* set value */
    widget->spec.progressbar.value = value;
    gui_progressbar_adjust( widget );
    /* done */
    if ( widget->visible )
        gui_widget_draw( widget );
}
void gui_progressbar_set_max( 
    GuiWidget *widget, int max )
{
    if ( widget->type != GUI_PROGRESSBAR ) return;
    /* set max */
    widget->spec.progressbar.max = max;
    gui_progressbar_adjust( widget );
    /* done */
    if ( widget->visible )
        gui_widget_draw( widget );
}
int gui_progressbar_get_value( 
    GuiWidget *widget, int *value )
{
    if ( widget->type != GUI_PROGRESSBAR ) return 0;
    *value = widget->spec.progressbar.value;
    return 1;
}
int gui_progressbar_get_max( 
    GuiWidget *widget, int *max )
{
    if ( widget->type != GUI_PROGRESSBAR ) return 0;
    *max = widget->spec.progressbar.max;
    return 1;
}