From 2240a98cce54e34da167d1e0357d960d338a29f0 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Thu, 7 May 2020 16:57:16 +0100 Subject: [PATCH] WTextBox CWFs --- wtextbox.cc | 102 ++++++++++++++++++++++++---------------------------- wtextbox.h | 16 ++++----- 2 files changed, 53 insertions(+), 65 deletions(-) diff --git a/wtextbox.cc b/wtextbox.cc index 6a9e9e0..33464b1 100644 --- a/wtextbox.cc +++ b/wtextbox.cc @@ -14,38 +14,28 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with VOMP; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + along with VOMP. If not, see . */ -#include "wtextbox.h" - #include "colour.h" #include "input.h" -WTextbox::WTextbox(const char* ttext): +#include "wtextbox.h" + +WTextbox::WTextbox(const char* takeText): foreColour(DrawStyle::LIGHTTEXT) { // int fontHeight = Surface::getFontHeight(); //setDimensions(70, fontHeight); //setDimensions(100,100); setSize(100, 100); - text = NULL; textX = 5; textY = 2; paraMode = true; - if (ttext) setText(ttext); - - cur_scroll_line=0; - rem_scroll_line=0; -} - -WTextbox::~WTextbox() -{ - if (text) delete[] text; + if (takeText) setText(takeText); } void WTextbox::setParaMode(bool mode) @@ -55,10 +45,7 @@ void WTextbox::setParaMode(bool mode) void WTextbox::setText(const char* takeText) { - if (text) delete[] text; - int length = strlen(takeText); - text = new char[length + 1]; - strcpy(text, takeText); + text = takeText; } void WTextbox::setForegroundColour(const DrawStyle& fcolour) @@ -69,10 +56,10 @@ void WTextbox::setForegroundColour(const DrawStyle& fcolour) void WTextbox::draw() { Boxx::draw(); - if (text) + if (text.length()) { - if (paraMode) rem_scroll_line=drawPara(text, textX, textY, foreColour,cur_scroll_line); - else drawText(text, textX, textY, foreColour); + if (paraMode) rem_scroll_line = drawPara(text.c_str(), textX, textY, foreColour, cur_scroll_line); + else drawText(text.c_str(), textX, textY, foreColour); } } @@ -90,55 +77,60 @@ int WTextbox::handleCommand(int command) { if (cur_scroll_line > 0) { - cur_scroll_line--; - rem_scroll_line++; - return 1; + --cur_scroll_line; + --rem_scroll_line; + return 1; } else { - return 4; // Signal return control to parent + return 4; // Signal return control to parent } } case Input::DOWN: { - if (rem_scroll_line > 0) - { - cur_scroll_line++; - rem_scroll_line--; - return 1; - } else { - return 4; - } + if (rem_scroll_line > 0) + { + ++cur_scroll_line; + --rem_scroll_line; + return 1; + } + else + { + return 4; + } } case Input::LEFT: case Input::RIGHT: { - return 5; + return 5; } - } return 0; } -bool WTextbox::mouseAndroidScroll(int x, int y, int sx, int sy) +bool WTextbox::mouseAndroidScroll(int x, int y, int /* sx */, int sy) { - if ((x - getRootBoxOffsetX()) >= 0 && (y - getRootBoxOffsetY()) >= 0 - && (x - getRootBoxOffsetX()) <= (int)area.w && (y - getRootBoxOffsetY()) <= (int)area.h ) - { - int change = -sy /120; - if (change < 0) { - int rel_change = min(cur_scroll_line, -change); - cur_scroll_line-=rel_change; - rem_scroll_line+=rel_change; - } - else if (change > 0) { - int rel_change = min(rem_scroll_line, change); - cur_scroll_line += rel_change; - rem_scroll_line -= rel_change; - } - return true; - } - return false; - + if ( (x - getRootBoxOffsetX()) >= 0 + && (y - getRootBoxOffsetY()) >= 0 + && (x - getRootBoxOffsetX()) <= static_cast(area.w) + && (y - getRootBoxOffsetY()) <= static_cast(area.h) + ) + { + int change = -sy /120; + if (change < 0) + { + int rel_change = min(cur_scroll_line, -change); + cur_scroll_line-=rel_change; + rem_scroll_line+=rel_change; + } + else if (change > 0) + { + int rel_change = min(rem_scroll_line, change); + cur_scroll_line += rel_change; + rem_scroll_line -= rel_change; + } + return true; + } + return false; } diff --git a/wtextbox.h b/wtextbox.h index 1f82c97..c9b256d 100644 --- a/wtextbox.h +++ b/wtextbox.h @@ -14,15 +14,13 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with VOMP; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + along with VOMP. If not, see . */ #ifndef WTEXTBOX_H #define WTEXTBOX_H -#include -#include +#include #include "defines.h" #include "boxx.h" @@ -33,7 +31,6 @@ class WTextbox : public Boxx { public: WTextbox(const char* ttext = NULL); - virtual ~WTextbox(); void setText(const char* text); void draw(); void setForegroundColour(const DrawStyle& fcolour); @@ -42,17 +39,16 @@ class WTextbox : public Boxx // if added as a pane int handleCommand(int command); - bool mouseAndroidScroll(int x, int y, int sx, int sy); + bool mouseAndroidScroll(int x, int y, int sx, int sy); private: - - char* text; + std::string text; DrawStyle foreColour; int textX; int textY; bool paraMode; - unsigned int cur_scroll_line; - unsigned int rem_scroll_line; + unsigned int cur_scroll_line{}; + unsigned int rem_scroll_line{}; }; #endif -- 2.39.5