Update Turkish translation
[dasher.git] / Src / Win32 / Widgets / Canvas.cpp
blobbb62ee9704f849185e76a7c57367cf1c9c0c8bbe
1 // Canvas.cpp
2 //
3 // Copyright (c) 2007 The Dasher Team
4 //
5 // This file is part of Dasher.
6 //
7 // Dasher is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
12 // Dasher is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with Dasher; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 // 1 needed for WM_THEMECHANGED
22 // XXX Why lie? #define _WIN32_WINNT 0x0501
25 #include "..\Common\WinCommon.h"
27 #include <Vsstyle.h>
28 #include "../TabletPC/SystemInfo.h"
30 #include "Canvas.h"
31 #include "../Dasher.h"
32 #include "Screen.h"
34 #define PRESSED 0x8000
35 #define REPEAT 0x40000000
37 using namespace Dasher;
39 CCanvas::CCanvas(CDasher *DI) : m_pDasherInterface(DI) {
41 m_pScreen = 0;
42 #ifndef HAVE_NO_THEME
43 m_hTheme = NULL;
44 #endif
47 HWND CCanvas::Create(HWND hParent, const std::string &strFont) {
48 HWND hWnd = CWindowImpl<CCanvas>::Create(hParent, NULL, NULL, WS_CHILD | WS_VISIBLE , 0);//WS_EX_CLIENTEDGE);
50 m_hdc = GetDC();
52 // TODO: Check out whether any of this needs to be reimplemented
54 // TODO: Is this better placed in CDasher?
55 m_pKeyboardHelper = new CKeyboardHelper;
57 m_pScreen = new CScreen(m_hdc, m_hWnd, 300, 300, strFont);
59 m_pDasherInterface->ChangeScreen(m_pScreen);
61 return hWnd;
64 LRESULT CCanvas::OnCreate(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
65 bHandled = TRUE;
68 #ifndef HAVE_NO_THEME
69 m_hTheme = OpenThemeData(m_hWnd, L"Edit");
70 #endif
72 // If we're a tablet, initialize the event-generator
73 if(IsTabletPC()) {
74 HRESULT h = m_CursorInRange.Initialize(m_hWnd);
75 if(! SUCCEEDED(h)) {
76 // detected tablet, but cant initialize the event-generator
77 return -1;
80 return 0;
83 /////////////////////////////////////////////////////////////////////////////
85 LRESULT CCanvas::OnDestroy(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
86 int iRC = ReleaseDC(m_hdc);
88 if(!iRC) {
89 // TODO: general error reporting code?
90 LPVOID lpMsgBuf;
91 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
92 NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
93 (LPTSTR) & lpMsgBuf, 0, NULL);
94 // Process any inserts in lpMsgBuf.
95 // ...
96 // Display the string.
97 ::MessageBox(NULL, (LPCTSTR) lpMsgBuf, TEXT("Error"), MB_OK | MB_ICONINFORMATION);
98 // Free the buffer.
99 LocalFree(lpMsgBuf);
102 bHandled = true;
103 return 0;
106 /////////////////////////////////////////////////////////////////////////////
108 CCanvas::~CCanvas() {
109 #ifndef HAVE_NO_THEME
110 if(m_hTheme)
111 CloseThemeData(m_hTheme);
112 #endif
115 delete m_pScreen;
117 if(m_pKeyboardHelper)
118 delete m_pKeyboardHelper;
121 void CCanvas::Move(int x, int y, int Width, int Height) {
122 MoveWindow(x, y, Width, Height, TRUE);
125 void CCanvas::Paint() {
126 // InvalidateRect(NULL, FALSE);
127 // UpdateWindow();
130 LRESULT CCanvas::OnCommand(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
131 bHandled = TRUE;
132 SendMessage(GetParent(), message, wParam, lParam);
133 return 0;
136 LRESULT CCanvas::OnSetFocus(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
137 bHandled = TRUE;
138 SendMessage(GetParent(), WM_DASHER_FOCUS, 0, (LPARAM)&m_hWnd);
139 return 0;
142 LRESULT CCanvas::OnPaint(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
143 PAINTSTRUCT ps;
144 BeginPaint(&ps);
146 RECT rc;
147 GetClientRect(&rc);
149 RECT rcContent;
151 #ifndef HAVE_NO_THEME
152 if(m_hTheme) {
153 DTBGOPTS oOpts;
154 oOpts.dwSize = sizeof(DTBGOPTS);
155 oOpts.dwFlags = DTBG_OMITCONTENT;
156 oOpts.rcClip = rc;
158 DrawThemeBackgroundEx(m_hTheme, ps.hdc, EP_EDITTEXT,
159 ETS_NORMAL, &rc, &oOpts);
161 GetThemeBackgroundContentRect(m_hTheme, ps.hdc,
162 EP_EDITTEXT,
163 ETS_NORMAL, &rc, &rcContent);
165 else {
166 #endif
167 DrawEdge(ps.hdc, &rc, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
169 //rcContent.top = rc.top + 1;
170 //rcContent.bottom = rc.bottom -1;
171 //rcContent.left = rc.left + 1;
172 //rcContent.right = rc.right - 1;
173 rcContent = rc;
174 #ifndef HAVE_NO_THEME
176 #endif
178 m_pScreen->RealDisplay(ps.hdc, rcContent);
181 EndPaint(&ps);
183 bHandled = TRUE;
184 return 0;
187 LRESULT CCanvas::OnKeyUp(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
188 bHandled = TRUE;
190 int iKeyVal(-1);
192 if(m_pKeyboardHelper)
193 iKeyVal = m_pKeyboardHelper->ConvertKeyCode(wParam);
195 if(iKeyVal != -1) {
196 m_pDasherInterface->KeyUp(GetTickCount(), iKeyVal);
198 return 0;
201 // TODO: I believe all this is obsolete, but check
202 // switch(wParam)
203 // {
204 // case VK_SHIFT:
205 // if(GetKeyState(VK_CONTROL) & PRESSED)
206 // m_pDasherInterface->SetLongParameter(LP_BOOSTFACTOR, 25);
207 // else
208 // m_pDasherInterface->SetLongParameter(LP_BOOSTFACTOR, 100);
209 // return 0;
210 // case VK_CONTROL:
211 // if(GetKeyState(VK_SHIFT) & PRESSED)
212 // m_pDasherInterface->SetLongParameter(LP_BOOSTFACTOR, 175);
213 // else
214 // m_pDasherInterface->SetLongParameter(LP_BOOSTFACTOR, 100);
216 // return 0;
217 // default:
218 // return 0;
219 // }
222 LRESULT CCanvas::OnKeyDown(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
223 bHandled = TRUE;
225 int iKeyVal(-1);
227 if(m_pKeyboardHelper)
228 iKeyVal = m_pKeyboardHelper->ConvertKeyCode(wParam);
230 if(iKeyVal != -1) {
231 m_pDasherInterface->KeyDown(GetTickCount(), iKeyVal);
233 return 0;
235 // TODO: Also believed to be obsolete
237 // switch (wParam) {
238 // // Space, for start/stop events etc.
239 // case VK_SPACE:
240 // m_pDasherInterface->KeyDown(GetTickCount(), 0);
241 // return 0;
242 // case VK_F12:
243 // centrecursor();
244 // return 0;
246 // // Boost keys
247 // case VK_SHIFT:
248 // if(lParam ^ REPEAT) // ignore auto-repeat
249 // m_pDasherInterface->SetLongParameter(LP_BOOSTFACTOR, 175);
250 // return 0;
251 // case VK_CONTROL:
252 // if(lParam ^ REPEAT) // ignore auto-repeat
253 // m_pDasherInterface->SetLongParameter(LP_BOOSTFACTOR, 25);
254 // return 0;
256 // // Button mode keys
257 // case 0x41:
258 // m_pDasherInterface->KeyDown(GetTickCount(), 1);
259 // return 0;
260 // case 0x53:
261 // m_pDasherInterface->KeyDown(GetTickCount(), 2);
262 // return 0;
263 // case 0x57:
264 // m_pDasherInterface->KeyDown(GetTickCount(), 3);
265 // return 0;
266 // case 0x58:
267 // m_pDasherInterface->KeyDown(GetTickCount(), 4);
268 // return 0;
269 // default:
270 // return 0;
271 // }
274 LRESULT CCanvas::OnLButtonDblClk(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
276 // TODO: No need to handle this
278 // bHandled = TRUE;
279 // if( m_pDasherInterface->GetBoolParameter(BP_START_STYLUS) )
280 // {
281 // m_pDasherInterface->PauseAt(0, 0);
282 // }
283 return 0;
286 LRESULT CCanvas::OnLButtonDown(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
288 bHandled = TRUE;
289 m_pDasherInterface->KeyDown(GetTickCount(), 100);
290 SetFocus();
291 return 0;
294 LRESULT CCanvas::OnLButtonUp(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
296 bHandled = TRUE;
297 m_pDasherInterface->KeyUp(GetTickCount(), 100);
298 return 0;
302 // PRLW: These two functions are called on Tablet PCs when the stylus comes
303 // within or leaves the detection range of screen. Make these appear as mouse
304 // clicks to start/stop on mouse click. (XXX Ideally we would prefer start/stop
305 // outside of canvas...)
306 LRESULT CCanvas::OnCursorInRange(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
308 OutputDebugString(TEXT("CursorInRange\n"));
310 bHandled = TRUE;
312 m_pDasherInterface->KeyDown(GetTickCount(), 100);
314 return 0;
318 LRESULT CCanvas::OnCursorOutOfRange(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
320 OutputDebugString(TEXT("CursorOutOfRange\n"));
322 bHandled = TRUE;
324 m_pDasherInterface->KeyUp(GetTickCount(), 100);
326 return 0;
329 LRESULT CCanvas::OnSize(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
330 if (LOWORD(lParam)>0 && HIWORD(lParam) >0) {
331 m_pScreen->resize(LOWORD(lParam), HIWORD(lParam));
332 m_pDasherInterface->ScreenResized(m_pScreen);
333 InvalidateRect( NULL, FALSE);
336 return 0;
339 #ifndef HAVE_NO_THEME
340 LRESULT CCanvas::OnThemeChanged(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
341 if(m_hTheme)
342 CloseThemeData(m_hTheme);
344 m_hTheme = OpenThemeData(m_hWnd, L"Edit");
346 return 0;
348 #endif
350 LRESULT CCanvas::OnTimer(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
351 bHandled = true;
353 m_pDasherInterface->Main();
355 return 0;
358 /////////////////////////////////////////////////////////////////////////////
360 // Gets the size of the canvas in screen coordinates. Need if we
361 // want to log mouse positions normalized by the size of the
362 // canvas.
363 bool CCanvas::GetCanvasSize(int& iTop, int& iLeft, int& iBottom, int& iRight) {
365 RECT sWindowRect;
367 if (GetWindowRect( &sWindowRect)) {
368 iTop = sWindowRect.top;
369 iLeft = sWindowRect.left;
370 iBottom = sWindowRect.bottom;
371 iRight = sWindowRect.right;
372 return true;
375 return false;
379 void CCanvas::SetFont(const std::string &strFont) {
380 m_pScreen->SetFont(strFont);