/
InfoMan.cpp
241 lines (203 loc) · 6 KB
/
InfoMan.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "stdafx.h"
#include "InfoMan.h"
#include "InfoManPreferences.h"
#include "MainWindow.h"
#include "Tests.h"
#include "LookupManager.h"
#include "HyperlinkHandler.h"
#include "ModuleDialog.h"
#include <Text.hpp>
#include <SysUtils.hpp>
#include <DataStore.hpp>
#include <ExtendedEvent.hpp>
#include <DefinitionStyle.hpp>
#include <windows.h>
#include <commctrl.h>
#define MAX_LOADSTRING 100
static BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
void CleanUp()
{
LookupManagerDispose();
StyleDisposeStaticStyles();
HyperlinkHandlerDispose();
PrefsDispose();
DataStore::dispose();
DeinitLogging();
ArsLexis::cleanAllocationLogging();
}
char_t* GetStorePath(const char_t* name)
{
char_t* path = GetAppDataPath();
if (NULL == path)
return NULL;
path = StrAppend(path, -1, TEXT("\\InfoMan"), -1);
if (NULL == path)
return NULL;
BOOL res = CreateDirectory(path, NULL);
if (!res && ERROR_ALREADY_EXISTS != GetLastError())
{
free(path);
return NULL;
}
path = StrAppend(path, -1, TEXT("\\"), 1);
if (NULL == path)
return NULL;
path = StrAppend(path, -1, name, -1);
if (NULL == path)
return NULL;
path = StrAppend(path, -1, TEXT(".dat"), -1);
if (NULL == path)
return NULL;
return path;
}
status_t DataStoreInit()
{
char_t* path = GetStorePath(TEXT("AppData"));
if (NULL == path)
return memErrNotEnoughSpace;
status_t err = DataStore::initialize(path);
free(path);
return err;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow))
return FALSE;
//#ifndef NDEBUG
// test_ExtEventSend();
//#endif
#ifndef WIN32_PLATFORM_WFSP
HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_INFOMAN));
#endif // !WIN32_PLATFORM_WFSP
// Main message loop:
LookupManager* lm = GetLookupManager();
if (NULL == lm)
{
Alert(IDS_ALERT_NOT_ENOUGH_MEMORY);
CleanUp();
return memErrNotEnoughSpace;
}
HANDLE managerThread = lm->connectionManager().startManagerThread();
if (NULL == managerThread)
{
// TODO: alert
CleanUp();
return -1;
}
MSG msg;
status_t err;
while (true)
{
/*
err = lm->connectionManager().waitForMessage(msg, 20);
if (errNone != err)
break;
*/
BOOL res = GetMessage(&msg, NULL, 0, 0);
if (-1 == res)
{
err = GetLastError();
break;
}
if (WM_QUIT == msg.message)
{
err = msg.wParam;
break;
}
//if (NULL != ModuleDialogGetCurrent() && IsDialogMessage(ModuleDialogGetCurrent()->handle(), &msg))
HWND fw = GetForegroundWindow();
if (NULL != fw && IsDialogMessage(fw, &msg))
{
//OutputDebugString(_T("IsDialogMessage(): "));
//DumpMessage(msg.message, msg.wParam, msg.lParam);
goto ExtEventCheck;
}
#ifndef WIN32_PLATFORM_WFSP
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
#endif // !WIN32_PLATFORM_WFSP
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
ExtEventCheck:
if (extEvent == msg.message)
{
//#ifndef NDEBUG
// test_ExtEventReceive(msg.lParam);
//#endif
lm->handleLookupEvent(msg.lParam);
ExtEventFree(msg.lParam);
}
}
lm->connectionManager().stop();
if (WAIT_TIMEOUT == WaitForSingleObject(managerThread, 1000))
{
assert(false);
TerminateThread(managerThread, -1);
}
PrefsSave();
CleanUp();
return err;
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
assert(GetInstance() == hInstance);
TCHAR szTitle[MAX_LOADSTRING]; // title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // main window class name
#ifdef WIN32_PLATFORM_PSPC
// SHInitExtraControls should be called once during your application's initialization to initialize any
// of the Pocket PC special controls such as CAPEDIT and SIPPREF.
SHInitExtraControls();
#endif // WIN32_PLATFORM_PSPC
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_INFOMAN, szWindowClass, MAX_LOADSTRING);
//If it is already running, then focus on the window, and exit
HWND hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
// set focus to foremost child window
// The "| 0x00000001" is used to bring any owned windows to the foreground and
// activate them.
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
return 0;
}
LogAddDebuggerLog(eLogAlways);
status_t err = DataStoreInit();
if (errNone != err)
{
if (memErrNotEnoughSpace == err)
Alert(IDS_ALERT_NOT_ENOUGH_MEMORY);
CleanUp();
return FALSE;
}
if (NULL == GetPreferences() || NULL == GetHyperlinkHandler())
{
Alert(IDS_ALERT_NOT_ENOUGH_MEMORY);
CleanUp();
return FALSE;
}
StylePrepareStaticStyles();
MainWindow* w = MainWindow::create(szTitle, szWindowClass);
if (NULL == w)
{
// TODO: show some alert
CleanUp();
return FALSE;
}
w->show(nCmdShow);
w->update();
RunTests(w->handle());
return TRUE;
}