diff options
Diffstat (limited to 'contrib/SDL-3.2.8/test/testtray.c')
| -rw-r--r-- | contrib/SDL-3.2.8/test/testtray.c | 645 |
1 files changed, 645 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/test/testtray.c b/contrib/SDL-3.2.8/test/testtray.c new file mode 100644 index 0000000..fdb12da --- /dev/null +++ b/contrib/SDL-3.2.8/test/testtray.c | |||
| @@ -0,0 +1,645 @@ | |||
| 1 | #include <SDL3/SDL.h> | ||
| 2 | #include <SDL3/SDL_main.h> | ||
| 3 | #include <SDL3/SDL_test.h> | ||
| 4 | |||
| 5 | static void SDLCALL tray_quit(void *ptr, SDL_TrayEntry *entry) | ||
| 6 | { | ||
| 7 | SDL_Event e; | ||
| 8 | e.type = SDL_EVENT_QUIT; | ||
| 9 | SDL_PushEvent(&e); | ||
| 10 | } | ||
| 11 | |||
| 12 | static bool trays_destroyed = false; | ||
| 13 | |||
| 14 | static void SDLCALL tray_close(void *ptr, SDL_TrayEntry *entry) | ||
| 15 | { | ||
| 16 | SDL_Tray **trays = (SDL_Tray **) ptr; | ||
| 17 | |||
| 18 | trays_destroyed = true; | ||
| 19 | |||
| 20 | SDL_DestroyTray(trays[0]); | ||
| 21 | SDL_DestroyTray(trays[1]); | ||
| 22 | } | ||
| 23 | |||
| 24 | static void SDLCALL apply_icon(void *ptr, const char * const *filelist, int filter) | ||
| 25 | { | ||
| 26 | if (!*filelist) { | ||
| 27 | return; | ||
| 28 | } | ||
| 29 | |||
| 30 | SDL_Surface *icon = SDL_LoadBMP(*filelist); | ||
| 31 | |||
| 32 | if (!icon) { | ||
| 33 | SDL_Log("Couldn't load icon '%s': %s", *filelist, SDL_GetError()); | ||
| 34 | return; | ||
| 35 | } | ||
| 36 | |||
| 37 | SDL_Tray *tray = (SDL_Tray *) ptr; | ||
| 38 | SDL_SetTrayIcon(tray, icon); | ||
| 39 | |||
| 40 | SDL_DestroySurface(icon); | ||
| 41 | } | ||
| 42 | |||
| 43 | static void SDLCALL change_icon(void *ptr, SDL_TrayEntry *entry) | ||
| 44 | { | ||
| 45 | SDL_DialogFileFilter filters[] = { | ||
| 46 | { "BMP image files", "bmp" }, | ||
| 47 | { "All files", "*" }, | ||
| 48 | }; | ||
| 49 | |||
| 50 | SDL_ShowOpenFileDialog(apply_icon, ptr, NULL, filters, 2, NULL, 0); | ||
| 51 | } | ||
| 52 | |||
| 53 | static void SDLCALL print_entry(void *ptr, SDL_TrayEntry *entry) | ||
| 54 | { | ||
| 55 | SDL_Log("Clicked on button '%s'", SDL_GetTrayEntryLabel(entry)); | ||
| 56 | } | ||
| 57 | |||
| 58 | static void SDLCALL set_entry_enabled(void *ptr, SDL_TrayEntry *entry) | ||
| 59 | { | ||
| 60 | SDL_TrayEntry *target = (SDL_TrayEntry *) ptr; | ||
| 61 | SDL_SetTrayEntryEnabled(target, true); | ||
| 62 | } | ||
| 63 | |||
| 64 | static void SDLCALL set_entry_disabled(void *ptr, SDL_TrayEntry *entry) | ||
| 65 | { | ||
| 66 | SDL_TrayEntry *target = (SDL_TrayEntry *) ptr; | ||
| 67 | SDL_SetTrayEntryEnabled(target, false); | ||
| 68 | } | ||
| 69 | |||
| 70 | static void SDLCALL set_entry_checked(void *ptr, SDL_TrayEntry *entry) | ||
| 71 | { | ||
| 72 | SDL_TrayEntry *target = (SDL_TrayEntry *) ptr; | ||
| 73 | SDL_SetTrayEntryChecked(target, true); | ||
| 74 | } | ||
| 75 | |||
| 76 | static void SDLCALL set_entry_unchecked(void *ptr, SDL_TrayEntry *entry) | ||
| 77 | { | ||
| 78 | SDL_TrayEntry *target = (SDL_TrayEntry *) ptr; | ||
| 79 | SDL_SetTrayEntryChecked(target, false); | ||
| 80 | } | ||
| 81 | |||
| 82 | static void SDLCALL remove_entry(void *ptr, SDL_TrayEntry *entry) | ||
| 83 | { | ||
| 84 | SDL_TrayEntry *target = (SDL_TrayEntry *) ptr; | ||
| 85 | SDL_RemoveTrayEntry(target); | ||
| 86 | |||
| 87 | SDL_TrayMenu *ctrl_submenu = SDL_GetTrayEntryParent(entry); | ||
| 88 | SDL_TrayEntry *ctrl_entry = SDL_GetTrayMenuParentEntry(ctrl_submenu); | ||
| 89 | |||
| 90 | if (!ctrl_entry) { | ||
| 91 | SDL_Log("Attempt to remove a menu that isn't a submenu. This shouldn't happen."); | ||
| 92 | return; | ||
| 93 | } | ||
| 94 | |||
| 95 | SDL_RemoveTrayEntry(ctrl_entry); | ||
| 96 | } | ||
| 97 | |||
| 98 | static void SDLCALL append_button_to(void *ptr, SDL_TrayEntry *entry) | ||
| 99 | { | ||
| 100 | SDL_TrayMenu *menu = (SDL_TrayMenu *) ptr; | ||
| 101 | SDL_TrayMenu *submenu; | ||
| 102 | SDL_TrayEntry *new_ctrl; | ||
| 103 | SDL_TrayEntry *new_ctrl_remove; | ||
| 104 | SDL_TrayEntry *new_ctrl_enabled; | ||
| 105 | SDL_TrayEntry *new_ctrl_disabled; | ||
| 106 | SDL_TrayEntry *new_example; | ||
| 107 | |||
| 108 | new_ctrl = SDL_InsertTrayEntryAt(SDL_GetTrayEntryParent(entry), -1, "New button", SDL_TRAYENTRY_SUBMENU); | ||
| 109 | |||
| 110 | if (!new_ctrl) { | ||
| 111 | SDL_Log("Couldn't insert entry in control tray: %s", SDL_GetError()); | ||
| 112 | return; | ||
| 113 | } | ||
| 114 | |||
| 115 | /* ---------- */ | ||
| 116 | |||
| 117 | submenu = SDL_CreateTraySubmenu(new_ctrl); | ||
| 118 | |||
| 119 | if (!new_ctrl) { | ||
| 120 | SDL_Log("Couldn't create control tray entry submenu: %s", SDL_GetError()); | ||
| 121 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 122 | return; | ||
| 123 | } | ||
| 124 | |||
| 125 | /* ---------- */ | ||
| 126 | |||
| 127 | new_example = SDL_InsertTrayEntryAt(menu, -1, "New button", SDL_TRAYENTRY_BUTTON); | ||
| 128 | |||
| 129 | if (new_example == NULL) { | ||
| 130 | SDL_Log("Couldn't insert entry in example tray: %s", SDL_GetError()); | ||
| 131 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 132 | return; | ||
| 133 | } | ||
| 134 | |||
| 135 | SDL_SetTrayEntryCallback(new_example, print_entry, NULL); | ||
| 136 | |||
| 137 | /* ---------- */ | ||
| 138 | |||
| 139 | new_ctrl_remove = SDL_InsertTrayEntryAt(submenu, -1, "Remove", SDL_TRAYENTRY_BUTTON); | ||
| 140 | |||
| 141 | if (new_ctrl_remove == NULL) { | ||
| 142 | SDL_Log("Couldn't insert new_ctrl_remove: %s", SDL_GetError()); | ||
| 143 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 144 | SDL_RemoveTrayEntry(new_example); | ||
| 145 | return; | ||
| 146 | } | ||
| 147 | |||
| 148 | SDL_SetTrayEntryCallback(new_ctrl_remove, remove_entry, new_example); | ||
| 149 | |||
| 150 | /* ---------- */ | ||
| 151 | |||
| 152 | new_ctrl_enabled = SDL_InsertTrayEntryAt(submenu, -1, "Enable", SDL_TRAYENTRY_BUTTON); | ||
| 153 | |||
| 154 | if (new_ctrl_enabled == NULL) { | ||
| 155 | SDL_Log("Couldn't insert new_ctrl_enabled: %s", SDL_GetError()); | ||
| 156 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 157 | SDL_RemoveTrayEntry(new_example); | ||
| 158 | return; | ||
| 159 | } | ||
| 160 | |||
| 161 | SDL_SetTrayEntryCallback(new_ctrl_enabled, set_entry_enabled, new_example); | ||
| 162 | |||
| 163 | /* ---------- */ | ||
| 164 | |||
| 165 | new_ctrl_disabled = SDL_InsertTrayEntryAt(submenu, -1, "Disable", SDL_TRAYENTRY_BUTTON); | ||
| 166 | |||
| 167 | if (new_ctrl_disabled == NULL) { | ||
| 168 | SDL_Log("Couldn't insert new_ctrl_disabled: %s", SDL_GetError()); | ||
| 169 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 170 | SDL_RemoveTrayEntry(new_example); | ||
| 171 | return; | ||
| 172 | } | ||
| 173 | |||
| 174 | SDL_SetTrayEntryCallback(new_ctrl_disabled, set_entry_disabled, new_example); | ||
| 175 | } | ||
| 176 | |||
| 177 | static void SDLCALL append_checkbox_to(void *ptr, SDL_TrayEntry *entry) | ||
| 178 | { | ||
| 179 | SDL_TrayMenu *menu = (SDL_TrayMenu *) ptr; | ||
| 180 | SDL_TrayMenu *submenu; | ||
| 181 | SDL_TrayEntry *new_ctrl; | ||
| 182 | SDL_TrayEntry *new_ctrl_remove; | ||
| 183 | SDL_TrayEntry *new_ctrl_enabled; | ||
| 184 | SDL_TrayEntry *new_ctrl_disabled; | ||
| 185 | SDL_TrayEntry *new_ctrl_checked; | ||
| 186 | SDL_TrayEntry *new_ctrl_unchecked; | ||
| 187 | SDL_TrayEntry *new_example; | ||
| 188 | |||
| 189 | new_ctrl = SDL_InsertTrayEntryAt(SDL_GetTrayEntryParent(entry), -1, "New checkbox", SDL_TRAYENTRY_SUBMENU); | ||
| 190 | |||
| 191 | if (!new_ctrl) { | ||
| 192 | SDL_Log("Couldn't insert entry in control tray: %s", SDL_GetError()); | ||
| 193 | return; | ||
| 194 | } | ||
| 195 | |||
| 196 | /* ---------- */ | ||
| 197 | |||
| 198 | submenu = SDL_CreateTraySubmenu(new_ctrl); | ||
| 199 | |||
| 200 | if (!new_ctrl) { | ||
| 201 | SDL_Log("Couldn't create control tray entry submenu: %s", SDL_GetError()); | ||
| 202 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 203 | return; | ||
| 204 | } | ||
| 205 | |||
| 206 | /* ---------- */ | ||
| 207 | |||
| 208 | new_example = SDL_InsertTrayEntryAt(menu, -1, "New checkbox", SDL_TRAYENTRY_CHECKBOX); | ||
| 209 | |||
| 210 | if (new_example == NULL) { | ||
| 211 | SDL_Log("Couldn't insert entry in example tray: %s", SDL_GetError()); | ||
| 212 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 213 | return; | ||
| 214 | } | ||
| 215 | |||
| 216 | SDL_SetTrayEntryCallback(new_example, print_entry, NULL); | ||
| 217 | |||
| 218 | /* ---------- */ | ||
| 219 | |||
| 220 | new_ctrl_remove = SDL_InsertTrayEntryAt(submenu, -1, "Remove", SDL_TRAYENTRY_BUTTON); | ||
| 221 | |||
| 222 | if (new_ctrl_remove == NULL) { | ||
| 223 | SDL_Log("Couldn't insert new_ctrl_remove: %s", SDL_GetError()); | ||
| 224 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 225 | SDL_RemoveTrayEntry(new_example); | ||
| 226 | return; | ||
| 227 | } | ||
| 228 | |||
| 229 | SDL_SetTrayEntryCallback(new_ctrl_remove, remove_entry, new_example); | ||
| 230 | |||
| 231 | /* ---------- */ | ||
| 232 | |||
| 233 | new_ctrl_enabled = SDL_InsertTrayEntryAt(submenu, -1, "Enable", SDL_TRAYENTRY_BUTTON); | ||
| 234 | |||
| 235 | if (new_ctrl_enabled == NULL) { | ||
| 236 | SDL_Log("Couldn't insert new_ctrl_enabled: %s", SDL_GetError()); | ||
| 237 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 238 | SDL_RemoveTrayEntry(new_example); | ||
| 239 | return; | ||
| 240 | } | ||
| 241 | |||
| 242 | SDL_SetTrayEntryCallback(new_ctrl_enabled, set_entry_enabled, new_example); | ||
| 243 | |||
| 244 | /* ---------- */ | ||
| 245 | |||
| 246 | new_ctrl_disabled = SDL_InsertTrayEntryAt(submenu, -1, "Disable", SDL_TRAYENTRY_BUTTON); | ||
| 247 | |||
| 248 | if (new_ctrl_disabled == NULL) { | ||
| 249 | SDL_Log("Couldn't insert new_ctrl_disabled: %s", SDL_GetError()); | ||
| 250 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 251 | SDL_RemoveTrayEntry(new_example); | ||
| 252 | return; | ||
| 253 | } | ||
| 254 | |||
| 255 | SDL_SetTrayEntryCallback(new_ctrl_disabled, set_entry_disabled, new_example); | ||
| 256 | |||
| 257 | /* ---------- */ | ||
| 258 | |||
| 259 | new_ctrl_checked = SDL_InsertTrayEntryAt(submenu, -1, "Check", SDL_TRAYENTRY_BUTTON); | ||
| 260 | |||
| 261 | if (new_ctrl_checked == NULL) { | ||
| 262 | SDL_Log("Couldn't insert new_ctrl_checked: %s", SDL_GetError()); | ||
| 263 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 264 | SDL_RemoveTrayEntry(new_example); | ||
| 265 | return; | ||
| 266 | } | ||
| 267 | |||
| 268 | SDL_SetTrayEntryCallback(new_ctrl_checked, set_entry_checked, new_example); | ||
| 269 | |||
| 270 | /* ---------- */ | ||
| 271 | |||
| 272 | new_ctrl_unchecked = SDL_InsertTrayEntryAt(submenu, -1, "Uncheck", SDL_TRAYENTRY_BUTTON); | ||
| 273 | |||
| 274 | if (new_ctrl_unchecked == NULL) { | ||
| 275 | SDL_Log("Couldn't insert new_ctrl_unchecked: %s", SDL_GetError()); | ||
| 276 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 277 | SDL_RemoveTrayEntry(new_example); | ||
| 278 | return; | ||
| 279 | } | ||
| 280 | |||
| 281 | SDL_SetTrayEntryCallback(new_ctrl_unchecked, set_entry_unchecked, new_example); | ||
| 282 | } | ||
| 283 | |||
| 284 | static void SDLCALL append_separator_to(void *ptr, SDL_TrayEntry *entry) | ||
| 285 | { | ||
| 286 | SDL_TrayMenu *menu = (SDL_TrayMenu *) ptr; | ||
| 287 | SDL_TrayMenu *submenu; | ||
| 288 | SDL_TrayEntry *new_ctrl; | ||
| 289 | SDL_TrayEntry *new_ctrl_remove; | ||
| 290 | SDL_TrayEntry *new_example; | ||
| 291 | |||
| 292 | new_ctrl = SDL_InsertTrayEntryAt(SDL_GetTrayEntryParent(entry), -1, "[Separator]", SDL_TRAYENTRY_SUBMENU); | ||
| 293 | |||
| 294 | if (!new_ctrl) { | ||
| 295 | SDL_Log("Couldn't insert entry in control tray: %s", SDL_GetError()); | ||
| 296 | return; | ||
| 297 | } | ||
| 298 | |||
| 299 | /* ---------- */ | ||
| 300 | |||
| 301 | submenu = SDL_CreateTraySubmenu(new_ctrl); | ||
| 302 | |||
| 303 | if (!new_ctrl) { | ||
| 304 | SDL_Log("Couldn't create control tray entry submenu: %s", SDL_GetError()); | ||
| 305 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 306 | return; | ||
| 307 | } | ||
| 308 | |||
| 309 | /* ---------- */ | ||
| 310 | |||
| 311 | new_example = SDL_InsertTrayEntryAt(menu, -1, NULL, SDL_TRAYENTRY_BUTTON); | ||
| 312 | |||
| 313 | if (new_example == NULL) { | ||
| 314 | SDL_Log("Couldn't insert separator in example tray: %s", SDL_GetError()); | ||
| 315 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 316 | return; | ||
| 317 | } | ||
| 318 | |||
| 319 | /* ---------- */ | ||
| 320 | |||
| 321 | new_ctrl_remove = SDL_InsertTrayEntryAt(submenu, -1, "Remove", SDL_TRAYENTRY_BUTTON); | ||
| 322 | |||
| 323 | if (new_ctrl_remove == NULL) { | ||
| 324 | SDL_Log("Couldn't insert new_ctrl_remove: %s", SDL_GetError()); | ||
| 325 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 326 | SDL_RemoveTrayEntry(new_example); | ||
| 327 | return; | ||
| 328 | } | ||
| 329 | |||
| 330 | SDL_SetTrayEntryCallback(new_ctrl_remove, remove_entry, new_example); | ||
| 331 | } | ||
| 332 | |||
| 333 | static void SDLCALL append_submenu_to(void *ptr, SDL_TrayEntry *entry) | ||
| 334 | { | ||
| 335 | SDL_TrayMenu *menu = (SDL_TrayMenu *) ptr; | ||
| 336 | SDL_TrayMenu *submenu; | ||
| 337 | SDL_TrayMenu *entry_submenu; | ||
| 338 | SDL_TrayEntry *new_ctrl; | ||
| 339 | SDL_TrayEntry *new_ctrl_remove; | ||
| 340 | SDL_TrayEntry *new_ctrl_enabled; | ||
| 341 | SDL_TrayEntry *new_ctrl_disabled; | ||
| 342 | SDL_TrayEntry *new_example; | ||
| 343 | |||
| 344 | new_ctrl = SDL_InsertTrayEntryAt(SDL_GetTrayEntryParent(entry), -1, "New submenu", SDL_TRAYENTRY_SUBMENU); | ||
| 345 | |||
| 346 | if (!new_ctrl) { | ||
| 347 | SDL_Log("Couldn't insert entry in control tray: %s", SDL_GetError()); | ||
| 348 | return; | ||
| 349 | } | ||
| 350 | |||
| 351 | /* ---------- */ | ||
| 352 | |||
| 353 | submenu = SDL_CreateTraySubmenu(new_ctrl); | ||
| 354 | |||
| 355 | if (!new_ctrl) { | ||
| 356 | SDL_Log("Couldn't create control tray entry submenu: %s", SDL_GetError()); | ||
| 357 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 358 | return; | ||
| 359 | } | ||
| 360 | |||
| 361 | /* ---------- */ | ||
| 362 | |||
| 363 | new_example = SDL_InsertTrayEntryAt(menu, -1, "New submenu", SDL_TRAYENTRY_SUBMENU); | ||
| 364 | |||
| 365 | if (new_example == NULL) { | ||
| 366 | SDL_Log("Couldn't insert entry in example tray: %s", SDL_GetError()); | ||
| 367 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 368 | return; | ||
| 369 | } | ||
| 370 | |||
| 371 | SDL_SetTrayEntryCallback(new_example, print_entry, NULL); | ||
| 372 | |||
| 373 | /* ---------- */ | ||
| 374 | |||
| 375 | entry_submenu = SDL_CreateTraySubmenu(new_example); | ||
| 376 | |||
| 377 | if (entry_submenu == NULL) { | ||
| 378 | SDL_Log("Couldn't create new entry submenu: %s", SDL_GetError()); | ||
| 379 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 380 | SDL_RemoveTrayEntry(new_example); | ||
| 381 | return; | ||
| 382 | } | ||
| 383 | |||
| 384 | /* ---------- */ | ||
| 385 | |||
| 386 | new_ctrl_remove = SDL_InsertTrayEntryAt(submenu, -1, "Remove", SDL_TRAYENTRY_BUTTON); | ||
| 387 | |||
| 388 | if (new_ctrl_remove == NULL) { | ||
| 389 | SDL_Log("Couldn't insert new_ctrl_remove: %s", SDL_GetError()); | ||
| 390 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 391 | SDL_RemoveTrayEntry(new_example); | ||
| 392 | return; | ||
| 393 | } | ||
| 394 | |||
| 395 | SDL_SetTrayEntryCallback(new_ctrl_remove, remove_entry, new_example); | ||
| 396 | |||
| 397 | /* ---------- */ | ||
| 398 | |||
| 399 | new_ctrl_enabled = SDL_InsertTrayEntryAt(submenu, -1, "Enable", SDL_TRAYENTRY_BUTTON); | ||
| 400 | |||
| 401 | if (new_ctrl_enabled == NULL) { | ||
| 402 | SDL_Log("Couldn't insert new_ctrl_enabled: %s", SDL_GetError()); | ||
| 403 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 404 | SDL_RemoveTrayEntry(new_example); | ||
| 405 | return; | ||
| 406 | } | ||
| 407 | |||
| 408 | SDL_SetTrayEntryCallback(new_ctrl_enabled, set_entry_enabled, new_example); | ||
| 409 | |||
| 410 | /* ---------- */ | ||
| 411 | |||
| 412 | new_ctrl_disabled = SDL_InsertTrayEntryAt(submenu, -1, "Disable", SDL_TRAYENTRY_BUTTON); | ||
| 413 | |||
| 414 | if (new_ctrl_disabled == NULL) { | ||
| 415 | SDL_Log("Couldn't insert new_ctrl_disabled: %s", SDL_GetError()); | ||
| 416 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 417 | SDL_RemoveTrayEntry(new_example); | ||
| 418 | return; | ||
| 419 | } | ||
| 420 | |||
| 421 | SDL_SetTrayEntryCallback(new_ctrl_disabled, set_entry_disabled, new_example); | ||
| 422 | |||
| 423 | /* ---------- */ | ||
| 424 | |||
| 425 | SDL_InsertTrayEntryAt(submenu, -1, NULL, 0); | ||
| 426 | |||
| 427 | /* ---------- */ | ||
| 428 | |||
| 429 | SDL_TrayEntry *entry_newbtn = SDL_InsertTrayEntryAt(submenu, -1, "Create button", SDL_TRAYENTRY_BUTTON); | ||
| 430 | |||
| 431 | if (entry_newbtn == NULL) { | ||
| 432 | SDL_Log("Couldn't insert entry_newbtn: %s", SDL_GetError()); | ||
| 433 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 434 | SDL_RemoveTrayEntry(new_example); | ||
| 435 | return; | ||
| 436 | } | ||
| 437 | |||
| 438 | SDL_SetTrayEntryCallback(entry_newbtn, append_button_to, entry_submenu); | ||
| 439 | |||
| 440 | /* ---------- */ | ||
| 441 | |||
| 442 | SDL_TrayEntry *entry_newchk = SDL_InsertTrayEntryAt(submenu, -1, "Create checkbox", SDL_TRAYENTRY_BUTTON); | ||
| 443 | |||
| 444 | if (entry_newchk == NULL) { | ||
| 445 | SDL_Log("Couldn't insert entry_newchk: %s", SDL_GetError()); | ||
| 446 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 447 | SDL_RemoveTrayEntry(new_example); | ||
| 448 | return; | ||
| 449 | } | ||
| 450 | |||
| 451 | SDL_SetTrayEntryCallback(entry_newchk, append_checkbox_to, entry_submenu); | ||
| 452 | |||
| 453 | /* ---------- */ | ||
| 454 | |||
| 455 | SDL_TrayEntry *entry_newsub = SDL_InsertTrayEntryAt(submenu, -1, "Create submenu", SDL_TRAYENTRY_BUTTON); | ||
| 456 | |||
| 457 | if (entry_newsub == NULL) { | ||
| 458 | SDL_Log("Couldn't insert entry_newsub: %s", SDL_GetError()); | ||
| 459 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 460 | SDL_RemoveTrayEntry(new_example); | ||
| 461 | return; | ||
| 462 | } | ||
| 463 | |||
| 464 | SDL_SetTrayEntryCallback(entry_newsub, append_submenu_to, entry_submenu); | ||
| 465 | |||
| 466 | /* ---------- */ | ||
| 467 | |||
| 468 | SDL_TrayEntry *entry_newsep = SDL_InsertTrayEntryAt(submenu, -1, "Create separator", SDL_TRAYENTRY_BUTTON); | ||
| 469 | |||
| 470 | if (entry_newsep == NULL) { | ||
| 471 | SDL_Log("Couldn't insert entry_newsep: %s", SDL_GetError()); | ||
| 472 | SDL_RemoveTrayEntry(new_ctrl); | ||
| 473 | SDL_RemoveTrayEntry(new_example); | ||
| 474 | return; | ||
| 475 | } | ||
| 476 | |||
| 477 | SDL_SetTrayEntryCallback(entry_newsep, append_separator_to, entry_submenu); | ||
| 478 | |||
| 479 | /* ---------- */ | ||
| 480 | |||
| 481 | SDL_InsertTrayEntryAt(submenu, -1, NULL, 0); | ||
| 482 | } | ||
| 483 | |||
| 484 | int main(int argc, char **argv) | ||
| 485 | { | ||
| 486 | SDL_Tray **trays = NULL; | ||
| 487 | SDLTest_CommonState *state; | ||
| 488 | int i; | ||
| 489 | |||
| 490 | /* Initialize test framework */ | ||
| 491 | state = SDLTest_CommonCreateState(argv, 0); | ||
| 492 | if (state == NULL) { | ||
| 493 | return 1; | ||
| 494 | } | ||
| 495 | |||
| 496 | /* Parse commandline */ | ||
| 497 | for (i = 1; i < argc;) { | ||
| 498 | int consumed; | ||
| 499 | |||
| 500 | consumed = SDLTest_CommonArg(state, i); | ||
| 501 | |||
| 502 | if (consumed <= 0) { | ||
| 503 | static const char *options[] = { NULL }; | ||
| 504 | SDLTest_CommonLogUsage(state, argv[0], options); | ||
| 505 | return 1; | ||
| 506 | } | ||
| 507 | |||
| 508 | i += consumed; | ||
| 509 | } | ||
| 510 | |||
| 511 | if (!SDL_Init(SDL_INIT_VIDEO)) { | ||
| 512 | SDL_Log("SDL_Init failed (%s)", SDL_GetError()); | ||
| 513 | return 1; | ||
| 514 | } | ||
| 515 | |||
| 516 | SDL_Window *w = SDL_CreateWindow("testtray", 640, 480, 0); | ||
| 517 | |||
| 518 | if (!w) { | ||
| 519 | SDL_Log("Couldn't create window: %s", SDL_GetError()); | ||
| 520 | goto quit; | ||
| 521 | } | ||
| 522 | |||
| 523 | /* TODO: Resource paths? */ | ||
| 524 | SDL_Surface *icon = SDL_LoadBMP("../test/sdl-test_round.bmp"); | ||
| 525 | |||
| 526 | if (!icon) { | ||
| 527 | SDL_Log("Couldn't load icon 1, proceeding without: %s", SDL_GetError()); | ||
| 528 | } | ||
| 529 | |||
| 530 | SDL_Surface *icon2 = SDL_LoadBMP("../test/speaker.bmp"); | ||
| 531 | |||
| 532 | if (!icon2) { | ||
| 533 | SDL_Log("Couldn't load icon 2, proceeding without: %s", SDL_GetError()); | ||
| 534 | } | ||
| 535 | |||
| 536 | SDL_Tray *tray = SDL_CreateTray(icon, "SDL Tray control menu"); | ||
| 537 | |||
| 538 | if (!tray) { | ||
| 539 | SDL_Log("Couldn't create control tray: %s", SDL_GetError()); | ||
| 540 | goto clean_window; | ||
| 541 | } | ||
| 542 | |||
| 543 | SDL_Tray *tray2 = SDL_CreateTray(icon2, "SDL Tray example"); | ||
| 544 | |||
| 545 | if (!tray2) { | ||
| 546 | SDL_Log("Couldn't create example tray: %s", SDL_GetError()); | ||
| 547 | goto clean_tray1; | ||
| 548 | } | ||
| 549 | |||
| 550 | SDL_DestroySurface(icon); | ||
| 551 | SDL_DestroySurface(icon2); | ||
| 552 | |||
| 553 | #define CHECK(name) \ | ||
| 554 | if (!name) { \ | ||
| 555 | SDL_Log("Couldn't create " #name ": %s", SDL_GetError()); \ | ||
| 556 | goto clean_all; \ | ||
| 557 | } | ||
| 558 | |||
| 559 | SDL_TrayMenu *menu = SDL_CreateTrayMenu(tray); | ||
| 560 | CHECK(menu); | ||
| 561 | |||
| 562 | SDL_TrayMenu *menu2 = SDL_CreateTrayMenu(tray2); | ||
| 563 | CHECK(menu2); | ||
| 564 | |||
| 565 | SDL_TrayEntry *entry_quit = SDL_InsertTrayEntryAt(menu, -1, "Quit", SDL_TRAYENTRY_BUTTON); | ||
| 566 | CHECK(entry_quit); | ||
| 567 | |||
| 568 | SDL_TrayEntry *entry_close = SDL_InsertTrayEntryAt(menu, -1, "Close", SDL_TRAYENTRY_BUTTON); | ||
| 569 | CHECK(entry_close); | ||
| 570 | |||
| 571 | /* TODO: Track memory! */ | ||
| 572 | trays = SDL_malloc(sizeof(SDL_Tray *) * 2); | ||
| 573 | if (!trays) { | ||
| 574 | goto clean_all; | ||
| 575 | } | ||
| 576 | |||
| 577 | trays[0] = tray; | ||
| 578 | trays[1] = tray2; | ||
| 579 | |||
| 580 | SDL_SetTrayEntryCallback(entry_quit, tray_quit, NULL); | ||
| 581 | SDL_SetTrayEntryCallback(entry_close, tray_close, trays); | ||
| 582 | |||
| 583 | SDL_InsertTrayEntryAt(menu, -1, NULL, 0); | ||
| 584 | |||
| 585 | SDL_TrayEntry *entry_icon = SDL_InsertTrayEntryAt(menu, -1, "Change icon", SDL_TRAYENTRY_BUTTON); | ||
| 586 | CHECK(entry_icon); | ||
| 587 | |||
| 588 | SDL_SetTrayEntryCallback(entry_icon, change_icon, tray2); | ||
| 589 | |||
| 590 | SDL_InsertTrayEntryAt(menu, -1, NULL, 0); | ||
| 591 | |||
| 592 | SDL_TrayEntry *entry_newbtn = SDL_InsertTrayEntryAt(menu, -1, "Create button", SDL_TRAYENTRY_BUTTON); | ||
| 593 | CHECK(entry_newbtn); | ||
| 594 | |||
| 595 | SDL_SetTrayEntryCallback(entry_newbtn, append_button_to, menu2); | ||
| 596 | |||
| 597 | SDL_TrayEntry *entry_newchk = SDL_InsertTrayEntryAt(menu, -1, "Create checkbox", SDL_TRAYENTRY_BUTTON); | ||
| 598 | CHECK(entry_newchk); | ||
| 599 | |||
| 600 | SDL_SetTrayEntryCallback(entry_newchk, append_checkbox_to, menu2); | ||
| 601 | |||
| 602 | SDL_TrayEntry *entry_newsub = SDL_InsertTrayEntryAt(menu, -1, "Create submenu", SDL_TRAYENTRY_BUTTON); | ||
| 603 | CHECK(entry_newsub); | ||
| 604 | |||
| 605 | SDL_SetTrayEntryCallback(entry_newsub, append_submenu_to, menu2); | ||
| 606 | |||
| 607 | SDL_TrayEntry *entry_newsep = SDL_InsertTrayEntryAt(menu, -1, "Create separator", SDL_TRAYENTRY_BUTTON); | ||
| 608 | CHECK(entry_newsep); | ||
| 609 | |||
| 610 | SDL_SetTrayEntryCallback(entry_newsep, append_separator_to, menu2); | ||
| 611 | |||
| 612 | SDL_InsertTrayEntryAt(menu, -1, NULL, 0); | ||
| 613 | |||
| 614 | SDL_Event e; | ||
| 615 | while (SDL_WaitEvent(&e)) { | ||
| 616 | if (e.type == SDL_EVENT_QUIT) { | ||
| 617 | break; | ||
| 618 | } else if (e.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) { | ||
| 619 | SDL_DestroyWindow(w); | ||
| 620 | w = NULL; | ||
| 621 | } | ||
| 622 | } | ||
| 623 | |||
| 624 | clean_all: | ||
| 625 | if (!trays_destroyed) { | ||
| 626 | SDL_DestroyTray(tray2); | ||
| 627 | } | ||
| 628 | |||
| 629 | clean_tray1: | ||
| 630 | if (!trays_destroyed) { | ||
| 631 | SDL_DestroyTray(tray); | ||
| 632 | } | ||
| 633 | SDL_free(trays); | ||
| 634 | |||
| 635 | clean_window: | ||
| 636 | if (w) { | ||
| 637 | SDL_DestroyWindow(w); | ||
| 638 | } | ||
| 639 | |||
| 640 | quit: | ||
| 641 | SDL_Quit(); | ||
| 642 | SDLTest_CommonDestroyState(state); | ||
| 643 | |||
| 644 | return 0; | ||
| 645 | } | ||
