widget.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #ifndef bbwidget_H_INCLUDED
  2. #define bbwidget_H_INCLUDED
  3. #include "eruta.h"
  4. #include "image.h"
  5. #include "rebox.h"
  6. /* BBBBWidget, Beoran's Bad BBWidget set for allegro. */
  7. /* Custom action types compatible with Allegro events */
  8. enum BBWidgetEventNumbers_ {
  9. BBWIDGET_EVENT_DRAW = AL_ID('B', 'W', 'I', 0),
  10. BBWIDGET_EVENT_UPDATE = AL_ID('B', 'W', 'I', 1),
  11. BBWIDGET_EVENT_DONE = AL_ID('B', 'W', 'I', 2),
  12. BBWIDGET_EVENT_FREE = AL_ID('B', 'W', 'I', 3),
  13. BBWIDGET_EVENT_FOCUS = AL_ID('B', 'W', 'I', 4)
  14. };
  15. /* Style describes the style of a widget. */
  16. struct Style_ {
  17. Color forecolor;
  18. Color backcolor;
  19. Font * font;
  20. Image * background;
  21. };
  22. typedef struct Style_ Style;
  23. /* Predefine BBWidget typedef. */
  24. typedef struct BBWidget_ BBWidget;
  25. /* Predefine BBConsole typedef. */
  26. typedef struct BBConsole_ BBConsole;
  27. /* Predefine widget method table. */
  28. typedef struct BBWidgetMetab_ BBWidgetMetab;
  29. /* Very simple array based event handler. It's O(N) for now,
  30. but N is very small here, so the simplicity of creating a method table
  31. is more important. */
  32. typedef struct BBWidgetAction_ BBWidgetAction;
  33. typedef int BBWidgetHandler(BBWidget * widget, void * data);
  34. struct BBWidgetAction_ {
  35. int type;
  36. BBWidgetHandler * handler;
  37. };
  38. /* BBWidget flags. A widget can be : visible or not
  39. * listening (accepting input) input or not (if it invisble and not listening
  40. * input it's inactive, otherwise active)
  41. * it can have focus or not (no focus listens to input
  42. * but may ignore it, focus accepts input).
  43. * Selected is for checkboxes that are selected or buttons that are down, etc...
  44. *
  45. */
  46. enum BBWidgetFlags_ {
  47. BBWIDGET_FLAG_VISIBLE = 1,
  48. BBWIDGET_FLAG_LISTENING = 2,
  49. BBWIDGET_FLAG_ACTIVE = 3,
  50. BBWIDGET_FLAG_FOCUSED = 4,
  51. BBWIDGET_FLAG_SELECTED = 8,
  52. };
  53. /* Widget state flags . */
  54. typedef enum BBWidgetFlags_ BBWidgetFlags;
  55. /* Flag testing macros. */
  56. /* Checks if flag is set.
  57. Flag will be evaluated 2 times so must be a constant or a variable that is
  58. only read.
  59. */
  60. #define BBWIDGET_FLAG_P(BBWIDGET, FLAG) (((BBWIDGET)->flag&(FLAG))==(FLAG))
  61. #define BBWIDGET_VISIBLE_P(BBWIDGET, FLAG) (((BBWIDGET)->flag&(FLAG))==(FLAG))
  62. #define BBWIDGET_HANDLE_OK 0
  63. #define BBWIDGET_HANDLE_IGNORE 1
  64. #define BBWIDGET_HANDLE_ERROR -1
  65. /* BBWidget method cache for commonly used methods */
  66. struct BBWidgetMetab_ {
  67. BBWidgetHandler * free;
  68. BBWidgetHandler * done;
  69. BBWidgetHandler * draw;
  70. BBWidgetHandler * update;
  71. };
  72. /* BBWidget interface */
  73. struct BBWidgetIfa_ {
  74. BBWidget * self;
  75. BBWidgetMetab * metab;
  76. };
  77. /* BBWidgets are individual parts of the UI.
  78. As a simplification, BBWidgets are considered to occupy "panes" ordered
  79. in the user interface from back to front. They do not contain other widgets
  80. and do not have any generic relations between them.
  81. A note on pointer ownership: the pointers to font and image in style
  82. are NOT cleaned up, since style is intended to be mostly a shallow copy in which
  83. font and background image are repeated many times.
  84. */
  85. struct BBWidget_ {
  86. /* Event handler table. */
  87. BBWidgetAction * acts;
  88. /* Method cache */
  89. BBWidgetMetab metab;
  90. /* Bounds, this is a rectangular box. */
  91. Rebox bounds;
  92. /* Styling elements. */
  93. Style style;
  94. /* BBWidget elements: */
  95. /* Unique ID. */
  96. int id;
  97. /* Flags (active, disabled, etc) */
  98. int flags;
  99. /* Priority of widget */
  100. int z;
  101. };
  102. // BBConsole input handler, called when a line of text (a command)
  103. // is typed.
  104. typedef int (BBConsoleCommand)
  105. (BBConsole * console, const char * command, void * extra);
  106. /* This file was generated with:
  107. 'cfunctions -c -aoff -n -w bbwidget_proto src/widget.c' */
  108. #ifndef CFH_BBWIDGET_PROTO
  109. #define CFH_BBWIDGET_PROTO
  110. /* From 'src/widget.c': */
  111. Style style_make (Color fore , Color back , Font * font , Image * background );
  112. Style * style_initstyle (Style * self , Style style );
  113. Style * style_init (Style * self , Color fore , Color back , Font * font , Image * background );
  114. Color style_forecolor (Style * self );
  115. Color style_backcolor (Style * self );
  116. Image * style_background (Style * self );
  117. Font * style_font (Style * self );
  118. Rebox bbwidget_bounds (BBWidget * self );
  119. int bbwidget_w (BBWidget * self );
  120. int bbwidget_h (BBWidget * self );
  121. int bbwidget_x (BBWidget * self );
  122. int bbwidget_y (BBWidget * self );
  123. int bbwidget_z (BBWidget * self );
  124. Color bbwidget_forecolor (BBWidget * self );
  125. Color bbwidget_backcolor (BBWidget * self );
  126. Font * bbwidget_font (BBWidget * self );
  127. Image * bbwidget_background (BBWidget * self );
  128. int bbwidget_flags (BBWidget * self );
  129. int bbwidget_id (BBWidget * self , int id );
  130. int bbwidget_flags_ (BBWidget * self , int flags );
  131. int bbwidget_id_ (BBWidget * self , int id );
  132. int bbwidget_flag (BBWidget * self , int flag );
  133. int bbwidget_unflag (BBWidget * self , int flag );
  134. int bbwidget_doflag (BBWidget * self , int flag , int set );
  135. int bbwidget_flag_p (BBWidget * self , int flag );
  136. int bbwidget_visible (BBWidget * self );
  137. int bbwidget_listening (BBWidget * self );
  138. int bbwidget_active (BBWidget * self );
  139. int bbwidget_focused (BBWidget * self );
  140. int bbwidget_selected (BBWidget * self );
  141. int bbwidget_visible_ (BBWidget * self , int set );
  142. int bbwidget_listening_ (BBWidget * self , int set );
  143. int bbwidget_active_ (BBWidget * self , int set );
  144. int bbwidget_focused_ (BBWidget * self , int set );
  145. int bbwidget_selected_ (BBWidget * self , int set );
  146. BBWidget *
  147. bbwidget_acts_(BBWidget * self, BBWidgetAction * acts);
  148. BBWidget *
  149. bbwidget_initall(BBWidget * self, int id, BBWidgetAction * acts,
  150. Rebox bounds, Style style);
  151. BBWidget *
  152. bbwidget_initbounds(BBWidget * self, int id, BBWidgetAction * acts, Rebox bounds);
  153. BBWidget *
  154. bbwidget_initparent (BBWidget * self , int id , BBWidget * parent );
  155. BBWidget *
  156. bbwidget_allocate(void);
  157. BBWidget *
  158. bbwidget_done (BBWidget * widget );
  159. BBWidget *
  160. bbwidget_free (BBWidget * self );
  161. void bbwidget_draw (BBWidget * self );
  162. void bbwidget_handle (BBWidget * self , ALLEGRO_EVENT * event );
  163. void bbwidget_update (BBWidget * self , ALLEGRO_EVENT * event );
  164. void bbwidget_drawroundframe (BBWidget * self );
  165. int bbconsole_handle(BBWidget * widget, ALLEGRO_EVENT * event);
  166. void
  167. bbconsole_command_(BBConsole * self , BBConsoleCommand * command , void * data );
  168. int bbconsole_docommand (BBConsole * self , const char * text );
  169. int bbconsole_addstr (BBConsole * self , const char * str );
  170. int bbconsole_puts(BBConsole * self , const char * str );
  171. int bbconsole_vprintf(BBConsole * self, const char * format, va_list args);
  172. int bbconsole_printf(BBConsole * self, const char * format, ...);
  173. int bbconsole_draw(BBWidget * widget, void * data);
  174. void bbconsole_active_ (BBConsole * self , int active );
  175. int bbconsole_active (BBConsole * self );
  176. int bbconsole_scroll (BBConsole * self , int direction );
  177. int bbconsole_done(BBWidget * widget, void * data );
  178. int bbconsole_free(BBWidget * widget, void * data );
  179. BBConsole * bbconsole_alloc(void);
  180. BBConsole * bbconsole_initall (BBConsole * self , int id , Rebox bounds , Style style );
  181. BBConsole * bbconsole_new (int id , Rebox bounds , Style style );
  182. #endif /* CFH_BBWIDGET_PROTO */
  183. typedef BBWidget * (BBWidgetDraw)(BBWidget * self);
  184. #endif