From 84954cd78d6e0fa8eff24e7a0558f48748e4ba47 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Nusser?= <andre.nusser@googlemail.com>
Date: Wed, 12 Apr 2017 22:29:17 +0200
Subject: Fit text of TextEdit to widget size of resize.

---
 plugingui/mainwindow.cc | 19 ++++++++++---------
 plugingui/textedit.cc   | 13 ++++++++++++-
 plugingui/textedit.h    |  2 ++
 3 files changed, 24 insertions(+), 10 deletions(-)

(limited to 'plugingui')

diff --git a/plugingui/mainwindow.cc b/plugingui/mainwindow.cc
index 51fd3a3..5c729b0 100644
--- a/plugingui/mainwindow.cc
+++ b/plugingui/mainwindow.cc
@@ -38,18 +38,18 @@ namespace
 std::string getGPLText()
 {
 	return
-		"DrumGizmo is free software; you can redistribute it and/or modify\n"
-		"it under the terms of the GNU Lesser General Public License as published by\n"
-		"the Free Software Foundation; either version 3 of the License, or\n"
+		"DrumGizmo is free software; you can redistribute it and/or modify "
+		"it under the terms of the GNU Lesser General Public License as published by "
+		"the Free Software Foundation; either version 3 of the License, or "
 		"(at your option) any later version.\n"
 		"\n"
-		"DrumGizmo is distributed in the hope that it will be useful,\n"
-		"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
-		"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
+		"DrumGizmo is distributed in the hope that it will be useful, "
+		"but WITHOUT ANY WARRANTY; without even the implied warranty of "
+		"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the "
 		"GNU Lesser General Public License for more details.\n"
 		"\n"
-		"You should have received a copy of the GNU Lesser General Public License\n"
-		"along with DrumGizmo; if not, write to the Free Software\n"
+		"You should have received a copy of the GNU Lesser General Public License "
+		"along with DrumGizmo; if not, write to the Free Software "
 		"Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.\n";
 }
 
@@ -75,7 +75,7 @@ MainWindow::MainWindow(Settings& settings, void* native_window)
 	tabs.addTab("GPL", &gpl_text_field);
 
 	gpl_text_field.setText(getGPLText());
-	gpl_text_field.preprocessText();
+	gpl_text_field.adaptTextOnResize(true);
 	gpl_text_field.setReadOnly(true);
 }
 
@@ -123,6 +123,7 @@ void MainWindow::repaintEvent(RepaintEvent* repaintEvent)
 
 void MainWindow::sizeChanged(std::size_t width, std::size_t height)
 {
+	gpl_text_field.preprocessText();
 	tabs.resize(width - 2 * 16, height);
 }
 
diff --git a/plugingui/textedit.cc b/plugingui/textedit.cc
index 9ef21ec..c91b300 100644
--- a/plugingui/textedit.cc
+++ b/plugingui/textedit.cc
@@ -56,6 +56,12 @@ void TextEdit::resize(std::size_t width, std::size_t height)
 	Widget::resize(width, height);
 	scroll.resize(scroll.width(), height - 14);
 	scroll.move(width - 30, 7);
+
+	// TODO: This might be bad for performance. Improve at some point.
+	if (adapt_text_on_resize)
+	{
+		preprocessText();
+	}
 }
 
 void TextEdit::setReadOnly(bool readonly)
@@ -92,6 +98,11 @@ std::string TextEdit::text()
 	return _text;
 }
 
+void TextEdit::adaptTextOnResize(bool adapt)
+{
+	adapt_text_on_resize = adapt;
+}
+
 void TextEdit::preprocessText()
 {
 	preprocessedtext.clear();
@@ -129,7 +140,7 @@ void TextEdit::preprocessText()
 			for(size_t i = 0; i < line.length(); ++i)
 			{
 				size_t linewidth = font.textWidth(line.substr(0, i));
-				if(linewidth >= width() - BORDER - 4 + 3 - 10 - scroll.width())
+				if(linewidth >= width() - BORDER - 20 - scroll.width())
 				{
 					preprocessedtext.push_back(line.substr(0, i));
 					line = line.substr(i);
diff --git a/plugingui/textedit.h b/plugingui/textedit.h
index 83a7627..8df14b0 100644
--- a/plugingui/textedit.h
+++ b/plugingui/textedit.h
@@ -53,6 +53,7 @@ public:
 	void setReadOnly(bool readonly);
 	bool readOnly();
 
+	void adaptTextOnResize(bool adapt);
 	void preprocessText();
 
 	Notifier<> textChangedNotifier;
@@ -74,6 +75,7 @@ private:
 
 	std::string _text;
 
+	bool adapt_text_on_resize{false};
 	bool readonly{true};
 
 	std::list< std::string > preprocessedtext;
-- 
cgit v1.2.3