ngui.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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_MOTION_REDRAW_MS 33
  12. #define NGUI_INPUT_CHANGED 1
  13. #define NGUI_INPUT_SUBMIT 2
  14. #define NGUI_INPUT_CANCEL 4
  15. /* Key codes from io.c. Arrows are 0x100..; the page/home/end keys are
  16. * produced by the extended PS/2 path. */
  17. #define NGUI_KEY_ESC 0x1B
  18. #define NGUI_KEY_BACKSPACE 0x08
  19. #define NGUI_KEY_TAB 0x09
  20. #define NGUI_KEY_LEFT 0x100
  21. #define NGUI_KEY_RIGHT 0x101
  22. #define NGUI_KEY_UP 0x102
  23. #define NGUI_KEY_DOWN 0x103
  24. #define NGUI_KEY_PAGE_UP 0x104
  25. #define NGUI_KEY_PAGE_DOWN 0x105
  26. #define NGUI_KEY_HOME 0x106
  27. #define NGUI_KEY_END 0x107
  28. #define NGUI_KEY_DELETE 0x108
  29. #define NGUI_KEY_CTRL_LEFT 0x109
  30. #define NGUI_KEY_CTRL_RIGHT 0x10A
  31. #define NGUI_KEY_CTRL_DELETE 0x10B
  32. #define NGUI_KEY_CTRL_BACKSPACE 0x10C
  33. #define NGUI_RGB(r, g, b) \
  34. ((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3)))
  35. struct ufont_face;
  36. struct ngui_rect {
  37. int x, y, w, h;
  38. };
  39. struct ngui_theme {
  40. unsigned short bg;
  41. unsigned short panel;
  42. unsigned short panel_alt;
  43. unsigned short border;
  44. unsigned short text;
  45. unsigned short text_dim;
  46. unsigned short accent;
  47. unsigned short selection;
  48. unsigned short button;
  49. unsigned short button_hot;
  50. unsigned short button_down;
  51. unsigned short icon_folder;
  52. unsigned short icon_file;
  53. unsigned short icon_disk;
  54. unsigned short error;
  55. };
  56. extern const struct ngui_theme ngui_theme_dark;
  57. enum ngui_icon {
  58. NGUI_ICON_FOLDER,
  59. NGUI_ICON_FILE,
  60. NGUI_ICON_DISK,
  61. NGUI_ICON_UP,
  62. NGUI_ICON_HOME,
  63. NGUI_ICON_REFRESH,
  64. NGUI_ICON_PLUS,
  65. NGUI_ICON_CHEVRON_LEFT,
  66. NGUI_ICON_CHEVRON_RIGHT
  67. };
  68. struct ngui_input {
  69. char text[NGUI_INPUT_MAX];
  70. unsigned int len;
  71. unsigned int cursor;
  72. unsigned int scroll; /* first visible character */
  73. unsigned int anchor; /* mouse selection anchor (== cursor: none) */
  74. int dragging; /* a press in the field is extending a selection */
  75. int disabled; /* ignore input and paint the value dimmed */
  76. const char *placeholder;
  77. unsigned int last_click_ms; /* previous press, for double-click select-all */
  78. };
  79. struct ngui_scroll {
  80. int offset; /* pixels scrolled from the top */
  81. int content_h;
  82. int view_h;
  83. int dragging;
  84. int drag_dy;
  85. };
  86. struct ngui {
  87. unsigned short *px;
  88. int w, h;
  89. struct ngui_theme theme;
  90. /* Fonts are owned by ufont.c's heap arena. ngui_font_attach() opens
  91. * them; ngui_font_detach() frees the arena (the browser calls it before
  92. * heap_reset()). NULL means the 8x16 bitmap fallback. */
  93. struct ufont_face *font;
  94. struct ufont_face *font_bold;
  95. struct ufont_face *font_italic;
  96. struct ufont_face *font_bold_italic;
  97. struct ufont_face *font_mono;
  98. int font_px;
  99. /* Current pointer state, content-relative. */
  100. int mouse_x, mouse_y;
  101. unsigned int buttons;
  102. int inside;
  103. int focused;
  104. /* Scroll accumulated since the previous frame, in pixels. */
  105. int scroll_x, scroll_y;
  106. /* Press/release edges collected by ngui_begin(). */
  107. unsigned int pressed;
  108. unsigned int released;
  109. int press_x, press_y;
  110. int release_x, release_y;
  111. int clicked;
  112. int focus_claimed;
  113. unsigned int hot;
  114. unsigned int active;
  115. unsigned int focus;
  116. struct ngui_rect clip[4];
  117. int nclip;
  118. int dirty;
  119. unsigned int last_blink;
  120. int caret_on;
  121. int keys[32];
  122. unsigned int key_head, key_tail;
  123. int last_mouse_x, last_mouse_y;
  124. int last_inside, last_focused;
  125. /* Pointer motion is throttled: a redraw is needed for hover feedback,
  126. * but not for every 200 Hz mouse report. A pending motion is flushed
  127. * once 33 ms have passed, so the final hover state still appears. */
  128. unsigned int motion_ms;
  129. int motion_pending;
  130. };
  131. void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h);
  132. void ngui_font_attach(struct ngui *ui); /* opens /ramfs/fonts; no-op if absent */
  133. void ngui_font_detach(struct ngui *ui); /* frees the ufont arena */
  134. int ngui_begin(struct ngui *ui); /* 1 = app should redraw */
  135. void ngui_end(struct ngui *ui);
  136. /* Same bookkeeping, but only publish the canvas when `present` is nonzero.
  137. * Lets an app poll input without copying an unchanged frame every loop. */
  138. void ngui_end_ex(struct ngui *ui, int present);
  139. void ngui_invalidate(struct ngui *ui);
  140. void ngui_set_focus(struct ngui *ui, unsigned int id);
  141. unsigned int ngui_focus(struct ngui *ui);
  142. int ngui_key(struct ngui *ui);
  143. int ngui_key_peek(struct ngui *ui);
  144. /* Layout: cut a rectangle off one side of `r` and return the piece. */
  145. struct ngui_rect ngui_cut_top(struct ngui_rect *r, int h);
  146. struct ngui_rect ngui_cut_bottom(struct ngui_rect *r, int h);
  147. struct ngui_rect ngui_cut_left(struct ngui_rect *r, int w);
  148. struct ngui_rect ngui_cut_right(struct ngui_rect *r, int w);
  149. void ngui_inset(struct ngui_rect *r, int n);
  150. int ngui_rect_contains(const struct ngui_rect *r, int x, int y);
  151. /* Primitives. All drawing is clipped to the current clip stack and canvas. */
  152. void ngui_push_clip(struct ngui *ui, struct ngui_rect r);
  153. void ngui_pop_clip(struct ngui *ui);
  154. void ngui_fill_rect(struct ngui *ui, struct ngui_rect r, unsigned short color);
  155. void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color);
  156. void ngui_fill_round_rect(struct ngui *ui, struct ngui_rect r,
  157. int tl, int tr, int br, int bl,
  158. unsigned short color);
  159. void ngui_frame_round(struct ngui *ui, struct ngui_rect r,
  160. int tl, int tr, int br, int bl, int thickness,
  161. unsigned short color);
  162. void ngui_line(struct ngui *ui, int x0, int y0, int x1, int y1,
  163. unsigned short color);
  164. void ngui_text(struct ngui *ui, int x, int y, const char *s,
  165. unsigned short color);
  166. void ngui_text_n(struct ngui *ui, int x, int y, const char *s,
  167. unsigned int n, unsigned short color);
  168. void ngui_text_styled(struct ngui *ui, int x, int y, const char *s,
  169. unsigned int n, unsigned short color, int scale, int bold);
  170. /* Browser-oriented style: arbitrary pixel size, optional italic and mono. */
  171. void ngui_text_px(struct ngui *ui, int x, int y, const char *s,
  172. unsigned int n, unsigned short color, int px, int bold,
  173. int italic, int mono);
  174. void ngui_text_rot180(struct ngui *ui, int x, int y, const char *s,
  175. unsigned int n, unsigned short color);
  176. void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
  177. const char *s, unsigned short color);
  178. unsigned int ngui_text_width(const char *s);
  179. unsigned int ngui_text_width_n(const char *s, unsigned int n, int scale);
  180. unsigned int ngui_text_width_ui(struct ngui *ui, const char *s);
  181. unsigned int ngui_text_width_px(struct ngui *ui, const char *s, unsigned int n,
  182. int px, int bold, int italic, int mono);
  183. int ngui_text_ascent(struct ngui *ui, int px, int mono);
  184. int ngui_text_line_height(struct ngui *ui, int px, int mono);
  185. /* Face-aware variants: a non-NULL `face` (an @font-face loaded by the
  186. * caller) overrides the built-in regular/bold/italic/mono selection. */
  187. unsigned int ngui_text_width_face(struct ngui *ui, struct ufont_face *face,
  188. const char *s, unsigned int n, int px,
  189. int bold, int italic, int mono);
  190. int ngui_text_ascent_face(struct ngui *ui, struct ufont_face *face, int px,
  191. int mono);
  192. int ngui_text_line_height_face(struct ngui *ui, struct ufont_face *face,
  193. int px, int mono);
  194. void ngui_text_px_face(struct ngui *ui, struct ufont_face *face, int x,
  195. int y, const char *s, unsigned int n,
  196. unsigned short color, int px, int bold, int italic,
  197. int mono);
  198. void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
  199. unsigned short color);
  200. /* Widgets. IDs must be stable across frames. */
  201. int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  202. const char *label);
  203. int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
  204. const char *label, const char *shortcut, int checked);
  205. int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  206. enum ngui_icon icon);
  207. int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
  208. struct ngui_input *in);
  209. void ngui_input_set(struct ngui_input *in, const char *s);
  210. /* Scrolling pane: call begin, draw the content with scroll->offset, end. */
  211. void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
  212. struct ngui_scroll *s);
  213. void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
  214. struct ngui_scroll *s);
  215. /* Apply pending wheel/touchpad scroll to s when the pointer is over r and
  216. * consume it. Call after ngui_scroll_begin() set view_h/content_h. */
  217. int ngui_scroll_wheel(struct ngui *ui, struct ngui_rect r,
  218. struct ngui_scroll *s);
  219. void ngui_scroll_to(struct ngui_scroll *s, int offset);
  220. void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h);
  221. int ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s);
  222. #endif