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/newfont.h
- /fonts/vgafont.h - /fonts/vgafont.h
- /fonts/unicodemap.h - /fonts/unicodemap.h
- /build.sh
- /ltmain.sh - /ltmain.sh
component_depth: 3 component_depth: 3
languages: 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> 2017-12-19 Markus Gans <guru.mail@muenster.de>
* Refactoring FMenu::drawItems * Refactoring FMenu::drawItems

View File

@ -30,7 +30,39 @@
#include <final/final.h> #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() void init()
{ {
std::cout << "----------------[ terminal ]-------------------" std::cout << "----------------[ terminal ]-------------------"

View File

@ -34,7 +34,10 @@ static FVTerm* terminal;
void tcapBooleans (const std::string&, bool); void tcapBooleans (const std::string&, bool);
void tcapNumeric (const std::string&, int); void tcapNumeric (const std::string&, int);
void tcapString (const std::string&, const char*); void tcapString (const std::string&, const char*);
void debug (FApplication&);
void booleans();
void numeric();
void string(FTermcap::tcap_map*&);
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// functions // functions
@ -77,7 +80,7 @@ void tcapString (const std::string& name, const char* cap_str)
if ( c > 127 ) if ( c > 127 )
{ {
std::ostringstream o; std::ostringstream o;
o << std::oct << int(uChar(c)); o << std::oct << int(c);
sequence += "\\"; sequence += "\\";
sequence += o.str(); sequence += o.str();
} }
@ -88,11 +91,11 @@ void tcapString (const std::string& name, const char* cap_str)
else else
{ {
sequence += '^'; sequence += '^';
sequence += c + 64; sequence += char(c + 64);
} }
} }
else else
sequence += c; sequence += char(c);
} }
std::cout << sequence << " "; std::cout << sequence << " ";

View File

@ -99,6 +99,16 @@ class FMenuBar : public FWindow, public FMenuList
void cb_item_deactivated (FWidget*, data_ptr); void cb_item_deactivated (FWidget*, data_ptr);
private: private:
// Typedef
typedef struct
{
wchar_t* text;
int length;
int startpos;
int hotkeypos;
bool no_underline;
} menuText;
// Disable copy constructor // Disable copy constructor
FMenuBar (const FMenuBar&); FMenuBar (const FMenuBar&);
@ -117,6 +127,12 @@ class FMenuBar : public FWindow, public FMenuList
int getHotkeyPos (wchar_t[], wchar_t[], uInt); int getHotkeyPos (wchar_t[], wchar_t[], uInt);
void draw(); void draw();
void drawItems(); 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 adjustItems();
void leaveMenuBar(); void leaveMenuBar();
@ -127,6 +143,7 @@ class FMenuBar : public FWindow, public FMenuList
// Data Members // Data Members
bool mouse_down; bool mouse_down;
bool drop_down; bool drop_down;
int screenWidth;
}; };
#pragma pack(pop) #pragma pack(pop)

View File

@ -1301,26 +1301,27 @@ void FDialog::drawTitleBar()
for (x = 1; x <= i; x++) for (x = 1; x <= i; x++)
print (' '); print (' ');
// the title bar text // Print title bar text
if ( tb_text ) if ( tb_text )
{ {
if ( length <= getWidth() - menu_btn - zoom_btn ) if ( length <= getWidth() - menu_btn - zoom_btn )
print (tb_text); print (tb_text);
else else
{ {
// Print ellipsis
print (tb_text.left(getWidth() - menu_btn - zoom_btn - 2)); print (tb_text.left(getWidth() - menu_btn - zoom_btn - 2));
print (".."); print ("..");
} }
} }
// fill the rest of the bar // Fill the rest of the bar
for ( ; x + 1 + length < getWidth() - zoom_btn - 1; x++) for ( ; x + 1 + length < getWidth() - zoom_btn - 1; x++)
print (' '); print (' ');
if ( getMaxColor() < 16 ) if ( getMaxColor() < 16 )
unsetBold(); unsetBold();
// draw the zoom/unzoom button // Draw the zoom/unzoom button
if ( isResizeable() ) if ( isResizeable() )
{ {
if ( zoom_button_pressed ) if ( zoom_button_pressed )
@ -1379,7 +1380,7 @@ void FDialog::drawTitleBar()
if ( isMonochron() ) if ( isMonochron() )
setReverse(false); setReverse(false);
/* print the number of window in stack */ /* Print the number of window in stack */
//setPrintPos (getWidth() - 2, 1); //setPrintPos (getWidth() - 2, 1);
//printf ("(%d)", getWindowLayer(this)); //printf ("(%d)", getWindowLayer(this));
} }

View File

@ -542,14 +542,16 @@ void FLabel::printLine ( wchar_t line[]
if ( length > uInt(getWidth()) ) if ( length > uInt(getWidth()) )
{ {
// Print ellipsis
setColor (ellipsis_color, getBackgroundColor()); setColor (ellipsis_color, getBackgroundColor());
print (".."); print ("..");
setColor(); setColor();
} }
else if ( align_offset + to_char < getWidth() ) else if ( align_offset + to_char < getWidth() )
{ {
// Print trailing spaces
int len = getWidth() - align_offset - to_char; int len = getWidth() - align_offset - to_char;
print (FString(len, ' ')); // trailing spaces print (FString(len, ' '));
} }
if ( hasReverseMode() ) if ( hasReverseMode() )

View File

@ -1186,6 +1186,7 @@ void FListBox::drawLabel()
print (txt); print (txt);
else else
{ {
// Print ellipsis
print (text.left(uInt(getClientWidth() - 2))); print (text.left(uInt(getClientWidth() - 2)));
setColor (wc.label_ellipsis_fg, wc.label_bg); setColor (wc.label_ellipsis_fg, wc.label_bg);
print(".."); print("..");

View File

@ -1439,6 +1439,7 @@ void FListView::drawColumnLabels()
} }
else else
{ {
// Print ellipsis
headerline << ' '; headerline << ' ';
headerline << text.left(uInt(width - ellipsis_length)); headerline << text.left(uInt(width - ellipsis_length));
setColor (wc.label_ellipsis_fg, wc.label_bg); 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) 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 // + generate a new string without the '&'-sign
int hotkeypos = -1; int hotkeypos = -1;
wchar_t* txt = src; wchar_t* txt = src;
@ -1319,7 +1319,7 @@ int FMenu::getHotkeyPos (wchar_t src[], wchar_t dest[], uInt length)
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FMenu::draw() void FMenu::draw()
{ {
// fill the background // Fill the background
setColor (wc.menu_active_fg, wc.menu_active_bg); setColor (wc.menu_active_fg, wc.menu_active_bg);
if ( isMonochron() ) if ( isMonochron() )

View File

@ -37,6 +37,7 @@ FMenuBar::FMenuBar(FWidget* parent)
: FWindow(parent) : FWindow(parent)
, mouse_down(false) , mouse_down(false)
, drop_down(false) , drop_down(false)
, screenWidth(80)
{ {
init(); init();
} }
@ -52,7 +53,6 @@ FMenuBar::~FMenuBar() // destructor
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FMenuBar::hide() void FMenuBar::hide()
{ {
int screenWidth;
short fg, bg; short fg, bg;
char* blank; char* blank;
@ -776,9 +776,7 @@ void FMenuBar::draw()
void FMenuBar::drawItems() void FMenuBar::drawItems()
{ {
std::vector<FMenuItem*>::const_iterator iter, last; std::vector<FMenuItem*>::const_iterator iter, last;
int screenWidth;
int x = 1; int x = 1;
screenWidth = getColumnNumber();
if ( item_list.empty() ) if ( item_list.empty() )
return; return;
@ -788,27 +786,92 @@ void FMenuBar::drawItems()
if ( isMonochron() ) if ( isMonochron() )
setReverse(true); setReverse(true);
screenWidth = getColumnNumber();
iter = item_list.begin(); iter = item_list.begin();
last = item_list.end(); last = item_list.end();
while ( iter != last ) while ( iter != last )
{ {
wchar_t* item_text; drawItem (*iter, x);
FString txt; ++iter;
uInt txt_length; }
int hotkeypos
, startpos
, to_char;
bool is_active
, is_selected
, is_noUnderline;
startpos = x + 1; // Print spaces to end of line
is_active = (*iter)->isEnabled(); for (; x <= screenWidth; x++)
is_selected = (*iter)->isSelected(); print (' ');
is_noUnderline = (((*iter)->getFlags() & fc::no_underline) != 0);
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 ) if ( is_selected )
{ {
@ -831,108 +894,89 @@ void FMenuBar::drawItems()
} }
setColor(); setColor();
if ( x < screenWidth )
{
x++;
print (' ');
} }
txt = (*iter)->getText(); //----------------------------------------------------------------------
txt_length = uInt(txt.getLength()); void FMenuBar::drawMenuText (menuText& data)
try
{ {
item_text = new wchar_t[txt_length + 1](); // Print menu text
}
catch (const std::bad_alloc& ex) for (int z = 0; z < data.length; z++)
{ {
std::cerr << "not enough memory to alloc " << ex.what() << std::endl; if ( data.startpos > screenWidth - z )
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; break;
if ( ! std::iswprint(wint_t(item_text[z])) ) if ( ! std::iswprint(wint_t(data.text[z])) )
{ {
if ( ! isNewFont() if ( ! isNewFont()
&& ( int(item_text[z]) < fc::NF_rev_left_arrow2 && ( int(data.text[z]) < fc::NF_rev_left_arrow2
|| int(item_text[z]) > fc::NF_check_mark ) ) || 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); setColor (wc.menu_hotkey_fg, wc.menu_hotkey_bg);
if ( ! is_noUnderline ) if ( ! data.no_underline )
setUnderline(); setUnderline();
print (item_text[z]); print (data.text[z]);
if ( ! is_noUnderline ) if ( ! data.no_underline )
unsetUnderline(); unsetUnderline();
setColor(); setColor();
} }
else else
print (item_text[z]); print (data.text[z]);
}
} }
//----------------------------------------------------------------------
inline void FMenuBar::drawEllipsis (menuText& txtdata, int x)
{
if ( x > screenWidth + 1 ) if ( x > screenWidth + 1 )
{ {
if ( startpos < screenWidth ) if ( txtdata.startpos < screenWidth )
{ {
// Print ellipsis
setPrintPos (screenWidth - 1, 1); setPrintPos (screenWidth - 1, 1);
print (".."); print ("..");
} }
else if ( startpos - 1 <= screenWidth ) else if ( txtdata.startpos - 1 <= screenWidth )
{ {
// Hide first character from text
setPrintPos (screenWidth, 1); setPrintPos (screenWidth, 1);
print (' '); print (' ');
} }
} }
}
//----------------------------------------------------------------------
inline void FMenuBar::drawLeadingSpace (int& x)
{
// Print a leading blank space
if ( x < screenWidth ) if ( x < screenWidth )
{ {
x++; x++;
print (' '); 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() ) if ( x < screenWidth )
setReverse(false); {
x++;
print (' ');
}
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------

View File

@ -281,6 +281,7 @@ void FStatusBar::drawMessage()
print (getMessage()); print (getMessage());
else else
{ {
// Print ellipsis
print ( getMessage().left(uInt(msg_length + termWidth - x - 1)) ); print ( getMessage().left(uInt(msg_length + termWidth - x - 1)) );
print (".."); print ("..");
} }
@ -629,6 +630,7 @@ void FStatusBar::drawKeys()
} }
else else
{ {
// Print ellipsis
print ( (*iter)->getText() print ( (*iter)->getText()
.left(uInt(txt_length + screenWidth - x - 1)) ); .left(uInt(txt_length + screenWidth - x - 1)) );
print (".."); print ("..");
@ -657,6 +659,7 @@ void FStatusBar::drawKeys()
print ((*iter)->getText()); print ((*iter)->getText());
else else
{ {
// Print ellipsis
print ( (*iter)->getText() print ( (*iter)->getText()
.left(uInt(txt_length + screenWidth - x - 1)) ); .left(uInt(txt_length + screenWidth - x - 1)) );
print (".."); print ("..");