1 | /********************************************************************** <BR>
|
---|
2 | This file is part of Crack dot Com's free source code release of
|
---|
3 | Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
|
---|
4 | information about compiling & licensing issues visit this URL</a>
|
---|
5 | <PRE> If that doesn't help, contact Jonathan Clark at
|
---|
6 | golgotha_source@usa.net (Subject should have "GOLG" in it)
|
---|
7 | ***********************************************************************/
|
---|
8 |
|
---|
9 | #include "gui/button.hh"
|
---|
10 | #include "device/kernel.hh"
|
---|
11 | #include "device/keys.hh"
|
---|
12 | #include "window/win_evt.hh"
|
---|
13 |
|
---|
14 | i4_button_class::i4_button_class(const i4_const_str *idle_context_help, // can be null
|
---|
15 | i4_window_class *child,
|
---|
16 | i4_graphical_style_class *hint,
|
---|
17 | i4_event_reaction_class *press,
|
---|
18 | i4_event_reaction_class *depress,
|
---|
19 | i4_event_reaction_class *activate,
|
---|
20 | i4_event_reaction_class *deactivate) :
|
---|
21 | i4_menu_item_class(idle_context_help,
|
---|
22 | hint,
|
---|
23 | child ? child->width()+4 : 0,
|
---|
24 | child ? child->height()+4 : 0,
|
---|
25 | press,depress,
|
---|
26 | activate,
|
---|
27 | deactivate),
|
---|
28 | decore(child)
|
---|
29 | {
|
---|
30 | popup=i4_F;
|
---|
31 | state=WAIT_CLICK;
|
---|
32 | repeat_down=i4_F;
|
---|
33 | repeat_event=0;
|
---|
34 | grabbing=i4_F;
|
---|
35 | if (child)
|
---|
36 | add_child(2,2,child);
|
---|
37 | }
|
---|
38 |
|
---|
39 | i4_button_class::~i4_button_class()
|
---|
40 | {
|
---|
41 | if (decore)
|
---|
42 | {
|
---|
43 | remove_child(decore);
|
---|
44 | delete decore;
|
---|
45 |
|
---|
46 | if (grabbing)
|
---|
47 | {
|
---|
48 | I4_ASSERT(parent, "~button/grab/and no parent");
|
---|
49 |
|
---|
50 | i4_window_request_mouse_ungrab_class ungrab(this);
|
---|
51 | i4_kernel.send_event(parent,&ungrab);
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | void i4_button_class::reparent(i4_image_class *draw_area, i4_parent_window_class *par)
|
---|
57 | {
|
---|
58 | if (grabbing && !draw_area)
|
---|
59 | {
|
---|
60 | grabbing=i4_F;
|
---|
61 | i4_window_request_mouse_ungrab_class ungrab(this);
|
---|
62 | i4_kernel.send_event(parent,&ungrab);
|
---|
63 | }
|
---|
64 | i4_menu_item_class::reparent(draw_area, par);
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | void i4_button_class::receive_event(i4_event *ev)
|
---|
69 | {
|
---|
70 | if (!disabled)
|
---|
71 | {
|
---|
72 | if (ev->type()==i4_event::OBJECT_MESSAGE)
|
---|
73 | {
|
---|
74 | CAST_PTR(oev,i4_object_message_event_class,ev);
|
---|
75 | if (oev->object==this)
|
---|
76 | {
|
---|
77 | i4_kernel.not_idle();
|
---|
78 |
|
---|
79 | if (repeat_event)
|
---|
80 | send_event(repeat_event, PRESSED);
|
---|
81 | else
|
---|
82 | send_event(send.press, PRESSED);
|
---|
83 |
|
---|
84 | time_id=i4_time_dev.request_event(this,
|
---|
85 | new i4_object_message_event_class(this),
|
---|
86 | hint->time_hint->button_repeat);
|
---|
87 | state=WAIT_REPEAT;
|
---|
88 |
|
---|
89 | } else i4_menu_item_class::receive_event(ev);
|
---|
90 | }
|
---|
91 | else if (ev->type()==i4_event::MOUSE_BUTTON_DOWN)
|
---|
92 | {
|
---|
93 | CAST_PTR(b,i4_mouse_button_down_event_class,ev);
|
---|
94 | if (b->but==i4_mouse_button_down_event_class::LEFT)
|
---|
95 | {
|
---|
96 | if (pressed)
|
---|
97 | {
|
---|
98 | do_depress();
|
---|
99 |
|
---|
100 | send_event(send.depress, DEPRESSED);
|
---|
101 | }
|
---|
102 | else
|
---|
103 | {
|
---|
104 | do_press();
|
---|
105 | send_event(send.press, PRESSED);
|
---|
106 |
|
---|
107 | if (!grabbing)
|
---|
108 | {
|
---|
109 | i4_window_request_mouse_grab_class grab(this);
|
---|
110 | i4_kernel.send_event(parent,&grab);
|
---|
111 | grabbing=i4_T;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (repeat_down)
|
---|
115 | {
|
---|
116 | i4_kernel.not_idle();
|
---|
117 | time_id=i4_time_dev.request_event(this,
|
---|
118 | new i4_object_message_event_class(this),
|
---|
119 | hint->time_hint->button_delay);
|
---|
120 | state=WAIT_DELAY;
|
---|
121 | }
|
---|
122 |
|
---|
123 | }
|
---|
124 | }
|
---|
125 | } else if (ev->type()==i4_event::MOUSE_BUTTON_UP)
|
---|
126 | {
|
---|
127 | CAST_PTR(b,i4_mouse_button_down_event_class,ev);
|
---|
128 | if (b->but==i4_mouse_button_down_event_class::LEFT)
|
---|
129 | {
|
---|
130 | if (grabbing)
|
---|
131 | {
|
---|
132 | grabbing=i4_F;
|
---|
133 | i4_window_request_mouse_ungrab_class ungrab(this);
|
---|
134 | i4_kernel.send_event(parent,&ungrab);
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (state==WAIT_DELAY || state==WAIT_REPEAT)
|
---|
138 | {
|
---|
139 | i4_time_dev.cancel_event(time_id);
|
---|
140 | state=WAIT_CLICK;
|
---|
141 | }
|
---|
142 |
|
---|
143 | if (pressed && popup)
|
---|
144 | {
|
---|
145 | do_depress();
|
---|
146 | send_event(send.depress, DEPRESSED);
|
---|
147 | }
|
---|
148 |
|
---|
149 | }
|
---|
150 | }
|
---|
151 | else if (ev->type()==i4_event::KEY_PRESS)
|
---|
152 | {
|
---|
153 | CAST_PTR(k,i4_key_press_event_class,ev);
|
---|
154 | if (k->key==I4_ENTER)
|
---|
155 | {
|
---|
156 | if (pressed)
|
---|
157 | {
|
---|
158 | do_depress();
|
---|
159 | send_event(send.depress, DEPRESSED);
|
---|
160 |
|
---|
161 | }
|
---|
162 | else
|
---|
163 | {
|
---|
164 | do_press();
|
---|
165 | send_event(send.press, PRESSED);
|
---|
166 |
|
---|
167 | }
|
---|
168 | } else if (k->key==I4_TAB)
|
---|
169 | {
|
---|
170 | i4_window_message_class t(i4_window_message_class::REQUEST_NEXT_KEY_FOCUS,this);
|
---|
171 | i4_kernel.send_event(parent, &t);
|
---|
172 | }
|
---|
173 | else if (k->key==I4_LEFT)
|
---|
174 | {
|
---|
175 | i4_window_message_class l(i4_window_message_class::REQUEST_LEFT_KEY_FOCUS,this);
|
---|
176 | i4_kernel.send_event(parent, &l);
|
---|
177 | }
|
---|
178 | else if (k->key==I4_RIGHT)
|
---|
179 | {
|
---|
180 | i4_window_message_class r(i4_window_message_class::REQUEST_RIGHT_KEY_FOCUS,this);
|
---|
181 | i4_kernel.send_event(parent, &r);
|
---|
182 | }
|
---|
183 | else if (k->key==I4_UP)
|
---|
184 | {
|
---|
185 | i4_window_message_class u(i4_window_message_class::REQUEST_UP_KEY_FOCUS,this);
|
---|
186 | i4_kernel.send_event(parent, &u);
|
---|
187 | }
|
---|
188 | else if (k->key==I4_DOWN)
|
---|
189 | {
|
---|
190 | i4_window_message_class d(i4_window_message_class::REQUEST_DOWN_KEY_FOCUS,this);
|
---|
191 | i4_kernel.send_event(parent, &d);
|
---|
192 | }
|
---|
193 | } else
|
---|
194 | i4_menu_item_class::receive_event(ev);
|
---|
195 | } else i4_menu_item_class::receive_event(ev);
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 | void i4_button_class::parent_draw(i4_draw_context_class &context)
|
---|
200 | {
|
---|
201 | local_image->add_dirty(0,0,width()-1,height()-1,context);
|
---|
202 |
|
---|
203 | i4_color_hint_class::bevel *color;
|
---|
204 | if (active)
|
---|
205 | color=&hint->color_hint->button.active;
|
---|
206 | else
|
---|
207 | color=&hint->color_hint->button.passive;
|
---|
208 |
|
---|
209 | i4_color b,d,m=color->medium;
|
---|
210 | if (!pressed)
|
---|
211 | {
|
---|
212 | b=color->bright;
|
---|
213 | d=color->dark;
|
---|
214 | } else
|
---|
215 | {
|
---|
216 | b=color->dark;
|
---|
217 | d=color->bright;
|
---|
218 | }
|
---|
219 |
|
---|
220 | local_image->bar(0,0,width()-1,0,b,context);
|
---|
221 | local_image->bar(0,0,0,height()-1,b,context);
|
---|
222 | local_image->bar(1,1,width()-2,1,m,context);
|
---|
223 | local_image->bar(1,1,1,height()-2,m,context);
|
---|
224 |
|
---|
225 | local_image->bar(2,height()-2,width()-2,height()-2,d,context);
|
---|
226 | local_image->bar(width()-2,2,width()-2,height()-2,d,context);
|
---|
227 | local_image->bar(0,height()-1,width()-1,height()-1,hint->color_hint->black,context);
|
---|
228 | local_image->bar(width()-1,0,width()-1,height()-1,hint->color_hint->black,context);
|
---|
229 |
|
---|
230 |
|
---|
231 | local_image->bar(2,2,width()-3,height()-3,color->medium,context);
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 | void i4_button_class::set_popup(i4_bool value)
|
---|
236 | {
|
---|
237 | popup=value;
|
---|
238 | }
|
---|
239 |
|
---|
240 | void i4_button_class::set_repeat_down(i4_bool value, i4_event_reaction_class *_repeat_event)
|
---|
241 | {
|
---|
242 | if (!value && (state==WAIT_DELAY || state==WAIT_REPEAT))
|
---|
243 | i4_time_dev.cancel_event(time_id);
|
---|
244 | state=WAIT_CLICK;
|
---|
245 | repeat_down=value;
|
---|
246 |
|
---|
247 | if (repeat_event) delete repeat_event;
|
---|
248 | repeat_event=_repeat_event;
|
---|
249 | }
|
---|