fixed id numbering in winapi that caused crashes on submenus
This commit is contained in:
@@ -41,7 +41,7 @@ static void quit_cb(struct tray_menu *item) {
|
|||||||
|
|
||||||
static void submenu_cb(struct tray_menu *item) {
|
static void submenu_cb(struct tray_menu *item) {
|
||||||
(void)item;
|
(void)item;
|
||||||
printf("submenu cb!!!\n");
|
printf("submenu: clicked on %s\n", item->text);
|
||||||
tray_update(&tray);
|
tray_update(&tray);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
17
tray.h
17
tray.h
@@ -209,8 +209,10 @@ static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam,
|
|||||||
};
|
};
|
||||||
if (GetMenuItemInfo(hmenu, wparam, FALSE, &item)) {
|
if (GetMenuItemInfo(hmenu, wparam, FALSE, &item)) {
|
||||||
struct tray_menu *menu = (struct tray_menu *)item.dwItemData;
|
struct tray_menu *menu = (struct tray_menu *)item.dwItemData;
|
||||||
|
if (menu != NULL && menu->cb != NULL) {
|
||||||
menu->cb(menu);
|
menu->cb(menu);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -218,11 +220,11 @@ static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam,
|
|||||||
return DefWindowProc(hwnd, msg, wparam, lparam);
|
return DefWindowProc(hwnd, msg, wparam, lparam);
|
||||||
}
|
}
|
||||||
|
|
||||||
static HMENU _tray_menu(struct tray_menu *m) {
|
static HMENU _tray_menu(struct tray_menu *m, UINT *id) {
|
||||||
HMENU hmenu = CreatePopupMenu();
|
HMENU hmenu = CreatePopupMenu();
|
||||||
for (int i = 0; m != NULL && m->text != NULL; m++, i++) {
|
for (; m != NULL && m->text != NULL; m++, (*id)++) {
|
||||||
if (strcmp(m->text, "-") == 0) {
|
if (strcmp(m->text, "-") == 0) {
|
||||||
InsertMenu(hmenu, i, MF_SEPARATOR, TRUE, "");
|
InsertMenu(hmenu, *id, MF_SEPARATOR, TRUE, "");
|
||||||
} else {
|
} else {
|
||||||
MENUITEMINFO item;
|
MENUITEMINFO item;
|
||||||
memset(&item, 0, sizeof(item));
|
memset(&item, 0, sizeof(item));
|
||||||
@@ -232,7 +234,7 @@ static HMENU _tray_menu(struct tray_menu *m) {
|
|||||||
item.fState = 0;
|
item.fState = 0;
|
||||||
if (m->submenu != NULL) {
|
if (m->submenu != NULL) {
|
||||||
item.fMask = item.fMask | MIIM_SUBMENU;
|
item.fMask = item.fMask | MIIM_SUBMENU;
|
||||||
item.hSubMenu = _tray_menu(m->submenu);
|
item.hSubMenu = _tray_menu(m->submenu, id);
|
||||||
}
|
}
|
||||||
if (m->disabled) {
|
if (m->disabled) {
|
||||||
item.fState |= MFS_DISABLED;
|
item.fState |= MFS_DISABLED;
|
||||||
@@ -240,11 +242,11 @@ static HMENU _tray_menu(struct tray_menu *m) {
|
|||||||
if (m->checked) {
|
if (m->checked) {
|
||||||
item.fState |= MFS_CHECKED;
|
item.fState |= MFS_CHECKED;
|
||||||
}
|
}
|
||||||
item.wID = i + ID_TRAY_FIRST;
|
item.wID = *id;
|
||||||
item.dwTypeData = m->text;
|
item.dwTypeData = m->text;
|
||||||
item.dwItemData = (ULONG_PTR)m;
|
item.dwItemData = (ULONG_PTR)m;
|
||||||
|
|
||||||
InsertMenuItem(hmenu, i, TRUE, &item);
|
InsertMenuItem(hmenu, *id, TRUE, &item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return hmenu;
|
return hmenu;
|
||||||
@@ -295,7 +297,8 @@ static int tray_loop(int blocking) {
|
|||||||
|
|
||||||
static void tray_update(struct tray *tray) {
|
static void tray_update(struct tray *tray) {
|
||||||
HMENU prevmenu = hmenu;
|
HMENU prevmenu = hmenu;
|
||||||
hmenu = _tray_menu(tray->menu);
|
UINT id = ID_TRAY_FIRST;
|
||||||
|
hmenu = _tray_menu(tray->menu, &id);
|
||||||
SendMessage(hwnd, WM_INITMENUPOPUP, (WPARAM)hmenu, 0);
|
SendMessage(hwnd, WM_INITMENUPOPUP, (WPARAM)hmenu, 0);
|
||||||
HICON icon;
|
HICON icon;
|
||||||
ExtractIconEx(tray->icon, 0, NULL, &icon, 1);
|
ExtractIconEx(tray->icon, 0, NULL, &icon, 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user