A bit of refactoring

This commit is contained in:
Markus Gans 2019-01-12 09:11:22 +01:00
parent 016f15c860
commit cb090e8aff
14 changed files with 205 additions and 242 deletions

View File

@ -1,3 +1,16 @@
2019-01-12 Markus Gans <guru.mail@muenster.de>
* Refactoring FFileDialog::fileOpenChooser
* Refactoring FFileDialog::fileSaveChooser
* Refactoring FListBox::init()
* Refactoring FListView::init()
* Refactoring FTextView::init()
* Refactoring FTermXTerminal::resetXTermForeground()
* Refactoring FTermXTerminal::resetXTermBackground()
* Refactoring FTermXTerminal::resetXTermCursorColor()
* Refactoring FTermXTerminal::resetXTermMouseForeground()
* Refactoring FTermXTerminal::resetXTermMouseBackground()
* Refactoring FTermXTerminal::resetXTermHighlightBackground()
2019-01-11 Markus Gans <guru.mail@muenster.de> 2019-01-11 Markus Gans <guru.mail@muenster.de>
* Generalize hide() method * Generalize hide() method

View File

@ -52,6 +52,40 @@ bool sortDirFirst ( const FFileDialog::dir_entry& lhs
return false; return false;
} }
//----------------------------------------------------------------------
const FString fileChooser ( FWidget* parent
, const FString& dirname
, const FString& filter
, FFileDialog::DialogType type )
{
FString ret;
FString path = dirname;
FString file_filter = filter;
if ( path.isNull() || path.isEmpty() )
{
path = FFileDialog::getHomeDir();
if ( path.isNull() || path.isEmpty() )
path = FString("/");
}
if ( file_filter.isNull() || file_filter.isEmpty() )
file_filter = FString("*");
FFileDialog fileopen ( path
, file_filter
, type
, parent );
if ( fileopen.exec() == FDialog::Accept )
ret = fileopen.getPath() + fileopen.getSelectedFile();
else
ret = FString();
return ret;
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// class FFileDialog // class FFileDialog
@ -225,32 +259,7 @@ const FString FFileDialog::fileOpenChooser ( FWidget* parent
, const FString& dirname , const FString& dirname
, const FString& filter ) , const FString& filter )
{ {
FString ret; return fileChooser (parent, dirname, filter, FFileDialog::Open);
FString path = dirname;
FString file_filter = filter;
if ( path.isNull() || path.isEmpty() )
{
path = getHomeDir();
if ( path.isNull() || path.isEmpty() )
path = FString("/");
}
if ( file_filter.isNull() || file_filter.isEmpty() )
file_filter = FString("*");
FFileDialog fileopen ( path
, file_filter
, FFileDialog::Open
, parent );
if ( fileopen.exec() == FDialog::Accept )
ret = fileopen.getPath() + fileopen.getSelectedFile();
else
ret = FString();
return ret;
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -258,32 +267,7 @@ const FString FFileDialog::fileSaveChooser ( FWidget* parent
, const FString& dirname , const FString& dirname
, const FString& filter ) , const FString& filter )
{ {
FString ret; return fileChooser (parent, dirname, filter, FFileDialog::Save);
FString path = dirname;
FString file_filter = filter;
if ( path.isNull() || path.isEmpty() )
{
path = getHomeDir();
if ( path.isNull() || path.isEmpty() )
path = FString("/");
}
if ( file_filter.isNull() || file_filter.isEmpty() )
file_filter = FString("*");
FFileDialog fileopen ( path
, file_filter
, FFileDialog::Save
, parent );
if ( fileopen.exec() == FDialog::Accept )
ret = fileopen.getPath() + fileopen.getSelectedFile();
else
ret = FString();
return ret;
} }

View File

@ -760,37 +760,8 @@ inline FString& FListBox::getString (listBoxItems::iterator iter)
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FListBox::init() void FListBox::init()
{ {
try initScrollbar (vbar, fc::vertical, &FListBox::cb_VBarChange);
{ initScrollbar (hbar, fc::horizontal, &FListBox::cb_HBarChange);
vbar = std::make_shared<FScrollbar>(fc::vertical, this);
hbar = std::make_shared<FScrollbar>(fc::horizontal, this);
}
catch (const std::bad_alloc& ex)
{
std::cerr << bad_alloc_str << ex.what() << std::endl;
return;
}
vbar->setMinimum(0);
vbar->setValue(0);
vbar->hide();
hbar->setMinimum(0);
hbar->setValue(0);
hbar->hide();
vbar->addCallback
(
"change-value",
F_METHOD_CALLBACK (this, &FListBox::cb_VBarChange)
);
hbar->addCallback
(
"change-value",
F_METHOD_CALLBACK (this, &FListBox::cb_HBarChange)
);
setGeometry (1, 1, 5, 4, false); // initialize geometry values setGeometry (1, 1, 5, 4, false); // initialize geometry values
setForegroundColor (wc.dialog_fg); setForegroundColor (wc.dialog_fg);
setBackgroundColor (wc.dialog_bg); setBackgroundColor (wc.dialog_bg);
@ -801,6 +772,32 @@ void FListBox::init()
setRightPadding(1 + int(nf_offset)); setRightPadding(1 + int(nf_offset));
} }
//----------------------------------------------------------------------
void FListBox::initScrollbar ( FScrollbarPtr& bar
, fc::orientation o
, FListBoxCallback callback )
{
try
{
bar = std::make_shared<FScrollbar>(o, this);
}
catch (const std::bad_alloc& ex)
{
std::cerr << bad_alloc_str << ex.what() << std::endl;
return;
}
bar->setMinimum(0);
bar->setValue(0);
bar->hide();
bar->addCallback
(
"change-value",
F_METHOD_CALLBACK (this, callback)
);
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FListBox::draw() void FListBox::draw()
{ {

View File

@ -1369,37 +1369,8 @@ void FListView::adjustSize()
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FListView::init() void FListView::init()
{ {
try initScrollbar (vbar, fc::vertical, &FListView::cb_VBarChange);
{ initScrollbar (hbar, fc::horizontal, &FListView::cb_HBarChange);
vbar = std::make_shared<FScrollbar>(fc::vertical, this);
hbar = std::make_shared<FScrollbar>(fc::horizontal, this);
}
catch (const std::bad_alloc& ex)
{
std::cerr << bad_alloc_str << ex.what() << std::endl;
return;
}
vbar->setMinimum(0);
vbar->setValue(0);
vbar->hide();
hbar->setMinimum(0);
hbar->setValue(0);
hbar->hide();
vbar->addCallback
(
"change-value",
F_METHOD_CALLBACK (this, &FListView::cb_VBarChange)
);
hbar->addCallback
(
"change-value",
F_METHOD_CALLBACK (this, &FListView::cb_HBarChange)
);
selflist.push_back(this); selflist.push_back(this);
root = selflist.begin(); root = selflist.begin();
null_iter = selflist.end(); null_iter = selflist.end();
@ -1413,6 +1384,32 @@ void FListView::init()
setRightPadding(1 + int(nf_offset)); setRightPadding(1 + int(nf_offset));
} }
//----------------------------------------------------------------------
void FListView::initScrollbar ( FScrollbarPtr& bar
, fc::orientation o
, FListViewCallback callback )
{
try
{
bar = std::make_shared<FScrollbar>(o, this);
}
catch (const std::bad_alloc& ex)
{
std::cerr << bad_alloc_str << ex.what() << std::endl;
return;
}
bar->setMinimum(0);
bar->setValue(0);
bar->hide();
bar->addCallback
(
"change-value",
F_METHOD_CALLBACK (this, callback)
);
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
template <typename Compare> template <typename Compare>
void FListView::sort (Compare cmp) void FListView::sort (Compare cmp)

View File

@ -42,7 +42,7 @@ FScrollbar::FScrollbar(FWidget* parent)
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
FScrollbar::FScrollbar(int o, FWidget* parent) FScrollbar::FScrollbar(fc::orientation o, FWidget* parent)
: FWidget(parent) : FWidget(parent)
{ {
setOrientation (o); setOrientation (o);
@ -124,7 +124,7 @@ void FScrollbar::setPageSize (int document_size, int page_size)
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FScrollbar::setOrientation (int o) void FScrollbar::setOrientation (fc::orientation o)
{ {
std::size_t nf = 0; std::size_t nf = 0;
length = ( getHeight() > getWidth() ) ? getHeight() : getWidth(); length = ( getHeight() > getWidth() ) ? getHeight() : getWidth();

View File

@ -605,23 +605,18 @@ void FTermXTerminal::resetXTermColorMap()
{ {
// Reset the entire color table // Reset the entire color table
if ( term_detection->isGnomeTerminal() if ( term_detection->isMinttyTerm() )
&& term_detection->getGnomeTerminalID() < 3502 ) {
return; FTerm::putstringf (ESC "c"); // Full Reset (RIS)
}
if ( term_detection->isPuttyTerminal() else if ( canResetColor() )
|| term_detection->isMltermTerminal() )
return;
if ( term_detection->isXTerminal()
|| term_detection->isScreenTerm()
|| FTermcap::osc_support )
{ {
oscPrefix(); oscPrefix();
FTerm::putstringf (OSC "104" BEL); FTerm::putstringf (OSC "104" BEL);
oscPostfix(); oscPostfix();
std::fflush(stdout); std::fflush(stdout);
} }
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -629,17 +624,7 @@ void FTermXTerminal::resetXTermForeground()
{ {
// Reset the XTerm text foreground color // Reset the XTerm text foreground color
if ( term_detection->isGnomeTerminal() if ( canResetColor() )
&& term_detection->getGnomeTerminalID() < 3502 )
return;
if ( term_detection->isPuttyTerminal()
|| term_detection->isMltermTerminal() )
return;
if ( term_detection->isXTerminal()
|| term_detection->isScreenTerm()
|| FTermcap::osc_support )
{ {
oscPrefix(); oscPrefix();
FTerm::putstring (OSC "110" BEL); FTerm::putstring (OSC "110" BEL);
@ -653,17 +638,7 @@ void FTermXTerminal::resetXTermBackground()
{ {
// Reset the XTerm text background color // Reset the XTerm text background color
if ( term_detection->isGnomeTerminal() if ( canResetColor() )
&& term_detection->getGnomeTerminalID() < 3502 )
return;
if ( term_detection->isPuttyTerminal()
|| term_detection->isMltermTerminal() )
return;
if ( term_detection->isXTerminal()
|| term_detection->isScreenTerm()
|| FTermcap::osc_support )
{ {
oscPrefix(); oscPrefix();
FTerm::putstring (OSC "111" BEL); FTerm::putstring (OSC "111" BEL);
@ -677,16 +652,7 @@ void FTermXTerminal::resetXTermCursorColor()
{ {
// Reset the text cursor color // Reset the text cursor color
if ( term_detection->isGnomeTerminal() if ( canResetColor() )
&& term_detection->getGnomeTerminalID() < 3502 )
return;
if ( term_detection->isPuttyTerminal() )
return;
if ( term_detection->isXTerminal()
|| term_detection->isScreenTerm()
|| FTermcap::osc_support )
{ {
oscPrefix(); oscPrefix();
FTerm::putstring (OSC "112" BEL); FTerm::putstring (OSC "112" BEL);
@ -700,16 +666,7 @@ void FTermXTerminal::resetXTermMouseForeground()
{ {
// Reset the mouse foreground color // Reset the mouse foreground color
if ( term_detection->isGnomeTerminal() if ( canResetColor() )
&& term_detection->getGnomeTerminalID() < 3502 )
return;
if ( term_detection->isPuttyTerminal() )
return;
if ( term_detection->isXTerminal()
|| term_detection->isScreenTerm()
|| FTermcap::osc_support )
{ {
oscPrefix(); oscPrefix();
FTerm::putstring (OSC "113" BEL); FTerm::putstring (OSC "113" BEL);
@ -723,16 +680,7 @@ void FTermXTerminal::resetXTermMouseBackground()
{ {
// Reset the mouse background color // Reset the mouse background color
if ( term_detection->isGnomeTerminal() if ( canResetColor() )
&& term_detection->getGnomeTerminalID() < 3502 )
return;
if ( term_detection->isPuttyTerminal() )
return;
if ( term_detection->isXTerminal()
|| term_detection->isScreenTerm()
|| FTermcap::osc_support )
{ {
oscPrefix(); oscPrefix();
FTerm::putstring (OSC "114" BEL); FTerm::putstring (OSC "114" BEL);
@ -746,17 +694,7 @@ void FTermXTerminal::resetXTermHighlightBackground()
{ {
// Reset the highlight background color // Reset the highlight background color
if ( term_detection->isGnomeTerminal() if ( canResetColor() )
&& term_detection->getGnomeTerminalID() < 3502 )
return;
if ( term_detection->isPuttyTerminal() )
return;
if ( term_detection->isXTerminal()
|| term_detection->isScreenTerm()
|| term_detection->isUrxvtTerminal()
|| FTermcap::osc_support )
{ {
oscPrefix(); oscPrefix();
FTerm::putstringf (OSC "117" BEL); FTerm::putstringf (OSC "117" BEL);
@ -765,6 +703,25 @@ void FTermXTerminal::resetXTermHighlightBackground()
} }
} }
//----------------------------------------------------------------------
bool FTermXTerminal::canResetColor()
{
if ( term_detection->isGnomeTerminal()
&& term_detection->getGnomeTerminalID() < 3502 )
return false;
if ( term_detection->isPuttyTerminal()
|| term_detection->isMltermTerminal() )
return false;
if ( term_detection->isXTerminal()
|| term_detection->isScreenTerm()
|| FTermcap::osc_support )
return true;
return false;
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FTermXTerminal::oscPrefix() void FTermXTerminal::oscPrefix()
{ {

View File

@ -585,37 +585,8 @@ std::size_t FTextView::getTextWidth()
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FTextView::init() void FTextView::init()
{ {
try initScrollbar (vbar, fc::vertical, &FTextView::cb_VBarChange);
{ initScrollbar (hbar, fc::horizontal, &FTextView::cb_HBarChange);
vbar = std::make_shared<FScrollbar>(fc::vertical, this);
hbar = std::make_shared<FScrollbar>(fc::horizontal, this);
}
catch (const std::bad_alloc& ex)
{
std::cerr << bad_alloc_str << ex.what() << std::endl;
return;
}
vbar->setMinimum(0);
vbar->setValue(0);
vbar->hide();
hbar->setMinimum(0);
hbar->setValue(0);
hbar->hide();
vbar->addCallback
(
"change-value",
F_METHOD_CALLBACK (this, &FTextView::cb_VBarChange)
);
hbar->addCallback
(
"change-value",
F_METHOD_CALLBACK (this, &FTextView::cb_HBarChange)
);
setForegroundColor (wc.dialog_fg); setForegroundColor (wc.dialog_fg);
setBackgroundColor (wc.dialog_bg); setBackgroundColor (wc.dialog_bg);
nf_offset = isNewFont() ? 1 : 0; nf_offset = isNewFont() ? 1 : 0;
@ -625,6 +596,32 @@ void FTextView::init()
setRightPadding(1 + nf_offset); setRightPadding(1 + nf_offset);
} }
//----------------------------------------------------------------------
void FTextView::initScrollbar ( FScrollbarPtr& bar
, fc::orientation o
, FTextViewCallback callback )
{
try
{
bar = std::make_shared<FScrollbar>(o, this);
}
catch (const std::bad_alloc& ex)
{
std::cerr << bad_alloc_str << ex.what() << std::endl;
return;
}
bar->setMinimum(0);
bar->setValue(0);
bar->hide();
bar->addCallback
(
"change-value",
F_METHOD_CALLBACK (this, callback)
);
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FTextView::draw() void FTextView::draw()
{ {

View File

@ -139,6 +139,11 @@ class FFileDialog : public FDialog
static const FString fileSaveChooser ( FWidget* static const FString fileSaveChooser ( FWidget*
, const FString& = FString() , const FString& = FString()
, const FString& = FString() ); , const FString& = FString() );
// Friend function
friend const FString fileChooser ( FWidget*
, const FString&
, const FString&
, FFileDialog::DialogType);
protected: protected:
// Method // Method

View File

@ -243,6 +243,7 @@ class FListBox : public FWidget
private: private:
// Typedef // Typedef
typedef std::shared_ptr<FScrollbar> FScrollbarPtr; typedef std::shared_ptr<FScrollbar> FScrollbarPtr;
typedef void (FListBox::*FListBoxCallback)(FWidget*, FDataPtr);
// Enumeration // Enumeration
enum convert_type enum convert_type
@ -261,6 +262,9 @@ class FListBox : public FWidget
// Methods // Methods
void init(); void init();
void initScrollbar ( FScrollbarPtr&
, fc::orientation
, FListBoxCallback );
virtual void draw() override; virtual void draw() override;
void drawHeadline(); void drawHeadline();
void drawList(); void drawList();

View File

@ -375,6 +375,7 @@ class FListView : public FWidget
typedef std::vector<Header> headerItems; typedef std::vector<Header> headerItems;
typedef std::vector<fc::sorting_type> sortTypes; typedef std::vector<fc::sorting_type> sortTypes;
typedef std::shared_ptr<FScrollbar> FScrollbarPtr; typedef std::shared_ptr<FScrollbar> FScrollbarPtr;
typedef void (FListView::*FListViewCallback)(FWidget*, FDataPtr);
// Constants // Constants
static constexpr int USE_MAX_SIZE = -1; static constexpr int USE_MAX_SIZE = -1;
@ -385,6 +386,9 @@ class FListView : public FWidget
// Methods // Methods
void init(); void init();
void initScrollbar ( FScrollbarPtr&
, fc::orientation
, FListViewCallback );
template <typename Compare> template <typename Compare>
void sort (Compare); void sort (Compare);
std::size_t getAlignOffset ( fc::text_alignment std::size_t getAlignOffset ( fc::text_alignment

View File

@ -85,7 +85,7 @@ class FScrollbar : public FWidget
// Constructors // Constructors
explicit FScrollbar (FWidget* = nullptr); explicit FScrollbar (FWidget* = nullptr);
explicit FScrollbar (int = fc::vertical, FWidget* = nullptr); explicit FScrollbar (fc::orientation = fc::vertical, FWidget* = nullptr);
// Disable copy constructor // Disable copy constructor
FScrollbar (const FScrollbar&) = delete; FScrollbar (const FScrollbar&) = delete;
@ -108,7 +108,7 @@ class FScrollbar : public FWidget
void setValue (int); void setValue (int);
void setSteps (double); void setSteps (double);
void setPageSize (int, int); void setPageSize (int, int);
void setOrientation (int); void setOrientation (fc::orientation);
virtual void setGeometry ( int, int virtual void setGeometry ( int, int
, std::size_t, std::size_t , std::size_t, std::size_t
, bool = true) override; , bool = true) override;
@ -159,7 +159,7 @@ class FScrollbar : public FWidget
double steps{1}; double steps{1};
int pagesize{0}; int pagesize{0};
std::size_t length{20}; std::size_t length{20};
int bar_orientation{fc::vertical}; fc::orientation bar_orientation{fc::vertical};
int max_color{getMaxColor()}; int max_color{getMaxColor()};
}; };
#pragma pack(pop) #pragma pack(pop)

View File

@ -126,6 +126,7 @@ class FTermXTerminal final
static void resetXTermMouseForeground(); static void resetXTermMouseForeground();
static void resetXTermMouseBackground(); static void resetXTermMouseBackground();
static void resetXTermHighlightBackground(); static void resetXTermHighlightBackground();
static bool canResetColor();
static void oscPrefix(); static void oscPrefix();
static void oscPostfix(); static void oscPostfix();
static const FString* captureXTermFont(); static const FString* captureXTermFont();

View File

@ -136,6 +136,7 @@ class FTextView : public FWidget
private: private:
// Typedef // Typedef
typedef std::shared_ptr<FScrollbar> FScrollbarPtr; typedef std::shared_ptr<FScrollbar> FScrollbarPtr;
typedef void (FTextView::*FTextViewCallback)(FWidget*, FDataPtr);
// Accessors // Accessors
std::size_t getTextHeight(); std::size_t getTextHeight();
@ -147,6 +148,9 @@ class FTextView : public FWidget
// Methods // Methods
void init(); void init();
void initScrollbar ( FScrollbarPtr&
, fc::orientation
, FTextViewCallback );
virtual void draw() override; virtual void draw() override;
void drawText(); void drawText();
void processChanged(); void processChanged();

View File

@ -513,7 +513,7 @@ class FWidget : public FVTerm, public FObject
static bool init_desktop; static bool init_desktop;
static bool hideable; static bool hideable;
// Friend class // Friend classes
friend class FToggleButton; friend class FToggleButton;
friend class FScrollView; friend class FScrollView;
}; };