Newer
Older
static char RCSId[] = "$Id$";
static char Copyright[] = "Copyright Robert J. Amstadt, 1993";
static char Copyright2[] = "Copyright Martin Ayotte, 1993";
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xaw/SmeBSB.h>
#include <X11/Xaw/SmeLine.h>
#include <X11/Xaw/SimpleMenu.h>
#include "WinMenuButto.h"
#include "SmeMenuButto.h"
#include "windows.h"
#include "menu.h"
#include "heap.h"
#include "win.h"
#include "bitmaps/check_bitmap"
#include "bitmaps/nocheck_bitmap"
static LPMENUBAR firstMenu = NULL;
static MENUITEM *parentItem;
static MENUITEM *siblingItem;
static int lastLevel;
static int menuId = 0;
static Pixmap checkBitmap = XtUnspecifiedPixmap;
static Pixmap nocheckBitmap = XtUnspecifiedPixmap;
LPPOPUPMENU PopupMenuGetStorageHeader(HWND hwnd);
LPPOPUPMENU PopupMenuGetWindowAndStorage(HWND hwnd, WND **wndPtr);
LPMENUITEM MenuFindItem(HWND hwnd, int x, int y, WORD *lpRet);
LPMENUITEM MenuFindItemBySelKey(HWND hwnd, WORD key, WORD *lpRet);
void PopupMenuCalcSize(HWND hwnd);
void MenuBarCalcSize(HWND hwnd);
WORD GetSelectionKey(LPSTR str);
LPSTR GetShortCutString(LPSTR str);
WORD GetShortCutPos(LPSTR str);
BOOL HideAllSubPopupMenu(LPPOPUPMENU menu);
WORD * ParseMenuResource(WORD *first_item, int level, HMENU hMenu);
void SetMenuLogicalParent(HMENU hMenu, HWND hWnd);
/***********************************************************************
* PopupMenuWndProc
*/
LONG PopupMenuWndProc( HWND hwnd, WORD message, WORD wParam, LONG lParam )
{
CREATESTRUCT *createStruct;
WORD wRet;
short x, y;
WND *wndPtr;
LPMENUITEM lpitem, lpitem2;
HMENU hSubMenu;
RECT rect;
HDC hDC;
switch(message)
{
case WM_CREATE:
#ifdef DEBUG_MENU
printf("PopupMenu WM_CREATE lParam=%08X !\n", lParam);
#endif
createStruct = (CREATESTRUCT *)lParam;
lppop = (LPPOPUPMENU)createStruct->lpCreateParams;
wndPtr = WIN_FindWndPtr(hwnd);
*((LPPOPUPMENU *)&wndPtr->wExtra[1]) = lppop;
#ifdef DEBUG_MENU
printf("PopupMenu WM_CREATE lppop=%08X !\n", lppop);
#endif
/*
if (lppop->BarFlags == 0) {
PopupMenuCalcSize(hwnd);
printf("PopupMenu WM_CREATE Width=%d Height=%d !\n",
lppop->Width, lppop->Height);
SetWindowPos(hwnd, 0, 0, 0, lppop->Width + 2, lppop->Height,
SWP_NOZORDER | SWP_NOMOVE);
}
else {
MenuBarCalcSize(hwnd);
SetWindowPos(hwnd, 0, 0, -16, lppop->Width, lppop->Height,
SWP_NOZORDER);
}
*/
if (hStdCheck == (HBITMAP)NULL)
hStdCheck = LoadBitmap((HANDLE)NULL, (LPSTR)OBM_CHECK);
if (hStdMnArrow == (HBITMAP)NULL)
hStdMnArrow = LoadBitmap((HANDLE)NULL, (LPSTR)OBM_MNARROW);
return 0;
case WM_DESTROY:
lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
#ifdef DEBUG_MENU
printf("PopupMenu WM_DESTROY %lX !\n", lppop);
#endif
return 0;
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
case WM_COMMAND:
lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
#ifdef DEBUG_MENU
printf("PopupMenu // push to lower parent WM_COMMAND !\n");
#endif
if (lppop->hWndParent != (HWND)NULL)
SendMessage(lppop->hWndParent, WM_COMMAND, wParam, lParam);
else
SendMessage(lppop->ownerWnd, WM_COMMAND, wParam, lParam);
if (lppop->BarFlags == 0) ShowWindow(hwnd, SW_HIDE);
break;
case WM_SHOWWINDOW:
lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
if (wParam == 0) {
HideAllSubPopupMenu(lppop);
#ifdef DEBUG_MENU
printf("PopupMenu WM_SHOWWINDOW -> HIDE!\n");
#endif
break;
}
lppop->FocusedItem = (WORD)-1;
if (lppop->BarFlags == 0) {
PopupMenuCalcSize(hwnd);
#ifdef DEBUG_MENU
printf("PopupMenu WM_SHOWWINDOW Width=%d Height=%d !\n",
lppop->Width, lppop->Height);
#endif
SetWindowPos(hwnd, 0, 0, 0, lppop->Width + 2, lppop->Height,
SWP_NOZORDER | SWP_NOMOVE);
}
else {
MenuBarCalcSize(hwnd);
#ifdef DEBUG_MENU
printf("MenuBarMenu WM_SHOWWINDOW Width=%d Height=%d !\n",
lppop->Width, lppop->Height);
#endif
SetWindowPos(hwnd, 0, 0, -16, lppop->Width, lppop->Height,
SWP_NOZORDER);
}
break;
case WM_LBUTTONDOWN:
lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
SetCapture(hwnd);
lpitem = MenuFindItem(hwnd, LOWORD(lParam), HIWORD(lParam), &wRet);
#ifdef DEBUG_MENU
printf("PopupMenu WM_LBUTTONDOWN wRet=%d lpitem=%08X !\n", wRet, lpitem);
if (lppop->FocusedItem != (WORD)-1) {
HideAllSubPopupMenu(lppop);
lpitem2 = GetMenuItemPtr(lppop, lppop->FocusedItem);
if (((lpitem2->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
((lpitem2->item_flags & MF_MENUBREAK) != MF_MENUBREAK)) {
hDC = GetDC(hwnd);
InvertRect(hDC, &lpitem2->rect);
ReleaseDC(hwnd, hDC);
}
}
lppop->FocusedItem = wRet;
if (((lpitem->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
((lpitem->item_flags & MF_MENUBREAK) != MF_MENUBREAK)) {
hDC = GetDC(hwnd);
InvertRect(hDC, &lpitem->rect);
ReleaseDC(hwnd, hDC);
}
if ((lpitem->item_flags & MF_POPUP) == MF_POPUP) {
hSubMenu = (HMENU)lpitem->item_id;
lppop2 = (LPPOPUPMENU) GlobalLock(hSubMenu);
if (lppop2 == NULL) break;
lppop2->hWndParent = hwnd;
GetClientRect(hwnd, &rect);
if (lppop->BarFlags != 0) {
y = rect.bottom - rect.top;
TrackPopupMenu(hSubMenu, TPM_LEFTBUTTON,
lpitem->rect.left, 0,
0, lppop->ownerWnd, (LPRECT)NULL);
}
else {
x = rect.right;
GetWindowRect(hwnd, &rect);
x += rect.left;
TrackPopupMenu(hSubMenu, TPM_LEFTBUTTON,
x, lpitem->rect.top,
0, lppop->ownerWnd, (LPRECT)NULL);
}
break;
}
}
break;
case WM_LBUTTONUP:
lppop = PopupMenuGetStorageHeader(hwnd);
ReleaseCapture();
lpitem = MenuFindItem(hwnd, LOWORD(lParam), HIWORD(lParam), &wRet);
#ifdef DEBUG_MENU
printf("PopupMenu WM_LBUTTONUP wRet=%d lpitem=%08X !\n", wRet, lpitem);
if (lpitem != NULL) {
if ((lpitem->item_flags & MF_POPUP) == MF_POPUP) {
break;
}
if (((lpitem->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
((lpitem->item_flags & MF_POPUP) != MF_POPUP)) {
ShowWindow(lppop->hWnd, SW_HIDE);
if (lppop->hWndParent != (HWND)NULL)
SendMessage(lppop->hWndParent, WM_COMMAND,
lpitem->item_id, 0L);
else
SendMessage(lppop->ownerWnd, WM_COMMAND,
lpitem->item_id, 0L);
#ifdef DEBUG_MENU
printf("PopupMenu // SendMessage WM_COMMAND wParam=%d !\n",
lpitem->item_id);
if (lppop->FocusedItem != (WORD)-1) {
HideAllSubPopupMenu(lppop);
lpitem2 = GetMenuItemPtr(lppop, lppop->FocusedItem);
if (((lpitem2->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
((lpitem2->item_flags & MF_MENUBREAK) != MF_MENUBREAK)) {
hDC = GetDC(hwnd);
InvertRect(hDC, &lpitem2->rect);
ReleaseDC(hwnd, hDC);
}
}
break;
case WM_MOUSEMOVE:
if ((wParam & MK_LBUTTON) != 0) {
lppop = PopupMenuGetStorageHeader(hwnd);
lpitem = MenuFindItem(hwnd, LOWORD(lParam), HIWORD(lParam), &wRet);
if ((lpitem != NULL) && (lppop->FocusedItem != wRet)) {
lpitem2 = GetMenuItemPtr(lppop, lppop->FocusedItem);
hDC = GetDC(hwnd);
if (((lpitem2->item_flags & MF_POPUP) == MF_POPUP) ||
((lpitem2->item_flags & MF_STRING) == MF_STRING)) {
InvertRect(hDC, &lpitem2->rect);
}
if ((lpitem2->item_flags & MF_POPUP) == MF_POPUP) {
HideAllSubPopupMenu(lppop);
}
if (((lpitem->item_flags & MF_POPUP) == MF_POPUP) ||
((lpitem->item_flags & MF_STRING) == MF_STRING)) {
InvertRect(hDC, &lpitem->rect);
}
if ((lpitem->item_flags & MF_POPUP) == MF_POPUP) {
hSubMenu = (HMENU)lpitem->item_id;
lppop2 = (LPPOPUPMENU) GlobalLock(hSubMenu);
if (lppop2 == NULL) break;
if (lppop->BarFlags != 0) {
lppop2->hWndParent = hwnd;
GetClientRect(hwnd, &rect);
y = rect.bottom - rect.top;
TrackPopupMenu(hSubMenu, TPM_LEFTBUTTON,
lpitem->rect.left, 0,
0, lppop->ownerWnd, (LPRECT)NULL);
}
}
ReleaseDC(hwnd, hDC);
}
}
break;
case WM_KEYDOWN:
case WM_KEYUP:
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
if (lParam < 0L) break;
lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
if (lppop->FocusedItem == (WORD)-1) {
if (wParam == VK_UP || wParam == VK_DOWN ||
wParam == VK_LEFT || wParam == VK_RIGHT) {
hDC = GetDC(hwnd);
lppop->FocusedItem = 0;
lpitem = GetMenuItemPtr(lppop, lppop->FocusedItem);
if (((lpitem->item_flags & MF_POPUP) == MF_POPUP) ||
((lpitem->item_flags & MF_STRING) == MF_STRING)) {
InvertRect(hDC, &lpitem->rect);
}
ReleaseDC(hwnd, hDC);
}
break;
}
switch(wParam) {
case VK_UP:
if (lppop->BarFlags != 0) break;
if (lppop->FocusedItem < 1) break;
lpitem = GetMenuItemPtr(lppop, lppop->FocusedItem);
if ((lpitem->item_flags & MF_POPUP) == MF_POPUP)
HideAllSubPopupMenu(lppop);
hDC = GetDC(hwnd);
if (((lpitem->item_flags & MF_POPUP) == MF_POPUP) ||
((lpitem->item_flags & MF_STRING) == MF_STRING)) {
InvertRect(hDC, &lpitem->rect);
}
lppop->FocusedItem--;
lpitem = GetMenuItemPtr(lppop, lppop->FocusedItem);
if (((lpitem->item_flags & MF_POPUP) == MF_POPUP) ||
((lpitem->item_flags & MF_STRING) == MF_STRING)) {
InvertRect(hDC, &lpitem->rect);
}
ReleaseDC(hwnd, hDC);
break;
case VK_DOWN:
if (lppop->BarFlags != 0) goto ProceedSPACE;
if (lppop->FocusedItem >= lppop->nItems - 1) break;
lpitem = GetMenuItemPtr(lppop, lppop->FocusedItem);
if ((lpitem->item_flags & MF_POPUP) == MF_POPUP)
HideAllSubPopupMenu(lppop);
hDC = GetDC(hwnd);
if (((lpitem->item_flags & MF_POPUP) == MF_POPUP) ||
((lpitem->item_flags & MF_STRING) == MF_STRING)) {
InvertRect(hDC, &lpitem->rect);
}
lppop->FocusedItem++;
lpitem = GetMenuItemPtr(lppop, lppop->FocusedItem);
if (((lpitem->item_flags & MF_POPUP) == MF_POPUP) ||
((lpitem->item_flags & MF_STRING) == MF_STRING)) {
InvertRect(hDC, &lpitem->rect);
}
ReleaseDC(hwnd, hDC);
break;
case VK_LEFT:
if (lppop->BarFlags == 0) {
if (lppop->hWndParent != 0)
SendMessage(lppop->hWndParent, WM_KEYDOWN, wParam, lParam);
break;
}
if (lppop->FocusedItem < 1) break;
lpitem = GetMenuItemPtr(lppop, lppop->FocusedItem);
if ((lpitem->item_flags & MF_POPUP) == MF_POPUP)
HideAllSubPopupMenu(lppop);
hDC = GetDC(hwnd);
if (((lpitem->item_flags & MF_POPUP) == MF_POPUP) ||
((lpitem->item_flags & MF_STRING) == MF_STRING)) {
InvertRect(hDC, &lpitem->rect);
}
lppop->FocusedItem--;
lpitem = GetMenuItemPtr(lppop, lppop->FocusedItem);
if (((lpitem->item_flags & MF_POPUP) == MF_POPUP) ||
((lpitem->item_flags & MF_STRING) == MF_STRING)) {
InvertRect(hDC, &lpitem->rect);
}
ReleaseDC(hwnd, hDC);
break;
case VK_RIGHT:
if (lppop->BarFlags == 0) {
if (lppop->hWndParent != 0)
SendMessage(lppop->hWndParent, WM_KEYDOWN, wParam, lParam);
break;
}
if (lppop->FocusedItem >= lppop->nItems - 1) break;
lpitem = GetMenuItemPtr(lppop, lppop->FocusedItem);
if ((lpitem->item_flags & MF_POPUP) == MF_POPUP)
HideAllSubPopupMenu(lppop);
hDC = GetDC(hwnd);
if (((lpitem->item_flags & MF_POPUP) == MF_POPUP) ||
((lpitem->item_flags & MF_STRING) == MF_STRING)) {
InvertRect(hDC, &lpitem->rect);
}
lppop->FocusedItem++;
lpitem = GetMenuItemPtr(lppop, lppop->FocusedItem);
if (((lpitem->item_flags & MF_POPUP) == MF_POPUP) ||
((lpitem->item_flags & MF_STRING) == MF_STRING)) {
InvertRect(hDC, &lpitem->rect);
}
ReleaseDC(hwnd, hDC);
break;
case VK_RETURN:
case VK_SPACE:
ProceedSPACE:
printf("PopupMenu VK_SPACE !\n");
lpitem = GetMenuItemPtr(lppop, lppop->FocusedItem);
if ((lpitem->item_flags & MF_POPUP) == MF_POPUP) {
hSubMenu = (HMENU)lpitem->item_id;
lppop2 = (LPPOPUPMENU) GlobalLock(hSubMenu);
if (lppop2 == NULL) break;
lppop2->hWndParent = hwnd;
GetClientRect(hwnd, &rect);
if (lppop->BarFlags != 0) {
y = rect.bottom - rect.top;
TrackPopupMenu(hSubMenu, TPM_LEFTBUTTON,
lpitem->rect.left, 0,
0, lppop->ownerWnd, (LPRECT)NULL);
}
else {
x = rect.right;
GetWindowRect(hwnd, &rect);
x += rect.left;
TrackPopupMenu(hSubMenu, TPM_LEFTBUTTON,
x, lpitem->rect.top,
0, lppop->ownerWnd, (LPRECT)NULL);
}
break;
}
if (((lpitem->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
((lpitem->item_flags & MF_POPUP) != MF_POPUP)) {
ShowWindow(lppop->hWnd, SW_HIDE);
if (lppop->hWndParent != (HWND)NULL)
SendMessage(lppop->hWndParent, WM_COMMAND,
lpitem->item_id, 0L);
else
SendMessage(lppop->ownerWnd, WM_COMMAND,
lpitem->item_id, 0L);
#ifdef DEBUG_MENU
printf("PopupMenu // SendMessage WM_COMMAND wParam=%d !\n",
lpitem->item_id);
#endif
}
break;
}
break;
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
if (wParam == VK_ESCAPE) {
if (lppop->hWndParent != 0) {
lppop2 = PopupMenuGetWindowAndStorage(
lppop->hWndParent, &wndPtr);
HideAllSubPopupMenu(lppop2);
break;
}
if (lppop->FocusedItem != (WORD)-1) {
lpitem2 = GetMenuItemPtr(lppop, lppop->FocusedItem);
hDC = GetDC(hwnd);
if (((lpitem2->item_flags & MF_POPUP) == MF_POPUP) ||
((lpitem2->item_flags & MF_STRING) == MF_STRING)) {
InvertRect(hDC, &lpitem2->rect);
}
ReleaseDC(hwnd, hDC);
lppop->FocusedItem = (WORD)-1;
}
}
if (wParam >= 'a' && wParam <= 'z') wParam -= 'a' - 'A';
lpitem = MenuFindItemBySelKey(hwnd, wParam, &wRet);
if (lpitem != NULL) {
printf("Found wRet=%d !\n", wRet);
if (lppop->FocusedItem != (WORD)-1) {
lpitem2 = GetMenuItemPtr(lppop, lppop->FocusedItem);
if ((lpitem2->item_flags & MF_POPUP) == MF_POPUP)
HideAllSubPopupMenu(lppop);
hDC = GetDC(hwnd);
if (((lpitem2->item_flags & MF_POPUP) == MF_POPUP) ||
((lpitem2->item_flags & MF_STRING) == MF_STRING)) {
InvertRect(hDC, &lpitem2->rect);
}
ReleaseDC(hwnd, hDC);
}
lppop->FocusedItem = wRet;
goto ProceedSPACE;
}
break;
lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
if (lppop->BarFlags != 0) {
MenuBarCalcSize(hwnd);
printf("PopupMenu WM_PAINT Width=%d Height=%d !\n",
lppop->Width, lppop->Height);
StdDrawMenuBar(hwnd);
}
else{
PopupMenuCalcSize(hwnd);
StdDrawPopupMenu(hwnd);
}
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
break;
default:
return DefWindowProc( hwnd, message, wParam, lParam );
}
return(0);
}
LPPOPUPMENU PopupMenuGetWindowAndStorage(HWND hwnd, WND **wndPtr)
{
WND *Ptr;
LPPOPUPMENU lppop;
*(wndPtr) = Ptr = WIN_FindWndPtr(hwnd);
if (Ptr == 0) {
printf("Bad Window handle on PopupMenu !\n");
return 0;
}
lppop = *((LPPOPUPMENU *)&Ptr->wExtra[1]);
return lppop;
}
LPPOPUPMENU PopupMenuGetStorageHeader(HWND hwnd)
{
WND *Ptr;
LPPOPUPMENU lppop;
Ptr = WIN_FindWndPtr(hwnd);
if (Ptr == 0) {
printf("Bad Window handle on PopupMenu !\n");
return 0;
}
lppop = *((LPPOPUPMENU *)&Ptr->wExtra[1]);
return lppop;
}
void SetMenuLogicalParent(HMENU hMenu, HWND hWnd)
{
LPPOPUPMENU lppop;
lppop = (LPPOPUPMENU)GlobalLock(hMenu);
lppop->hWndParent = hWnd;
}
void StdDrawPopupMenu(HWND hwnd)
{
WND *wndPtr;
LPPOPUPMENU lppop;
LPMENUITEM lpitem;
PAINTSTRUCT ps;
HBRUSH hBrush;
HPEN hOldPen;
HWND hWndParent;
HDC hDC, hMemDC;
RECT rect, rect2, rect3;
hDC = BeginPaint( hwnd, &ps );
if (!IsWindowVisible(hwnd)) {
EndPaint( hwnd, &ps );
return;
}
lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
if (lppop == NULL) goto EndOfPaint;
GetClientRect(hwnd, &rect);
GetClientRect(hwnd, &rect2);
FillRect(hDC, &rect, hBrush);
if (lpitem == NULL) goto EndOfPaint;
for(i = 0; i < lppop->nItems; i++) {
if ((lpitem->item_flags & MF_SEPARATOR) == MF_SEPARATOR) {
CopyRect(&rect2, &lpitem->rect);
hOldPen = SelectObject(hDC, GetStockObject(BLACK_PEN));
MoveTo(hDC, rect2.left, rect2.top + 1);
LineTo(hDC, rect2.right, rect2.top + 1);
SelectObject(hDC, hOldPen);
}
if ((lpitem->item_flags & MF_BITMAP) == MF_BITMAP) {
hBitMap = (HBITMAP)LOWORD((LONG)lpitem->item_text);
CopyRect(&rect2, &lpitem->rect);
hMemDC = CreateCompatibleDC(hDC);
SelectObject(hMemDC, hBitMap);
GetObject(hBitMap, sizeof(BITMAP), (LPSTR)&bm);
BitBlt(hDC, rect2.left, rect2.top,
bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
DeleteDC(hMemDC);
}
if (((lpitem->item_flags & MF_BITMAP) != MF_BITMAP) &&
((lpitem->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
((lpitem->item_flags & MF_MENUBREAK) != MF_MENUBREAK)) {
hOldFont = SelectObject(hDC, GetStockObject(SYSTEM_FONT));
if ((lpitem->item_flags & MF_DISABLED) == MF_DISABLED)
OldTextColor = SetTextColor(hDC, 0x00C0C0C0L);
else
OldTextColor = SetTextColor(hDC, 0x00000000L);
CopyRect(&rect3, &lpitem->rect);
InflateRect(&rect3, 0, -2);
if ((x = GetShortCutPos(lpitem->item_text)) != (WORD)-1) {
DrawText(hDC, lpitem->item_text, x, &rect3,
DT_LEFT | DT_VCENTER | DT_SINGLELINE);
DrawText(hDC, &lpitem->item_text[x], -1, &rect3,
DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
}
else
DrawText(hDC, lpitem->item_text, -1, &rect3,
DT_LEFT | DT_VCENTER | DT_SINGLELINE);
SetTextColor(hDC, OldTextColor);
SelectObject(hDC, hOldFont);
CopyRect(&rect2, &lpitem->rect);
}
if ((lpitem->item_flags & MF_CHECKED) == MF_CHECKED) {
CopyRect(&rect3, &rect2);
rect3.left = rect3.right - rect3.bottom + rect3.top;
hMemDC = CreateCompatibleDC(hDC);
if (lpitem->hCheckBit == 0)
SelectObject(hMemDC, hStdCheck);
else
SelectObject(hMemDC, lpitem->hCheckBit);
GetObject(hStdCheck, sizeof(BITMAP), (LPSTR)&bm);
BitBlt(hDC, rect3.left, rect3.top,
bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
DeleteDC(hMemDC);
printf("StdDrawPopupMenu // MF_CHECKED hStdCheck=%04X !\n", hStdCheck);
}
else {
if (lpitem->hUnCheckBit != 0)
SelectObject(hMemDC, lpitem->hUnCheckBit);
}
if ((lpitem->item_flags & MF_POPUP) == MF_POPUP) {
CopyRect(&rect3, &rect2);
rect3.left = rect3.right - rect3.bottom + rect3.top;
hMemDC = CreateCompatibleDC(hDC);
SelectObject(hMemDC, hStdMnArrow);
GetObject(hStdMnArrow, sizeof(BITMAP), (LPSTR)&bm);
BitBlt(hDC, rect3.left, rect3.top,
bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
DeleteDC(hMemDC);
}
if (lpitem->next == NULL) goto EndOfPaint;
lpitem = (LPMENUITEM)lpitem->next;
}
EndOfPaint:
EndPaint( hwnd, &ps );
}
{
WND *wndPtr;
LPPOPUPMENU lppop;
LPMENUITEM lpitem;
PAINTSTRUCT ps;
HBRUSH hBrush;
HPEN hOldPen;
HWND hWndParent;
HDC hDC, hMemDC;
RECT rect, rect2, rect3;
HFONT hOldFont;
UINT i, textwidth;
hDC = BeginPaint( hwnd, &ps );
if (!IsWindowVisible(hwnd)) {
EndPaint( hwnd, &ps );
return;
}
hOldFont = SelectObject(hDC, GetStockObject(SYSTEM_FONT));
if (lppop == NULL) goto EndOfPaint;
hBrush = GetStockObject(WHITE_BRUSH);
FillRect(hDC, &rect, hBrush);
FrameRect(hDC, &rect, GetStockObject(BLACK_BRUSH));
if (lppop->nItems == 0) goto EndOfPaint;
lpitem = lppop->firstItem;
if (lpitem == NULL) goto EndOfPaint;
for(i = 0; i < lppop->nItems; i++) {
if ((lpitem->item_flags & MF_BITMAP) == MF_BITMAP) {
hBitMap = (HBITMAP)LOWORD((LONG)lpitem->item_text);
CopyRect(&rect2, &lpitem->rect);
hMemDC = CreateCompatibleDC(hDC);
SelectObject(hMemDC, hBitMap);
BitBlt(hDC, rect2.left, rect2.top,
bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
DeleteDC(hMemDC);
}
if (((lpitem->item_flags & MF_BITMAP) != MF_BITMAP) &&
((lpitem->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
((lpitem->item_flags & MF_MENUBREAK) != MF_MENUBREAK)) {
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
CopyRect(&rect2, &lpitem->rect);
DrawText(hDC, lpitem->item_text, -1, &rect2,
DT_LEFT | DT_VCENTER | DT_SINGLELINE);
}
if ((lpitem->item_flags & MF_CHECKED) == MF_CHECKED) {
CopyRect(&rect3, &rect2);
rect3.left = rect3.right - rect3.bottom + rect3.top;
hMemDC = CreateCompatibleDC(hDC);
SelectObject(hMemDC, hStdCheck);
GetObject(hBitMap, sizeof(BITMAP), (LPSTR)&bm);
BitBlt(hDC, rect3.left, rect3.top,
bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
DeleteDC(hMemDC);
}
if (lpitem->next == NULL) goto EndOfPaint;
lpitem = (LPMENUITEM)lpitem->next;
}
EndOfPaint:
SelectObject(hDC, hOldFont);
EndPaint( hwnd, &ps );
}
LPMENUITEM MenuFindItem(HWND hwnd, int x, int y, WORD *lpRet)
{
WND *wndPtr;
LPPOPUPMENU lppop;
LPMENUITEM lpitem;
UINT i;
lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
if (lppop == NULL) return NULL;
if (lppop->nItems == 0) return NULL;
lpitem = lppop->firstItem;
for(i = 0; i < lppop->nItems; i++) {
if (lpitem == NULL) return NULL;
#ifdef DEBUG_MENUFINDITEM
printf("FindItem // left=%d top=%d right=%d bottom=%d\n",
lpitem->rect.left, lpitem->rect.top,
lpitem->rect.right, lpitem->rect.bottom);
#endif
if (x > lpitem->rect.left && x < lpitem->rect.right &&
y > lpitem->rect.top && y < lpitem->rect.bottom) {
if (lpRet != NULL) *lpRet = i;
return lpitem;
lpitem = (LPMENUITEM)lpitem->next;
}
return NULL;
}
LPMENUITEM MenuFindItemBySelKey(HWND hwnd, WORD key, WORD *lpRet)
{
WND *wndPtr;
LPPOPUPMENU lppop;
LPMENUITEM lpitem;
UINT i;
lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
if (lppop == NULL) return NULL;
if (lppop->nItems == 0) return NULL;
lpitem = lppop->firstItem;
for(i = 0; i < lppop->nItems; i++) {
if (lpitem == NULL) return NULL;
#ifdef DEBUG_MENUFINDITEM
printf("FindItemBySelKey // key=%d lpitem->sel_key=%d\n",
key, lpitem->sel_key);
#endif
if (key == lpitem->sel_key) {
if (lpRet != NULL) *lpRet = i;
return lpitem;
}
lpitem = (LPMENUITEM)lpitem->next;
}
return NULL;
}
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
void PopupMenuCalcSize(HWND hwnd)
{
WND *wndPtr;
LPPOPUPMENU lppop;
LPMENUITEM lpitem;
HDC hDC;
RECT rect;
HBITMAP hBitMap;
BITMAP bm;
HFONT hOldFont;
UINT i, OldWidth, TempWidth;
DWORD dwRet;
lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
if (lppop == NULL) return;
if (lppop->nItems == 0) return;
hDC = GetDC(hwnd);
lppop->Width = 20;
hOldFont = SelectObject(hDC, GetStockObject(SYSTEM_FONT));
CalcAGAIN:
OldWidth = lppop->Width;
SetRect(&rect, 1, 1, OldWidth, 0);
lpitem = lppop->firstItem;
for(i = 0; i < lppop->nItems; i++) {
if (lpitem == NULL) break;
rect.right = rect.left + lppop->Width;
if ((lpitem->item_flags & MF_SEPARATOR) == MF_SEPARATOR) {
rect.bottom = rect.top + 3;
}
if ((lpitem->item_flags & MF_BITMAP) == MF_BITMAP) {
hBitMap = (HBITMAP)LOWORD((LONG)lpitem->item_text);
GetObject(hBitMap, sizeof(BITMAP), (LPSTR)&bm);
rect.bottom = rect.top + bm.bmHeight;
lppop->Width = max(lppop->Width, bm.bmWidth);
}
if (((lpitem->item_flags & MF_BITMAP) != MF_BITMAP) &&
((lpitem->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
((lpitem->item_flags & MF_MENUBREAK) != MF_MENUBREAK)) {
dwRet = GetTextExtent(hDC, (char *)lpitem->item_text,
strlen((char *)lpitem->item_text));
rect.bottom = rect.top + HIWORD(dwRet);
InflateRect(&rect, 0, 2);
TempWidth = LOWORD(dwRet);
if (GetShortCutPos(lpitem->item_text) != (WORD)-1)
TempWidth += 15;
lppop->Width = max(lppop->Width, TempWidth);
}
CopyRect(&lpitem->rect, &rect);
rect.top = rect.bottom;
lpitem = (LPMENUITEM)lpitem->next;
}
if (OldWidth < lppop->Width) goto CalcAGAIN;
lppop->Height = rect.bottom;
#ifdef DEBUG_MENUCALC
printf("PopupMenuCalcSize w=%d h=%d !\n",
lppop->Width, lppop->Height);
#endif
SelectObject(hDC, hOldFont);
ReleaseDC(0, hDC);
}
void MenuBarCalcSize(HWND hwnd)
{
WND *wndPtr;
LPPOPUPMENU lppop;
LPMENUITEM lpitem;
HDC hDC;
RECT rect;
HBITMAP hBitMap;
BITMAP bm;
HFONT hOldFont;
UINT i, OldHeight;
DWORD dwRet;
lppop = PopupMenuGetWindowAndStorage(hwnd, &wndPtr);
if (lppop == NULL) return;
if (lppop->nItems == 0) return;
hDC = GetDC(hwnd);
hOldFont = SelectObject(hDC, GetStockObject(SYSTEM_FONT));
lppop->Height = 10;
CalcAGAIN:
OldHeight = lppop->Height;
SetRect(&rect, 1, 1, 0, OldHeight);
lpitem = lppop->firstItem;
for(i = 0; i < lppop->nItems; i++) {
if (lpitem == NULL) break;
rect.bottom = rect.top + lppop->Height;
if ((lpitem->item_flags & MF_BITMAP) == MF_BITMAP) {
hBitMap = (HBITMAP)LOWORD((LONG)lpitem->item_text);
GetObject(hBitMap, sizeof(BITMAP), (LPSTR)&bm);
rect.right = rect.left + bm.bmWidth;
lppop->Height = max(lppop->Height, bm.bmHeight);
}
if (((lpitem->item_flags & MF_BITMAP) != MF_BITMAP) &&
((lpitem->item_flags & MF_SEPARATOR) != MF_SEPARATOR) &&
((lpitem->item_flags & MF_MENUBREAK) != MF_MENUBREAK)) {
dwRet = GetTextExtent(hDC, (char *)lpitem->item_text,
strlen((char *)lpitem->item_text));
rect.right = rect.left + LOWORD(dwRet) + 10;
lppop->Height = max(lppop->Height, HIWORD(dwRet) + 10);
}
CopyRect(&lpitem->rect, &rect);
rect.left = rect.right;
lpitem = (LPMENUITEM)lpitem->next;
}
if (OldHeight < lppop->Height) goto CalcAGAIN;
lppop->Width = rect.right;
#ifdef DEBUG_MENUCALC
printf("MenuBarCalcSize w=%d h=%d !\n",
lppop->Width, lppop->Height);
#endif
SelectObject(hDC, hOldFont);
ReleaseDC(hwnd, hDC);
}
LPMENUITEM GetMenuItemPtr(LPPOPUPMENU menu, WORD nPos)
{
LPMENUITEM lpitem;
int i;
if (menu == NULL) return NULL;
lpitem = menu->firstItem;
for (i = 0; i < menu->nItems; i++) {
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
WORD GetSelectionKey(LPSTR str)
{
int i;
WORD sel_key;
for (i = 0; i < strlen(str); i++) {
if (str[i] == '&' && str[i + 1] != '&')
{
sel_key = str[i + 1];
if (sel_key >= 'a' && sel_key <= 'z') sel_key -= 'a' - 'A';
#ifdef DEBUG_MENU
printf("GetSelectionKey // %04X\n", sel_key);
#endif
return sel_key;
}
}
#ifdef DEBUG_MENU
printf("GetSelectionKey NULL \n");
#endif
return 0;
}
LPSTR GetShortCutString(LPSTR str)
{
int i;
LPSTR str2;
for (i = 0; i < strlen(str); i++) {
if (str[i] == '\t' && str[i + 1] != '\t')
{
str2 = &str[i + 1];
#ifdef DEBUG_MENU
printf("GetShortCutString // '%s' \n", str2);
#endif
return str2;
}
}
#ifdef DEBUG_MENU
printf("GetShortCutString NULL \n");
#endif
return NULL;
}
WORD GetShortCutPos(LPSTR str)
{
int i;
for (i = 0; i < strlen(str); i++) {
if (str[i] == '\t' && str[i + 1] != '\t')
{
#ifdef DEBUG_MENU
printf("GetShortCutPos = %d \n", i);
#endif
return i;
}
}
#ifdef DEBUG_MENU
printf("GetShortCutString NULL \n");
#endif
return -1;
}
BOOL HideAllSubPopupMenu(LPPOPUPMENU menu)
{
LPPOPUPMENU submenu;
LPMENUITEM lpitem;
BOOL someClosed = FALSE;
int i;
if (menu == NULL) return;
lpitem = menu->firstItem;
for (i = 0; i < menu->nItems; i++) {
if (lpitem == NULL) return;
if (lpitem->item_flags & MF_POPUP) {
submenu = (LPPOPUPMENU) GlobalLock((HMENU)lpitem->item_id);
if (submenu != NULL) {
if (IsWindowVisible(submenu->hWnd)) {
ShowWindow(submenu->hWnd, SW_HIDE);
someClosed = TRUE;
}
}
}
lpitem = (LPMENUITEM)lpitem->next;
}
return someClosed;
}
#ifdef USE_XTMENU
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
* MENU_CheckWidget
*/
void
MENU_CheckWidget(Widget w, Boolean check)
{
if (checkBitmap == XtUnspecifiedPixmap)
{
Display *display = XtDisplayOfObject(w);
checkBitmap = XCreateBitmapFromData(display,
DefaultRootWindow(display),
check_bitmap_bits,
check_bitmap_width,
check_bitmap_height);
nocheckBitmap = XCreateBitmapFromData(display,
DefaultRootWindow(display),
nocheck_bitmap_bits,
nocheck_bitmap_width,
nocheck_bitmap_height);
}
if (check)
XtVaSetValues(w, XtNleftBitmap, checkBitmap, NULL);
else
XtVaSetValues(w, XtNleftBitmap, nocheckBitmap, NULL);
}
/**********************************************************************
* MENU_ParseMenu
*/
WORD *
MENU_ParseMenu(WORD *first_item,
int level,