android/GlueIOIOPort: fix spurious errors after IOIO baud rate change
[xcsoar.git] / test / src / RunCanvas.cpp
bloba1c511eae02ba02eeebf173bc8d49dc434396b06
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #define ENABLE_SCREEN
26 #include "Main.hpp"
27 #include "Screen/SingleWindow.hpp"
28 #include "Screen/ButtonWindow.hpp"
29 #include "Screen/BufferCanvas.hpp"
31 #ifndef ENABLE_OPENGL
32 #include "Screen/WindowCanvas.hpp"
33 #endif
35 #ifndef _MSC_VER
36 #include <algorithm>
37 using std::min;
38 #endif
40 class TestWindow : public SingleWindow {
41 #ifndef ENABLE_OPENGL
42 ButtonWindow buffer_button;
43 #endif
44 ButtonWindow close_button;
45 unsigned page;
46 #ifndef ENABLE_OPENGL
47 bool buffered;
48 BufferCanvas buffer;
49 #endif
51 enum {
52 ID_START = 100,
53 #ifndef ENABLE_OPENGL
54 ID_BUFFER,
55 #endif
56 ID_CLOSE
59 public:
60 TestWindow():page(0)
61 #ifndef ENABLE_OPENGL
62 , buffered(false)
63 #endif
66 void Create(PixelSize size) {
67 SingleWindow::Create(_T("RunCanvas"), size);
69 PixelRect rc = GetClientRect();
71 PixelRect button_rc = rc;
72 button_rc.bottom -= 5;
73 button_rc.top = button_rc.bottom - 25;
75 #ifndef ENABLE_OPENGL
76 button_rc.left += 5;
77 button_rc.right = button_rc.left + 65;
79 buffer_button.Create(*this, _T("Buffer"), ID_BUFFER, button_rc);
80 buffer_button.SetFont(normal_font);
81 #endif
83 button_rc.right = rc.right - 5;
84 button_rc.left = button_rc.right - 65;
86 close_button.Create(*this, _T("Close"), ID_CLOSE, button_rc);
87 close_button.SetFont(normal_font);
90 private:
91 void paint(Canvas &canvas) {
92 canvas.SelectHollowBrush();
93 canvas.SelectBlackPen();
95 Brush red_brush(COLOR_RED);
97 const PixelRect rc = GetClientRect();
98 const int width = rc.right - rc.left;
99 const int height = rc.bottom - rc.top;
100 const int hmiddle = (rc.left + rc.right) / 2;
101 const int vmiddle = (rc.top + rc.bottom) / 2;
103 RasterPoint p1[3] = {
104 { -100, vmiddle },
105 { (width * 2) / 3, -100 },
106 { hmiddle, height * 2 },
109 RasterPoint p2[3] = {
110 { -2000, vmiddle },
111 { width * 10, -3000 },
112 { width * 5, 3000 },
115 const TCHAR *label;
116 switch (page) {
117 case 0:
118 canvas.DrawSegment(hmiddle, vmiddle,
119 min(width, height) / 3,
120 Angle::Zero(), Angle::Degrees(90),
121 false);
122 label = _T("segment 0-90 horizon=false");
123 break;
125 case 1:
126 canvas.DrawSegment(hmiddle, vmiddle,
127 min(width, height) / 3,
128 Angle::Degrees(45), Angle::Degrees(180),
129 true);
130 label = _T("segment 45-180 horizon=true");
131 break;
133 case 2:
134 canvas.DrawCircle(hmiddle, vmiddle,
135 min(width, height) / 3);
136 label = _T("circle");
137 break;
139 case 3:
140 case 4:
141 PixelRect rc;
142 rc.left = hmiddle - 50;
143 rc.top = vmiddle - 20;
144 rc.right = hmiddle + 50;
145 rc.bottom = vmiddle + 20;
146 canvas.DrawButton(rc, page == 4);
147 label = page == 4
148 ? _T("button down=true") : _T("button down=false");
149 break;
151 case 5:
152 canvas.Select(red_brush);
153 canvas.DrawPolygon(p1, 3);
154 label = _T("big polygon");
155 break;
157 case 6:
158 canvas.Select(red_brush);
159 canvas.DrawPolygon(p2, 3);
160 label = _T("huge polygon");
161 break;
164 canvas.SetTextColor(Color(0, 0, 128));
165 canvas.SetBackgroundTransparent();
166 canvas.Select(normal_font);
167 canvas.DrawText(5, 5, label);
168 #ifndef ENABLE_OPENGL
169 canvas.DrawText(5, 25,
170 buffered ? _T("buffered") : _T("not buffered"));
171 #endif
174 void update() {
175 #ifndef ENABLE_OPENGL
176 if (buffered) {
177 buffer.ClearWhite();
179 paint(buffer);
181 #endif
183 Invalidate();
186 protected:
187 virtual bool OnMouseDown(PixelScalar x, PixelScalar y) override {
188 if (SingleWindow::OnMouseDown(x, y))
189 return true;
191 page = (page + 1) % 7;
192 update();
193 return true;
196 virtual bool OnCommand(unsigned id, unsigned code) override {
197 switch (id) {
198 case ID_CLOSE:
199 Close();
200 return true;
202 #ifndef ENABLE_OPENGL
203 case ID_BUFFER:
204 buffered = !buffered;
205 if (buffered) {
206 WindowCanvas canvas(*this);
207 buffer.Create(canvas, canvas.GetSize());
208 } else
209 buffer.Destroy();
210 update();
211 return true;
212 #endif
215 return SingleWindow::OnCommand(id, code);
218 virtual void OnPaint(Canvas &canvas) override {
219 #ifndef ENABLE_OPENGL
220 if (!buffered) {
221 #endif
222 canvas.ClearWhite();
224 paint(canvas);
225 #ifndef ENABLE_OPENGL
226 } else
227 canvas.Copy(buffer);
228 #endif
230 SingleWindow::OnPaint(canvas);
234 static void
235 Main()
237 TestWindow window;
238 window.Create({250, 250});
239 window.Show();
241 window.RunEventLoop();