ui.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "eruta.h"
  2. #include "image.h"
  3. #include "ui.h"
  4. /*
  5. * UI handles the user interface of Eruta.
  6. *
  7. * There is only one top-level widget, however, every widget can contain
  8. * any amout of child widgets. BBWidgets that are children of the same parent
  9. * widget are said to be on the same level.
  10. *
  11. *
  12. * The box model used in eruta is as follows:
  13. * ......................................
  14. * .margin .
  15. * . +-------------border-----------+ .
  16. * . | padding | .
  17. * . | .......................... | .
  18. * . | . . | .
  19. * . | . contents . | .
  20. * . | . . | .
  21. * . | .......................... | .
  22. * . | padding | .
  23. * . +------------------------------+ .
  24. * .margin .
  25. * ......................................
  26. *
  27. * The padding determines the minimal distance between the border or the
  28. * parent widget and it's contents and/or child widgets.
  29. *
  30. * The border's thickness is only relevant for visual effects. It does not change
  31. * the layout. The border is effectively "inside" the padding of the widget.
  32. *
  33. * The margin of a widget determines how closely that widget may be packed
  34. * to it's sibling widgets.
  35. *
  36. * The work in UI is divided between the UI and the BBWidget. The UI struct
  37. * handles everything that depends on and/or may influence several widgets at
  38. * once, such as event dispatching but also setting the focus, determining which
  39. * widget is being hovered, or dragged, etc. The latter fuctions change the state
  40. * of several widgets, so they are handled on the level of the system.
  41. * The BBWidget class and it's child classes handle the individual state and
  42. * actions of the various widgets individually.
  43. *
  44. */
  45. #include "ui.h"
  46. #include "dynar.h"
  47. #include <stdarg.h>
  48. /** UI manages the graphical user interface and menus. */
  49. struct UI_ {
  50. /* Ths UI is a widget itself. How meta that is! :) */
  51. /*BBWidget widget;*/
  52. /* ID generator. */
  53. int last_id;
  54. /* The widgets in the UI, in a dynamic array. */
  55. Dynar * widgets;
  56. };
  57. /** Allocates an unitinialized UI. */
  58. UI * ui_alloc() {
  59. return STRUCT_ALLOC(UI);
  60. }
  61. /** Draws the user interface */
  62. /** Initializes the user interface. */
  63. typedef void void_function(void);
  64. typedef void_function * void_function_ptr;
  65. /** Generic utility */
  66. /** Helps to scan variable arguments with a format string,
  67. much like how scanf would work fro stdin. */
  68. void vva_list_scan(va_list args, char * format, va_list data) {
  69. for( ; (*format); format++) {
  70. switch((*format)) {
  71. case 'p': (*va_arg(data, void**) ) = va_arg(args, void*); break;
  72. case 'd':
  73. case 'i': (*va_arg(data, int*)) = va_arg(args, int); break;
  74. case 'o':
  75. case 'u': (*va_arg(data, unsigned int*))
  76. = va_arg(args, unsigned int); break;
  77. case 'l': (*va_arg(data, long*)) = va_arg(args, long); break;
  78. case 's': (*va_arg(data, char**)) = va_arg(args, char*); break;
  79. case 'f': (*va_arg(data, double*)) = va_arg(args, double); break;
  80. case 'F': (*va_arg(data, void_function_ptr*))
  81. = va_arg(args, void_function_ptr); break;
  82. default:
  83. break;
  84. }
  85. }
  86. }
  87. /** Helps to scan variable arguments with a format string,
  88. much like how scanf would work for stdin. */
  89. void va_list_scan(va_list args, char * format, ...) {
  90. va_list data;
  91. va_start(data, format);
  92. vva_list_scan(args, format, data);
  93. va_end(data);
  94. }