testtray.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. #include "testutils.h"
  2. #include <SDL3/SDL.h>
  3. #include <SDL3/SDL_main.h>
  4. #include <SDL3/SDL_test.h>
  5. /*
  6. * testtray - SDL system tray API test application
  7. *
  8. * This program creates two system tray icons to demonstrate and test the
  9. * SDL tray API:
  10. *
  11. * 1. Control tray (sdl-test_round.png) - Provides a menu to:
  12. * - Quit: Exit the application
  13. * - Destroy trays: Remove both tray icons and show the window
  14. * - Hide/Show window: Toggle the window visibility
  15. * - Change icon: Change the example tray's icon via file dialog
  16. * - Create button/checkbox/submenu/separator: Add menu items to the
  17. * example tray, demonstrating dynamic menu construction
  18. *
  19. * 2. Example tray (speaker.png) - A target tray that can be manipulated
  20. * through the control tray's menu. Menu items created here can be
  21. * enabled, disabled, checked, unchecked, or removed via submenus that
  22. * appear in the control tray. This tray is created with
  23. * SDL_CreateTrayWithProperties to demonstrate click callbacks:
  24. * - Left click: Logs a message and shows the menu (returns true)
  25. * - Right click: Logs a message and suppresses the menu (returns false)
  26. *
  27. * Window behavior:
  28. * - Closing the window (X button) hides it to the tray rather than exiting
  29. * - The "Hide/Show window" menu item toggles visibility and updates its label
  30. * - If trays are destroyed while the window is hidden, it is shown first
  31. * - If trays are destroyed, closing the window exits the application
  32. */
  33. static void SDLCALL tray_quit(void *ptr, SDL_TrayEntry *entry)
  34. {
  35. SDL_Event e;
  36. e.type = SDL_EVENT_QUIT;
  37. SDL_PushEvent(&e);
  38. }
  39. static bool SDLCALL tray2_leftclick(void *userdata, SDL_Tray *tray)
  40. {
  41. SDL_Log("Left click on example tray - menu shown");
  42. return true;
  43. }
  44. static bool SDLCALL tray2_rightclick(void *userdata, SDL_Tray *tray)
  45. {
  46. SDL_Log("Right click on example tray - menu suppressed");
  47. return false;
  48. }
  49. static bool trays_destroyed = false;
  50. static SDL_Window *window = NULL;
  51. static SDL_TrayEntry *entry_toggle = NULL;
  52. static void SDLCALL tray_close(void *ptr, SDL_TrayEntry *entry)
  53. {
  54. SDL_Tray **trays = (SDL_Tray **) ptr;
  55. trays_destroyed = true;
  56. if (window) {
  57. SDL_ShowWindow(window);
  58. }
  59. SDL_DestroyTray(trays[0]);
  60. SDL_DestroyTray(trays[1]);
  61. }
  62. static void SDLCALL toggle_window(void *ptr, SDL_TrayEntry *entry)
  63. {
  64. if (SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN) {
  65. SDL_ShowWindow(window);
  66. SDL_SetTrayEntryLabel(entry, "Hide window");
  67. } else {
  68. SDL_HideWindow(window);
  69. SDL_SetTrayEntryLabel(entry, "Show window");
  70. }
  71. }
  72. static void SDLCALL apply_icon(void *ptr, const char * const *filelist, int filter)
  73. {
  74. if (!*filelist) {
  75. return;
  76. }
  77. SDL_Surface *icon = SDL_LoadPNG(*filelist);
  78. if (!icon) {
  79. SDL_Log("Couldn't load icon '%s': %s", *filelist, SDL_GetError());
  80. return;
  81. }
  82. SDL_Tray *tray = (SDL_Tray *) ptr;
  83. SDL_SetTrayIcon(tray, icon);
  84. SDL_DestroySurface(icon);
  85. }
  86. static void SDLCALL change_icon(void *ptr, SDL_TrayEntry *entry)
  87. {
  88. SDL_DialogFileFilter filters[] = {
  89. { "PNG image files", "png" },
  90. { "All files", "*" },
  91. };
  92. SDL_ShowOpenFileDialog(apply_icon, ptr, NULL, filters, 2, NULL, 0);
  93. }
  94. static void SDLCALL print_entry(void *ptr, SDL_TrayEntry *entry)
  95. {
  96. SDL_Log("Clicked on button '%s'", SDL_GetTrayEntryLabel(entry));
  97. }
  98. static void SDLCALL set_entry_enabled(void *ptr, SDL_TrayEntry *entry)
  99. {
  100. SDL_TrayEntry *target = (SDL_TrayEntry *) ptr;
  101. SDL_SetTrayEntryEnabled(target, true);
  102. }
  103. static void SDLCALL set_entry_disabled(void *ptr, SDL_TrayEntry *entry)
  104. {
  105. SDL_TrayEntry *target = (SDL_TrayEntry *) ptr;
  106. SDL_SetTrayEntryEnabled(target, false);
  107. }
  108. static void SDLCALL set_entry_checked(void *ptr, SDL_TrayEntry *entry)
  109. {
  110. SDL_TrayEntry *target = (SDL_TrayEntry *) ptr;
  111. SDL_SetTrayEntryChecked(target, true);
  112. }
  113. static void SDLCALL set_entry_unchecked(void *ptr, SDL_TrayEntry *entry)
  114. {
  115. SDL_TrayEntry *target = (SDL_TrayEntry *) ptr;
  116. SDL_SetTrayEntryChecked(target, false);
  117. }
  118. static void SDLCALL remove_entry(void *ptr, SDL_TrayEntry *entry)
  119. {
  120. SDL_TrayEntry *target = (SDL_TrayEntry *) ptr;
  121. SDL_RemoveTrayEntry(target);
  122. SDL_TrayMenu *ctrl_submenu = SDL_GetTrayEntryParent(entry);
  123. SDL_TrayEntry *ctrl_entry = SDL_GetTrayMenuParentEntry(ctrl_submenu);
  124. if (!ctrl_entry) {
  125. SDL_Log("Attempt to remove a menu that isn't a submenu. This shouldn't happen.");
  126. return;
  127. }
  128. SDL_RemoveTrayEntry(ctrl_entry);
  129. }
  130. static void SDLCALL append_button_to(void *ptr, SDL_TrayEntry *entry)
  131. {
  132. SDL_TrayMenu *menu = (SDL_TrayMenu *) ptr;
  133. SDL_TrayMenu *submenu;
  134. SDL_TrayEntry *new_ctrl;
  135. SDL_TrayEntry *new_ctrl_remove;
  136. SDL_TrayEntry *new_ctrl_enabled;
  137. SDL_TrayEntry *new_ctrl_disabled;
  138. SDL_TrayEntry *new_example;
  139. new_ctrl = SDL_InsertTrayEntryAt(SDL_GetTrayEntryParent(entry), -1, "New button", SDL_TRAYENTRY_SUBMENU);
  140. if (!new_ctrl) {
  141. SDL_Log("Couldn't insert entry in control tray: %s", SDL_GetError());
  142. return;
  143. }
  144. /* ---------- */
  145. submenu = SDL_CreateTraySubmenu(new_ctrl);
  146. if (!new_ctrl) {
  147. SDL_Log("Couldn't create control tray entry submenu: %s", SDL_GetError());
  148. SDL_RemoveTrayEntry(new_ctrl);
  149. return;
  150. }
  151. /* ---------- */
  152. new_example = SDL_InsertTrayEntryAt(menu, -1, "New button", SDL_TRAYENTRY_BUTTON);
  153. if (new_example == NULL) {
  154. SDL_Log("Couldn't insert entry in example tray: %s", SDL_GetError());
  155. SDL_RemoveTrayEntry(new_ctrl);
  156. return;
  157. }
  158. SDL_SetTrayEntryCallback(new_example, print_entry, NULL);
  159. /* ---------- */
  160. new_ctrl_remove = SDL_InsertTrayEntryAt(submenu, -1, "Remove", SDL_TRAYENTRY_BUTTON);
  161. if (new_ctrl_remove == NULL) {
  162. SDL_Log("Couldn't insert new_ctrl_remove: %s", SDL_GetError());
  163. SDL_RemoveTrayEntry(new_ctrl);
  164. SDL_RemoveTrayEntry(new_example);
  165. return;
  166. }
  167. SDL_SetTrayEntryCallback(new_ctrl_remove, remove_entry, new_example);
  168. /* ---------- */
  169. new_ctrl_enabled = SDL_InsertTrayEntryAt(submenu, -1, "Enable", SDL_TRAYENTRY_BUTTON);
  170. if (new_ctrl_enabled == NULL) {
  171. SDL_Log("Couldn't insert new_ctrl_enabled: %s", SDL_GetError());
  172. SDL_RemoveTrayEntry(new_ctrl);
  173. SDL_RemoveTrayEntry(new_example);
  174. return;
  175. }
  176. SDL_SetTrayEntryCallback(new_ctrl_enabled, set_entry_enabled, new_example);
  177. /* ---------- */
  178. new_ctrl_disabled = SDL_InsertTrayEntryAt(submenu, -1, "Disable", SDL_TRAYENTRY_BUTTON);
  179. if (new_ctrl_disabled == NULL) {
  180. SDL_Log("Couldn't insert new_ctrl_disabled: %s", SDL_GetError());
  181. SDL_RemoveTrayEntry(new_ctrl);
  182. SDL_RemoveTrayEntry(new_example);
  183. return;
  184. }
  185. SDL_SetTrayEntryCallback(new_ctrl_disabled, set_entry_disabled, new_example);
  186. }
  187. static void SDLCALL append_checkbox_to(void *ptr, SDL_TrayEntry *entry)
  188. {
  189. SDL_TrayMenu *menu = (SDL_TrayMenu *) ptr;
  190. SDL_TrayMenu *submenu;
  191. SDL_TrayEntry *new_ctrl;
  192. SDL_TrayEntry *new_ctrl_remove;
  193. SDL_TrayEntry *new_ctrl_enabled;
  194. SDL_TrayEntry *new_ctrl_disabled;
  195. SDL_TrayEntry *new_ctrl_checked;
  196. SDL_TrayEntry *new_ctrl_unchecked;
  197. SDL_TrayEntry *new_example;
  198. new_ctrl = SDL_InsertTrayEntryAt(SDL_GetTrayEntryParent(entry), -1, "New checkbox", SDL_TRAYENTRY_SUBMENU);
  199. if (!new_ctrl) {
  200. SDL_Log("Couldn't insert entry in control tray: %s", SDL_GetError());
  201. return;
  202. }
  203. /* ---------- */
  204. submenu = SDL_CreateTraySubmenu(new_ctrl);
  205. if (!new_ctrl) {
  206. SDL_Log("Couldn't create control tray entry submenu: %s", SDL_GetError());
  207. SDL_RemoveTrayEntry(new_ctrl);
  208. return;
  209. }
  210. /* ---------- */
  211. new_example = SDL_InsertTrayEntryAt(menu, -1, "New checkbox", SDL_TRAYENTRY_CHECKBOX);
  212. if (new_example == NULL) {
  213. SDL_Log("Couldn't insert entry in example tray: %s", SDL_GetError());
  214. SDL_RemoveTrayEntry(new_ctrl);
  215. return;
  216. }
  217. SDL_SetTrayEntryCallback(new_example, print_entry, NULL);
  218. /* ---------- */
  219. new_ctrl_remove = SDL_InsertTrayEntryAt(submenu, -1, "Remove", SDL_TRAYENTRY_BUTTON);
  220. if (new_ctrl_remove == NULL) {
  221. SDL_Log("Couldn't insert new_ctrl_remove: %s", SDL_GetError());
  222. SDL_RemoveTrayEntry(new_ctrl);
  223. SDL_RemoveTrayEntry(new_example);
  224. return;
  225. }
  226. SDL_SetTrayEntryCallback(new_ctrl_remove, remove_entry, new_example);
  227. /* ---------- */
  228. new_ctrl_enabled = SDL_InsertTrayEntryAt(submenu, -1, "Enable", SDL_TRAYENTRY_BUTTON);
  229. if (new_ctrl_enabled == NULL) {
  230. SDL_Log("Couldn't insert new_ctrl_enabled: %s", SDL_GetError());
  231. SDL_RemoveTrayEntry(new_ctrl);
  232. SDL_RemoveTrayEntry(new_example);
  233. return;
  234. }
  235. SDL_SetTrayEntryCallback(new_ctrl_enabled, set_entry_enabled, new_example);
  236. /* ---------- */
  237. new_ctrl_disabled = SDL_InsertTrayEntryAt(submenu, -1, "Disable", SDL_TRAYENTRY_BUTTON);
  238. if (new_ctrl_disabled == NULL) {
  239. SDL_Log("Couldn't insert new_ctrl_disabled: %s", SDL_GetError());
  240. SDL_RemoveTrayEntry(new_ctrl);
  241. SDL_RemoveTrayEntry(new_example);
  242. return;
  243. }
  244. SDL_SetTrayEntryCallback(new_ctrl_disabled, set_entry_disabled, new_example);
  245. /* ---------- */
  246. new_ctrl_checked = SDL_InsertTrayEntryAt(submenu, -1, "Check", SDL_TRAYENTRY_BUTTON);
  247. if (new_ctrl_checked == NULL) {
  248. SDL_Log("Couldn't insert new_ctrl_checked: %s", SDL_GetError());
  249. SDL_RemoveTrayEntry(new_ctrl);
  250. SDL_RemoveTrayEntry(new_example);
  251. return;
  252. }
  253. SDL_SetTrayEntryCallback(new_ctrl_checked, set_entry_checked, new_example);
  254. /* ---------- */
  255. new_ctrl_unchecked = SDL_InsertTrayEntryAt(submenu, -1, "Uncheck", SDL_TRAYENTRY_BUTTON);
  256. if (new_ctrl_unchecked == NULL) {
  257. SDL_Log("Couldn't insert new_ctrl_unchecked: %s", SDL_GetError());
  258. SDL_RemoveTrayEntry(new_ctrl);
  259. SDL_RemoveTrayEntry(new_example);
  260. return;
  261. }
  262. SDL_SetTrayEntryCallback(new_ctrl_unchecked, set_entry_unchecked, new_example);
  263. }
  264. static void SDLCALL append_separator_to(void *ptr, SDL_TrayEntry *entry)
  265. {
  266. SDL_TrayMenu *menu = (SDL_TrayMenu *) ptr;
  267. SDL_TrayMenu *submenu;
  268. SDL_TrayEntry *new_ctrl;
  269. SDL_TrayEntry *new_ctrl_remove;
  270. SDL_TrayEntry *new_example;
  271. new_ctrl = SDL_InsertTrayEntryAt(SDL_GetTrayEntryParent(entry), -1, "[Separator]", SDL_TRAYENTRY_SUBMENU);
  272. if (!new_ctrl) {
  273. SDL_Log("Couldn't insert entry in control tray: %s", SDL_GetError());
  274. return;
  275. }
  276. /* ---------- */
  277. submenu = SDL_CreateTraySubmenu(new_ctrl);
  278. if (!new_ctrl) {
  279. SDL_Log("Couldn't create control tray entry submenu: %s", SDL_GetError());
  280. SDL_RemoveTrayEntry(new_ctrl);
  281. return;
  282. }
  283. /* ---------- */
  284. new_example = SDL_InsertTrayEntryAt(menu, -1, NULL, SDL_TRAYENTRY_BUTTON);
  285. if (new_example == NULL) {
  286. SDL_Log("Couldn't insert separator in example tray: %s", SDL_GetError());
  287. SDL_RemoveTrayEntry(new_ctrl);
  288. return;
  289. }
  290. /* ---------- */
  291. new_ctrl_remove = SDL_InsertTrayEntryAt(submenu, -1, "Remove", SDL_TRAYENTRY_BUTTON);
  292. if (new_ctrl_remove == NULL) {
  293. SDL_Log("Couldn't insert new_ctrl_remove: %s", SDL_GetError());
  294. SDL_RemoveTrayEntry(new_ctrl);
  295. SDL_RemoveTrayEntry(new_example);
  296. return;
  297. }
  298. SDL_SetTrayEntryCallback(new_ctrl_remove, remove_entry, new_example);
  299. }
  300. static void SDLCALL append_submenu_to(void *ptr, SDL_TrayEntry *entry)
  301. {
  302. SDL_TrayMenu *menu = (SDL_TrayMenu *) ptr;
  303. SDL_TrayMenu *submenu;
  304. SDL_TrayMenu *entry_submenu;
  305. SDL_TrayEntry *new_ctrl;
  306. SDL_TrayEntry *new_ctrl_remove;
  307. SDL_TrayEntry *new_ctrl_enabled;
  308. SDL_TrayEntry *new_ctrl_disabled;
  309. SDL_TrayEntry *new_example;
  310. new_ctrl = SDL_InsertTrayEntryAt(SDL_GetTrayEntryParent(entry), -1, "New submenu", SDL_TRAYENTRY_SUBMENU);
  311. if (!new_ctrl) {
  312. SDL_Log("Couldn't insert entry in control tray: %s", SDL_GetError());
  313. return;
  314. }
  315. /* ---------- */
  316. submenu = SDL_CreateTraySubmenu(new_ctrl);
  317. if (!new_ctrl) {
  318. SDL_Log("Couldn't create control tray entry submenu: %s", SDL_GetError());
  319. SDL_RemoveTrayEntry(new_ctrl);
  320. return;
  321. }
  322. /* ---------- */
  323. new_example = SDL_InsertTrayEntryAt(menu, -1, "New submenu", SDL_TRAYENTRY_SUBMENU);
  324. if (new_example == NULL) {
  325. SDL_Log("Couldn't insert entry in example tray: %s", SDL_GetError());
  326. SDL_RemoveTrayEntry(new_ctrl);
  327. return;
  328. }
  329. SDL_SetTrayEntryCallback(new_example, print_entry, NULL);
  330. /* ---------- */
  331. entry_submenu = SDL_CreateTraySubmenu(new_example);
  332. if (entry_submenu == NULL) {
  333. SDL_Log("Couldn't create new entry submenu: %s", SDL_GetError());
  334. SDL_RemoveTrayEntry(new_ctrl);
  335. SDL_RemoveTrayEntry(new_example);
  336. return;
  337. }
  338. /* ---------- */
  339. new_ctrl_remove = SDL_InsertTrayEntryAt(submenu, -1, "Remove", SDL_TRAYENTRY_BUTTON);
  340. if (new_ctrl_remove == NULL) {
  341. SDL_Log("Couldn't insert new_ctrl_remove: %s", SDL_GetError());
  342. SDL_RemoveTrayEntry(new_ctrl);
  343. SDL_RemoveTrayEntry(new_example);
  344. return;
  345. }
  346. SDL_SetTrayEntryCallback(new_ctrl_remove, remove_entry, new_example);
  347. /* ---------- */
  348. new_ctrl_enabled = SDL_InsertTrayEntryAt(submenu, -1, "Enable", SDL_TRAYENTRY_BUTTON);
  349. if (new_ctrl_enabled == NULL) {
  350. SDL_Log("Couldn't insert new_ctrl_enabled: %s", SDL_GetError());
  351. SDL_RemoveTrayEntry(new_ctrl);
  352. SDL_RemoveTrayEntry(new_example);
  353. return;
  354. }
  355. SDL_SetTrayEntryCallback(new_ctrl_enabled, set_entry_enabled, new_example);
  356. /* ---------- */
  357. new_ctrl_disabled = SDL_InsertTrayEntryAt(submenu, -1, "Disable", SDL_TRAYENTRY_BUTTON);
  358. if (new_ctrl_disabled == NULL) {
  359. SDL_Log("Couldn't insert new_ctrl_disabled: %s", SDL_GetError());
  360. SDL_RemoveTrayEntry(new_ctrl);
  361. SDL_RemoveTrayEntry(new_example);
  362. return;
  363. }
  364. SDL_SetTrayEntryCallback(new_ctrl_disabled, set_entry_disabled, new_example);
  365. /* ---------- */
  366. SDL_InsertTrayEntryAt(submenu, -1, NULL, 0);
  367. /* ---------- */
  368. SDL_TrayEntry *entry_newbtn = SDL_InsertTrayEntryAt(submenu, -1, "Create button", SDL_TRAYENTRY_BUTTON);
  369. if (entry_newbtn == NULL) {
  370. SDL_Log("Couldn't insert entry_newbtn: %s", SDL_GetError());
  371. SDL_RemoveTrayEntry(new_ctrl);
  372. SDL_RemoveTrayEntry(new_example);
  373. return;
  374. }
  375. SDL_SetTrayEntryCallback(entry_newbtn, append_button_to, entry_submenu);
  376. /* ---------- */
  377. SDL_TrayEntry *entry_newchk = SDL_InsertTrayEntryAt(submenu, -1, "Create checkbox", SDL_TRAYENTRY_BUTTON);
  378. if (entry_newchk == NULL) {
  379. SDL_Log("Couldn't insert entry_newchk: %s", SDL_GetError());
  380. SDL_RemoveTrayEntry(new_ctrl);
  381. SDL_RemoveTrayEntry(new_example);
  382. return;
  383. }
  384. SDL_SetTrayEntryCallback(entry_newchk, append_checkbox_to, entry_submenu);
  385. /* ---------- */
  386. SDL_TrayEntry *entry_newsub = SDL_InsertTrayEntryAt(submenu, -1, "Create submenu", SDL_TRAYENTRY_BUTTON);
  387. if (entry_newsub == NULL) {
  388. SDL_Log("Couldn't insert entry_newsub: %s", SDL_GetError());
  389. SDL_RemoveTrayEntry(new_ctrl);
  390. SDL_RemoveTrayEntry(new_example);
  391. return;
  392. }
  393. SDL_SetTrayEntryCallback(entry_newsub, append_submenu_to, entry_submenu);
  394. /* ---------- */
  395. SDL_TrayEntry *entry_newsep = SDL_InsertTrayEntryAt(submenu, -1, "Create separator", SDL_TRAYENTRY_BUTTON);
  396. if (entry_newsep == NULL) {
  397. SDL_Log("Couldn't insert entry_newsep: %s", SDL_GetError());
  398. SDL_RemoveTrayEntry(new_ctrl);
  399. SDL_RemoveTrayEntry(new_example);
  400. return;
  401. }
  402. SDL_SetTrayEntryCallback(entry_newsep, append_separator_to, entry_submenu);
  403. /* ---------- */
  404. SDL_InsertTrayEntryAt(submenu, -1, NULL, 0);
  405. }
  406. int main(int argc, char **argv)
  407. {
  408. SDL_Tray **trays = NULL;
  409. SDLTest_CommonState *state;
  410. int i;
  411. /* Initialize test framework */
  412. state = SDLTest_CommonCreateState(argv, 0);
  413. if (state == NULL) {
  414. return 1;
  415. }
  416. /* Parse commandline */
  417. for (i = 1; i < argc;) {
  418. int consumed;
  419. consumed = SDLTest_CommonArg(state, i);
  420. if (consumed <= 0) {
  421. static const char *options[] = { NULL };
  422. SDLTest_CommonLogUsage(state, argv[0], options);
  423. return 1;
  424. }
  425. i += consumed;
  426. }
  427. if (!SDL_Init(SDL_INIT_VIDEO)) {
  428. SDL_Log("SDL_Init failed (%s)", SDL_GetError());
  429. return 1;
  430. }
  431. window = SDL_CreateWindow("testtray", 640, 480, 0);
  432. if (!window) {
  433. SDL_Log("Couldn't create window: %s", SDL_GetError());
  434. goto quit;
  435. }
  436. char *icon1filename = GetResourceFilename(NULL, "sdl-test_round.png");
  437. SDL_Surface *icon = SDL_LoadPNG(icon1filename);
  438. SDL_free(icon1filename);
  439. if (!icon) {
  440. SDL_Log("Couldn't load icon 1, proceeding without: %s", SDL_GetError());
  441. }
  442. char *icon2filename = GetResourceFilename(NULL, "speaker.png");
  443. SDL_Surface *icon2 = SDL_LoadPNG(icon2filename);
  444. SDL_free(icon2filename);
  445. if (!icon2) {
  446. SDL_Log("Couldn't load icon 2, proceeding without: %s", SDL_GetError());
  447. }
  448. SDL_Tray *tray = SDL_CreateTray(icon, "SDL Tray control menu");
  449. if (!tray) {
  450. SDL_Log("Couldn't create control tray: %s", SDL_GetError());
  451. goto clean_window;
  452. }
  453. SDL_PropertiesID tray2_props = SDL_CreateProperties();
  454. SDL_SetPointerProperty(tray2_props, SDL_PROP_TRAY_CREATE_ICON_POINTER, icon2);
  455. SDL_SetStringProperty(tray2_props, SDL_PROP_TRAY_CREATE_TOOLTIP_STRING, "SDL Tray example");
  456. SDL_SetPointerProperty(tray2_props, SDL_PROP_TRAY_CREATE_LEFTCLICK_CALLBACK_POINTER, tray2_leftclick);
  457. SDL_SetPointerProperty(tray2_props, SDL_PROP_TRAY_CREATE_RIGHTCLICK_CALLBACK_POINTER, tray2_rightclick);
  458. SDL_Tray *tray2 = SDL_CreateTrayWithProperties(tray2_props);
  459. SDL_DestroyProperties(tray2_props);
  460. if (!tray2) {
  461. SDL_Log("Couldn't create example tray: %s", SDL_GetError());
  462. goto clean_tray1;
  463. }
  464. SDL_DestroySurface(icon);
  465. SDL_DestroySurface(icon2);
  466. #define CHECK(name) \
  467. if (!name) { \
  468. SDL_Log("Couldn't create " #name ": %s", SDL_GetError()); \
  469. goto clean_all; \
  470. }
  471. SDL_TrayMenu *menu = SDL_CreateTrayMenu(tray);
  472. CHECK(menu);
  473. SDL_TrayMenu *menu2 = SDL_CreateTrayMenu(tray2);
  474. CHECK(menu2);
  475. SDL_TrayEntry *entry_quit = SDL_InsertTrayEntryAt(menu, -1, "Quit", SDL_TRAYENTRY_BUTTON);
  476. CHECK(entry_quit);
  477. SDL_TrayEntry *entry_close = SDL_InsertTrayEntryAt(menu, -1, "Destroy trays", SDL_TRAYENTRY_BUTTON);
  478. CHECK(entry_close);
  479. /* TODO: Track memory! */
  480. trays = SDL_malloc(sizeof(SDL_Tray *) * 2);
  481. if (!trays) {
  482. goto clean_all;
  483. }
  484. trays[0] = tray;
  485. trays[1] = tray2;
  486. SDL_SetTrayEntryCallback(entry_quit, tray_quit, NULL);
  487. SDL_SetTrayEntryCallback(entry_close, tray_close, trays);
  488. SDL_InsertTrayEntryAt(menu, -1, NULL, 0);
  489. entry_toggle = SDL_InsertTrayEntryAt(menu, -1, "Hide window", SDL_TRAYENTRY_BUTTON);
  490. CHECK(entry_toggle);
  491. SDL_SetTrayEntryCallback(entry_toggle, toggle_window, NULL);
  492. SDL_InsertTrayEntryAt(menu, -1, NULL, 0);
  493. SDL_TrayEntry *entry_icon = SDL_InsertTrayEntryAt(menu, -1, "Change icon", SDL_TRAYENTRY_BUTTON);
  494. CHECK(entry_icon);
  495. SDL_SetTrayEntryCallback(entry_icon, change_icon, tray2);
  496. SDL_InsertTrayEntryAt(menu, -1, NULL, 0);
  497. SDL_TrayEntry *entry_newbtn = SDL_InsertTrayEntryAt(menu, -1, "Create button", SDL_TRAYENTRY_BUTTON);
  498. CHECK(entry_newbtn);
  499. SDL_SetTrayEntryCallback(entry_newbtn, append_button_to, menu2);
  500. SDL_TrayEntry *entry_newchk = SDL_InsertTrayEntryAt(menu, -1, "Create checkbox", SDL_TRAYENTRY_BUTTON);
  501. CHECK(entry_newchk);
  502. SDL_SetTrayEntryCallback(entry_newchk, append_checkbox_to, menu2);
  503. SDL_TrayEntry *entry_newsub = SDL_InsertTrayEntryAt(menu, -1, "Create submenu", SDL_TRAYENTRY_BUTTON);
  504. CHECK(entry_newsub);
  505. SDL_SetTrayEntryCallback(entry_newsub, append_submenu_to, menu2);
  506. SDL_TrayEntry *entry_newsep = SDL_InsertTrayEntryAt(menu, -1, "Create separator", SDL_TRAYENTRY_BUTTON);
  507. CHECK(entry_newsep);
  508. SDL_SetTrayEntryCallback(entry_newsep, append_separator_to, menu2);
  509. SDL_InsertTrayEntryAt(menu, -1, NULL, 0);
  510. SDL_Event e;
  511. while (SDL_WaitEvent(&e)) {
  512. if (e.type == SDL_EVENT_QUIT) {
  513. break;
  514. } else if (e.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) {
  515. if (trays_destroyed) {
  516. break;
  517. }
  518. toggle_window(NULL, entry_toggle);
  519. }
  520. }
  521. clean_all:
  522. if (!trays_destroyed) {
  523. SDL_DestroyTray(tray2);
  524. }
  525. clean_tray1:
  526. if (!trays_destroyed) {
  527. SDL_DestroyTray(tray);
  528. }
  529. SDL_free(trays);
  530. clean_window:
  531. if (window) {
  532. SDL_DestroyWindow(window);
  533. }
  534. quit:
  535. SDL_Quit();
  536. SDLTest_CommonDestroyState(state);
  537. return 0;
  538. }