forked from thomashope/native-menu-bar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnative_menu_bar.h
More file actions
196 lines (155 loc) · 5.74 KB
/
native_menu_bar.h
File metadata and controls
196 lines (155 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#ifndef NATIVE_MENU_BAR_H
#define NATIVE_MENU_BAR_H
/*
Native Menu Bar - A simple cross-platform C library for adding native menu bars to your game or app.
For an explanation of how to use it, see function comments and example usage at end of file.
This project is released into the public domain under the terms of the UNLICENSE.
*/
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef void* nmb_Handle;
typedef enum nmb_Platform
{
nmb_Platform_windows,
nmb_Platform_macos
} nmb_Platform;
typedef enum nmb_EventType
{
nmb_EventType_none,
nmb_EventType_itemTriggered
} nmb_EventType;
typedef struct nmb_Event
{
nmb_EventType event;
nmb_Handle sender; /* The handle of the menu item that issued the event. */
} nmb_Event;
/** Call once at the start of your application to set up Native Menu Bar.
*
* @param windowHandle On Windows you should pass the HWND of your main window. On macOS you should pass NULL.
*/
void nmb_setup(void* windowHandle);
/** Call once at the end of your application life cycle to clean up. */
void nmb_shutdown();
/** Every frame (or other regular interval) call this function in a loop to retrieve events sent by menus and respond to them. Once it returns false, all current events have been processed.
*
* @param event Pointer to an nmb_Event structure to be populated.
* @return true if an event was written to the event pointer, false if there are no events to process at the moment.
*/
bool nmb_pollEvent(nmb_Event* event);
/** Get the platform on which the application is running. You may call this function before calling nmb_setup().
*
* @return The platform enum value indicating the current platform.
*/
nmb_Platform nmb_getPlatform(void);
/** Append a new submenu to the given parent menu.
*
* @param parent The parent menu handle, or NULL to create a top-level menu.
* @param caption The name of the submenu.
* @return The handle to the newly created submenu, or NULL on failure.
*/
nmb_Handle nmb_appendMenu(nmb_Handle parent, const char* caption);
/** Creates a new submenu as a child of the parent menu.
*
* @param parent The parent menu handle, or NULL to create a top-level menu.
* @param index The index at which to insert the submenu. If -1, it will be appended to the end.
* @param caption The name of the submenu.
* @return The handle to the newly created submenu, or NULL on failure.
*/
nmb_Handle nmb_insertMenu(nmb_Handle parent, int index, const char* caption);
/** Append a new menu item to the given parent menu. You should store the returned handle so you can respond to events from this menu item later.
*
* @param parent The parent menu handle.
* @param caption The name of the menu item.
* @return The handle to the newly created menu item, or NULL on failure.
*/
nmb_Handle nmb_appendMenuItem(nmb_Handle parent, const char* caption);
/** Creates a new menu item as a child of the parent menu. You should store the returned handle so you can respond to events from this menu item later.
*
* @param parent The parent menu handle.
* @param index The index at which to insert the menu item. If -1, it will be appended to the end.
* @param caption The name of the menu item.
* @return The handle to the newly created menu item, or NULL on failure.
*/
nmb_Handle nmb_insertMenuItem(nmb_Handle parent, int index, const char* caption);
/** Appends a separator line to the given parent menu. */
void nmb_appendSeparator(nmb_Handle parent);
/** Inserts a separator line at the specified index in the parent menu. If index is -1, it will be appended to the end. */
void nmb_insertSeparator(nmb_Handle parent, int index);
/** Sets the checked state of the given menu item. */
void nmb_setMenuItemChecked(nmb_Handle menuItem, bool checked);
/** Returns true if the given menu item is checked. */
bool nmb_isMenuItemChecked(nmb_Handle menuItem);
/** Sets the enabled state of the given menu item. When an item is disabled it is greyed out and cannot be clicked. */
void nmb_setMenuItemEnabled(nmb_Handle menuItem, bool enabled);
/** Returns true if the given menu item is enabled. */
bool nmb_isMenuItemEnabled(nmb_Handle menuItem);
/** If a function fails, call this to get a null-terminated string describing the error. The memory for this string is managed internally, do not free it. */
const char* nmb_getLastError(void);
#ifdef __cplusplus
}
#endif
#endif
#if 0 /* EXAMPLE USAGE */
/* in your app setup function: */
// Initialise the Native Menu Bar
if (nmb_getPlatform() == nmb_Platform_windows)
{
nmb_setup(hWnd); // On Wondows, pass the HWND of your main window
}
else if (nmb_getPlatform() == nmb_Platform_macos)
{
nmb_setup(NULL); // Pass NULL on macOS
}
// build your menu bar
nmb_Handle hFileMenu = nmb_appendMenu(hMenuBar, "File");
nmb_Handle hEditMenu = nmb_appendMenu(hMenuBar, "Edit");
g_hFileNew = nmb_appendMenuItem(hFileMenu, "New");
g_hFileOpen = nmb_appendMenuItem(hFileMenu, "Open...");
g_hFileSave = nmb_appendMenuItem(hFileMenu, "Save");
if (nmb_getPlatform() == nmb_Platform_windows)
{
nmb_appendSeparator(hFileMenu);
g_hFileExit = nmb_appendMenuItem(hFileMenu, "Exit");
}
g_hEditCopy = nmb_appendMenuItem(hEditMenu, "Copy");
g_hEditPaste = nmb_appendMenuItem(hEditMenu, "Paste");
/* In your frame loop: */
bool running = true;
while (running)
{
// Respond to menu events
nmb_Event e;
while (nmb_pollEvent(&e))
{
if (e.sender == g_hFileNew)
{
printf("New file selected\n");
}
else if (e.sender == g_hFileOpen)
{
printf("Open file selected\n");
}
else if (e.sender == g_hFileSave)
{
printf("Save file selected\n");
}
else if (e.sender == g_hFileExit)
{
running = false; // Exit the app
}
else if (e.sender == g_hEditCopy)
{
printf("Copy selected\n");
}
else if (e.sender == g_hEditPaste)
{
printf("Paste selected\n");
}
}
// ... your code ...
}
// cleanup
nmb_shutdown();
#endif