added separator items

This commit is contained in:
Serge A. Zaitsev
2017-01-09 14:21:44 +02:00
parent 14a8be3424
commit 1a4a1446f5
2 changed files with 50 additions and 31 deletions

View File

@@ -5,6 +5,12 @@
static struct tray tray; static struct tray tray;
static void toggle_cb(struct tray_menu *item) {
printf("toggle cb\n");
item->checked = !item->checked;
tray_update(&tray);
}
static void hello_cb(struct tray_menu *item) { static void hello_cb(struct tray_menu *item) {
printf("hello cb\n"); printf("hello cb\n");
if (strcmp(tray.icon, "indicator-messages") == 0) { if (strcmp(tray.icon, "indicator-messages") == 0) {
@@ -21,9 +27,12 @@ static void quit_cb(struct tray_menu *item) {
} }
static struct tray tray = { static struct tray tray = {
.menu = (struct tray_menu[]){{NULL, "Hello", 0, hello_cb, NULL}, .menu = (struct tray_menu[]){{"Hello", 0, 0, hello_cb, NULL},
{NULL, "Quit", 0, quit_cb, NULL}, {"Checked", 0, 1, toggle_cb, NULL},
{NULL, NULL, 0, NULL, NULL}}, {"Disabled", 1, 0, NULL, NULL},
{"-", 0, 0, NULL, NULL},
{"Quit", 0, 0, quit_cb, NULL},
{NULL, 0, 0, NULL, NULL}},
}; };
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {

66
tray.h
View File

@@ -10,9 +10,9 @@ struct tray {
}; };
struct tray_menu { struct tray_menu {
char *icon; char *text;
char *text; /* label */ int disabled;
int flags; int checked;
void (*cb)(struct tray_menu *); void (*cb)(struct tray_menu *);
void *context; void *context;
@@ -52,12 +52,15 @@ static int tray_loop(int blocking) {
} }
static void tray_update(struct tray *tray) { static void tray_update(struct tray *tray) {
struct tray_menu *m;
app_indicator_set_icon(indicator, tray->icon); app_indicator_set_icon(indicator, tray->icon);
GtkMenuShell *gtk_menu = (GtkMenuShell *)gtk_menu_new(); GtkMenuShell *gtk_menu = (GtkMenuShell *)gtk_menu_new();
for (struct tray_menu *m = tray->menu; m != NULL && m->text != NULL; m++) { for (struct tray_menu *m = tray->menu; m != NULL && m->text != NULL; m++) {
GtkWidget *item = gtk_menu_item_new_with_label(m->text); GtkWidget *item;
if (strcmp(m->text, "-") == 0) {
item = gtk_separator_menu_item_new();
} else {
item = gtk_menu_item_new_with_label(m->text);
}
gtk_widget_show(item); gtk_widget_show(item);
gtk_menu_shell_append(GTK_MENU_SHELL(gtk_menu), item); gtk_menu_shell_append(GTK_MENU_SHELL(gtk_menu), item);
if (m->cb != NULL) { if (m->cb != NULL) {
@@ -127,17 +130,19 @@ static void tray_update(struct tray *tray) {
[menu autorelease]; [menu autorelease];
[menu setAutoenablesItems:NO]; [menu setAutoenablesItems:NO];
for (struct tray_menu *m = tray->menu; m != NULL && m->text != NULL; m++) { for (struct tray_menu *m = tray->menu; m != NULL && m->text != NULL; m++) {
NSMenuItem *menuItem = [NSMenuItem alloc]; if (strcmp(m->text, "-") == 0) {
[menuItem autorelease]; [menu addItem:[NSMenuItem separatorItem]];
[menuItem initWithTitle:[NSString stringWithUTF8String:m->text] } else {
action:@selector(menuCallback:) NSMenuItem *menuItem = [NSMenuItem alloc];
keyEquivalent:@""]; [menuItem autorelease];
[menuItem setEnabled:YES]; [menuItem initWithTitle:[NSString stringWithUTF8String:m->text]
[menuItem setRepresentedObject:[NSValue valueWithPointer:m]]; action:@selector(menuCallback:)
keyEquivalent:@""];
[menuItem setEnabled:YES];
[menuItem setRepresentedObject:[NSValue valueWithPointer:m]];
[menu addItem:menuItem]; [menu addItem:menuItem];
}
//[menu addItem:[NSMenuItem separatorItem]];
} }
[statusItem setMenu:menu]; [statusItem setMenu:menu];
@@ -146,9 +151,10 @@ static void tray_update(struct tray *tray) {
static void tray_exit() { [NSApp terminate:NSApp]; } static void tray_exit() { [NSApp terminate:NSApp]; }
#elif defined(TRAY_WINAPI) #elif defined(TRAY_WINAPI)
#include <shellapi.h>
#include <windows.h> #include <windows.h>
#include <shellapi.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 "TRAY"
#define ID_TRAY_FIRST 1000 #define ID_TRAY_FIRST 1000
@@ -234,18 +240,22 @@ static int tray_loop(int blocking) {
static void tray_update(struct tray *tray) { static void tray_update(struct tray *tray) {
int i = 0; int i = 0;
hmenu = CreatePopupMenu(); hmenu = CreatePopupMenu();
for (struct tray_menu *m = tray->menu; m != NULL && m->text != NULL; m++) { for (struct tray_menu *m = tray->menu; m != NULL && m->text != NULL;
MENUITEMINFO *item = (MENUITEMINFO *)malloc(sizeof(MENUITEMINFO)); m++, i++) {
item->cbSize = sizeof(MENUITEMINFO); if (strcmp(m->text, "-") == 0) {
item->fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE | MIIM_DATA; InsertMenu(hmenu, i, MF_SEPARATOR, TRUE, "");
item->fType = 0; } else {
item->fState = 0; MENUITEMINFO *item = (MENUITEMINFO *)malloc(sizeof(MENUITEMINFO));
item->wID = i + ID_TRAY_FIRST; item->cbSize = sizeof(MENUITEMINFO);
item->dwTypeData = m->text; item->fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE | MIIM_DATA;
item->dwItemData = (ULONG_PTR)m; item->fType = 0;
item->fState = 0;
item->wID = i + ID_TRAY_FIRST;
item->dwTypeData = m->text;
item->dwItemData = (ULONG_PTR)m;
InsertMenuItem(hmenu, i, TRUE, item); InsertMenuItem(hmenu, i, TRUE, item);
i++; }
} }
SendMessage(hwnd, WM_INITMENUPOPUP, (WPARAM)hmenu, 0); SendMessage(hwnd, WM_INITMENUPOPUP, (WPARAM)hmenu, 0);
ExtractIconEx(tray->icon, 0, NULL, &(nid.hIcon), 1); ExtractIconEx(tray->icon, 0, NULL, &(nid.hIcon), 1);