summaryrefslogtreecommitdiffstatshomepage
path: root/src/frontend/mame/ui/menu.cpp
blob: 9e98511a8825f4b697c7e8735771a3237a23ff83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
// license:BSD-3-Clause
// copyright-holders:Nicola Salmoria, Aaron Giles, Nathan Woods, Maurizio Petrarota
/*********************************************************************

    ui/menu.cpp

    Internal MAME menus for the user interface.

*********************************************************************/

#include "emu.h"
#include "ui/menu.h"

#include "ui/ui.h"
#include "ui/mainmenu.h"
#include "ui/utils.h"
#include "ui/miscmenu.h"

#include "cheat.h"
#include "mame.h"

#include "corestr.h"
#include "drivenum.h"
#include "rendutil.h"
#include "uiinput.h"

#include <algorithm>
#include <cassert>
#include <cmath>
#include <utility>


namespace ui {

/***************************************************************************
    GLOBAL VARIABLES
***************************************************************************/

std::mutex menu::s_global_state_guard;
menu::global_state_map menu::s_global_states;

/***************************************************************************
    INLINE FUNCTIONS
***************************************************************************/

menu::global_state_ptr menu::get_global_state(running_machine &machine)
{
	std::lock_guard<std::mutex> guard(s_global_state_guard);
	auto const it(s_global_states.find(&machine));
	return (it != s_global_states.end()) ? it->second : global_state_ptr();
}

//-------------------------------------------------
//  exclusive_input_pressed - return true if the
//  given key is pressed and we haven't already
//  reported a key
//-------------------------------------------------

bool menu::exclusive_input_pressed(int &iptkey, int key, int repeat)
{
	if ((iptkey == IPT_INVALID) && machine().ui_input().pressed_repeat(key, repeat))
	{
		iptkey = key;
		return true;
	}
	else
	{
		return false;
	}
}



/***************************************************************************
    CORE SYSTEM MANAGEMENT
***************************************************************************/

menu::global_state::global_state(running_machine &machine, ui_options const &options)
	: widgets_manager(machine)
	, m_machine(machine)
	, m_bgrnd_bitmap()
	, m_bgrnd_texture(nullptr, machine.render())
	, m_stack()
	, m_free()
{
	render_manager &render(machine.render());

	// create a texture for main menu background
	m_bgrnd_texture.reset(render.texture_alloc(render_texture::hq_scale));
	if (options.use_background_image() && (&machine.system() == &GAME_NAME(___empty)))
	{
		m_bgrnd_bitmap = std::make_unique<bitmap_argb32>(0, 0);
		emu_file backgroundfile(".", OPEN_FLAG_READ);
		if (!backgroundfile.open("background.jpg"))
		{
			render_load_jpeg(*m_bgrnd_bitmap, backgroundfile);
			backgroundfile.close();
		}

		if (!m_bgrnd_bitmap->valid() && !backgroundfile.open("background.png"))
		{
			render_load_png(*m_bgrnd_bitmap, backgroundfile);
			backgroundfile.close();
		}

		if (m_bgrnd_bitmap->valid())
			m_bgrnd_texture->set_bitmap(*m_bgrnd_bitmap, m_bgrnd_bitmap->cliprect(), TEXFORMAT_ARGB32);
		else
			m_bgrnd_bitmap->reset();
	}
}


menu::global_state::~global_state()
{
	// it shouldn't really be possible to get here with active menus because of reference loops
	assert(!m_stack);
	assert(!m_free);

	stack_reset();
	clear_free_list();
}


void menu::global_state::stack_push(std::unique_ptr<menu> &&menu)
{
	menu->m_parent = std::move(m_stack);
	m_stack = std::move(menu);
	m_stack->machine().ui_input().reset();
}


void menu::global_state::stack_pop()
{
	if (m_stack)
	{
		std::unique_ptr<menu> menu(std::move(m_stack));
		m_stack = std::move(menu->m_parent);
		menu->m_parent = std::move(m_free);
		m_free = std::move(menu);
		m_machine.ui_input().reset();
	}
}


void menu::global_state::stack_reset()
{
	while (m_stack)
		stack_pop();
}


void menu::global_state::clear_free_list()
{
	// free stack is in reverse order - unwind it properly
	std::unique_ptr<menu> reversed;
	while (m_free)
	{
		std::unique_ptr<menu> menu(std::move(m_free));
		m_free = std::move(menu->m_parent);
		menu->m_parent = std::move(reversed);
		reversed = std::move(menu);
	}
	while (reversed)
		reversed = std::move(reversed->m_parent);
}


bool menu::global_state::stack_has_special_main_menu() const
{
	for (auto menu = m_stack.get(); menu != nullptr; menu = menu->m_parent.get())
	{
		if (menu->is_special_main_menu())
			return true;
	}
	return false;
}




//-------------------------------------------------
//  init - initialize the menu system
//-------------------------------------------------

void menu::init(running_machine &machine, ui_options &mopt)
{
	// initialize the menu stack
	{
		std::lock_guard<std::mutex> guard(s_global_state_guard);
		auto const ins(s_global_states.emplace(&machine, std::make_shared<global_state>(machine, mopt)));
		assert(ins.second); // calling init twice is bad
		if (ins.second)
			machine.add_notifier(MACHINE_NOTIFY_EXIT, machine_notify_delegate(&menu::exit, &machine)); // add an exit callback to free memory
		else
			ins.first->second->stack_reset();
	}
}


//-------------------------------------------------
//  exit - clean up after ourselves
//-------------------------------------------------

void menu::exit(running_machine &machine)
{
	// free menus
	global_state_ptr const state(get_global_state(machine));
	state->stack_reset();
	state->clear_free_list();

	std::lock_guard<std::mutex> guard(s_global_state_guard);
	s_global_states.erase(&machine);
}



/***************************************************************************
    CORE MENU MANAGEMENT
***************************************************************************/

//-------------------------------------------------
//  menu - menu constructor
//-------------------------------------------------

menu::menu(mame_ui_manager &mui, render_container &container)
	: m_selected(0)
	, m_items()
	, m_visible_lines(0)
	, m_visible_items(0)
	, m_global_state(get_global_state(mui.machine()))
	, m_special_main_menu(false)
	, m_needs_prev_menu_item(true)
	, m_ui(mui)
	, m_container(container)
	, m_parent()
	, m_event()
	, m_customtop(0.0f)
	, m_custombottom(0.0f)
	, m_resetpos(0)
	, m_resetref(nullptr)
	, m_mouse_hit(false)
	, m_mouse_button(false)
	, m_mouse_x(-1.0f)
	, m_mouse_y(-1.0f)
{
	assert(m_global_state); // not calling init is bad

	reset(reset_options::SELECT_FIRST);

	top_line = 0;
}


//-------------------------------------------------
//  ~menu - menu destructor
//-------------------------------------------------

menu::~menu()
{
}


//-------------------------------------------------
//  reset - free all items in the menu
//-------------------------------------------------

void menu::reset(reset_options options)
{
	// based on the reset option, set the reset info
	m_resetpos = 0;
	m_resetref = nullptr;
	if (options == reset_options::REMEMBER_POSITION)
		m_resetpos = m_selected;
	else if (options == reset_options::REMEMBER_REF)
		m_resetref = get_selection_ref();

	// reset the item count back to 0
	m_items.clear();
	m_visible_items = 0;
	m_selected = 0;
}


//-------------------------------------------------
//  is_special_main_menu - returns whether the
//  menu has special needs
//-------------------------------------------------

bool menu::is_special_main_menu() const
{
	return m_special_main_menu;
}


//-------------------------------------------------
//  set_special_main_menu - set whether the
//  menu has special needs
//-------------------------------------------------

void menu::set_special_main_menu(bool special)
{
	m_special_main_menu = special;
}


//-------------------------------------------------
//  item_append - append a new item to the
//  end of the menu
//-------------------------------------------------

void menu::item_append(menu_item_type type, uint32_t flags)
{
	if (type == menu_item_type::SEPARATOR)
		item_append(MENU_SEPARATOR_ITEM, flags, nullptr, menu_item_type::SEPARATOR);
}

//-------------------------------------------------
//  item_append - append a new item to the
//  end of the menu
//-------------------------------------------------

void menu::item_append(std::string &&text, std::string &&subtext, uint32_t flags, void *ref, menu_item_type type)
{
	if ((flags & FLAG_MULTILINE) != 0) // only allow multiline as the first item
		assert(m_items.size() == 1);
	else if (m_items.size() >= 2) // only allow a single multi-line item
		assert((m_items[0].flags & FLAG_MULTILINE) == 0);

	// allocate a new item and populate it
	menu_item pitem;
	pitem.text = std::move(text);
	pitem.subtext = std::move(subtext);
	pitem.flags = flags;
	pitem.ref = ref;
	pitem.type = type;

	// append to array
	auto index = m_items.size();
	if (!m_items.empty() && m_needs_prev_menu_item)
	{
		m_items.emplace(m_items.end() - 1, std::move(pitem));
		--index;
	}
	else
		m_items.emplace_back(std::move(pitem));

	// update the selection if we need to
	if (m_resetpos == index || (m_resetref != nullptr && m_resetref == ref))
		m_selected = index;
	if (m_resetpos == (m_items.size() - 1))
		m_selected = m_items.size() - 1;
}


//-------------------------------------------------
//  item_append_on_off - append a new "On"/"Off"
//  item to the end of the menu
//-------------------------------------------------

void menu::item_append_on_off(const std::string &text, bool state, uint32_t flags, void *ref, menu_item_type type)
{
	if (flags & FLAG_DISABLE)
		ref = nullptr;
	else
		flags |= state ? FLAG_LEFT_ARROW : FLAG_RIGHT_ARROW;

	item_append(std::string(text), state ? _("On") : _("Off"), flags, ref, type);
}


//-------------------------------------------------
//  process - process a menu, drawing it
//  and returning any interesting events
//-------------------------------------------------

const menu::event *menu::process(uint32_t flags, float x0, float y0)
{
	// reset the event
	m_event.iptkey = IPT_INVALID;

	// first make sure our selection is valid
	validate_selection(1);

	// draw the menu
	if (m_items.size() > 1 && (m_items[0].flags & FLAG_MULTILINE) != 0)
		draw_text_box();
	else
		draw(flags);

	// process input
	if (!(flags & PROCESS_NOKEYS) && !(flags & PROCESS_NOINPUT))
	{
		// read events
		handle_events(flags, m_event);

		// handle the keys if we don't already have an event
		if (m_event.iptkey == IPT_INVALID)
			handle_keys(flags, m_event.iptkey);
	}

	// update the selected item in the event
	if ((m_event.iptkey != IPT_INVALID) && selection_valid())
	{
		m_event.itemref = get_selection_ref();
		m_event.type = m_items[m_selected].type;
		return &m_event;
	}
	else
	{
		return nullptr;
	}
}


//-------------------------------------------------
//  set_selection - changes the index
//  of the currently selected menu item
//-------------------------------------------------

void menu::set_selection(void *selected_itemref)
{
	m_selected = -1;
	for (int itemnum = 0; itemnum < m_items.size(); itemnum++)
	{
		if (m_items[itemnum].ref == selected_itemref)
		{
			m_selected = itemnum;
			break;
		}
	}
}



/***************************************************************************
    INTERNAL MENU PROCESSING
***************************************************************************/

//-------------------------------------------------
//  draw - draw a menu
//-------------------------------------------------

void menu::draw(uint32_t flags)
{
	// first draw the FPS counter
	if (ui().show_fps_counter())
	{
		ui().draw_text_full(
				container(),
				machine().video().speed_text(),
				0.0f, 0.0f, 1.0f,
				text_layout::text_justify::RIGHT, text_layout::word_wrapping::WORD,
				mame_ui_manager::OPAQUE_, rgb_t::white(), rgb_t::black(),
				nullptr, nullptr);
	}

	bool const customonly = (flags & PROCESS_CUSTOM_ONLY);
	bool const noimage = (flags & PROCESS_NOIMAGE);
	bool const noinput = (flags & PROCESS_NOINPUT);
	float const aspect = machine().render().ui_aspect(&container());
	float const line_height = ui().get_line_height();
	float const lr_arrow_width = 0.4f * line_height * aspect;
	float const ud_arrow_width = line_height * aspect;
	float const gutter_width = lr_arrow_width * 1.3f;
	float const lr_border = ui().box_lr_border() * aspect;

	if (&machine().system() == &GAME_NAME(___empty) && !noimage)
		draw_background();

	// compute the width and height of the full menu
	float visible_width = 0;
	float visible_main_menu_height = 0;
	for (auto const &pitem : m_items)
	{
		// compute width of left hand side
		float total_width = gutter_width + ui().get_string_width(pitem.text) + gutter_width;

		// add in width of right hand side
		if (!pitem.subtext.empty())
			total_width += 2.0f * gutter_width + ui().get_string_width(pitem.subtext);

		// track the maximum
		if (total_width > visible_width)
			visible_width = total_width;

		// track the height as well
		visible_main_menu_height += line_height;
	}

	// account for extra space at the top and bottom
	float const visible_extra_menu_height = m_customtop + m_custombottom;

	// add a little bit of slop for rounding
	visible_width += 0.01f;
	visible_main_menu_height += 0.01f;

	// if we are too wide or too tall, clamp it down
	if (visible_width + 2.0f * lr_border > 1.0f)
		visible_width = 1.0f - 2.0f * lr_border;

	// if the menu and extra menu won't fit, take away part of the regular menu, it will scroll
	if (visible_main_menu_height + visible_extra_menu_height + 2.0f * ui().box_tb_border() > 1.0f)
		visible_main_menu_height = 1.0f - 2.0f * ui().box_tb_border() - visible_extra_menu_height;

	m_visible_lines = std::min(int(std::floor(visible_main_menu_height / line_height)), int(unsigned(m_items.size())));
	visible_main_menu_height = float(m_visible_lines) * line_height;

	// compute top/left of inner menu area by centering
	float const visible_left = (1.0f - visible_width) * 0.5f;
	float const visible_top = ((1.0f - visible_main_menu_height - visible_extra_menu_height) * 0.5f) + m_customtop;

	// first add us a box
	float const x1 = visible_left - lr_border;
	float const y1 = visible_top - ui().box_tb_border();
	float const x2 = visible_left + visible_width + lr_border;
	float const y2 = visible_top + visible_main_menu_height + ui().box_tb_border();
	if (!customonly)
		ui().draw_outlined_box(container(), x1, y1, x2, y2, ui().colors().background_color());

	if ((m_selected >= (top_line + m_visible_lines)) || (m_selected < (top_line + 1)))
		top_line = m_selected - (m_visible_lines / 2);
	if (top_line < 0 || is_first_selected())
		top_line = 0;
	else if ((top_line > (m_items.size() - m_visible_lines)) || is_last_selected())
		top_line = m_items.size() - m_visible_lines;
	else if (m_selected >= (top_line + m_visible_lines - 2))
		top_line = m_selected - m_visible_lines + ((m_selected == (m_items.size() - 1)) ? 1: 2);

	// if scrolling, show arrows
	bool const show_top_arrow((m_items.size() > m_visible_lines) && !first_item_visible());
	bool const show_bottom_arrow((m_items.size() > m_visible_lines) && !last_item_visible());

	// set the number of visible lines, minus 1 for top arrow and 1 for bottom arrow
	m_visible_items = m_visible_lines - (show_top_arrow ? 1 : 0) - (show_bottom_arrow ? 1 : 0);

	// determine effective positions taking into account the hilighting arrows
	float const effective_width = visible_width - 2.0f * gutter_width;
	float const effective_left = visible_left + gutter_width;

	// locate mouse
	if (!customonly && !noinput)
		map_mouse();
	else
		ignore_mouse();

	// loop over visible lines
	m_hover = m_items.size() + 1;
	bool selected_subitem_too_big = false;
	float const line_x0 = x1 + 0.5f * UI_LINE_WIDTH;
	float const line_x1 = x2 - 0.5f * UI_LINE_WIDTH;
	if (!customonly)
	{
		for (int linenum = 0; linenum < m_visible_lines; linenum++)
		{
			auto const itemnum = top_line + linenum;
			menu_item const &pitem = m_items[itemnum];
			std::string_view const itemtext = pitem.text;
			rgb_t fgcolor = ui().colors().text_color();
			rgb_t bgcolor = ui().colors().text_bg_color();
			rgb_t fgcolor2 = ui().colors().subitem_color();
			rgb_t fgcolor3 = ui().colors().clone_color();
			float const line_y0 = visible_top + (float)linenum * line_height;
			float const line_y1 = line_y0 + line_height;

			// work out what we're dealing with
			bool const uparrow = !linenum && show_top_arrow;
			bool const downarrow = (linenum == (m_visible_lines - 1)) && show_bottom_arrow;

			// set the hover if this is our item
			bool const hovered = mouse_in_rect(line_x0, line_y0, line_x1, line_y1);
			if (hovered)
			{
				if (uparrow)
					m_hover = HOVER_ARROW_UP;
				else if (downarrow)
					m_hover = HOVER_ARROW_DOWN;
				else if (is_selectable(pitem))
					m_hover = itemnum;
			}

			if (is_selected(itemnum))
			{
				// if we're selected, draw with a different background
				fgcolor = fgcolor2 = fgcolor3 = ui().colors().selected_color();
				bgcolor = ui().colors().selected_bg_color();
			}
			else if (hovered && (uparrow || downarrow || is_selectable(pitem)))
			{
				// else if the mouse is over this item, draw with a different background
				fgcolor = fgcolor2 = fgcolor3 = ui().colors().mouseover_color();
				bgcolor = ui().colors().mouseover_bg_color();
			}

			// if we have some background hilighting to do, add a quad behind everything else
			if (bgcolor != ui().colors().text_bg_color())
				highlight(line_x0, line_y0, line_x1, line_y1, bgcolor);

			if (uparrow)
			{
				// if we're on the top line, display the up arrow
				draw_arrow(
							0.5f * (x1 + x2) - 0.5f * ud_arrow_width,
							line_y0 + 0.25f * line_height,
							0.5f * (x1 + x2) + 0.5f * ud_arrow_width,
							line_y0 + 0.75f * line_height,
							fgcolor,
							ROT0);
			}
			else if (downarrow)
			{
				// if we're on the bottom line, display the down arrow
				draw_arrow(
							0.5f * (x1 + x2) - 0.5f * ud_arrow_width,
							line_y0 + 0.25f * line_height,
							0.5f * (x1 + x2) + 0.5f * ud_arrow_width,
							line_y0 + 0.75f * line_height,
							fgcolor,
							ROT0 ^ ORIENTATION_FLIP_Y);
			}
			else if (pitem.type == menu_item_type::SEPARATOR)
			{
				// if we're just a divider, draw a line
				container().add_line(visible_left, line_y0 + 0.5f * line_height, visible_left + visible_width, line_y0 + 0.5f * line_height, UI_LINE_WIDTH, ui().colors().border_color(), PRIMFLAG_BLENDMODE(BLENDMODE_ALPHA));
			}
			else if (pitem.subtext.empty())
			{
				// if we don't have a subitem, just draw the string centered
				if (pitem.flags & FLAG_UI_HEADING)
				{
					float heading_width = ui().get_string_width(itemtext);
					container().add_line(visible_left, line_y0 + 0.5f * line_height, visible_left + ((visible_width - heading_width) / 2) - lr_border, line_y0 + 0.5f * line_height, UI_LINE_WIDTH, ui().colors().border_color(), PRIMFLAG_BLENDMODE(BLENDMODE_ALPHA));
					container().add_line(visible_left + visible_width - ((visible_width - heading_width) / 2) + lr_border, line_y0 + 0.5f * line_height, visible_left + visible_width, line_y0 + 0.5f * line_height, UI_LINE_WIDTH, ui().colors().border_color(), PRIMFLAG_BLENDMODE(BLENDMODE_ALPHA));
				}
				ui().draw_text_full(
						container(),
						itemtext,
						effective_left, line_y0, effective_width,
						text_layout::text_justify::CENTER, text_layout::word_wrapping::TRUNCATE,
						mame_ui_manager::NORMAL, fgcolor, bgcolor,
						nullptr, nullptr);
			}
			else
			{
				// otherwise, draw the item on the left and the subitem text on the right
				bool const subitem_invert(pitem.flags & FLAG_INVERT);
				float item_width, subitem_width;

				// draw the left-side text
				ui().draw_text_full(
						container(),
						itemtext,
						effective_left, line_y0, effective_width,
						text_layout::text_justify::LEFT, text_layout::word_wrapping::TRUNCATE,
						mame_ui_manager::NORMAL, fgcolor, bgcolor,
						&item_width, nullptr);

				if (pitem.flags & FLAG_COLOR_BOX)
				{
					rgb_t color = rgb_t((uint32_t)strtoul(pitem.subtext.c_str(), nullptr, 16));

					// give 2 spaces worth of padding
					subitem_width = ui().get_string_width("FF00FF00");

					ui().draw_outlined_box(
							container(),
							effective_left + effective_width - subitem_width, line_y0 + (UI_LINE_WIDTH * 2.0f),
							effective_left + effective_width, line_y1 - (UI_LINE_WIDTH * 2.0f),
							color);
				}
				else
				{
					std::string_view subitem_text(pitem.subtext);

					// give 2 spaces worth of padding
					item_width += 2.0f * gutter_width;

					// if the subitem doesn't fit here, display dots
					if (ui().get_string_width(subitem_text) > effective_width - item_width)
					{
						subitem_text = "...";
						if (is_selected(itemnum))
							selected_subitem_too_big = true;
					}

					// customize subitem text color
					if (!core_stricmp(pitem.subtext.c_str(), _("On")))
						fgcolor2 = rgb_t(0x00,0xff,0x00);

					if (!core_stricmp(pitem.subtext.c_str(), _("Off")))
						fgcolor2 = rgb_t(0xff,0x00,0x00);

					if (!core_stricmp(pitem.subtext.c_str(), _("Auto")))
						fgcolor2 = rgb_t(0xff,0xff,0x00);

					// draw the subitem right-justified
					ui().draw_text_full(
							container(),
							subitem_text,
							effective_left + item_width, line_y0, effective_width - item_width,
							text_layout::text_justify::RIGHT, text_layout::word_wrapping::TRUNCATE,
							mame_ui_manager::NORMAL, subitem_invert ? fgcolor3 : fgcolor2, bgcolor,
							&subitem_width, nullptr);
				}

				// apply arrows
				if (is_selected(itemnum) && (pitem.flags & FLAG_LEFT_ARROW))
				{
					float const l = effective_left + effective_width - subitem_width - gutter_width;
					float const r = l + lr_arrow_width;
					draw_arrow(
								l, line_y0 + 0.1f * line_height, r, line_y0 + 0.9f * line_height,
								fgcolor,
								ROT90 ^ ORIENTATION_FLIP_X);
					if (mouse_in_rect(l, line_y0 + 0.1f * line_height, r, line_y0 + 0.9f * line_height))
						m_hover = HOVER_UI_LEFT;
				}
				if (is_selected(itemnum) && (pitem.flags & FLAG_RIGHT_ARROW))
				{
					float const r = effective_left + effective_width + gutter_width;
					float const l = r - lr_arrow_width;
					draw_arrow(
								l, line_y0 + 0.1f * line_height, r, line_y0 + 0.9f * line_height,
								fgcolor,
								ROT90);
					if (mouse_in_rect(l, line_y0 + 0.1f * line_height, r, line_y0 + 0.9f * line_height))
						m_hover = HOVER_UI_RIGHT;
				}
			}
		}
	}

	// if the selected subitem is too big, display it in a separate offset box
	if (selected_subitem_too_big)
	{
		menu_item const &pitem = selected_item();
		bool const subitem_invert(pitem.flags & FLAG_INVERT);
		auto const linenum = m_selected - top_line;
		float const line_y = visible_top + (float)linenum * line_height;
		float target_width, target_height;

		// compute the multi-line target width/height
		ui().draw_text_full(
				container(),
				pitem.subtext,
				0, 0, visible_width * 0.75f,
				text_layout::text_justify::RIGHT, text_layout::word_wrapping::WORD,
				mame_ui_manager::NONE, rgb_t::white(), rgb_t::black(),
				&target_width, &target_height);

		// determine the target location
		float const target_x = visible_left + visible_width - target_width - lr_border;
		float target_y = line_y + line_height + ui().box_tb_border();
		if (target_y + target_height + ui().box_tb_border() > visible_main_menu_height)
			target_y = line_y - target_height - ui().box_tb_border();

		// add a box around that
		ui().draw_outlined_box(
				container(),
				target_x - lr_border, target_y - ui().box_tb_border(),
				target_x + target_width + lr_border, target_y + target_height + ui().box_tb_border(),
				subitem_invert ? ui().colors().selected_bg_color() : ui().colors().background_color());

		ui().draw_text_full(
				container(),
				pitem.subtext,
				target_x, target_y, target_width,
				text_layout::text_justify::RIGHT, text_layout::word_wrapping::WORD,
				mame_ui_manager::NORMAL, ui().colors().selected_color(), ui().colors().selected_bg_color(),
				nullptr, nullptr);
	}

	// if there is something special to add, do it by calling the virtual method
	custom_render(get_selection_ref(), m_customtop, m_custombottom, x1, y1, x2, y2);
}

void menu::custom_render(void *selectedref, float top, float bottom, float x, float y, float x2, float y2)
{
}

//-------------------------------------------------
//  draw_text_box - draw a multiline
//  word-wrapped text box with a menu item at the
//  bottom
//-------------------------------------------------

void menu::draw_text_box()
{
	std::string_view const text = m_items[0].text;
	std::string_view const backtext = m_items[1].text;
	float const aspect = machine().render().ui_aspect(&container());
	float line_height = ui().get_line_height();
	float lr_arrow_width = 0.4f * line_height * aspect;
	float gutter_width = lr_arrow_width;
	float const lr_border = ui().box_lr_border() * aspect;
	float target_width, target_height, prior_width;
	float target_x, target_y;

	// compute the multi-line target width/height
	ui().draw_text_full(
			container(),
			text,
			0, 0, 1.0f - 2.0f * lr_border - 2.0f * gutter_width,
			text_layout::text_justify::LEFT, text_layout::word_wrapping::WORD,
			mame_ui_manager::NONE, rgb_t::white(), rgb_t::black(),
			&target_width, &target_height);
	target_height += 2.0f * line_height;
	if (target_height > 1.0f - 2.0f * ui().box_tb_border())
		target_height = floorf((1.0f - 2.0f * ui().box_tb_border()) / line_height) * line_height;

	// maximum against "return to prior menu" text
	prior_width = ui().get_string_width(backtext) + 2.0f * gutter_width;
	target_width = std::max(target_width, prior_width);

	// determine the target location
	target_x = 0.5f - 0.5f * target_width;
	target_y = 0.5f - 0.5f * target_height;

	// make sure we stay on-screen
	if (target_x < lr_border + gutter_width)
		target_x = lr_border + gutter_width;
	if (target_x + target_width + gutter_width + lr_border > 1.0f)
		target_x = 1.0f - lr_border - gutter_width - target_width;
	if (target_y < ui().box_tb_border())
		target_y = ui().box_tb_border();
	if (target_y + target_height + ui().box_tb_border() > 1.0f)
		target_y = 1.0f - ui().box_tb_border() - target_height;

	// add a box around that
	ui().draw_outlined_box(
			container(),
			target_x - lr_border - gutter_width, target_y - ui().box_tb_border(),
			target_x + target_width + gutter_width + lr_border, target_y + target_height + ui().box_tb_border(),
			(m_items[0].flags & FLAG_REDTEXT) ?  UI_RED_COLOR : ui().colors().background_color());
	ui().draw_text_full(
			container(),
			text,
			target_x, target_y, target_width,
			text_layout::text_justify::LEFT, text_layout::word_wrapping::WORD,
			mame_ui_manager::NORMAL, ui().colors().text_color(), ui().colors().text_bg_color(),
			nullptr, nullptr);

	// draw the "return to prior menu" text with a hilight behind it
	highlight(
			target_x + 0.5f * UI_LINE_WIDTH,
			target_y + target_height - line_height,
			target_x + target_width - 0.5f * UI_LINE_WIDTH,
			target_y + target_height,
			ui().colors().selected_bg_color());
	ui().draw_text_full(
			container(),
			backtext,
			target_x, target_y + target_height - line_height, target_width,
			text_layout::text_justify::CENTER, text_layout::word_wrapping::TRUNCATE,
			mame_ui_manager::NORMAL, ui().colors().selected_color(), ui().colors().selected_bg_color(),
			nullptr, nullptr);

	// artificially set the hover to the last item so a double-click exits
	m_hover = m_items.size() - 1;
}


//-------------------------------------------------
//  map_mouse - map mouse pointer location to menu
//  coordinates
//-------------------------------------------------

void menu::map_mouse()
{
	ignore_mouse();
	int32_t mouse_target_x, mouse_target_y;
	render_target *const mouse_target = machine().ui_input().find_mouse(&mouse_target_x, &mouse_target_y, &m_mouse_button);
	if (mouse_target)
	{
		if (mouse_target->map_point_container(mouse_target_x, mouse_target_y, container(), m_mouse_x, m_mouse_y))
			m_mouse_hit = true;
	}
}


//-------------------------------------------------
//  ignore_mouse - set members to ignore mouse
//  input
//-------------------------------------------------

void menu::ignore_mouse()
{
	m_mouse_hit = false;
	m_mouse_button = false;
	m_mouse_x = -1.0f;
	m_mouse_y = -1.0f;
}


//-------------------------------------------------
//  handle_events - generically handle
//  input events for a menu
//-------------------------------------------------

void menu::handle_events(uint32_t flags, event &ev)
{
	bool stop = false;
	ui_event local_menu_event;

	// loop while we have interesting events
	while (!stop && machine().ui_input().pop_event(&local_menu_event))
	{
		switch (local_menu_event.event_type)
		{
		// if we are hovering over a valid item, select it with a single click
		case ui_event::type::MOUSE_DOWN:
			if (custom_mouse_down())
				return;

			if (!(flags & PROCESS_ONLYCHAR))
			{
				if (m_hover >= 0 && m_hover < m_items.size())
				{
					m_selected = m_hover;
				}
				else if (m_hover == HOVER_ARROW_UP)
				{
					if (flags & PROCESS_CUSTOM_NAV)
					{
						ev.iptkey = IPT_UI_PAGE_UP;
						stop = true;
					}
					else
					{
						m_selected -= m_visible_items;
						if (m_selected < 0)
							m_selected = 0;
						top_line -= m_visible_items - (last_item_visible() ? 1 : 0);
					}
				}
				else if (m_hover == HOVER_ARROW_DOWN)
				{
					if (flags & PROCESS_CUSTOM_NAV)
					{
						ev.iptkey = IPT_UI_PAGE_DOWN;
						stop = true;
					}
					else
					{
						m_selected += m_visible_lines - 2 + is_first_selected();
						if (m_selected > m_items.size() - 1)
							m_selected = m_items.size() - 1;
						top_line += m_visible_lines - 2;
					}
				}
				else if (m_hover == HOVER_UI_LEFT)
				{
					ev.iptkey = IPT_UI_LEFT;
					stop = true;
				}
				else if (m_hover == HOVER_UI_RIGHT)
				{
					ev.iptkey = IPT_UI_RIGHT;
					stop = true;
				}
			}
			break;

		// if we are hovering over a valid item, fake a UI_SELECT with a double-click
		case ui_event::type::MOUSE_DOUBLE_CLICK:
			if (!(flags & PROCESS_ONLYCHAR) && m_hover >= 0 && m_hover < m_items.size())
			{
				m_selected = m_hover;
				ev.iptkey = IPT_UI_SELECT;
				if (is_last_selected() && m_needs_prev_menu_item)
				{
					ev.iptkey = IPT_UI_CANCEL;
					stack_pop();
				}
				stop = true;
			}
			break;

		// caught scroll event
		case ui_event::type::MOUSE_WHEEL:
			if (!(flags & PROCESS_ONLYCHAR))
			{
				if (local_menu_event.zdelta > 0)
				{
					if (flags & PROCESS_CUSTOM_NAV) // FIXME: DAT menu logic - let the derived class handle this
					{
						top_line -= local_menu_event.num_lines;
						return;
					}
					else if (is_first_selected())
					{
						select_last_item();
					}
					else
					{
						m_selected -= local_menu_event.num_lines;
						validate_selection(-1);
					}
					top_line -= (m_selected <= top_line && top_line != 0);
					if (m_selected <= top_line && m_visible_items != m_visible_lines)
						top_line -= local_menu_event.num_lines;
				}
				else
				{
					if (flags & PROCESS_CUSTOM_NAV) // FIXME: DAT menu logic - let the derived class handle this
					{
						top_line += local_menu_event.num_lines;
						return;
					}
					else if (is_last_selected())
					{
						select_first_item();
					}
					else
					{
						m_selected += local_menu_event.num_lines;
						validate_selection(1);
					}
					top_line += (m_selected >= top_line + m_visible_items + (top_line != 0));
					if (m_selected >= (top_line + m_visible_items + (top_line != 0)))
						top_line += local_menu_event.num_lines;
				}
			}
			break;

		// translate CHAR events into specials
		case ui_event::type::IME_CHAR:
			ev.iptkey = IPT_SPECIAL;
			ev.unichar = local_menu_event.ch;
			stop = true;
			break;

		// ignore everything else
		default:
			break;
		}
	}
}


//-------------------------------------------------
//  handle_keys - generically handle
//  keys for a menu
//-------------------------------------------------

void menu::handle_keys(uint32_t flags, int &iptkey)
{
	bool ignorepause = stack_has_special_main_menu();
	int code;

	// bail if no items
	if (m_items.empty())
		return;

	// if we hit select, return true or pop the stack, depending on the item
	if (exclusive_input_pressed(iptkey, IPT_UI_SELECT, 0))
	{
		if (is_last_selected() && m_needs_prev_menu_item)
		{
			iptkey = IPT_UI_CANCEL;
			stack_pop();
		}
		return;
	}

	// bail out
	if (flags & PROCESS_ONLYCHAR)
		return;

	// hitting cancel also pops the stack
	if (exclusive_input_pressed(iptkey, IPT_UI_CANCEL, 0))
	{
		if (!custom_ui_cancel())
			stack_pop();
		return;
	}

	// validate the current selection
	validate_selection(1);

	// swallow left/right keys if they are not appropriate
	bool const ignoreleft = !(flags & PROCESS_LR_ALWAYS) && !(selected_item().flags & FLAG_LEFT_ARROW);
	bool const ignoreright = !(flags & PROCESS_LR_ALWAYS) && !(selected_item().flags & FLAG_RIGHT_ARROW);

	// accept left/right keys as-is with repeat
	if (!ignoreleft && exclusive_input_pressed(iptkey, IPT_UI_LEFT, (flags & PROCESS_LR_REPEAT) ? 6 : 0))
		return;
	if (!ignoreright && exclusive_input_pressed(iptkey, IPT_UI_RIGHT, (flags & PROCESS_LR_REPEAT) ? 6 : 0))
		return;

	// up backs up by one item
	if (exclusive_input_pressed(iptkey, IPT_UI_UP, 6))
	{
		if (flags & PROCESS_CUSTOM_NAV)
		{
			return;
		}
		else if (is_first_selected())
		{
			select_last_item();
		}
		else
		{
			--m_selected;
			validate_selection(-1);
		}
		top_line -= (m_selected <= top_line && top_line != 0);
		if (m_selected <= top_line && m_visible_items != m_visible_lines)
			top_line--;
	}

	// down advances by one item
	if (exclusive_input_pressed(iptkey, IPT_UI_DOWN, 6))
	{
		if (flags & PROCESS_CUSTOM_NAV)
		{
			return;
		}
		else if (is_last_selected())
		{
			select_first_item();
		}
		else
		{
			++m_selected;
			validate_selection(1);
		}
		top_line += (m_selected >= top_line + m_visible_items + (top_line != 0));
		if (m_selected >= (top_line + m_visible_items + (top_line != 0)))
			top_line++;
	}

	// page up backs up by m_visible_items
	if (exclusive_input_pressed(iptkey, IPT_UI_PAGE_UP, 6))
	{
		if (flags & PROCESS_CUSTOM_NAV)
			return;
		m_selected -= m_visible_items;
		top_line -= m_visible_items - (last_item_visible() ? 1 : 0);
		if (m_selected < 0)
			m_selected = 0;
		validate_selection(1);
	}

	// page down advances by m_visible_items
	if (exclusive_input_pressed(iptkey, IPT_UI_PAGE_DOWN, 6))
	{
		if (flags & PROCESS_CUSTOM_NAV)
			return;
		m_selected += m_visible_lines - 2 + is_first_selected();
		top_line += m_visible_lines - 2;

		if (m_selected > m_items.size() - 1)
			m_selected = m_items.size() - 1;
		validate_selection(-1);
	}

	// home goes to the start
	if (exclusive_input_pressed(iptkey, IPT_UI_HOME, 0))
	{
		if (flags & PROCESS_CUSTOM_NAV)
			return;
		select_first_item();
	}

	// end goes to the last
	if (exclusive_input_pressed(iptkey, IPT_UI_END, 0))
	{
		if (flags & PROCESS_CUSTOM_NAV)
			return;
		select_last_item();
	}

	// pause enables/disables pause
	if (!ignorepause && exclusive_input_pressed(iptkey, IPT_UI_PAUSE, 0))
	{
		if (machine().paused())
			machine().resume();
		else
			machine().pause();
	}

	// handle a toggle cheats request
	if (machine().ui_input().pressed_repeat(IPT_UI_TOGGLE_CHEAT, 0))
		mame_machine_manager::instance()->cheat().set_enable(!mame_machine_manager::instance()->cheat().enabled());

	// see if any other UI keys are pressed
	if (iptkey == IPT_INVALID)
	{
		for (code = IPT_UI_FIRST + 1; code < IPT_UI_LAST; code++)
		{
			if (code == IPT_UI_CONFIGURE || (code == IPT_UI_LEFT && ignoreleft) || (code == IPT_UI_RIGHT && ignoreright) || (code == IPT_UI_PAUSE && ignorepause))
				continue;
			if (exclusive_input_pressed(iptkey, code, 0))
				break;
		}
	}
}


//-------------------------------------------------
//  select_first_item - select the first item in
//  the menu
//-------------------------------------------------

void menu::select_first_item()
{
	m_selected = top_line = 0;
	validate_selection(1);
}


//-------------------------------------------------
//  select_last_item - select the last item in the
//  menu
//-------------------------------------------------

void menu::select_last_item()
{
	m_selected = top_line = m_items.size() - 1;
	validate_selection(-1);
}


//-------------------------------------------------
//  validate_selection - validate the
//  current selection and ensure it is on a
//  correct item
//-------------------------------------------------

void menu::validate_selection(int scandir)
{
	// clamp to be in range
	if (m_selected < 0)
		m_selected = 0;
	else if (m_selected >= m_items.size())
		m_selected = m_items.size() - 1;

	// skip past unselectable items
	while (!is_selectable(m_items[m_selected]))
		m_selected = (m_selected + m_items.size() + scandir) % m_items.size();
}



/***************************************************************************
    MENU STACK MANAGEMENT
***************************************************************************/

void menu::do_handle()
{
	if (m_items.empty())
	{
		// add an item to return - this is a really hacky way of doing this
		if (m_needs_prev_menu_item)
			item_append(_("Return to Previous Menu"), 0, nullptr);

		// let implementation add other items
		populate(m_customtop, m_custombottom);
	}
	handle();
}


/***************************************************************************
    UI SYSTEM INTERACTION
***************************************************************************/

//-------------------------------------------------
//  ui_menu_ui_handler - displays the current menu
//  and calls the menu handler
//-------------------------------------------------

uint32_t menu::ui_handler(render_container &container, mame_ui_manager &mui)
{
	global_state_ptr const state(get_global_state(mui.machine()));

	// if we have no menus stacked up, start with the main menu
	if (!state->topmost_menu<menu>())
		state->stack_push(std::unique_ptr<menu>(make_unique_clear<menu_main>(mui, container)));

	// update the menu state
	if (state->topmost_menu<menu>())
		state->topmost_menu<menu>()->do_handle();

	// clear up anything pending to be released
	state->clear_free_list();

	// if the menus are to be hidden, return a cancel here
	if (mui.is_menu_active() && ((mui.machine().ui_input().pressed(IPT_UI_CONFIGURE) && !state->stack_has_special_main_menu()) || !state->topmost_menu<menu>()))
		return UI_HANDLER_CANCEL;

	return 0;
}

/***************************************************************************
    MENU HELPERS
***************************************************************************/

//-------------------------------------------------
//  highlight
//-------------------------------------------------

void menu::highlight(float x0, float y0, float x1, float y1, rgb_t bgcolor)
{
	container().add_quad(x0, y0, x1, y1, bgcolor, m_global_state->hilight_texture(), PRIMFLAG_BLENDMODE(BLENDMODE_ALPHA) | PRIMFLAG_TEXWRAP(1) | PRIMFLAG_PACKABLE);
}


//-------------------------------------------------
//  draw_arrow
//-------------------------------------------------

void menu::draw_arrow(float x0, float y0, float x1, float y1, rgb_t fgcolor, uint32_t orientation)
{
	container().add_quad(x0, y0, x1, y1, fgcolor, m_global_state->arrow_texture(), PRIMFLAG_BLENDMODE(BLENDMODE_ALPHA) | PRIMFLAG_TEXORIENT(orientation) | PRIMFLAG_PACKABLE);
}


//-------------------------------------------------
//  extra_text_draw_box - generically adds header
//  or footer text
//-------------------------------------------------

void menu::extra_text_draw_box(float origx1, float origx2, float origy, float yspan, std::string_view text, int direction)
{
	// get the size of the text
	auto layout = ui().create_layout(container());
	layout.add_text(text);

	// position this extra text
	float x1, y1, x2, y2;
	extra_text_position(origx1, origx2, origy, yspan, layout, direction, x1, y1, x2, y2);

	// draw a box
	ui().draw_outlined_box(container(), x1, y1, x2, y2, ui().colors().background_color());

	// take off the borders
	x1 += ui().box_lr_border() * machine().render().ui_aspect(&container());
	y1 += ui().box_tb_border();

	// draw the text within it
	layout.emit(container(), x1, y1);
}


void menu::draw_background()
{
	// draw background image if available
	if (ui().options().use_background_image() && m_global_state->bgrnd_bitmap() && m_global_state->bgrnd_bitmap()->valid())
		container().add_quad(0.0f, 0.0f, 1.0f, 1.0f, rgb_t::white(), m_global_state->bgrnd_texture(), PRIMFLAG_BLENDMODE(BLENDMODE_ALPHA));
}


//-------------------------------------------------
//  extra_text_position - given extra text that has
//  been put into a layout, position it
//-------------------------------------------------

void menu::extra_text_position(float origx1, float origx2, float origy, float yspan, text_layout &layout,
	int direction, float &x1, float &y1, float &x2, float &y2)
{
	float width = layout.actual_width() + (2 * ui().box_lr_border() * machine().render().ui_aspect(&container()));
	float maxwidth = std::max(width, origx2 - origx1);

	// compute our bounds
	x1 = 0.5f - 0.5f * maxwidth;
	x2 = x1 + maxwidth;
	y1 = origy + (yspan * direction);
	y2 = origy + (ui().box_tb_border() * direction);

	if (y1 > y2)
		std::swap(y1, y2);
}


//-------------------------------------------------
//  extra_text_render - generically adds header
//  and footer text
//-------------------------------------------------

void menu::extra_text_render(float top, float bottom, float origx1, float origy1, float origx2, float origy2, std::string_view header, std::string_view footer)
{
	if (!header.empty())
		extra_text_draw_box(origx1, origx2, origy1, top, header, -1);
	if (!footer.empty())
		extra_text_draw_box(origx1, origx2, origy2, bottom, footer, +1);
}

} // namespace ui