testtray.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. #include "testutils.h"
  2. #include <SDL3/SDL.h>
  3. #include <SDL3/SDL_main.h>
  4. #include <SDL3/SDL_test.h>
  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. static bool trays_destroyed = false;
  12. static void SDLCALL tray_close(void *ptr, SDL_TrayEntry *entry)
  13. {
  14. SDL_Tray **trays = (SDL_Tray **) ptr;
  15. trays_destroyed = true;
  16. SDL_DestroyTray(trays[0]);
  17. SDL_DestroyTray(trays[1]);
  18. }
  19. static void SDLCALL apply_icon(void *ptr, const char * const *filelist, int filter)
  20. {
  21. if (!*filelist) {
  22. return;
  23. }
  24. SDL_Surface *icon = SDL_LoadBMP(*filelist);
  25. if (!icon) {
  26. SDL_Log("Couldn't load icon '%s': %s", *filelist, SDL_GetError());
  27. return;
  28. }
  29. SDL_Tray *tray = (SDL_Tray *) ptr;
  30. SDL_SetTrayIcon(tray, icon);
  31. SDL_DestroySurface(icon);
  32. }
  33. static void SDLCALL change_icon(void *ptr, SDL_TrayEntry *entry)
  34. {
  35. SDL_DialogFileFilter filters[] = {
  36. { "BMP image files", "bmp" },
  37. { "All files", "*" },
  38. };
  39. SDL_ShowOpenFileDialog(apply_icon, ptr, NULL, filters, 2, NULL, 0);
  40. }
  41. static void SDLCALL print_entry(void *ptr, SDL_TrayEntry *entry)
  42. {
  43. SDL_Log("Clicked on button '%s'", SDL_GetTrayEntryLabel(entry));
  44. }
  45. static void SDLCALL set_entry_enabled(void *ptr, SDL_TrayEntry *entry)
  46. {
  47. SDL_TrayEntry *target = (SDL_TrayEntry *) ptr;
  48. SDL_SetTrayEntryEnabled(target, true);
  49. }
  50. static void SDLCALL set_entry_disabled(void *ptr, SDL_TrayEntry *entry)
  51. {
  52. SDL_TrayEntry *target = (SDL_TrayEntry *) ptr;
  53. SDL_SetTrayEntryEnabled(target, false);
  54. }
  55. static void SDLCALL set_entry_checked(void *ptr, SDL_TrayEntry *entry)
  56. {
  57. SDL_TrayEntry *target = (SDL_TrayEntry *) ptr;
  58. SDL_SetTrayEntryChecked(target, true);
  59. }
  60. static void SDLCALL set_entry_unchecked(void *ptr, SDL_TrayEntry *entry)
  61. {
  62. SDL_TrayEntry *target = (SDL_TrayEntry *) ptr;
  63. SDL_SetTrayEntryChecked(target, false);
  64. }
  65. static void SDLCALL remove_entry(void *ptr, SDL_TrayEntry *entry)
  66. {
  67. SDL_TrayEntry *target = (SDL_TrayEntry *) ptr;
  68. SDL_RemoveTrayEntry(target);
  69. SDL_TrayMenu *ctrl_submenu = SDL_GetTrayEntryParent(entry);
  70. SDL_TrayEntry *ctrl_entry = SDL_GetTrayMenuParentEntry(ctrl_submenu);
  71. if (!ctrl_entry) {
  72. SDL_Log("Attempt to remove a menu that isn't a submenu. This shouldn't happen.");
  73. return;
  74. }
  75. SDL_RemoveTrayEntry(ctrl_entry);
  76. }
  77. static void SDLCALL append_button_to(void *ptr, SDL_TrayEntry *entry)
  78. {
  79. SDL_TrayMenu *menu = (SDL_TrayMenu *) ptr;
  80. SDL_TrayMenu *submenu;
  81. SDL_TrayEntry *new_ctrl;
  82. SDL_TrayEntry *new_ctrl_remove;
  83. SDL_TrayEntry *new_ctrl_enabled;
  84. SDL_TrayEntry *new_ctrl_disabled;
  85. SDL_TrayEntry *new_example;
  86. new_ctrl = SDL_InsertTrayEntryAt(SDL_GetTrayEntryParent(entry), -1, "New button", SDL_TRAYENTRY_SUBMENU);
  87. if (!new_ctrl) {
  88. SDL_Log("Couldn't insert entry in control tray: %s", SDL_GetError());
  89. return;
  90. }
  91. /* ---------- */
  92. submenu = SDL_CreateTraySubmenu(new_ctrl);
  93. if (!new_ctrl) {
  94. SDL_Log("Couldn't create control tray entry submenu: %s", SDL_GetError());
  95. SDL_RemoveTrayEntry(new_ctrl);
  96. return;
  97. }
  98. /* ---------- */
  99. new_example = SDL_InsertTrayEntryAt(menu, -1, "New button", SDL_TRAYENTRY_BUTTON);
  100. if (new_example == NULL) {
  101. SDL_Log("Couldn't insert entry in example tray: %s", SDL_GetError());
  102. SDL_RemoveTrayEntry(new_ctrl);
  103. return;
  104. }
  105. SDL_SetTrayEntryCallback(new_example, print_entry, NULL);
  106. /* ---------- */
  107. new_ctrl_remove = SDL_InsertTrayEntryAt(submenu, -1, "Remove", SDL_TRAYENTRY_BUTTON);
  108. if (new_ctrl_remove == NULL) {
  109. SDL_Log("Couldn't insert new_ctrl_remove: %s", SDL_GetError());
  110. SDL_RemoveTrayEntry(new_ctrl);
  111. SDL_RemoveTrayEntry(new_example);
  112. return;
  113. }
  114. SDL_SetTrayEntryCallback(new_ctrl_remove, remove_entry, new_example);
  115. /* ---------- */
  116. new_ctrl_enabled = SDL_InsertTrayEntryAt(submenu, -1, "Enable", SDL_TRAYENTRY_BUTTON);
  117. if (new_ctrl_enabled == NULL) {
  118. SDL_Log("Couldn't insert new_ctrl_enabled: %s", SDL_GetError());
  119. SDL_RemoveTrayEntry(new_ctrl);
  120. SDL_RemoveTrayEntry(new_example);
  121. return;
  122. }
  123. SDL_SetTrayEntryCallback(new_ctrl_enabled, set_entry_enabled, new_example);
  124. /* ---------- */
  125. new_ctrl_disabled = SDL_InsertTrayEntryAt(submenu, -1, "Disable", SDL_TRAYENTRY_BUTTON);
  126. if (new_ctrl_disabled == NULL) {
  127. SDL_Log("Couldn't insert new_ctrl_disabled: %s", SDL_GetError());
  128. SDL_RemoveTrayEntry(new_ctrl);
  129. SDL_RemoveTrayEntry(new_example);
  130. return;
  131. }
  132. SDL_SetTrayEntryCallback(new_ctrl_disabled, set_entry_disabled, new_example);
  133. }
  134. static void SDLCALL append_checkbox_to(void *ptr, SDL_TrayEntry *entry)
  135. {
  136. SDL_TrayMenu *menu = (SDL_TrayMenu *) ptr;
  137. SDL_TrayMenu *submenu;
  138. SDL_TrayEntry *new_ctrl;
  139. SDL_TrayEntry *new_ctrl_remove;
  140. SDL_TrayEntry *new_ctrl_enabled;
  141. SDL_TrayEntry *new_ctrl_disabled;
  142. SDL_TrayEntry *new_ctrl_checked;
  143. SDL_TrayEntry *new_ctrl_unchecked;
  144. SDL_TrayEntry *new_example;
  145. new_ctrl = SDL_InsertTrayEntryAt(SDL_GetTrayEntryParent(entry), -1, "New checkbox", SDL_TRAYENTRY_SUBMENU);
  146. if (!new_ctrl) {
  147. SDL_Log("Couldn't insert entry in control tray: %s", SDL_GetError());
  148. return;
  149. }
  150. /* ---------- */
  151. submenu = SDL_CreateTraySubmenu(new_ctrl);
  152. if (!new_ctrl) {
  153. SDL_Log("Couldn't create control tray entry submenu: %s", SDL_GetError());
  154. SDL_RemoveTrayEntry(new_ctrl);
  155. return;
  156. }
  157. /* ---------- */
  158. new_example = SDL_InsertTrayEntryAt(menu, -1, "New checkbox", SDL_TRAYENTRY_CHECKBOX);
  159. if (new_example == NULL) {
  160. SDL_Log("Couldn't insert entry in example tray: %s", SDL_GetError());
  161. SDL_RemoveTrayEntry(new_ctrl);
  162. return;
  163. }
  164. SDL_SetTrayEntryCallback(new_example, print_entry, NULL);
  165. /* ---------- */
  166. new_ctrl_remove = SDL_InsertTrayEntryAt(submenu, -1, "Remove", SDL_TRAYENTRY_BUTTON);
  167. if (new_ctrl_remove == NULL) {
  168. SDL_Log("Couldn't insert new_ctrl_remove: %s", SDL_GetError());
  169. SDL_RemoveTrayEntry(new_ctrl);
  170. SDL_RemoveTrayEntry(new_example);
  171. return;
  172. }
  173. SDL_SetTrayEntryCallback(new_ctrl_remove, remove_entry, new_example);
  174. /* ---------- */
  175. new_ctrl_enabled = SDL_InsertTrayEntryAt(submenu, -1, "Enable", SDL_TRAYENTRY_BUTTON);
  176. if (new_ctrl_enabled == NULL) {
  177. SDL_Log("Couldn't insert new_ctrl_enabled: %s", SDL_GetError());
  178. SDL_RemoveTrayEntry(new_ctrl);
  179. SDL_RemoveTrayEntry(new_example);
  180. return;
  181. }
  182. SDL_SetTrayEntryCallback(new_ctrl_enabled, set_entry_enabled, new_example);
  183. /* ---------- */
  184. new_ctrl_disabled = SDL_InsertTrayEntryAt(submenu, -1, "Disable", SDL_TRAYENTRY_BUTTON);
  185. if (new_ctrl_disabled == NULL) {
  186. SDL_Log("Couldn't insert new_ctrl_disabled: %s", SDL_GetError());
  187. SDL_RemoveTrayEntry(new_ctrl);
  188. SDL_RemoveTrayEntry(new_example);
  189. return;
  190. }
  191. SDL_SetTrayEntryCallback(new_ctrl_disabled, set_entry_disabled, new_example);
  192. /* ---------- */
  193. new_ctrl_checked = SDL_InsertTrayEntryAt(submenu, -1, "Check", SDL_TRAYENTRY_BUTTON);
  194. if (new_ctrl_checked == NULL) {
  195. SDL_Log("Couldn't insert new_ctrl_checked: %s", SDL_GetError());
  196. SDL_RemoveTrayEntry(new_ctrl);
  197. SDL_RemoveTrayEntry(new_example);
  198. return;
  199. }
  200. SDL_SetTrayEntryCallback(new_ctrl_checked, set_entry_checked, new_example);
  201. /* ---------- */
  202. new_ctrl_unchecked = SDL_InsertTrayEntryAt(submenu, -1, "Uncheck", SDL_TRAYENTRY_BUTTON);
  203. if (new_ctrl_unchecked == NULL) {
  204. SDL_Log("Couldn't insert new_ctrl_unchecked: %s", SDL_GetError());
  205. SDL_RemoveTrayEntry(new_ctrl);
  206. SDL_RemoveTrayEntry(new_example);
  207. return;
  208. }
  209. SDL_SetTrayEntryCallback(new_ctrl_unchecked, set_entry_unchecked, new_example);
  210. }
  211. static void SDLCALL append_separator_to(void *ptr, SDL_TrayEntry *entry)
  212. {
  213. SDL_TrayMenu *menu = (SDL_TrayMenu *) ptr;
  214. SDL_TrayMenu *submenu;
  215. SDL_TrayEntry *new_ctrl;
  216. SDL_TrayEntry *new_ctrl_remove;
  217. SDL_TrayEntry *new_example;
  218. new_ctrl = SDL_InsertTrayEntryAt(SDL_GetTrayEntryParent(entry), -1, "[Separator]", SDL_TRAYENTRY_SUBMENU);
  219. if (!new_ctrl) {
  220. SDL_Log("Couldn't insert entry in control tray: %s", SDL_GetError());
  221. return;
  222. }
  223. /* ---------- */
  224. submenu = SDL_CreateTraySubmenu(new_ctrl);
  225. if (!new_ctrl) {
  226. SDL_Log("Couldn't create control tray entry submenu: %s", SDL_GetError());
  227. SDL_RemoveTrayEntry(new_ctrl);
  228. return;
  229. }
  230. /* ---------- */
  231. new_example = SDL_InsertTrayEntryAt(menu, -1, NULL, SDL_TRAYENTRY_BUTTON);
  232. if (new_example == NULL) {
  233. SDL_Log("Couldn't insert separator in example tray: %s", SDL_GetError());
  234. SDL_RemoveTrayEntry(new_ctrl);
  235. return;
  236. }
  237. /* ---------- */
  238. new_ctrl_remove = SDL_InsertTrayEntryAt(submenu, -1, "Remove", SDL_TRAYENTRY_BUTTON);
  239. if (new_ctrl_remove == NULL) {
  240. SDL_Log("Couldn't insert new_ctrl_remove: %s", SDL_GetError());
  241. SDL_RemoveTrayEntry(new_ctrl);
  242. SDL_RemoveTrayEntry(new_example);
  243. return;
  244. }
  245. SDL_SetTrayEntryCallback(new_ctrl_remove, remove_entry, new_example);
  246. }
  247. static void SDLCALL append_submenu_to(void *ptr, SDL_TrayEntry *entry)
  248. {
  249. SDL_TrayMenu *menu = (SDL_TrayMenu *) ptr;
  250. SDL_TrayMenu *submenu;
  251. SDL_TrayMenu *entry_submenu;
  252. SDL_TrayEntry *new_ctrl;
  253. SDL_TrayEntry *new_ctrl_remove;
  254. SDL_TrayEntry *new_ctrl_enabled;
  255. SDL_TrayEntry *new_ctrl_disabled;
  256. SDL_TrayEntry *new_example;
  257. new_ctrl = SDL_InsertTrayEntryAt(SDL_GetTrayEntryParent(entry), -1, "New submenu", SDL_TRAYENTRY_SUBMENU);
  258. if (!new_ctrl) {
  259. SDL_Log("Couldn't insert entry in control tray: %s", SDL_GetError());
  260. return;
  261. }
  262. /* ---------- */
  263. submenu = SDL_CreateTraySubmenu(new_ctrl);
  264. if (!new_ctrl) {
  265. SDL_Log("Couldn't create control tray entry submenu: %s", SDL_GetError());
  266. SDL_RemoveTrayEntry(new_ctrl);
  267. return;
  268. }
  269. /* ---------- */
  270. new_example = SDL_InsertTrayEntryAt(menu, -1, "New submenu", SDL_TRAYENTRY_SUBMENU);
  271. if (new_example == NULL) {
  272. SDL_Log("Couldn't insert entry in example tray: %s", SDL_GetError());
  273. SDL_RemoveTrayEntry(new_ctrl);
  274. return;
  275. }
  276. SDL_SetTrayEntryCallback(new_example, print_entry, NULL);
  277. /* ---------- */
  278. entry_submenu = SDL_CreateTraySubmenu(new_example);
  279. if (entry_submenu == NULL) {
  280. SDL_Log("Couldn't create new entry submenu: %s", SDL_GetError());
  281. SDL_RemoveTrayEntry(new_ctrl);
  282. SDL_RemoveTrayEntry(new_example);
  283. return;
  284. }
  285. /* ---------- */
  286. new_ctrl_remove = SDL_InsertTrayEntryAt(submenu, -1, "Remove", SDL_TRAYENTRY_BUTTON);
  287. if (new_ctrl_remove == NULL) {
  288. SDL_Log("Couldn't insert new_ctrl_remove: %s", SDL_GetError());
  289. SDL_RemoveTrayEntry(new_ctrl);
  290. SDL_RemoveTrayEntry(new_example);
  291. return;
  292. }
  293. SDL_SetTrayEntryCallback(new_ctrl_remove, remove_entry, new_example);
  294. /* ---------- */
  295. new_ctrl_enabled = SDL_InsertTrayEntryAt(submenu, -1, "Enable", SDL_TRAYENTRY_BUTTON);
  296. if (new_ctrl_enabled == NULL) {
  297. SDL_Log("Couldn't insert new_ctrl_enabled: %s", SDL_GetError());
  298. SDL_RemoveTrayEntry(new_ctrl);
  299. SDL_RemoveTrayEntry(new_example);
  300. return;
  301. }
  302. SDL_SetTrayEntryCallback(new_ctrl_enabled, set_entry_enabled, new_example);
  303. /* ---------- */
  304. new_ctrl_disabled = SDL_InsertTrayEntryAt(submenu, -1, "Disable", SDL_TRAYENTRY_BUTTON);
  305. if (new_ctrl_disabled == NULL) {
  306. SDL_Log("Couldn't insert new_ctrl_disabled: %s", SDL_GetError());
  307. SDL_RemoveTrayEntry(new_ctrl);
  308. SDL_RemoveTrayEntry(new_example);
  309. return;
  310. }
  311. SDL_SetTrayEntryCallback(new_ctrl_disabled, set_entry_disabled, new_example);
  312. /* ---------- */
  313. SDL_InsertTrayEntryAt(submenu, -1, NULL, 0);
  314. /* ---------- */
  315. SDL_TrayEntry *entry_newbtn = SDL_InsertTrayEntryAt(submenu, -1, "Create button", SDL_TRAYENTRY_BUTTON);
  316. if (entry_newbtn == NULL) {
  317. SDL_Log("Couldn't insert entry_newbtn: %s", SDL_GetError());
  318. SDL_RemoveTrayEntry(new_ctrl);
  319. SDL_RemoveTrayEntry(new_example);
  320. return;
  321. }
  322. SDL_SetTrayEntryCallback(entry_newbtn, append_button_to, entry_submenu);
  323. /* ---------- */
  324. SDL_TrayEntry *entry_newchk = SDL_InsertTrayEntryAt(submenu, -1, "Create checkbox", SDL_TRAYENTRY_BUTTON);
  325. if (entry_newchk == NULL) {
  326. SDL_Log("Couldn't insert entry_newchk: %s", SDL_GetError());
  327. SDL_RemoveTrayEntry(new_ctrl);
  328. SDL_RemoveTrayEntry(new_example);
  329. return;
  330. }
  331. SDL_SetTrayEntryCallback(entry_newchk, append_checkbox_to, entry_submenu);
  332. /* ---------- */
  333. SDL_TrayEntry *entry_newsub = SDL_InsertTrayEntryAt(submenu, -1, "Create submenu", SDL_TRAYENTRY_BUTTON);
  334. if (entry_newsub == NULL) {
  335. SDL_Log("Couldn't insert entry_newsub: %s", SDL_GetError());
  336. SDL_RemoveTrayEntry(new_ctrl);
  337. SDL_RemoveTrayEntry(new_example);
  338. return;
  339. }
  340. SDL_SetTrayEntryCallback(entry_newsub, append_submenu_to, entry_submenu);
  341. /* ---------- */
  342. SDL_TrayEntry *entry_newsep = SDL_InsertTrayEntryAt(submenu, -1, "Create separator", SDL_TRAYENTRY_BUTTON);
  343. if (entry_newsep == NULL) {
  344. SDL_Log("Couldn't insert entry_newsep: %s", SDL_GetError());
  345. SDL_RemoveTrayEntry(new_ctrl);
  346. SDL_RemoveTrayEntry(new_example);
  347. return;
  348. }
  349. SDL_SetTrayEntryCallback(entry_newsep, append_separator_to, entry_submenu);
  350. /* ---------- */
  351. SDL_InsertTrayEntryAt(submenu, -1, NULL, 0);
  352. }
  353. int main(int argc, char **argv)
  354. {
  355. SDL_Tray **trays = NULL;
  356. SDLTest_CommonState *state;
  357. int i;
  358. /* Initialize test framework */
  359. state = SDLTest_CommonCreateState(argv, 0);
  360. if (state == NULL) {
  361. return 1;
  362. }
  363. /* Parse commandline */
  364. for (i = 1; i < argc;) {
  365. int consumed;
  366. consumed = SDLTest_CommonArg(state, i);
  367. if (consumed <= 0) {
  368. static const char *options[] = { NULL };
  369. SDLTest_CommonLogUsage(state, argv[0], options);
  370. return 1;
  371. }
  372. i += consumed;
  373. }
  374. if (!SDL_Init(SDL_INIT_VIDEO)) {
  375. SDL_Log("SDL_Init failed (%s)", SDL_GetError());
  376. return 1;
  377. }
  378. SDL_Window *w = SDL_CreateWindow("testtray", 640, 480, 0);
  379. if (!w) {
  380. SDL_Log("Couldn't create window: %s", SDL_GetError());
  381. goto quit;
  382. }
  383. char *icon1filename = GetResourceFilename(NULL, "sdl-test_round.bmp");
  384. SDL_Surface *icon = SDL_LoadBMP(icon1filename);
  385. SDL_free(icon1filename);
  386. if (!icon) {
  387. SDL_Log("Couldn't load icon 1, proceeding without: %s", SDL_GetError());
  388. }
  389. char *icon2filename = GetResourceFilename(NULL, "speaker.bmp");
  390. SDL_Surface *icon2 = SDL_LoadBMP(icon2filename);
  391. SDL_free(icon2filename);
  392. if (!icon2) {
  393. SDL_Log("Couldn't load icon 2, proceeding without: %s", SDL_GetError());
  394. }
  395. SDL_Tray *tray = SDL_CreateTray(icon, "SDL Tray control menu");
  396. if (!tray) {
  397. SDL_Log("Couldn't create control tray: %s", SDL_GetError());
  398. goto clean_window;
  399. }
  400. SDL_Tray *tray2 = SDL_CreateTray(icon2, "SDL Tray example");
  401. if (!tray2) {
  402. SDL_Log("Couldn't create example tray: %s", SDL_GetError());
  403. goto clean_tray1;
  404. }
  405. SDL_DestroySurface(icon);
  406. SDL_DestroySurface(icon2);
  407. #define CHECK(name) \
  408. if (!name) { \
  409. SDL_Log("Couldn't create " #name ": %s", SDL_GetError()); \
  410. goto clean_all; \
  411. }
  412. SDL_TrayMenu *menu = SDL_CreateTrayMenu(tray);
  413. CHECK(menu);
  414. SDL_TrayMenu *menu2 = SDL_CreateTrayMenu(tray2);
  415. CHECK(menu2);
  416. SDL_TrayEntry *entry_quit = SDL_InsertTrayEntryAt(menu, -1, "Quit", SDL_TRAYENTRY_BUTTON);
  417. CHECK(entry_quit);
  418. SDL_TrayEntry *entry_close = SDL_InsertTrayEntryAt(menu, -1, "Close", SDL_TRAYENTRY_BUTTON);
  419. CHECK(entry_close);
  420. /* TODO: Track memory! */
  421. trays = SDL_malloc(sizeof(SDL_Tray *) * 2);
  422. if (!trays) {
  423. goto clean_all;
  424. }
  425. trays[0] = tray;
  426. trays[1] = tray2;
  427. SDL_SetTrayEntryCallback(entry_quit, tray_quit, NULL);
  428. SDL_SetTrayEntryCallback(entry_close, tray_close, trays);
  429. SDL_InsertTrayEntryAt(menu, -1, NULL, 0);
  430. SDL_TrayEntry *entry_icon = SDL_InsertTrayEntryAt(menu, -1, "Change icon", SDL_TRAYENTRY_BUTTON);
  431. CHECK(entry_icon);
  432. SDL_SetTrayEntryCallback(entry_icon, change_icon, tray2);
  433. SDL_InsertTrayEntryAt(menu, -1, NULL, 0);
  434. SDL_TrayEntry *entry_newbtn = SDL_InsertTrayEntryAt(menu, -1, "Create button", SDL_TRAYENTRY_BUTTON);
  435. CHECK(entry_newbtn);
  436. SDL_SetTrayEntryCallback(entry_newbtn, append_button_to, menu2);
  437. SDL_TrayEntry *entry_newchk = SDL_InsertTrayEntryAt(menu, -1, "Create checkbox", SDL_TRAYENTRY_BUTTON);
  438. CHECK(entry_newchk);
  439. SDL_SetTrayEntryCallback(entry_newchk, append_checkbox_to, menu2);
  440. SDL_TrayEntry *entry_newsub = SDL_InsertTrayEntryAt(menu, -1, "Create submenu", SDL_TRAYENTRY_BUTTON);
  441. CHECK(entry_newsub);
  442. SDL_SetTrayEntryCallback(entry_newsub, append_submenu_to, menu2);
  443. SDL_TrayEntry *entry_newsep = SDL_InsertTrayEntryAt(menu, -1, "Create separator", SDL_TRAYENTRY_BUTTON);
  444. CHECK(entry_newsep);
  445. SDL_SetTrayEntryCallback(entry_newsep, append_separator_to, menu2);
  446. SDL_InsertTrayEntryAt(menu, -1, NULL, 0);
  447. SDL_Event e;
  448. while (SDL_WaitEvent(&e)) {
  449. if (e.type == SDL_EVENT_QUIT) {
  450. break;
  451. } else if (e.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) {
  452. SDL_DestroyWindow(w);
  453. w = NULL;
  454. }
  455. }
  456. clean_all:
  457. if (!trays_destroyed) {
  458. SDL_DestroyTray(tray2);
  459. }
  460. clean_tray1:
  461. if (!trays_destroyed) {
  462. SDL_DestroyTray(tray);
  463. }
  464. SDL_free(trays);
  465. clean_window:
  466. if (w) {
  467. SDL_DestroyWindow(w);
  468. }
  469. quit:
  470. SDL_Quit();
  471. SDLTest_CommonDestroyState(state);
  472. return 0;
  473. }