Visual studio 2005 thinks that long long int is 64 bit. The difference between
[dasher.git] / Src / Win32 / Dasher.cpp
blob6834ea9c17d4a6813b7511dcd79deaf0690134f9
2 #include "Common\WinCommon.h" // must include pch first
4 #include "../DasherCore/ModuleManager.h"
6 #include "Dasher.h"
7 #include "../DasherCore/Event.h"
8 #include "Common\WinUTF8.h"
9 #include "Widgets/Canvas.h"
10 #include "DasherMouseInput.h"
12 #ifndef _WIN32_WCE
13 #include "Sockets/SocketInput.h"
14 #include "BTSocketInput.h"
15 #endif
17 #include "Common/WinOptions.h"
19 #ifndef _WIN32_WCE
20 #include <sys/stat.h>
21 #endif
23 using namespace std;
24 using namespace Dasher;
25 using namespace WinUTF8;
27 // Used to signal our message loop to do our periodic work, the value
28 // shouldn't collide with anything else in our code.
29 #define WM_DASHER_TIMER WM_USER + 128
31 CONST UINT WM_DASHER_EVENT = RegisterWindowMessage(_WM_DASHER_EVENT);
32 CONST UINT WM_DASHER_FOCUS = RegisterWindowMessage(_WM_DASHER_FOCUS);
33 CONST UINT WM_DASHER_GAME_MESSAGE = RegisterWindowMessage(_WM_DASHER_GAME_MESSAGE);
35 CDasher::CDasher(HWND Parent):m_hParent(Parent) {
36 // This class will be a wrapper for the Dasher 'control' - think ActiveX
37 m_pEdit = 0;
39 #ifndef _WIN32_WCE
40 // Set up COM for the accessibility stuff
41 CoInitialize(NULL);
42 #endif
44 Realize();
47 CDasher::~CDasher(void) {
48 // WriteTrainFileFull();
49 delete m_pCanvas;
52 void CDasher::CreateLocalFactories() {
53 #ifndef _WIN32_WCE
54 RegisterModule(new CSocketInput(m_pEventHandler, m_pSettingsStore));
55 RegisterModule(new CBTSocketInput(m_pEventHandler, m_pSettingsStore));
56 #endif
57 RegisterModule(new CDasherMouseInput(m_pEventHandler, m_pSettingsStore, m_pCanvas->getwindow()));
60 void CDasher::Main() {
61 if(m_pCanvas) {
62 m_pCanvas->DoFrame();
65 DWORD dwTicks = GetTickCount();
66 NewFrame(dwTicks, false);
69 // Handles the work we need to do periodically on a timer event
70 void CDasher::Log() {
72 CUserLogBase* pUserLog = GetUserLogPtr();
74 // We'll use this timer event to periodically log the user's mouse position
75 if ((pUserLog != NULL) && (m_pCanvas != NULL)) {
76 // Get the mouse x and y coordinates
77 POINT sMousePos;
78 GetCursorPos(&sMousePos);
80 // Since the everything is in screen relative coordinates, we'll
81 // make sure we have the right coordinates for our canvas and
82 // screen since the user may have move the window around.
83 int iTop = 0;
84 int iLeft = 0;
85 int iBottom = 0;
86 int iRight = 0;
88 if (m_pCanvas->GetCanvasSize(iTop, iLeft, iBottom, iRight))
89 pUserLog->AddCanvasSize(iTop, iLeft, iBottom, iRight);
91 // Also update the size of the window in the UserLogTrial object
92 if (GetWindowSize(&iTop, &iLeft, &iBottom, &iRight))
93 pUserLog->AddWindowSize(iTop, iLeft, iBottom, iRight);
95 // We'll store a normalized version so if the user changes the window
96 // size during a trial, it won't effect our coordinates. The
97 // normalization is with respect to the canvas and not the main
98 // window.
99 pUserLog->AddMouseLocationNormalized(sMousePos.x,
100 sMousePos.y,
101 true,
102 (float) GetNats());
107 void Dasher::CDasher::ExternalEventHandler(CEvent* pEvent) {
108 SendMessage(m_hParent, WM_DASHER_EVENT, 0, (LPARAM)pEvent);
111 void Dasher::CDasher::GameMessageOut(int message, const void *messagedata)
113 SendMessage(m_hParent, WM_DASHER_GAME_MESSAGE, (WPARAM)message, (LPARAM)messagedata);
116 // Gets the size of the window in screen coordinates.
117 bool Dasher::CDasher::GetWindowSize(int* pTop, int* pLeft, int* pBottom, int* pRight) {
118 if ((pTop == NULL) || (pLeft == NULL) || (pBottom == NULL) || (pRight == NULL))
119 return false;
121 RECT sWindowRect;
122 if (GetWindowRect(m_hParent, &sWindowRect))
124 *pTop = sWindowRect.top;
125 *pLeft = sWindowRect.left;
126 *pBottom = sWindowRect.bottom;
127 *pRight = sWindowRect.right;
128 return true;
130 else
131 return false;
134 void Dasher::CDasher::SetEdit(CDashEditbox * pEdit) {
135 // FIXME - we really need to make sure we have a
136 // more sensible way of passing messages out here.
138 m_pEdit = pEdit;
141 void Dasher::CDasher::WriteTrainFile(const std::string &strNewText) {
142 const std::string TrainFile = GetStringParameter(SP_USER_LOC) + GetStringParameter(SP_TRAIN_FILE);
144 if(strNewText.size() == 0)
145 return;
147 Tstring TTrainFile;
148 UTF8string_to_wstring(TrainFile, TTrainFile);
150 HANDLE hFile = CreateFile(TTrainFile.c_str(),
151 GENERIC_WRITE, 0, NULL,
152 OPEN_ALWAYS,
153 FILE_ATTRIBUTE_NORMAL, 0);
155 if(hFile == INVALID_HANDLE_VALUE) {
156 OutputDebugString(TEXT("Can not open file\n"));
158 else {
159 DWORD NumberOfBytesWritten;
160 SetFilePointer(hFile, 0, NULL, FILE_END);
162 //// Surely there are better ways to write to files than this??
164 for(unsigned int i = 0; i < strNewText.size(); i++) {
165 WriteFile(hFile, &strNewText[i], 1, &NumberOfBytesWritten, NULL);
168 CloseHandle(hFile);
172 void CDasher::ScanDirectory(const Tstring &strMask, std::vector<std::string> &vFileList) {
173 using namespace WinUTF8;
175 std::string filename;
176 WIN32_FIND_DATA find;
177 HANDLE handle;
179 handle = FindFirstFile(strMask.c_str(), &find);
180 if(handle != INVALID_HANDLE_VALUE) {
181 wstring_to_UTF8string(wstring(find.cFileName), filename);
182 vFileList.push_back(filename);
183 while(FindNextFile(handle, &find) != false) {
184 wstring_to_UTF8string(wstring(find.cFileName), filename);
185 vFileList.push_back(filename);
187 FindClose(handle);
191 void CDasher::ScanColourFiles(std::vector<std::string> &vFileList) {
192 Tstring Colours;
194 // TODO: Is it okay to have duplicate names in the array?
195 std::string strAppData2(GetStringParameter(SP_SYSTEM_LOC));
196 Tstring strAppData;
198 WinUTF8::UTF8string_to_wstring(strAppData2, strAppData);
200 Colours = strAppData;
201 Colours += TEXT("colour*.xml");
202 ScanDirectory(Colours, vFileList);
204 std::string strUserData2(GetStringParameter(SP_USER_LOC));
205 Tstring strUserData;
207 WinUTF8::UTF8string_to_wstring(strUserData2, strUserData);
209 Colours = strUserData;
210 Colours += TEXT("colour*.xml");
211 ScanDirectory(Colours, vFileList);
214 void CDasher::ScanAlphabetFiles(std::vector<std::string> &vFileList) {
215 Tstring Alphabets;
217 // TODO: Is it okay to have duplicate names in the array?
218 std::string strAppData2(GetStringParameter(SP_SYSTEM_LOC));
219 Tstring strAppData;
221 WinUTF8::UTF8string_to_wstring(strAppData2, strAppData);
223 Alphabets = strAppData;
224 Alphabets += TEXT("alphabet*.xml");
225 ScanDirectory(Alphabets, vFileList);
227 std::string strUserData2(GetStringParameter(SP_USER_LOC));
228 Tstring strUserData;
230 WinUTF8::UTF8string_to_wstring(strUserData2, strUserData);
232 Alphabets = strUserData;
233 Alphabets += TEXT("alphabet*.xml");
234 ScanDirectory(Alphabets, vFileList);
237 void CDasher::SetupPaths() {
238 using namespace WinHelper;
239 using namespace WinUTF8;
241 Tstring UserData, AppData;
242 std::string UserData2, AppData2;
243 GetUserDirectory(&UserData);
244 GetAppDirectory(&AppData);
245 UserData += TEXT("dasher.rc\\");
246 AppData += TEXT("system.rc\\");
247 CreateDirectory(UserData.c_str(), NULL); // Try and create folders. Doesn't seem
248 CreateDirectory(AppData.c_str(), NULL); // to do any harm if they already exist.
249 wstring_to_UTF8string(UserData, UserData2); // TODO: I don't know if special characters will work.
250 wstring_to_UTF8string(AppData, AppData2); // ASCII-only filenames are safest. Being English doesn't help debug this...
251 SetStringParameter(SP_SYSTEM_LOC, AppData2);
252 SetStringParameter(SP_USER_LOC, UserData2);
255 void CDasher::SetupUI() {
256 m_pCanvas = new CCanvas(this, m_pEventHandler, m_pSettingsStore);
257 m_pCanvas->Create(m_hParent); // TODO - check return
259 OnUIRealised();
262 int CDasher::GetFileSize(const std::string &strFileName) {
263 #ifndef _WIN32_WCE
264 struct _stat sStatInfo;
265 _stat(strFileName.c_str(), &sStatInfo);
266 return sStatInfo.st_size;
267 #else
268 // TODO: Fix this on Win CE
269 return 0;
270 #endif
273 void CDasher::CreateSettingsStore(void) {
274 m_pSettingsStore = new CWinOptions( "Inference Group", "Dasher3", m_pEventHandler );
277 void CDasher::StartTimer() {
278 // TODO: See MessageLoop, Main in CDasherWindow - should be brought into this class
279 // Framerate settings: currently 40fps.
280 SetTimer(m_pCanvas->getwindow(), 1, 25, NULL);
283 void CDasher::ShutdownTimer() {
286 // TODO: Check that syntax here is sensible
287 void CDasher::Move(int iX, int iY, int iWidth, int iHeight) {
288 if(m_pCanvas)
289 m_pCanvas->Move(iX, iY, iWidth, iHeight);
292 void CDasher::TakeFocus() {
293 // TODO: Implement me