zori.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. #ifndef zori_H_INCLUDED
  2. #define zori_H_INCLUDED
  3. /*
  4. * ZORI is a a GUI subsystem for use with Allegro 5 that handles
  5. * the user interface of Eruta.
  6. *
  7. * ZORI is a classic retained-mode GUI system that has an additional
  8. * pseudo-direct mode API. ZORI uses a handler system where events are handled
  9. * by call back functions which can be installed as specific handlers that are
  10. * activated on certain events. But, in the spirit of direct mode GUI's, all
  11. * widgets also keep state flags and value flags that can be used to see if they
  12. * have been activated and that can be inspected in a direct-mode GUI fashion.
  13. * With ZORI you can have your retained mode GUI pie and also eat it in direct
  14. * mode.
  15. *
  16. * In ZORI, there is only one top-level widget, the root, however, every widget
  17. * can contain any amount of child widgets. One level below the root widget are
  18. * the screen widgets which are intent to handle the GUI on one physical screen
  19. * or display. Every GUI screen can have different "pages", but only one page
  20. * should be active per screen. The idea is that a GUI page represents a single
  21. * GUI mode such as the main menu, the settings menu, the in game menu, etc,
  22. * where you definitely want only one to be active at any given time.
  23. *
  24. * The box model used in Zori will be as follows (XXX implement this):
  25. * ......................................
  26. * .margin .
  27. * . +-------------border-----------+ .
  28. * . | padding | .
  29. * . | .......................... | .
  30. * . | . . | .
  31. * . | . contents . | .
  32. * . | . . | .
  33. * . | .......................... | .
  34. * . | padding | .
  35. * . +------------------------------+ .
  36. * .margin .
  37. * ......................................
  38. *
  39. * The padding determines the minimal distance between the border or the
  40. * parent widget and it's contents and/or child widgets.
  41. *
  42. * The border's thickness is only relevant for visual effects.
  43. * It does not change the layout. The border is effectively "inside"
  44. * the padding and/or contents of the widget and is unrelated to the padding.
  45. *
  46. * The margin of a widget determines how closely that widget may be packed
  47. * to it's sibling widgets.
  48. *
  49. * The work in UI is divided between the top-level widgets,
  50. * namely the root and the page, and the other widgets.
  51. * The root and the page handle everything that depends on and/or may influence
  52. * several widgets at once, such as event dispatching but also setting the
  53. * focus, determining which widget is being hovered, or dragged, etc.
  54. * The latter functions change the state of several widgets, so they
  55. * are handled on the level of the container widgets.
  56. * The zori_widget and the other concrete widgets handle the individual
  57. * state and actions of the various widgets individually.
  58. *
  59. *
  60. *
  61. *
  62. */
  63. #include <stdbool.h>
  64. #include "eruta.h"
  65. #include "rebox.h"
  66. #include "miao.h"
  67. #include "str.h"
  68. /* Defines for C11/C99/C89 portability */
  69. #ifndef inline
  70. #define inline inline
  71. #endif
  72. /* Macros for possible later system library portability. */
  73. #define ZORI_EVENT_TYPE_IS_USER(T) ALLEGRO_EVENT_TYPE_IS_USER(T)
  74. #define ZORI_SYSTEM_EVENT_KEY_DOWN ALLEGRO_EVENT_KEY_DOWN
  75. #define ZORI_SYSTEM_EVENT_KEY_UP ALLEGRO_EVENT_KEY_UP
  76. #define ZORI_SYSTEM_EVENT_KEY_CHAR ALLEGRO_EVENT_KEY_CHAR
  77. #define ZORI_SYSTEM_EVENT_MOUSE_AXES ALLEGRO_EVENT_MOUSE_AXES
  78. #define ZORI_SYSTEM_EVENT_MOUSE_AXES ALLEGRO_EVENT_MOUSE_AXES
  79. #define ZORI_SYSTEM_EVENT_MOUSE_BUTTON_DOWN ALLEGRO_EVENT_MOUSE_BUTTON_DOWN
  80. #define ZORI_SYSTEM_EVENT_MOUSE_BUTTON_UP ALLEGRO_EVENT_MOUSE_BUTTON_UP
  81. /* Typedefs for possible later system library portability. */
  82. typedef ALLEGRO_COLOR zori_color;
  83. typedef ALLEGRO_BITMAP zori_bitmap;
  84. typedef ALLEGRO_FONT zori_font;
  85. typedef ALLEGRO_EVENT zori_system_event;
  86. typedef ALLEGRO_EVENT_TYPE zori_event_type;
  87. typedef ALLEGRO_DISPLAY zori_display;
  88. typedef Point zori_point;
  89. typedef Rebox zori_rebox;
  90. typedef Rebox zori_box;
  91. typedef int zori_id;
  92. typedef ALLEGRO_USTR zori_string;
  93. typedef ALLEGRO_USTR zori_text;
  94. /* Macros for possible portability. */
  95. #define zori_font_lineheight(FONT) al_get_font_line_height(FONT)
  96. #define zori_font_drawstr(FONT, COLOR, X, Y, ATTR, TEXT) al_draw_ustr(FONT, COLOR, X, Y, ATTR, TEXT)
  97. /* Default sizes of a widget. */
  98. #define ZORI_WIDGET_DEFAULT_W 640
  99. #define ZORI_WIDGET_DEFAULT_H 480
  100. /* Defaut padding and margin. */
  101. #define ZORI_PADDING_DEFAULT 4
  102. #define ZORI_MARGIN_DEFAULT 0
  103. /* Zori ID's. */
  104. #define ZORI_ID_OK_P(ID) ((ID) > -1)
  105. #define ZORI_ID_OK ((zori_id)(0))
  106. #define ZOTI_ID_GENERATE ((zori_id)(-1))
  107. #define ZORI_ID_ERROR ((zori_id)(-1))
  108. #define ZORI_ID_ENOMEM ((zori_id)(-2))
  109. #define ZORI_ID_EINVAL ((zori_id)(-3))
  110. /** Macro for the number of elements of an array .*/
  111. #define ZORI_ARRAY_AMOUNT(A) ((size_t)(sizeof(A))/(sizeof(*A)))
  112. /** Macro that expands to the the array itself, and the amount of elements
  113. * in that array, in that order. Only works on a statically sized array. */
  114. #define ZORI_ARRAY_AND_AMOUNT(A) (A), ZORI_ARRAY_AMOUNT(A)
  115. /* Macro: ZORI_CONTAINER_OF(PTR, TYPE, MEMBER)
  116. This macro returns, for the given pointer, a pointer to a containing struct
  117. of type TYPE, in which PTR is a member named MEMBER.
  118. This enables cool ways of type genericity and extension in plain C.
  119. It should not run afoul of strict aliasing since it passes over a char * pointer
  120. and a pointer of a containing struct or union.
  121. */
  122. #define ZORI_CONTAINER_OF(PTR, TYPE, MEMBER) \
  123. ((PTR) ? ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, MEMBER))) : NULL)
  124. /** Custom event types, used in conjunction with Allegro event types. */
  125. enum zori_custom_event_type {
  126. ZORI_EVENT_CUSTOM = ALLEGRO_GET_EVENT_TYPE('z', 'o', 'r', 'i'),
  127. /** Update event, called at regular intervals to update the state of the UI.*/
  128. ZORI_EVENT_UPDATE,
  129. /** First draw pass. */
  130. ZORI_EVENT_DRAW,
  131. /** Second draw pass for cursors. */
  132. ZORI_EVENT_OVERDRAW,
  133. /** Cleanup event. */
  134. ZORI_EVENT_DONE,
  135. /** Destruction event. */
  136. ZORI_EVENT_FREE,
  137. /** User definable activation event, such as a button being clicked
  138. * or a menu being opened. */
  139. ZORI_EVENT_ACTION,
  140. /** User definable close event, such as a menu being closed.
  141. * Note that the element may be re-opened later. */
  142. ZORI_EVENT_CLOSE,
  143. /** Child added event to ease setup. */
  144. ZORI_EVENT_NEW_CHILD,
  145. /* Below these line are internal events that should not be set or
  146. * activated by the user of the Zori library. */
  147. /** Activation event, such as a button being clicked or a menu being opened.
  148. * This event is ment to be used internally by the ZORI library before
  149. * the ZORI_EVENT_ACTION is raised. */
  150. ZORI_INTERNAL_EVENT_ACTION,
  151. /** Raised just before closing a widget. Sent to the sub-widgets to indicate
  152. * that they should close as well. */
  153. ZORI_INTERNAL_EVENT_CLOSE,
  154. };
  155. struct zori_widget;
  156. /* Common fields for an event. */
  157. struct zori_any_event {
  158. /* Type of the event, possibly copied from sysev. */
  159. zori_event_type type;
  160. /* Widget the event is sent to. */
  161. struct zori_widget * widget;
  162. /* Data specified in the handler. */
  163. void * data;
  164. };
  165. /* System event, that is coming from the underlying library used, e.g. Allegro. */
  166. struct zori_sys_event {
  167. struct zori_any_event any;
  168. zori_system_event * ev;
  169. };
  170. /* Update event, when UI has to update (animations, etc). */
  171. struct zori_update_event {
  172. struct zori_any_event any;
  173. double dt;
  174. };
  175. /* Draw event when the UI has to draw itself. */
  176. struct zori_draw_event {
  177. struct zori_any_event any;
  178. };
  179. /* Cleanup event. */
  180. struct zori_done_event {
  181. struct zori_any_event any;
  182. };
  183. /* Cleanup event. */
  184. struct zori_free_event {
  185. struct zori_any_event any;
  186. };
  187. /* Action event. */
  188. struct zori_action_event {
  189. struct zori_any_event any;
  190. };
  191. /* Close event. */
  192. struct zori_close_event {
  193. struct zori_any_event any;
  194. /* The widget that was closed and that sent this message to it's parent. */
  195. struct zori_widget * from;
  196. };
  197. /* New child event. */
  198. struct zori_new_child_event {
  199. struct zori_any_event any;
  200. /* The widget that was added as a child and that sent this message to it's parent. */
  201. struct zori_widget * child;
  202. };
  203. /** An event that ZORI can handle. The union is cunningly overlaid so
  204. * that the type field and the any field can be used in all cases. */
  205. union zori_event {
  206. /* Type of the event, possibly copied from sysev. */
  207. zori_event_type type;
  208. struct zori_any_event any;
  209. struct zori_sys_event sys;
  210. struct zori_draw_event draw;
  211. struct zori_update_event update;
  212. struct zori_done_event done;
  213. struct zori_free_event free;
  214. struct zori_action_event action;
  215. struct zori_close_event close;
  216. struct zori_new_child_event new_child;
  217. };
  218. /* Helper functions to get widget from an event. */
  219. static inline struct zori_widget *
  220. zori_event_widget(union zori_event * event) {
  221. if (!event) return NULL;
  222. return event->any.widget;
  223. }
  224. /* Helper functions to get data from an event. */
  225. static inline void * zori_event_data(union zori_event * event) {
  226. if (!event) return NULL;
  227. return event->any.data;
  228. }
  229. /* Helper function that checks if an event is a sys_event. */
  230. static inline bool zori_event_is_sys_event(union zori_event * event) {
  231. if (!event) return false;
  232. return (!ALLEGRO_EVENT_TYPE_IS_USER(event->type));
  233. }
  234. /* Helper functions to get system event from an event. Type checks the type. */
  235. static inline zori_system_event * zori_event_system_event(union zori_event * event) {
  236. if (!event) return NULL;
  237. if (!zori_event_is_sys_event(event)) return NULL;
  238. return event->sys.ev;
  239. }
  240. /* Helper functions to set widget for an event. */
  241. static inline struct zori_widget *
  242. zori_event_set_widget(union zori_event * event, struct zori_widget * widget) {
  243. if (!event) return NULL;
  244. return event->any.widget = widget;
  245. }
  246. /* Helper functions to set data for an event. */
  247. static inline void * zori_event_set_data(union zori_event * event, void * data) {
  248. if (!event) return NULL;
  249. return event->any.data = data;
  250. }
  251. /* Style flags */
  252. enum zori_graphic_style_flag {
  253. ZORI_STYLE_FLAG_BORDER = 1,
  254. ZORI_STYLE_FLAG_FILL = 2,
  255. ZORI_STYLE_FLAG_DEFAULT= (ZORI_STYLE_FLAG_BORDER | ZORI_STYLE_FLAG_FILL),
  256. };
  257. /* Font flags, from ALLEGRO font flags. */
  258. enum zori_font_style_flag {
  259. ZORI_FONT_ALIGN_LEFT = ALLEGRO_ALIGN_LEFT,
  260. ZORI_FONT_ALIGN_CENTRE = ALLEGRO_ALIGN_CENTRE,
  261. ZORI_FONT_ALIGN_RIGHT = ALLEGRO_ALIGN_RIGHT,
  262. ZORI_FONT_ALIGN_INTEGER = ALLEGRO_ALIGN_INTEGER,
  263. };
  264. /* Bitmap flags from allegro bitmap flags. */
  265. enum zori_bitmap_flag {
  266. ZORI_BITMAP_FLIP_HORIZONTAL = ALLEGRO_FLIP_HORIZONTAL,
  267. ZORI_BITMAP_FLIP_VERTICAL = ALLEGRO_FLIP_VERTICAL,
  268. };
  269. /**
  270. * For style: not all combinations make sense. How to improve this?
  271. *
  272. * What is available in CSS 1:
  273. *
  274. * font : font of the text (with many sub details in CCS not relevant here).
  275. * color: text color of an element
  276. * background-color: background color of an element.
  277. * background-image: background image of an element.
  278. * background-repeat: determines how/if the image is repeated.
  279. * background-attachment: fixed with regard to the canvas.
  280. * background-position: initial position of the background image.
  281. * word-spacing: text spacing
  282. * letter-spacing: text spacing
  283. * text-decoration: text decoration (underline, overline, etc)
  284. * vertical-align: vertical alignment, top, center, bottom based on the parent.
  285. * text-transform:
  286. * text-align: text alignment, left, right, centered.
  287. * text-indent: Text indentation.
  288. * line-height: Height of a line of text.
  289. * margin: sizes of the margin
  290. * padding: sizes of the padding
  291. * border-width: sizes of the border
  292. * border-color: color of the border
  293. * border-style: style of the border (per side)
  294. * border: border style, general of per side
  295. * width: width of the box
  296. * height: height of the box
  297. * float: floating
  298. * clear: float control
  299. * display: display type (visual, block, hiden, none, etc)
  300. * white-space: white space display.
  301. * list-style: list styling with type, image and position of the image.
  302. *
  303. * Squeezing it down to the essential, we retain:
  304. *
  305. * text.font : Font for texts.
  306. * text.color : Color for texts.
  307. * text.flags : Style flags, merges horizontal alignment, decoration, etc.
  308. *
  309. * back.color : Background color.
  310. * back.image : Background image.
  311. * back.flags : Style flags, solid background, no background, etc.
  312. * back.radius : Size of corner for image_scale9 algorithm.
  313. *
  314. * border.color : Border color.
  315. * border.size : Border size (thickness).
  316. * border.flags : Border style flags.
  317. * border.radius: Rounded corners radius.
  318. *
  319. * cursor.color : Text cursor color.
  320. *
  321. * mouse.back : Mouse cursor background information.
  322. * mouse.border : Mouse cursor border.
  323. * keyjoy.back : Keyjoy cursor background information.
  324. * keyjoy.border : Keyjoy cursor border.
  325. *
  326. *
  327. * width, height, margins and padding are not considered style but positioning.
  328. * The keyjoy and mouse cursor and a border are only available
  329. * per-screen.
  330. *
  331. * The text cursor has a color only.
  332. *
  333. * 1. Text can have a font, a color and font flags (centered, etc)
  334. * 2. A rectangle / container / widget can have a background image combined
  335. * with a background color it can have draw flags for the background image and
  336. * for the color (fill or not). Furthermore it can have a border color.
  337. * Possibly (through not suupported now) would be a background
  338. *
  339. *
  340. */
  341. /* The style of the backgrounbd of a widget. */
  342. struct zori_background_style {
  343. zori_color color;
  344. zori_bitmap * image;
  345. int flags;
  346. int radius;
  347. };
  348. /* The style of the border of a widget. */
  349. struct zori_border_style {
  350. zori_color color;
  351. zori_bitmap * image;
  352. int flags;
  353. int radius;
  354. int size;
  355. };
  356. /* A text style has all elements needed to style a piece of text. I consists of the text color, font and font flags flags applied to a part of the GUI.
  357. */
  358. struct zori_text_style {
  359. zori_color color;
  360. zori_font * font;
  361. int flags;
  362. };
  363. /* A style is a set of style elements for a widget or cursor. */
  364. struct zori_style {
  365. struct zori_background_style back;
  366. struct zori_border_style border;
  367. struct zori_text_style text;
  368. };
  369. struct zori_widget;
  370. /* Event handler results. */
  371. enum zori_handle_result {
  372. ZORI_HANDLE_ERROR = -1, /* An error ocurred, stop propagating to siblings as wel.*/
  373. ZORI_HANDLE_DONE = 0, /* The event was handled and consumed, no need to propagate to children automatically (widget may re-send event manually to children.) Sibling still receive the event. */
  374. ZORI_HANDLE_IGNORE = 1, /* Event wasn't handled, propagate to children. */
  375. ZORI_HANDLE_PASS = 2, /* Event was handled, but needs to be propagated.*/
  376. };
  377. /* Returns whether or not a event needs to be propagated based on the
  378. * result of a handler. */
  379. static int zori_propagate_event_p(enum zori_handle_result result) {
  380. switch(result) {
  381. case ZORI_HANDLE_ERROR: return 0;
  382. case ZORI_HANDLE_DONE: return 0;
  383. case ZORI_HANDLE_IGNORE: return !0;
  384. case ZORI_HANDLE_PASS: return !0;
  385. default: return !0;
  386. }
  387. }
  388. typedef enum zori_handle_result (zori_handler_func)(union zori_event * event);
  389. /* A single event handler */
  390. struct zori_handler {
  391. zori_event_type type;
  392. zori_handler_func * handler;
  393. void * data;
  394. };
  395. /* A dynamic array of event handlers event handlers. */
  396. struct zori_handlers miao_of_type(struct zori_handler);
  397. /* An entry in a widget registry. */
  398. struct zori_registry_entry {
  399. zori_id id;
  400. struct zori_widget * widget;
  401. };
  402. /* A widget registry as a dynamic array of entries. */
  403. struct zori_registry miao_of_type(struct zori_registry_entry);
  404. /* Generic state flags for several zori structs. */
  405. enum zori_flag {
  406. /* The object is not visible, though it may still be interacted with.*/
  407. ZORI_FLAG_HIDDEN = 1 << 0,
  408. /* The object cannot be interacted with, though it is still visible. */
  409. ZORI_FLAG_DISABLED = 1 << 1,
  410. /* The object is both hidden and disabled. */
  411. ZORI_FLAG_DEACTIVATED = ZORI_FLAG_HIDDEN | ZORI_FLAG_DISABLED,
  412. /* The object is being hovered by the mouse cursor. */
  413. ZORI_FLAG_HOVERED = 1 << 2,
  414. /* The object is "marked" for activation by the keyjoy cursor */
  415. ZORI_FLAG_MARKED = 1 << 3,
  416. /* The object is ready to report a "result".
  417. * What that is depends on the object. */
  418. ZORI_FLAG_READY = 1 << 4,
  419. };
  420. /* Typedef for the type of a widget.
  421. * Not an enum since every widget may define this itself.
  422. */
  423. typedef uint32_t zori_widget_type;
  424. /* Generic widget types. */
  425. #define ZORI_WIDGET_TYPE_NONE ((zori_widget_type)(0))
  426. /* Macro to help generate widget types. */
  427. #define ZORI_WIDGET_TYPE(A, B, C, D) ((zori_widget_type)((A<<24) | (B<<16) | (C<<8) | D))
  428. /* Mouse or keyboard/joystick cursor. */
  429. struct zori_cursor {
  430. zori_point p;
  431. struct zori_widget * hover;
  432. struct zori_widget * focus;
  433. zori_bitmap * bitmap;
  434. enum zori_flag flags;
  435. /* Style of the cursor itself. */
  436. struct zori_style style;
  437. /* Style for the element marked or hovered if that element responds to it. */
  438. struct zori_style target_style;
  439. };
  440. /* Support multiple cursors... */
  441. struct zori_cursors {
  442. struct zori_cursor mouse;
  443. struct zori_cursor keyjoy;
  444. };
  445. /* The type of the value of the result of a widget. */
  446. enum zori_result_type {
  447. ZORI_RESULT_TYPE_NONE = 0,
  448. ZORI_RESULT_TYPE_INTEGER= 1,
  449. ZORI_RESULT_TYPE_STRING = 2,
  450. ZORI_RESULT_TYPE_CLOSED = 3,
  451. };
  452. /* The value of a result of a widget, see below. */
  453. union zori_result_value {
  454. int integer;
  455. zori_string * string;
  456. int closed;
  457. };
  458. /* The "result" of a widget. If the flag ready is set, the
  459. * widget has a result to report . This normally happens when it was
  460. * clicked, or when a menu item was selected, */
  461. struct zori_result {
  462. int ready;
  463. union zori_result_value value;
  464. enum zori_result_type type;
  465. void * extra;
  466. };
  467. /*
  468. on_enter
  469. on_enter(data = {})
  470. on_event(*args)
  471. on_event(*data)
  472. on_key_down(*args)
  473. on_leave(name=nil)
  474. on_mouse_axes(t, x, y, z, w, dx, dy, dz, dw)
  475. on_mouse_button_down(t, x, y, z, w, b)
  476. on_mouse_button_up(t, x, y, z, w, b)
  477. on_mouse_in(x, y, from)
  478. on_mouse_out(x, y, to)
  479. on_resize
  480. */
  481. struct zori_widget;
  482. struct zori_widget_list miao_of_type(struct zori_widget *);
  483. struct zori_widget {
  484. /* ID of the widget, used in most external API's. */
  485. zori_id id;
  486. /* Root level widget under which this widget is active. */
  487. struct zori_widget * root;
  488. /* Position and size of the widget. */
  489. zori_rebox box;
  490. /* Outer rectangle of the widget, with the margin added. */
  491. zori_rebox outer;
  492. /* Inner rectangle of the widget, with the padding removed. */
  493. zori_rebox inner;
  494. /* Z ordering. */
  495. int z;
  496. /* Style. */
  497. struct zori_style style;
  498. /* Handlers. */
  499. struct zori_handlers handlers;
  500. /* Related widgets. */
  501. struct zori_widget * parent;
  502. struct zori_widget_list children;
  503. /* Flags. */
  504. enum zori_flag flags;
  505. /* Type of the widget. */
  506. zori_widget_type type;
  507. /* Generic "result", of last operation on widget. */
  508. struct zori_result result;
  509. /* Cannot easily use the handers for destroying the widget, so in stead
  510. * provide a destructor. */
  511. void (*destroy)(struct zori_widget * widget);
  512. };
  513. /* An array of widget pointers. */
  514. struct zori_widget_array miao_of_type(struct zori_widget *);
  515. /* Forward declarations. */
  516. struct zori_screen;
  517. struct zori_console;
  518. /** Icons of the root style (see below) */
  519. struct zori_root_style_icons {
  520. zori_bitmap * paused;
  521. };
  522. /* Style elements specific for the root element. These are global to the
  523. * whole GUI under the given root.*/
  524. struct zori_root_style {
  525. struct zori_root_style_icons icons;
  526. };
  527. /*
  528. * Root level widget, my spread out over several displays.
  529. * In Zori, there can only be a single root level widget active.
  530. * It's ID is always 0;
  531. */
  532. struct zori_root {
  533. /* A root is a widget. */
  534. struct zori_widget widget;
  535. /* Current active screen widget if any. */
  536. struct zori_screen * active_screen;
  537. /* Current active console if any. */
  538. struct zori_console * console;
  539. /*- Style elements for all the GUI, not per widget. */
  540. struct zori_root_style style;
  541. };
  542. /* Forward declaration of a page. */
  543. struct zori_page;
  544. struct zori_root * zori_get_root(void);
  545. /* Initializes Zori and creates a top level widget. Returns 0 on success
  546. * or negative on error. The style will be copied and set as default
  547. * if it is not NULL. Otherwise a built-in style will be used.
  548. * Not that ZORI will NOT clean up any images or fonts it uses by itself.
  549. */
  550. zori_id zori_start(struct zori_style * default_style);
  551. /* Shut down Zori and destroys all widgets. Return 0 on succes or
  552. * negative on error.
  553. */
  554. zori_id zori_shutdown();
  555. /* Creates a new screen widget. Normally this should be the first widget
  556. * you create after zori_start. */
  557. /* Activates the page on it's display. All other pages are dectivated and
  558. * hidden. */
  559. zori_id zori_activate_page(zori_id page);
  560. /* Creates a new generic widget on the given screen with the given
  561. * dimensions. */
  562. zori_id zori_new(zori_id screen, zori_rebox * box);
  563. /* Sets the flags of a widget. */
  564. zori_id zori_set_flags(zori_id widget, enum zori_flag flags);
  565. /* Sets the whole style of a widget. */
  566. zori_id zori_set_style(zori_id id, struct zori_style * style);
  567. /* Sets the background color of the widget. */
  568. zori_id zori_set_background_color(zori_id id, zori_color color);
  569. /* Sets the foreground color of the widget. */
  570. zori_id zori_set_foreground_color(zori_id id, zori_color color);
  571. /* Creates a new frame widget. */
  572. zori_id zori_new_frame_widget(zori_id parent, zori_rebox box);
  573. /* Creates a new (vertical) menu widget. */
  574. zori_id zori_new_menu_widget(zori_id parent, zori_rebox box, char * text);
  575. /* Creates a new button widget. */
  576. zori_id zori_new_button_widget(zori_id parent, zori_rebox box, char * text);
  577. /* Creates a new conversation widget. */
  578. zori_id zori_new_conversation_widget(zori_id parent, zori_rebox box, char * text);
  579. /* Draws the whole UI and all visible parts. */
  580. void zori_draw_all(void);
  581. /* Updates the state of the UI. Pass in the time passed since last update. */
  582. void zori_update(double dt);
  583. /* Registers an event handler for a widget. */
  584. zori_id zori_register(zori_id id, zori_event_type type, zori_handler_func handler, void * extra);
  585. int zori_handler_compare(const void *v1, const void *v2);
  586. struct zori_handler *zori_handlers_add(struct zori_handlers *me, zori_event_type type, zori_handler_func *handler, void *data);
  587. void zori_handlers_done(struct zori_handlers *me);
  588. void zori_handlers_init(struct zori_handlers *me);
  589. struct zori_handler *zori_handlers_search(struct zori_handlers *me, zori_event_type type);
  590. int zori_handlers_handle(struct zori_handlers *me, union zori_event *event);
  591. struct zori_root *zori_get_root(void);
  592. struct zori_widget *zori_get_root_widget(void);
  593. struct zori_widget *zori_get_widget(zori_id id);
  594. zori_id zori_get_unused_id(void);
  595. zori_id zori_initialize_root(void);
  596. zori_id zori_start(struct zori_style *default_style);
  597. zori_id zori_set_margins(zori_id id, int left, int top, int right, int bottom);
  598. zori_id zori_set_margin(zori_id id, int size);
  599. zori_id zori_set_paddings(zori_id id, int left, int top, int right, int bottom);
  600. zori_id zori_set_padding(zori_id id, int size);
  601. zori_font *zori_text_font(zori_id id);
  602. void zori_destroy_root(void);
  603. zori_id zori_shutdown(void);
  604. void zori_draw_all(void);
  605. int zori_handle_system_event(zori_system_event *sysev);
  606. void zori_update(double dt);
  607. int zori_result(zori_id id);
  608. /* Use generated header file for function prototypes. */
  609. #include "zori_proto.h"
  610. #endif