1
0

SDL_tray.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * # CategoryTray
  20. *
  21. * SDL offers a way to add items to the "system tray" (more correctly called
  22. * the "notification area" on Windows). On platforms that offer this concept,
  23. * an SDL app can add a tray icon, submenus, checkboxes, and clickable
  24. * entries, and register a callback that is fired when the user clicks on
  25. * these pieces.
  26. */
  27. #ifndef SDL_tray_h_
  28. #define SDL_tray_h_
  29. #include <SDL3/SDL_stdinc.h>
  30. #include <SDL3/SDL_error.h>
  31. #include <SDL3/SDL_surface.h>
  32. #include <SDL3/SDL_video.h>
  33. #include <SDL3/SDL_begin_code.h>
  34. /* Set up for C function definitions, even when using C++ */
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /**
  39. * An opaque handle representing a toplevel system tray object.
  40. *
  41. * \since This struct is available since SDL 3.2.0.
  42. */
  43. typedef struct SDL_Tray SDL_Tray;
  44. /**
  45. * An opaque handle representing a menu/submenu on a system tray object.
  46. *
  47. * \since This struct is available since SDL 3.2.0.
  48. */
  49. typedef struct SDL_TrayMenu SDL_TrayMenu;
  50. /**
  51. * An opaque handle representing an entry on a system tray object.
  52. *
  53. * \since This struct is available since SDL 3.2.0.
  54. */
  55. typedef struct SDL_TrayEntry SDL_TrayEntry;
  56. /**
  57. * Flags that control the creation of system tray entries.
  58. *
  59. * Some of these flags are required; exactly one of them must be specified at
  60. * the time a tray entry is created. Other flags are optional; zero or more of
  61. * those can be OR'ed together with the required flag.
  62. *
  63. * \since This datatype is available since SDL 3.2.0.
  64. *
  65. * \sa SDL_InsertTrayEntryAt
  66. */
  67. typedef Uint32 SDL_TrayEntryFlags;
  68. #define SDL_TRAYENTRY_BUTTON 0x00000001u /**< Make the entry a simple button. Required. */
  69. #define SDL_TRAYENTRY_CHECKBOX 0x00000002u /**< Make the entry a checkbox. Required. */
  70. #define SDL_TRAYENTRY_SUBMENU 0x00000004u /**< Prepare the entry to have a submenu. Required */
  71. #define SDL_TRAYENTRY_DISABLED 0x80000000u /**< Make the entry disabled. Optional. */
  72. #define SDL_TRAYENTRY_CHECKED 0x40000000u /**< Make the entry checked. This is valid only for checkboxes. Optional. */
  73. /**
  74. * A callback that is invoked when a tray entry is selected.
  75. *
  76. * \param userdata an optional pointer to pass extra data to the callback when
  77. * it will be invoked.
  78. * \param entry the tray entry that was selected.
  79. *
  80. * \since This datatype is available since SDL 3.2.0.
  81. *
  82. * \sa SDL_SetTrayEntryCallback
  83. */
  84. typedef void (SDLCALL *SDL_TrayCallback)(void *userdata, SDL_TrayEntry *entry);
  85. /**
  86. * Create an icon to be placed in the operating system's tray, or equivalent.
  87. *
  88. * Many platforms advise not using a system tray unless persistence is a
  89. * necessary feature. Avoid needlessly creating a tray icon, as the user may
  90. * feel like it clutters their interface.
  91. *
  92. * Using tray icons require the video subsystem.
  93. *
  94. * \param icon a surface to be used as icon. May be NULL.
  95. * \param tooltip a tooltip to be displayed when the mouse hovers the icon in
  96. * UTF-8 encoding. Not supported on all platforms. May be NULL.
  97. * \returns The newly created system tray icon.
  98. *
  99. * \threadsafety This function should only be called on the main thread.
  100. *
  101. * \since This function is available since SDL 3.2.0.
  102. *
  103. * \sa SDL_CreateTrayMenu
  104. * \sa SDL_GetTrayMenu
  105. * \sa SDL_DestroyTray
  106. */
  107. extern SDL_DECLSPEC SDL_Tray * SDLCALL SDL_CreateTray(SDL_Surface *icon, const char *tooltip);
  108. /**
  109. * Updates the system tray icon's icon.
  110. *
  111. * \param tray the tray icon to be updated.
  112. * \param icon the new icon. May be NULL.
  113. *
  114. * \threadsafety This function should be called on the thread that created the
  115. * tray.
  116. *
  117. * \since This function is available since SDL 3.2.0.
  118. *
  119. * \sa SDL_CreateTray
  120. */
  121. extern SDL_DECLSPEC void SDLCALL SDL_SetTrayIcon(SDL_Tray *tray, SDL_Surface *icon);
  122. /**
  123. * Updates the system tray icon's tooltip.
  124. *
  125. * \param tray the tray icon to be updated.
  126. * \param tooltip the new tooltip in UTF-8 encoding. May be NULL.
  127. *
  128. * \threadsafety This function should be called on the thread that created the
  129. * tray.
  130. *
  131. * \since This function is available since SDL 3.2.0.
  132. *
  133. * \sa SDL_CreateTray
  134. */
  135. extern SDL_DECLSPEC void SDLCALL SDL_SetTrayTooltip(SDL_Tray *tray, const char *tooltip);
  136. /**
  137. * Create a menu for a system tray.
  138. *
  139. * This should be called at most once per tray icon.
  140. *
  141. * This function does the same thing as SDL_CreateTraySubmenu(), except that
  142. * it takes a SDL_Tray instead of a SDL_TrayEntry.
  143. *
  144. * A menu does not need to be destroyed; it will be destroyed with the tray.
  145. *
  146. * \param tray the tray to bind the menu to.
  147. * \returns the newly created menu.
  148. *
  149. * \threadsafety This function should be called on the thread that created the
  150. * tray.
  151. *
  152. * \since This function is available since SDL 3.2.0.
  153. *
  154. * \sa SDL_CreateTray
  155. * \sa SDL_GetTrayMenu
  156. * \sa SDL_GetTrayMenuParentTray
  157. */
  158. extern SDL_DECLSPEC SDL_TrayMenu * SDLCALL SDL_CreateTrayMenu(SDL_Tray *tray);
  159. /**
  160. * Create a submenu for a system tray entry.
  161. *
  162. * This should be called at most once per tray entry.
  163. *
  164. * This function does the same thing as SDL_CreateTrayMenu, except that it
  165. * takes a SDL_TrayEntry instead of a SDL_Tray.
  166. *
  167. * A menu does not need to be destroyed; it will be destroyed with the tray.
  168. *
  169. * \param entry the tray entry to bind the menu to.
  170. * \returns the newly created menu.
  171. *
  172. * \threadsafety This function should be called on the thread that created the
  173. * tray.
  174. *
  175. * \since This function is available since SDL 3.2.0.
  176. *
  177. * \sa SDL_InsertTrayEntryAt
  178. * \sa SDL_GetTraySubmenu
  179. * \sa SDL_GetTrayMenuParentEntry
  180. */
  181. extern SDL_DECLSPEC SDL_TrayMenu * SDLCALL SDL_CreateTraySubmenu(SDL_TrayEntry *entry);
  182. /**
  183. * Gets a previously created tray menu.
  184. *
  185. * You should have called SDL_CreateTrayMenu() on the tray object. This
  186. * function allows you to fetch it again later.
  187. *
  188. * This function does the same thing as SDL_GetTraySubmenu(), except that it
  189. * takes a SDL_Tray instead of a SDL_TrayEntry.
  190. *
  191. * A menu does not need to be destroyed; it will be destroyed with the tray.
  192. *
  193. * \param tray the tray entry to bind the menu to.
  194. * \returns the newly created menu.
  195. *
  196. * \threadsafety This function should be called on the thread that created the
  197. * tray.
  198. *
  199. * \since This function is available since SDL 3.2.0.
  200. *
  201. * \sa SDL_CreateTray
  202. * \sa SDL_CreateTrayMenu
  203. */
  204. extern SDL_DECLSPEC SDL_TrayMenu * SDLCALL SDL_GetTrayMenu(SDL_Tray *tray);
  205. /**
  206. * Gets a previously created tray entry submenu.
  207. *
  208. * You should have called SDL_CreateTraySubmenu() on the entry object. This
  209. * function allows you to fetch it again later.
  210. *
  211. * This function does the same thing as SDL_GetTrayMenu(), except that it
  212. * takes a SDL_TrayEntry instead of a SDL_Tray.
  213. *
  214. * A menu does not need to be destroyed; it will be destroyed with the tray.
  215. *
  216. * \param entry the tray entry to bind the menu to.
  217. * \returns the newly created menu.
  218. *
  219. * \threadsafety This function should be called on the thread that created the
  220. * tray.
  221. *
  222. * \since This function is available since SDL 3.2.0.
  223. *
  224. * \sa SDL_InsertTrayEntryAt
  225. * \sa SDL_CreateTraySubmenu
  226. */
  227. extern SDL_DECLSPEC SDL_TrayMenu * SDLCALL SDL_GetTraySubmenu(SDL_TrayEntry *entry);
  228. /**
  229. * Returns a list of entries in the menu, in order.
  230. *
  231. * \param menu The menu to get entries from.
  232. * \param count An optional pointer to obtain the number of entries in the
  233. * menu.
  234. * \returns a NULL-terminated list of entries within the given menu. The
  235. * pointer becomes invalid when any function that inserts or deletes
  236. * entries in the menu is called.
  237. *
  238. * \threadsafety This function should be called on the thread that created the
  239. * tray.
  240. *
  241. * \since This function is available since SDL 3.2.0.
  242. *
  243. * \sa SDL_RemoveTrayEntry
  244. * \sa SDL_InsertTrayEntryAt
  245. */
  246. extern SDL_DECLSPEC const SDL_TrayEntry ** SDLCALL SDL_GetTrayEntries(SDL_TrayMenu *menu, int *count);
  247. /**
  248. * Removes a tray entry.
  249. *
  250. * \param entry The entry to be deleted.
  251. *
  252. * \threadsafety This function should be called on the thread that created the
  253. * tray.
  254. *
  255. * \since This function is available since SDL 3.2.0.
  256. *
  257. * \sa SDL_GetTrayEntries
  258. * \sa SDL_InsertTrayEntryAt
  259. */
  260. extern SDL_DECLSPEC void SDLCALL SDL_RemoveTrayEntry(SDL_TrayEntry *entry);
  261. /**
  262. * Insert a tray entry at a given position.
  263. *
  264. * If label is NULL, the entry will be a separator. Many functions won't work
  265. * for an entry that is a separator.
  266. *
  267. * An entry does not need to be destroyed; it will be destroyed with the tray.
  268. *
  269. * \param menu the menu to append the entry to.
  270. * \param pos the desired position for the new entry. Entries at or following
  271. * this place will be moved. If pos is -1, the entry is appended.
  272. * \param label the text to be displayed on the entry, in UTF-8 encoding, or
  273. * NULL for a separator.
  274. * \param flags a combination of flags, some of which are mandatory.
  275. * \returns the newly created entry, or NULL if pos is out of bounds.
  276. *
  277. * \threadsafety This function should be called on the thread that created the
  278. * tray.
  279. *
  280. * \since This function is available since SDL 3.2.0.
  281. *
  282. * \sa SDL_TrayEntryFlags
  283. * \sa SDL_GetTrayEntries
  284. * \sa SDL_RemoveTrayEntry
  285. * \sa SDL_GetTrayEntryParent
  286. */
  287. extern SDL_DECLSPEC SDL_TrayEntry * SDLCALL SDL_InsertTrayEntryAt(SDL_TrayMenu *menu, int pos, const char *label, SDL_TrayEntryFlags flags);
  288. /**
  289. * Sets the label of an entry.
  290. *
  291. * An entry cannot change between a separator and an ordinary entry; that is,
  292. * it is not possible to set a non-NULL label on an entry that has a NULL
  293. * label (separators), or to set a NULL label to an entry that has a non-NULL
  294. * label. The function will silently fail if that happens.
  295. *
  296. * \param entry the entry to be updated.
  297. * \param label the new label for the entry in UTF-8 encoding.
  298. *
  299. * \threadsafety This function should be called on the thread that created the
  300. * tray.
  301. *
  302. * \since This function is available since SDL 3.2.0.
  303. *
  304. * \sa SDL_GetTrayEntries
  305. * \sa SDL_InsertTrayEntryAt
  306. * \sa SDL_GetTrayEntryLabel
  307. */
  308. extern SDL_DECLSPEC void SDLCALL SDL_SetTrayEntryLabel(SDL_TrayEntry *entry, const char *label);
  309. /**
  310. * Gets the label of an entry.
  311. *
  312. * If the returned value is NULL, the entry is a separator.
  313. *
  314. * \param entry the entry to be read.
  315. * \returns the label of the entry in UTF-8 encoding.
  316. *
  317. * \threadsafety This function should be called on the thread that created the
  318. * tray.
  319. *
  320. * \since This function is available since SDL 3.2.0.
  321. *
  322. * \sa SDL_GetTrayEntries
  323. * \sa SDL_InsertTrayEntryAt
  324. * \sa SDL_SetTrayEntryLabel
  325. */
  326. extern SDL_DECLSPEC const char * SDLCALL SDL_GetTrayEntryLabel(SDL_TrayEntry *entry);
  327. /**
  328. * Sets whether or not an entry is checked.
  329. *
  330. * The entry must have been created with the SDL_TRAYENTRY_CHECKBOX flag.
  331. *
  332. * \param entry the entry to be updated.
  333. * \param checked true if the entry should be checked; false otherwise.
  334. *
  335. * \threadsafety This function should be called on the thread that created the
  336. * tray.
  337. *
  338. * \since This function is available since SDL 3.2.0.
  339. *
  340. * \sa SDL_GetTrayEntries
  341. * \sa SDL_InsertTrayEntryAt
  342. * \sa SDL_GetTrayEntryChecked
  343. */
  344. extern SDL_DECLSPEC void SDLCALL SDL_SetTrayEntryChecked(SDL_TrayEntry *entry, bool checked);
  345. /**
  346. * Gets whether or not an entry is checked.
  347. *
  348. * The entry must have been created with the SDL_TRAYENTRY_CHECKBOX flag.
  349. *
  350. * \param entry the entry to be read.
  351. * \returns true if the entry is checked; false otherwise.
  352. *
  353. * \threadsafety This function should be called on the thread that created the
  354. * tray.
  355. *
  356. * \since This function is available since SDL 3.2.0.
  357. *
  358. * \sa SDL_GetTrayEntries
  359. * \sa SDL_InsertTrayEntryAt
  360. * \sa SDL_SetTrayEntryChecked
  361. */
  362. extern SDL_DECLSPEC bool SDLCALL SDL_GetTrayEntryChecked(SDL_TrayEntry *entry);
  363. /**
  364. * Sets whether or not an entry is enabled.
  365. *
  366. * \param entry the entry to be updated.
  367. * \param enabled true if the entry should be enabled; false otherwise.
  368. *
  369. * \threadsafety This function should be called on the thread that created the
  370. * tray.
  371. *
  372. * \since This function is available since SDL 3.2.0.
  373. *
  374. * \sa SDL_GetTrayEntries
  375. * \sa SDL_InsertTrayEntryAt
  376. * \sa SDL_GetTrayEntryEnabled
  377. */
  378. extern SDL_DECLSPEC void SDLCALL SDL_SetTrayEntryEnabled(SDL_TrayEntry *entry, bool enabled);
  379. /**
  380. * Gets whether or not an entry is enabled.
  381. *
  382. * \param entry the entry to be read.
  383. * \returns true if the entry is enabled; false otherwise.
  384. *
  385. * \threadsafety This function should be called on the thread that created the
  386. * tray.
  387. *
  388. * \since This function is available since SDL 3.2.0.
  389. *
  390. * \sa SDL_GetTrayEntries
  391. * \sa SDL_InsertTrayEntryAt
  392. * \sa SDL_SetTrayEntryEnabled
  393. */
  394. extern SDL_DECLSPEC bool SDLCALL SDL_GetTrayEntryEnabled(SDL_TrayEntry *entry);
  395. /**
  396. * Sets a callback to be invoked when the entry is selected.
  397. *
  398. * \param entry the entry to be updated.
  399. * \param callback a callback to be invoked when the entry is selected.
  400. * \param userdata an optional pointer to pass extra data to the callback when
  401. * it will be invoked.
  402. *
  403. * \threadsafety This function should be called on the thread that created the
  404. * tray.
  405. *
  406. * \since This function is available since SDL 3.2.0.
  407. *
  408. * \sa SDL_GetTrayEntries
  409. * \sa SDL_InsertTrayEntryAt
  410. */
  411. extern SDL_DECLSPEC void SDLCALL SDL_SetTrayEntryCallback(SDL_TrayEntry *entry, SDL_TrayCallback callback, void *userdata);
  412. /**
  413. * Simulate a click on a tray entry.
  414. *
  415. * \param entry The entry to activate.
  416. *
  417. * \threadsafety This function should be called on the thread that created the
  418. * tray.
  419. *
  420. * \since This function is available since SDL 3.2.0.
  421. */
  422. extern SDL_DECLSPEC void SDLCALL SDL_ClickTrayEntry(SDL_TrayEntry *entry);
  423. /**
  424. * Destroys a tray object.
  425. *
  426. * This also destroys all associated menus and entries.
  427. *
  428. * \param tray the tray icon to be destroyed.
  429. *
  430. * \threadsafety This function should be called on the thread that created the
  431. * tray.
  432. *
  433. * \since This function is available since SDL 3.2.0.
  434. *
  435. * \sa SDL_CreateTray
  436. */
  437. extern SDL_DECLSPEC void SDLCALL SDL_DestroyTray(SDL_Tray *tray);
  438. /**
  439. * Gets the menu containing a certain tray entry.
  440. *
  441. * \param entry the entry for which to get the parent menu.
  442. * \returns the parent menu.
  443. *
  444. * \threadsafety This function should be called on the thread that created the
  445. * tray.
  446. *
  447. * \since This function is available since SDL 3.2.0.
  448. *
  449. * \sa SDL_InsertTrayEntryAt
  450. */
  451. extern SDL_DECLSPEC SDL_TrayMenu * SDLCALL SDL_GetTrayEntryParent(SDL_TrayEntry *entry);
  452. /**
  453. * Gets the entry for which the menu is a submenu, if the current menu is a
  454. * submenu.
  455. *
  456. * Either this function or SDL_GetTrayMenuParentTray() will return non-NULL
  457. * for any given menu.
  458. *
  459. * \param menu the menu for which to get the parent entry.
  460. * \returns the parent entry, or NULL if this menu is not a submenu.
  461. *
  462. * \threadsafety This function should be called on the thread that created the
  463. * tray.
  464. *
  465. * \since This function is available since SDL 3.2.0.
  466. *
  467. * \sa SDL_CreateTraySubmenu
  468. * \sa SDL_GetTrayMenuParentTray
  469. */
  470. extern SDL_DECLSPEC SDL_TrayEntry * SDLCALL SDL_GetTrayMenuParentEntry(SDL_TrayMenu *menu);
  471. /**
  472. * Gets the tray for which this menu is the first-level menu, if the current
  473. * menu isn't a submenu.
  474. *
  475. * Either this function or SDL_GetTrayMenuParentEntry() will return non-NULL
  476. * for any given menu.
  477. *
  478. * \param menu the menu for which to get the parent enttrayry.
  479. * \returns the parent tray, or NULL if this menu is a submenu.
  480. *
  481. * \threadsafety This function should be called on the thread that created the
  482. * tray.
  483. *
  484. * \since This function is available since SDL 3.2.0.
  485. *
  486. * \sa SDL_CreateTrayMenu
  487. * \sa SDL_GetTrayMenuParentEntry
  488. */
  489. extern SDL_DECLSPEC SDL_Tray * SDLCALL SDL_GetTrayMenuParentTray(SDL_TrayMenu *menu);
  490. /**
  491. * Update the trays.
  492. *
  493. * This is called automatically by the event loop and is only needed if you're
  494. * using trays but aren't handling SDL events.
  495. *
  496. * \threadsafety This function should only be called on the main thread.
  497. *
  498. * \since This function is available since SDL 3.2.0.
  499. */
  500. extern SDL_DECLSPEC void SDLCALL SDL_UpdateTrays(void);
  501. /* Ends C function definitions when using C++ */
  502. #ifdef __cplusplus
  503. }
  504. #endif
  505. #include <SDL3/SDL_close_code.h>
  506. #endif /* SDL_tray_h_ */