3 // Copyright (c) 2007 The Dasher Team
5 // This file is part of Dasher.
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"
28 #include "../TabletPC/SystemInfo.h"
31 #include "../Dasher.h"
34 #define PRESSED 0x8000
35 #define REPEAT 0x40000000
37 using namespace Dasher
;
39 CCanvas::CCanvas(CDasher
*DI
) : m_pDasherInterface(DI
) {
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);
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
);
64 LRESULT
CCanvas::OnCreate(UINT message
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
) {
69 m_hTheme
= OpenThemeData(m_hWnd
, L
"Edit");
72 // If we're a tablet, initialize the event-generator
74 HRESULT h
= m_CursorInRange
.Initialize(m_hWnd
);
76 // detected tablet, but cant initialize the event-generator
83 /////////////////////////////////////////////////////////////////////////////
85 LRESULT
CCanvas::OnDestroy(UINT message
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
) {
86 int iRC
= ReleaseDC(m_hdc
);
89 // TODO: general error reporting code?
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.
96 // Display the string.
97 ::MessageBox(NULL
, (LPCTSTR
) lpMsgBuf
, TEXT("Error"), MB_OK
| MB_ICONINFORMATION
);
106 /////////////////////////////////////////////////////////////////////////////
108 CCanvas::~CCanvas() {
109 #ifndef HAVE_NO_THEME
111 CloseThemeData(m_hTheme
);
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);
130 LRESULT
CCanvas::OnCommand(UINT message
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
) {
132 SendMessage(GetParent(), message
, wParam
, lParam
);
136 LRESULT
CCanvas::OnSetFocus(UINT message
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
) {
138 SendMessage(GetParent(), WM_DASHER_FOCUS
, 0, (LPARAM
)&m_hWnd
);
142 LRESULT
CCanvas::OnPaint(UINT message
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
) {
151 #ifndef HAVE_NO_THEME
154 oOpts
.dwSize
= sizeof(DTBGOPTS
);
155 oOpts
.dwFlags
= DTBG_OMITCONTENT
;
158 DrawThemeBackgroundEx(m_hTheme
, ps
.hdc
, EP_EDITTEXT
,
159 ETS_NORMAL
, &rc
, &oOpts
);
161 GetThemeBackgroundContentRect(m_hTheme
, ps
.hdc
,
163 ETS_NORMAL
, &rc
, &rcContent
);
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;
174 #ifndef HAVE_NO_THEME
178 m_pScreen
->RealDisplay(ps
.hdc
, rcContent
);
187 LRESULT
CCanvas::OnKeyUp(UINT message
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
) {
192 if(m_pKeyboardHelper
)
193 iKeyVal
= m_pKeyboardHelper
->ConvertKeyCode(wParam
);
196 m_pDasherInterface
->KeyUp(GetTickCount(), iKeyVal
);
201 // TODO: I believe all this is obsolete, but check
205 // if(GetKeyState(VK_CONTROL) & PRESSED)
206 // m_pDasherInterface->SetLongParameter(LP_BOOSTFACTOR, 25);
208 // m_pDasherInterface->SetLongParameter(LP_BOOSTFACTOR, 100);
211 // if(GetKeyState(VK_SHIFT) & PRESSED)
212 // m_pDasherInterface->SetLongParameter(LP_BOOSTFACTOR, 175);
214 // m_pDasherInterface->SetLongParameter(LP_BOOSTFACTOR, 100);
222 LRESULT
CCanvas::OnKeyDown(UINT message
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
) {
227 if(m_pKeyboardHelper
)
228 iKeyVal
= m_pKeyboardHelper
->ConvertKeyCode(wParam
);
231 m_pDasherInterface
->KeyDown(GetTickCount(), iKeyVal
);
235 // TODO: Also believed to be obsolete
238 // // Space, for start/stop events etc.
240 // m_pDasherInterface->KeyDown(GetTickCount(), 0);
248 // if(lParam ^ REPEAT) // ignore auto-repeat
249 // m_pDasherInterface->SetLongParameter(LP_BOOSTFACTOR, 175);
252 // if(lParam ^ REPEAT) // ignore auto-repeat
253 // m_pDasherInterface->SetLongParameter(LP_BOOSTFACTOR, 25);
256 // // Button mode keys
258 // m_pDasherInterface->KeyDown(GetTickCount(), 1);
261 // m_pDasherInterface->KeyDown(GetTickCount(), 2);
264 // m_pDasherInterface->KeyDown(GetTickCount(), 3);
267 // m_pDasherInterface->KeyDown(GetTickCount(), 4);
274 LRESULT
CCanvas::OnLButtonDblClk(UINT message
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
276 // TODO: No need to handle this
279 // if( m_pDasherInterface->GetBoolParameter(BP_START_STYLUS) )
281 // m_pDasherInterface->PauseAt(0, 0);
286 LRESULT
CCanvas::OnLButtonDown(UINT message
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
289 m_pDasherInterface
->KeyDown(GetTickCount(), 100);
294 LRESULT
CCanvas::OnLButtonUp(UINT message
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
297 m_pDasherInterface
->KeyUp(GetTickCount(), 100);
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"));
312 m_pDasherInterface
->KeyDown(GetTickCount(), 100);
318 LRESULT
CCanvas::OnCursorOutOfRange(UINT message
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
320 OutputDebugString(TEXT("CursorOutOfRange\n"));
324 m_pDasherInterface
->KeyUp(GetTickCount(), 100);
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
);
339 #ifndef HAVE_NO_THEME
340 LRESULT
CCanvas::OnThemeChanged(UINT message
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
) {
342 CloseThemeData(m_hTheme
);
344 m_hTheme
= OpenThemeData(m_hWnd
, L
"Edit");
350 LRESULT
CCanvas::OnTimer(UINT message
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
) {
353 m_pDasherInterface
->Main();
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
363 bool CCanvas::GetCanvasSize(int& iTop
, int& iLeft
, int& iBottom
, int& iRight
) {
367 if (GetWindowRect( &sWindowRect
)) {
368 iTop
= sWindowRect
.top
;
369 iLeft
= sWindowRect
.left
;
370 iBottom
= sWindowRect
.bottom
;
371 iRight
= sWindowRect
.right
;
379 void CCanvas::SetFont(const std::string
&strFont
) {
380 m_pScreen
->SetFont(strFont
);