-
Notifications
You must be signed in to change notification settings - Fork 4
/
FBE.cpp
255 lines (210 loc) · 5.53 KB
/
FBE.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// FBE.cpp : main source file for FBE.exe
//
#include "stdafx.h"
#include <locale.h>
#include <sys/stat.h>
#include "resource.h"
#include "utils.h"
#include "Settings.h"
#include "apputils.h"
#include "FBEView.h"
#include "FBDoc.h"
#include "TreeView.h"
#include "ContainerWnd.h"
#include "Scintilla.h"
#include "MainFrm.h"
#include "MemProtocol.h"
// typelib interfaces
#include "FBE.h"
// implementation
#include "ExternalHelper.h"
// typelib guids
#include "fbe_i.c"
#define DEFINE_CLSID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
EXTERN_C const CLSID DECLSPEC_SELECTANY name \
= { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
// {7301FF90-9029-4819-B778-19D9999DB419}
DEFINE_CLSID(CLSID_MemProtocol, 0x7301ff90, 0x9029, 0x4819, 0xb7, 0x78, 0x19, 0xd9, 0x99, 0x9d, 0xb4, 0x19);
CAppModule _Module;
extern CElementDescMnr _EDMnr;
BEGIN_OBJECT_MAP(ObjectMap)
OBJECT_ENTRY(CLSID_MemProtocol, CMemProtocol)
END_OBJECT_MAP()
CSettings _Settings;
CSimpleArray<CString> _ARGV;
// External helpers
IDispatchPtr CFBEView::CreateHelper()
{
CComObject<ExternalHelper> *obj;
if(FAILED(CComObject<ExternalHelper>::CreateInstance(&obj)))
obj = NULL;
return obj;
}
// Command line parser
static void ParseCommandLine(LPTSTR cmd, CSimpleArray<CString>& args)
{
TCHAR* p=cmd;
int len= _tcslen(p);
TCHAR* e = p + len;
for (;;)
{
// Skip ws
while(p < e && (unsigned)*p <= 32)
++p;
if(p >= e)
break;
// Process argument
CString arg;
TCHAR* buf = arg.GetBuffer(e - p);
TCHAR* q = buf;
bool fQuote = false;
while(p < e)
{
if(fQuote)
{
if(*p == L'"')
{
// Possible end of arg
if(p + 1 < e && p[1] == L'"')
{
// Literal quote
*q++ = L'"';
++p;
}
else
fQuote = false;
}
else
*q++ = *p; // normal char
}
else
{
if(*p <= 32) // end of arg
break;
if(*p == L'"') // quoted part
fQuote = true;
else // normal text
*q++ = *p;
}
++p;
}
arg.ReleaseBuffer(q - buf);
args.Add(arg);
}
}
HINSTANCE resLib;
int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int nCmdShow = SW_SHOWDEFAULT)
{
CMessageLoop theLoop;
_Module.AddMessageLoop(&theLoop);
CMainFrame wndMain;
resLib = ::LoadLibrary(_Settings.GetInterfaceLanguageDllName());
if(resLib)
ATL::_AtlBaseModule.SetResourceInstance(resLib);
else
ATL::_AtlBaseModule.SetResourceInstance(ATL::_AtlBaseModule.GetModuleInstance());
HookSysDialogs();
U::InitKeycodes();
U::InitSettingsHotkeyGroups();
if(wndMain.CreateEx() == NULL)
{
ATLTRACE(L"Main window creation failed!\n");
return 0;
}
WINDOWPLACEMENT wpl;
if(_Settings.GetWindowPosition(wpl))
wndMain.SetWindowPlacement(&wpl);
else
{
wndMain.ShowWindow(nCmdShow);
wndMain.GetWindowPlacement(&wpl);
_Settings.SetWindowPosition(wpl);
}
int nRet = theLoop.Run();
_Module.RemoveMessageLoop();
// exit CBT hook
UnhookSysDialogs();
return nRet;
}
bool LoadEditor()
{
HINSTANCE hLib = ::LoadLibrary(L"SciLexer.dll");
if(hLib == NULL)
return false;
#if 0
bool (*scireg)(void *hinst) = (bool (*)(void*)) ::GetProcAddress(hLib,"Scintilla_RegisterClasses");
if(scireg == NULL || !scireg(_Module.GetModuleInstance()))
{
::FreeLibrary(hLib);
return false;
}
#endif
return true;
}
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
int nRet=1;
#if 1
#ifdef _DEBUG
_CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDERR );
_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDERR );
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDERR );
#endif
#endif
// initialize RNG
srand((unsigned int)time(NULL));
// switch to user's locale
setlocale(LC_CTYPE,"");
setlocale(LC_COLLATE,"");
// initialize COM/OLE
HRESULT hRes = ::OleInitialize(NULL);
ATLASSERT(SUCCEEDED(hRes));
// this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
::DefWindowProc(NULL, 0, 0, 0L);
AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES); // add flags to support other controls
// init module
hRes = _Module.Init(ObjectMap, hInstance, &LIBID_FBELib);
ATLASSERT(SUCCEEDED(hRes));
// register typelib
hRes = _Module.RegisterTypeLib();
ATLASSERT(SUCCEEDED(hRes));
// enable web browser hosting
AtlAxWinInit();
// initialize registry settings
U::InitSettings();
// parse command line
ParseCommandLine(lpstrCmdLine,_ARGV);
if (!AU::ParseCmdLineArgs())
goto out;
// load xml source editor
if (!LoadEditor())
{
wchar_t msg[MAX_LOAD_STRING + 1];
wchar_t cpt[MAX_LOAD_STRING + 1];
::LoadString(_Module.GetResourceInstance(), IDS_SCINTILLA_LOAD_ERR_MSG, msg, MAX_LOAD_STRING);
::LoadString(_Module.GetResourceInstance(), IDS_ERRMSGBOX_CAPTION, cpt, MAX_LOAD_STRING);
::MessageBox(NULL, msg, cpt,MB_OK|MB_ICONERROR);
goto out;
}
// register our protocol handler
IInternetSession *isess;
if (SUCCEEDED(::CoInternetGetSession(0, &isess, 0))) {
IClassFactory *cf;
if (SUCCEEDED(_Module.GetClassObject(CLSID_MemProtocol, IID_IClassFactory, (void**)&cf))) {
HRESULT hr=isess->RegisterNameSpace(cf,CLSID_MemProtocol,L"fbw-internal",0,NULL,0);
if (FAILED(hr))
ATLTRACE("Failed to register protocol handler: %x\n",hr);
cf->Release();
}
isess->Release();
}
// run the main loop
nRet = Run(lpstrCmdLine, nCmdShow);
out:
_Module.Term();
::OleUninitialize();
return nRet;
}