Renderer, ...: use PixelRect::GetCenter()
[xcsoar.git] / test / src / KeyCodeDumper.cpp
blobd2e7982c39189c67891c1b455a9f5eedd148403c
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/Canvas.hpp"
31 #include <algorithm>
33 #include <stdio.h>
35 class KeyCodeDumper : public PaintWindow {
36 protected:
37 struct key_event {
38 unsigned code;
39 bool down;
42 enum {
43 MAX_EVENTS = 8,
46 protected:
47 struct key_event events[MAX_EVENTS];
48 unsigned num_events;
50 public:
51 KeyCodeDumper():num_events(0) {}
53 protected:
54 void add_event(unsigned key_code, bool down) {
55 if (num_events >= MAX_EVENTS) {
56 std::copy(events + 1, events + MAX_EVENTS, events);
57 --num_events;
60 events[num_events].code = key_code;
61 events[num_events].down = down;
62 ++num_events;
64 Invalidate();
67 protected:
68 virtual bool OnMouseDown(PixelScalar x, PixelScalar y) override {
69 SetFocus();
70 return true;
73 virtual bool OnKeyDown(unsigned key_code) override {
74 add_event(key_code, true);
75 return true;
78 virtual bool OnKeyUp(unsigned key_code) override {
79 add_event(key_code, false);
80 return true;
83 virtual void OnSetFocus() override {
84 PaintWindow::OnSetFocus();
85 Invalidate();
88 virtual void OnKillFocus() override {
89 PaintWindow::OnKillFocus();
90 Invalidate();
93 virtual void OnPaint(Canvas &canvas) override {
94 canvas.SelectWhiteBrush();
95 if (HasFocus())
96 canvas.SelectBlackPen();
97 else
98 canvas.SelectWhitePen();
99 canvas.Clear();
101 canvas.SetTextColor(COLOR_BLACK);
102 canvas.SetBackgroundTransparent();
103 canvas.Select(normal_font);
105 unsigned text_height = canvas.CalcTextSize(_T("W")).cy;
106 for (int i = num_events - 1, y = 4; i >= 0; --i, y += text_height) {
107 const struct key_event &event = events[i];
108 TCHAR buffer[64];
109 _stprintf(buffer, _T("key %s = 0x%x"),
110 event.down ? _T("down") : _T("up"), event.code);
111 canvas.DrawText(4, y, buffer);
116 class TestWindow : public SingleWindow {
117 KeyCodeDumper key_code_dumper;
118 ButtonWindow close_button;
120 enum {
121 ID_START = 100,
122 ID_CLOSE
125 public:
126 void Create(PixelSize size) {
127 SingleWindow::Create(_T("KeyCodeDumper"), size);
129 PixelRect rc = GetClientRect();
131 PixelRect d_rc = rc;
132 d_rc.bottom = (rc.top + rc.bottom + 1) / 2;
133 key_code_dumper.Create(*this, d_rc);
135 PixelRect button_rc = rc;
136 button_rc.top = (rc.top + rc.bottom + 1) / 2;
138 close_button.Create(*this, _T("Close"), ID_CLOSE, button_rc);
139 close_button.SetFont(normal_font);
141 key_code_dumper.SetFocus();
144 protected:
145 virtual void OnResize(PixelSize new_size) override {
146 SingleWindow::OnResize(new_size);
148 if (key_code_dumper.IsDefined())
149 key_code_dumper.Move(0, 0, new_size.cx, (new_size.cy + 1) / 2);
151 if (close_button.IsDefined())
152 close_button.Move(0, (new_size.cy + 1) / 2, new_size.cx, new_size.cy / 2);
155 virtual bool OnCommand(unsigned id, unsigned code) override {
156 switch (id) {
157 case ID_CLOSE:
158 Close();
159 return true;
162 return SingleWindow::OnCommand(id, code);
166 static void
167 Main()
169 TestWindow window;
170 window.Create({240, 100});
171 window.Show();
173 window.RunEventLoop();