ui.c 3.5 KB

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