Debian Codegraph @

similar/main/newmenu.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Routines for menus.
23 *
24 */
25
26 #include "dxxsconf.h"
27 #include <stdio.h>
28 #include <cstdlib>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <ctype.h>
32 #include <functional>
33 #include <ranges>
34
35 #include "pstypes.h"
36 #include "dxxerror.h"
37 #include "gr.h"
38 #include "window.h"
39 #include "key.h"
40 #include "mouse.h"
41 #include "palette.h"
42 #include "game.h"
43 #include "text.h"
44 #include "newmenu.h"
45 #include "pcx.h"
46 #include "u_mem.h"
47 #include "mouse.h"
48 #include "joy.h"
49 #include "newdemo.h"
50 #include "kconfig.h"
51 #include "strutil.h"
52 #include "vers_id.h"
53 #include "timer.h"
54 #include "args.h"
55 #if DXX_BUILD_DESCENT == 2
56 #include "gamepal.h"
57 #endif
58
59 #include "compiler-range_for.h"
60 #include "d_zip.h"
61 #include "partial_range.h"
62
63 #define MESSAGEBOX_TEXT_SIZE 2176 // How many characters in messagebox
64 #define MAX_TEXT_WIDTH FSPACX(120) // How many pixels wide a input box can be
65
66 namespace dcx {
67
68 namespace {
69
70 struct callback_newmenu : newmenu
71 {
72 callback_newmenu(const menu_title title, const menu_subtitle subtitle, const menu_filename filename, const tiny_mode_flag tiny_mode, const tab_processing_flag tabs_flag, const adjusted_citem citem_init, grs_canvas &src, subfunction_type subfunction, void *userdata) :
73 newmenu(title, subtitle, filename, tiny_mode, tabs_flag, citem_init, src, filename ? draw_box_flag::none : draw_box_flag::menu_background),
74 subfunction(subfunction), userdata(userdata)
75 {
76 }
77 const subfunction_type subfunction;
78 void *const userdata; // For whatever - like with window system
79 virtual window_event_result event_handler(const d_event &) override;
80 };
81
82 struct step_down
83 {
84 template <typename T>
85 T operator()(T &t) const
86 {
87 return ++t;
88 }
89 };
90
91 struct step_up
92 {
93 template <typename T>
94 T operator()(T &t) const
95 {
96 return --t;
97 }
98 };
99
100 static grs_main_bitmap nm_background, nm_background1;
101 static grs_subbitmap_ptr nm_background_sub;
102
103 static void prepare_slider_text(std::array<char, NM_MAX_TEXT_LEN + 1> &text, const std::size_t offset, const std::size_t steps)
104 {
105 /* 3 = (1 for SLIDER_LEFT) + (1 for SLIDER_RIGHT) + (1 null) */
106 constexpr std::size_t reserved_space = 3;
107 const std::size_t unreserved_space = text.size() - reserved_space;
108 if (offset > unreserved_space ||
109 steps > unreserved_space ||
110 offset + steps > unreserved_space)
111 {
112 text[0] = 0;
113 return;
114 }
115 text[offset] = SLIDER_LEFT[0];
116 text[offset + 1 + steps] = SLIDER_RIGHT[0];
117 text[offset + 2 + steps] = 0;
118 std::fill_n(&text[offset + 1], steps, SLIDER_MIDDLE[0]);
119 }
120
121 }
122
123 void newmenu_free_background() {
124 if (nm_background.bm_data)
125 {
126 nm_background_sub.reset();
127 }
128 nm_background.reset();
129 nm_background1.reset();
130 }
131
132 newmenu_layout::adjusted_citem newmenu_layout::adjusted_citem::create(const std::ranges::subrange<newmenu_item *> items, int citem)
133 {
134 if (citem < 0)
135 citem = 0;
136 const std::size_t nitems = items.size();
137 if (citem > nitems - 1)
138 citem = nitems - 1;
139 uint_fast32_t i{0};
140 const auto begin = items.begin();
141 uint8_t all_text{0};
142 while (std::next(begin, citem)->type == nm_type::text)
143 {
144 if (++ citem >= nitems)
145 citem = 0;
146 if (++ i > nitems)
147 {
148 citem = 0;
149 all_text = 1;
150 break;
151 }
152 }
153 return adjusted_citem{items, citem, all_text};
154 }
155
156 window_event_result callback_newmenu::event_handler(const d_event &event)
157 {
158 if (subfunction)
159 {
160 const auto rval = (*subfunction)(this, event, userdata);
161 if (rval)
162 {
163 if (rval < -1)
164 {
165 if (this->rval)
166 *this->rval = rval;
167 return window_event_result::close;
168 }
169 return window_event_result::handled; // event handled
170 }
171 }
172 return newmenu::event_handler(event);
173 }
174
175 unsigned get_border_x(const grs_canvas &canvas)
176 {
177 return (15 * canvas.cv_bitmap.bm_w) / 320;
178 }
179
180 unsigned get_border_y(const grs_canvas &canvas)
181 {
182 return (15 * canvas.cv_bitmap.bm_h) / 200;
183 }
184
185 }
186
187 namespace dsx {
188
189 namespace {
190
191 #if DXX_BUILD_DESCENT == 1
192 static const char *UP_ARROW_MARKER(const grs_font &, const grs_font &)
193 {
194 return "+"; // 135
195 }
196
197 static const char *DOWN_ARROW_MARKER(const grs_font &, const grs_font &)
198 {
199 return "+"; // 136
200 }
201 #elif DXX_BUILD_DESCENT == 2
202 static const char *UP_ARROW_MARKER(const grs_font &cv_font, const grs_font &game_font)
203 {
204 return &cv_font == &game_font ? "\202" : "\207"; // 135
205 }
206
207 static const char *DOWN_ARROW_MARKER(const grs_font &cv_font, const grs_font &game_font)
208 {
209 return &cv_font == &game_font ? "\200" : "\210"; // 136
210 }
211 #endif
212
213 // Draws the custom menu background pcx, if available
214 static void nm_draw_background1(grs_canvas &canvas, const menu_filename filename)
215 {
216 if (filename != NULL)
217 {
218 if (nm_background1.bm_data == NULL)
219 {
220 pcx_read_bitmap_or_default(filename, nm_background1, gr_palette);
221 }
222 gr_palette_load( gr_palette );
223 show_fullscr(canvas, nm_background1);
224 }
225 #if DXX_BUILD_DESCENT == 2
226 strcpy(last_palette_loaded,""); //force palette load next time
227 #endif
228 }
229
230 }
231
232 }
233
234 #define MENU_BACKGROUND_BITMAP_HIRES (PHYSFS_exists("scoresb.pcx") ? "scoresb.pcx" : "scores.pcx")
235 #define MENU_BACKGROUND_BITMAP_LORES (PHYSFS_exists("scores.pcx") ? "scores.pcx" : "scoresb.pcx")
236 #define MENU_BACKGROUND_BITMAP (HIRESMODE?MENU_BACKGROUND_BITMAP_HIRES:MENU_BACKGROUND_BITMAP_LORES)
237
238 // Draws the frame background for menus
239 void nm_draw_background(grs_canvas &canvas, int x1, int y1, int x2, int y2)
240 {
241 int w,h,init_sub=0;
242 static float BGScaleX=1,BGScaleY=1;
243 if (nm_background.bm_data == NULL)
244 {
245 palette_array_t background_palette;
246 pcx_read_bitmap_or_default(MENU_BACKGROUND_BITMAP, nm_background, background_palette);
247 gr_remap_bitmap_good(nm_background, background_palette, -1, -1);
248 BGScaleX=(static_cast<float>(SWIDTH)/nm_background.bm_w);
249 BGScaleY=(static_cast<float>(SHEIGHT)/nm_background.bm_h);
250 init_sub=1;
251 }
252
253 if ( x1 < 0 ) x1 = 0;
254 if ( y1 < 0 ) y1 = 0;
255 if ( x2 > SWIDTH - 1) x2 = SWIDTH - 1;
256 if ( y2 > SHEIGHT - 1) y2 = SHEIGHT - 1;
257
258 w = x2-x1;
259 h = y2-y1;
260
261 if (w > SWIDTH) w = SWIDTH;
262 if (h > SHEIGHT) h = SHEIGHT;
263
264 gr_palette_load( gr_palette );
265 {
266 const auto &&tmp = gr_create_sub_canvas(canvas, x1, y1, w, h);
267 show_fullscr(*tmp, nm_background); // show so we load all necessary data for the sub-bitmap
268 if (!init_sub && ((nm_background_sub->bm_w != w*((static_cast<float>(nm_background.bm_w))/SWIDTH)) || (nm_background_sub->bm_h != h*((static_cast<float>(nm_background.bm_h))/SHEIGHT))))
269 {
270 init_sub=1;
271 }
272 if (init_sub)
273 nm_background_sub = gr_create_sub_bitmap(nm_background,0,0,w*((static_cast<float>(nm_background.bm_w))/SWIDTH),h*((static_cast<float>(nm_background.bm_h))/SHEIGHT));
274 show_fullscr(*tmp, *nm_background_sub.get());
275 }
276
277 gr_settransblend(canvas, gr_fade_level{14}, gr_blend::normal);
278 {
279 const auto color = BM_XRGB(1, 1, 1);
280 for (w=5*BGScaleX;w>0;w--)
281 gr_urect(canvas, x2 - w, y1 + w * (BGScaleY / BGScaleX), x2 - w, y2 - w * (BGScaleY / BGScaleX), color);//right edge
282 }
283 {
284 const auto color = BM_XRGB(0, 0, 0);
285 for (h=5*BGScaleY;h>0;h--)
286 gr_urect(canvas, x1 + h * (BGScaleX / BGScaleY), y2 - h, x2 - h * (BGScaleX / BGScaleY), y2 - h, color);//bottom edge
287 }
288 gr_settransblend(canvas, GR_FADE_OFF, gr_blend::normal);
289 }
290
291 namespace dcx {
292
293 namespace {
294
295 // Draw a left justfied string
296 static void nm_string(grs_canvas &canvas, const grs_font &cv_font, const int w1, int x, const int y, const char *const s, const tab_processing_flag tabs_flag)
297 {
298 if (tabs_flag == tab_processing_flag::ignore)
299 {
300 const char *s1 = s;
301 const char *p = nullptr;
302 std::unique_ptr<char[]> s2;
303 if (w1 > 0 && (p = strchr(s, '\t')))
304 {
305 s2 = d_strdup(s);
306 s1 = s2.get();
307 *std::next(s2.get(), std::distance(s, p)) = '\0';
308 }
309 gr_string(canvas, cv_font, x, y, s1);
310 if (p)
311 {
312 ++ p;
313 const auto &&[w, h] = gr_get_string_size(cv_font, p);
314 gr_string(canvas, cv_font, x + w1 - w, y, p, w, h);
315 }
316 return;
317 }
318 std::array<int, 6> XTabs = {{18, 90, 127, 165, 231, 256}};
319 const auto &&fspacx = FSPACX();
320 range_for (auto &i, XTabs)
321 {
322 i = fspacx(i) + x;
323 }
324 unsigned t{0};
325 char measure[2];
326 measure[1] = 0;
327 for (unsigned i = 0; const char c = s[i]; ++i)
328 {
329 if (c == '\t')
330 {
331 x=XTabs[t];
332 t++;
333 continue;
334 }
335 measure[0] = c;
336 const auto &&[tx, th] = gr_get_string_size(cv_font, measure);
337 gr_string(canvas, cv_font, x, y, measure, tx, th);
338 x+=tx;
339 }
340 }
341
342 // Draw a slider and it's string
343 static void nm_string_slider(grs_canvas &canvas, const grs_font &cv_font, const int w1, const int x, const int y, char *const s)
344 {
345 char *p,*s1;
346
347 s1=NULL;
348
349 p = strchr( s, '\t' );
350 if (p) {
351 *p = '\0';
352 s1 = p+1;
353 }
354
355 gr_string(canvas, cv_font, x, y, s);
356
357 if (p) {
358 const auto &&[w, h] = gr_get_string_size(cv_font, s1);
359 gr_string(canvas, cv_font, x + w1 - w, y, s1, w, h);
360
361 *p = '\t';
362 }
363 }
364
365
366 // Draw a left justfied string with black background.
367 static void nm_string_black(grs_canvas &canvas, int w1, const int x, const int y, const char *const s)
368 {
369 const auto &&[w, h] = gr_get_string_size(*canvas.cv_font, s);
370
371 if (w1 == 0) w1 = w;
372
373 const auto &&fspacx = FSPACX();
374 const auto &&fspacy = FSPACY();
375 {
376 const uint8_t color = BM_XRGB(5, 5, 5);
377 gr_rect(canvas, x - fspacx(2), y - fspacy(1), x + w1, y + h, color);
378 }
379 {
380 const uint8_t color = BM_XRGB(2, 2, 2);
381 gr_rect(canvas, x - fspacx(2), y - fspacy(1), x, y + h, color);
382 }
383 {
384 const uint8_t color = BM_XRGB(0, 0, 0);
385 gr_rect(canvas, x - fspacx(1), y - fspacy(1), x + w1 - fspacx(1), y + h, color);
386 }
387 gr_string(canvas, *canvas.cv_font, x, y, s, w, h);
388 }
389
390
391 // Draw a right justfied string
392 static void nm_rstring(grs_canvas &canvas, const grs_font &cv_font, int w1, int x, const int y, const char *const s)
393 {
394 const auto &&[w, h] = gr_get_string_size(cv_font, s);
395 x -= FSPACX(3);
396
397 if (w1 == 0) w1 = w;
398 gr_string(canvas, cv_font, x - w, y, s, w, h);
399 }
400
401 static void nm_string_inputbox(grs_canvas &canvas, const grs_font &cv_font, const int w, const int x, const int y, const char *const entry_text, const int current)
402 {
403 const auto &&[w1, text] = [](const grs_font &font, const int w, const char *text) -> std::pair<int, const char *> {
404 // even with variable char widths and a box that goes over the whole screen, we maybe never get more than 75 chars on the line
405 if (const auto l = strlen(text); l > 75)
406 text += l - 75;
407 const auto threshold = w - FSPACX(10);
408 for (;;)
409 {
410 const char c = *text;
411 if (!c)
412 return {0, text};
413 const int w1 = gr_get_string_size(font, text, UINT_MAX).width;
414 if (w1 > threshold)
415 ++text;
416 else
417 return {w1, text};
418 }
419 }(cv_font, w, entry_text);
420
421 nm_string_black(canvas, w, x, y, text);
422
423 if ( current && timer_query() & 0x8000 )
424 gr_string(canvas, cv_font, x + w1, y, CURSOR_STRING);
425 }
426
427 static void draw_item(grs_canvas &canvas, const grs_font &cv_font, newmenu_item &item, int is_current, const tiny_mode_flag tiny, const tab_processing_flag tabs_flag, int scroll_offset)
428 {
429 if (tiny != tiny_mode_flag::normal)
430 {
431 int r, g, b;
432 if (item.text[0] == '\t')
433 r = g = b = 63;
434 else if (is_current)
435 r = 57, g = 49, b = 20;
436 else
437 r = g = 29, b = 47;
438 gr_set_fontcolor(canvas, gr_find_closest_color_current(r, g, b), -1);
439 }
440
441 const int line_spacing = static_cast<int>(LINE_SPACING(cv_font, *GAME_FONT));
442 switch(item.type)
443 {
444 case nm_type::slider:
445 {
446 int i;
447 auto &slider = item.slider();
448 if (item.value < slider.min_value)
449 item.value = slider.min_value;
450 if (item.value > slider.max_value)
451 item.value = slider.max_value;
452 auto &saved_text = slider.saved_text;
453 i = snprintf(saved_text.data(), saved_text.size(), "%s\t", item.text);
454 prepare_slider_text(saved_text, i, slider.max_value - slider.min_value + 1);
455 saved_text[item.value + 1 + strlen(item.text) + 1] = SLIDER_MARKER[0];
456 nm_string_slider(canvas, cv_font, item.w, item.x, item.y - (line_spacing * scroll_offset), saved_text.data());
457 }
458 break;
459 case nm_type::input_menu:
460 if (item.imenu().group == 0)
461 {
462 case nm_type::text:
463 case nm_type::menu:
464 nm_string(canvas, cv_font, item.w, item.x, item.y - (line_spacing * scroll_offset), item.text, tabs_flag);
465 break;
466 }
467 [[fallthrough]];
468 case nm_type::input:
469 nm_string_inputbox(canvas, cv_font, item.w, item.x, item.y - (line_spacing * scroll_offset), item.text, is_current);
470 break;
471 case nm_type::check:
472 nm_string(canvas, cv_font, item.w, item.x, item.y - (line_spacing * scroll_offset), item.text, tabs_flag);
473 nm_rstring(canvas, cv_font, item.right_offset, item.x, item.y - (line_spacing * scroll_offset), item.value ? CHECKED_CHECK_BOX : NORMAL_CHECK_BOX);
474 break;
475 case nm_type::radio:
476 nm_string(canvas, cv_font, item.w, item.x, item.y - (line_spacing * scroll_offset), item.text, tabs_flag);
477 nm_rstring(canvas, cv_font, item.right_offset, item.x, item.y - (line_spacing * scroll_offset), item.value ? CHECKED_RADIO_BOX : NORMAL_RADIO_BOX);
478 break;
479 case nm_type::number:
480 {
481 char text[sizeof("-2147483647")];
482 auto &number = item.number();
483 if (item.value < number.min_value)
484 item.value = number.min_value;
485 if (item.value > number.max_value)
486 item.value = number.max_value;
487 nm_string(canvas, cv_font, item.w, item.x, item.y - (line_spacing * scroll_offset), item.text, tabs_flag);
488 snprintf(text, sizeof(text), "%d", item.value );
489 nm_rstring(canvas, cv_font, item.right_offset, item.x, item.y - (line_spacing * scroll_offset), text);
490 }
491 break;
492 }
493 }
494
495 //returns true if char is allowed
496 static bool char_disallowed(char c, const char *const allowed_chars)
497 {
498 const char *p = allowed_chars;
499 if (!p)
500 return false;
501 for (uint8_t a, b; (a = p[0]) && (b = p[1]); p += 2)
502 {
503 if (likely(c >= a && c <= b))
504 return false;
505 }
506 return true;
507 }
508
509 static bool char_allowed(char c, const char *const allowed_chars)
510 {
511 return !char_disallowed(c, allowed_chars);
512 }
513
514 static void strip_end_whitespace( char * text )
515 {
516 char *ns = text;
517 for (char c; (c = *text);)
518 {
519 ++ text;
520 if (!isspace(static_cast<unsigned>(c)))
521 ns = text;
522 }
523 *ns = 0;
524 }
525
526 }
527
528 int newmenu_do2(const menu_title title, const menu_subtitle subtitle, const std::ranges::subrange<newmenu_item *> items, const newmenu_subfunction subfunction, void *const userdata, const int citem, const menu_filename filename)
529 {
530 if (items.size() < 1)
531 return -1;
532 auto menu = window_create<callback_newmenu>(title, subtitle, filename, tiny_mode_flag::normal, tab_processing_flag::ignore, newmenu_layout::adjusted_citem::create(items, citem), grd_curscreen->sc_canvas, subfunction, userdata);
533 newmenu_free_background();
534 return newmenu::process_until_closed(menu);
535 }
536
537 int newmenu::process_until_closed(newmenu *const menu)
538 {
539 auto rval = std::make_shared<int>(-1);
540 menu->rval = rval;
541 // Track to see when the window is freed
542 // Doing this way in case another window is opened on top without its own polling loop
543 // newmenu_do2 and simpler get their own event loop
544 // This is so the caller doesn't have to provide a callback that responds to event_type::newmenu_selected
545 for (const auto exists = menu->track(); *exists;)
546 event_process();
547
548 /* menu is now a pointer to freed memory, and cannot be accessed
549 * further
550 */
551 return *rval;
552 }
553
554 namespace {
555
556 static void swap_menu_item_entries(newmenu_item &a, newmenu_item &b)
557 {
558 using std::swap;
559 swap(a.text, b.text);
560 swap(a.value, b.value);
561 }
562
563 static inline void rotate_menu_item_subrange(auto &&step_function, newmenu_item *const range_begin, const std::size_t start_index, const newmenu_item *const subrange_stop)
564 {
565 const auto subrange_start = std::next(range_begin, start_index);
566 /* The terminating pointer is named `subrange_stop`, rather than `end`,
567 * because traditional semantics hold that `end` cannot be
568 * dereferenced. In this function, the terminating pointer points
569 * to the last element that is legal to dereference, rather than the
570 * first element that is not legal.
571 */
572 auto iter = subrange_start;
573 if (iter == subrange_stop)
574 /* If iter == subrange_stop, then this function is an elaborate
575 * no-op. Return immediately if there is no work to do.
576 */
577 return;
578 auto &&selected = std::pair(iter->text, iter->value);
579 for (; iter != subrange_stop;)
580 {
581 auto &a = *iter;
582 iter = step_function(iter, 1);
583 auto &b = *iter;
584 a.text = b.text;
585 a.value = b.value;
586 }
587 iter->text = selected.first;
588 iter->value = selected.second;
589 }
590
591 }
592
593 void reorder_newmenu::event_key_command(const d_event &event)
594 {
595 switch(event_key_get(event))
596 {
597 case KEY_SHIFTED+KEY_UP:
598 if (const auto ci{citem}; ci > 0)
599 {
600 const auto ni = -- citem;
601 const auto begin = items.begin();
602 auto &a = *std::next(begin, ci);
603 auto &b = *std::next(begin, ni);
604 swap_menu_item_entries(a, b);
605 }
606 break;
607 case KEY_SHIFTED+KEY_DOWN:
608 if (const auto ci{citem}; ci < items.size() - 1)
609 {
610 const auto ni = ++ citem;
611 const auto begin = items.begin();
612 auto &a = *std::next(begin, ci);
613 auto &b = *std::next(begin, ni);
614 swap_menu_item_entries(a, b);
615 }
616 break;
617 case KEY_PAGEUP + KEY_SHIFTED:
618 {
619 const auto begin = items.begin();
620 const auto stop{begin};
621 rotate_menu_item_subrange(std::minus<void>(), begin, citem, stop);
622 }
623 break;
624 case KEY_PAGEDOWN + KEY_SHIFTED:
625 {
626 const auto begin = items.begin();
627 const auto stop = std::prev(items.end());
628 rotate_menu_item_subrange(std::plus<void>(), begin, citem, stop);
629 }
630 break;
631 }
632 }
633
634 }
635
636 newmenu_item *newmenu_get_items(newmenu *menu)
637 {
638 return menu->items.begin();
639 }
640
641 int newmenu_get_citem(newmenu *menu)
642 {
643 return menu->citem;
644 }
645
646 namespace {
647
648 static void update_menu_position(newmenu &menu, newmenu_item *const stop, int_fast32_t amount, auto &&step, auto &&overflow)
649 {
650 auto icitem = menu.citem;
651 auto pcitem = std::next(menu.items.begin(), icitem);
652 do // count until we reached a non NM_TYPE_TEXT item and reached our amount
653 {
654 if (pcitem == stop)
655 break;
656 step(icitem);
657 step(pcitem);
658 if (menu.is_scroll_box) // update scroll_offset as we go
659 {
660 if (overflow(icitem))
661 step(menu.scroll_offset);
662 }
663 } while (-- amount > 0 || pcitem->type == nm_type::text);
664 menu.citem = icitem;
665 }
666
667 static void newmenu_scroll(newmenu *const menu, const int amount)
668 {
669 if (amount == 0) // nothing to do for us
670 return;
671
672 if (menu->all_text)
673 {
674 menu->scroll_offset += amount;
675 if (menu->scroll_offset < 0)
676 menu->scroll_offset = 0;
677 const auto nitems = menu->items.size();
678 if (menu->max_on_menu+menu->scroll_offset > nitems)
679 menu->scroll_offset = nitems - menu->max_on_menu;
680 return;
681 }
682 /* Otherwise, at least one element is not of type text. Find that element.
683 */
684 const auto &range = menu->items;
685 const auto predicate = [](const newmenu_item &n) {
686 return n.type != nm_type::text;
687 };
688 const auto &&first{std::ranges::find_if(range, predicate)};
689 if (first == range.end())
690 /* This should not happen. If every entry is of type `nm_type::text`,
691 * then `menu->all_text` should have been true.
692 */
693 return;
694 const auto &&rlast{std::ranges::find_if(std::reverse_iterator(range.end()), std::reverse_iterator(first), predicate).base()};
695 /* `first == rlast` should not happen, since that would mean that
696 * there are no elements in `range` for which `predicate` is true.
697 * If there are no such elements, then `first == range.end()` should
698 * be true and the function would have returned above.
699 */
700 assert(first != rlast);
701 if (first == rlast) // nothing to do for us
702 return;
703 const auto last = std::prev(rlast);
704 /* Exactly one element that satisfies `predicate` exists in `range`.
705 * Only elements that satisfy `predicate` can be selected, so the
706 * selection cannot be changed.
707 */
708 if (first == last)
709 return;
710 auto citem = std::next(menu->items.begin(), menu->citem);
711 if (citem == last && amount == 1) // if citem == last item and we want to go down one step, go to first item
712 {
713 newmenu_scroll(menu, -menu->items.size());
714 return;
715 }
716 if (citem == first && amount == -1) // if citem == first item and we want to go up one step, go to last item
717 {
718 newmenu_scroll(menu, menu->items.size());
719 return;
720 }
721
722 if (amount > 0) // down the list
723 {
724 const auto nitems = menu->items.size();
725 const auto overflow = [menu, nitems](int icitem) {
726 return icitem + 4 >= menu->max_on_menu + menu->scroll_offset && menu->scroll_offset < nitems - menu->max_on_menu;
727 };
728 update_menu_position(*menu, last, amount, step_down(), overflow);
729 }
730 else if (amount < 0) // up the list
731 {
732 const auto overflow = [menu](int icitem) {
733 return icitem - 4 < menu->scroll_offset && menu->scroll_offset > 0;
734 };
735 update_menu_position(*menu, first, std::abs(amount), step_up(), overflow);
736 }
737 }
738
739 static int nm_trigger_radio_button(newmenu &menu, newmenu_item &citem)
740 {
741 citem.value = 1;
742 const auto cg = citem.radio().group;
743 range_for (auto &r, menu.items)
744 {
745 if (&r != &citem && r.type == nm_type::radio && r.radio().group == cg)
746 {
747 if (r.value)
748 {
749 r.value = 0;
750 return 1;
751 }
752 }
753 }
754 return 0;
755 }
756
757 static unsigned check_mouse_scroll_allowed_by_timer()
758 {
759 static fix64 ScrollTime;
760 const auto now = timer_query();
761 if (ScrollTime + F1_0/5 < now)
762 {
763 ScrollTime = now;
764 return 1;
765 }
766 return 0;
767 }
768
769 static void check_apply_mouse_scroll(newmenu *const menu, const grs_canvas &canvas, const font_x_scale_float fspacx, const int line_spacing, const int mx, const int my)
770 {
771 const auto scroll_offset = menu->scroll_offset;
772 if (!scroll_offset && menu->max_displayable >= menu->items.size())
773 return;
774
775 const auto x1 = canvas.cv_bitmap.bm_x + BORDERX - fspacx(12);
776 if (mx <= x1)
777 /* If the mouse is left of the arrows, return. */
778 return;
779 /* In practice, both arrows are the same width and same height. In
780 * D1, they are the same character. In D2, they are visually
781 * distinct, but have the same dimensions.
782 *
783 * For both games, measure one and use its dimensions for both.
784 */
785 const auto &&[arrow_width, arrow_height] = gr_get_string_size(*canvas.cv_font, UP_ARROW_MARKER(*canvas.cv_font, *GAME_FONT));
786 const auto x2 = x1 + arrow_width;
787 if (mx >= x2)
788 /* If the mouse is to the right of the upper arrow, it will
789 * also be right of the lower arrow.
790 */
791 return;
792 if (scroll_offset)
793 {
794 const auto y1 = canvas.cv_bitmap.bm_y + std::next(menu->items.begin(), scroll_offset)->y - (line_spacing * scroll_offset);
795 if (my <= y1)
796 /* If the mouse is above the upper arrow, it must also be
797 * above the lower arrow, so return instead of jumping
798 * forward to the down arrow check.
799 */
800 return;
801 const auto y2 = y1 + arrow_height;
802 if (my < y2)
803 {
804 if (check_mouse_scroll_allowed_by_timer())
805 newmenu_scroll(menu, -1);
806 /* If the mouse is on the upper arrow, it cannot be on the
807 * lower arrow.
808 */
809 return;
810 }
811 }
812 if (scroll_offset + menu->max_displayable < menu->items.size())
813 {
814 const auto y1 = canvas.cv_bitmap.bm_y + std::next(menu->items.begin(), scroll_offset + menu->max_displayable - 1)->y - (line_spacing * scroll_offset);
815 if (my <= y1)
816 return;
817 const auto y2 = y1 + arrow_height;
818 if (my < y2)
819 {
820 if (check_mouse_scroll_allowed_by_timer())
821 newmenu_scroll(menu, 1);
822 /* Explicitly returning is redundant here since there are no
823 * more statements, but it is present for consistency with
824 * the up arrow. Also, this serves as future proofing in
825 * case someone adds another clickable area.
826 */
827 return;
828 }
829 }
830 }
831
832 static window_event_result newmenu_mouse(const d_event &event, newmenu *menu, const mbtn button)
833 {
834 int old_choice, x1 = 0, x2, y1, y2, changed = 0;
835 grs_canvas &menu_canvas = menu->w_canv;
836
837 switch (button)
838 {
839 case mbtn::left:
840 {
841 auto &canvas = menu_canvas;
842
843 old_choice = menu->citem;
844
845 const auto &&fspacx = FSPACX();
846 if (event.type == event_type::mouse_button_down && !menu->all_text)
847 {
848 const auto [mx, my, mz] = mouse_get_pos();
849 const int line_spacing = static_cast<int>(LINE_SPACING(*canvas.cv_font, *GAME_FONT));
850 for (int i = menu->scroll_offset; i < menu->max_on_menu + menu->scroll_offset; ++i)
851 {
852 auto &iitem = *std::next(menu->items.begin(), i);
853 x1 = canvas.cv_bitmap.bm_x + iitem.x - fspacx(13) /*- menu->items[i].right_offset - 6*/;
854 x2 = x1 + iitem.w + fspacx(13);
855 y1 = canvas.cv_bitmap.bm_y + iitem.y - (line_spacing * menu->scroll_offset);
856 y2 = y1 + iitem.h;
857 if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2))) {
858 menu->citem = i;
859 auto &citem = iitem;
860 switch (citem.type)
861 {
862 case nm_type::check:
863 citem.value = !citem.value;
864 changed = 1;
865 break;
866 case nm_type::radio:
867 changed = nm_trigger_radio_button(*menu, citem);
868 break;
869 case nm_type::text:
870 menu->citem=old_choice;
871 menu->mouse_state=0;
872 break;
873 case nm_type::menu:
874 case nm_type::input:
875 case nm_type::number:
876 case nm_type::input_menu:
877 case nm_type::slider:
878 break;
879 }
880 break;
881 }
882 }
883 }
884
885 if ( menu->mouse_state ) {
886 const auto [mx, my, mz] = mouse_get_pos();
887 // check possible scrollbar stuff first
888 const int line_spacing = static_cast<int>(LINE_SPACING(*canvas.cv_font, *GAME_FONT));
889 if (menu->is_scroll_box) {
890 check_apply_mouse_scroll(menu, canvas, fspacx, line_spacing, mx, my);
891 }
892
893 for (int i = menu->scroll_offset; i < menu->max_on_menu + menu->scroll_offset; ++i)
894 {
895 auto &iitem = *std::next(menu->items.begin(), i);
896 x1 = canvas.cv_bitmap.bm_x + iitem.x - fspacx(13);
897 x2 = x1 + iitem.w + fspacx(13);
898 y1 = canvas.cv_bitmap.bm_y + iitem.y - (line_spacing * menu->scroll_offset);
899 y2 = y1 + iitem.h;
900
901 if (mx > x1 && mx < x2 && my > y1 && my < y2 && iitem.type != nm_type::text)
902 {
903 menu->citem = i;
904 auto &citem = iitem;
905 if (citem.type == nm_type::slider)
906 {
907 auto &slider = citem.slider();
908 auto &saved_text = slider.saved_text;
909 char slider_text[NM_MAX_TEXT_LEN+1], *p, *s1;
910
911 strcpy(slider_text, saved_text);
912 p = strchr(slider_text, '\t');
913 if (p) {
914 *p = '\0';
915 s1 = p+1;
916 }
917 if (p) {
918 const auto slider_width = gr_get_string_size(*canvas.cv_font, s1).width;
919 const auto sleft_width = gr_get_string_size(*canvas.cv_font, SLIDER_LEFT).width;
920 const auto sright_width = gr_get_string_size(*canvas.cv_font, SLIDER_RIGHT).width;
921
922 x1 = canvas.cv_bitmap.bm_x + citem.x + citem.w - slider_width;
923 x2 = x1 + slider_width + sright_width;
924 int new_value;
925 auto &slider = citem.slider();
926 if (
927 (mx > x1 && mx < x1 + sleft_width && (new_value = slider.min_value, true)) ||
928 (mx < x2 && mx > x2 - sright_width && (new_value = slider.max_value, true)) ||
929 (mx > x1 + sleft_width && mx < x2 - sright_width - sleft_width && (new_value = (mx - x1 - sleft_width) / ((slider_width - sleft_width - sright_width) / (slider.max_value - slider.min_value + 1)), true))
930 )
931 if (citem.value != new_value)
932 {
933 citem.value = new_value;
934 changed = 1;
935 }
936 *p = '\t';
937 }
938 }
939 if (menu->citem == old_choice)
940 break;
941 if (citem.type == nm_type::input)
942 citem.value = -1;
943 if (!(old_choice > -1))
944 break;
945 auto &oitem = *std::next(menu->items.begin(), old_choice);
946 if (oitem.type == nm_type::input_menu)
947 {
948 auto &im = oitem.imenu();
949 im.group = 0;
950 oitem.value = -1;
951 strcpy(oitem.text, im.saved_text);
952 }
953 break;
954 }
955 }
956 }
957
958 if (event.type == event_type::mouse_button_up &&
959 !menu->all_text &&
960 menu->citem != -1 &&
961 std::next(menu->items.begin(), menu->citem)->type == nm_type::menu)
962 {
963 const auto [mx, my, mz] = mouse_get_pos();
964 const int line_spacing = static_cast<int>(LINE_SPACING(*canvas.cv_font, *GAME_FONT));
965 for (int i = menu->scroll_offset; i < menu->max_on_menu + menu->scroll_offset; ++i)
966 {
967 auto &iitem = *std::next(menu->items.begin(), i);
968 x1 = canvas.cv_bitmap.bm_x + iitem.x - fspacx(13);
969 x2 = x1 + iitem.w + fspacx(13);
970 y1 = canvas.cv_bitmap.bm_y + iitem.y - (line_spacing * menu->scroll_offset);
971 y2 = y1 + iitem.h;
972 if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2))) {
973 // Tell callback, allow staying in menu
974 if (const auto r = menu->event_handler(d_select_event{menu->citem}); r == window_event_result::handled)
975 return r;
976
977 if (menu->rval)
978 *menu->rval = menu->citem;
979 return window_event_result::close;
980 }
981 }
982 }
983
984 if (event.type == event_type::mouse_button_up && menu->citem > -1)
985 {
986 auto &citem = *std::next(menu->items.begin(), menu->citem);
987 if (citem.type == nm_type::input_menu && citem.imenu().group == 0)
988 {
989 auto &im = citem.imenu();
990 im.group = 1;
991 if (!d_stricmp(im.saved_text, TXT_EMPTY))
992 {
993 citem.text[0] = 0;
994 citem.value = -1;
995 } else {
996 strip_end_whitespace(citem.text);
997 }
998 }
999 }
1000 if (changed)
1001 {
1002 menu->event_handler(d_change_event{menu->citem});
1003 }
1004 break;
1005 }
1006 case mbtn::right:
1007 if (menu->mouse_state)
1008 {
1009 if (!(menu->citem > -1))
1010 return window_event_result::close;
1011 auto &citem = *std::next(menu->items.begin(), menu->citem);
1012 if (citem.type != nm_type::input_menu)
1013 return window_event_result::close;
1014 if (auto &im = citem.imenu(); im.group == 1)
1015 {
1016 im.group = 0;
1017 citem.value = -1;
1018 strcpy(citem.text, im.saved_text);
1019 } else {
1020 return window_event_result::close;
1021 }
1022 }
1023 break;
1024 case mbtn::z_up:
1025 if (menu->mouse_state)
1026 newmenu_scroll(menu, -1);
1027 break;
1028 case mbtn::z_down:
1029 if (menu->mouse_state)
1030 newmenu_scroll(menu, 1);
1031 break;
1032 default:
1033 break;
1034 }
1035
1036 return window_event_result::ignored;
1037 }
1038
1039 static window_event_result newmenu_key_command(const d_event &event, newmenu *const menu)
1040 {
1041 int k = event_key_get(event);
1042 int old_choice;
1043 int changed{0};
1044 window_event_result rval = window_event_result::handled;
1045
1046 if (keyd_pressed[KEY_NUMLOCK])
1047 {
1048 switch( k )
1049 {
1050 case KEY_PAD0: k = KEY_0; break;
1051 case KEY_PAD1: k = KEY_1; break;
1052 case KEY_PAD2: k = KEY_2; break;
1053 case KEY_PAD3: k = KEY_3; break;
1054 case KEY_PAD4: k = KEY_4; break;
1055 case KEY_PAD5: k = KEY_5; break;
1056 case KEY_PAD6: k = KEY_6; break;
1057 case KEY_PAD7: k = KEY_7; break;
1058 case KEY_PAD8: k = KEY_8; break;
1059 case KEY_PAD9: k = KEY_9; break;
1060 case KEY_PADPERIOD: k = KEY_PERIOD; break;
1061 }
1062 }
1063
1064 old_choice = menu->citem;
1065 auto &citem = *std::next(menu->items.begin(), menu->citem);
1066
1067 switch( k ) {
1068 case KEY_HOME:
1069 case KEY_PAD7:
1070 newmenu_scroll(menu, -menu->items.size());
1071 break;
1072 case KEY_END:
1073 case KEY_PAD1:
1074 newmenu_scroll(menu, menu->items.size());
1075 break;
1076 case KEY_TAB + KEY_SHIFTED:
1077 case KEY_UP:
1078 case KEY_PAGEUP:
1079 case KEY_PAD8:
1080 newmenu_scroll(menu, k == KEY_PAGEUP ? -10 : -1);
1081 if (citem.type == nm_type::input && menu->citem != old_choice)
1082 citem.value = -1;
1083 if (old_choice > -1 && old_choice != menu->citem)
1084 {
1085 auto &oitem = *std::next(menu->items.begin(), old_choice);
1086 if (oitem.type == nm_type::input_menu)
1087 {
1088 auto &im = oitem.imenu();
1089 im.group = 0;
1090 oitem.value = -1;
1091 strcpy(oitem.text, im.saved_text);
1092 }
1093 }
1094 break;
1095 case KEY_TAB:
1096 case KEY_DOWN:
1097 case KEY_PAGEDOWN:
1098 case KEY_PAD2:
1099 newmenu_scroll(menu, k == KEY_PAGEDOWN ? 10 : 1);
1100 if (citem.type == nm_type::input && menu->citem != old_choice)
1101 citem.value = -1;
1102 if (old_choice > -1 && old_choice != menu->citem)
1103 {
1104 auto &oitem = *std::next(menu->items.begin(), old_choice);
1105 if (oitem.type == nm_type::input_menu)
1106 {
1107 auto &im = oitem.imenu();
1108 im.group = 0;
1109 oitem.value = -1;
1110 strcpy(oitem.text, im.saved_text);
1111 }
1112 }
1113 break;
1114 case KEY_SPACEBAR:
1115 if ( menu->citem > -1 ) {
1116 switch (citem.type)
1117 {
1118 case nm_type::text:
1119 case nm_type::number:
1120 case nm_type::slider:
1121 case nm_type::menu:
1122 case nm_type::input:
1123 case nm_type::input_menu:
1124 break;
1125 case nm_type::check:
1126 citem.value = !citem.value;
1127 changed = 1;
1128 break;
1129 case nm_type::radio:
1130 changed = nm_trigger_radio_button(*menu, citem);
1131 break;
1132 }
1133 }
1134 break;
1135
1136 case KEY_ENTER:
1137 case KEY_PADENTER:
1138 if (menu->citem > -1 && citem.type == nm_type::input_menu && citem.imenu().group == 0)
1139 {
1140 auto &im = citem.imenu();
1141 im.group = 1;
1142 if (!d_stricmp(im.saved_text, TXT_EMPTY))
1143 {
1144 citem.text[0] = 0;
1145 citem.value = -1;
1146 } else {
1147 strip_end_whitespace(citem.text);
1148 }
1149 } else
1150 {
1151 if (citem.type == nm_type::input_menu)
1152 citem.imenu().group = 0; // go out of editing mode
1153
1154 // Tell callback, allow staying in menu
1155 if (const auto r = menu->event_handler(d_select_event{menu->citem}); r == window_event_result::handled)
1156 return r;
1157
1158 if (menu->rval)
1159 *menu->rval = menu->citem;
1160 return window_event_result::close;
1161 }
1162 break;
1163
1164 case KEY_ESC:
1165 if (menu->citem > -1 && citem.type == nm_type::input_menu && citem.imenu().group == 1)
1166 {
1167 auto &im = citem.imenu();
1168 im.group = 0;
1169 citem.value = -1;
1170 strcpy(citem.text, im.saved_text);
1171 } else {
1172 return window_event_result::close;
1173 }
1174 break;
1175 default:
1176 rval = window_event_result::ignored;
1177 break;
1178 }
1179
1180 if ( menu->citem > -1 ) {
1181 // Alerting callback of every keypress for NM_TYPE_INPUT. Alternatively, just respond to event_type::newmenu_selected
1182 if ((citem.type == nm_type::input || (citem.type == nm_type::input_menu && citem.imenu().group == 1)) && old_choice == menu->citem)
1183 {
1184 if ( k==KEY_LEFT || k==KEY_BACKSP || k==KEY_PAD4 ) {
1185 if (citem.value == -1) citem.value = strlen(citem.text);
1186 if (citem.value > 0)
1187 citem.value--;
1188 citem.text[citem.value] = 0;
1189
1190 if (citem.type == nm_type::input)
1191 changed = 1;
1192 rval = window_event_result::handled;
1193 }
1194 else if (const auto im = citem.input_or_menu(); citem.value < im->text_len || citem.value == -1)
1195 {
1196 auto ascii = key_ascii();
1197 if (ascii < 255)
1198 {
1199 if (citem.value == -1) {
1200 citem.value = 0;
1201 }
1202 const auto allowed_chars = im->allowed_chars;
1203 if (char_allowed(ascii, allowed_chars) || (ascii == ' ' && char_allowed(ascii = '_', allowed_chars)))
1204 {
1205 citem.text[citem.value++] = ascii;
1206 citem.text[citem.value] = 0;
1207
1208 if (citem.type == nm_type::input)
1209 changed = 1;
1210 }
1211 }
1212 }
1213 }
1214 else if (citem.type != nm_type::input && citem.type != nm_type::input_menu)
1215 {
1216 auto ascii = key_ascii();
1217 if (ascii < 255 ) {
1218 int choice1 = menu->citem;
1219 ascii = toupper(ascii);
1220 do {
1221 choice1++;
1222 if (choice1 >= menu->items.size())
1223 choice1=0;
1224
1225 unsigned ch;
1226 auto &choice_item = *std::next(menu->items.begin(), choice1);
1227 for (int i = 0; (ch = choice_item.text[i]) != 0 && ch == ' ' ; ++i)
1228 {
1229 }
1230
1231 if ((choice_item.type == nm_type::menu ||
1232 choice_item.type == nm_type::check ||
1233 choice_item.type == nm_type::radio ||
1234 choice_item.type == nm_type::number ||
1235 choice_item.type == nm_type::slider)
1236 && (ascii==toupper(ch)) )
1237 {
1238 k = 0;
1239 menu->citem = choice1;
1240 }
1241
1242 while (menu->citem + 4 >= menu->max_on_menu + menu->scroll_offset && menu->scroll_offset < menu->items.size() - menu->max_on_menu)
1243 menu->scroll_offset++;
1244 while (menu->citem-4<menu->scroll_offset && menu->scroll_offset > 0)
1245 menu->scroll_offset--;
1246
1247 } while (choice1 != menu->citem );
1248 }
1249 }
1250
1251 if (const auto ns = citem.number_or_slider())
1252 {
1253 switch( k ) {
1254 case KEY_LEFT:
1255 case KEY_PAD4:
1256 citem.value -= 1;
1257 changed = 1;
1258 rval = window_event_result::handled;
1259 break;
1260 case KEY_RIGHT:
1261 case KEY_PAD6:
1262 citem.value++;
1263 changed = 1;
1264 rval = window_event_result::handled;
1265 break;
1266 case KEY_SPACEBAR:
1267 citem.value += 10;
1268 changed = 1;
1269 rval = window_event_result::handled;
1270 break;
1271 case KEY_BACKSP:
1272 citem.value -= 10;
1273 changed = 1;
1274 rval = window_event_result::handled;
1275 break;
1276 }
1277
1278 auto &min_value = ns->min_value;
1279 if (citem.value < min_value)
1280 citem.value = min_value;
1281 auto &max_value = ns->max_value;
1282 if (citem.value > max_value)
1283 citem.value = max_value;
1284 }
1285
1286 }
1287
1288 if (changed)
1289 {
1290 menu->event_handler(d_change_event{menu->citem});
1291 }
1292
1293 return rval;
1294 }
1295
1296 }
1297
1298 namespace dsx {
1299
1300 namespace {
1301
1302 static void newmenu_create_structure(newmenu_layout &menu, const grs_font &cv_font)
1303 {
1304 auto &canvas = menu.parent_canvas;
1305
1306 auto iterative_layout_max_width = 0u;
1307 auto iterative_layout_max_height = 0u;
1308
1309 if (menu.title)
1310 {
1311 auto &huge_font = *HUGE_FONT;
1312 const auto &&[string_width, string_height] = gr_get_string_size(huge_font, menu.title);
1313 iterative_layout_max_width = string_width;
1314 iterative_layout_max_height = string_height;
1315 }
1316 if (menu.subtitle)
1317 {
1318 auto &medium3_font = *MEDIUM3_FONT;
1319 const auto &&[string_width, string_height] = gr_get_string_size(medium3_font, menu.subtitle);
1320 if (iterative_layout_max_width < string_width)
1321 iterative_layout_max_width = string_width;
1322 iterative_layout_max_height += string_height;
1323 }
1324
1325 iterative_layout_max_height += FSPACY(5); //put some space between titles & body
1326
1327 int aw{0};
1328 auto iterative_layout_body_width = 0u;
1329 const auto initial_layout_height{iterative_layout_max_height};
1330
1331 const auto &&fspacx = FSPACX();
1332 const auto &&fspacy = FSPACY();
1333 const auto &&fspacx8 = fspacx(8);
1334 const auto &&fspacy1 = fspacy(1);
1335 // Find menu height & width (store in iterative_layout_max_width,
1336 // iterative_layout_max_height)
1337 range_for (auto &i, menu.items)
1338 {
1339 i.y = iterative_layout_max_height;
1340 const auto average_width = cv_font.ft_w;
1341 auto [string_width, string_height] = gr_get_string_size(cv_font, i.text);
1342 i.right_offset = 0;
1343
1344 if (i.type == nm_type::slider)
1345 {
1346 auto &slider = i.slider();
1347 auto &saved_text = slider.saved_text;
1348 prepare_slider_text(saved_text, 0, slider.max_value - slider.min_value + 1);
1349 const auto w1 = gr_get_string_size(cv_font, saved_text.data()).width;
1350 string_width += w1 + aw;
1351 }
1352
1353 else if (i.type == nm_type::check)
1354 {
1355 const auto wnormal = gr_get_string_size(cv_font, NORMAL_CHECK_BOX).width;
1356 const auto wchecked = gr_get_string_size(cv_font, CHECKED_CHECK_BOX).width;
1357 i.right_offset = std::max(wnormal, wchecked);
1358 }
1359
1360 else if (i.type == nm_type::radio)
1361 {
1362 const auto wnormal = gr_get_string_size(cv_font, NORMAL_RADIO_BOX).width;
1363 const auto wchecked = gr_get_string_size(cv_font, CHECKED_RADIO_BOX).width;
1364 i.right_offset = std::max(wnormal, wchecked);
1365 }
1366
1367 else if (i.type == nm_type::number)
1368 {
1369 char test_text[20];
1370 auto &number = i.number();
1371 snprintf(test_text, sizeof(test_text), "%d", number.max_value);
1372 const auto wmax = gr_get_string_size(cv_font, test_text).width;
1373 snprintf(test_text, sizeof(test_text), "%d", number.min_value);
1374 const auto wmin = gr_get_string_size(cv_font, test_text).width;
1375 i.right_offset = std::max(wmax, wmin);
1376 }
1377
1378 if (const auto input_or_menu = i.input_or_menu())
1379 {
1380 const auto text_len = input_or_menu->text_len;
1381 string_width = text_len * fspacx8 + text_len;
1382 if (i.type == nm_type::input && string_width > MAX_TEXT_WIDTH)
1383 string_width = MAX_TEXT_WIDTH;
1384
1385 i.value = -1;
1386 if (i.type == nm_type::input_menu)
1387 {
1388 auto &im = i.imenu();
1389 im.group = 0;
1390 im.saved_text.copy_if(i.text);
1391 }
1392 }
1393
1394 i.w = string_width;
1395 i.h = string_height;
1396
1397 if (iterative_layout_body_width < string_width)
1398 iterative_layout_body_width = string_width; // Save maximum width
1399 if ( average_width > aw )
1400 aw = average_width;
1401 iterative_layout_max_height += string_height + fspacy1; // Find the height of all strings
1402 }
1403
1404 if (menu.items.size() > menu.max_on_menu)
1405 {
1406 iterative_layout_max_height = initial_layout_height + (LINE_SPACING(cv_font, *GAME_FONT) * menu.max_on_menu);
1407
1408 // if our last citem was > menu.max_on_menu, make sure we re-scroll when we call this menu again
1409 if (menu.citem > menu.max_on_menu - 4)
1410 {
1411 menu.scroll_offset = menu.citem - (menu.max_on_menu - 4);
1412 if (menu.scroll_offset + menu.max_on_menu > menu.items.size())
1413 menu.scroll_offset = menu.items.size() - menu.max_on_menu;
1414 }
1415 }
1416 menu.h = iterative_layout_max_height;
1417
1418 int right_offset{0};
1419
1420 range_for (auto &i, menu.items)
1421 {
1422 i.w = iterative_layout_body_width;
1423 if (right_offset < i.right_offset)
1424 right_offset = i.right_offset;
1425 }
1426
1427 menu.w = iterative_layout_body_width + right_offset;
1428
1429 int twidth{0};
1430 if (menu.w < iterative_layout_max_width)
1431 {
1432 twidth = (iterative_layout_max_width - menu.w) / 2;
1433 menu.w = iterative_layout_max_width;
1434 }
1435
1436 // Find min point of menu border
1437 menu.w += BORDERX*2;
1438 menu.h += BORDERY*2;
1439
1440 menu.x = (canvas.cv_bitmap.bm_w - menu.w) / 2;
1441 menu.y = (canvas.cv_bitmap.bm_h - menu.h) / 2;
1442
1443 if (menu.x < 0)
1444 menu.x = 0;
1445 if (menu.y < 0)
1446 menu.y = 0;
1447
1448 nm_draw_background1(canvas, menu.filename);
1449
1450 // Update all item's x & y values.
1451 range_for (auto &i, menu.items)
1452 {
1453 i.x = BORDERX + twidth + right_offset;
1454 i.y += BORDERY;
1455 if (i.type == nm_type::radio)
1456 {
1457 // find first marked one
1458 newmenu_item *fm = nullptr;
1459 range_for (auto &j, menu.items)
1460 {
1461 if (j.type == nm_type::radio && j.radio().group == i.radio().group)
1462 {
1463 if (!fm && j.value)
1464 fm = &j;
1465 j.value = 0;
1466 }
1467 }
1468 (fm ? *fm : i).value = 1;
1469 }
1470 }
1471
1472 menu.mouse_state = 0;
1473 menu.swidth = SWIDTH;
1474 menu.sheight = SHEIGHT;
1475 menu.fntscalex = FNTScaleX;
1476 menu.fntscaley = FNTScaleY;
1477 }
1478
1479 static window_event_result newmenu_draw(newmenu *menu)
1480 {
1481 auto &menu_canvas = menu->w_canv;
1482 int th{0}, ty, sx, sy;
1483
1484 if (menu->swidth != SWIDTH || menu->sheight != SHEIGHT || menu->fntscalex != FNTScaleX || menu->fntscaley != FNTScaleY)
1485 {
1486 menu->create_structure();
1487 {
1488 gr_init_sub_canvas(menu_canvas, grd_curscreen->sc_canvas, menu->x, menu->y, menu->w, menu->h);
1489 }
1490 }
1491
1492 {
1493 auto &parent_canvas = menu->parent_canvas;
1494 nm_draw_background1(parent_canvas, menu->filename);
1495 if (menu->draw_box != draw_box_flag::none)
1496 {
1497 const auto mx = menu->x;
1498 const auto my = menu->y;
1499 auto ex = mx;
1500 if (menu->is_scroll_box)
1501 ex -= FSPACX(5);
1502 nm_draw_background(parent_canvas, ex, my, mx + menu->w, my + menu->h);
1503 }
1504 }
1505
1506 ty = BORDERY;
1507
1508 if (const auto title = menu->title)
1509 {
1510 gr_set_fontcolor(menu_canvas, BM_XRGB(31, 31, 31), -1);
1511 auto &huge_font = *HUGE_FONT;
1512 auto &&[string_width, string_height] = gr_get_string_size(huge_font, title);
1513 th = string_height;
1514 gr_string(menu_canvas, huge_font, 0x8000, ty, title, string_width, string_height);
1515 }
1516
1517 if (const auto subtitle = menu->subtitle)
1518 {
1519 gr_set_fontcolor(menu_canvas, BM_XRGB(21, 21, 21), -1);
1520 auto &medium3_font = *MEDIUM3_FONT;
1521 gr_string(menu_canvas, medium3_font, 0x8000, ty + th, subtitle);
1522 }
1523
1524 const auto tiny_mode = menu->tiny_mode;
1525 const auto &game_font = *GAME_FONT.get();
1526 const grs_font &noncurrent_item_cv_font = tiny_mode != tiny_mode_flag::normal
1527 ? game_font
1528 : *MEDIUM1_FONT.get();
1529 // Redraw everything...
1530 {
1531 const auto begin = menu->items.begin();
1532 /* If the menu is all text, no item is current. Set the
1533 * "current" index to an impossible value so that the all_text
1534 * test does not need to be run at every step.
1535 */
1536 const auto current_index = menu->all_text ? ~0u : menu->citem;
1537 const auto tabs_flag = menu->tabs_flag;
1538 const auto scroll_offset = menu->scroll_offset;
1539 for (auto i = scroll_offset; i < menu->max_displayable + scroll_offset; ++i)
1540 {
1541 const auto is_current = (i == current_index);
1542 const grs_font &cv_font = (tiny_mode == tiny_mode_flag::normal && is_current)
1543 ? *MEDIUM2_FONT.get()
1544 : noncurrent_item_cv_font;
1545 gr_set_curfont(menu_canvas, cv_font);
1546 draw_item(menu_canvas, cv_font, *std::next(begin, i), is_current, tiny_mode, tabs_flag, scroll_offset);
1547 }
1548 }
1549
1550 if (menu->is_scroll_box)
1551 {
1552 auto &cv_font = (menu->tiny_mode != tiny_mode_flag::normal ? noncurrent_item_cv_font : *MEDIUM2_FONT);
1553
1554 const int line_spacing = static_cast<int>(LINE_SPACING(cv_font, game_font));
1555 const auto scroll_offset = menu->scroll_offset;
1556 const auto begin = menu->items.begin();
1557 sy = std::next(begin, scroll_offset)->y - (line_spacing * scroll_offset);
1558 const auto &&fspacx = FSPACX();
1559 sx = BORDERX - fspacx(12);
1560
1561 gr_string(menu_canvas, cv_font, sx, sy, scroll_offset ? UP_ARROW_MARKER(cv_font, game_font) : " ");
1562
1563 sy = std::next(begin, scroll_offset + menu->max_displayable - 1)->y - (line_spacing * scroll_offset);
1564 sx = BORDERX - fspacx(12);
1565
1566 gr_string(menu_canvas, cv_font, sx, sy, (scroll_offset + menu->max_displayable < menu->items.size()) ? DOWN_ARROW_MARKER(cv_font, game_font) : " ");
1567 }
1568 menu->event_handler(d_event{event_type::newmenu_draw});
1569 return window_event_result::handled;
1570 }
1571
1572 }
1573
1574 }
1575
1576 namespace dcx {
1577
1578 void newmenu_layout::create_structure()
1579 {
1580 newmenu_create_structure(*this, *(tiny_mode != tiny_mode_flag::normal ? GAME_FONT : MEDIUM1_FONT));
1581 }
1582
1583 }
1584
1585 window_event_result newmenu::event_handler(const d_event &event)
1586 {
1587 #if DXX_MAX_BUTTONS_PER_JOYSTICK
1588 if (joy_translate_menu_key(event))
1589 return window_event_result::handled;
1590 #endif
1591
1592 switch (event.type)
1593 {
1594 case event_type::window_activated:
1595 game_flush_inputs(Controls);
1596 event_toggle_focus(0);
1597 key_toggle_repeat(1);
1598 break;
1599
1600 case event_type::window_deactivated:
1601 //event_toggle_focus(1); // No cursor recentering
1602 key_toggle_repeat(1);
1603 mouse_state = 0;
1604 break;
1605
1606 case event_type::mouse_button_down:
1607 case event_type::mouse_button_up:
1608 {
1609 mouse_state = event.type == event_type::mouse_button_down;
1610 const auto button = event_mouse_get_button(event);
1611 return newmenu_mouse(event, this, button);
1612 }
1613
1614 case event_type::key_command:
1615 return newmenu_key_command(event, this);
1616 case event_type::idle:
1617 if (!(Game_mode & GM_MULTI) || !Game_wind || !Game_wind->is_visible())
1618 timer_delay2(CGameArg.SysMaxFPS);
1619 break;
1620 case event_type::window_draw:
1621 return newmenu_draw(this);
1622 case event_type::window_close:
1623 break;
1624 default:
1625 break;
1626 }
1627 return window_event_result::ignored;
1628 }
1629
1630 int nm_messagebox(const menu_title title, const nm_messagebox_tie &tie, const char *format, ...)
1631 {
1632 va_list args;
1633 char nm_text[MESSAGEBOX_TEXT_SIZE];
1634 va_start(args, format);
1635 vsnprintf(nm_text,sizeof(nm_text),format,args);
1636 va_end(args);
1637 return nm_messagebox_str(title, tie, menu_subtitle{nm_text});
1638 }
1639
1640 namespace dcx {
1641
1642 int nm_messagebox_str(const menu_title title, const nm_messagebox_tie &tie, const menu_subtitle subtitle)
1643 {
1644 auto wind = window_create<messagebox_newmenu>(title, subtitle, tie, grd_curscreen->sc_canvas);
1645 return newmenu::process_until_closed(wind);
1646 }
1647
1648 messagebox_newmenu::messagebox_newmenu(const menu_title title, const menu_subtitle subtitle, const nm_messagebox_tie &tie, grs_canvas &canvas) :
1649 newmenu(title, subtitle, menu_filename{nullptr}, tiny_mode_flag::normal, tab_processing_flag::ignore, create_adjusted_citem(*this, tie), canvas)
1650 {
1651 }
1652
1653 messagebox_newmenu::adjusted_citem messagebox_newmenu::create_adjusted_citem(std::array<newmenu_item, nm_messagebox_tie::maximum_arity> &items, const nm_messagebox_tie &tie)
1654 {
1655 auto &&item_range = partial_range(items, tie.count());
1656 for (auto &&[i, s] : zip(item_range, tie))
1657 nm_set_item_menu(i, s);
1658 return adjusted_citem::create(item_range, 0);
1659 }
1660
1661 // Example listbox callback function...
1662 // int lb_callback( int * citem, int *nitems, char * items[], int *keypress )
1663 // {
1664 // int i;
1665 //
1666 // if ( *keypress = KEY_CTRLED+KEY_D ) {
1667 // if ( *nitems > 1 ) {
1668 // PHYSFS_delete(items[*citem]); // Delete the file
1669 // for (i=*citem; i<*nitems-1; i++ ) {
1670 // items[i] = items[i+1];
1671 // }
1672 // *nitems = *nitems - 1;
1673 // d_free( items[*nitems] );
1674 // items[*nitems] = NULL;
1675 // return 1; // redraw;
1676 // }
1677 // *keypress = 0;
1678 // }
1679 // return 0;
1680 // }
1681
1682 #define LB_ITEMS_ON_SCREEN 8
1683
1684 listbox::listbox(int citem, unsigned nitems, const char **item, menu_title title, grs_canvas &canvas, uint8_t allow_abort_flag) :
1685 listbox_layout{citem, nitems, item, title, canvas},
1686 window(canvas, box_x - BORDERX, box_y - title_height - BORDERY, box_w + 2 * BORDERX, height + 2 * BORDERY),
1687 allow_abort_flag(allow_abort_flag)
1688 {
1689 }
1690
1691 const char **listbox_get_items(listbox &lb)
1692 {
1693 return lb.item;
1694 }
1695
1696 int listbox_get_citem(listbox &lb)
1697 {
1698 return lb.citem;
1699 }
1700
1701 void listbox_delete_item(listbox &lb, int item)
1702 {
1703 Assert(item >= 0);
1704
1705 const auto nitems{lb.nitems};
1706 if (nitems <= 0)
1707 return;
1708 if (item < nitems - 1)
1709 {
1710 auto &items = lb.item;
1711 std::rotate(&items[item], &items[item + 1], &items[nitems]);
1712 }
1713 -- lb.nitems;
1714 if (lb.citem >= lb.nitems)
1715 lb.citem = lb.nitems ? lb.nitems - 1 : 0;
1716 }
1717
1718 }
1719
1720 namespace {
1721
1722 static void update_scroll_position(listbox_layout &lb)
1723 {
1724 if (lb.citem < 0)
1725 lb.citem = 0;
1726
1727 if (lb.citem >= lb.nitems)
1728 lb.citem = lb.nitems-1;
1729
1730 if (lb.citem < lb.first_item)
1731 lb.first_item = lb.citem;
1732
1733 if (lb.citem >= lb.items_on_screen)
1734 {
1735 if (lb.first_item < lb.citem - lb.items_on_screen + 1)
1736 lb.first_item = lb.citem - lb.items_on_screen + 1;
1737 }
1738
1739 if (lb.nitems <= lb.items_on_screen)
1740 lb.first_item = 0;
1741
1742 if (lb.nitems >= lb.items_on_screen)
1743 {
1744 if (lb.first_item > lb.nitems - lb.items_on_screen)
1745 lb.first_item = lb.nitems - lb.items_on_screen;
1746 }
1747 if (lb.first_item < 0)
1748 lb.first_item = 0;
1749 }
1750
1751 static window_event_result listbox_mouse(const d_event &event, listbox *lb, const mbtn button)
1752 {
1753 switch (button)
1754 {
1755 case mbtn::left:
1756 {
1757 if (lb->mouse_state)
1758 {
1759 const auto [mx, my, mz] = mouse_get_pos();
1760 const int x1{lb->box_x};
1761 if (mx <= x1)
1762 break;
1763 const int x2 = x1 + lb->box_w;
1764 if (mx >= x2)
1765 break;
1766 const int by{lb->box_y};
1767 if (my <= by)
1768 break;
1769 const int my_relative_by = my - by;
1770 const auto &&line_spacing = LINE_SPACING(*MEDIUM3_FONT, *GAME_FONT);
1771 if (line_spacing < 1)
1772 break;
1773 const int idx_relative_first_visible_item = my_relative_by / line_spacing;
1774 const auto first_visible_item = lb->first_item;
1775 const auto nitems = lb->nitems;
1776 const auto last_visible_item = std::min(first_visible_item + lb->items_on_screen, nitems);
1777 if (idx_relative_first_visible_item >= last_visible_item)
1778 break;
1779 const int px_within_item = my_relative_by % static_cast<int>(line_spacing);
1780 const auto h = gr_get_string_height(*MEDIUM3_FONT, 1);
1781 if (px_within_item >= h)
1782 break;
1783 const int idx_absolute_item = idx_relative_first_visible_item + first_visible_item;
1784 if (idx_absolute_item >= nitems)
1785 break;
1786 lb->citem = idx_absolute_item;
1787 }
1788 else if (event.type == event_type::mouse_button_up)
1789 {
1790 if (lb->citem < 0)
1791 return window_event_result::ignored;
1792
1793 const auto [mx, my, mz] = mouse_get_pos();
1794 const auto h = gr_get_string_size(*MEDIUM3_FONT, lb->item[lb->citem]).height;
1795 const int x1{lb->box_x};
1796 const int x2 = lb->box_x + lb->box_w;
1797 const int y1 = (lb->citem - lb->first_item) * LINE_SPACING(*MEDIUM3_FONT, *GAME_FONT) + lb->box_y;
1798 const int y2 = y1 + h;
1799 if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) )
1800 {
1801 // Tell callback, if it wants to close it will return window_event_result::close
1802 const d_select_event selected{lb->citem};
1803 return lb->callback_handler(selected, window_event_result::close);
1804 }
1805 }
1806 break;
1807 }
1808 case mbtn::right:
1809 {
1810 if (lb->allow_abort_flag && lb->mouse_state) {
1811 lb->citem = -1;
1812 return window_event_result::close;
1813 }
1814 break;
1815 }
1816 case mbtn::z_up:
1817 {
1818 if (lb->mouse_state)
1819 {
1820 lb->citem--;
1821 update_scroll_position(*lb);
1822 }
1823 break;
1824 }
1825 case mbtn::z_down:
1826 {
1827 if (lb->mouse_state)
1828 {
1829 lb->citem++;
1830 update_scroll_position(*lb);
1831 }
1832 break;
1833 }
1834 default:
1835 break;
1836 }
1837
1838 return window_event_result::ignored;
1839 }
1840
1841 static window_event_result listbox_key_command(const d_event &event, listbox *lb)
1842 {
1843 int key = event_key_get(event);
1844 window_event_result rval = window_event_result::handled;
1845
1846 switch(key) {
1847 case KEY_HOME:
1848 case KEY_PAD7:
1849 lb->citem = 0;
1850 break;
1851 case KEY_END:
1852 case KEY_PAD1:
1853 lb->citem = lb->nitems-1;
1854 break;
1855 case KEY_UP:
1856 case KEY_PAD8:
1857 lb->citem--;
1858 break;
1859 case KEY_DOWN:
1860 case KEY_PAD2:
1861 lb->citem++;
1862 break;
1863 case KEY_PAGEDOWN:
1864 case KEY_PAD3:
1865 lb->citem += lb->items_on_screen;
1866 break;
1867 case KEY_PAGEUP:
1868 case KEY_PAD9:
1869 lb->citem -= lb->items_on_screen;
1870 break;
1871 case KEY_ESC:
1872 if (lb->allow_abort_flag) {
1873 lb->citem = -1;
1874 return window_event_result::close;
1875 }
1876 break;
1877 case KEY_ENTER:
1878 case KEY_PADENTER:
1879 // Tell callback, if it wants to close it will return window_event_result::close
1880 {
1881 const d_select_event selected{lb->citem};
1882 return lb->callback_handler(selected, window_event_result::close);
1883 }
1884 default:
1885 {
1886 const unsigned ascii = key_ascii();
1887 if ( ascii < 255 ) {
1888 const unsigned upper_ascii = toupper(ascii);
1889 const auto nitems = lb->nitems;
1890 for (unsigned cc = lb->citem + 1, steps = 0; steps < nitems; ++steps, ++cc)
1891 {
1892 if (cc >= nitems)
1893 cc = 0;
1894 if (toupper(static_cast<unsigned>(lb->item[cc][0])) == upper_ascii)
1895 {
1896 lb->citem = cc;
1897 break;
1898 }
1899 }
1900 }
1901 rval = window_event_result::ignored;
1902 }
1903 }
1904 update_scroll_position(*lb);
1905 return rval;
1906 }
1907
1908 }
1909
1910 namespace dcx {
1911
1912 void listbox_layout::create_structure()
1913 {
1914 auto &canvas = parent_canvas;
1915
1916 auto &medium3_font = *MEDIUM3_FONT;
1917
1918 box_w = 0;
1919 const auto &&fspacx = FSPACX();
1920 const auto &&fspacx10 = fspacx(10);
1921 const auto border_x{BORDERX};
1922 const unsigned max_box_width = SWIDTH - (border_x * 2);
1923 unsigned marquee_maxchars = UINT_MAX;
1924 for (const auto i : unchecked_partial_range(item, nitems))
1925 {
1926 auto w = gr_get_string_size(medium3_font, i).width;
1927 w += fspacx10;
1928 if (w > max_box_width)
1929 {
1930 unsigned mmc{1};
1931 for (;; ++mmc)
1932 {
1933 const auto w2 = gr_get_string_size(medium3_font, i, mmc).width;
1934 if (w2 > max_box_width - fspacx10 || mmc > 128)
1935 break;
1936 }
1937 /* mmc is now the shortest initial subsequence that is wider
1938 * than max_box_width.
1939 *
1940 * Next, search for whether any internal subsequences of
1941 * lesser length are also too wide. This can happen if all
1942 * the initial characters are narrow, then characters
1943 * outside the initial subsequence are wide.
1944 */
1945 for (auto j = i;;)
1946 {
1947 const auto w2 = gr_get_string_size(medium3_font, j, mmc).width;
1948 if (w2 > max_box_width - fspacx10)
1949 {
1950 /* This subsequence is too long. Reduce the length
1951 * and retry.
1952 */
1953 if (!--mmc)
1954 break;
1955 }
1956 else
1957 {
1958 /* This subsequence fits. Move to the next
1959 * character.
1960 */
1961 if (!*++j)
1962 break;
1963 }
1964 }
1965 w = max_box_width;
1966 if (marquee_maxchars > mmc)
1967 marquee_maxchars = mmc;
1968 }
1969 if (box_w < w)
1970 box_w = w;
1971 }
1972
1973 {
1974 const auto &&[w, h] = gr_get_string_size(medium3_font, title);
1975 if (box_w < w)
1976 box_w = w;
1977 title_height = h+FSPACY(5);
1978 }
1979
1980 // The box is bigger than we can fit on the screen since at least one string is too long. Check how many chars we can fit on the screen (at least only - MEDIUM*_FONT is variable font!) so we can make a marquee-like effect.
1981 if (marquee_maxchars != UINT_MAX)
1982 {
1983 box_w = max_box_width;
1984 marquee = listbox::marquee::allocate(marquee_maxchars);
1985 marquee->lasttime = timer_query();
1986 }
1987
1988 const auto &&line_spacing = LINE_SPACING(medium3_font, *GAME_FONT);
1989 const auto border_y{BORDERY};
1990 const unsigned bordery2{border_y * 2u};
1991 const auto items_on_screen = std::max<unsigned>(
1992 std::min<unsigned>(((canvas.cv_bitmap.bm_h - bordery2 - title_height) / line_spacing) - 2, nitems),
1993 LB_ITEMS_ON_SCREEN);
1994 this->items_on_screen = items_on_screen;
1995 height = line_spacing * items_on_screen;
1996 const auto raw_box_x{(canvas.cv_bitmap.bm_w - box_w) / 2};
1997 box_x = std::cmp_greater(raw_box_x, border_x) ? raw_box_x : border_x;
1998 const auto raw_box_y{((canvas.cv_bitmap.bm_h - (height + title_height)) / 2) + title_height};
1999 const auto min_box_y{title_height + border_y};
2000 box_y = std::cmp_greater(raw_box_y, min_box_y) ? raw_box_y : min_box_y;
2001 if (box_y < bordery2)
2002 box_y = bordery2;
2003
2004 if (citem < 0)
2005 citem = 0;
2006 else if (citem >= nitems)
2007 citem = 0;
2008
2009 first_item = 0;
2010 update_scroll_position(*this);
2011
2012 swidth = SWIDTH;
2013 sheight = SHEIGHT;
2014 fntscalex = FNTScaleX;
2015 fntscaley = FNTScaleY;
2016 }
2017
2018 }
2019
2020 namespace {
2021
2022 static window_event_result listbox_draw(listbox *lb)
2023 {
2024 if (lb->swidth != SWIDTH || lb->sheight != SHEIGHT || lb->fntscalex != FNTScaleX || lb->fntscaley != FNTScaleY)
2025 lb->create_structure();
2026
2027 auto &canvas = lb->parent_canvas;
2028 nm_draw_background(canvas, lb->box_x - BORDERX, lb->box_y - lb->title_height - BORDERY,lb->box_x + lb->box_w + BORDERX, lb->box_y + lb->height + BORDERY);
2029 auto &medium3_font = *MEDIUM3_FONT;
2030 gr_string(canvas, medium3_font, 0x8000, lb->box_y - lb->title_height, lb->title);
2031
2032 const auto &&line_spacing = LINE_SPACING(medium3_font, *GAME_FONT);
2033 for (int i = lb->first_item; i < lb->first_item + lb->items_on_screen; ++i)
2034 {
2035 int y = (i - lb->first_item) * line_spacing + lb->box_y;
2036 const auto &&fspacx = FSPACX();
2037 const auto &&fspacy = FSPACY();
2038 const uint8_t color5 = BM_XRGB(5, 5, 5);
2039 const uint8_t color2 = BM_XRGB(2, 2, 2);
2040 const uint8_t color0 = BM_XRGB(0, 0, 0);
2041 if ( i >= lb->nitems ) {
2042 gr_rect(canvas, lb->box_x + lb->box_w - fspacx(1), y - fspacy(1), lb->box_x + lb->box_w, y + line_spacing, color5);
2043 gr_rect(canvas, lb->box_x - fspacx(1), y - fspacy(1), lb->box_x, y + line_spacing, color2);
2044 gr_rect(canvas, lb->box_x, y - fspacy(1), lb->box_x + lb->box_w - fspacx(1), y + line_spacing, color0);
2045 } else {
2046 auto &mediumX_font = *(i == lb->citem ? MEDIUM2_FONT : MEDIUM1_FONT);
2047 gr_rect(canvas, lb->box_x + lb->box_w - fspacx(1), y - fspacy(1), lb->box_x + lb->box_w, y + line_spacing, color5);
2048 gr_rect(canvas, lb->box_x - fspacx(1), y - fspacy(1), lb->box_x, y + line_spacing, color2);
2049 gr_rect(canvas, lb->box_x, y - fspacy(1), lb->box_x + lb->box_w - fspacx(1), y + line_spacing, color0);
2050
2051 const char *showstr;
2052 std::size_t item_len;
2053 if (lb->marquee && (item_len = strlen(lb->item[i])) > lb->marquee->maxchars)
2054 {
2055 showstr = lb->marquee->text;
2056 static int prev_citem = -1;
2057
2058 if (prev_citem != lb->citem)
2059 {
2060 lb->marquee->pos = lb->marquee->scrollback = 0;
2061 lb->marquee->lasttime = timer_query();
2062 prev_citem = lb->citem;
2063 }
2064
2065 unsigned srcoffset{0};
2066 if (i == lb->citem)
2067 {
2068 const auto tq = timer_query();
2069 if (lb->marquee->lasttime + (F1_0 / 3) < tq)
2070 {
2071 lb->marquee->pos += lb->marquee->scrollback ? -1 : 1;
2072 lb->marquee->lasttime = tq;
2073 }
2074 if (lb->marquee->pos < 0) // reached beginning of string -> scroll forward
2075 {
2076 lb->marquee->pos = 0;
2077 lb->marquee->scrollback = 0;
2078 }
2079 if (lb->marquee->pos + lb->marquee->maxchars - 1 > item_len) // reached end of string -> scroll backward
2080 {
2081 lb->marquee->pos = item_len - lb->marquee->maxchars + 1;
2082 lb->marquee->scrollback = 1;
2083 }
2084 srcoffset = lb->marquee->pos;
2085 }
2086 snprintf(lb->marquee->text, lb->marquee->maxchars, "%s", lb->item[i] + srcoffset);
2087 }
2088 else
2089 {
2090 showstr = lb->item[i];
2091 }
2092 gr_string(canvas, mediumX_font, lb->box_x + fspacx(5), y, showstr);
2093 }
2094 }
2095
2096 return lb->callback_handler(d_event{event_type::newmenu_draw}, window_event_result::handled);
2097 }
2098
2099 }
2100
2101 window_event_result listbox::event_handler(const d_event &event)
2102 {
2103 if (const auto rval = callback_handler(event, window_event_result::ignored); rval != window_event_result::ignored)
2104 return rval; // event handled
2105
2106 #if DXX_MAX_BUTTONS_PER_JOYSTICK
2107 if (joy_translate_menu_key(event))
2108 return window_event_result::handled;
2109 #endif
2110
2111 switch (event.type)
2112 {
2113 case event_type::window_activated:
2114 game_flush_inputs(Controls);
2115 event_toggle_focus(0);
2116 key_toggle_repeat(1);
2117 break;
2118
2119 case event_type::window_deactivated:
2120 //event_toggle_focus(1); // No cursor recentering
2121 key_toggle_repeat(0);
2122 break;
2123
2124 case event_type::mouse_button_down:
2125 case event_type::mouse_button_up:
2126 mouse_state = event.type == event_type::mouse_button_down;
2127 return listbox_mouse(event, this, event_mouse_get_button(event));
2128 case event_type::key_command:
2129 return listbox_key_command(event, this);
2130 case event_type::idle:
2131 if (!(Game_mode & GM_MULTI && Game_wind))
2132 timer_delay2(CGameArg.SysMaxFPS);
2133 return window_event_result::ignored;
2134 case event_type::window_draw:
2135 return listbox_draw(this);
2136 case event_type::window_close:
2137 break;
2138 default:
2139 break;
2140 }
2141 return window_event_result::ignored;
2142 }