ngui.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_KEY_CTRL_LEFT 0x109
  29. #define NGUI_KEY_CTRL_RIGHT 0x10A
  30. #define NGUI_KEY_CTRL_DELETE 0x10B
  31. #define NGUI_KEY_CTRL_BACKSPACE 0x10C
  32. #define NGUI_RGB(r, g, b) \
  33. ((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3)))
  34. struct ngui_rect {
  35. int x, y, w, h;
  36. };
  37. struct ngui_theme {
  38. unsigned short bg;
  39. unsigned short panel;
  40. unsigned short panel_alt;
  41. unsigned short border;
  42. unsigned short text;
  43. unsigned short text_dim;
  44. unsigned short accent;
  45. unsigned short selection;
  46. unsigned short button;
  47. unsigned short button_hot;
  48. unsigned short button_down;
  49. unsigned short icon_folder;
  50. unsigned short icon_file;
  51. unsigned short icon_disk;
  52. unsigned short error;
  53. };
  54. extern const struct ngui_theme ngui_theme_dark;
  55. enum ngui_icon {
  56. NGUI_ICON_FOLDER,
  57. NGUI_ICON_FILE,
  58. NGUI_ICON_DISK,
  59. NGUI_ICON_UP,
  60. NGUI_ICON_HOME,
  61. NGUI_ICON_REFRESH,
  62. NGUI_ICON_PLUS,
  63. NGUI_ICON_CHEVRON_LEFT,
  64. NGUI_ICON_CHEVRON_RIGHT
  65. };
  66. struct ngui_input {
  67. char text[NGUI_INPUT_MAX];
  68. unsigned int len;
  69. unsigned int cursor;
  70. unsigned int scroll; /* first visible character */
  71. unsigned int anchor; /* mouse selection anchor (== cursor: none) */
  72. int dragging; /* a press in the field is extending a selection */
  73. const char *placeholder;
  74. };
  75. struct ngui_scroll {
  76. int offset; /* pixels scrolled from the top */
  77. int content_h;
  78. int view_h;
  79. int dragging;
  80. int drag_dy;
  81. };
  82. struct ngui {
  83. unsigned short *px;
  84. int w, h;
  85. struct ngui_theme theme;
  86. /* Current pointer state, content-relative. */
  87. int mouse_x, mouse_y;
  88. unsigned int buttons;
  89. int inside;
  90. int focused;
  91. /* Press/release edges collected by ngui_begin(). */
  92. unsigned int pressed;
  93. unsigned int released;
  94. int press_x, press_y;
  95. int release_x, release_y;
  96. int clicked;
  97. int focus_claimed;
  98. unsigned int hot;
  99. unsigned int active;
  100. unsigned int focus;
  101. struct ngui_rect clip[4];
  102. int nclip;
  103. int dirty;
  104. unsigned int last_blink;
  105. int caret_on;
  106. int keys[32];
  107. unsigned int key_head, key_tail;
  108. int last_mouse_x, last_mouse_y;
  109. int last_inside, last_focused;
  110. };
  111. void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h);
  112. int ngui_begin(struct ngui *ui); /* 1 = app should redraw */
  113. void ngui_end(struct ngui *ui);
  114. /* Same bookkeeping, but only publish the canvas when `present` is nonzero.
  115. * Lets an app poll input without copying an unchanged frame every loop. */
  116. void ngui_end_ex(struct ngui *ui, int present);
  117. void ngui_invalidate(struct ngui *ui);
  118. void ngui_set_focus(struct ngui *ui, unsigned int id);
  119. unsigned int ngui_focus(struct ngui *ui);
  120. int ngui_key(struct ngui *ui);
  121. int ngui_key_peek(struct ngui *ui);
  122. /* Layout: cut a rectangle off one side of `r` and return the piece. */
  123. struct ngui_rect ngui_cut_top(struct ngui_rect *r, int h);
  124. struct ngui_rect ngui_cut_bottom(struct ngui_rect *r, int h);
  125. struct ngui_rect ngui_cut_left(struct ngui_rect *r, int w);
  126. struct ngui_rect ngui_cut_right(struct ngui_rect *r, int w);
  127. void ngui_inset(struct ngui_rect *r, int n);
  128. int ngui_rect_contains(const struct ngui_rect *r, int x, int y);
  129. /* Primitives. All drawing is clipped to the current clip stack and canvas. */
  130. void ngui_push_clip(struct ngui *ui, struct ngui_rect r);
  131. void ngui_pop_clip(struct ngui *ui);
  132. void ngui_fill_rect(struct ngui *ui, struct ngui_rect r, unsigned short color);
  133. void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color);
  134. void ngui_line(struct ngui *ui, int x0, int y0, int x1, int y1,
  135. unsigned short color);
  136. void ngui_text(struct ngui *ui, int x, int y, const char *s,
  137. unsigned short color);
  138. void ngui_text_n(struct ngui *ui, int x, int y, const char *s,
  139. unsigned int n, unsigned short color);
  140. void ngui_text_styled(struct ngui *ui, int x, int y, const char *s,
  141. unsigned int n, unsigned short color, int scale, int bold);
  142. void ngui_text_rot180(struct ngui *ui, int x, int y, const char *s,
  143. unsigned int n, unsigned short color);
  144. void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
  145. const char *s, unsigned short color);
  146. unsigned int ngui_text_width(const char *s);
  147. unsigned int ngui_text_width_n(const char *s, unsigned int n, int scale);
  148. void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
  149. unsigned short color);
  150. /* Widgets. IDs must be stable across frames. */
  151. int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  152. const char *label);
  153. int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
  154. const char *label, const char *shortcut, int checked);
  155. int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  156. enum ngui_icon icon);
  157. int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
  158. struct ngui_input *in);
  159. void ngui_input_set(struct ngui_input *in, const char *s);
  160. /* Scrolling pane: call begin, draw the content with scroll->offset, end. */
  161. void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
  162. struct ngui_scroll *s);
  163. void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
  164. struct ngui_scroll *s);
  165. void ngui_scroll_to(struct ngui_scroll *s, int offset);
  166. void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h);
  167. int ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s);
  168. #endif