summaryrefslogtreecommitdiffstatshomepage
path: root/src/frontend/mame/ui/swlist.cpp
blob: 70c9471da56321974976c9167f0e2901da3c6558 (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
// license:BSD-3-Clause
// copyright-holders:Nicola Salmoria, Aaron Giles, Nathan Woods
/*********************************************************************

    ui/swlist.cpp

    Internal MAME user interface for software list.

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

#include "emu.h"

#include "ui/ui.h"
#include "ui/swlist.h"
#include "ui/utils.h"

#include "corestr.h"
#include "softlist_dev.h"

#include <locale>


namespace ui {

/***************************************************************************
    CONSTANTS
***************************************************************************/

// time (in seconds) to display errors
#define ERROR_MESSAGE_TIME      5

// item reference for "Switch Item Ordering"
#define ITEMREF_SWITCH_ITEM_ORDERING    ((void *)1)


/***************************************************************************
    SOFTWARE PARTS
***************************************************************************/

//-------------------------------------------------
//  is_valid_softlist_part_char - returns whether
//  this character is a valid char for a softlist
//  part
//-------------------------------------------------

static bool is_valid_softlist_part_char(char32_t ch)
{
	return (ch == (char)ch) && isalnum(ch);
}


//-------------------------------------------------
//  ctor
//-------------------------------------------------

menu_software_parts::menu_software_parts(mame_ui_manager &mui, render_container &container, const software_info *info, const char *interface, const software_part **part, bool other_opt, result &result)
	: menu(mui, container),
		m_result(result)
{
	m_info = info;
	m_interface = interface;
	m_selected_part = part;
	m_other_opt = other_opt;
}


//-------------------------------------------------
//  dtor
//-------------------------------------------------

menu_software_parts::~menu_software_parts()
{
}


//-------------------------------------------------
//  populate
//-------------------------------------------------

void menu_software_parts::populate(float &customtop, float &custombottom)
{
	m_entries.clear();

	if (m_other_opt)
	{
		software_part_menu_entry &entry1(*m_entries.emplace(m_entries.end()));
		entry1.type = result::EMPTY;
		entry1.part = nullptr;
		item_append(_("[empty slot]"), 0, &entry1);

		software_part_menu_entry &entry2(*m_entries.emplace(m_entries.end()));
		entry2.type = result::FMGR;
		entry2.part = nullptr;
		item_append(_("[file manager]"), 0, &entry2);


		software_part_menu_entry &entry3(*m_entries.emplace(m_entries.end()));
		entry3.type = result::SWLIST;
		entry3.part = nullptr;
		item_append(_("[software list]"), 0, &entry3);
	}

	for (const software_part &swpart : m_info->parts())
	{
		if (swpart.matches_interface(m_interface))
		{
			software_part_menu_entry &entry(*m_entries.emplace(m_entries.end()));
			// check if the available parts have specific part_id to be displayed (e.g. "Map Disc", "Bonus Disc", etc.)
			// if not, we simply display "part_name"; if yes we display "part_name (part_id)"
			std::string menu_part_name(swpart.name());
			if (swpart.feature("part_id") != nullptr)
				menu_part_name.append(" (").append(swpart.feature("part_id")).append(")");
			entry.type = result::ENTRY;
			entry.part = &swpart;
			item_append(m_info->shortname(), menu_part_name, 0, &entry);
		}
	}

	item_append(menu_item_type::SEPARATOR);
}


//-------------------------------------------------
//  handle
//-------------------------------------------------

void menu_software_parts::handle(event const *ev)
{
	// process the menu
	if (ev && (ev->iptkey == IPT_UI_SELECT) && ev->itemref)
	{
		software_part_menu_entry *entry = (software_part_menu_entry *)ev->itemref;
		m_result = entry->type;
		*m_selected_part = entry->part;
		stack_pop();
	}
}


/***************************************************************************
    SOFTWARE LIST
***************************************************************************/

//-------------------------------------------------
//  ctor
//-------------------------------------------------

menu_software_list::menu_software_list(mame_ui_manager &mui, render_container &container, software_list_device *swlist, const char *interface, std::string &result)
	: menu(mui, container), m_result(result)
{
	set_process_flags(PROCESS_IGNOREPAUSE);
	m_swlist = swlist;
	m_interface = interface;
	m_ordered_by_shortname = false;
}


//-------------------------------------------------
//  dtor
//-------------------------------------------------

menu_software_list::~menu_software_list()
{
}


//-------------------------------------------------
//  append_software_entry - populate a specific list
//-------------------------------------------------

void menu_software_list::append_software_entry(const software_info &swinfo)
{
	entry_info entry;
	bool entry_updated = false;

	// check if at least one of the parts has the correct interface and add a menu entry only in this case
	for (const software_part &swpart : swinfo.parts())
	{
		if (swpart.matches_interface(m_interface) && m_swlist->is_compatible(swpart) == SOFTWARE_IS_COMPATIBLE)
		{
			entry_updated = true;
			entry.short_name.assign(swinfo.shortname());
			entry.long_name.assign(swinfo.longname());
			break;
		}
	}

	// skip this if no new entry has been allocated (e.g. if the software has no matching interface for this image device)
	if (entry_updated)
		m_entrylist.emplace_back(std::move(entry));
}


//-------------------------------------------------
//  populate
//-------------------------------------------------

void menu_software_list::populate(float &customtop, float &custombottom)
{
	// build up the list of entries for the menu
	if (m_entrylist.empty())
		for (const software_info &swinfo : m_swlist->get_info())
			append_software_entry(swinfo);

	if (m_ordered_by_shortname)
	{
		// short names are restricted to lowercase ASCII anyway, a dumb compare works
		m_entrylist.sort([] (entry_info const &e1, entry_info const &e2) { return e1.short_name < e2.short_name; });
	}
	else
	{
		std::collate<wchar_t> const &coll = std::use_facet<std::collate<wchar_t>>(std::locale());
		m_entrylist.sort(
				[&coll] (entry_info const &e1, entry_info const &e2) -> bool
				{
					std::wstring const xstr = wstring_from_utf8(e1.long_name);
					std::wstring const ystr = wstring_from_utf8(e2.long_name);
					auto const cmp = coll.compare(xstr.data(), xstr.data() + xstr.size(), ystr.data(), ystr.data() + ystr.size());
					if (cmp)
						return cmp < 0;
					else
						return e1.short_name < e2.short_name;
				});
	}

	// add an entry to change ordering
	item_append(_("Switch Item Ordering"), 0, ITEMREF_SWITCH_ITEM_ORDERING);

	// append all of the menu entries
	for (auto &entry : m_entrylist)
		item_append(entry.long_name, entry.short_name, 0, &entry);

	item_append(menu_item_type::SEPARATOR);
}


//-------------------------------------------------
//  handle
//-------------------------------------------------

void menu_software_list::handle(event const *ev)
{
	// process the menu
	if (ev)
	{
		if (ev->iptkey == IPT_UI_SELECT)
		{
			if (ev->itemref == ITEMREF_SWITCH_ITEM_ORDERING)
			{
				m_ordered_by_shortname = !m_ordered_by_shortname;

				// reset the char buffer if we change ordering criterion
				m_search.clear();

				// reload the menu with the new order
				reset(reset_options::REMEMBER_REF);
				machine().popmessage(
						m_ordered_by_shortname
							? _("Switched Order: entries now ordered by shortname")
							: _("Switched Order: entries now ordered by description"));
			}
			else if (ev->itemref)
			{
				// handle selections
				entry_info *info = (entry_info *)ev->itemref;
				m_result = info->short_name;
				stack_pop();
			}
		}
		else if (ev->iptkey == IPT_SPECIAL)
		{
			if (input_character(m_search, ev->unichar, m_ordered_by_shortname ? is_valid_softlist_part_char : [] (char32_t ch) { return true; }))
			{
				// display the popup
				ui().popup_time(ERROR_MESSAGE_TIME, "%s", m_search);

				// identify the selected entry
				entry_info const *const cur_selected = (uintptr_t(ev->itemref) != 1)
						? reinterpret_cast<entry_info const *>(get_selection_ref())
						: nullptr;

				// if it's a perfect match for the current selection, don't move it
				if (!cur_selected || core_strnicmp((m_ordered_by_shortname ? cur_selected->short_name : cur_selected->long_name).c_str(), m_search.c_str(), m_search.size()))
				{
					std::string::size_type bestmatch(0);
					entry_info const *selected_entry(cur_selected);
					for (auto &entry : m_entrylist)
					{
						// TODO: more efficient "common prefix" code
						auto const &compare_name = m_ordered_by_shortname ? entry.short_name : entry.long_name;
						std::string::size_type match(0);
						for (std::string::size_type i = 1; m_search.size() >= i; ++i)
						{
							if (!core_strnicmp(compare_name.c_str(), m_search.c_str(), i))
								match = i;
							else
								break;
						}

						if (match > bestmatch)
						{
							bestmatch = match;
							selected_entry = &entry;
						}
					}

					if (selected_entry && (selected_entry != cur_selected))
					{
						set_selection((void *)selected_entry);
						centre_selection();
					}
				}
			}
		}
		else if (ev->iptkey == IPT_UI_CANCEL)
		{
			// reset the char buffer also in this case
			if (!m_search.empty())
			{
				m_search.clear();
				ui().popup_time(ERROR_MESSAGE_TIME, "%s", m_search);
			}
		}
	}
}


/***************************************************************************
    SOFTWARE MENU - list of available software lists - i.e. cartridges,
    floppies
***************************************************************************/

//-------------------------------------------------
//  ctor
//-------------------------------------------------

menu_software::menu_software(mame_ui_manager &mui, render_container &container, const char *interface, software_list_device **result)
	: menu(mui, container)
{
	m_interface = interface;
	m_result = result;
}


//-------------------------------------------------
//  dtor
//-------------------------------------------------

menu_software::~menu_software()
{
}


//-------------------------------------------------
//  populate
//-------------------------------------------------

void menu_software::populate(float &customtop, float &custombottom)
{
	bool have_compatible = false;

	// Add original software lists for this system
	software_list_device_enumerator iter(machine().config().root_device());
	for (software_list_device &swlistdev : iter)
		if (swlistdev.is_original())
			if (!swlistdev.get_info().empty() && m_interface != nullptr)
			{
				bool found = false;
				for (const software_info &swinfo : swlistdev.get_info())
					for (const software_part &swpart : swinfo.parts())
						if (swpart.matches_interface(m_interface))
						{
							found = true;
							break;
						}
				if (found)
					item_append(swlistdev.description(), 0, (void *)&swlistdev);
			}

	// add compatible software lists for this system
	for (software_list_device &swlistdev : iter)
		if (swlistdev.is_compatible())
			if (!swlistdev.get_info().empty() && m_interface != nullptr)
			{
				bool found = false;
				for (const software_info &swinfo : swlistdev.get_info())
					for (const software_part &swpart : swinfo.parts())
						if (swpart.matches_interface(m_interface))
						{
							found = true;
							break;
						}
				if (found)
				{
					if (!have_compatible)
						item_append(_("[compatible lists]"), FLAG_DISABLE, nullptr);
					item_append(swlistdev.description(), 0, (void *)&swlistdev);
				}
				have_compatible = true;
			}

	item_append(menu_item_type::SEPARATOR);
}


//-------------------------------------------------
//  handle
//-------------------------------------------------

void menu_software::handle(event const *ev)
{
	// process the menu
	if (ev && (ev->iptkey == IPT_UI_SELECT))
	{
		//menu::stack_push<menu_software_list>(ui(), container(), (software_list_config *)ev->itemref, image);
		*m_result = reinterpret_cast<software_list_device *>(ev->itemref);
		stack_pop();
	}
}

} // namespace ui