ngui.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* ngui: tiny immediate-mode GUI toolkit for ring-3 canvas windows.
  2. *
  3. * The toolkit owns no memory: the app passes its framebuffer and keeps the
  4. * state structs for text fields and scroll panes. Everything is RGB565 and
  5. * ASCII, using the kernel's 8x16 font. See ucmd_files.c for a user. */
  6. #ifndef NGUI_H
  7. #define NGUI_H
  8. #include "syscall.h"
  9. #include "wm_abi.h"
  10. #define NGUI_INPUT_MAX 128
  11. #define NGUI_INPUT_CHANGED 1
  12. #define NGUI_INPUT_SUBMIT 2
  13. #define NGUI_INPUT_CANCEL 4
  14. /* Key codes from io.c. Arrows are 0x100..; the page/home/end keys are
  15. * produced by the extended PS/2 path. */
  16. #define NGUI_KEY_ESC 0x1B
  17. #define NGUI_KEY_BACKSPACE 0x08
  18. #define NGUI_KEY_TAB 0x09
  19. #define NGUI_KEY_LEFT 0x100
  20. #define NGUI_KEY_RIGHT 0x101
  21. #define NGUI_KEY_UP 0x102
  22. #define NGUI_KEY_DOWN 0x103
  23. #define NGUI_KEY_PAGE_UP 0x104
  24. #define NGUI_KEY_PAGE_DOWN 0x105
  25. #define NGUI_KEY_HOME 0x106
  26. #define NGUI_KEY_END 0x107
  27. #define NGUI_KEY_DELETE 0x108
  28. #define NGUI_RGB(r, g, b) \
  29. ((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3)))
  30. struct ngui_rect {
  31. int x, y, w, h;
  32. };
  33. struct ngui_theme {
  34. unsigned short bg;
  35. unsigned short panel;
  36. unsigned short panel_alt;
  37. unsigned short border;
  38. unsigned short text;
  39. unsigned short text_dim;
  40. unsigned short accent;
  41. unsigned short selection;
  42. unsigned short button;
  43. unsigned short button_hot;
  44. unsigned short button_down;
  45. unsigned short icon_folder;
  46. unsigned short icon_file;
  47. unsigned short icon_disk;
  48. unsigned short error;
  49. };
  50. extern const struct ngui_theme ngui_theme_dark;
  51. enum ngui_icon {
  52. NGUI_ICON_FOLDER,
  53. NGUI_ICON_FILE,
  54. NGUI_ICON_DISK,
  55. NGUI_ICON_UP,
  56. NGUI_ICON_HOME,
  57. NGUI_ICON_REFRESH,
  58. NGUI_ICON_PLUS,
  59. NGUI_ICON_CHEVRON_LEFT,
  60. NGUI_ICON_CHEVRON_RIGHT
  61. };
  62. struct ngui_input {
  63. char text[NGUI_INPUT_MAX];
  64. unsigned int len;
  65. unsigned int cursor;
  66. unsigned int scroll; /* first visible character */
  67. const char *placeholder;
  68. };
  69. struct ngui_scroll {
  70. int offset; /* pixels scrolled from the top */
  71. int content_h;
  72. int view_h;
  73. int dragging;
  74. int drag_dy;
  75. };
  76. struct ngui {
  77. unsigned short *px;
  78. int w, h;
  79. struct ngui_theme theme;
  80. /* Current pointer state, content-relative. */
  81. int mouse_x, mouse_y;
  82. unsigned int buttons;
  83. int inside;
  84. int focused;
  85. /* Press/release edges collected by ngui_begin(). */
  86. unsigned int pressed;
  87. unsigned int released;
  88. int press_x, press_y;
  89. int release_x, release_y;
  90. int clicked;
  91. int focus_claimed;
  92. unsigned int hot;
  93. unsigned int active;
  94. unsigned int focus;
  95. struct ngui_rect clip[4];
  96. int nclip;
  97. int dirty;
  98. unsigned int last_blink;
  99. int caret_on;
  100. int keys[32];
  101. unsigned int key_head, key_tail;
  102. int last_mouse_x, last_mouse_y;
  103. int last_inside, last_focused;
  104. };
  105. void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h);
  106. int ngui_begin(struct ngui *ui); /* 1 = app should redraw */
  107. void ngui_end(struct ngui *ui);
  108. /* Same bookkeeping, but only publish the canvas when `present` is nonzero.
  109. * Lets an app poll input without copying an unchanged frame every loop. */
  110. void ngui_end_ex(struct ngui *ui, int present);
  111. void ngui_invalidate(struct ngui *ui);
  112. void ngui_set_focus(struct ngui *ui, unsigned int id);
  113. unsigned int ngui_focus(struct ngui *ui);
  114. int ngui_key(struct ngui *ui);
  115. int ngui_key_peek(struct ngui *ui);
  116. /* Layout: cut a rectangle off one side of `r` and return the piece. */
  117. struct ngui_rect ngui_cut_top(struct ngui_rect *r, int h);
  118. struct ngui_rect ngui_cut_bottom(struct ngui_rect *r, int h);
  119. struct ngui_rect ngui_cut_left(struct ngui_rect *r, int w);
  120. struct ngui_rect ngui_cut_right(struct ngui_rect *r, int w);
  121. void ngui_inset(struct ngui_rect *r, int n);
  122. int ngui_rect_contains(const struct ngui_rect *r, int x, int y);
  123. /* Primitives. All drawing is clipped to the current clip stack and canvas. */
  124. void ngui_push_clip(struct ngui *ui, struct ngui_rect r);
  125. void ngui_pop_clip(struct ngui *ui);
  126. void ngui_fill_rect(struct ngui *ui, struct ngui_rect r, unsigned short color);
  127. void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color);
  128. void ngui_line(struct ngui *ui, int x0, int y0, int x1, int y1,
  129. unsigned short color);
  130. void ngui_text(struct ngui *ui, int x, int y, const char *s,
  131. unsigned short color);
  132. void ngui_text_n(struct ngui *ui, int x, int y, const char *s,
  133. unsigned int n, unsigned short color);
  134. void ngui_text_styled(struct ngui *ui, int x, int y, const char *s,
  135. unsigned int n, unsigned short color, int scale, int bold);
  136. void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
  137. const char *s, unsigned short color);
  138. unsigned int ngui_text_width(const char *s);
  139. unsigned int ngui_text_width_n(const char *s, unsigned int n, int scale);
  140. void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
  141. unsigned short color);
  142. /* Widgets. IDs must be stable across frames. */
  143. int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  144. const char *label);
  145. int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  146. enum ngui_icon icon);
  147. int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
  148. struct ngui_input *in);
  149. void ngui_input_set(struct ngui_input *in, const char *s);
  150. /* Scrolling pane: call begin, draw the content with scroll->offset, end. */
  151. void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
  152. struct ngui_scroll *s);
  153. void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
  154. struct ngui_scroll *s);
  155. void ngui_scroll_to(struct ngui_scroll *s, int offset);
  156. void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h);
  157. int ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s);
  158. #endif