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
|
/***************************************************************************
gui_button.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 <stdlib.h>
#include <string.h>
#include "gui_widget.h"
#include "gui_button.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:
/* the button surface provides for states in a
vertical order: normal, focused, clicked,
inactive */
if ( !widget->active ) {
stk_surface_blit( widget->surface,
0, (widget->screen_region.h<<1) +
widget->screen_region.h,
-1, widget->screen_region.h,
stk_display,
widget->screen_region.x,
widget->screen_region.y );
break;
}
if ( widget->focused ) {
if ( widget->pressed )
stk_surface_blit( widget->surface,
0, widget->screen_region.h<<1,
-1, widget->screen_region.h,
stk_display,
widget->screen_region.x,
widget->screen_region.y );
else
stk_surface_blit( widget->surface,
0, widget->screen_region.h,
-1, widget->screen_region.h,
stk_display,
widget->screen_region.x,
widget->screen_region.y );
}
else
stk_surface_blit( widget->surface, 0, 0,
-1, widget->screen_region.h,
stk_display,
widget->screen_region.x,
widget->screen_region.y );
break;
case GUI_BUTTON_PRESSED:
stk_sound_play( gui_theme->click_sound );
case GUI_ACTIVATED:
case GUI_DEACTIVATED:
case GUI_BUTTON_RELEASED:
case GUI_CLICKED:
case GUI_FOCUS_IN:
case GUI_FOCUS_OUT:
gui_widget_draw( widget );
break;
}
}
/*
====================================================================
PUBLICS
====================================================================
*/
/*
====================================================================
Create a button that may contain an icon and a text. Both are
displayed centered.
====================================================================
*/
GuiWidget* gui_button_create(
GuiWidget *parent, int x, int y, int width, int height,
void (*user_event_handler)(GuiWidget*,GuiEvent*),
SDL_Surface *icon, int ix, int iy, int iw, int ih,
char *caption )
{
int offset, i;
GuiWidget *widget = gui_widget_create(
parent, GUI_BUTTON, x, y, width, height,
default_event_handler, user_event_handler );
/* events */
gui_widget_enable_event( widget, GUI_CLICKED );
/* basic surface */
/* four different states in a vertical order:
NORMAL: normal wallpaper + normal button frame
FOCUSED: focus wallpaper + normal button frame
CLICKED: focus wallpaper + clicked button frame
INACTIVE: grayed NORMAL
*/
widget->surface = stk_surface_create(
SDL_SWSURFACE, width, height * 4 );
/* NORMAL */
stk_surface_apply_wallpaper(
widget->surface, 0,0,-1,height,
gui_theme->button_wallpaper_normal, -1 );
SDL_SetColorKey( widget->surface, 0,0 );
widget->border = stk_surface_apply_frame(
widget->surface, 0,0,-1,height,
gui_theme->button_frame_normal );
/* FOCUSED */
stk_surface_apply_wallpaper(
widget->surface, 0,height,-1,height,
gui_theme->button_wallpaper_focused, -1 );
stk_surface_apply_frame(
widget->surface, 0,height,-1,height,
gui_theme->button_frame_normal );
/* CLICKED */
stk_surface_apply_wallpaper(
widget->surface,
widget->border, widget->border + 2 * height,
-1,height,
gui_theme->button_wallpaper_focused, -1 );
stk_surface_apply_frame(
widget->surface, 0,2*height,-1,height,
gui_theme->button_frame_clicked );
/* INACTIVE */
stk_surface_apply_wallpaper(
widget->surface, 0,height*3,-1,height,
gui_theme->button_wallpaper_normal, -1 );
stk_surface_apply_frame(
widget->surface, 0,height*3,-1,height,
gui_theme->button_frame_normal );
stk_surface_gray( widget->surface,
0, height*3, width, height, 0 );
/* size w/o border */
widget->width -= 2 * widget->border;
widget->height -= 2 * widget->border;
/* add label and icon */
offset = width;
if ( icon )
offset -= iw;
if ( caption )
offset -= stk_font_string_width(
gui_theme->button_font, caption );
offset /= 2;
/* icon */
if ( icon ) {
for ( i = 0; i < 4; i++ )
stk_surface_alpha_blit( icon, ix, iy, iw, ih,
widget->surface,
offset + (i==2)*widget->border/2,
i * height + (height - ih) / 2 +
(i==2)*widget->border/2,
(i==3)?128:SDL_ALPHA_OPAQUE );
offset += iw;
}
if ( caption ) {
gui_theme->button_font->align = STK_FONT_ALIGN_CENTER_Y;
for ( i = 0; i < 4; i++ )
stk_font_write( gui_theme->button_font,
widget->surface,
offset + (i==2)*widget->border/2,
i * height + height / 2 +
(i==2)*widget->border/2,
(i==3)?128:SDL_ALPHA_OPAQUE, caption );
}
/* done */
return widget;
}
|