Refactoring FMenuBar::drawItems
This commit is contained in:
parent
ef5970b09c
commit
ff1fe44132
|
@ -2,6 +2,7 @@ exclude:
|
|||
- /fonts/newfont.h
|
||||
- /fonts/vgafont.h
|
||||
- /fonts/unicodemap.h
|
||||
- /build.sh
|
||||
- /ltmain.sh
|
||||
component_depth: 3
|
||||
languages:
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
2017-12-21 Markus Gans <guru.mail@muenster.de>
|
||||
* Refactoring FMenuBar::drawItems
|
||||
|
||||
2017-12-19 Markus Gans <guru.mail@muenster.de>
|
||||
* Refactoring FMenu::drawItems
|
||||
|
||||
|
|
|
@ -30,7 +30,39 @@
|
|||
|
||||
#include <final/final.h>
|
||||
|
||||
// function prototype
|
||||
void init();
|
||||
void inputStreamExample();
|
||||
void outputStreamExample();
|
||||
void streamingIntoFStringExample();
|
||||
void streamingFromFStringExample();
|
||||
void CStringOutputExample();
|
||||
void copyIntoFString();
|
||||
void utf8StringOutputExample();
|
||||
void letterCaseExample();
|
||||
void stringConcatenationExample();
|
||||
void stringCompareExample();
|
||||
void stringSplittingExample();
|
||||
void fromatStringExample();
|
||||
void convertToNumberExample();
|
||||
void convertNumberToStringExample();
|
||||
void formatedNumberExample();
|
||||
void trimExample();
|
||||
void substringExample();
|
||||
void insertExample();
|
||||
void indexExample();
|
||||
void iteratorExample();
|
||||
void overwriteExample();
|
||||
void removeExample();
|
||||
void substringIncludeExample();
|
||||
void replaceExample();
|
||||
void tabToSpaceExample();
|
||||
void backspaceControlCharacterExample();
|
||||
void deleteControlCharacterExample();
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
// functions
|
||||
//----------------------------------------------------------------------
|
||||
void init()
|
||||
{
|
||||
std::cout << "----------------[ terminal ]-------------------"
|
||||
|
|
|
@ -34,7 +34,10 @@ static FVTerm* terminal;
|
|||
void tcapBooleans (const std::string&, bool);
|
||||
void tcapNumeric (const std::string&, int);
|
||||
void tcapString (const std::string&, const char*);
|
||||
|
||||
void debug (FApplication&);
|
||||
void booleans();
|
||||
void numeric();
|
||||
void string(FTermcap::tcap_map*&);
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
// functions
|
||||
|
@ -77,7 +80,7 @@ void tcapString (const std::string& name, const char* cap_str)
|
|||
if ( c > 127 )
|
||||
{
|
||||
std::ostringstream o;
|
||||
o << std::oct << int(uChar(c));
|
||||
o << std::oct << int(c);
|
||||
sequence += "\\";
|
||||
sequence += o.str();
|
||||
}
|
||||
|
@ -88,11 +91,11 @@ void tcapString (const std::string& name, const char* cap_str)
|
|||
else
|
||||
{
|
||||
sequence += '^';
|
||||
sequence += c + 64;
|
||||
sequence += char(c + 64);
|
||||
}
|
||||
}
|
||||
else
|
||||
sequence += c;
|
||||
sequence += char(c);
|
||||
}
|
||||
|
||||
std::cout << sequence << " ";
|
||||
|
|
|
@ -196,7 +196,7 @@ class FMenu : public FWindow, public FMenuList
|
|||
int getHotkeyPos (wchar_t[], wchar_t[], uInt);
|
||||
void draw();
|
||||
void drawItems();
|
||||
void drawSeparator(int);
|
||||
void drawSeparator (int);
|
||||
void drawMenuLine (FMenuItem*, int);
|
||||
void drawCheckMarkPrefix (FMenuItem*);
|
||||
void drawMenuText (menuText&);
|
||||
|
|
|
@ -99,6 +99,16 @@ class FMenuBar : public FWindow, public FMenuList
|
|||
void cb_item_deactivated (FWidget*, data_ptr);
|
||||
|
||||
private:
|
||||
// Typedef
|
||||
typedef struct
|
||||
{
|
||||
wchar_t* text;
|
||||
int length;
|
||||
int startpos;
|
||||
int hotkeypos;
|
||||
bool no_underline;
|
||||
} menuText;
|
||||
|
||||
// Disable copy constructor
|
||||
FMenuBar (const FMenuBar&);
|
||||
|
||||
|
@ -117,6 +127,12 @@ class FMenuBar : public FWindow, public FMenuList
|
|||
int getHotkeyPos (wchar_t[], wchar_t[], uInt);
|
||||
void draw();
|
||||
void drawItems();
|
||||
void drawItem (FMenuItem*, int&);
|
||||
void setLineAttributes (FMenuItem*);
|
||||
void drawMenuText (menuText&);
|
||||
void drawEllipsis (menuText&, int);
|
||||
void drawLeadingSpace (int&);
|
||||
void drawTrailingSpace (int&);
|
||||
void adjustItems();
|
||||
void leaveMenuBar();
|
||||
|
||||
|
@ -127,6 +143,7 @@ class FMenuBar : public FWindow, public FMenuList
|
|||
// Data Members
|
||||
bool mouse_down;
|
||||
bool drop_down;
|
||||
int screenWidth;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
|
|
|
@ -1301,26 +1301,27 @@ void FDialog::drawTitleBar()
|
|||
for (x = 1; x <= i; x++)
|
||||
print (' ');
|
||||
|
||||
// the title bar text
|
||||
// Print title bar text
|
||||
if ( tb_text )
|
||||
{
|
||||
if ( length <= getWidth() - menu_btn - zoom_btn )
|
||||
print (tb_text);
|
||||
else
|
||||
{
|
||||
// Print ellipsis
|
||||
print (tb_text.left(getWidth() - menu_btn - zoom_btn - 2));
|
||||
print ("..");
|
||||
}
|
||||
}
|
||||
|
||||
// fill the rest of the bar
|
||||
// Fill the rest of the bar
|
||||
for ( ; x + 1 + length < getWidth() - zoom_btn - 1; x++)
|
||||
print (' ');
|
||||
|
||||
if ( getMaxColor() < 16 )
|
||||
unsetBold();
|
||||
|
||||
// draw the zoom/unzoom button
|
||||
// Draw the zoom/unzoom button
|
||||
if ( isResizeable() )
|
||||
{
|
||||
if ( zoom_button_pressed )
|
||||
|
@ -1379,7 +1380,7 @@ void FDialog::drawTitleBar()
|
|||
if ( isMonochron() )
|
||||
setReverse(false);
|
||||
|
||||
/* print the number of window in stack */
|
||||
/* Print the number of window in stack */
|
||||
//setPrintPos (getWidth() - 2, 1);
|
||||
//printf ("(%d)", getWindowLayer(this));
|
||||
}
|
||||
|
|
|
@ -542,14 +542,16 @@ void FLabel::printLine ( wchar_t line[]
|
|||
|
||||
if ( length > uInt(getWidth()) )
|
||||
{
|
||||
// Print ellipsis
|
||||
setColor (ellipsis_color, getBackgroundColor());
|
||||
print ("..");
|
||||
setColor();
|
||||
}
|
||||
else if ( align_offset + to_char < getWidth() )
|
||||
{
|
||||
// Print trailing spaces
|
||||
int len = getWidth() - align_offset - to_char;
|
||||
print (FString(len, ' ')); // trailing spaces
|
||||
print (FString(len, ' '));
|
||||
}
|
||||
|
||||
if ( hasReverseMode() )
|
||||
|
|
|
@ -1186,6 +1186,7 @@ void FListBox::drawLabel()
|
|||
print (txt);
|
||||
else
|
||||
{
|
||||
// Print ellipsis
|
||||
print (text.left(uInt(getClientWidth() - 2)));
|
||||
setColor (wc.label_ellipsis_fg, wc.label_bg);
|
||||
print("..");
|
||||
|
|
|
@ -1439,6 +1439,7 @@ void FListView::drawColumnLabels()
|
|||
}
|
||||
else
|
||||
{
|
||||
// Print ellipsis
|
||||
headerline << ' ';
|
||||
headerline << text.left(uInt(width - ellipsis_length));
|
||||
setColor (wc.label_ellipsis_fg, wc.label_bg);
|
||||
|
|
|
@ -1296,7 +1296,7 @@ bool FMenu::hotkeyMenu (FKeyEvent*& ev)
|
|||
//----------------------------------------------------------------------
|
||||
int FMenu::getHotkeyPos (wchar_t src[], wchar_t dest[], uInt length)
|
||||
{
|
||||
// find hotkey position in string
|
||||
// Find hotkey position in string
|
||||
// + generate a new string without the '&'-sign
|
||||
int hotkeypos = -1;
|
||||
wchar_t* txt = src;
|
||||
|
@ -1319,7 +1319,7 @@ int FMenu::getHotkeyPos (wchar_t src[], wchar_t dest[], uInt length)
|
|||
//----------------------------------------------------------------------
|
||||
void FMenu::draw()
|
||||
{
|
||||
// fill the background
|
||||
// Fill the background
|
||||
setColor (wc.menu_active_fg, wc.menu_active_bg);
|
||||
|
||||
if ( isMonochron() )
|
||||
|
@ -1345,7 +1345,7 @@ void FMenu::drawItems()
|
|||
while ( iter != last )
|
||||
{
|
||||
if ( (*iter)->isSeparator() )
|
||||
drawSeparator(y);
|
||||
drawSeparator (y);
|
||||
else
|
||||
drawMenuLine (*iter, y);
|
||||
|
||||
|
@ -1355,7 +1355,7 @@ void FMenu::drawItems()
|
|||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
inline void FMenu::drawSeparator(int y)
|
||||
inline void FMenu::drawSeparator (int y)
|
||||
{
|
||||
setPrintPos (1, 2 + y);
|
||||
setColor (wc.menu_active_fg, wc.menu_active_bg);
|
||||
|
|
314
src/fmenubar.cpp
314
src/fmenubar.cpp
|
@ -37,6 +37,7 @@ FMenuBar::FMenuBar(FWidget* parent)
|
|||
: FWindow(parent)
|
||||
, mouse_down(false)
|
||||
, drop_down(false)
|
||||
, screenWidth(80)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
@ -52,7 +53,6 @@ FMenuBar::~FMenuBar() // destructor
|
|||
//----------------------------------------------------------------------
|
||||
void FMenuBar::hide()
|
||||
{
|
||||
int screenWidth;
|
||||
short fg, bg;
|
||||
char* blank;
|
||||
|
||||
|
@ -776,9 +776,7 @@ void FMenuBar::draw()
|
|||
void FMenuBar::drawItems()
|
||||
{
|
||||
std::vector<FMenuItem*>::const_iterator iter, last;
|
||||
int screenWidth;
|
||||
int x = 1;
|
||||
screenWidth = getColumnNumber();
|
||||
|
||||
if ( item_list.empty() )
|
||||
return;
|
||||
|
@ -788,146 +786,17 @@ void FMenuBar::drawItems()
|
|||
if ( isMonochron() )
|
||||
setReverse(true);
|
||||
|
||||
screenWidth = getColumnNumber();
|
||||
iter = item_list.begin();
|
||||
last = item_list.end();
|
||||
|
||||
while ( iter != last )
|
||||
{
|
||||
wchar_t* item_text;
|
||||
FString txt;
|
||||
uInt txt_length;
|
||||
int hotkeypos
|
||||
, startpos
|
||||
, to_char;
|
||||
bool is_active
|
||||
, is_selected
|
||||
, is_noUnderline;
|
||||
|
||||
startpos = x + 1;
|
||||
is_active = (*iter)->isEnabled();
|
||||
is_selected = (*iter)->isSelected();
|
||||
is_noUnderline = (((*iter)->getFlags() & fc::no_underline) != 0);
|
||||
|
||||
if ( is_active )
|
||||
{
|
||||
if ( is_selected )
|
||||
{
|
||||
if ( isMonochron() )
|
||||
setReverse(false);
|
||||
|
||||
setForegroundColor (wc.menu_active_focus_fg);
|
||||
setBackgroundColor (wc.menu_active_focus_bg);
|
||||
}
|
||||
else
|
||||
{
|
||||
setForegroundColor (wc.menu_active_fg);
|
||||
setBackgroundColor (wc.menu_active_bg);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
setForegroundColor (wc.menu_inactive_fg);
|
||||
setBackgroundColor (wc.menu_inactive_bg);
|
||||
}
|
||||
|
||||
setColor();
|
||||
|
||||
if ( x < screenWidth )
|
||||
{
|
||||
x++;
|
||||
print (' ');
|
||||
}
|
||||
|
||||
txt = (*iter)->getText();
|
||||
txt_length = uInt(txt.getLength());
|
||||
|
||||
try
|
||||
{
|
||||
item_text = new wchar_t[txt_length + 1]();
|
||||
}
|
||||
catch (const std::bad_alloc& ex)
|
||||
{
|
||||
std::cerr << "not enough memory to alloc " << ex.what() << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if ( x - 1 <= screenWidth )
|
||||
to_char = int(txt_length);
|
||||
else
|
||||
to_char = int(txt_length) - (screenWidth - x - 1);
|
||||
|
||||
hotkeypos = getHotkeyPos (txt.wc_str(), item_text, txt_length);
|
||||
|
||||
if ( hotkeypos != -1 )
|
||||
{
|
||||
txt_length--;
|
||||
to_char--;
|
||||
}
|
||||
|
||||
x += int(txt_length);
|
||||
|
||||
for (int z = 0; z < to_char; z++)
|
||||
{
|
||||
if ( startpos > screenWidth - z )
|
||||
break;
|
||||
|
||||
if ( ! std::iswprint(wint_t(item_text[z])) )
|
||||
{
|
||||
if ( ! isNewFont()
|
||||
&& ( int(item_text[z]) < fc::NF_rev_left_arrow2
|
||||
|| int(item_text[z]) > fc::NF_check_mark ) )
|
||||
{
|
||||
item_text[z] = L' ';
|
||||
}
|
||||
}
|
||||
|
||||
if ( (z == hotkeypos) && is_active && ! is_selected )
|
||||
{
|
||||
setColor (wc.menu_hotkey_fg, wc.menu_hotkey_bg);
|
||||
|
||||
if ( ! is_noUnderline )
|
||||
setUnderline();
|
||||
|
||||
print (item_text[z]);
|
||||
|
||||
if ( ! is_noUnderline )
|
||||
unsetUnderline();
|
||||
|
||||
setColor();
|
||||
}
|
||||
else
|
||||
print (item_text[z]);
|
||||
}
|
||||
|
||||
if ( x > screenWidth + 1 )
|
||||
{
|
||||
if ( startpos < screenWidth )
|
||||
{
|
||||
setPrintPos (screenWidth - 1, 1);
|
||||
print ("..");
|
||||
}
|
||||
else if ( startpos - 1 <= screenWidth )
|
||||
{
|
||||
setPrintPos (screenWidth, 1);
|
||||
print (' ');
|
||||
}
|
||||
}
|
||||
|
||||
if ( x < screenWidth )
|
||||
{
|
||||
x++;
|
||||
print (' ');
|
||||
}
|
||||
|
||||
setColor (wc.menu_active_fg, wc.menu_active_bg);
|
||||
|
||||
if ( isMonochron() && is_active && is_selected )
|
||||
setReverse(true);
|
||||
|
||||
delete[] item_text;
|
||||
drawItem (*iter, x);
|
||||
++iter;
|
||||
}
|
||||
|
||||
// Print spaces to end of line
|
||||
for (; x <= screenWidth; x++)
|
||||
print (' ');
|
||||
|
||||
|
@ -935,6 +804,181 @@ void FMenuBar::drawItems()
|
|||
setReverse(false);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
void FMenuBar::drawItem (FMenuItem* menuitem, int& x)
|
||||
{
|
||||
FString txt = menuitem->getText();
|
||||
menuText txtdata;
|
||||
uInt txt_length = txt.getLength();
|
||||
int hotkeypos;
|
||||
int to_char;
|
||||
bool is_enabled = menuitem->isEnabled();
|
||||
bool is_selected = menuitem->isSelected();
|
||||
|
||||
txtdata.startpos = x + 1;
|
||||
txtdata.no_underline = ((menuitem->getFlags() & fc::no_underline) != 0);
|
||||
|
||||
// Set screen attributes
|
||||
setLineAttributes (menuitem);
|
||||
drawLeadingSpace (x);
|
||||
|
||||
try
|
||||
{
|
||||
txtdata.text = new wchar_t[txt_length + 1]();
|
||||
}
|
||||
catch (const std::bad_alloc& ex)
|
||||
{
|
||||
std::cerr << "not enough memory to alloc " << ex.what() << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if ( x - 1 <= screenWidth )
|
||||
to_char = int(txt_length);
|
||||
else
|
||||
to_char = int(txt_length) - (screenWidth - x - 1);
|
||||
|
||||
hotkeypos = getHotkeyPos (txt.wc_str(), txtdata.text, txt_length);
|
||||
|
||||
if ( hotkeypos != -1 )
|
||||
{
|
||||
txt_length--;
|
||||
to_char--;
|
||||
}
|
||||
|
||||
txtdata.length = to_char;
|
||||
x += int(txt_length);
|
||||
|
||||
if ( ! is_enabled || is_selected )
|
||||
txtdata.hotkeypos = -1;
|
||||
else
|
||||
txtdata.hotkeypos = hotkeypos;
|
||||
|
||||
drawMenuText (txtdata);
|
||||
drawEllipsis (txtdata, x);
|
||||
drawTrailingSpace (x);
|
||||
|
||||
setColor (wc.menu_active_fg, wc.menu_active_bg);
|
||||
|
||||
if ( isMonochron() && is_enabled && is_selected )
|
||||
setReverse(true);
|
||||
|
||||
delete[] txtdata.text;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
void FMenuBar::setLineAttributes (FMenuItem* menuitem)
|
||||
{
|
||||
bool is_enabled = menuitem->isEnabled();
|
||||
bool is_selected = menuitem->isSelected();
|
||||
|
||||
if ( is_enabled )
|
||||
{
|
||||
if ( is_selected )
|
||||
{
|
||||
if ( isMonochron() )
|
||||
setReverse(false);
|
||||
|
||||
setForegroundColor (wc.menu_active_focus_fg);
|
||||
setBackgroundColor (wc.menu_active_focus_bg);
|
||||
}
|
||||
else
|
||||
{
|
||||
setForegroundColor (wc.menu_active_fg);
|
||||
setBackgroundColor (wc.menu_active_bg);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
setForegroundColor (wc.menu_inactive_fg);
|
||||
setBackgroundColor (wc.menu_inactive_bg);
|
||||
}
|
||||
|
||||
setColor();
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
void FMenuBar::drawMenuText (menuText& data)
|
||||
{
|
||||
// Print menu text
|
||||
|
||||
for (int z = 0; z < data.length; z++)
|
||||
{
|
||||
if ( data.startpos > screenWidth - z )
|
||||
break;
|
||||
|
||||
if ( ! std::iswprint(wint_t(data.text[z])) )
|
||||
{
|
||||
if ( ! isNewFont()
|
||||
&& ( int(data.text[z]) < fc::NF_rev_left_arrow2
|
||||
|| int(data.text[z]) > fc::NF_check_mark ) )
|
||||
{
|
||||
data.text[z] = L' ';
|
||||
}
|
||||
}
|
||||
|
||||
if ( z == data.hotkeypos )
|
||||
{
|
||||
setColor (wc.menu_hotkey_fg, wc.menu_hotkey_bg);
|
||||
|
||||
if ( ! data.no_underline )
|
||||
setUnderline();
|
||||
|
||||
print (data.text[z]);
|
||||
|
||||
if ( ! data.no_underline )
|
||||
unsetUnderline();
|
||||
|
||||
setColor();
|
||||
}
|
||||
else
|
||||
print (data.text[z]);
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
inline void FMenuBar::drawEllipsis (menuText& txtdata, int x)
|
||||
{
|
||||
if ( x > screenWidth + 1 )
|
||||
{
|
||||
if ( txtdata.startpos < screenWidth )
|
||||
{
|
||||
// Print ellipsis
|
||||
setPrintPos (screenWidth - 1, 1);
|
||||
print ("..");
|
||||
}
|
||||
else if ( txtdata.startpos - 1 <= screenWidth )
|
||||
{
|
||||
// Hide first character from text
|
||||
setPrintPos (screenWidth, 1);
|
||||
print (' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
inline void FMenuBar::drawLeadingSpace (int& x)
|
||||
{
|
||||
// Print a leading blank space
|
||||
|
||||
if ( x < screenWidth )
|
||||
{
|
||||
x++;
|
||||
print (' ');
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
inline void FMenuBar::drawTrailingSpace (int& x)
|
||||
{
|
||||
// Print a trailing blank space
|
||||
|
||||
if ( x < screenWidth )
|
||||
{
|
||||
x++;
|
||||
print (' ');
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
void FMenuBar::adjustItems()
|
||||
{
|
||||
|
|
|
@ -281,6 +281,7 @@ void FStatusBar::drawMessage()
|
|||
print (getMessage());
|
||||
else
|
||||
{
|
||||
// Print ellipsis
|
||||
print ( getMessage().left(uInt(msg_length + termWidth - x - 1)) );
|
||||
print ("..");
|
||||
}
|
||||
|
@ -629,6 +630,7 @@ void FStatusBar::drawKeys()
|
|||
}
|
||||
else
|
||||
{
|
||||
// Print ellipsis
|
||||
print ( (*iter)->getText()
|
||||
.left(uInt(txt_length + screenWidth - x - 1)) );
|
||||
print ("..");
|
||||
|
@ -657,6 +659,7 @@ void FStatusBar::drawKeys()
|
|||
print ((*iter)->getText());
|
||||
else
|
||||
{
|
||||
// Print ellipsis
|
||||
print ( (*iter)->getText()
|
||||
.left(uInt(txt_length + screenWidth - x - 1)) );
|
||||
print ("..");
|
||||
|
|
Loading…
Reference in New Issue