-
Notifications
You must be signed in to change notification settings - Fork 0
/
kguimenu2.cpp
366 lines (328 loc) · 8.95 KB
/
kguimenu2.cpp
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*********************************************************************************/
/* kGUI - kguimenu2.cpp */
/* */
/* Initially Designed and Programmed by Kevin Pickell */
/* */
/* http://code.google.com/p/kgui/ */
/* */
/* kGUI is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU Lesser General Public License as published by*/
/* the Free Software Foundation; version 2. */
/* */
/* kGUI is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* http://www.gnu.org/licenses/lgpl.txt */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with kGUI; if not, write to the Free Software */
/* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/* */
/*********************************************************************************/
/*********************************************************************************/
/* */
/* This is a full menu class with sub-menus, not finished yet, just starting */
/* */
/*********************************************************************************/
#include "kgui.h"
#define TITLEGAPX 10
#define TITLEGAPY 4
kGUIMenuObj::kGUIMenuObj()
{
m_numentries=0;
m_depth=0;
m_track=false;
m_colhover=-1;
m_title.Init(16,4);
m_titlex.Init(16,4);
m_titlew.Init(16,4);
m_entry.Init(16,4);
m_activeentry.Init(4,4);
}
kGUIMenuObj::~kGUIMenuObj()
{
if(m_track==true)
{
kGUI::DelUpdateTask(this,CALLBACKNAME(Track));
m_track=false;
}
}
void kGUIMenuObj::SetNumEntries(unsigned int n)
{
unsigned int i;
m_numentries=n;
for(i=0;i<n;++i)
{
m_title.GetEntryPtr(i)->SetMenu(this);
m_title.GetEntryPtr(i)->SetFontInfo(this,false);
}
}
void kGUIMenuObj::Resize(void)
{
int i;
unsigned int x;
unsigned int y;
unsigned int w;
unsigned int h;
/* calc new size */
x=0;
y=0;
for(i=0;i<m_numentries;++i)
{
w=m_title.GetEntryPtr(i)->GetWidth();
h=m_title.GetEntryPtr(i)->GetLineHeight();
m_titlex.SetEntry(i,x);
m_titlew.SetEntry(i,w);
w+=TITLEGAPX+TITLEGAPX; /* left gap + right gap */
x+=w;
y=MAX(y,h);
}
SetZone(0,0,x,y+TITLEGAPY+TITLEGAPY);
}
void kGUIMenuObj::Track(void)
{
bool over;
kGUICorners c;
GetCorners(&c);
/* is the mouse over the title area? */
over=kGUI::MouseOver(&c);
if(over==false && !m_depth)
{
kGUI::DelUpdateTask(this,CALLBACKNAME(Track));
m_track=false;
m_colhover=-1;
Dirty();
}
}
bool kGUIMenuObj::UpdateInput(void)
{
int i;
kGUICorners c;
kGUICorners tc;
bool over;
GetCorners(&c);
/* is the mouse over the title area? */
over=kGUI::MouseOver(&c);
if(over)
{
/* if i'm not active then activate me */
/* I need to be active so I can track the mouse when it moves */
/* off of the header area so I can unhilight the last header under the cursor */
if(m_track==false)
{
m_track=true;
kGUI::AddUpdateTask(this,CALLBACKNAME(Track));
}
/* yes they have mouse over the tabs on the top */
/* which tab is the mouse over? (if any) */
for(i=0;i<m_numentries;++i)
{
tc.lx=c.lx+m_titlex.GetEntry(i);
tc.rx=tc.lx+m_titlew.GetEntry(i);
tc.ty=c.ty;
tc.by=c.by;
if(kGUI::MouseOver(&tc))
{
/* yes mouse is over this column */
if(i!=m_colhover)
{
Dirty();
m_colhover=i;
if(m_depth)
{
CloseMenu();
OpenMenu(m_entry.GetEntry(i),tc.lx,tc.by);
}
}
/* are they clicking on the menu header? */
if(kGUI::GetMouseClickLeft()==true)
{
kGUI::SetForceUsed(true);
if(!m_depth)
OpenMenu(m_entry.GetEntry(i),tc.lx,tc.by);
else
CloseMenu();
}
return(true);
}
}
}
return(false);
}
void kGUIMenuObj::OpenMenu(kGUIMenuColObj *menu,int x,int y)
{
kGUIMenuEntryObj *me;
/* this callback can be trapped to enable / disable menu entries */
if(!m_depth)
CallEvent(EVENT_ENTER);
m_activeentry.SetEntry(m_depth++,menu);
menu->SetEventHandler(0,0);
menu->SetEventHandler(this,CALLBACKNAME(MenuEvent));
menu->SetDrawPopRow(false);
menu->Activate(x,y);
Dirty();
/* check to see if the first entry has a sub-menu */
me=menu->GetCurrentEntry();
if(me->GetSubMenu())
{
kGUICorners c;
/* activate sub-menu */
me->GetCorners(&c);
OpenMenu(me->GetSubMenu(),c.rx-6,c.ty+2);
}
}
void kGUIMenuObj::MenuEvent(kGUIEvent *event)
{
switch(event->GetEvent())
{
case EVENT_MOUSEOFF:
/* pop the menu as the mouse is no longer over it */
if(m_depth==1)
UpdateInput();
else
{
if(event->GetObj()==this)
{
if(m_depth>1)
CloseMenu();
}
else
{
int d;
bool found;
kGUIMenuColObj *menu;
kGUIMenuEntryObj *me;
kGUICorners c;
found=false;
for(d=0;d<m_depth;++d)
{
if(event->GetObj()==m_activeentry.GetEntry(d))
{
found=true;
break;
}
}
assert(found,"Huh, object not in list!");
if(d>0)
{
menu=m_activeentry.GetEntry(d-1);
me=menu->GetCurrentEntry();
me->GetCorners(&c);
if(kGUI::MouseOver(&c)==false)
CloseMenu();
}
}
}
break;
case EVENT_MOVED:
{
int d;
bool found;
kGUIMenuColObj *menu;
kGUIMenuEntryObj *me;
found=false;
for(d=0;d<m_depth;++d)
{
if(event->GetObj()==m_activeentry.GetEntry(d))
{
found=true;
break;
}
}
assert(found,"Huh, object not in list!");
/* check for open/close submenu */
while(d<(m_depth-1))
CloseMenu();
/* open sub-menu? */
if(m_depth>0)
{
menu=m_activeentry.GetEntry(m_depth-1);
me=menu->GetCurrentEntry();
if(me->GetSubMenu())
{
kGUICorners c;
/* activate sub-menu */
me->GetCorners(&c);
OpenMenu(me->GetSubMenu(),c.rx-6,c.ty+2);
}
}
}
break;
case EVENT_SELECTED:
/* pass selected event back to parent event handler */
CallEvent(EVENT_SELECTED,event);
while(m_depth)
CloseMenu();
break;
}
}
void kGUIMenuObj::CloseMenu(void)
{
kGUIMenuColObj *col;
#if 0
kGUIMenuColObj *menu;
kGUIMenuEntryObj *me;
#endif
/* close the current open menu */
if(m_depth)
{
col=m_activeentry.GetEntry(m_depth-1);
col->Close();
--m_depth;
Dirty();
if(m_depth)
{
col=m_activeentry.GetEntry(m_depth-1);
col->ReActivate();
}
}
#if 0
/* check for open-submenu */
if(m_depth>0)
{
menu=m_activeentry.GetEntry(m_depth-1);
me=menu->GetCurrentEntry();
if(me->GetSubMenu())
{
kGUICorners c;
/* activate sub-menu */
me->GetCorners(&c);
OpenMenu(me->GetSubMenu(),c.rx-6,c.ty+2);
}
}
#endif
}
void kGUIMenuObj::Draw(void)
{
int i;
kGUICorners c;
GetCorners(&c);
// kGUI::DrawRect(c.lx,c.ty,c.rx,c.by,DrawColor(128,128,128));
for(i=0;i<m_numentries;++i)
{
if(i==m_colhover)
{
if(!m_depth)
kGUI::DrawRectFrame(c.lx+m_titlex.GetEntry(i),c.ty,c.lx+m_titlex.GetEntry(i)+m_titlew.GetEntry(i)+(TITLEGAPX<<1),c.by,DrawColor(192,192,255),DrawColor(64,64,255));
else
{
kGUI::DrawRect(c.lx+m_titlex.GetEntry(i),c.ty,c.lx+m_titlex.GetEntry(i)+m_titlew.GetEntry(i)+(TITLEGAPX<<1),c.ty+1,DrawColor(128,128,128));
kGUI::DrawRect(c.lx+m_titlex.GetEntry(i),c.ty,c.lx+m_titlex.GetEntry(i)+1,c.by,DrawColor(128,128,128));
kGUI::DrawRect(c.lx+m_titlex.GetEntry(i)+m_titlew.GetEntry(i)+(TITLEGAPX<<1)-1,c.ty,c.lx+m_titlex.GetEntry(i)+m_titlew.GetEntry(i)+(TITLEGAPX<<1),c.by,DrawColor(128,128,128));
}
}
m_title.GetEntryPtr(i)->Draw(c.lx+TITLEGAPX+m_titlex.GetEntry(i),c.ty+TITLEGAPY,0,0);
}
}
void kGUIMenuColTitleObj::StringChanged(void)
{
m_m->Resize();
m_m->Dirty();
}
void kGUIMenuColTitleObj::FontChanged(void)
{
m_m->Resize();
m_m->Dirty();
}