From a6c30bb2b54d737141173137997555bba959dd99 Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Wed, 20 Sep 2017 05:44:41 +0200 Subject: [PATCH] Stream and assignment operator support for FLabel --- ChangeLog | 1 + examples/dialog.cpp | 20 ++++---- examples/listbox.cpp | 2 +- examples/mouse.cpp | 4 +- examples/scrollview.cpp | 2 +- examples/ui.cpp | 9 ++-- examples/watch.cpp | 9 ++-- examples/windows.cpp | 4 +- include/final/flabel.h | 44 ++++++++++++++++- src/flabel.cpp | 101 ++++++++++++++++++++++++++++++++++++---- src/fstring.cpp | 8 +++- 11 files changed, 168 insertions(+), 36 deletions(-) diff --git a/ChangeLog b/ChangeLog index ea0511b3..c70d6bde 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,7 @@ 2017-09-19 Markus Gans * FString has now got its own streaming functionality for inbound and outbound type conversion + * Added stream and assignment operator support for FLabel 2017-09-19 Markus Gans * The command line help text is now available in all applications diff --git a/examples/dialog.cpp b/examples/dialog.cpp index ec783be0..f4cddcc0 100644 --- a/examples/dialog.cpp +++ b/examples/dialog.cpp @@ -31,17 +31,17 @@ int main (int argc, char* argv[]) dgl.setGeometry (4, 3, 41, 11); // Create text labels - FLabel label_1(&dgl), - label_2(&dgl); + FLabel label_1(&dgl); + FLabel label_2(&dgl); - label_1.setText ( wchar_t(fc::BlackUpPointingTriangle) - + std::wstring(L"\n") - + wchar_t(fc::BoxDrawingsUpAndRight) - + FString(2, wchar_t(fc::BoxDrawingsHorizontal)) - + " Double click the title bar button," ); - label_2.setText ( "press Q on the keyboard,\n" - "or push the button below to exit\n" - "the program." ); + label_1 << wchar_t(fc::BlackUpPointingTriangle) + << std::wstring(L"\n") + << wchar_t(fc::BoxDrawingsUpAndRight) + << FString(2, wchar_t(fc::BoxDrawingsHorizontal)) + << " Double click the title bar button,"; + label_2 << "press Q on the keyboard,\n" + << "or push the button below to exit\n" + << "the program."; label_1.setGeometry (1, 1, 38, 2); label_2.setGeometry (5, 3, 34, 3); diff --git a/examples/listbox.cpp b/examples/listbox.cpp index 286a975f..5c7d2ede 100644 --- a/examples/listbox.cpp +++ b/examples/listbox.cpp @@ -87,7 +87,7 @@ Listbox::Listbox (FWidget* parent) list1->setText ("FListBoxItem"); for (int i=1; i < 30; i++) - list1->insert (L"----- " + FString().setNumber(i) + L" -----"); + list1->insert (L"----- " + (FString() << i) + L" -----"); // listbox 2 double_list = new std::list; diff --git a/examples/mouse.cpp b/examples/mouse.cpp index 64dbb413..9fdb3534 100644 --- a/examples/mouse.cpp +++ b/examples/mouse.cpp @@ -63,9 +63,9 @@ ColorChooser::ColorChooser (FWidget* parent) // Text label FLabel* headline = new FLabel (this); headline->setGeometry(1, 1, 8, 1); - headline->setText ("Color"); headline->setEmphasis(); headline->setAlignment (fc::alignCenter); + *headline << "Color"; } //---------------------------------------------------------------------- @@ -203,9 +203,9 @@ Brushes::Brushes (FWidget* parent) // Text label FLabel* headline = new FLabel (this); headline->setGeometry(1, 1, 8, 1); - headline->setText ("Brush"); headline->setEmphasis(); headline->setAlignment (fc::alignCenter); + *headline << "Brush"; } //---------------------------------------------------------------------- diff --git a/examples/scrollview.cpp b/examples/scrollview.cpp index 37dd51fd..a6aab13f 100644 --- a/examples/scrollview.cpp +++ b/examples/scrollview.cpp @@ -220,8 +220,8 @@ scrollviewdemo::scrollviewdemo (FWidget* parent) // Text label FLabel* label = new FLabel (this); label->setGeometry(2, 1, 46, 1); - label->setText (L"Use scrollbars to change the viewport position"); label->setEmphasis(); + *label << L"Use scrollbars to change the viewport position"; } //---------------------------------------------------------------------- diff --git a/examples/ui.cpp b/examples/ui.cpp index 59ada30e..5f1e8e91 100644 --- a/examples/ui.cpp +++ b/examples/ui.cpp @@ -516,16 +516,16 @@ MyDialog::MyDialog (FWidget* parent) // Text labels FLabel* headline = new FLabel (this); headline->setGeometry(21, 3, 10, 1); - headline->setText (L"List items"); headline->setEmphasis(); headline->setAlignment (fc::alignCenter); + *headline = L"List items"; FLabel* tagged = new FLabel (L"Tagged:", this); tagged->setGeometry(21, 4, 7, 1); FLabel* tagged_count = new FLabel(this); tagged_count->setGeometry(29, 4, 5, 1); - tagged_count->setNumber(0); + *tagged_count << 0; FLabel* sum = new FLabel (L"Sum:", this); sum->setGeometry(21, 5, 7, 3); @@ -533,7 +533,7 @@ MyDialog::MyDialog (FWidget* parent) FLabel* sum_count = new FLabel (this); sum_count->setGeometry(29, 5, 5, 3); - sum_count->setNumber (long(myList->getCount())); + *sum_count << myList->getCount(); // Statusbar at the bottom FStatusBar* statusbar = new FStatusBar (this); @@ -830,7 +830,8 @@ void MyDialog::cb_updateNumber (FWidget* widget, data_ptr data) if ( list->isSelected(int(n)) ) select_num++; - num->setNumber(select_num); + num->clear(); + *num << select_num; num->redraw(); } diff --git a/examples/watch.cpp b/examples/watch.cpp index d624cb78..74ec5040 100644 --- a/examples/watch.cpp +++ b/examples/watch.cpp @@ -130,7 +130,7 @@ void watch::printTime() else str.sprintf("%02d:%02d ", now.tm_hour, now.tm_min); - time_str->setText(str); + *time_str = str; time_str->redraw(); } @@ -165,7 +165,7 @@ void watch::cb_clock (FWidget*, data_ptr) else { delAllTimer(); - time_str->setText("--:--:--"); + *time_str = "--:--:--"; time_str->redraw(); } } @@ -183,9 +183,10 @@ void watch::cb_seconds (FWidget*, data_ptr) else { if ( sec ) - time_str->setText("--:--:--"); + *time_str = "--:--:--"; else - time_str->setText("--:-- "); + *time_str = "--:-- "; + time_str->redraw(); } } diff --git a/examples/windows.cpp b/examples/windows.cpp index d550a94d..61ca8646 100644 --- a/examples/windows.cpp +++ b/examples/windows.cpp @@ -111,12 +111,12 @@ void smallWindow::adjustSize() { if ( isZoomed() ) { - top_right_label->setText("unzoom"); + *top_right_label = "unzoom"; bottom_label->hide(); } else { - top_right_label->setText("zoom"); + *top_right_label = "zoom"; bottom_label->setVisible(); } diff --git a/include/final/flabel.h b/include/final/flabel.h index 593e1bc6..bed830bf 100644 --- a/include/final/flabel.h +++ b/include/final/flabel.h @@ -53,6 +53,19 @@ class FLabel : public FWidget // Destructor virtual ~FLabel(); + // Overloaded operators + FLabel& operator = (const FString&); + FLabel& operator << (const FString&); + FLabel& operator << (const wchar_t); + FLabel& operator << (const uInt); + FLabel& operator << (const int); + FLabel& operator << (const uLong); + FLabel& operator << (const long); + FLabel& operator << (const float); + FLabel& operator << (const double); + FLabel& operator << (const lDouble); + const FLabel& operator >> (FString&); + // Accessors const char* getClassName() const; FTerm* getAccelWidget(); @@ -69,7 +82,11 @@ class FLabel : public FWidget bool setReverseMode(); bool unsetReverseMode(); bool setEnable (bool); - void setNumber(long); + void setNumber (uLong); + void setNumber (long); + void setNumber (float, int = FLT_DIG); + void setNumber (double, int = DBL_DIG); + void setNumber (lDouble, int = LDBL_DIG); void setText (const FString&); // Inquiries @@ -78,6 +95,7 @@ class FLabel : public FWidget // Methods void hide(); + void clear(); // Event handlers void onMouseDown (FMouseEvent*); @@ -152,6 +170,26 @@ inline bool FLabel::setReverseMode() inline bool FLabel::unsetReverseMode() { return setReverseMode(false); } +//---------------------------------------------------------------------- +inline void FLabel::setNumber (uLong num) +{ setText(FString().setNumber(num)); } + +//---------------------------------------------------------------------- +inline void FLabel::setNumber (long num) +{ setText(FString().setNumber(num)); } + +//---------------------------------------------------------------------- +inline void FLabel::setNumber (float num, int precision) +{ setText(FString().setNumber(num, precision)); } + +//---------------------------------------------------------------------- +inline void FLabel::setNumber (double num, int precision) +{ setText(FString().setNumber(num, precision)); } + +//---------------------------------------------------------------------- +inline void FLabel::setNumber (lDouble num, int precision) +{ setText(FString().setNumber(num, precision)); } + //---------------------------------------------------------------------- inline bool FLabel::hasEmphasis() { return emphasis; } @@ -160,4 +198,8 @@ inline bool FLabel::hasEmphasis() inline bool FLabel::hasReverseMode() { return reverse_mode; } +//---------------------------------------------------------------------- +inline void FLabel::clear() +{ text.clear(); } + #endif // FLABEL_H diff --git a/src/flabel.cpp b/src/flabel.cpp index beac82ef..bd61d57a 100644 --- a/src/flabel.cpp +++ b/src/flabel.cpp @@ -50,6 +50,98 @@ FLabel::~FLabel() // destructor delAccelerator(); } +// FLabel operators +//---------------------------------------------------------------------- +FLabel& FLabel::operator = (const FString& s) +{ + setText(s); + return *this; +} + +//---------------------------------------------------------------------- +FLabel& FLabel::operator << (const FString& s) +{ + setText(text + s); + return *this; +} + +//---------------------------------------------------------------------- +FLabel& FLabel::operator << (const wchar_t c) +{ + setText(text + c); + return *this; +} + +//---------------------------------------------------------------------- +FLabel& FLabel::operator << (const uInt num) +{ + FString num_str; + num_str << num; + setText(text + num_str); + return *this; +} + +//---------------------------------------------------------------------- +FLabel& FLabel::operator << (const int num) +{ + FString num_str; + num_str << num; + setText(text + num_str); + return *this; +} + +//---------------------------------------------------------------------- +FLabel& FLabel::operator << (const uLong num) +{ + FString num_str; + num_str << num; + setText(text + num_str); + return *this; +} + +//---------------------------------------------------------------------- +FLabel& FLabel::operator << (const long num) +{ + FString num_str; + num_str << num; + setText(text + num_str); + return *this; +} + +//---------------------------------------------------------------------- +FLabel& FLabel::operator << (const float num) +{ + FString num_str; + num_str << num; + setText(text + num_str); + return *this; +} + +//---------------------------------------------------------------------- +FLabel& FLabel::operator << (const double num) +{ + FString num_str; + num_str << num; + setText(text + num_str); + return *this; +} + +//---------------------------------------------------------------------- +FLabel& FLabel::operator << (const lDouble num) +{ + FString num_str; + num_str << num; + setText(text + num_str); + return *this; +} + +//---------------------------------------------------------------------- +const FLabel& FLabel::operator >> (FString& s) +{ + s += text; + return *this; +} + // public methods of FLabel //---------------------------------------------------------------------- @@ -107,12 +199,6 @@ bool FLabel::setEnable (bool on) return on; } -//---------------------------------------------------------------------- -void FLabel::setNumber (long num) -{ - setText(FString().setNumber(num)); -} - //---------------------------------------------------------------------- void FLabel::setText (const FString& txt) { @@ -459,9 +545,6 @@ void FLabel::draw() uInt length; int hotkeypos, align_offset; - if ( text.isNull() || text.isEmpty() ) - return; - if ( isMonochron() ) { setReverse(true); diff --git a/src/fstring.cpp b/src/fstring.cpp index 9f4b049f..aacac103 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -468,7 +468,7 @@ FString& FString::operator << (const FString& s) //---------------------------------------------------------------------- FString& FString::operator << (const wchar_t c) { - FString s = FString(c); + FString s(c); _insert (length, s.length, s.string); return *this; } @@ -476,7 +476,7 @@ FString& FString::operator << (const wchar_t c) //---------------------------------------------------------------------- FString& FString::operator << (const char c) { - FString s = FString(c); + FString s(c); _insert (length, s.length, s.string); return *this; } @@ -2467,6 +2467,10 @@ inline void FString::_insert (uInt pos, uInt len, const wchar_t* s) if ( ! string ) { // string is null + + if ( len == 0 ) // String s is also a null string + return; + length = len; bufsize = FWDBUFFER + length + 1;