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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
|
/*
* Copyright (c) 2005 Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/* $Id$ */
#include<khmapp.h>
#include<assert.h>
HWND khui_main_menu_toolbar;
int mm_last_hot_item = -1;
int mm_next_hot_item = -1;
BOOL mm_hot_track = FALSE;
#define MAX_ILIST 256
/* not the same as MENU_SIZE_ICON_* */
#define ILIST_ICON_X 16
#define ILIST_ICON_Y 15
khui_ilist * il_icon;
int il_icon_id[MAX_ILIST];
void khui_init_menu(void) {
int i;
il_icon = khui_create_ilist(ILIST_ICON_X,
ILIST_ICON_Y,
MAX_ILIST, 5, 0);
for(i=0;i<MAX_ILIST;i++)
il_icon_id[i] = -1;
}
void khui_exit_menu(void) {
khui_delete_ilist(il_icon);
}
int khui_get_icon_index(int id) {
int i;
HBITMAP hbm;
for(i=0;i<MAX_ILIST;i++)
if(il_icon_id[i] == id) {
return i;
}
hbm = LoadImage(khm_hInstance,
MAKEINTRESOURCE(id),
IMAGE_BITMAP,
ILIST_ICON_X, ILIST_ICON_Y,
LR_DEFAULTCOLOR);
i = khui_ilist_add_masked(il_icon, hbm, KHUI_TOOLBAR_BGCOLOR);
il_icon_id[i] = id;
DeleteObject(hbm);
return i;
}
void add_action_to_menu(HMENU hm, khui_action * act,
int idx, int flags) {
MENUITEMINFO mii;
wchar_t buf[MAX_RES_STRING] = L"";
wchar_t accel[MAX_RES_STRING] = L"";
assert(!act || act->cmd);
mii.cbSize = sizeof(mii);
mii.fMask = 0;
if(act == NULL) {
mii.fMask = MIIM_FTYPE;
mii.fType = MFT_SEPARATOR;
} else {
khui_menu_def * def;
if (act->caption) {
StringCbCopy(buf, sizeof(buf), act->caption);
} else {
LoadString(khm_hInstance,
act->is_caption,
buf, ARRAYLENGTH(buf));
}
if(khui_get_cmd_accel_string(act->cmd, accel,
ARRAYLENGTH(accel))) {
StringCbCat(buf, sizeof(buf), L"\t");
StringCbCat(buf, sizeof(buf), accel);
}
mii.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_ID;
mii.fType = MFT_STRING;
mii.dwTypeData = buf;
mii.cch = (int) wcslen(buf);
mii.wID = act->cmd;
if(act->state & KHUI_ACTIONSTATE_DISABLED) {
mii.fMask |= MIIM_STATE;
mii.fState = MFS_DISABLED;
} else {
mii.fState = 0;
}
if((act->type & KHUI_ACTIONTYPE_TOGGLE) &&
(act->state & KHUI_ACTIONSTATE_CHECKED)) {
mii.fMask |= MIIM_STATE;
mii.fState |= MFS_CHECKED;
}
if(act->ib_icon) {
mii.fMask |= MIIM_BITMAP;
mii.hbmpItem = HBMMENU_CALLBACK;
}
if (flags & KHUI_ACTIONREF_SUBMENU) {
def = khui_find_menu(act->cmd);
if(def) {
mii.fMask |= MIIM_SUBMENU;
mii.hSubMenu = mm_create_menu_from_def(def, FALSE);
}
}
if(flags & KHUI_ACTIONREF_DEFAULT)
mii.fState |= MFS_DEFAULT;
}
InsertMenuItem(hm,idx,TRUE,&mii);
}
static void refresh_menu(HMENU hm, khui_menu_def * def);
static void refresh_menu_item(HMENU hm, khui_action * act,
int idx, int flags) {
MENUITEMINFO mii;
mii.cbSize = sizeof(mii);
mii.fMask = 0;
if (act == NULL)
return;
else {
khui_menu_def * def;
/* first check if the menu item is there. Otherwise we need
to add it. */
mii.fMask = MIIM_STATE;
if (!GetMenuItemInfo(hm, act->cmd, FALSE, &mii)) {
/* the 1000 is fairly arbitrary, but there should be much
less menu items on a menu anyway. If there are that
many items, the system would be unusable to the extent
that the order of the items would be the least of our
worries. */
add_action_to_menu(hm, act, 1000, flags);
return;
}
mii.fMask = 0;
if(act->state & KHUI_ACTIONSTATE_DISABLED) {
mii.fMask |= MIIM_STATE;
mii.fState = MFS_DISABLED;
} else {
mii.fMask |= MIIM_STATE;
mii.fState = MFS_ENABLED;
}
if(act->type & KHUI_ACTIONTYPE_TOGGLE) {
mii.fMask |= MIIM_STATE;
if (act->state & KHUI_ACTIONSTATE_CHECKED) {
mii.fState |= MFS_CHECKED;
} else {
mii.fState |= MFS_UNCHECKED;
}
}
SetMenuItemInfo(hm, act->cmd, FALSE, &mii);
def = khui_find_menu(act->cmd);
if(def) {
MENUITEMINFO mii2;
mii2.cbSize = sizeof(mii2);
mii2.fMask = MIIM_SUBMENU;
if (GetMenuItemInfo(hm, act->cmd, FALSE, &mii2)) {
refresh_menu(mii2.hSubMenu, def);
}
}
}
}
static void refresh_menu(HMENU hm, khui_menu_def * def) {
khui_action_ref * act;
int i;
act = def->items;
i = 0;
while ((def->n_items == -1 && act->action != KHUI_MENU_END) ||
(def->n_items >= 0 && i < (int) def->n_items)) {
refresh_menu_item(hm, khui_find_action(act->action), i, act->flags);
act++; i++;
}
}
static HMENU mm_create_menu_from_def(khui_menu_def * def, BOOL main) {
HMENU hm;
khui_action_ref * act;
int i;
if (main)
hm = CreateMenu();
else
hm = CreatePopupMenu();
act = def->items;
i = 0;
while((!(def->state & KHUI_MENUSTATE_ALLOCD) && act->action != KHUI_MENU_END) ||
((def->state & KHUI_MENUSTATE_ALLOCD) && i < (int) def->n_items)) {
add_action_to_menu(hm,khui_find_action(act->action),i,act->flags);
act++; i++;
}
return hm;
}
void mm_begin_hot_track(void);
void mm_end_hot_track(void);
static void mm_show_panel_def(khui_menu_def * def, LONG x, LONG y)
{
HMENU hm;
hm = mm_create_menu_from_def(def, FALSE);
mm_hot_track = (mm_last_hot_item >= 0);
if (mm_hot_track)
mm_begin_hot_track();
TrackPopupMenuEx(hm,
TPM_LEFTALIGN | TPM_TOPALIGN |
TPM_VERPOSANIMATION,
x, y, khm_hwnd_main, NULL);
mm_last_hot_item = -1;
if (mm_hot_track)
mm_end_hot_track();
mm_hot_track = FALSE;
DestroyMenu(hm);
}
void khm_menu_show_panel(int id, LONG x, LONG y) {
khui_menu_def * def;
def = khui_find_menu(id);
if(!def)
return;
mm_show_panel_def(def, x, y);
}
LRESULT khm_menu_activate(int menu_id) {
khui_menu_def * mmdef;
int nmm;
mmdef = khui_find_menu(KHUI_MENU_MAIN);
nmm = (int) khui_action_list_length(mmdef->items);
if(menu_id == MENU_ACTIVATE_DEFAULT) {
if (mm_last_hot_item != -1)
menu_id = mm_last_hot_item;
else
menu_id = 0;
} else if(menu_id == MENU_ACTIVATE_LEFT) {
menu_id = (mm_last_hot_item > 0)?
mm_last_hot_item - 1:
((mm_last_hot_item == 0)? nmm - 1: 0);
} else if(menu_id == MENU_ACTIVATE_RIGHT) {
menu_id = (mm_last_hot_item >=0 && mm_last_hot_item < nmm - 1)?
mm_last_hot_item + 1:
0;
} else if(menu_id == MENU_ACTIVATE_NONE) {
menu_id = -1;
}
SendMessage(khui_main_menu_toolbar,
TB_SETHOTITEM,
menu_id,
0);
khm_menu_track_current();
return TRUE;
}
LRESULT khm_menu_measure_item(WPARAM wParam, LPARAM lParam) {
/* all menu icons have a fixed size */
LPMEASUREITEMSTRUCT lpm = (LPMEASUREITEMSTRUCT) lParam;
lpm->itemWidth = MENU_SIZE_ICON_X;
lpm->itemHeight = MENU_SIZE_ICON_Y;
return TRUE;
}
LRESULT khm_menu_draw_item(WPARAM wParam, LPARAM lParam) {
LPDRAWITEMSTRUCT lpd;
khui_action * act;
int resid;
int iidx;
UINT style;
lpd = (LPDRAWITEMSTRUCT) lParam;
act = khui_find_action(lpd->itemID);
resid = 0;
if((lpd->itemState & ODS_DISABLED) || (lpd->itemState & ODS_GRAYED)) {
resid = act->ib_icon_dis;
}
if(!resid)
resid = act->ib_icon;
if(!resid) /* nothing to draw */
return TRUE;
iidx = khui_get_icon_index(resid);
if(iidx == -1)
return TRUE;
style = ILD_TRANSPARENT;
if(lpd->itemState & ODS_HOTLIGHT || lpd->itemState & ODS_SELECTED) {
style |= ILD_SELECTED;
}
khui_ilist_draw(il_icon,
iidx,
lpd->hDC,
lpd->rcItem.left, lpd->rcItem.top, style);
return TRUE;
}
void khm_track_menu(int menu) {
TBBUTTON bi;
RECT r;
RECT wr;
if (menu != -1)
mm_last_hot_item = menu;
if (mm_last_hot_item != -1) {
SendMessage(khui_main_menu_toolbar,
TB_GETBUTTON,
mm_last_hot_item,
(LPARAM) &bi);
SendMessage(khui_main_menu_toolbar,
TB_GETITEMRECT,
mm_last_hot_item,
(LPARAM) &r);
GetWindowRect(khui_main_menu_toolbar, &wr);
khm_menu_show_panel(bi.idCommand, wr.left + r.left, wr.top + r.bottom);
r.left = 0;
if (mm_next_hot_item != -1) {
mm_last_hot_item = mm_next_hot_item;
mm_next_hot_item = -1;
PostMessage(khm_hwnd_main, WM_COMMAND,
MAKEWPARAM(KHUI_PACTION_MENU,0),
MAKELPARAM(mm_last_hot_item,1));
}
}
}
void khm_menu_track_current(void) {
khm_track_menu(-1);
}
LRESULT khm_menu_handle_select(WPARAM wParam, LPARAM lParam) {
if((HIWORD(wParam) == 0xffff && lParam == 0) ||
(HIWORD(wParam) & MF_POPUP)) {
/* the menu was closed */
khm_statusbar_set_part(KHUI_SBPART_INFO, NULL, NULL);
} else {
khui_action * act;
int id;
wchar_t buf[MAX_RES_STRING] = L"";
id = LOWORD(wParam);
act = khui_find_action(id);
if(act == NULL || (act->is_tooltip == 0 && act->tooltip == NULL))
khm_statusbar_set_part(KHUI_SBPART_INFO, NULL, NULL);
else {
if (act->tooltip)
StringCbCopy(buf, sizeof(buf), act->tooltip);
else
LoadString(khm_hInstance,
act->is_tooltip,
buf, ARRAYLENGTH(buf));
khm_statusbar_set_part(KHUI_SBPART_INFO, NULL, buf);
}
}
return 0;
}
HHOOK mm_hevt_hook = NULL;
HWND mm_hwnd_menu_panel = NULL;
LRESULT CALLBACK mm_event_filter(int code,
WPARAM wParam,
LPARAM lParam) {
MSG * m;
RECT r;
int x,y;
if (code == MSGF_MENU) {
/* do stuff */
m = (MSG *) lParam;
GetWindowRect(khui_main_menu_toolbar, &r);
if (m->hwnd != khm_hwnd_main)
mm_hwnd_menu_panel = m->hwnd;
switch(m->message) {
case WM_MOUSEMOVE:
x = GET_X_LPARAM(m->lParam);
y = GET_Y_LPARAM(m->lParam);
x -= r.left;
y -= r.top;
SendMessage(khui_main_menu_toolbar,
m->message,
m->wParam,
MAKELPARAM(x,y));
break;
}
}
return CallNextHookEx(mm_hevt_hook, code, wParam, lParam);
}
void mm_begin_hot_track(void) {
if (mm_hevt_hook)
UnhookWindowsHookEx(mm_hevt_hook);
mm_hevt_hook = SetWindowsHookEx(WH_MSGFILTER,
mm_event_filter,
NULL,
GetCurrentThreadId());
}
void mm_end_hot_track(void) {
if (mm_hevt_hook)
UnhookWindowsHookEx(mm_hevt_hook);
mm_hevt_hook = NULL;
mm_hwnd_menu_panel = NULL;
}
void mm_cancel_menu(void) {
if (mm_hwnd_menu_panel)
SendMessage(mm_hwnd_menu_panel, WM_CANCELMODE, 0, 0);
}
LRESULT khm_menu_notify_main(LPNMHDR notice) {
LPNMTOOLBAR nmt;
LRESULT ret = FALSE;
RECT r;
khui_menu_def * mmdef;
khui_action_ref * mm;
int nmm;
mmdef = khui_find_menu(KHUI_MENU_MAIN);
mm = mmdef->items;
nmm = (int) khui_action_list_length(mm);
GetWindowRect(khui_main_menu_toolbar, &r);
nmt = (LPNMTOOLBAR) notice;
switch(notice->code) {
case TBN_DROPDOWN:
khm_track_menu(-1);
/*
khm_menu_show_panel(nmt->iItem,
r.left + nmt->rcButton.left,
r.top + nmt->rcButton.bottom);
*/
ret = TBDDRET_DEFAULT;
break;
case TBN_HOTITEMCHANGE:
{
LPNMTBHOTITEM nmhi;
int new_item = -1;
nmhi = (LPNMTBHOTITEM) notice;
if(nmhi->dwFlags & HICF_LEAVING)
new_item = -1;
else {
int i;
for(i=0; i < nmm; i++) {
if(mm[i].action == nmhi->idNew) {
new_item = i;
break;
}
}
}
if (mm_hot_track &&
new_item != mm_last_hot_item &&
new_item != -1 &&
mm_last_hot_item != -1) {
EndMenu();
mm_next_hot_item = new_item;
}
ret = 0;
if (!mm_hot_track || new_item != -1)
mm_last_hot_item = new_item;
} break;
default:
/* hmm. what to do */
ret = FALSE;
}
return ret;
}
HMENU khui_hmenu_main = NULL;
void khm_menu_refresh_items(void) {
khui_menu_def * def;
if (!khui_hmenu_main)
return;
def = khui_find_menu(KHUI_MENU_MAIN);
refresh_menu(khui_hmenu_main, def);
DrawMenuBar(khm_hwnd_main);
}
void khm_menu_create_main(HWND parent) {
HMENU hmenu;
khui_menu_def * def;
def = khui_find_menu(KHUI_MENU_MAIN);
hmenu = mm_create_menu_from_def(def, TRUE);
SetMenu(parent, hmenu);
khui_hmenu_main = hmenu;
return;
#ifdef USE_EXPLORER_STYLE_MENU_BAR
HWND hwtb;
REBARBANDINFO rbi;
SIZE sz;
int i;
khui_menu_def * mmdef;
khui_action_ref * mm;
int nmm;
mmdef = khui_find_menu(KHUI_MENU_MAIN);
mm = mmdef->items;
nmm = (int) khui_action_list_length(mm);
hwtb = CreateWindowEx(0
#if (_WIN32_IE >= 0x0501)
| TBSTYLE_EX_MIXEDBUTTONS
#endif
,
TOOLBARCLASSNAME,
(LPWSTR) NULL,
WS_CHILD |
CCS_ADJUSTABLE |
TBSTYLE_FLAT |
TBSTYLE_AUTOSIZE |
TBSTYLE_LIST |
CCS_NORESIZE |
CCS_NOPARENTALIGN |
CCS_NODIVIDER,
0, 0, 0, 0, rebar,
(HMENU) NULL, khm_hInstance,
NULL);
if(!hwtb) {
#ifdef DEBUG
assert(FALSE);
#else
return;
#endif
}
khui_main_menu_toolbar = hwtb;
SendMessage(hwtb,
TB_BUTTONSTRUCTSIZE,
(WPARAM) sizeof(TBBUTTON),
0);
for(i=0; i<nmm; i++) {
khui_add_action_to_toolbar(hwtb,
khui_find_action(mm[i].action),
KHUI_TOOLBAR_ADD_TEXT |
KHUI_TOOLBAR_ADD_DROPDOWN |
KHUI_TOOLBAR_VARSIZE,
NULL);
}
SendMessage(hwtb,
TB_AUTOSIZE,
0,0);
SendMessage(hwtb,
TB_GETMAXSIZE,
0,
(LPARAM) &sz);
ZeroMemory(&rbi, sizeof(rbi));
rbi.cbSize = sizeof(rbi);
rbi.fMask =
RBBIM_ID |
RBBIM_STYLE |
RBBIM_CHILD |
RBBIM_CHILDSIZE |
RBBIM_SIZE |
RBBIM_IDEALSIZE;
rbi.fStyle =
RBBS_USECHEVRON;
rbi.hwndChild = hwtb;
rbi.wID = KHUI_MENU_MAIN;
rbi.cx = sz.cx;
rbi.cxMinChild = rbi.cx;
rbi.cxIdeal = rbi.cx;
rbi.cyMinChild = sz.cy;
rbi.cyChild = rbi.cyMinChild;
rbi.cyIntegral = rbi.cyMinChild;
rbi.cyMaxChild = rbi.cyMinChild;
SendMessage(rebar,
RB_INSERTBAND,
0,
(LPARAM) &rbi);
#endif
}
|