1 /***************************************************************************
2 gui_icon.c - description
3 -------------------
4 begin : Wed Oct 16 2002
5 copyright : (C) 2002 by Michael Speck
6 email : kulkanie@gmx.net
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18 #include <stdlib.h>
19 #include "gui_widget.h"
20 #include "gui_icon.h"
21
22 extern GuiTheme *gui_theme;
23 extern SDL_Surface *stk_display;
24
25 /*
26 ====================================================================
27 LOCALS
28 ====================================================================
29 */
30
31 /*
32 ====================================================================
33 Default event handler
34 ====================================================================
35 */
default_event_handler(GuiWidget * widget,GuiEvent * event)36 static void default_event_handler(
37 GuiWidget *widget, GuiEvent *event )
38 {
39 switch ( event->type ) {
40 case GUI_DESTROY:
41 break;
42 case GUI_DRAW:
43 /* display surface */
44 stk_surface_blit(
45 widget->surface, 0, 0, -1, -1,
46 stk_display,
47 widget->screen_region.x,
48 widget->screen_region.y );
49 break;
50 }
51 }
52
53 /*
54 ====================================================================
55 PUBLICS
56 ====================================================================
57 */
58
59 /*
60 ====================================================================
61 Create an icon widget that displays the icon centered and uses
62 the parent's background.
63 ====================================================================
64 */
gui_icon_create(GuiWidget * parent,int x,int y,int width,int height,int with_frame,void (* user_event_handler)(GuiWidget *,GuiEvent *),SDL_Surface * icon,int ix,int iy,int iw,int ih)65 GuiWidget *gui_icon_create(
66 GuiWidget *parent, int x, int y, int width, int height, int with_frame,
67 void (*user_event_handler)(GuiWidget*,GuiEvent*),
68 SDL_Surface *icon, int ix, int iy, int iw, int ih )
69 {
70 GuiWidget *widget = gui_widget_create(
71 parent, GUI_ICON, x, y, width, height,
72 default_event_handler, user_event_handler );
73 /* events */
74 gui_widget_enable_event( widget, GUI_CLICKED );
75 /* create surface and add frame -
76 contents is set by icon_set_icon() */
77 widget->surface = stk_surface_create(
78 SDL_SWSURFACE, width, height );
79 if ( with_frame )
80 widget->border = stk_surface_apply_frame(
81 widget->surface, 0, 0, -1, -1,
82 gui_theme->widget_frame );
83 /* size w/o border */
84 widget->width -= 2 * widget->border;
85 widget->height -= 2 * widget->border;
86 /* set icon */
87 gui_icon_set_icon( widget, icon, ix, iy, iw, ih );
88 /* done */
89 return widget;
90 }
91
92 /*
93 ====================================================================
94 Apply a new icon.
95 ====================================================================
96 */
gui_icon_set_icon(GuiWidget * widget,SDL_Surface * icon,int ix,int iy,int iw,int ih)97 void gui_icon_set_icon(
98 GuiWidget *widget, SDL_Surface *icon,
99 int ix, int iy, int iw, int ih )
100 {
101 if ( widget->type != GUI_ICON ) return;
102 /* clear widget to wallpaper */
103 gui_widget_apply_wallpaper(
104 widget, gui_theme->widget_wallpaper, -1 );
105 /* add icon */
106 if ( icon ) {
107 /* adjust size */
108 if ( iw == -1 ) iw = icon->w;
109 if ( ih == -1 ) ih = icon->h;
110 stk_surface_blit( icon, ix, iy, iw, ih,
111 widget->surface,
112 (widget->surface->w - iw)/2,
113 (widget->surface->h - ih)/2 );
114 }
115 /* done */
116 if ( widget->visible )
117 gui_widget_draw( widget );
118 }
119
120