From 051fbab046f0311c0ef382b97ee348db7bb73567 Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Wed, 22 Nov 2017 23:56:21 +0100 Subject: [PATCH] Remove duplicated code in FVTerm::print --- ChangeLog | 3 + examples/ui.cpp | 67 ++++++----- include/final/fterm.h | 8 +- include/final/fvterm.h | 2 + src/fterm.cpp | 29 +++-- src/fvterm.cpp | 251 +++++++++++++---------------------------- src/fwidget.cpp | 2 +- 7 files changed, 151 insertions(+), 211 deletions(-) diff --git a/ChangeLog b/ChangeLog index 722df601..e5fb2e87 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +2017-11-22 Markus Gans + * Remove duplicated code in FVTerm::print + 2017-11-19 Markus Gans * Splitting the FString example into sub-functions to make the code more comprehensible diff --git a/examples/ui.cpp b/examples/ui.cpp index c56e9d9f..95331749 100644 --- a/examples/ui.cpp +++ b/examples/ui.cpp @@ -277,6 +277,7 @@ class MyDialog : public FDialog void initStatusBar(); void initStatusBarCallbacks(); void initWidgets(); + void initWidgetsCallbacks(); void adjustSize(); // Event handlers @@ -300,24 +301,32 @@ class MyDialog : public FDialog void cb_setInput (FWidget*, data_ptr); // Data Members - FMenuItem* Open; - FMenuItem* Quit; - FMenuItem* File1; - FMenuItem* File2; - FMenuItem* File3; - FMenuItem* Cut; - FMenuItem* Copy; - FMenuItem* Paste; - FMenuItem* Clear; - FMenuItem* Env; - FMenuItem* Drive; - FMenuItem* Help; - FStatusKey* key_F1; - FStatusKey* key_F2; - FStatusKey* key_F3; - FLineEdit* myLineEdit; - FListBox* myList; - FString clipboard; + FMenuItem* Open; + FMenuItem* Quit; + FMenuItem* File1; + FMenuItem* File2; + FMenuItem* File3; + FMenuItem* Cut; + FMenuItem* Copy; + FMenuItem* Paste; + FMenuItem* Clear; + FMenuItem* Env; + FMenuItem* Drive; + FMenuItem* Help; + 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; }; #pragma pack(pop) @@ -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", diff --git a/include/final/fterm.h b/include/final/fterm.h index 39f1faca..d8a7e9c9 100644 --- a/include/final/fterm.h +++ b/include/final/fterm.h @@ -60,7 +60,11 @@ #if defined(__linux__) #include // Linux framebuffer console #include // need for gpm keyboard modifiers - #include // is deprecated + + #if defined(__x86_64__) || defined(__i386) || defined(__arm__) + #include // is deprecated + #endif + #include #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 diff --git a/include/final/fvterm.h b/include/final/fvterm.h index c13251de..96cd0524 100644 --- a/include/final/fvterm.h +++ b/include/final/fvterm.h @@ -244,6 +244,8 @@ class FVTerm : public FTerm int print (term_area*, const std::vector&); int print (int); int print (term_area*, int); + int print (char_data&); + int print (term_area*, char_data&); FVTerm& print(); protected: diff --git a/src/fterm.cpp b/src/fterm.cpp index e968eb8d..e1455dd3 100644 --- a/src/fterm.cpp +++ b/src/fterm.cpp @@ -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; } - #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 ( isLinuxConsole() ) @@ -2721,16 +2726,22 @@ void FTerm::detectTerminal() } closeConsole(); - setLinuxConsoleCursorStyle (fc::underscore_cursor, true); } - if ( linux_terminal && getFramebuffer_bpp() >= 4 ) - FTermcap::max_color = 16; - #endif + if ( linux_terminal ) + { + 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); - #endif +#endif t.c_lflag |= uInt(ICANON | ECHO); tcsetattr(stdin_no, TCSADRAIN, &t); @@ -3833,7 +3844,7 @@ void FTerm::init_OptiMove() //---------------------------------------------------------------------- void FTerm::init_OptiAttr() { - // Setting video attribute optimization + // Setting video attribute optimization opti_attr->setNoColorVideo (int(FTermcap::attr_without_color)); opti_attr->set_enter_bold_mode (TCAP(fc::t_enter_bold_mode)); @@ -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 diff --git a/src/fvterm.cpp b/src/fvterm.cpp index f6df2f14..b82b59b0 100644 --- a/src/fvterm.cpp +++ b/src/fvterm.cpp @@ -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 term_string; if ( ! area ) return -1; @@ -455,120 +454,29 @@ 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; - 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; - } - + 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; + 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& termString) +int FVTerm::print (const std::vector& 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& termString) return -1; } - return print (area, termString); + return print (area, term_string); } //---------------------------------------------------------------------- -int FVTerm::print (term_area* area, const std::vector& termString) +int FVTerm::print (term_area* area, const std::vector& term_string) { register int len = 0; std::vector::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; - bsh = area->bottom_shadow; + int width = area->width, + height = area->height, + rsh = area->right_shadow, + bsh = area->bottom_shadow; + bool printable_character = false; switch ( (*iter).code ) { @@ -635,62 +541,27 @@ int FVTerm::print (term_area* area, const std::vector& termString) default: { - int ax = area->cursor_x - 1; - int ay = area->cursor_y - 1; - char_data nc = *iter; // next character - - 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++; + print (area, nc); + printable_character = true; } } - if ( area->cursor_x > width + rsh ) + if ( ! printable_character ) { - area->cursor_x = 1; - area->cursor_y++; - } + // Line break at right margin + if ( area->cursor_x > width + rsh ) + { + area->cursor_x = 1; + area->cursor_y++; + } - if ( area->cursor_y > height + bsh ) - { - area->cursor_y--; - break; + // Prevent up scrolling + if ( area->cursor_y > height + bsh ) + { + area->cursor_y--; + break; + } } len++; @@ -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 //---------------------------------------------------------------------- diff --git a/src/fwidget.cpp b/src/fwidget.cpp index 5a30f6e4..4933f09f 100644 --- a/src/fwidget.cpp +++ b/src/fwidget.cpp @@ -2474,7 +2474,7 @@ void FWidget::setColorTheme() set16ColorTheme(); if ( isKdeTerminal() ) - wc.term_bg = fc::SteelBlue3; + wc.term_bg = fc::SteelBlue3; } }