1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /***************************************************************************
3  *            resizetest.cc
4  *
5  *  Sun Feb  5 20:05:24 CET 2017
6  *  Copyright 2017 Bent Bisballe Nyeng
7  *  deva@aasimon.org
8  ****************************************************************************/
9 
10 /*
11  *  This file is part of DrumGizmo.
12  *
13  *  DrumGizmo is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU Lesser General Public License as published by
15  *  the Free Software Foundation; either version 3 of the License, or
16  *  (at your option) any later version.
17  *
18  *  DrumGizmo is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *  GNU Lesser General Public License for more details.
22  *
23  *  You should have received a copy of the GNU Lesser General Public License
24  *  along with DrumGizmo; if not, write to the Free Software
25  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
26  */
27 #include <iostream>
28 #include <chrono>
29 #include <thread>
30 
31 #include <hugin.hpp>
32 #include <window.h>
33 #include <font.h>
34 #include <painter.h>
35 
36 class TestWindow
37 	: public GUI::Window
38 {
39 public:
TestWindow()40 	TestWindow()
41 		: GUI::Window(nullptr)
42 	{
43 		setCaption("ResizeTest Window");
44 		CONNECT(eventHandler(), closeNotifier,
45 		        this, &TestWindow::closeEventHandler);
46 		CONNECT(this, sizeChangeNotifier, this, &TestWindow::sizeChanged);
47 		CONNECT(this, positionChangeNotifier, this, &TestWindow::positionChanged);
48 	}
49 
sizeChanged(std::size_t width,std::size_t height)50 	void sizeChanged(std::size_t width, std::size_t height)
51 	{
52 		reportedSize = std::make_pair(width, height);
53 		repaintEvent(nullptr);
54 	}
55 
positionChanged(int x,int y)56 	void positionChanged(int x, int y)
57 	{
58 		reportedPosition = std::make_pair(x, y);
59 		repaintEvent(nullptr);
60 	}
61 
closeEventHandler()62 	void closeEventHandler()
63 	{
64 		closing = true;
65 	}
66 
processEvents()67 	bool processEvents()
68 	{
69 		eventHandler()->processEvents();
70 		return !closing;
71 	}
72 
repaintEvent(GUI::RepaintEvent * repaintEvent)73 	void repaintEvent(GUI::RepaintEvent* repaintEvent)
74 	{
75 		GUI::Painter painter(*this);
76 
77 		//painter.clear();
78 		painter.setColour(GUI::Colour(0,1,0));
79 		painter.drawFilledRectangle(0, 0, width(), height());
80 
81 		auto currentSize = std::make_pair(width(), height());
82 		auto currentPosition = std::make_pair(x(), y());
83 
84 		{
85 			painter.setColour(GUI::Colour(1,0,0));
86 			char str[64];
87 			sprintf(str, "reported: (%d, %d); (%d, %d)",
88 			        (int)reportedPosition.first,
89 			        (int)reportedPosition.second,
90 			        (int)reportedSize.first,
91 			        (int)reportedSize.second);
92 			auto stringWidth = font.textWidth(str);
93 			auto stringHeight = font.textHeight(str);
94 			painter.drawText(reportedSize.first / 2 - stringWidth / 2,
95 			                 reportedSize.second / 2 + stringHeight / 2 - 7,
96 			                 font, str, false);
97 		}
98 
99 		{
100 			painter.setColour(GUI::Colour(1,0,0));
101 			char str[64];
102 			sprintf(str, "current: (%d, %d); (%d, %d)",
103 			        (int)currentPosition.first,
104 			        (int)currentPosition.second,
105 			        (int)currentSize.first,
106 			        (int)currentSize.second);
107 			auto stringWidth = font.textWidth(str);
108 			auto stringHeight = font.textHeight(str);
109 			painter.drawText(currentSize.first / 2 - stringWidth / 2,
110 			                 currentSize.second / 2 + stringHeight / 2 + 7,
111 			                 font, str, false);
112 		}
113 	}
114 
115 private:
116 	bool closing{false};
117 	GUI::Font font{":resources/font.png"};
118 	std::pair<std::size_t, std::size_t> reportedSize;
119 	std::pair<int, int> reportedPosition;
120 };
121 
main()122 int main()
123 {
124 	INFO(example, "We are up and running");
125 
126 	TestWindow test_window;
127 	test_window.show();
128 	test_window.resize(300,300);
129 
130 	while(test_window.processEvents())
131 	{
132 		std::this_thread::sleep_for(std::chrono::milliseconds(50));
133 	}
134 
135 	return 0;
136 }
137