forked from Yadciel/c_tray
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fc77dda643 | |||
| 8df4e27360 | |||
| 951dfb930d | |||
| a71be4e1e3 | |||
| e9f0c9b8c9 | |||
| 9a8694d755 |
@@ -31,7 +31,6 @@ add_library(tray STATIC ${SRCS})
|
||||
set_property(TARGET tray PROPERTY C_STANDARD 99)
|
||||
if(WIN32)
|
||||
target_compile_definitions(tray PRIVATE TRAY_WINAPI=1 WIN32_LEAN_AND_MEAN NOMINMAX)
|
||||
target_compile_options(tray PRIVATE "/MT$<$<CONFIG:Debug>:d>")
|
||||
else()
|
||||
if(UNIX)
|
||||
if(APPLE)
|
||||
|
||||
56
README.md
56
README.md
@@ -16,13 +16,17 @@ Works well on:
|
||||
|
||||
The code is C++ friendly and will compile fine in C++98 and up.
|
||||
|
||||
This fork is intended to bring together the [original work of Serge Zaitsev](https://github.com/zserge/tray) and the most interesting forks and PRs of respectable contributors:
|
||||
This repository started as a fork of the [original work by Serge Zaitsev](https://github.com/zserge/tray), but it is now maintained as a separate continuation. It keeps the small public C API and cross-platform focus, while carrying its own fixes and behavior changes.
|
||||
|
||||
* [Only process messages coming from the tray window on Windows](https://github.com/zserge/tray/pull/18)
|
||||
* [Become C++-friendly](https://github.com/zserge/tray/pull/16)
|
||||
* [Fix all menu items have a check box](https://github.com/zserge/tray/pull/11)
|
||||
* [Add support for tooltip](https://github.com/zserge/tray/pull/11)
|
||||
* Darwin implementation translated from C to Objective C adapted from [@trevex fork](https://github.com/trevex/tray)
|
||||
Compared to the original upstream, this fork already includes and continues work around:
|
||||
|
||||
* improved Windows message handling
|
||||
* C++-friendly headers and usage
|
||||
* tooltip support
|
||||
* checkbox/menu state fixes
|
||||
* Darwin/AppKit support adapted from the [@trevex fork](https://github.com/trevex/tray)
|
||||
* support for left-click callbacks
|
||||
* Windows UTF-8 tray text handling
|
||||
|
||||
## Prerequisites
|
||||
|
||||
@@ -53,21 +57,27 @@ Execute the `tray_example` application:
|
||||
|
||||
## API
|
||||
|
||||
Tray structure defines an icon and a menu.
|
||||
Menu is a NULL-terminated array of items.
|
||||
Menu item defines menu text, menu checked and disabled (grayed) flags and a
|
||||
callback with some optional context pointer.
|
||||
A tray is defined by an icon, an optional tooltip and a menu.
|
||||
The menu is a NULL-terminated array of items.
|
||||
Each menu item can be disabled, checked, represented as a checkbox and invoke a
|
||||
callback with an optional context pointer.
|
||||
|
||||
```c
|
||||
struct tray {
|
||||
char *icon;
|
||||
const char *icon;
|
||||
char *tooltip;
|
||||
struct tray_menu *menu;
|
||||
void *icon_handle;
|
||||
int icon_is_shared;
|
||||
void (*left_click_cb)(void *context);
|
||||
void *left_click_context;
|
||||
};
|
||||
|
||||
struct tray_menu {
|
||||
char *text;
|
||||
const char *text;
|
||||
int disabled;
|
||||
int checked;
|
||||
int checkbox;
|
||||
|
||||
void (*cb)(struct tray_menu *);
|
||||
void *context;
|
||||
@@ -76,15 +86,25 @@ struct tray_menu {
|
||||
};
|
||||
```
|
||||
|
||||
* `int tray_init(struct tray *)` - creates tray icon. Returns -1 if tray icon/menu can't be created.
|
||||
* `void tray_update(struct tray *)` - updates tray icon and menu.
|
||||
* `int tray_loop(int blocking)` - runs one iteration of the UI loop. Returns -1 if `tray_exit()` has been called.
|
||||
* `void tray_exit()` - terminates UI loop.
|
||||
* `int tray_init(struct tray *)` - creates the tray icon and its menu. Returns `-1` if the tray icon or menu cannot be created.
|
||||
* `void tray_update(struct tray *)` - updates the tray icon, tooltip and menu state.
|
||||
* `int tray_loop(int blocking)` - runs one iteration of the UI loop. Returns `-1` after `tray_exit()` has been called.
|
||||
* `void tray_exit(void)` - terminates the UI loop and cleans up tray resources.
|
||||
|
||||
All functions are meant to be called from the UI thread only.
|
||||
|
||||
Menu arrays must be terminated with a NULL item, e.g. the last item in the
|
||||
array must have text field set to NULL.
|
||||
Menu arrays must be terminated with a NULL item, i.e. the last item in the
|
||||
array must have `text == NULL`.
|
||||
|
||||
### Notes
|
||||
|
||||
* `tooltip` is optional.
|
||||
* `left_click_cb` and `left_click_context` are optional.
|
||||
* `icon_handle` and `icon_is_shared` are backend-specific fields used internally by the Windows implementation and should usually be left untouched by callers.
|
||||
* A separator is created by using `text = "-"`.
|
||||
* Some behavior may depend on the underlying backend or desktop environment.
|
||||
Keep the public API generic and treat platform-specific interaction details as
|
||||
backend-specific implementation behavior.
|
||||
|
||||
## License
|
||||
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
#include <windows.h>
|
||||
#include <shellapi.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "tray.h"
|
||||
|
||||
#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
|
||||
|
||||
static WNDCLASSEX wc;
|
||||
static NOTIFYICONDATA nid;
|
||||
static WNDCLASSEXW wc;
|
||||
static NOTIFYICONDATAW nid;
|
||||
static HWND hwnd;
|
||||
static HMENU hmenu = NULL;
|
||||
static struct tray *g_tray = NULL;
|
||||
@@ -42,10 +44,10 @@ static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam,
|
||||
break;
|
||||
case WM_COMMAND:
|
||||
if (wparam >= ID_TRAY_FIRST) {
|
||||
MENUITEMINFO item = {
|
||||
.cbSize = sizeof(MENUITEMINFO), .fMask = MIIM_ID | MIIM_DATA,
|
||||
MENUITEMINFOW item = {
|
||||
.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;
|
||||
if (menu != NULL && menu->cb != NULL) {
|
||||
menu->cb(menu);
|
||||
@@ -55,36 +57,46 @@ static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam,
|
||||
}
|
||||
break;
|
||||
}
|
||||
return DefWindowProc(hwnd, msg, wparam, lparam);
|
||||
return DefWindowProcW(hwnd, msg, wparam, lparam);
|
||||
}
|
||||
|
||||
static HMENU _tray_menu(struct tray_menu *m, UINT *id) {
|
||||
HMENU hmenu = CreatePopupMenu();
|
||||
for (; m != NULL && m->text != NULL; m++, (*id)++) {
|
||||
if (strcmp(m->text, "-") == 0) {
|
||||
InsertMenu(hmenu, *id, MF_SEPARATOR, TRUE, "");
|
||||
InsertMenuW(hmenu, *id, MF_BYPOSITION | MF_SEPARATOR, *id, L"");
|
||||
} else {
|
||||
MENUITEMINFO item;
|
||||
memset(&item, 0, sizeof(item));
|
||||
item.cbSize = sizeof(MENUITEMINFO);
|
||||
item.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE | MIIM_DATA;
|
||||
item.fType = 0;
|
||||
item.fState = 0;
|
||||
if (m->submenu != NULL) {
|
||||
item.fMask = item.fMask | MIIM_SUBMENU;
|
||||
item.hSubMenu = _tray_menu(m->submenu, id);
|
||||
}
|
||||
if (m->disabled) {
|
||||
item.fState |= MFS_DISABLED;
|
||||
}
|
||||
if (m->checked) {
|
||||
item.fState |= MFS_CHECKED;
|
||||
}
|
||||
item.wID = *id;
|
||||
item.dwTypeData = (LPSTR)m->text;
|
||||
item.dwItemData = (ULONG_PTR)m;
|
||||
// UTF-8 zu UTF-16 (WCHAR) konvertieren
|
||||
int len = MultiByteToWideChar(CP_UTF8, 0, m->text, -1, NULL, 0);
|
||||
if (len > 0) {
|
||||
wchar_t *wtext = (wchar_t*)malloc(len * sizeof(wchar_t));
|
||||
if (wtext) {
|
||||
MultiByteToWideChar(CP_UTF8, 0, m->text, -1, wtext, len);
|
||||
|
||||
InsertMenuItem(hmenu, *id, TRUE, &item);
|
||||
MENUITEMINFOW item;
|
||||
memset(&item, 0, sizeof(item));
|
||||
item.cbSize = sizeof(MENUITEMINFOW);
|
||||
item.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE | MIIM_DATA;
|
||||
item.fType = 0;
|
||||
item.fState = 0;
|
||||
if (m->submenu != NULL) {
|
||||
item.fMask = item.fMask | MIIM_SUBMENU;
|
||||
item.hSubMenu = _tray_menu(m->submenu, id);
|
||||
}
|
||||
if (m->disabled) {
|
||||
item.fState |= MFS_DISABLED;
|
||||
}
|
||||
if (m->checked) {
|
||||
item.fState |= MFS_CHECKED;
|
||||
}
|
||||
item.wID = *id;
|
||||
item.dwTypeData = wtext;
|
||||
item.dwItemData = (ULONG_PTR)m;
|
||||
|
||||
InsertMenuItemW(hmenu, *id, TRUE, &item);
|
||||
free(wtext);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return hmenu;
|
||||
@@ -92,15 +104,15 @@ static HMENU _tray_menu(struct tray_menu *m, UINT *id) {
|
||||
|
||||
int tray_init(struct tray *tray) {
|
||||
memset(&wc, 0, sizeof(wc));
|
||||
wc.cbSize = sizeof(WNDCLASSEX);
|
||||
wc.cbSize = sizeof(WNDCLASSEXW);
|
||||
wc.lpfnWndProc = _tray_wnd_proc;
|
||||
wc.hInstance = GetModuleHandle(NULL);
|
||||
wc.lpszClassName = WC_TRAY_CLASS_NAME;
|
||||
if (!RegisterClassEx(&wc)) {
|
||||
if (!RegisterClassExW(&wc)) {
|
||||
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) {
|
||||
return -1;
|
||||
}
|
||||
@@ -112,7 +124,7 @@ int tray_init(struct tray *tray) {
|
||||
nid.uID = 0;
|
||||
nid.uFlags = NIF_ICON | NIF_MESSAGE;
|
||||
nid.uCallbackMessage = WM_TRAY_CALLBACK_MESSAGE;
|
||||
Shell_NotifyIcon(NIM_ADD, &nid);
|
||||
Shell_NotifyIconW(NIM_ADD, &nid);
|
||||
|
||||
g_tray = tray;
|
||||
tray_update(tray);
|
||||
@@ -146,7 +158,7 @@ void tray_update(struct tray *tray) {
|
||||
icon = (HICON)tray->icon_handle;
|
||||
icon_is_shared = tray->icon_is_shared;
|
||||
} 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) {
|
||||
DestroyIcon(nid.hIcon);
|
||||
@@ -156,11 +168,10 @@ void tray_update(struct tray *tray) {
|
||||
g_icon_is_shared = icon_is_shared;
|
||||
}
|
||||
if(tray->tooltip != 0 && strlen(tray->tooltip) > 0) {
|
||||
strncpy(nid.szTip, tray->tooltip, sizeof(nid.szTip) - 1);
|
||||
nid.szTip[sizeof(nid.szTip) - 1] = '\0';
|
||||
MultiByteToWideChar(CP_UTF8, 0, tray->tooltip, -1, nid.szTip, sizeof(nid.szTip)/sizeof(wchar_t));
|
||||
nid.uFlags |= NIF_TIP;
|
||||
}
|
||||
Shell_NotifyIcon(NIM_MODIFY, &nid);
|
||||
Shell_NotifyIconW(NIM_MODIFY, &nid);
|
||||
|
||||
if (prevmenu != NULL) {
|
||||
DestroyMenu(prevmenu);
|
||||
@@ -168,7 +179,7 @@ void tray_update(struct tray *tray) {
|
||||
}
|
||||
|
||||
void tray_exit(void) {
|
||||
Shell_NotifyIcon(NIM_DELETE, &nid);
|
||||
Shell_NotifyIconW(NIM_DELETE, &nid);
|
||||
if (nid.hIcon != 0 && !g_icon_is_shared) {
|
||||
DestroyIcon(nid.hIcon);
|
||||
}
|
||||
@@ -176,6 +187,6 @@ void tray_exit(void) {
|
||||
DestroyMenu(hmenu);
|
||||
}
|
||||
PostQuitMessage(0);
|
||||
UnregisterClass(WC_TRAY_CLASS_NAME, GetModuleHandle(NULL));
|
||||
UnregisterClassW(WC_TRAY_CLASS_NAME, GetModuleHandle(NULL));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user