From d1083b6a950d9e06bb6e771eb0dcfae0d4e3a581 Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Sun, 4 Feb 2018 19:42:30 +0100 Subject: [PATCH] Refactoring of some functions --- examples/listview.cpp | 55 +++++++------ examples/treeview.cpp | 2 +- include/final/fterm.h | 5 ++ include/final/fvterm.h | 3 + scripts/valgrind.sh | 1 + src/fterm.cpp | 171 +++++++++++++++++++++++------------------ src/fvterm.cpp | 145 +++++++++++++++++++--------------- 7 files changed, 220 insertions(+), 162 deletions(-) diff --git a/examples/listview.cpp b/examples/listview.cpp index a98ca484..f00eb211 100644 --- a/examples/listview.cpp +++ b/examples/listview.cpp @@ -3,7 +3,7 @@ * * * This file is part of the Final Cut widget toolkit * * * -* Copyright 2017 Markus Gans * +* Copyright 2017-2018 Markus Gans * * * * The Final Cut is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public License * @@ -49,6 +49,9 @@ class Listview : public FDialog // Disable assignment operator (=) Listview& operator = (const Listview&); + // Method + void populate (FListView*); + // Event handlers void onClose (FCloseEvent*); @@ -78,6 +81,34 @@ Listview::Listview (FWidget* parent) listView->setColumnAlignment (5, fc::alignRight); // Populate FListView with a list of items + populate (listView); + + // Quit button + FButton* Quit = new FButton (this); + Quit->setGeometry(24, 16, 10, 1); + Quit->setText (L"&Quit"); + + // Add some function callbacks + Quit->addCallback + ( + "clicked", + F_METHOD_CALLBACK (this, &FApplication::cb_exitApp) + ); + + listView->addCallback + ( + "clicked", + F_METHOD_CALLBACK (this, &Listview::cb_showInMessagebox) + ); +} + +//---------------------------------------------------------------------- +Listview::~Listview() // destructor +{ } + +//---------------------------------------------------------------------- +void Listview::populate (FListView* listView) +{ std::string weather[][5] = { { "Alexandria", "Sunny", "31°C", "61%", "1006.4 mb" }, @@ -130,30 +161,8 @@ Listview::Listview (FWidget* parent) FStringList line (&weather[i][0], &weather[i][0] + 5); listView->insert (line); } - - // Quit button - FButton* Quit = new FButton (this); - Quit->setGeometry(24, 16, 10, 1); - Quit->setText (L"&Quit"); - - // Add some function callbacks - Quit->addCallback - ( - "clicked", - F_METHOD_CALLBACK (this, &FApplication::cb_exitApp) - ); - - listView->addCallback - ( - "clicked", - F_METHOD_CALLBACK (this, &Listview::cb_showInMessagebox) - ); } -//---------------------------------------------------------------------- -Listview::~Listview() // destructor -{ } - //---------------------------------------------------------------------- void Listview::onClose (FCloseEvent* ev) { diff --git a/examples/treeview.cpp b/examples/treeview.cpp index 509027c4..0bfcc255 100644 --- a/examples/treeview.cpp +++ b/examples/treeview.cpp @@ -52,7 +52,7 @@ class Treeview : public FDialog // Disable assignment operator (=) Treeview& operator = (const Treeview&); - // Method + // Methods void adjustSize(); TreeItem* getAfrica(); TreeItem* getAsia(); diff --git a/include/final/fterm.h b/include/final/fterm.h index e7f73743..fe78a481 100644 --- a/include/final/fterm.h +++ b/include/final/fterm.h @@ -518,11 +518,16 @@ class FTerm static void restoreColorPalette(); static void enableMouse(); static void disableMouse(); + static void useAlternateScreenBuffer(); + static void useNormalScreenBuffer(); static void captureXTermFontAndTitle(); void allocationValues(); void deallocationValues(); void init(); + void initOSspecifics(); void finish(); + void finishOSspecifics1(); + void finishOSspecifics2(); static uInt cp437_to_unicode (uChar); static int getMouseProtocolKey (char[]); static int getTermcapKey (char[], int); diff --git a/include/final/fvterm.h b/include/final/fvterm.h index ddd312cf..b6755edf 100644 --- a/include/final/fvterm.h +++ b/include/final/fvterm.h @@ -300,6 +300,9 @@ class FVTerm : public FTerm static void removeArea (term_area*&); static void restoreVTerm (const FRect&); static void restoreVTerm (int, int, int, int); + static void setTextToDefault (term_area*, int, int); + static bool reallocateTextArea (term_area*, int, int); + static bool reallocateTextArea (term_area*, int); static covered_state isCovered ( const FPoint& , term_area* ); diff --git a/scripts/valgrind.sh b/scripts/valgrind.sh index 10ceb2d8..d3d964aa 100755 --- a/scripts/valgrind.sh +++ b/scripts/valgrind.sh @@ -7,6 +7,7 @@ then PROG="../examples/.libs/ui" else PROG="$1" + shift fi # Is the file executable? diff --git a/src/fterm.cpp b/src/fterm.cpp index 79ddf866..5a1ba2e8 100644 --- a/src/fterm.cpp +++ b/src/fterm.cpp @@ -4287,6 +4287,48 @@ void FTerm::disableMouse() mouse->disable(); } +//---------------------------------------------------------------------- +void FTerm::useAlternateScreenBuffer() +{ + if ( ! use_alternate_screen ) + return; + + // Save current cursor position + if ( TCAP(fc::t_save_cursor) ) + { + putstring (TCAP(fc::t_save_cursor)); + std::fflush(stdout); + } + + // Saves the screen and the cursor position + if ( TCAP(fc::t_enter_ca_mode) ) + { + putstring (TCAP(fc::t_enter_ca_mode)); + std::fflush(stdout); + } +} + +//---------------------------------------------------------------------- +void FTerm::useNormalScreenBuffer() +{ + if ( ! use_alternate_screen ) + return; + + // restores the screen and the cursor position + if ( TCAP(fc::t_exit_ca_mode) ) + { + putstring (TCAP(fc::t_exit_ca_mode)); + std::fflush(stdout); + } + + // restore cursor to position of last save_cursor + if ( TCAP(fc::t_restore_cursor) ) + { + putstring (TCAP(fc::t_restore_cursor)); + std::fflush(stdout); + } +} + //---------------------------------------------------------------------- void FTerm::captureXTermFontAndTitle() { @@ -4381,26 +4423,7 @@ void FTerm::init() if ( ttyname_r(stdout_no, term_name, sizeof(term_name)) ) term_name[0] = '\0'; -#if defined(__linux__) - - // initialize Linux console - initLinuxConsole(); - -#endif - -#if defined(__FreeBSD__) || defined(__DragonFly__) - - // Initialize BSD console - initFreeBSDConsole(); - -#endif - -#if defined(__NetBSD__) || defined(__OpenBSD__) - - // Initialize wscons console - initWSConsConsole(); - -#endif + initOSspecifics(); // Save termios settings storeTTYsettings(); @@ -4449,22 +4472,7 @@ void FTerm::init() std::fflush(stdout); } - if ( use_alternate_screen ) - { - // Save current cursor position - if ( TCAP(fc::t_save_cursor) ) - { - putstring (TCAP(fc::t_save_cursor)); - std::fflush(stdout); - } - - // Saves the screen and the cursor position - if ( TCAP(fc::t_enter_ca_mode) ) - { - putstring (TCAP(fc::t_enter_ca_mode)); - std::fflush(stdout); - } - } + useAlternateScreenBuffer(); // Enable alternate charset if ( TCAP(fc::t_enable_acs) ) @@ -4512,6 +4520,25 @@ void FTerm::init() term_initialized = true; } +//---------------------------------------------------------------------- +void FTerm::initOSspecifics() +{ +#if defined(__linux__) + // initialize Linux console + initLinuxConsole(); +#endif + +#if defined(__FreeBSD__) || defined(__DragonFly__) + // Initialize BSD console + initFreeBSDConsole(); +#endif + +#if defined(__NetBSD__) || defined(__OpenBSD__) + // Initialize wscons console + initWSConsConsole(); +#endif +} + //---------------------------------------------------------------------- void FTerm::finish() { @@ -4555,6 +4582,36 @@ void FTerm::finish() std::fflush(stdout); } + finishOSspecifics1(); + + if ( kde_konsole ) + setKDECursor(fc::BlockCursor); + + resetBeep(); + + // Disable the terminal mouse support + disableMouse(); + + // Deactivate meta key sends escape + if ( xterm_terminal ) + xtermMetaSendsESC(false); + + useNormalScreenBuffer(); + + // leave 'keyboard_transmit' mode + if ( TCAP(fc::t_keypad_local) ) + { + putstring (TCAP(fc::t_keypad_local)); + std::fflush(stdout); + } + + finishOSspecifics2(); + deallocationValues(); +} + +//---------------------------------------------------------------------- +void FTerm::finishOSspecifics1() +{ #if defined(__linux__) if ( linux_terminal ) { @@ -4573,43 +4630,11 @@ void FTerm::finish() #if defined(__NetBSD__) || defined(__OpenBSD__) resetWSConsEncoding(); #endif +} - if ( kde_konsole ) - setKDECursor(fc::BlockCursor); - - resetBeep(); - - // Disable the terminal mouse support - disableMouse(); - - // Deactivate meta key sends escape - if ( xterm_terminal ) - xtermMetaSendsESC(false); - - if ( use_alternate_screen ) - { - // restores the screen and the cursor position - if ( TCAP(fc::t_exit_ca_mode) ) - { - putstring (TCAP(fc::t_exit_ca_mode)); - std::fflush(stdout); - } - - // restore cursor to position of last save_cursor - if ( TCAP(fc::t_restore_cursor) ) - { - putstring (TCAP(fc::t_restore_cursor)); - std::fflush(stdout); - } - } - - // leave 'keyboard_transmit' mode - if ( TCAP(fc::t_keypad_local) ) - { - putstring (TCAP(fc::t_keypad_local)); - std::fflush(stdout); - } - +//---------------------------------------------------------------------- +void FTerm::finishOSspecifics2() +{ #if defined(__linux__) if ( linux_terminal && utf8_console ) setUTF8(true); @@ -4625,8 +4650,6 @@ void FTerm::finish() delete[] screen_unicode_map.entries; } #endif - - deallocationValues(); } //---------------------------------------------------------------------- diff --git a/src/fvterm.cpp b/src/fvterm.cpp index ec2b03d7..1b1f53d2 100644 --- a/src/fvterm.cpp +++ b/src/fvterm.cpp @@ -832,8 +832,7 @@ void FVTerm::resizeArea ( int offset_left, int offset_top assert ( rsw >= 0 ); assert ( bsh >= 0 ); int area_size; - char_data default_char; - line_changes unchanged; + bool realloc_success = false; if ( ! area ) return; @@ -856,48 +855,37 @@ void FVTerm::resizeArea ( int offset_left, int offset_top if ( area->height + area->bottom_shadow != height + bsh ) { - if ( area->changes != 0 ) - delete[] area->changes; - - if ( area->text != 0 ) - delete[] area->text; - - try - { - area->changes = new line_changes[height + bsh]; - area->text = new char_data[area_size]; - } - catch (const std::bad_alloc& ex) - { - std::cerr << "not enough memory to alloc " << ex.what() << std::endl; - return; - } + realloc_success = reallocateTextArea (area, height + bsh, area_size); } else if ( area->width + area->right_shadow != width + rsw ) { - if ( area->text != 0 ) - delete[] area->text; - - try - { - area->text = new char_data[area_size]; - } - catch (const std::bad_alloc& ex) - { - std::cerr << "not enough memory to alloc " << ex.what() << std::endl; - return; - } + realloc_success = reallocateTextArea (area, area_size); } else return; - area->offset_left = offset_left; - area->offset_top = offset_top; - area->width = width; - area->height = height; - area->right_shadow = rsw; - area->bottom_shadow = bsh; - area->has_changes = false; + if ( ! realloc_success ) + return; + + area->offset_left = offset_left; + area->offset_top = offset_top; + area->width = width; + area->height = height; + area->right_shadow = rsw; + area->bottom_shadow = bsh; + area->has_changes = false; + + setTextToDefault (area, width + rsw, height + bsh); +} + +//---------------------------------------------------------------------- +inline void FVTerm::setTextToDefault ( term_area* area + , int width + , int height ) +{ + char_data default_char; + line_changes unchanged; + int size = width * height; default_char.code = ' '; default_char.fg_color = fc::Default; @@ -906,13 +894,57 @@ void FVTerm::resizeArea ( int offset_left, int offset_top default_char.attr.byte[1] = 0; default_char.attr.byte[2] = 0; - std::fill_n (area->text, area_size, default_char); + std::fill_n (area->text, size, default_char); - unchanged.xmin = uInt(width + rsw); + unchanged.xmin = uInt(width); unchanged.xmax = 0; unchanged.trans_count = 0; - std::fill_n (area->changes, height + bsh, unchanged); + std::fill_n (area->changes, height, unchanged); +} + +//---------------------------------------------------------------------- +inline bool FVTerm::reallocateTextArea ( term_area* area + , int height + , int size ) +{ + if ( area->changes != 0 ) + delete[] area->changes; + + if ( area->text != 0 ) + delete[] area->text; + + try + { + area->changes = new line_changes[height]; + area->text = new char_data[size]; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return false; + } + + return true; +} + +//---------------------------------------------------------------------- +inline bool FVTerm::reallocateTextArea (term_area* area, int size) +{ + if ( area->text != 0 ) + delete[] area->text; + + try + { + area->text = new char_data[size]; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return false; + } + + return true; } //---------------------------------------------------------------------- @@ -2221,9 +2253,9 @@ void FVTerm::flush_out() //---------------------------------------------------------------------- void FVTerm::init() { - init_object = this; - vterm = 0; - vdesktop = 0; + init_object = this; + vterm = 0; + vdesktop = 0; try { @@ -2243,27 +2275,12 @@ void FVTerm::init() stop_terminal_updates = false; // term_attribute stores the current state of the terminal - term_attribute.code = '\0'; - term_attribute.fg_color = fc::Default; - term_attribute.bg_color = fc::Default; - term_attribute.attr.bit.bold = \ - term_attribute.attr.bit.dim = \ - term_attribute.attr.bit.italic = \ - term_attribute.attr.bit.underline = \ - term_attribute.attr.bit.blink = \ - term_attribute.attr.bit.reverse = \ - term_attribute.attr.bit.standout = \ - term_attribute.attr.bit.invisible = \ - term_attribute.attr.bit.protect = \ - term_attribute.attr.bit.crossed_out = \ - term_attribute.attr.bit.dbl_underline = \ - term_attribute.attr.bit.alt_charset = \ - term_attribute.attr.bit.pc_charset = \ - term_attribute.attr.bit.transparent = \ - term_attribute.attr.bit.trans_shadow = \ - term_attribute.attr.bit.inherit_bg = \ - term_attribute.attr.bit.no_changes = \ - term_attribute.attr.bit.printed = false; + term_attribute.code = '\0'; + term_attribute.fg_color = fc::Default; + term_attribute.bg_color = fc::Default; + term_attribute.attr.byte[0] = 0; + term_attribute.attr.byte[0] = 0; + term_attribute.attr.byte[0] = 0; // next_attribute contains the state of the next printed character std::memcpy (&next_attribute, &term_attribute, sizeof(char_data));