From f663352deb796fa2056df5290d38d283bfecee4a Mon Sep 17 00:00:00 2001 From: Robert Borzecki Date: Mon, 26 Aug 2019 11:51:23 +0200 Subject: [PATCH] Fixed single line mode for text widget. --- .../windows/NotesEditWindow.cpp | 61 +++++++++++++------ .../windows/NotesEditWindow.hpp | 2 +- module-gui/gui/widgets/Text.cpp | 52 ++++++++++++++-- 3 files changed, 89 insertions(+), 26 deletions(-) diff --git a/module-apps/application-notes/windows/NotesEditWindow.cpp b/module-apps/application-notes/windows/NotesEditWindow.cpp index 04d02350a..870a5f88f 100644 --- a/module-apps/application-notes/windows/NotesEditWindow.cpp +++ b/module-apps/application-notes/windows/NotesEditWindow.cpp @@ -53,24 +53,35 @@ void NotesEditWindow::buildInterface() { title->setText("New Note"); title->setAlignement( gui::Alignment(gui::Alignment::ALIGN_HORIZONTAL_CENTER, gui::Alignment::ALIGN_VERTICAL_BOTTOM)); - textMulti = new gui::Text( this, 11, 150, 480-22, 200 ); - textMulti->setFont("gt_pressura_bold_24"); - textMulti->setRadius(5); - textMulti->setMargins( gui::Margins(10, 10, 10, 10)); - textMulti->activatedCallback = [=] (gui::Item& item){ - LOG_INFO("Comparing text" ); - LOG_INFO("SOURCE:[%s]", textString.c_str()); - UTF8 getstr = textMulti->getText(); - LOG_INFO("GETSTR:[%s]", getstr.c_str()); - LOG_INFO("COMPARE: %s",(textString==getstr?"TRUE":"FALSE")); - return true; }; - textMulti->setTextType( gui::Text::TextType::MULTI_LINE ); - textMulti->setEditMode(gui::Text::EditMode::BROWSE ); + for( uint32_t i=0; i<3; i++ ) { + if( i == 0 ) + textMulti[i] = new gui::Text( this, 11, 100, 480-22, 120 ); + else if( i == 1) + textMulti[i] = new gui::Text( this, 11, 100+124, 480-22, 100 ); + else if( i == 2) + textMulti[i] = new gui::Text( this, 11, 100+124+104+54, 480-22, 120 ); - textSingle = new gui::Text( this, 11, 354, 480-22, 200 ); + textMulti[i]->setFont("gt_pressura_bold_24"); + textMulti[i]->setRadius(5); + textMulti[i]->setMargins( gui::Margins(10, 5, 10, 5)); + textMulti[i]->activatedCallback = [=] (gui::Item& item){ + LOG_INFO("Comparing text" ); + LOG_INFO("SOURCE:[%s]", textString.c_str()); + UTF8 getstr = textMulti[i]->getText(); + LOG_INFO("GETSTR:[%s]", getstr.c_str()); + LOG_INFO("COMPARE: %s",(textString==getstr?"TRUE":"FALSE")); + return true; }; + textMulti[i]->setTextType( gui::Text::TextType::MULTI_LINE ); + + + if( i == 1) + textMulti[i]->setEditMode(gui::Text::EditMode::BROWSE ); + } + + textSingle = new gui::Text( this, 11, 100+124+104, 480-22, 50 ); textSingle->setFont("gt_pressura_bold_24"); textSingle->setRadius(5); - textSingle->setMargins( gui::Margins(10, 10, 10, 10)); + textSingle->setMargins( gui::Margins(10, 5, 10, 5)); textSingle->activatedCallback = [=] (gui::Item& item){ LOG_INFO("Comparing text" ); LOG_INFO("SOURCE:[%s]", textString.c_str()); @@ -80,8 +91,15 @@ void NotesEditWindow::buildInterface() { return true; }; textSingle->setTextType( gui::Text::TextType::SINGLE_LINE ); - textMulti->setNavigationItem( NavigationDirection::DOWN, textSingle ); - textSingle->setNavigationItem( NavigationDirection::UP, textMulti ); + textMulti[0]->setNavigationItem( NavigationDirection::DOWN, textMulti[1] ); + + textMulti[1]->setNavigationItem( NavigationDirection::UP, textMulti[0] ); + textMulti[1]->setNavigationItem( NavigationDirection::DOWN, textSingle ); + + textSingle->setNavigationItem( NavigationDirection::UP, textMulti[1] ); + textSingle->setNavigationItem( NavigationDirection::DOWN, textMulti[2] ); + + textMulti[2]->setNavigationItem( NavigationDirection::UP, textSingle ); topBar->setActive(TopBar::Elements::TIME, true ); @@ -98,9 +116,12 @@ NotesEditWindow::~NotesEditWindow() { void NotesEditWindow::onBeforeShow( ShowMode mode, uint32_t command, SwitchData* data ) { application->setKeyboardProfile( "lang_eng_lower" ); - setFocusItem( textMulti ); - LOG_INFO("SETTING TEXT"); - textMulti->setText( textString ); + setFocusItem( textMulti[0] ); + LOG_INFO("SETTING MULTI TEXT WIDGET 0"); + textMulti[0]->setText( textString ); + LOG_INFO("SETTING MULTI TEXT WIDGET 1"); + textMulti[1]->setText( textString ); + LOG_INFO("SETTING SINGLE TEXT"); textSingle->setText( textString ); } diff --git a/module-apps/application-notes/windows/NotesEditWindow.hpp b/module-apps/application-notes/windows/NotesEditWindow.hpp index b9816de20..860f11164 100644 --- a/module-apps/application-notes/windows/NotesEditWindow.hpp +++ b/module-apps/application-notes/windows/NotesEditWindow.hpp @@ -24,7 +24,7 @@ namespace gui { class NotesEditWindow: public AppWindow { - Text* textMulti; + Text* textMulti[3]; Text* textSingle; public: NotesEditWindow( app::Application* app ); diff --git a/module-gui/gui/widgets/Text.cpp b/module-gui/gui/widgets/Text.cpp index 833240649..351fe188c 100644 --- a/module-gui/gui/widgets/Text.cpp +++ b/module-gui/gui/widgets/Text.cpp @@ -88,7 +88,7 @@ void Text::setEditMode( EditMode mode ) { } void Text::setTextType( TextType type ) { - + textType = type; } void Text::setNavigationBarrier( const NavigationBarrier& barrier, bool value ) { @@ -228,7 +228,8 @@ void Text::splitTextToLines( const UTF8& text) { endIndex = index+enterIndex; index += enterIndex + 1; lineEndType = LineEndType::BREAK; - textLines.push_back( new TextLine( tmpText.substr(0,enterIndex), startIndex, endIndex, lineEndType, spaceConsumed ) ); + textLines.push_back( new TextLine( tmpText.substr(0,enterIndex), startIndex, endIndex, lineEndType, + font->getPixelWidth(tmpText.substr(0,enterIndex))) ); // LOG_INFO("Text Input Line: [%s]", textLines.back()->text.c_str()); } //no enter found last line can be copied as a whole. else { @@ -281,12 +282,23 @@ void Text::splitTextToLines( const UTF8& text) { endIndex = index+spaceIndex; index += spaceIndex+1; textLines.push_back( new TextLine( tmpText.substr(0,spaceIndex), - startIndex, endIndex, lineEndType, spaceConsumed - spaceWidth) ); + startIndex, endIndex, lineEndType, font->getPixelWidth(tmpText.substr(0,spaceIndex))) ); // LOG_INFO("Text Input Line: [%s]", textLines.back()->text.c_str()); } } } } + + if( textType == TextType::SINGLE_LINE ) { + LOG_INFO("NUMBER OF LINES: %d", textLines.size()); + auto textLine = textLines.front(); + textLine->endType = LineEndType::EOT; + break; + } + } + + for( auto it : textLines ) { + LOG_INFO("text: [%s] ending: %d", it->text.c_str(), static_cast(it->endType)); } firstLine = textLines.begin(); @@ -549,6 +561,11 @@ bool Text::moveCursor( const NavigationDirection& direction ) { } } else if( direction == NavigationDirection::DOWN ) { + + //if this is a single line text widget there is no navigation down allowed + if( textType == TextType::SINGLE_LINE ) + return false; + auto itNext = std::next( it, 1 ); //this is the last line, check for barrier @@ -574,6 +591,10 @@ bool Text::moveCursor( const NavigationDirection& direction ) { } else if( direction == NavigationDirection::UP ) { + //if this is a single line text widget there is no navigation up allowed + if( textType == TextType::SINGLE_LINE ) + return false; + //if cursor is standing on the first line return false to allow focus change to previous widget if( it == textLines.begin()) { return false; @@ -603,6 +624,10 @@ bool Text::moveCursor( const NavigationDirection& direction ) { } bool Text::handleBrowsing( const InputEvent& inputEvent ) { + //if this is a single line text widget there is no browsing allowed + if( textType == TextType::SINGLE_LINE ) + return false; + switch( inputEvent.keyCode ) { case (KeyCode::KEY_UP): @@ -616,7 +641,7 @@ bool Text::handleBrowsing( const InputEvent& inputEvent ) { //move cursor to the last visible element auto it = firstLine; cursorRow = 0; - while( it != textLines.end() && cursorRow < visibleRows ) { + while( (it != textLines.end()) && (cursorRow < visibleRows-1) ) { it++; cursorRow++; } @@ -661,6 +686,9 @@ bool Text::handleNavigation( const InputEvent& inputEvent ) { bool Text::handleEnter() { + if( editMode == EditMode::BROWSE ) + return false; + //get textline where cursor is located auto it = firstLine; std::advance(it, cursorRow ); @@ -694,6 +722,9 @@ bool Text::handleEnter() { bool Text::handleBackspace() { + if( editMode == EditMode::BROWSE ) + return false; + //if cursor is in column 0 and there is no previous line return if( (cursorRow == 0 ) && (cursorColumn == 0) && (firstLine == textLines.begin()) ) @@ -752,6 +783,9 @@ bool Text::handleBackspace() { bool Text::handleChar( const InputEvent& inputEvent ) { + if( editMode == EditMode::BROWSE ) + return false; + //get text line where cursor is standing auto it = getCursorTextLine(); TextLine* currentTextLine = (*it); @@ -769,6 +803,14 @@ bool Text::handleChar( const InputEvent& inputEvent ) { uint32_t currentWidth = currentTextLine->pixelLength; if( currentWidth + charWidth > availableSpace ) { + //this is the case when new character inserted into single line text + //is creating the line that doesn't fit available space. + if( textType == TextType::SINGLE_LINE ) { + + currentTextLine->text.removeChar( cursorColumn, 1 ); + return true; + } + ++cursorColumn; reworkLines( it ); @@ -819,7 +861,7 @@ void Text::updateCursor() { void Text::recalculateDrawParams() { //calculate number of lines for displaying text - int32_t h = widgetArea.h - margins.top - margins.bottom - 2*radius; + int32_t h = widgetArea.h - margins.top - margins.bottom; if( h < 0 ) h = 0;