Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Become C++-friendly #16

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Works well on:
* Windows XP or newer (shellapi.h)
* MacOS (Cocoa/AppKit)

There is also a stub implementation that returns errors on attempt to create a tray menu.
The code is C++ friendly and will compile fine in C++98 and up.

# Setup

Expand All @@ -20,7 +20,7 @@ Before you can compile `tray`, you'll need to add an environment definition befo
#include <stdio.h>
#include <string.h>

#define TRAY_WINAPI 1
#define TRAY_WINAPI

#include "tray.h"
...
Expand All @@ -31,7 +31,7 @@ Before you can compile `tray`, you'll need to add an environment definition befo
#include <stdio.h>
#include <string.h>

#define TRAY_APPINDICATOR 1
#define TRAY_APPINDICATOR

#include "tray.h"
...
Expand All @@ -42,12 +42,14 @@ Before you can compile `tray`, you'll need to add an environment definition befo
#include <stdio.h>
#include <string.h>

#define TRAY_APPKIT 1
#define TRAY_APPKIT

#include "tray.h"
...
```

Failure to define one of the three above will result in a compile-time error.

# Demo

The included example `.c` files can be compiled based on your environment.
Expand All @@ -67,12 +69,16 @@ $> a [Enter]
# Example

```c
struct tray_menu menus[] = {
{ "Toggle me", 0, 0, toggle_cb, NULL },
{ "-" , 0, 0, NULL , NULL },
{ "Quit" , 0, 0, quit_cb , NULL },
{ NULL , 0, 0, NULL , NULL }
};

struct tray tray = {
.icon = "icon.png",
.menu = (struct tray_menu[]){{"Toggle me", 0, 0, toggle_cb, NULL},
{"-", 0, 0, NULL, NULL},
{"Quit", 0, 0, quit_cb, NULL},
{NULL, 0, 0, NULL, NULL}},
.menu = menus,
};

void toggle_cb(struct tray_menu *item) {
Expand Down
11 changes: 4 additions & 7 deletions tray.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
struct tray_menu;

struct tray {
char *icon;
const char *icon;
struct tray_menu *menu;
};

struct tray_menu {
char *text;
const char *text;
int disabled;
int checked;

Expand Down Expand Up @@ -279,7 +279,7 @@ static HMENU _tray_menu(struct tray_menu *m, UINT *id) {
item.fState |= MFS_CHECKED;
}
item.wID = *id;
item.dwTypeData = m->text;
item.dwTypeData = (LPSTR)m->text;
item.dwItemData = (ULONG_PTR)m;

InsertMenuItem(hmenu, *id, TRUE, &item);
Expand Down Expand Up @@ -361,10 +361,7 @@ static void tray_exit() {
UnregisterClass(WC_TRAY_CLASS_NAME, GetModuleHandle(NULL));
}
#else
static int tray_init(struct tray *tray) { return -1; }
static int tray_loop(int blocking) { return -1; }
static void tray_update(struct tray *tray) {}
static void tray_exit();
#error Please define TRAY_WINAPI, TRAY_APPINDICATOR or TRAY_APPKIT before including this file.
#endif

#endif /* TRAY_H */