Refactoring FMenuBar::drawItems

This commit is contained in:
Markus Gans 2017-12-21 00:25:58 +01:00
parent ef5970b09c
commit ff1fe44132
13 changed files with 257 additions and 149 deletions

View File

@ -2,6 +2,7 @@ exclude:
- /fonts/newfont.h
- /fonts/vgafont.h
- /fonts/unicodemap.h
- /build.sh
- /ltmain.sh
component_depth: 3
languages:

View File

@ -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

View File

@ -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 ]-------------------"

View File

@ -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 << " ";

View File

@ -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)

View File

@ -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));
}

View File

@ -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() )

View File

@ -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("..");

View File

@ -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);

View File

@ -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() )

View File

@ -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,27 +786,92 @@ 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;
drawItem (*iter, x);
++iter;
}
startpos = x + 1;
is_active = (*iter)->isEnabled();
is_selected = (*iter)->isSelected();
is_noUnderline = (((*iter)->getFlags() & fc::no_underline) != 0);
// Print spaces to end of line
for (; x <= screenWidth; x++)
print (' ');
if ( is_active )
if ( isMonochron() )
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 )
{
@ -831,108 +894,89 @@ void FMenuBar::drawItems()
}
setColor();
if ( x < screenWidth )
{
x++;
print (' ');
}
txt = (*iter)->getText();
txt_length = uInt(txt.getLength());
try
//----------------------------------------------------------------------
void FMenuBar::drawMenuText (menuText& data)
{
item_text = new wchar_t[txt_length + 1]();
}
catch (const std::bad_alloc& ex)
// Print menu text
for (int z = 0; z < data.length; z++)
{
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 )
if ( data.startpos > screenWidth - z )
break;
if ( ! std::iswprint(wint_t(item_text[z])) )
if ( ! std::iswprint(wint_t(data.text[z])) )
{
if ( ! isNewFont()
&& ( int(item_text[z]) < fc::NF_rev_left_arrow2
|| int(item_text[z]) > fc::NF_check_mark ) )
&& ( int(data.text[z]) < fc::NF_rev_left_arrow2
|| int(data.text[z]) > fc::NF_check_mark ) )
{
item_text[z] = L' ';
data.text[z] = L' ';
}
}
if ( (z == hotkeypos) && is_active && ! is_selected )
if ( z == data.hotkeypos )
{
setColor (wc.menu_hotkey_fg, wc.menu_hotkey_bg);
if ( ! is_noUnderline )
if ( ! data.no_underline )
setUnderline();
print (item_text[z]);
print (data.text[z]);
if ( ! is_noUnderline )
if ( ! data.no_underline )
unsetUnderline();
setColor();
}
else
print (item_text[z]);
print (data.text[z]);
}
}
//----------------------------------------------------------------------
inline void FMenuBar::drawEllipsis (menuText& txtdata, int x)
{
if ( x > screenWidth + 1 )
{
if ( startpos < screenWidth )
if ( txtdata.startpos < screenWidth )
{
// Print ellipsis
setPrintPos (screenWidth - 1, 1);
print ("..");
}
else if ( startpos - 1 <= screenWidth )
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 (' ');
}
setColor (wc.menu_active_fg, wc.menu_active_bg);
if ( isMonochron() && is_active && is_selected )
setReverse(true);
delete[] item_text;
++iter;
}
for (; x <= screenWidth; x++)
print (' ');
//----------------------------------------------------------------------
inline void FMenuBar::drawTrailingSpace (int& x)
{
// Print a trailing blank space
if ( isMonochron() )
setReverse(false);
if ( x < screenWidth )
{
x++;
print (' ');
}
}
//----------------------------------------------------------------------

View File

@ -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 ("..");