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