Moved input_character() template method to ui/utils.h, and changed menu_software_list::m_filename_buffer to be std::string

This commit is contained in:
Nathan Woods 2016-08-03 08:03:11 -04:00
parent 7b1d37d1f4
commit ba8559e897
4 changed files with 57 additions and 59 deletions

View File

@ -15,6 +15,7 @@
#include "ui/filecreate.h" #include "ui/filecreate.h"
#include "ui/ui.h" #include "ui/ui.h"
#include "ui/utils.h"
#include "imagedev/floppy.h" #include "imagedev/floppy.h"
@ -44,34 +45,6 @@ CONSTANTS
MENU HELPERS MENU HELPERS
***************************************************************************/ ***************************************************************************/
//-------------------------------------------------
// input_character - inputs a typed character
// into a buffer
//-------------------------------------------------
template <typename F>
static void input_character(std::string &buffer, unicode_char unichar, F &&filter)
{
auto buflen = buffer.size();
if ((unichar == 8) || (unichar == 0x7f))
{
// backspace
if (0 < buflen)
{
auto buffer_oldend = buffer.c_str() + buflen;
auto buffer_newend = utf8_previous_char(buffer_oldend);
buffer.resize(buffer_newend - buffer.c_str());
}
}
else if ((unichar >= ' ') && (!filter || filter(unichar)))
{
// append this character
buffer += utf8_from_uchar(unichar);
}
}
/*************************************************************************** /***************************************************************************
CONFIRM SAVE AS MENU CONFIRM SAVE AS MENU
***************************************************************************/ ***************************************************************************/

View File

@ -12,6 +12,7 @@
#include "ui/ui.h" #include "ui/ui.h"
#include "ui/swlist.h" #include "ui/swlist.h"
#include "ui/utils.h"
#include "softlist.h" #include "softlist.h"
@ -29,6 +30,18 @@ namespace ui {
SOFTWARE PARTS 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(unicode_char ch)
{
return (ch == (char)ch) && isalnum(ch);
}
//------------------------------------------------- //-------------------------------------------------
// ctor // ctor
//------------------------------------------------- //-------------------------------------------------
@ -233,7 +246,7 @@ void menu_software_list::handle()
m_ordered_by_shortname = !m_ordered_by_shortname; m_ordered_by_shortname = !m_ordered_by_shortname;
// reset the char buffer if we change ordering criterion // reset the char buffer if we change ordering criterion
memset(m_filename_buffer, '\0', ARRAY_LENGTH(m_filename_buffer)); m_filename_buffer.clear();
// reload the menu with the new order // reload the menu with the new order
reset(reset_options::REMEMBER_REF); reset(reset_options::REMEMBER_REF);
@ -248,33 +261,11 @@ void menu_software_list::handle()
} }
else if (event->iptkey == IPT_SPECIAL) else if (event->iptkey == IPT_SPECIAL)
{ {
auto const buflen = std::strlen(m_filename_buffer); if (input_character(m_filename_buffer, event->unichar, &is_valid_softlist_part_char))
bool update_selected = false;
if ((event->unichar == 8) || (event->unichar == 0x7f))
{ {
// if it's a backspace and we can handle it, do so // display the popup
if (0 < buflen) ui().popup_time(ERROR_MESSAGE_TIME, "%s", m_filename_buffer);
{
*const_cast<char *>(utf8_previous_char(&m_filename_buffer[buflen])) = 0;
update_selected = true;
ui().popup_time(ERROR_MESSAGE_TIME, "%s", m_filename_buffer);
}
}
else if (event->is_char_printable())
{
// if it's any other key and we're not maxed out, update
if (event->append_char(m_filename_buffer, buflen))
{
update_selected = true;
ui().popup_time(ERROR_MESSAGE_TIME, "%s", m_filename_buffer);
}
}
if (update_selected)
{
// identify the selected entry // identify the selected entry
entry_info const *const cur_selected = (FPTR(event->itemref) != 1) entry_info const *const cur_selected = (FPTR(event->itemref) != 1)
? reinterpret_cast<entry_info const *>(get_selection_ref()) ? reinterpret_cast<entry_info const *>(get_selection_ref())
@ -289,9 +280,9 @@ void menu_software_list::handle()
auto &compare_name = m_ordered_by_shortname ? entry.short_name : entry.long_name; auto &compare_name = m_ordered_by_shortname ? entry.short_name : entry.long_name;
int match = 0; int match = 0;
for (int i = 0; i < ARRAY_LENGTH(m_filename_buffer); i++) for (int i = 0; i < m_filename_buffer.length(); i++)
{ {
if (core_strnicmp(compare_name.c_str(), m_filename_buffer, i) == 0) if (core_strnicmp(compare_name.c_str(), m_filename_buffer.c_str(), i) == 0)
match = i; match = i;
} }
@ -313,8 +304,7 @@ void menu_software_list::handle()
else if (event->iptkey == IPT_UI_CANCEL) else if (event->iptkey == IPT_UI_CANCEL)
{ {
// reset the char buffer also in this case // reset the char buffer also in this case
if (m_filename_buffer[0] != '\0') m_filename_buffer.clear();
memset(m_filename_buffer, '\0', ARRAY_LENGTH(m_filename_buffer));
m_result = m_filename_buffer; m_result = m_filename_buffer;
stack_pop(); stack_pop();
} }

View File

@ -78,7 +78,7 @@ private:
const char * m_interface; const char * m_interface;
std::string & m_result; std::string & m_result;
std::list<entry_info> m_entrylist; std::list<entry_info> m_entrylist;
char m_filename_buffer[1024]; std::string m_filename_buffer;
bool m_ordered_by_shortname; bool m_ordered_by_shortname;
// functions // functions

View File

@ -13,6 +13,8 @@
#ifndef __UI_UTILS_H__ #ifndef __UI_UTILS_H__
#define __UI_UTILS_H__ #define __UI_UTILS_H__
#include "unicode.h"
#define MAX_CHAR_INFO 256 #define MAX_CHAR_INFO 256
#define MAX_CUST_FILTER 8 #define MAX_CUST_FILTER 8
@ -249,4 +251,37 @@ const char* strensure(const char* s);
int getprecisionchr(const char* s); int getprecisionchr(const char* s);
std::vector<std::string> tokenize(const std::string &text, char sep); std::vector<std::string> tokenize(const std::string &text, char sep);
//-------------------------------------------------
// input_character - inputs a typed character
// into a buffer
//-------------------------------------------------
template <typename F>
bool input_character(std::string &buffer, unicode_char unichar, F &&filter)
{
bool result = false;
auto buflen = buffer.size();
if ((unichar == 8) || (unichar == 0x7f))
{
// backspace
if (0 < buflen)
{
auto buffer_oldend = buffer.c_str() + buflen;
auto buffer_newend = utf8_previous_char(buffer_oldend);
buffer.resize(buffer_newend - buffer.c_str());
result = true;
}
}
else if ((unichar >= ' ') && (!filter || filter(unichar)))
{
// append this character
buffer += utf8_from_uchar(unichar);
result = true;
}
return result;
}
#endif /* __UI_UTILS_H__ */ #endif /* __UI_UTILS_H__ */