Migriere Windows Tray zu expliziten Wide-Character APIs
Diese Änderung stellt sicher, dass alle relevanten Windows API-Aufrufe und Datenstrukturen die Wide-Character (W-) Versionen verwenden. Dies gewährleistet eine konsistente und robuste Behandlung von Unicode-Zeichen, insbesondere nach der Einführung der UTF-8-Unterstützung für Menütexte und Tooltips. Zusätzlich wird die erzwungene statische Verlinkung der C-Laufzeitbibliothek (`/MT`) entfernt, um die Kompatibilität zu verbessern und die Binärgröße zu reduzieren, indem die standardmäßige dynamische Verlinkung ermöglicht wird.
This commit is contained in:
@@ -31,7 +31,6 @@ add_library(tray STATIC ${SRCS})
|
|||||||
set_property(TARGET tray PROPERTY C_STANDARD 99)
|
set_property(TARGET tray PROPERTY C_STANDARD 99)
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
target_compile_definitions(tray PRIVATE TRAY_WINAPI=1 WIN32_LEAN_AND_MEAN NOMINMAX)
|
target_compile_definitions(tray PRIVATE TRAY_WINAPI=1 WIN32_LEAN_AND_MEAN NOMINMAX)
|
||||||
target_compile_options(tray PRIVATE "/MT$<$<CONFIG:Debug>:d>")
|
|
||||||
else()
|
else()
|
||||||
if(UNIX)
|
if(UNIX)
|
||||||
if(APPLE)
|
if(APPLE)
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <shellapi.h>
|
#include <shellapi.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include "tray.h"
|
#include "tray.h"
|
||||||
|
|
||||||
#define WM_TRAY_CALLBACK_MESSAGE (WM_USER + 1)
|
#define WM_TRAY_CALLBACK_MESSAGE (WM_USER + 1)
|
||||||
#define WC_TRAY_CLASS_NAME "TRAY"
|
#define WC_TRAY_CLASS_NAME L"TRAY"
|
||||||
#define ID_TRAY_FIRST 1000
|
#define ID_TRAY_FIRST 1000
|
||||||
|
|
||||||
static WNDCLASSEX wc;
|
static WNDCLASSEXW wc;
|
||||||
static NOTIFYICONDATA nid;
|
static NOTIFYICONDATAW nid;
|
||||||
static HWND hwnd;
|
static HWND hwnd;
|
||||||
static HMENU hmenu = NULL;
|
static HMENU hmenu = NULL;
|
||||||
static struct tray *g_tray = NULL;
|
static struct tray *g_tray = NULL;
|
||||||
@@ -42,10 +44,10 @@ static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam,
|
|||||||
break;
|
break;
|
||||||
case WM_COMMAND:
|
case WM_COMMAND:
|
||||||
if (wparam >= ID_TRAY_FIRST) {
|
if (wparam >= ID_TRAY_FIRST) {
|
||||||
MENUITEMINFO item = {
|
MENUITEMINFOW item = {
|
||||||
.cbSize = sizeof(MENUITEMINFO), .fMask = MIIM_ID | MIIM_DATA,
|
.cbSize = sizeof(MENUITEMINFOW), .fMask = MIIM_ID | MIIM_DATA,
|
||||||
};
|
};
|
||||||
if (GetMenuItemInfo(hmenu, wparam, FALSE, &item)) {
|
if (GetMenuItemInfoW(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) {
|
if (menu != NULL && menu->cb != NULL) {
|
||||||
menu->cb(menu);
|
menu->cb(menu);
|
||||||
@@ -55,14 +57,14 @@ static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam,
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return DefWindowProc(hwnd, msg, wparam, lparam);
|
return DefWindowProcW(hwnd, msg, wparam, lparam);
|
||||||
}
|
}
|
||||||
|
|
||||||
static HMENU _tray_menu(struct tray_menu *m, UINT *id) {
|
static HMENU _tray_menu(struct tray_menu *m, UINT *id) {
|
||||||
HMENU hmenu = CreatePopupMenu();
|
HMENU hmenu = CreatePopupMenu();
|
||||||
for (; m != NULL && m->text != NULL; m++, (*id)++) {
|
for (; m != NULL && m->text != NULL; m++, (*id)++) {
|
||||||
if (strcmp(m->text, "-") == 0) {
|
if (strcmp(m->text, "-") == 0) {
|
||||||
InsertMenuW(hmenu, *id, MF_SEPARATOR, TRUE, L"");
|
InsertMenuW(hmenu, *id, MF_BYPOSITION | MF_SEPARATOR, *id, L"");
|
||||||
} else {
|
} else {
|
||||||
// UTF-8 zu UTF-16 (WCHAR) konvertieren
|
// UTF-8 zu UTF-16 (WCHAR) konvertieren
|
||||||
int len = MultiByteToWideChar(CP_UTF8, 0, m->text, -1, NULL, 0);
|
int len = MultiByteToWideChar(CP_UTF8, 0, m->text, -1, NULL, 0);
|
||||||
@@ -102,15 +104,15 @@ static HMENU _tray_menu(struct tray_menu *m, UINT *id) {
|
|||||||
|
|
||||||
int tray_init(struct tray *tray) {
|
int tray_init(struct tray *tray) {
|
||||||
memset(&wc, 0, sizeof(wc));
|
memset(&wc, 0, sizeof(wc));
|
||||||
wc.cbSize = sizeof(WNDCLASSEX);
|
wc.cbSize = sizeof(WNDCLASSEXW);
|
||||||
wc.lpfnWndProc = _tray_wnd_proc;
|
wc.lpfnWndProc = _tray_wnd_proc;
|
||||||
wc.hInstance = GetModuleHandle(NULL);
|
wc.hInstance = GetModuleHandle(NULL);
|
||||||
wc.lpszClassName = WC_TRAY_CLASS_NAME;
|
wc.lpszClassName = WC_TRAY_CLASS_NAME;
|
||||||
if (!RegisterClassEx(&wc)) {
|
if (!RegisterClassExW(&wc)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
hwnd = CreateWindowEx(0, WC_TRAY_CLASS_NAME, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
hwnd = CreateWindowExW(0, WC_TRAY_CLASS_NAME, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||||
if (hwnd == NULL) {
|
if (hwnd == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -122,7 +124,7 @@ int tray_init(struct tray *tray) {
|
|||||||
nid.uID = 0;
|
nid.uID = 0;
|
||||||
nid.uFlags = NIF_ICON | NIF_MESSAGE;
|
nid.uFlags = NIF_ICON | NIF_MESSAGE;
|
||||||
nid.uCallbackMessage = WM_TRAY_CALLBACK_MESSAGE;
|
nid.uCallbackMessage = WM_TRAY_CALLBACK_MESSAGE;
|
||||||
Shell_NotifyIcon(NIM_ADD, &nid);
|
Shell_NotifyIconW(NIM_ADD, &nid);
|
||||||
|
|
||||||
g_tray = tray;
|
g_tray = tray;
|
||||||
tray_update(tray);
|
tray_update(tray);
|
||||||
@@ -156,7 +158,7 @@ void tray_update(struct tray *tray) {
|
|||||||
icon = (HICON)tray->icon_handle;
|
icon = (HICON)tray->icon_handle;
|
||||||
icon_is_shared = tray->icon_is_shared;
|
icon_is_shared = tray->icon_is_shared;
|
||||||
} else if (tray->icon) {
|
} else if (tray->icon) {
|
||||||
ExtractIconEx(tray->icon, 0, NULL, &icon, 1);
|
ExtractIconExA(tray->icon, 0, NULL, &icon, 1);
|
||||||
}
|
}
|
||||||
if (nid.hIcon && !g_icon_is_shared) {
|
if (nid.hIcon && !g_icon_is_shared) {
|
||||||
DestroyIcon(nid.hIcon);
|
DestroyIcon(nid.hIcon);
|
||||||
@@ -169,7 +171,7 @@ void tray_update(struct tray *tray) {
|
|||||||
MultiByteToWideChar(CP_UTF8, 0, tray->tooltip, -1, nid.szTip, sizeof(nid.szTip)/sizeof(wchar_t));
|
MultiByteToWideChar(CP_UTF8, 0, tray->tooltip, -1, nid.szTip, sizeof(nid.szTip)/sizeof(wchar_t));
|
||||||
nid.uFlags |= NIF_TIP;
|
nid.uFlags |= NIF_TIP;
|
||||||
}
|
}
|
||||||
Shell_NotifyIcon(NIM_MODIFY, &nid);
|
Shell_NotifyIconW(NIM_MODIFY, &nid);
|
||||||
|
|
||||||
if (prevmenu != NULL) {
|
if (prevmenu != NULL) {
|
||||||
DestroyMenu(prevmenu);
|
DestroyMenu(prevmenu);
|
||||||
@@ -177,7 +179,7 @@ void tray_update(struct tray *tray) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void tray_exit(void) {
|
void tray_exit(void) {
|
||||||
Shell_NotifyIcon(NIM_DELETE, &nid);
|
Shell_NotifyIconW(NIM_DELETE, &nid);
|
||||||
if (nid.hIcon != 0 && !g_icon_is_shared) {
|
if (nid.hIcon != 0 && !g_icon_is_shared) {
|
||||||
DestroyIcon(nid.hIcon);
|
DestroyIcon(nid.hIcon);
|
||||||
}
|
}
|
||||||
@@ -185,6 +187,6 @@ void tray_exit(void) {
|
|||||||
DestroyMenu(hmenu);
|
DestroyMenu(hmenu);
|
||||||
}
|
}
|
||||||
PostQuitMessage(0);
|
PostQuitMessage(0);
|
||||||
UnregisterClass(WC_TRAY_CLASS_NAME, GetModuleHandle(NULL));
|
UnregisterClassW(WC_TRAY_CLASS_NAME, GetModuleHandle(NULL));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user