diff --git a/ChangeLog b/ChangeLog index b900d8a3..181b16bd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2015-11-29 Markus Gans + * Better handling of empty strings in FLineEdit and FButton + * Add a sub-menu to the "ui.cpp" example + 2015-11-25 Markus Gans * Small menu improvements diff --git a/src/fbutton.cpp b/src/fbutton.cpp index 64c154cf..427ec73d 100644 --- a/src/fbutton.cpp +++ b/src/fbutton.cpp @@ -137,9 +137,6 @@ void FButton::draw() bool is_ActiveFocus, is_Active, is_Focus, is_Flat; bool is_NonFlatShadow, is_NoUnderline; - if ( text.isNull() || text.isEmpty() ) - return; - length = int(text.getLength()); hotkeypos = -1; hotkey_offset = 0; @@ -150,7 +147,7 @@ void FButton::draw() else ButtonText = new wchar_t[length+1](); - txt = text; + txt = FString(text); src = const_cast(txt.wc_str()); dest = const_cast(ButtonText); @@ -710,6 +707,9 @@ void FButton::onFocusOut (FFocusEvent*) //---------------------------------------------------------------------- void FButton::setText (const FString& txt) { - text = txt; + if ( txt ) + text = txt; + else + text = ""; detectHotkey(); } diff --git a/src/flineedit.cpp b/src/flineedit.cpp index 5a66d45e..67a25614 100644 --- a/src/flineedit.cpp +++ b/src/flineedit.cpp @@ -185,10 +185,11 @@ void FLineEdit::drawInputField() if ( isUTF8_linux_terminal() ) { setUTF8(true); - print (show_text); + if ( show_text ) + print (show_text); setUTF8(false); } - else + else if ( show_text ) print (show_text); x = int(show_text.getLength()); @@ -782,12 +783,23 @@ void FLineEdit::onFocusOut (FFocusEvent*) hideCursor(); } +//---------------------------------------------------------------------- +void FLineEdit::clearText() +{ + offset = 0; + cursor_pos = 0; + text.clear(); +} + //---------------------------------------------------------------------- void FLineEdit::setText (FString txt) { offset = 0; cursor_pos = 0; - text = txt; + if ( txt ) + text = txt; + else + text = ""; } //---------------------------------------------------------------------- diff --git a/src/flineedit.h b/src/flineedit.h index 5a6fef67..7fba5f4c 100644 --- a/src/flineedit.h +++ b/src/flineedit.h @@ -105,6 +105,7 @@ class FLineEdit : public FWidget void onFocusIn (FFocusEvent*); void onFocusOut (FFocusEvent*); + void clearText(); void setText (FString); FString getText() const; void setLabelText (FString); diff --git a/src/fstring.cpp b/src/fstring.cpp index 51e2d3b5..77193175 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -659,15 +659,20 @@ const FString& FString::operator += (const char* s) //---------------------------------------------------------------------- const FString& FString::operator += (const wchar_t c) { - _insert (length, 1, &c); + wchar_t s[2]; + s[0] = c; + s[1] = L'\0'; + _insert (length, 1, s); return (*this); } //---------------------------------------------------------------------- const FString& FString::operator += (const char c) { - const wchar_t wc = static_cast(c); - _insert (length, 1, &wc ); + wchar_t s[2]; + s[0] = wchar_t(c & 0xff); + s[1] = L'\0'; + _insert (length, 1, s); return (*this); } @@ -724,17 +729,22 @@ const FString FString::operator + (const char* s) //---------------------------------------------------------------------- const FString FString::operator + (const wchar_t c) { + wchar_t s[2]; + s[0] = c; + s[1] = L'\0'; FString tmp(string); - tmp._insert (length, 1, &c); + tmp._insert (length, 1, s); return(tmp); } //---------------------------------------------------------------------- const FString FString::operator + (const char c) { - const wchar_t wc = static_cast(c); + wchar_t s[2]; + s[0] = wchar_t(c & 0xff); + s[1] = L'\0'; FString tmp(string); - tmp._insert (length, 1, &wc); + tmp._insert (length, 1, s); return(tmp); } diff --git a/test/ui.cpp b/test/ui.cpp index e54b76a0..f399a9f6 100644 --- a/test/ui.cpp +++ b/test/ui.cpp @@ -160,7 +160,9 @@ void ProgressDialog::cb_exit_bar (FWidget*, void*) class MyDialog : public FDialog { private: + FLineEdit* myLineEdit; FListBox* myList; + FString clipboard; private: MyDialog (const MyDialog&); // Disabled copy constructor @@ -174,6 +176,10 @@ class MyDialog : public FDialog void cb_about (FWidget*, void*); void cb_terminfo (FWidget*, void*); void cb_drives (FWidget*, void*); + void cb_cutClipboard (FWidget*, void*); + void cb_copyClipboard (FWidget*, void*); + void cb_pasteClipboard (FWidget*, void*); + void cb_clearInput (FWidget*, void*); void cb_input2buttonText (FWidget*, void*); void cb_setTitlebar (FWidget*, void*); void cb_ProgressBar (FWidget*, void*); @@ -190,7 +196,9 @@ class MyDialog : public FDialog //---------------------------------------------------------------------- MyDialog::MyDialog (FWidget* parent) : FDialog(parent) + , myLineEdit() , myList() + , clipboard() { // menu bar FMenuBar* Menubar = new FMenuBar (this); @@ -202,9 +210,10 @@ MyDialog::MyDialog (FWidget* parent) Edit->setStatusbarMessage ("Cut-and-paste editing commands"); FMenu* View = new FMenu ("&View", Menubar); View->setStatusbarMessage ("Show internal informations"); - FMenuItem* Options = new FMenuItem ("&Options", Menubar); + //FMenuItem* Options = new FMenuItem ("&Options", Menubar); + FMenu* Options = new FMenu ("&Options", Menubar); Options->setStatusbarMessage ("Set program defaults"); - Options->setDisable(); + //Options->setDisable(); FMenuItem* Help = new FMenuItem ("&Help", Menubar); Help->setStatusbarMessage ("Show version and copyright information"); @@ -212,12 +221,20 @@ MyDialog::MyDialog (FWidget* parent) FMenuItem* Open = new FMenuItem ("&Open...", File); Open->addAccelerator (fc::Fckey_o); // Ctrl + O Open->setStatusbarMessage ("Locate and open a text file"); + FMenu* Recent = new FMenu ("System files", File); + Recent->setStatusbarMessage ("View text file"); + FMenuItem* Line1 = new FMenuItem (File); Line1->setSeparator(); FMenuItem* Quit = new FMenuItem ("&Quit", File); Quit->addAccelerator (fc::Fmkey_x); // Meta/Alt + X Quit->setStatusbarMessage ("Exit the program"); + // "Recent" menu items + FMenuItem* File1 = new FMenuItem ("/etc/services", Recent); + FMenuItem* File2 = new FMenuItem ("/etc/fstab", Recent); + FMenuItem* File3 = new FMenuItem ("/etc/passwd", Recent); + // "Edit" menu items FMenuItem* Undo = new FMenuItem (fc::Fckey_z, "Undo", Edit); Undo->setDisable(); @@ -240,6 +257,15 @@ MyDialog::MyDialog (FWidget* parent) FMenuItem* Drive = new FMenuItem ("&Drive symbols...", View); Drive->setStatusbarMessage ("Show drive symbols"); + // "Options" menu items + FCheckMenuItem* aa = new FCheckMenuItem ("Disk &Status", Options); + aa->setStatusbarMessage ("1"); + aa->setChecked(); + FRadioMenuItem* bb = new FRadioMenuItem ("&Quick View", Options); + bb->setStatusbarMessage ("2"); + FRadioMenuItem* cc = new FRadioMenuItem ("&Baumstrucktur", Options); + cc->setStatusbarMessage ("3"); + // Menu function callbacks Open->addCallback ( @@ -254,22 +280,22 @@ MyDialog::MyDialog (FWidget* parent) Cut->addCallback ( "clicked", - _METHOD_CALLBACK (this, &MyDialog::cb_noFunctionMsg) + _METHOD_CALLBACK (this, &MyDialog::cb_cutClipboard) ); Copy->addCallback ( "clicked", - _METHOD_CALLBACK (this, &MyDialog::cb_noFunctionMsg) + _METHOD_CALLBACK (this, &MyDialog::cb_copyClipboard) ); Paste->addCallback ( "clicked", - _METHOD_CALLBACK (this, &MyDialog::cb_noFunctionMsg) + _METHOD_CALLBACK (this, &MyDialog::cb_pasteClipboard) ); Clear->addCallback ( "clicked", - _METHOD_CALLBACK (this, &MyDialog::cb_noFunctionMsg) + _METHOD_CALLBACK (this, &MyDialog::cb_clearInput) ); Env->addCallback ( @@ -286,6 +312,21 @@ MyDialog::MyDialog (FWidget* parent) "clicked", _METHOD_CALLBACK (this, &MyDialog::cb_about) ); + File1->addCallback + ( + "clicked", + _METHOD_CALLBACK (this, &MyDialog::cb_view) + ); + File2->addCallback + ( + "clicked", + _METHOD_CALLBACK (this, &MyDialog::cb_view) + ); + File3->addCallback + ( + "clicked", + _METHOD_CALLBACK (this, &MyDialog::cb_view) + ); // Buttons FButton* MyButton1 = new FButton (this); @@ -341,12 +382,12 @@ MyDialog::MyDialog (FWidget* parent) check2->setNoUnderline(); // A text input field - FLineEdit* MyLineEdit = new FLineEdit (this); - MyLineEdit->setGeometry(22, 1, 10, 1); - MyLineEdit->setText (FString("EnTry").toLower()); - MyLineEdit->setLabelText (L"&Input:"); - MyLineEdit->setStatusbarMessage ("Press Enter to set the title"); - MyLineEdit->setShadow(); + myLineEdit = new FLineEdit (this); + myLineEdit->setGeometry(22, 1, 10, 1); + myLineEdit->setText (FString("EnTry").toLower()); + myLineEdit->setLabelText (L"&Input:"); + myLineEdit->setStatusbarMessage ("Press Enter to set the title"); + myLineEdit->setShadow(); // Buttons FButton* MyButton4 = new FButton (this); @@ -431,7 +472,7 @@ MyDialog::MyDialog (FWidget* parent) ( "clicked", _METHOD_CALLBACK (this, &MyDialog::cb_input2buttonText), - dynamic_cast(MyLineEdit) + dynamic_cast(myLineEdit) ); MyButton5->addCallback @@ -446,7 +487,7 @@ MyDialog::MyDialog (FWidget* parent) _METHOD_CALLBACK (this, &MyDialog::cb_exitApp) ); - MyLineEdit->addCallback + myLineEdit->addCallback ( "activate", // e.g. on _METHOD_CALLBACK (this, &MyDialog::cb_setTitlebar) @@ -463,7 +504,7 @@ MyDialog::MyDialog (FWidget* parent) ( "clicked", _METHOD_CALLBACK (this, &MyDialog::cb_setInput), - dynamic_cast(MyLineEdit) + dynamic_cast(myLineEdit) ); myList->addCallback @@ -593,6 +634,43 @@ void MyDialog::cb_drives (FWidget*, void*) } } +//---------------------------------------------------------------------- +void MyDialog::cb_cutClipboard (FWidget*, void*) +{ + if ( ! myLineEdit ) + return; + clipboard = myLineEdit->getText(); + myLineEdit->clearText(); + myLineEdit->redraw(); +} + +//---------------------------------------------------------------------- +void MyDialog::cb_copyClipboard (FWidget*, void*) +{ + if ( ! myLineEdit ) + return; + clipboard = myLineEdit->getText(); +} + +//---------------------------------------------------------------------- +void MyDialog::cb_pasteClipboard (FWidget*, void*) +{ + if ( ! myLineEdit ) + return; + myLineEdit->setText(clipboard); + myLineEdit->redraw(); +} + +//---------------------------------------------------------------------- +void MyDialog::cb_clearInput (FWidget*, void*) +{ + if ( ! myLineEdit ) + return; + clipboard.clear(); + myLineEdit->clearText(); + myLineEdit->redraw(); +} + //---------------------------------------------------------------------- void MyDialog::cb_input2buttonText (FWidget* widget, void* data_ptr) { @@ -645,9 +723,14 @@ void MyDialog::cb_activateButton (FWidget* widget, void* data_ptr) } //---------------------------------------------------------------------- -void MyDialog::cb_view (FWidget*, void*) +void MyDialog::cb_view (FWidget* widget, void*) { - FString file = FFileDialog::fileOpenChooser (this); + FString file; + FMenuItem* item = static_cast(widget); + if ( item->getText() ) + file = item->getText(); + else + file = FFileDialog::fileOpenChooser (this); if ( file.isNull() ) return;