]> icculus.org git repositories - btb/d2x.git/blob - ui/icon.c
specify int type for state2_alt_down
[btb/d2x.git] / ui / icon.c
1 /* $Id: icon.c,v 1.4 2005-01-24 22:19:10 schaffner Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 #ifdef RCS
16 static char rcsid[] = "$Id: icon.c,v 1.4 2005-01-24 22:19:10 schaffner Exp $";
17 #endif
18
19 #ifdef HAVE_CONFIG_H
20 #include "conf.h"
21 #endif
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "u_mem.h"
27 #include "fix.h"
28 #include "pstypes.h"
29 #include "gr.h"
30 #include "ui.h"
31 #include "key.h"
32
33 #define Middle(x) ((2*(x)+1)/4)
34
35 extern void ui_draw_shad( short x1, short y1, short x2, short y2, short c1, short c2 );
36
37 void ui_draw_box_in1( short x1, short y1, short x2, short y2 )
38 {
39
40         gr_setcolor( CWHITE );
41         gr_urect( x1+1, y1+1, x2-1, y2-1 );
42
43         ui_draw_shad( x1+0, y1+0, x2-0, y2-0, CGREY, CBRIGHT );
44 }
45
46
47 void ui_draw_icon( UI_GADGET_ICON * icon )
48 {
49         int height, width, avg;
50         int x, y;
51         
52         
53         if ((icon->status==1) || (icon->position != icon->oldposition))
54         {
55                 icon->status = 0;
56
57                 ui_mouse_hide();
58         
59                 gr_set_current_canvas( icon->canvas );
60                 gr_get_string_size(icon->text, &width, &height, &avg );
61         
62                 x = ((icon->width-1)/2)-((width-1)/2);
63                 y = ((icon->height-1)/2)-((height-1)/2);
64
65                 if (icon->position==1 )
66                 {
67                         // Draw pressed
68                         ui_draw_box_in( 0, 0, icon->width, icon->height );
69                         x += 2; y += 2;
70                 }
71                 else if (icon->flag)
72                 {
73                         // Draw part out
74                         ui_draw_box_in1( 0, 0, icon->width, icon->height );
75                         x += 1; y += 1; 
76                 }
77                 else
78                 {
79                         // Draw released!
80                         ui_draw_box_out( 0, 0, icon->width, icon->height );
81                 }
82         
83                 gr_set_fontcolor( CBLACK, -1 );         
84                 gr_ustring( x, y, icon->text );
85
86                 ui_mouse_show();
87         }
88 }
89
90
91 UI_GADGET_ICON * ui_add_gadget_icon( UI_WINDOW * wnd, char * text, short x, short y, short w, short h, int k,int (*f)(void) )
92 {
93         UI_GADGET_ICON * icon;
94
95         icon = (UI_GADGET_ICON *)ui_gadget_add( wnd, 9, x, y, x+w-1, y+h-1 );
96
97         icon->width = w;
98         icon->height = h;
99         MALLOC( icon->text, char, strlen( text )+2);
100         strcpy( icon->text, text );
101         icon->trap_key = k;
102         icon->user_function = f;
103         icon->oldposition = 0;
104         icon->position = 0;
105         icon->pressed = 0;
106         icon->canvas->cv_font = ui_small_font;
107
108         // Call twice to get original;
109         if (f)
110         {
111                 icon->flag = (sbyte)f();
112                 icon->flag = (sbyte)f();
113         } else {
114                 icon->flag = 0;
115         }
116
117
118         return icon;
119
120 }
121
122 void ui_icon_do( UI_GADGET_ICON * icon, int keypress )
123 {
124         int OnMe;
125
126         OnMe = ui_mouse_on_gadget( (UI_GADGET *)icon );
127
128         icon->oldposition = icon->position;
129
130         if ( B1_PRESSED && OnMe )
131         {
132                 icon->position = 1;
133         } else  {
134                 icon->position = 0;
135         }
136
137         icon->pressed = 0;
138
139         if ((icon->position==0) && (icon->oldposition==1) && OnMe )
140                 icon->pressed = 1;
141
142         if (icon->pressed == 1 || keypress==icon->trap_key )
143         {
144                 icon->status = 1;
145                 icon->flag = (sbyte)icon->user_function();
146                 if (keypress==icon->trap_key) last_keypress = 0;
147         }
148
149         ui_draw_icon( icon );
150
151 }