ngui.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  1. /* ngui: immediate-mode widgets for RGB565 canvas windows. See ngui.h. */
  2. #include "ngui.h"
  3. #include "ucmd_lib.h"
  4. #include "font_8x16.h"
  5. const struct ngui_theme ngui_theme_dark = {
  6. .bg = NGUI_RGB(24, 28, 40),
  7. .panel = NGUI_RGB(38, 43, 58),
  8. .panel_alt = NGUI_RGB(50, 56, 74),
  9. .border = NGUI_RGB(92, 99, 124),
  10. .text = NGUI_RGB(228, 230, 240),
  11. .text_dim = NGUI_RGB(145, 151, 170),
  12. .accent = NGUI_RGB(84, 132, 224),
  13. .selection = NGUI_RGB(54, 92, 166),
  14. .button = NGUI_RGB(54, 61, 80),
  15. .button_hot = NGUI_RGB(70, 81, 108),
  16. .button_down = NGUI_RGB(48, 112, 204),
  17. .icon_folder = NGUI_RGB(238, 198, 88),
  18. .icon_file = NGUI_RGB(204, 210, 226),
  19. .icon_disk = NGUI_RGB(112, 174, 232),
  20. .error = NGUI_RGB(238, 116, 116),
  21. };
  22. static unsigned int slen(const char *s)
  23. {
  24. unsigned int n = 0;
  25. while (s && s[n])
  26. n++;
  27. return n;
  28. }
  29. static void scopy(char *dst, const char *src, unsigned int cap)
  30. {
  31. unsigned int i = 0;
  32. if (!cap)
  33. return;
  34. while (src && src[i] && i + 1 < cap) {
  35. dst[i] = src[i];
  36. i++;
  37. }
  38. dst[i] = '\0';
  39. }
  40. static int rect_empty(const struct ngui_rect *r)
  41. {
  42. return r->w <= 0 || r->h <= 0;
  43. }
  44. static int rect_intersect(const struct ngui_rect *a, const struct ngui_rect *b,
  45. struct ngui_rect *out)
  46. {
  47. int x0 = a->x > b->x ? a->x : b->x;
  48. int y0 = a->y > b->y ? a->y : b->y;
  49. int x1 = a->x + a->w < b->x + b->w ? a->x + a->w : b->x + b->w;
  50. int y1 = a->y + a->h < b->y + b->h ? a->y + a->h : b->y + b->h;
  51. out->x = x0;
  52. out->y = y0;
  53. out->w = x1 - x0;
  54. out->h = y1 - y0;
  55. return !rect_empty(out);
  56. }
  57. static struct ngui_rect rect_make(int x, int y, int w, int h)
  58. {
  59. struct ngui_rect r;
  60. r.x = x;
  61. r.y = y;
  62. r.w = w;
  63. r.h = h;
  64. return r;
  65. }
  66. int ngui_rect_contains(const struct ngui_rect *r, int x, int y)
  67. {
  68. return x >= r->x && y >= r->y && x < r->x + r->w && y < r->y + r->h;
  69. }
  70. void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h)
  71. {
  72. unsigned int i;
  73. for (i = 0; i < sizeof(*ui); i++)
  74. ((unsigned char *)ui)[i] = 0;
  75. ui->px = fb;
  76. ui->w = w;
  77. ui->h = h;
  78. ui->theme = ngui_theme_dark;
  79. ui->nclip = 0;
  80. ui->dirty = 1;
  81. ui->caret_on = 1;
  82. ui->last_blink = u_ticks_ms();
  83. ui->motion_ms = ui->last_blink;
  84. ui->last_mouse_x = -1;
  85. ui->last_mouse_y = -1;
  86. }
  87. void ngui_invalidate(struct ngui *ui)
  88. {
  89. ui->dirty = 1;
  90. }
  91. void ngui_set_focus(struct ngui *ui, unsigned int id)
  92. {
  93. if (ui->focus != id) {
  94. ui->focus = id;
  95. ui->last_blink = u_ticks_ms();
  96. ui->caret_on = 1;
  97. ui->dirty = 1;
  98. }
  99. }
  100. unsigned int ngui_focus(struct ngui *ui)
  101. {
  102. return ui->focus;
  103. }
  104. static void key_push(struct ngui *ui, int k)
  105. {
  106. unsigned int next = (ui->key_head + 1) % 32;
  107. if (next == ui->key_tail)
  108. return;
  109. ui->keys[ui->key_head] = k;
  110. ui->key_head = next;
  111. }
  112. int ngui_key_peek(struct ngui *ui)
  113. {
  114. if (ui->key_tail == ui->key_head)
  115. return 0;
  116. return ui->keys[ui->key_tail];
  117. }
  118. int ngui_key(struct ngui *ui)
  119. {
  120. int k = ngui_key_peek(ui);
  121. if (!k)
  122. return 0;
  123. ui->key_tail = (ui->key_tail + 1) % 32;
  124. return k;
  125. }
  126. int ngui_begin(struct ngui *ui)
  127. {
  128. struct win_info wi;
  129. struct win_pointer wp;
  130. struct win_event ev;
  131. unsigned int now;
  132. int want = ui->dirty;
  133. int i, k;
  134. ui->dirty = 0;
  135. ui->pressed = ui->released = 0;
  136. ui->clicked = 0;
  137. ui->focus_claimed = 0;
  138. ui->hot = 0;
  139. if (u_win_info(&wi) == 0) {
  140. if (wi.width != (unsigned int)ui->w ||
  141. wi.height != (unsigned int)ui->h) {
  142. if (u_win_ack_resize() == 0) {
  143. ui->px = (unsigned short *)wi.fb;
  144. ui->w = (int)wi.width;
  145. ui->h = (int)wi.height;
  146. }
  147. want = 1;
  148. } else if (wi.fb && wi.fb != (void *)ui->px) {
  149. ui->px = (unsigned short *)wi.fb;
  150. }
  151. }
  152. if (u_win_pointer(&wp) == 0) {
  153. ui->mouse_x = wp.x;
  154. ui->mouse_y = wp.y;
  155. ui->buttons = wp.buttons;
  156. ui->inside = wp.inside ? 1 : 0;
  157. ui->focused = wp.focused ? 1 : 0;
  158. } else {
  159. ui->mouse_x = ui->mouse_y = 0;
  160. ui->buttons = 0;
  161. ui->inside = 0;
  162. ui->focused = 0;
  163. }
  164. for (i = 0; i < 16; i++) {
  165. int r = u_win_event(&ev);
  166. if (r != 1)
  167. break;
  168. if (ev.type == WIN_EV_POINTER_DOWN) {
  169. ui->pressed |= 1;
  170. ui->press_x = ev.x;
  171. ui->press_y = ev.y;
  172. ui->clicked = 1;
  173. } else if (ev.type == WIN_EV_POINTER_UP) {
  174. ui->released |= 1;
  175. ui->release_x = ev.x;
  176. ui->release_y = ev.y;
  177. }
  178. }
  179. for (i = 0; i < 32; i++) {
  180. k = u_key_poll();
  181. if (!k)
  182. break;
  183. key_push(ui, k);
  184. want = 1;
  185. }
  186. now = u_ticks_ms();
  187. if (ui->focus) {
  188. if (now - ui->last_blink >= 500) {
  189. ui->last_blink = now;
  190. ui->caret_on = !ui->caret_on;
  191. want = 1;
  192. }
  193. } else {
  194. ui->caret_on = 1;
  195. }
  196. if (ui->inside != ui->last_inside) {
  197. want = 1;
  198. ui->motion_pending = 0;
  199. ui->motion_ms = now;
  200. } else if (ui->inside &&
  201. (ui->mouse_x != ui->last_mouse_x ||
  202. ui->mouse_y != ui->last_mouse_y)) {
  203. /* Hover feedback needs a repaint, but not one per mouse
  204. * report: while the pointer keeps moving, redraw at most every
  205. * 33 ms and remember that a final frame is still due. A held
  206. * button is a drag, which stays at full rate. */
  207. if (ui->buttons) {
  208. ui->motion_ms = now;
  209. ui->motion_pending = 0;
  210. want = 1;
  211. } else {
  212. ui->motion_pending = 1;
  213. if ((unsigned int)(now - ui->motion_ms) >=
  214. NGUI_MOTION_REDRAW_MS) {
  215. ui->motion_ms = now;
  216. ui->motion_pending = 0;
  217. want = 1;
  218. }
  219. }
  220. } else if (ui->motion_pending &&
  221. (unsigned int)(now - ui->motion_ms) >= NGUI_MOTION_REDRAW_MS) {
  222. ui->motion_ms = now;
  223. ui->motion_pending = 0;
  224. want = 1;
  225. }
  226. if (ui->focused != ui->last_focused)
  227. want = 1;
  228. ui->last_mouse_x = ui->mouse_x;
  229. ui->last_mouse_y = ui->mouse_y;
  230. ui->last_inside = ui->inside;
  231. ui->last_focused = ui->focused;
  232. if (ui->pressed || ui->released)
  233. want = 1;
  234. if (want)
  235. ui->motion_pending = 0;
  236. return want;
  237. }
  238. void ngui_end_ex(struct ngui *ui, int present)
  239. {
  240. if (present && u_win_present() != 0)
  241. ui->dirty = 1;
  242. if (ui->clicked && !ui->focus_claimed)
  243. ui->focus = 0;
  244. /* Only the release edge ends a press. The sampled button state is not
  245. * synchronized with the event queue: it can read 0 for a frame before
  246. * the release event is delivered. Clearing active on "!buttons" made
  247. * clicks fire only when the two happened to land in the same frame. */
  248. if (ui->released)
  249. ui->active = 0;
  250. ui->pressed = ui->released = 0;
  251. ui->clicked = 0;
  252. ui->focus_claimed = 0;
  253. ui->press_x = ui->press_y = 0;
  254. ui->release_x = ui->release_y = 0;
  255. }
  256. void ngui_end(struct ngui *ui)
  257. {
  258. ngui_end_ex(ui, 1);
  259. }
  260. struct ngui_rect ngui_cut_top(struct ngui_rect *r, int h)
  261. {
  262. struct ngui_rect out = *r;
  263. if (h < 0)
  264. h = 0;
  265. if (h > r->h)
  266. h = r->h;
  267. out.h = h;
  268. r->y += h;
  269. r->h -= h;
  270. return out;
  271. }
  272. struct ngui_rect ngui_cut_bottom(struct ngui_rect *r, int h)
  273. {
  274. struct ngui_rect out = *r;
  275. if (h < 0)
  276. h = 0;
  277. if (h > r->h)
  278. h = r->h;
  279. out.y = r->y + r->h - h;
  280. out.h = h;
  281. r->h -= h;
  282. return out;
  283. }
  284. struct ngui_rect ngui_cut_left(struct ngui_rect *r, int w)
  285. {
  286. struct ngui_rect out = *r;
  287. if (w < 0)
  288. w = 0;
  289. if (w > r->w)
  290. w = r->w;
  291. out.w = w;
  292. r->x += w;
  293. r->w -= w;
  294. return out;
  295. }
  296. struct ngui_rect ngui_cut_right(struct ngui_rect *r, int w)
  297. {
  298. struct ngui_rect out = *r;
  299. if (w < 0)
  300. w = 0;
  301. if (w > r->w)
  302. w = r->w;
  303. out.x = r->x + r->w - w;
  304. out.w = w;
  305. r->w -= w;
  306. return out;
  307. }
  308. void ngui_inset(struct ngui_rect *r, int n)
  309. {
  310. r->x += n;
  311. r->y += n;
  312. r->w -= n * 2;
  313. r->h -= n * 2;
  314. if (r->w < 0)
  315. r->w = 0;
  316. if (r->h < 0)
  317. r->h = 0;
  318. }
  319. void ngui_push_clip(struct ngui *ui, struct ngui_rect r)
  320. {
  321. struct ngui_rect out;
  322. struct ngui_rect canvas;
  323. if (ui->nclip >= 4)
  324. return;
  325. canvas = rect_make(0, 0, ui->w, ui->h);
  326. if (!rect_intersect(&r, &canvas, &out))
  327. out = rect_make(0, 0, 0, 0);
  328. if (ui->nclip > 0) {
  329. struct ngui_rect clipped = out;
  330. if (!rect_intersect(&clipped, &ui->clip[ui->nclip - 1], &out))
  331. out = rect_make(0, 0, 0, 0);
  332. }
  333. ui->clip[ui->nclip++] = out;
  334. }
  335. void ngui_pop_clip(struct ngui *ui)
  336. {
  337. if (ui->nclip > 0)
  338. ui->nclip--;
  339. }
  340. static int pixel_visible(struct ngui *ui, int x, int y)
  341. {
  342. struct ngui_rect *c;
  343. if (x < 0 || y < 0 || x >= ui->w || y >= ui->h)
  344. return 0;
  345. if (ui->nclip <= 0)
  346. return 1;
  347. c = &ui->clip[ui->nclip - 1];
  348. return x >= c->x && y >= c->y &&
  349. x < c->x + c->w && y < c->y + c->h;
  350. }
  351. void ngui_fill_rect(struct ngui *ui, struct ngui_rect r, unsigned short color)
  352. {
  353. struct ngui_rect canvas;
  354. int x, y;
  355. if (rect_empty(&r))
  356. return;
  357. canvas = rect_make(0, 0, ui->w, ui->h);
  358. if (!rect_intersect(&r, &canvas, &r))
  359. return;
  360. if (ui->nclip > 0 &&
  361. !rect_intersect(&r, &ui->clip[ui->nclip - 1], &r))
  362. return;
  363. for (y = r.y; y < r.y + r.h; y++) {
  364. unsigned short *p = ui->px + y * ui->w + r.x;
  365. for (x = 0; x < r.w; x++)
  366. p[x] = color;
  367. }
  368. }
  369. void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color)
  370. {
  371. if (r.w <= 0 || r.h <= 0)
  372. return;
  373. ngui_fill_rect(ui, rect_make(r.x, r.y, r.w, 1), color);
  374. ngui_fill_rect(ui, rect_make(r.x, r.y + r.h - 1, r.w, 1), color);
  375. if (r.h > 2) {
  376. ngui_fill_rect(ui, rect_make(r.x, r.y + 1, 1, r.h - 2), color);
  377. ngui_fill_rect(ui, rect_make(r.x + r.w - 1, r.y + 1, 1,
  378. r.h - 2), color);
  379. }
  380. }
  381. static void ngui_pixel(struct ngui *ui, int x, int y, unsigned short color)
  382. {
  383. if (pixel_visible(ui, x, y))
  384. ui->px[y * ui->w + x] = color;
  385. }
  386. void ngui_line(struct ngui *ui, int x0, int y0, int x1, int y1,
  387. unsigned short color)
  388. {
  389. int dx = x1 - x0;
  390. int dy = y1 - y0;
  391. int sx = dx < 0 ? -1 : 1;
  392. int sy = dy < 0 ? -1 : 1;
  393. int err, e2;
  394. if (dx < 0)
  395. dx = -dx;
  396. if (dy < 0)
  397. dy = -dy;
  398. dy = -dy;
  399. err = dx + dy;
  400. for (;;) {
  401. ngui_pixel(ui, x0, y0, color);
  402. if (x0 == x1 && y0 == y1)
  403. break;
  404. e2 = err * 2;
  405. if (e2 >= dy) {
  406. err += dy;
  407. x0 += sx;
  408. }
  409. if (e2 <= dx) {
  410. err += dx;
  411. y0 += sy;
  412. }
  413. }
  414. }
  415. static void ngui_glyph(struct ngui *ui, int x, int y, unsigned char c,
  416. unsigned short color, int scale, int bold)
  417. {
  418. const unsigned char *g = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
  419. int row, col, sx, sy;
  420. if (scale < 1)
  421. scale = 1;
  422. for (row = 0; row < FONT_8X16_ROWS; row++) {
  423. unsigned char bits = g[row];
  424. if (!bits)
  425. continue;
  426. for (col = 0; col < 8; col++) {
  427. if (!(bits & (0x80u >> col)))
  428. continue;
  429. for (sy = 0; sy < scale; sy++) {
  430. for (sx = 0; sx < scale; sx++) {
  431. ngui_pixel(ui,
  432. x + col * scale + sx +
  433. (bold && scale == 1 ? 1 : 0),
  434. y + row * scale + sy,
  435. color);
  436. }
  437. }
  438. if (bold && scale > 1)
  439. ngui_pixel(ui, x + col * scale + scale,
  440. y + row * scale, color);
  441. }
  442. }
  443. }
  444. unsigned int ngui_text_width(const char *s)
  445. {
  446. return slen(s) * 8;
  447. }
  448. unsigned int ngui_text_width_n(const char *s, unsigned int n, int scale)
  449. {
  450. (void)s;
  451. if (scale < 1)
  452. scale = 1;
  453. return n * 8u * (unsigned int)scale;
  454. }
  455. void ngui_text_n(struct ngui *ui, int x, int y, const char *s, unsigned int n,
  456. unsigned short color)
  457. {
  458. unsigned int i;
  459. for (i = 0; i < n && s && s[i]; i++)
  460. ngui_glyph(ui, x + (int)i * 8, y, (unsigned char)s[i], color, 1, 0);
  461. }
  462. void ngui_text(struct ngui *ui, int x, int y, const char *s,
  463. unsigned short color)
  464. {
  465. ngui_text_n(ui, x, y, s, slen(s), color);
  466. }
  467. void ngui_text_styled(struct ngui *ui, int x, int y, const char *s,
  468. unsigned int n, unsigned short color, int scale, int bold)
  469. {
  470. unsigned int i;
  471. if (scale < 1)
  472. scale = 1;
  473. for (i = 0; i < n && s && s[i]; i++)
  474. ngui_glyph(ui, x + (int)i * 8 * scale, y, (unsigned char)s[i],
  475. color, scale, bold);
  476. }
  477. /* Draw a string upside down. The whole n-character block is rotated, so the
  478. * first character ends up on the right; used for the mirrored corner index on
  479. * a playing card. */
  480. void ngui_text_rot180(struct ngui *ui, int x, int y, const char *s,
  481. unsigned int n, unsigned short color)
  482. {
  483. unsigned int i;
  484. int row, col;
  485. for (i = 0; i < n && s && s[i]; i++) {
  486. const unsigned char *g =
  487. &font_8x16[(unsigned int)(unsigned char)s[i] *
  488. FONT_8X16_ROWS];
  489. for (row = 0; row < FONT_8X16_ROWS; row++) {
  490. unsigned char bits = g[row];
  491. if (!bits)
  492. continue;
  493. for (col = 0; col < 8; col++) {
  494. if (!(bits & (0x80u >> col)))
  495. continue;
  496. ngui_pixel(ui,
  497. x + (int)(n - 1 - i) * 8 + 7 - col,
  498. y + FONT_8X16_ROWS - 1 - row,
  499. color);
  500. }
  501. }
  502. }
  503. }
  504. void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
  505. const char *s, unsigned short color)
  506. {
  507. unsigned int n = slen(s);
  508. int max = (r.x + r.w - x) / 8;
  509. unsigned int i;
  510. if (max <= 0)
  511. return;
  512. if (n <= (unsigned int)max || max < 4) {
  513. for (i = 0; i < n && (int)i < max; i++)
  514. ngui_glyph(ui, x + (int)i * 8, y,
  515. (unsigned char)s[i], color, 1, 0);
  516. return;
  517. }
  518. for (i = 0; i < (unsigned int)(max - 3); i++)
  519. ngui_glyph(ui, x + (int)i * 8, y, (unsigned char)s[i], color, 1, 0);
  520. ngui_glyph(ui, x + (max - 3) * 8, y, '.', color, 1, 0);
  521. ngui_glyph(ui, x + (max - 2) * 8, y, '.', color, 1, 0);
  522. ngui_glyph(ui, x + (max - 1) * 8, y, '.', color, 1, 0);
  523. }
  524. void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
  525. unsigned short color)
  526. {
  527. int x = r.x + r.w / 2 - 8;
  528. int y = r.y + r.h / 2 - 8;
  529. switch (icon) {
  530. case NGUI_ICON_FOLDER:
  531. ngui_fill_rect(ui, rect_make(x + 1, y + 3, 7, 4), color);
  532. ngui_fill_rect(ui, rect_make(x, y + 6, 16, 9), color);
  533. break;
  534. case NGUI_ICON_FILE:
  535. ngui_frame(ui, rect_make(x + 2, y, 12, 16), color);
  536. ngui_line(ui, x + 10, y, x + 13, y + 3, color);
  537. ngui_fill_rect(ui, rect_make(x + 4, y + 7, 8, 1), color);
  538. ngui_fill_rect(ui, rect_make(x + 4, y + 10, 8, 1), color);
  539. break;
  540. case NGUI_ICON_DISK:
  541. ngui_frame(ui, rect_make(x, y + 2, 16, 12), color);
  542. ngui_fill_rect(ui, rect_make(x + 2, y + 5, 12, 2), color);
  543. ngui_fill_rect(ui, rect_make(x + 11, y + 9, 3, 3), color);
  544. break;
  545. case NGUI_ICON_UP:
  546. ngui_line(ui, x + 8, y + 2, x + 3, y + 8, color);
  547. ngui_line(ui, x + 8, y + 2, x + 13, y + 8, color);
  548. ngui_fill_rect(ui, rect_make(x + 6, y + 7, 5, 7), color);
  549. break;
  550. case NGUI_ICON_HOME:
  551. ngui_line(ui, x + 2, y + 8, x + 8, y + 2, color);
  552. ngui_line(ui, x + 8, y + 2, x + 14, y + 8, color);
  553. ngui_frame(ui, rect_make(x + 3, y + 8, 10, 7), color);
  554. ngui_fill_rect(ui, rect_make(x + 7, y + 11, 3, 4), color);
  555. break;
  556. case NGUI_ICON_REFRESH:
  557. ngui_line(ui, x + 3, y + 8, x + 6, y + 4, color);
  558. ngui_line(ui, x + 6, y + 4, x + 10, y + 3, color);
  559. ngui_line(ui, x + 10, y + 3, x + 13, y + 6, color);
  560. ngui_line(ui, x + 13, y + 6, x + 13, y + 9, color);
  561. ngui_fill_rect(ui, rect_make(x + 11, y + 1, 4, 4), color);
  562. break;
  563. case NGUI_ICON_PLUS:
  564. ngui_fill_rect(ui, rect_make(x + 7, y + 3, 2, 10), color);
  565. ngui_fill_rect(ui, rect_make(x + 3, y + 7, 10, 2), color);
  566. break;
  567. case NGUI_ICON_CHEVRON_LEFT:
  568. ngui_line(ui, x + 10, y + 3, x + 6, y + 8, color);
  569. ngui_line(ui, x + 6, y + 8, x + 10, y + 13, color);
  570. break;
  571. case NGUI_ICON_CHEVRON_RIGHT:
  572. ngui_line(ui, x + 6, y + 3, x + 10, y + 8, color);
  573. ngui_line(ui, x + 10, y + 8, x + 6, y + 13, color);
  574. break;
  575. default:
  576. break;
  577. }
  578. }
  579. int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  580. const char *label)
  581. {
  582. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  583. int press_hot = ui->pressed &&
  584. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  585. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  586. int fired = 0;
  587. unsigned short bg;
  588. int tw, tx, ty;
  589. if (press_hot)
  590. ui->active = id;
  591. if (ui->pressed && ui->released && press_hot && release_hot)
  592. fired = 1;
  593. if (ui->released && ui->active == id) {
  594. if (release_hot)
  595. fired = 1;
  596. ui->active = 0;
  597. }
  598. if (ui->active == id)
  599. bg = ui->theme.button_down;
  600. else if (hot)
  601. bg = ui->theme.button_hot;
  602. else
  603. bg = ui->theme.button;
  604. ngui_fill_rect(ui, r, bg);
  605. ngui_frame(ui, r, hot ? ui->theme.accent : ui->theme.border);
  606. if (label && label[0]) {
  607. tw = (int)ngui_text_width(label);
  608. tx = r.x + (r.w - tw) / 2;
  609. ty = r.y + (r.h - 16) / 2;
  610. ngui_push_clip(ui, r);
  611. ngui_text(ui, tx, ty, label, ui->theme.text);
  612. ngui_pop_clip(ui);
  613. }
  614. if (hot)
  615. ui->hot = id;
  616. return fired;
  617. }
  618. /* One row of a drop-down menu: left-aligned label, optional check mark and
  619. * right-aligned shortcut. Uses the same press/release/active semantics as
  620. * ngui_button so a menu and its caller never disagree about a click. */
  621. int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
  622. const char *label, const char *shortcut, int checked)
  623. {
  624. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  625. int press_hot = ui->pressed &&
  626. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  627. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  628. int fired = 0;
  629. int selected;
  630. unsigned short bg, fg;
  631. int ty;
  632. if (press_hot)
  633. ui->active = id;
  634. if (ui->pressed && ui->released && press_hot && release_hot)
  635. fired = 1;
  636. if (ui->released && ui->active == id) {
  637. if (release_hot)
  638. fired = 1;
  639. ui->active = 0;
  640. }
  641. selected = (ui->active == id) || hot;
  642. bg = selected ? ui->theme.selection : ui->theme.panel;
  643. fg = selected ? NGUI_RGB(255, 255, 255) : ui->theme.text;
  644. ngui_fill_rect(ui, r, bg);
  645. ty = r.y + (r.h - 16) / 2;
  646. ngui_push_clip(ui, r);
  647. if (checked) {
  648. ngui_line(ui, r.x + 8, ty + 10, r.x + 10, ty + 13, fg);
  649. ngui_line(ui, r.x + 10, ty + 13, r.x + 15, ty + 5, fg);
  650. }
  651. if (label)
  652. ngui_text(ui, r.x + 22, ty, label, fg);
  653. if (shortcut) {
  654. int sw = (int)ngui_text_width(shortcut);
  655. ngui_text(ui, r.x + r.w - 8 - sw, ty, shortcut, fg);
  656. }
  657. ngui_pop_clip(ui);
  658. if (hot)
  659. ui->hot = id;
  660. return fired;
  661. }
  662. int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  663. enum ngui_icon icon){
  664. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  665. int press_hot = ui->pressed &&
  666. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  667. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  668. int fired = 0;
  669. unsigned short bg;
  670. if (press_hot)
  671. ui->active = id;
  672. if (ui->pressed && ui->released && press_hot && release_hot)
  673. fired = 1;
  674. if (ui->released && ui->active == id) {
  675. if (release_hot)
  676. fired = 1;
  677. ui->active = 0;
  678. }
  679. if (ui->active == id)
  680. bg = ui->theme.button_down;
  681. else if (hot)
  682. bg = ui->theme.button_hot;
  683. else
  684. bg = ui->theme.button;
  685. ngui_fill_rect(ui, r, bg);
  686. ngui_frame(ui, r, hot ? ui->theme.accent : ui->theme.border);
  687. ngui_icon(ui, r, icon, ui->theme.text);
  688. if (hot)
  689. ui->hot = id;
  690. return fired;
  691. }
  692. void ngui_input_set(struct ngui_input *in, const char *s)
  693. {
  694. scopy(in->text, s, NGUI_INPUT_MAX);
  695. in->len = slen(in->text);
  696. in->cursor = in->len;
  697. in->scroll = 0;
  698. in->anchor = in->cursor;
  699. in->dragging = 0;
  700. in->last_click_ms = 0;
  701. }
  702. static int input_word_char(char c)
  703. {
  704. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  705. (c >= '0' && c <= '9') || c == '_';
  706. }
  707. /* Next word start after `pos` (Windows edit-control semantics). */
  708. static unsigned int input_word_next(const struct ngui_input *in,
  709. unsigned int pos)
  710. {
  711. while (pos < in->len && input_word_char(in->text[pos]))
  712. pos++;
  713. while (pos < in->len && !input_word_char(in->text[pos]))
  714. pos++;
  715. return pos;
  716. }
  717. /* Start of the previous word before `pos`. */
  718. static unsigned int input_word_prev(const struct ngui_input *in,
  719. unsigned int pos)
  720. {
  721. while (pos > 0 && !input_word_char(in->text[pos - 1]))
  722. pos--;
  723. while (pos > 0 && input_word_char(in->text[pos - 1]))
  724. pos--;
  725. return pos;
  726. }
  727. /* End of the word to the right of `pos`. A caret inside a word takes the
  728. * rest of that word and its trailing separators; a caret on separators takes
  729. * them plus the next word. */
  730. static unsigned int input_word_delete_end(const struct ngui_input *in,
  731. unsigned int pos)
  732. {
  733. if (pos < in->len && input_word_char(in->text[pos])) {
  734. while (pos < in->len && input_word_char(in->text[pos]))
  735. pos++;
  736. while (pos < in->len && !input_word_char(in->text[pos]))
  737. pos++;
  738. return pos;
  739. }
  740. while (pos < in->len && !input_word_char(in->text[pos]))
  741. pos++;
  742. while (pos < in->len && input_word_char(in->text[pos]))
  743. pos++;
  744. return pos;
  745. }
  746. static void input_sel_bounds(const struct ngui_input *in,
  747. unsigned int *a, unsigned int *b)
  748. {
  749. if (in->anchor < in->cursor) {
  750. *a = in->anchor;
  751. *b = in->cursor;
  752. } else {
  753. *a = in->cursor;
  754. *b = in->anchor;
  755. }
  756. }
  757. /* Remove [a, b) and keep cursor/anchor consistent. Returns 1 when the text
  758. * changed. */
  759. static int input_delete_range(struct ngui_input *in, unsigned int a,
  760. unsigned int b)
  761. {
  762. unsigned int i;
  763. if (a >= b || b > in->len)
  764. return 0;
  765. for (i = b; i < in->len; i++)
  766. in->text[i - (b - a)] = in->text[i];
  767. in->len -= b - a;
  768. in->text[in->len] = '\0';
  769. if (in->cursor >= b)
  770. in->cursor -= b - a;
  771. else if (in->cursor > a)
  772. in->cursor = a;
  773. if (in->anchor >= b)
  774. in->anchor -= b - a;
  775. else if (in->anchor > a)
  776. in->anchor = a;
  777. return 1;
  778. }
  779. static int input_delete_selection(struct ngui_input *in)
  780. {
  781. unsigned int a, b;
  782. input_sel_bounds(in, &a, &b);
  783. return input_delete_range(in, a, b);
  784. }
  785. static void input_insert(struct ngui_input *in, char c)
  786. {
  787. unsigned int i;
  788. input_delete_selection(in);
  789. if (in->len + 1 >= NGUI_INPUT_MAX)
  790. return;
  791. i = in->len;
  792. while (i > in->cursor) {
  793. in->text[i] = in->text[i - 1];
  794. i--;
  795. }
  796. in->text[in->cursor] = c;
  797. in->cursor++;
  798. in->len++;
  799. in->text[in->len] = '\0';
  800. in->anchor = in->cursor;
  801. }
  802. static void input_delete(struct ngui_input *in, int back)
  803. {
  804. unsigned int pos;
  805. if (back) {
  806. if (in->cursor == 0)
  807. return;
  808. pos = in->cursor - 1;
  809. } else {
  810. if (in->cursor >= in->len)
  811. return;
  812. pos = in->cursor;
  813. }
  814. input_delete_range(in, pos, pos + 1);
  815. }
  816. static void input_move_to(struct ngui_input *in, unsigned int pos)
  817. {
  818. if (pos > in->len)
  819. pos = in->len;
  820. in->cursor = pos;
  821. in->anchor = pos;
  822. }
  823. /* Two presses in the same field within this window select all of its text. */
  824. #define NGUI_DOUBLE_CLICK_MS 500
  825. /* Character position under a content-relative x, clamped to the visible
  826. * part of the field. */
  827. static unsigned int input_pos_at(const struct ngui_input *in, int inner_x,
  828. int cols, int x)
  829. {
  830. int col = (x - inner_x) / 8;
  831. unsigned int pos;
  832. if (col < 0)
  833. col = 0;
  834. if (cols > 0 && col > cols)
  835. col = cols;
  836. pos = in->scroll + (unsigned int)col;
  837. if (pos > in->len)
  838. pos = in->len;
  839. return pos;
  840. }
  841. static void input_draw_selection(struct ngui *ui, const struct ngui_input *in,
  842. struct ngui_rect inner, int cols)
  843. {
  844. unsigned int a, b, va, vb;
  845. input_sel_bounds(in, &a, &b);
  846. if (a == b || cols <= 0)
  847. return;
  848. va = a < in->scroll ? in->scroll : a;
  849. vb = b > in->scroll + (unsigned int)cols
  850. ? in->scroll + (unsigned int)cols : b;
  851. if (va < vb)
  852. ngui_fill_rect(ui, rect_make(inner.x + (int)(va - in->scroll) * 8,
  853. inner.y, (int)(vb - va) * 8, 16),
  854. ui->theme.selection);
  855. }
  856. int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
  857. struct ngui_input *in)
  858. {
  859. struct ngui_rect inner;
  860. int focused = (ui->focus == id);
  861. int result = 0;
  862. int cols, k;
  863. int changed = 0;
  864. inner = rect_make(r.x + 4, r.y + (r.h - 16) / 2, r.w - 8, 16);
  865. if (inner.w < 0)
  866. inner.w = 0;
  867. cols = inner.w / 8;
  868. if (cols < 0)
  869. cols = 0;
  870. if (ui->pressed &&
  871. ngui_rect_contains(&r, ui->press_x, ui->press_y)) {
  872. unsigned int now = u_ticks_ms();
  873. int dbl = in->last_click_ms &&
  874. (unsigned int)(now - in->last_click_ms) <=
  875. NGUI_DOUBLE_CLICK_MS;
  876. if (dbl) {
  877. /* Double click: select the whole text and keep it
  878. * selected while the second press is held. */
  879. in->anchor = 0;
  880. in->cursor = in->len;
  881. in->dragging = 0;
  882. } else {
  883. unsigned int pos = input_pos_at(in, inner.x, cols,
  884. ui->press_x);
  885. in->cursor = pos;
  886. in->anchor = pos;
  887. in->dragging = 1;
  888. }
  889. in->last_click_ms = now;
  890. ui->focus = id;
  891. ui->focus_claimed = 1;
  892. ui->last_blink = now;
  893. ui->caret_on = 1;
  894. focused = 1;
  895. } else if (ui->pressed) {
  896. in->dragging = 0;
  897. }
  898. /* Extend the selection while the button that started in this field is
  899. * still held. Sampled coordinates may be outside the canvas. The
  900. * release *event* is authoritative: the sampled button state can lag
  901. * the event queue by a frame. */
  902. if (in->dragging && (ui->buttons & 1) && !ui->released) {
  903. in->cursor = input_pos_at(in, inner.x, cols, ui->mouse_x);
  904. /* Keep redrawing when the pointer leaves the window. */
  905. ngui_invalidate(ui);
  906. }
  907. if (ui->released)
  908. in->dragging = 0;
  909. if (focused) {
  910. while ((k = ngui_key(ui)) != 0) {
  911. if (k == '\n' || k == '\r') {
  912. result |= NGUI_INPUT_SUBMIT;
  913. break;
  914. }
  915. if (k == NGUI_KEY_ESC || k == NGUI_KEY_TAB) {
  916. result |= NGUI_INPUT_CANCEL;
  917. ui->focus = 0;
  918. break;
  919. }
  920. if (k == NGUI_KEY_BACKSPACE || k == 0x7F) {
  921. if (!input_delete_selection(in))
  922. input_delete(in, 1);
  923. changed = 1;
  924. } else if (k == NGUI_KEY_DELETE) {
  925. if (!input_delete_selection(in))
  926. input_delete(in, 0);
  927. changed = 1;
  928. } else if (k == NGUI_KEY_CTRL_BACKSPACE) {
  929. if (input_delete_selection(in))
  930. changed = 1;
  931. else
  932. changed = input_delete_range(in,
  933. input_word_prev(in, in->cursor),
  934. in->cursor);
  935. } else if (k == NGUI_KEY_CTRL_DELETE) {
  936. if (input_delete_selection(in)) {
  937. changed = 1;
  938. } else {
  939. unsigned int a = in->cursor;
  940. unsigned int b = input_word_delete_end(in,
  941. a);
  942. /* At the end of the line, delete the word
  943. * before the caret instead of doing
  944. * nothing. */
  945. if (b == a) {
  946. a = input_word_prev(in, in->cursor);
  947. b = in->cursor;
  948. }
  949. changed = input_delete_range(in, a, b);
  950. }
  951. } else if (k == NGUI_KEY_LEFT) {
  952. input_move_to(in, in->cursor > 0 ? in->cursor - 1 : 0);
  953. } else if (k == NGUI_KEY_RIGHT) {
  954. input_move_to(in, in->cursor < in->len
  955. ? in->cursor + 1 : in->len);
  956. } else if (k == NGUI_KEY_CTRL_LEFT) {
  957. input_move_to(in, input_word_prev(in, in->cursor));
  958. } else if (k == NGUI_KEY_CTRL_RIGHT) {
  959. input_move_to(in, input_word_next(in, in->cursor));
  960. } else if (k == NGUI_KEY_HOME) {
  961. input_move_to(in, 0);
  962. } else if (k == NGUI_KEY_END) {
  963. input_move_to(in, in->len);
  964. } else if (k == 0x01) { /* Ctrl+A: select all */
  965. in->anchor = 0;
  966. in->cursor = in->len;
  967. } else if (k == 0x15) { /* Ctrl+U */
  968. in->len = 0;
  969. in->cursor = 0;
  970. in->anchor = 0;
  971. in->text[0] = '\0';
  972. changed = 1;
  973. } else if (k >= 0x20 && k <= 0x7E) {
  974. input_insert(in, (char)k);
  975. changed = 1;
  976. }
  977. }
  978. }
  979. ngui_fill_rect(ui, r, focused ? ui->theme.panel_alt : ui->theme.panel);
  980. ngui_frame(ui, r, focused ? ui->theme.accent : ui->theme.border);
  981. if (in->scroll > in->len)
  982. in->scroll = in->len;
  983. if (in->cursor < in->scroll)
  984. in->scroll = in->cursor;
  985. if (cols > 0 && in->cursor >= in->scroll + (unsigned int)cols)
  986. in->scroll = in->cursor - (unsigned int)cols + 1;
  987. if (in->scroll > in->len)
  988. in->scroll = in->len;
  989. ngui_push_clip(ui, inner);
  990. if (in->len == 0 && in->placeholder) {
  991. ngui_text(ui, inner.x, inner.y, in->placeholder,
  992. ui->theme.text_dim);
  993. } else {
  994. if (focused)
  995. input_draw_selection(ui, in, inner, cols);
  996. ngui_text(ui, inner.x, inner.y, in->text + in->scroll,
  997. ui->theme.text);
  998. }
  999. if (focused && ui->caret_on) {
  1000. int col = (int)in->cursor - (int)in->scroll;
  1001. if (col >= 0 && col <= cols)
  1002. ngui_fill_rect(ui, rect_make(inner.x + col * 8, inner.y,
  1003. 1, 16), ui->theme.accent);
  1004. }
  1005. ngui_pop_clip(ui);
  1006. if (changed)
  1007. result |= NGUI_INPUT_CHANGED;
  1008. return result;
  1009. }
  1010. static int scrollable(const struct ngui_scroll *s)
  1011. {
  1012. return s->content_h > s->view_h && s->view_h > 0;
  1013. }
  1014. static int scroll_thumb_h(const struct ngui_rect *r,
  1015. const struct ngui_scroll *s)
  1016. {
  1017. int h = 0;
  1018. if (s->content_h > 0)
  1019. h = r->h * s->view_h / s->content_h;
  1020. if (h < 12)
  1021. h = 12;
  1022. if (h > r->h)
  1023. h = r->h;
  1024. return h;
  1025. }
  1026. static int scroll_thumb_y(const struct ngui_rect *r,
  1027. const struct ngui_scroll *s)
  1028. {
  1029. int th = scroll_thumb_h(r, s);
  1030. int avail = r->h - th;
  1031. int max = s->content_h - s->view_h;
  1032. if (avail <= 0 || max <= 0)
  1033. return r->y;
  1034. return r->y + s->offset * avail / max;
  1035. }
  1036. static void scroll_clamp(struct ngui_scroll *s)
  1037. {
  1038. int max = s->content_h - s->view_h;
  1039. if (max < 0)
  1040. max = 0;
  1041. if (s->offset > max)
  1042. s->offset = max;
  1043. if (s->offset < 0)
  1044. s->offset = 0;
  1045. }
  1046. void ngui_scroll_to(struct ngui_scroll *s, int offset)
  1047. {
  1048. s->offset = offset;
  1049. scroll_clamp(s);
  1050. }
  1051. void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h)
  1052. {
  1053. if (y < s->offset)
  1054. s->offset = y;
  1055. else if (y + h > s->offset + s->view_h)
  1056. s->offset = y + h - s->view_h;
  1057. scroll_clamp(s);
  1058. }
  1059. void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
  1060. struct ngui_scroll *s)
  1061. {
  1062. struct ngui_rect bar;
  1063. int scrolls = scrollable(s);
  1064. s->view_h = r.h;
  1065. scroll_clamp(s);
  1066. if (!ui->buttons)
  1067. s->dragging = 0;
  1068. if (scrolls) {
  1069. int th = scroll_thumb_h(&r, s);
  1070. int ty = scroll_thumb_y(&r, s);
  1071. bar = rect_make(r.x + r.w - 15, r.y, 14, r.h);
  1072. if (ui->pressed &&
  1073. ngui_rect_contains(&bar, ui->press_x, ui->press_y)) {
  1074. if (ui->press_y >= ty && ui->press_y < ty + th) {
  1075. s->dragging = 1;
  1076. s->drag_dy = ui->press_y - ty;
  1077. } else {
  1078. int rel = ui->press_y - r.y - th / 2;
  1079. int avail = r.h - th;
  1080. int max = s->content_h - s->view_h;
  1081. if (rel < 0)
  1082. rel = 0;
  1083. if (rel > avail)
  1084. rel = avail;
  1085. if (avail > 0)
  1086. s->offset = rel * max / avail;
  1087. s->dragging = 1;
  1088. s->drag_dy = th / 2;
  1089. scroll_clamp(s);
  1090. }
  1091. }
  1092. /* Sampled buttons are authoritative while held: some event paths
  1093. * deliver the press late or coalesce it with the release. Start
  1094. * the drag from the current pointer position as well. */
  1095. if (!s->dragging && (ui->buttons & 1) &&
  1096. ngui_rect_contains(&bar, ui->mouse_x, ui->mouse_y)) {
  1097. if (ui->mouse_y >= ty && ui->mouse_y < ty + th) {
  1098. s->dragging = 1;
  1099. s->drag_dy = ui->mouse_y - ty;
  1100. } else {
  1101. int rel = ui->mouse_y - r.y - th / 2;
  1102. int avail = r.h - th;
  1103. int max = s->content_h - s->view_h;
  1104. if (rel < 0)
  1105. rel = 0;
  1106. if (rel > avail)
  1107. rel = avail;
  1108. if (avail > 0)
  1109. s->offset = rel * max / avail;
  1110. s->dragging = 1;
  1111. s->drag_dy = th / 2;
  1112. scroll_clamp(s);
  1113. }
  1114. }
  1115. if (s->dragging && (ui->buttons & 1)) {
  1116. int rel = ui->mouse_y - r.y - s->drag_dy;
  1117. int avail = r.h - th;
  1118. int max = s->content_h - s->view_h;
  1119. if (rel < 0)
  1120. rel = 0;
  1121. if (rel > avail)
  1122. rel = avail;
  1123. if (avail > 0)
  1124. s->offset = rel * max / avail;
  1125. scroll_clamp(s);
  1126. }
  1127. if (ui->released)
  1128. s->dragging = 0;
  1129. }
  1130. ngui_push_clip(ui, r);
  1131. }
  1132. void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
  1133. struct ngui_scroll *s)
  1134. {
  1135. ngui_pop_clip(ui);
  1136. if (!scrollable(s))
  1137. return;
  1138. {
  1139. struct ngui_rect bar = rect_make(r.x + r.w - 15, r.y, 14, r.h);
  1140. int th = scroll_thumb_h(&r, s);
  1141. int ty = scroll_thumb_y(&r, s);
  1142. struct ngui_rect thumb = rect_make(bar.x, ty, 14, th);
  1143. ngui_fill_rect(ui, bar, ui->theme.panel_alt);
  1144. ngui_fill_rect(ui, thumb, s->dragging ?
  1145. ui->theme.accent : ui->theme.text_dim);
  1146. }
  1147. }
  1148. int ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s)
  1149. {
  1150. int k = ngui_key_peek(ui);
  1151. int page;
  1152. if (k != NGUI_KEY_PAGE_UP && k != NGUI_KEY_PAGE_DOWN)
  1153. return 0;
  1154. page = s->view_h - 22;
  1155. if (page < 22)
  1156. page = 22;
  1157. ngui_key(ui);
  1158. if (k == NGUI_KEY_PAGE_UP)
  1159. s->offset -= page;
  1160. else
  1161. s->offset += page;
  1162. scroll_clamp(s);
  1163. return 1;
  1164. }