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>
* Splitting the FString example into sub-functions to make
the code more comprehensible

View File

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

View File

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

View File

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

View File

@ -1954,6 +1954,7 @@ bool FTerm::isWSConsConsole()
#endif
#if defined(__linux__)
#if defined(__x86_64__) || defined(__i386) || defined(__arm__)
//----------------------------------------------------------------------
inline uInt16 FTerm::getInputStatusRegisterOne()
{
@ -2052,6 +2053,7 @@ int FTerm::setBlinkAsIntensity (bool on)
return 0;
}
#endif
//----------------------------------------------------------------------
int FTerm::getFramebuffer_bpp ()
@ -2709,7 +2711,10 @@ void FTerm::detectTerminal()
FTermcap::max_color = 16;
}
// Initialize Linux console
#if defined(__linux__)
#if defined(__x86_64__) || defined(__i386) || defined(__arm__)
// Enable 16 background colors
if ( linux_terminal && openConsole() == 0 )
{
if ( isLinuxConsole() )
@ -2721,13 +2726,19 @@ void FTerm::detectTerminal()
}
closeConsole();
setLinuxConsoleCursorStyle (fc::underscore_cursor, true);
}
if ( linux_terminal && getFramebuffer_bpp() >= 4 )
if ( linux_terminal )
{
setLinuxConsoleCursorStyle (fc::underscore_cursor, true);
if ( getFramebuffer_bpp() >= 4 )
FTermcap::max_color = 16;
}
#endif
#endif
// Initialize FreeBSD console cursor
#if defined(__FreeBSD__) || defined(__DragonFly__)
setFreeBSDConsoleCursorStyle (fc::destructive_cursor, true);
#endif
@ -4287,7 +4298,9 @@ void FTerm::finish()
#if defined(__linux__)
if ( linux_terminal )
{
#if defined(__x86_64__) || defined(__i386) || defined(__arm__)
setBlinkAsIntensity (false);
#endif
setLinuxConsoleCursorStyle (fc::default_cursor, false);
}
#endif

View File

@ -444,9 +444,8 @@ int FVTerm::print (const FString& s)
int FVTerm::print (term_area* area, const FString& s)
{
assert ( ! s.isNull() );
register int len = 0;
const wchar_t* p;
uInt tabstop = uInt(getTabstop());
std::vector<char_data> term_string;
if ( ! area )
return -1;
@ -455,47 +454,8 @@ int FVTerm::print (term_area* area, const FString& s)
if ( p )
{
if ( *p )
area->has_changes = true;
while ( *p )
{
int width, height, rsh, bsh;
width = area->width;
height = area->height;
rsh = area->right_shadow;
bsh = area->bottom_shadow;
switch ( *p )
{
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;
@ -503,72 +463,20 @@ int FVTerm::print (term_area* area, const FString& s)
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;
}
term_string.push_back(nc);
p++;
len++;
} // 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;
term_area* area = getPrintArea();
@ -581,32 +489,30 @@ int FVTerm::print (const std::vector<char_data>& termString)
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;
std::vector<char_data>::const_iterator iter;
iter = termString.begin();
iter = term_string.begin();
uInt tabstop = uInt(getTabstop());
if ( ! area )
return -1;
if ( termString.empty() )
if ( term_string.empty() )
return 0;
else
area->has_changes = true;
while ( iter != termString.end() )
while ( iter != term_string.end() )
{
int width, height, rsh, bsh;
width = area->width;
height = area->height;
rsh = area->right_shadow;
int width = area->width,
height = area->height,
rsh = area->right_shadow,
bsh = area->bottom_shadow;
bool printable_character = false;
switch ( (*iter).code )
{
@ -635,63 +541,28 @@ int FVTerm::print (term_area* area, const std::vector<char_data>& termString)
default:
{
int ax = area->cursor_x - 1;
int ay = area->cursor_y - 1;
char_data nc = *iter; // next character
print (area, nc);
printable_character = true;
}
}
if ( area->cursor_x > 0
&& area->cursor_y > 0
&& ax < area->width + area->right_shadow
&& ay < area->height + area->bottom_shadow )
if ( ! printable_character )
{
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++;
}
}
// Line break at right margin
if ( area->cursor_x > width + rsh )
{
area->cursor_x = 1;
area->cursor_y++;
}
// Prevent up scrolling
if ( area->cursor_y > height + bsh )
{
area->cursor_y--;
break;
}
}
len++;
++iter;
@ -720,6 +591,40 @@ int FVTerm::print (register int c)
int FVTerm::print (term_area* area, register int c)
{
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;
if ( ! area )
@ -732,13 +637,6 @@ int FVTerm::print (term_area* area, register int c)
ax = area->cursor_x - 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
&& area->cursor_y > 0
&& ax < area->width + area->right_shadow
@ -780,12 +678,14 @@ int FVTerm::print (term_area* area, register int c)
area->cursor_x++;
area->has_changes = true;
// Line break at right margin
if ( area->cursor_x > width + rsh )
{
area->cursor_x = 1;
area->cursor_y++;
}
// Prevent up scrolling
if ( area->cursor_y > height + bsh )
{
area->cursor_y--;
@ -795,6 +695,7 @@ int FVTerm::print (term_area* area, register int c)
return 1;
}
//----------------------------------------------------------------------
// protected methods of FVTerm
//----------------------------------------------------------------------