Remove duplicated code in FVTerm::print

This commit is contained in:
Markus Gans 2017-11-22 23:56:21 +01:00
parent a2061786c8
commit 051fbab046
7 changed files with 151 additions and 211 deletions

View File

@ -1,3 +1,6 @@
2017-11-22 Markus Gans <guru.mail@muenster.de>
* Remove duplicated code in FVTerm::print
2017-11-19 Markus Gans <guru.mail@muenster.de> 2017-11-19 Markus Gans <guru.mail@muenster.de>
* Splitting the FString example into sub-functions to make * Splitting the FString example into sub-functions to make
the code more comprehensible the code more comprehensible

View File

@ -277,6 +277,7 @@ class MyDialog : public FDialog
void initStatusBar(); void initStatusBar();
void initStatusBarCallbacks(); void initStatusBarCallbacks();
void initWidgets(); void initWidgets();
void initWidgetsCallbacks();
void adjustSize(); void adjustSize();
// Event handlers // Event handlers
@ -300,24 +301,32 @@ class MyDialog : public FDialog
void cb_setInput (FWidget*, data_ptr); void cb_setInput (FWidget*, data_ptr);
// Data Members // Data Members
FMenuItem* Open; FMenuItem* Open;
FMenuItem* Quit; FMenuItem* Quit;
FMenuItem* File1; FMenuItem* File1;
FMenuItem* File2; FMenuItem* File2;
FMenuItem* File3; FMenuItem* File3;
FMenuItem* Cut; FMenuItem* Cut;
FMenuItem* Copy; FMenuItem* Copy;
FMenuItem* Paste; FMenuItem* Paste;
FMenuItem* Clear; FMenuItem* Clear;
FMenuItem* Env; FMenuItem* Env;
FMenuItem* Drive; FMenuItem* Drive;
FMenuItem* Help; FMenuItem* Help;
FStatusKey* key_F1; FStatusKey* key_F1;
FStatusKey* key_F2; FStatusKey* key_F2;
FStatusKey* key_F3; FStatusKey* key_F3;
FLineEdit* myLineEdit; FButton* MyButton1;
FListBox* myList; FButton* MyButton2;
FString clipboard; FButton* MyButton3;
FButton* MyButton4;
FButton* MyButton5;
FButton* MyButton6;
FRadioButton* radio1;
FLabel* tagged_count;
FLineEdit* myLineEdit;
FListBox* myList;
FString clipboard;
}; };
#pragma pack(pop) #pragma pack(pop)
@ -333,6 +342,7 @@ MyDialog::MyDialog (FWidget* parent)
initStatusBar(); // Initialize the status bar initStatusBar(); // Initialize the status bar
initStatusBarCallbacks(); // Initialize status bar callbacks initStatusBarCallbacks(); // Initialize status bar callbacks
initWidgets(); // Initialize the dialog widgets initWidgets(); // Initialize the dialog widgets
initWidgetsCallbacks(); // Initialize dialog widget callbacks
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -523,7 +533,7 @@ void MyDialog::initStatusBarCallbacks()
void MyDialog::initWidgets() void MyDialog::initWidgets()
{ {
// Buttons // Buttons
FButton* MyButton1 = new FButton (this); MyButton1 = new FButton (this);
MyButton1->setGeometry(3, 3, 5, 1); MyButton1->setGeometry(3, 3, 5, 1);
MyButton1->setText (L"&SIN"); MyButton1->setText (L"&SIN");
MyButton1->setStatusbarMessage ("Sine function"); MyButton1->setStatusbarMessage ("Sine function");
@ -531,7 +541,7 @@ void MyDialog::initWidgets()
MyButton1->setFlat(); MyButton1->setFlat();
MyButton1->setDoubleFlatLine (fc::bottom); MyButton1->setDoubleFlatLine (fc::bottom);
FButton* MyButton2 = new FButton (this); MyButton2 = new FButton (this);
MyButton2->setGeometry(3, 5, 5, 1); MyButton2->setGeometry(3, 5, 5, 1);
MyButton2->setText (L"&COS"); MyButton2->setText (L"&COS");
MyButton2->setStatusbarMessage ("Cosine function"); MyButton2->setStatusbarMessage ("Cosine function");
@ -539,7 +549,7 @@ void MyDialog::initWidgets()
MyButton2->setFlat(); MyButton2->setFlat();
MyButton2->setDoubleFlatLine (fc::top); MyButton2->setDoubleFlatLine (fc::top);
FButton* MyButton3 = new FButton (this); MyButton3 = new FButton (this);
MyButton3->setGeometry(10, 3, 5, 3); MyButton3->setGeometry(10, 3, 5, 3);
MyButton3->setText (L"&="); MyButton3->setText (L"&=");
MyButton3->setStatusbarMessage ("Equal"); MyButton3->setStatusbarMessage ("Equal");
@ -551,7 +561,7 @@ void MyDialog::initWidgets()
radioButtonGroup->setGeometry(3, 8, 14, 4); radioButtonGroup->setGeometry(3, 8, 14, 4);
//radioButtonGroup->unsetBorder(); //radioButtonGroup->unsetBorder();
FRadioButton* radio1 = new FRadioButton ("E&nable", radioButtonGroup); radio1 = new FRadioButton ("E&nable", radioButtonGroup);
radio1->setGeometry(1, 1, 10, 1); radio1->setGeometry(1, 1, 10, 1);
radio1->setStatusbarMessage ("Enable button Test"); radio1->setStatusbarMessage ("Enable button Test");
@ -583,19 +593,19 @@ void MyDialog::initWidgets()
*myLineEdit << FString("EnTry").toLower(); *myLineEdit << FString("EnTry").toLower();
// Buttons // Buttons
FButton* MyButton4 = new FButton (this); MyButton4 = new FButton (this);
MyButton4->setGeometry(20, 8, 12, 1); MyButton4->setGeometry(20, 8, 12, 1);
MyButton4->setText (L"&Get input"); MyButton4->setText (L"&Get input");
MyButton4->setStatusbarMessage ("Take text from input field"); MyButton4->setStatusbarMessage ("Take text from input field");
MyButton4->setFocus(); MyButton4->setFocus();
FButton* MyButton5 = new FButton (this); MyButton5 = new FButton (this);
MyButton5->setGeometry(20, 11, 12, 1); MyButton5->setGeometry(20, 11, 12, 1);
MyButton5->setText (L"&Test"); MyButton5->setText (L"&Test");
MyButton5->setStatusbarMessage ("Progressbar testing dialog"); MyButton5->setStatusbarMessage ("Progressbar testing dialog");
MyButton5->setDisable(); MyButton5->setDisable();
FButton* MyButton6 = new FButton (this); MyButton6 = new FButton (this);
MyButton6->setGeometry(20, 14, 12, 1); MyButton6->setGeometry(20, 14, 12, 1);
MyButton6->setText (L"&Quit"); MyButton6->setText (L"&Quit");
MyButton6->setStatusbarMessage ("Exit the program"); MyButton6->setStatusbarMessage ("Exit the program");
@ -621,7 +631,7 @@ void MyDialog::initWidgets()
FLabel* tagged = new FLabel (L"Tagged:", this); FLabel* tagged = new FLabel (L"Tagged:", this);
tagged->setGeometry(21, 4, 7, 1); tagged->setGeometry(21, 4, 7, 1);
FLabel* tagged_count = new FLabel(this); tagged_count = new FLabel(this);
tagged_count->setGeometry(29, 4, 5, 1); tagged_count->setGeometry(29, 4, 5, 1);
*tagged_count << 0; *tagged_count << 0;
@ -632,8 +642,13 @@ void MyDialog::initWidgets()
FLabel* sum_count = new FLabel (this); FLabel* sum_count = new FLabel (this);
sum_count->setGeometry(29, 5, 5, 3); sum_count->setGeometry(29, 5, 5, 3);
*sum_count << myList->getCount(); *sum_count << myList->getCount();
}
//----------------------------------------------------------------------
void MyDialog::initWidgetsCallbacks()
{
// Add some function callbacks // Add some function callbacks
MyButton1->addCallback MyButton1->addCallback
( (
"clicked", "clicked",

View File

@ -60,7 +60,11 @@
#if defined(__linux__) #if defined(__linux__)
#include <linux/fb.h> // Linux framebuffer console #include <linux/fb.h> // Linux framebuffer console
#include <linux/keyboard.h> // need for gpm keyboard modifiers #include <linux/keyboard.h> // need for gpm keyboard modifiers
#include <sys/io.h> // <asm/io.h> is deprecated
#if defined(__x86_64__) || defined(__i386) || defined(__arm__)
#include <sys/io.h> // <asm/io.h> is deprecated
#endif
#include <sys/kd.h> #include <sys/kd.h>
#endif #endif
@ -400,12 +404,14 @@ class FTerm
// Methods // Methods
#if defined(__linux__) #if defined(__linux__)
#if defined(__x86_64__) || defined(__i386) || defined(__arm__)
static uInt16 getInputStatusRegisterOne(); static uInt16 getInputStatusRegisterOne();
static uChar readAttributeController (uChar); static uChar readAttributeController (uChar);
static void writeAttributeController (uChar, uChar); static void writeAttributeController (uChar, uChar);
static uChar getAttributeMode(); static uChar getAttributeMode();
static void setAttributeMode (uChar); static void setAttributeMode (uChar);
static int setBlinkAsIntensity (bool); static int setBlinkAsIntensity (bool);
#endif
static int getFramebuffer_bpp(); static int getFramebuffer_bpp();
#endif #endif

View File

@ -244,6 +244,8 @@ class FVTerm : public FTerm
int print (term_area*, const std::vector<char_data>&); int print (term_area*, const std::vector<char_data>&);
int print (int); int print (int);
int print (term_area*, int); int print (term_area*, int);
int print (char_data&);
int print (term_area*, char_data&);
FVTerm& print(); FVTerm& print();
protected: protected:

View File

@ -1954,6 +1954,7 @@ bool FTerm::isWSConsConsole()
#endif #endif
#if defined(__linux__) #if defined(__linux__)
#if defined(__x86_64__) || defined(__i386) || defined(__arm__)
//---------------------------------------------------------------------- //----------------------------------------------------------------------
inline uInt16 FTerm::getInputStatusRegisterOne() inline uInt16 FTerm::getInputStatusRegisterOne()
{ {
@ -2052,6 +2053,7 @@ int FTerm::setBlinkAsIntensity (bool on)
return 0; return 0;
} }
#endif
//---------------------------------------------------------------------- //----------------------------------------------------------------------
int FTerm::getFramebuffer_bpp () int FTerm::getFramebuffer_bpp ()
@ -2709,7 +2711,10 @@ void FTerm::detectTerminal()
FTermcap::max_color = 16; FTermcap::max_color = 16;
} }
#if defined(__linux__) // Initialize Linux console
#if defined(__linux__)
#if defined(__x86_64__) || defined(__i386) || defined(__arm__)
// Enable 16 background colors
if ( linux_terminal && openConsole() == 0 ) if ( linux_terminal && openConsole() == 0 )
{ {
if ( isLinuxConsole() ) if ( isLinuxConsole() )
@ -2721,16 +2726,22 @@ void FTerm::detectTerminal()
} }
closeConsole(); closeConsole();
setLinuxConsoleCursorStyle (fc::underscore_cursor, true);
} }
if ( linux_terminal && getFramebuffer_bpp() >= 4 ) if ( linux_terminal )
FTermcap::max_color = 16; {
#endif setLinuxConsoleCursorStyle (fc::underscore_cursor, true);
#if defined(__FreeBSD__) || defined(__DragonFly__) if ( getFramebuffer_bpp() >= 4 )
FTermcap::max_color = 16;
}
#endif
#endif
// Initialize FreeBSD console cursor
#if defined(__FreeBSD__) || defined(__DragonFly__)
setFreeBSDConsoleCursorStyle (fc::destructive_cursor, true); setFreeBSDConsoleCursorStyle (fc::destructive_cursor, true);
#endif #endif
t.c_lflag |= uInt(ICANON | ECHO); t.c_lflag |= uInt(ICANON | ECHO);
tcsetattr(stdin_no, TCSADRAIN, &t); tcsetattr(stdin_no, TCSADRAIN, &t);
@ -3833,7 +3844,7 @@ void FTerm::init_OptiMove()
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FTerm::init_OptiAttr() void FTerm::init_OptiAttr()
{ {
// Setting video attribute optimization // Setting video attribute optimization
opti_attr->setNoColorVideo (int(FTermcap::attr_without_color)); opti_attr->setNoColorVideo (int(FTermcap::attr_without_color));
opti_attr->set_enter_bold_mode (TCAP(fc::t_enter_bold_mode)); opti_attr->set_enter_bold_mode (TCAP(fc::t_enter_bold_mode));
@ -4287,7 +4298,9 @@ void FTerm::finish()
#if defined(__linux__) #if defined(__linux__)
if ( linux_terminal ) if ( linux_terminal )
{ {
#if defined(__x86_64__) || defined(__i386) || defined(__arm__)
setBlinkAsIntensity (false); setBlinkAsIntensity (false);
#endif
setLinuxConsoleCursorStyle (fc::default_cursor, false); setLinuxConsoleCursorStyle (fc::default_cursor, false);
} }
#endif #endif

View File

@ -444,9 +444,8 @@ int FVTerm::print (const FString& s)
int FVTerm::print (term_area* area, const FString& s) int FVTerm::print (term_area* area, const FString& s)
{ {
assert ( ! s.isNull() ); assert ( ! s.isNull() );
register int len = 0;
const wchar_t* p; const wchar_t* p;
uInt tabstop = uInt(getTabstop()); std::vector<char_data> term_string;
if ( ! area ) if ( ! area )
return -1; return -1;
@ -455,120 +454,29 @@ int FVTerm::print (term_area* area, const FString& s)
if ( p ) if ( p )
{ {
if ( *p )
area->has_changes = true;
while ( *p ) while ( *p )
{ {
int width, height, rsh, bsh; char_data nc; // next character
width = area->width; nc.code = *p;
height = area->height; nc.fg_color = next_attribute.fg_color;
rsh = area->right_shadow; nc.bg_color = next_attribute.bg_color;
bsh = area->bottom_shadow; nc.attr.byte[0] = next_attribute.attr.byte[0];
nc.attr.byte[1] = next_attribute.attr.byte[1];
switch ( *p ) nc.attr.byte[2] = 0;
{ term_string.push_back(nc);
case '\n':
area->cursor_y++;
// fall through
case '\r':
area->cursor_x = 1;
break;
case '\t':
area->cursor_x = short ( uInt(area->cursor_x)
+ tabstop
- uInt(area->cursor_x)
+ 1
% tabstop );
break;
case '\b':
area->cursor_x--;
break;
case '\a':
beep();
break;
default:
{
int ax = area->cursor_x - 1;
int ay = area->cursor_y - 1;
char_data nc; // next character
nc.code = *p;
nc.fg_color = next_attribute.fg_color;
nc.bg_color = next_attribute.bg_color;
nc.attr.byte[0] = next_attribute.attr.byte[0];
nc.attr.byte[1] = next_attribute.attr.byte[1];
nc.attr.byte[2] = 0;
if ( area->cursor_x > 0
&& area->cursor_y > 0
&& ax < area->width + area->right_shadow
&& ay < area->height + area->bottom_shadow )
{
char_data* ac; // area character
int line_len = area->width + area->right_shadow;
ac = &area->text[ay * line_len + ax];
if ( *ac != nc ) // compare with an overloaded operator
{
if ( ( ! ac->attr.bit.transparent && nc.attr.bit.transparent )
|| ( ! ac->attr.bit.trans_shadow && nc.attr.bit.trans_shadow )
|| ( ! ac->attr.bit.inherit_bg && nc.attr.bit.inherit_bg ) )
{
// add one transparent character form line
area->changes[ay].trans_count++;
}
else if ( ( ac->attr.bit.transparent && ! nc.attr.bit.transparent )
|| ( ac->attr.bit.trans_shadow && ! nc.attr.bit.trans_shadow )
|| ( ac->attr.bit.inherit_bg && ! nc.attr.bit.inherit_bg ) )
{
// remove one transparent character from line
area->changes[ay].trans_count--;
}
// copy character to area
std::memcpy (ac, &nc, sizeof(nc));
if ( ax < short(area->changes[ay].xmin) )
area->changes[ay].xmin = uInt(ax);
if ( ax > short(area->changes[ay].xmax) )
area->changes[ay].xmax = uInt(ax);
}
}
area->cursor_x++;
}
}
if ( area->cursor_x > width + rsh )
{
area->cursor_x = 1;
area->cursor_y++;
}
if ( area->cursor_y > height + bsh )
{
area->cursor_y--;
break;
}
p++; p++;
len++;
} // end of while } // end of while
return print (area, term_string);
} }
return len; return 0;
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
int FVTerm::print (const std::vector<char_data>& termString) int FVTerm::print (const std::vector<char_data>& term_string)
{ {
if ( termString.empty() ) if ( term_string.empty() )
return 0; return 0;
term_area* area = getPrintArea(); term_area* area = getPrintArea();
@ -581,32 +489,30 @@ int FVTerm::print (const std::vector<char_data>& termString)
return -1; return -1;
} }
return print (area, termString); return print (area, term_string);
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
int FVTerm::print (term_area* area, const std::vector<char_data>& termString) int FVTerm::print (term_area* area, const std::vector<char_data>& term_string)
{ {
register int len = 0; register int len = 0;
std::vector<char_data>::const_iterator iter; std::vector<char_data>::const_iterator iter;
iter = termString.begin(); iter = term_string.begin();
uInt tabstop = uInt(getTabstop()); uInt tabstop = uInt(getTabstop());
if ( ! area ) if ( ! area )
return -1; return -1;
if ( termString.empty() ) if ( term_string.empty() )
return 0; return 0;
else
area->has_changes = true;
while ( iter != termString.end() ) while ( iter != term_string.end() )
{ {
int width, height, rsh, bsh; int width = area->width,
width = area->width; height = area->height,
height = area->height; rsh = area->right_shadow,
rsh = area->right_shadow; bsh = area->bottom_shadow;
bsh = area->bottom_shadow; bool printable_character = false;
switch ( (*iter).code ) switch ( (*iter).code )
{ {
@ -635,62 +541,27 @@ int FVTerm::print (term_area* area, const std::vector<char_data>& termString)
default: default:
{ {
int ax = area->cursor_x - 1;
int ay = area->cursor_y - 1;
char_data nc = *iter; // next character char_data nc = *iter; // next character
print (area, nc);
if ( area->cursor_x > 0 printable_character = true;
&& area->cursor_y > 0
&& ax < area->width + area->right_shadow
&& ay < area->height + area->bottom_shadow )
{
char_data* ac; // area character
int line_len = area->width + area->right_shadow;
ac = &area->text[ay * line_len + ax];
if ( *ac != nc ) // compare with an overloaded operator
{
if ( ( ! ac->attr.bit.transparent && nc.attr.bit.transparent )
|| ( ! ac->attr.bit.trans_shadow && nc.attr.bit.trans_shadow )
|| ( ! ac->attr.bit.inherit_bg && nc.attr.bit.inherit_bg ) )
{
// add one transparent character form line
area->changes[ay].trans_count++;
}
else if ( ( ac->attr.bit.transparent && ! nc.attr.bit.transparent )
|| ( ac->attr.bit.trans_shadow && ! nc.attr.bit.trans_shadow )
|| ( ac->attr.bit.inherit_bg && ! nc.attr.bit.inherit_bg ) )
{
// remove one transparent character from line
area->changes[ay].trans_count--;
}
// copy character to area
std::memcpy (ac, &nc, sizeof(nc));
if ( ax < short(area->changes[ay].xmin) )
area->changes[ay].xmin = uInt(ax);
if ( ax > short(area->changes[ay].xmax) )
area->changes[ay].xmax = uInt(ax);
}
}
area->cursor_x++;
} }
} }
if ( area->cursor_x > width + rsh ) if ( ! printable_character )
{ {
area->cursor_x = 1; // Line break at right margin
area->cursor_y++; if ( area->cursor_x > width + rsh )
} {
area->cursor_x = 1;
area->cursor_y++;
}
if ( area->cursor_y > height + bsh ) // Prevent up scrolling
{ if ( area->cursor_y > height + bsh )
area->cursor_y--; {
break; area->cursor_y--;
break;
}
} }
len++; len++;
@ -720,6 +591,40 @@ int FVTerm::print (register int c)
int FVTerm::print (term_area* area, register int c) int FVTerm::print (term_area* area, register int c)
{ {
char_data nc; // next character char_data nc; // next character
if ( ! area )
return -1;
nc.code = c;
nc.fg_color = next_attribute.fg_color;
nc.bg_color = next_attribute.bg_color;
nc.attr.byte[0] = next_attribute.attr.byte[0];
nc.attr.byte[1] = next_attribute.attr.byte[1];
nc.attr.byte[2] = 0;
return print (area, nc);
}
//----------------------------------------------------------------------
int FVTerm::print (char_data& term_char)
{
term_area* area = getPrintArea();
if ( ! area )
{
if ( vdesktop )
area = vdesktop;
else
return -1;
}
return print (area, term_char);
}
//----------------------------------------------------------------------
int FVTerm::print (term_area* area, char_data& term_char)
{
char_data& nc = term_char; // next character
int width, height, rsh, bsh, ax, ay; int width, height, rsh, bsh, ax, ay;
if ( ! area ) if ( ! area )
@ -732,13 +637,6 @@ int FVTerm::print (term_area* area, register int c)
ax = area->cursor_x - 1; ax = area->cursor_x - 1;
ay = area->cursor_y - 1; ay = area->cursor_y - 1;
nc.code = c;
nc.fg_color = next_attribute.fg_color;
nc.bg_color = next_attribute.bg_color;
nc.attr.byte[0] = next_attribute.attr.byte[0];
nc.attr.byte[1] = next_attribute.attr.byte[1];
nc.attr.byte[2] = 0;
if ( area->cursor_x > 0 if ( area->cursor_x > 0
&& area->cursor_y > 0 && area->cursor_y > 0
&& ax < area->width + area->right_shadow && ax < area->width + area->right_shadow
@ -780,12 +678,14 @@ int FVTerm::print (term_area* area, register int c)
area->cursor_x++; area->cursor_x++;
area->has_changes = true; area->has_changes = true;
// Line break at right margin
if ( area->cursor_x > width + rsh ) if ( area->cursor_x > width + rsh )
{ {
area->cursor_x = 1; area->cursor_x = 1;
area->cursor_y++; area->cursor_y++;
} }
// Prevent up scrolling
if ( area->cursor_y > height + bsh ) if ( area->cursor_y > height + bsh )
{ {
area->cursor_y--; area->cursor_y--;
@ -795,6 +695,7 @@ int FVTerm::print (term_area* area, register int c)
return 1; return 1;
} }
//----------------------------------------------------------------------
// protected methods of FVTerm // protected methods of FVTerm
//---------------------------------------------------------------------- //----------------------------------------------------------------------

View File

@ -2474,7 +2474,7 @@ void FWidget::setColorTheme()
set16ColorTheme(); set16ColorTheme();
if ( isKdeTerminal() ) if ( isKdeTerminal() )
wc.term_bg = fc::SteelBlue3; wc.term_bg = fc::SteelBlue3;
} }
} }