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.
27 #include "Screen/SingleWindow.hpp"
28 #include "Screen/ButtonWindow.hpp"
29 #include "Screen/Canvas.hpp"
35 class KeyCodeDumper
: public PaintWindow
{
47 struct key_event events
[MAX_EVENTS
];
51 KeyCodeDumper():num_events(0) {}
54 void add_event(unsigned key_code
, bool down
) {
55 if (num_events
>= MAX_EVENTS
) {
56 std::copy(events
+ 1, events
+ MAX_EVENTS
, events
);
60 events
[num_events
].code
= key_code
;
61 events
[num_events
].down
= down
;
68 virtual bool OnMouseDown(PixelScalar x
, PixelScalar y
) override
{
73 virtual bool OnKeyDown(unsigned key_code
) override
{
74 add_event(key_code
, true);
78 virtual bool OnKeyUp(unsigned key_code
) override
{
79 add_event(key_code
, false);
83 virtual void OnSetFocus() override
{
84 PaintWindow::OnSetFocus();
88 virtual void OnKillFocus() override
{
89 PaintWindow::OnKillFocus();
93 virtual void OnPaint(Canvas
&canvas
) override
{
94 canvas
.SelectWhiteBrush();
96 canvas
.SelectBlackPen();
98 canvas
.SelectWhitePen();
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
];
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
;
126 void Create(PixelSize size
) {
127 SingleWindow::Create(_T("KeyCodeDumper"), size
);
129 PixelRect rc
= GetClientRect();
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();
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
{
162 return SingleWindow::OnCommand(id
, code
);
170 window
.Create({240, 100});
173 window
.RunEventLoop();