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>
* Generalize hide() method

View File

@ -52,6 +52,40 @@ bool sortDirFirst ( const FFileDialog::dir_entry& lhs
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
@ -225,32 +259,7 @@ const FString FFileDialog::fileOpenChooser ( FWidget* parent
, const FString& dirname
, const FString& filter )
{
FString ret;
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;
return fileChooser (parent, dirname, filter, FFileDialog::Open);
}
//----------------------------------------------------------------------
@ -258,32 +267,7 @@ const FString FFileDialog::fileSaveChooser ( FWidget* parent
, const FString& dirname
, const FString& filter )
{
FString ret;
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;
return fileChooser (parent, dirname, filter, FFileDialog::Save);
}

View File

@ -760,37 +760,8 @@ inline FString& FListBox::getString (listBoxItems::iterator iter)
//----------------------------------------------------------------------
void FListBox::init()
{
try
{
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)
);
initScrollbar (vbar, fc::vertical, &FListBox::cb_VBarChange);
initScrollbar (hbar, fc::horizontal, &FListBox::cb_HBarChange);
setGeometry (1, 1, 5, 4, false); // initialize geometry values
setForegroundColor (wc.dialog_fg);
setBackgroundColor (wc.dialog_bg);
@ -801,6 +772,32 @@ void FListBox::init()
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()
{

View File

@ -1369,37 +1369,8 @@ void FListView::adjustSize()
//----------------------------------------------------------------------
void FListView::init()
{
try
{
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)
);
initScrollbar (vbar, fc::vertical, &FListView::cb_VBarChange);
initScrollbar (hbar, fc::horizontal, &FListView::cb_HBarChange);
selflist.push_back(this);
root = selflist.begin();
null_iter = selflist.end();
@ -1413,6 +1384,32 @@ void FListView::init()
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>
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)
{
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;
length = ( getHeight() > getWidth() ) ? getHeight() : getWidth();

View File

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

View File

@ -585,37 +585,8 @@ std::size_t FTextView::getTextWidth()
//----------------------------------------------------------------------
void FTextView::init()
{
try
{
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)
);
initScrollbar (vbar, fc::vertical, &FTextView::cb_VBarChange);
initScrollbar (hbar, fc::horizontal, &FTextView::cb_HBarChange);
setForegroundColor (wc.dialog_fg);
setBackgroundColor (wc.dialog_bg);
nf_offset = isNewFont() ? 1 : 0;
@ -625,6 +596,32 @@ void FTextView::init()
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()
{

View File

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

View File

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

View File

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

View File

@ -85,7 +85,7 @@ class FScrollbar : public FWidget
// Constructors
explicit FScrollbar (FWidget* = nullptr);
explicit FScrollbar (int = fc::vertical, FWidget* = nullptr);
explicit FScrollbar (fc::orientation = fc::vertical, FWidget* = nullptr);
// Disable copy constructor
FScrollbar (const FScrollbar&) = delete;
@ -108,7 +108,7 @@ class FScrollbar : public FWidget
void setValue (int);
void setSteps (double);
void setPageSize (int, int);
void setOrientation (int);
void setOrientation (fc::orientation);
virtual void setGeometry ( int, int
, std::size_t, std::size_t
, bool = true) override;
@ -143,24 +143,24 @@ class FScrollbar : public FWidget
void processScroll();
// Data Members
sType scroll_type{FScrollbar::noScroll};
bool threshold_reached{false};
int threshold_time{500};
int repeat_time{10};
int slider_click_pos{-1};
int slider_click_stop_pos{-1};
int current_slider_pos{-1};
int slider_pos{0};
std::size_t slider_length{18}; // = bar_length
std::size_t bar_length{18}; // = length - 2
int val{0};
int min{0};
int max{99};
double steps{1};
int pagesize{0};
std::size_t length{20};
int bar_orientation{fc::vertical};
int max_color{getMaxColor()};
sType scroll_type{FScrollbar::noScroll};
bool threshold_reached{false};
int threshold_time{500};
int repeat_time{10};
int slider_click_pos{-1};
int slider_click_stop_pos{-1};
int current_slider_pos{-1};
int slider_pos{0};
std::size_t slider_length{18}; // = bar_length
std::size_t bar_length{18}; // = length - 2
int val{0};
int min{0};
int max{99};
double steps{1};
int pagesize{0};
std::size_t length{20};
fc::orientation bar_orientation{fc::vertical};
int max_color{getMaxColor()};
};
#pragma pack(pop)

View File

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

View File

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

View File

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