testtray.c 21 KB

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