-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUGrymPlugin.pas
More file actions
546 lines (468 loc) · 13.6 KB
/
UGrymPlugin.pas
File metadata and controls
546 lines (468 loc) · 13.6 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
unit UGrymPlugin;
interface
uses
Windows, ActiveX, Classes, GrymCore_TLB, Dialogs, SysUtils, UBaseController
, Generics.Collections, UGrymControlsManager, UBaseViewFrame
, UBaseViewThread, UGrym;
{$IFNDEF V_2GIS_1_4}
{$DEFINE V_2GIS_1_4}
{$ENDIF}
type
TGrymPlugin = class(TInterfacedObject, IGrymPlugin, IGrymPluginInfo
, IGrymPluginOptions, IDispatch, IUnknown)
private
pRoot: TGrym;
fBaseViewThread: TBaseViewThread;
FRunController: TBaseController;
FOptionController: TBaseController;
FRefreshController: TBaseController;
FPoint: IConnectionPoint;
FCookie: Integer;
FName: string;
FOrganization: string;
FDescription: string;
FCopyright: string;
FTag: string;
FLanguages: string;
FIsRefreshInstance: Boolean;
FRestrictedCities: TStringList;
FIsRestricted: Boolean;
class var
FPlugin: TGrymPlugin;
const PLUGIN_INFO: WideString
= '<grym_plugin_info>'
+ '<name>%s</name>'
+ '<description>%s</description>'
+ '<copyright>%s</copyright>'
+ '<tag>%s</tag>'
+ '<requirements>'
{$IFDEF V_2GIS_1_4}
+ '<requirement_api>API-1.4</requirement_api>'
{$ENDIF}
+ '</requirements>'
+ '<supported_languages>%s</supported_languages>'
+ '</grym_plugin_info>';
const PLUGIN_INFO_RESTRICTED: WideString
= '<grym_plugin_info>'
+ '<name>%s (Íå çàãðóæåí)</name>'
+ '<description>Ðàáîòà ïëàãèíà â ýòîì ãîðîäå íåâîçìîæíà. %s</description>'
+ '<copyright>%s</copyright>'
+ '<tag>%s</tag>'
+ '<requirements>'
{$IFDEF V_2GIS_1_4}
+ '<requirement_api>API-1.4</requirement_api>'
{$ENDIF}
+ '</requirements>'
+ '<supported_languages>%s</supported_languages>'
+ '</grym_plugin_info>';
procedure RefreshInstance;
public
constructor Create(Tag: string; Name: string; Languages: array of string
; Description: string = ''; Copyright: string = ''); overload;
constructor Create(Organization: string; Tag: string; Name: string; Languages: array of string
; Description: string = ''; Copyright: string = ''); overload;
constructor Launch(BaseName: string);
procedure FillRibbon(Manager: TGrymControlsManager);
procedure InnerInitialize(const Root: IGrym; const BaseView: IBaseViewThread);
procedure InnerTerminate;
destructor Destroy; override;
function GetRaster(Resource: WideString): IRaster; deprecated;
function IsInit: Boolean;
function SetRunController(Controller: TBaseController): TGrymPlugin;
function SetOptionsController(Controller: TBaseController): TGrymPlugin;
function SetRefreshController(Controller: TBaseController): TGrymPlugin;
class function GetInstance: TGrymPlugin;
// IGrymPlugin
function Initialize(const pRoot: IGrym; const pBaseView: IBaseViewThread): HResult; stdcall;
function Terminate: HResult; stdcall;
// IGrymPluginInfo
function Get_XMLInfo(out pData: WideString): HResult; stdcall;
// IGrymPluginOptions
function OptionDialog(out pRet: WordBool): HResult; stdcall;
// IUnknown
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
// IDispatch
function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo)
: HResult; stdcall;
function GetIDsOfNames(const IID: TGUID; Names: Pointer;
NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer)
: HResult; stdcall;
function RestrictCity(Cities: array of string): TGrymPlugin;
function InstallPath: string;
function GetID(ID: string; Group: string = ''): string;
property BaseViewThread: TBaseViewThread read FBaseViewThread;
property Root: TGrym read pRoot;
property Name: string read FName;
end;
implementation
uses
ComObj, URibbonBar, UGrymControlTab, UBasePlacementControl, Forms;
constructor TGrymPlugin.Create(Tag: string; Name: string
; Languages: array of string; Description: string = ''
; Copyright: string = '');
var
Language: string;
LanguagesBuilder: TStringBuilder;
begin
inherited Create;
Self.FName := Name;
Self.FTag := Tag;
Self.FDescription := Description;
Self.FCopyright := Copyright;
Self.FIsRefreshInstance := True;
LanguagesBuilder := TStringBuilder.Create;
try
for Language in Languages do
begin
LanguagesBuilder.AppendFormat('<language>%s</language>', [Language]);
end;
Self.FLanguages := LanguagesBuilder.ToString;
finally
FreeAndNil(LanguagesBuilder);
end;
Self.pRoot := nil;
Self.FRestrictedCities := TStringList.Create;
end;
constructor TGrymPlugin.Create(Organization, Tag, Name: string;
Languages: array of string; Description, Copyright: string);
begin
Self.Create(Tag, Name, Languages, Description, Copyright);
Self.FOrganization := Organization;
end;
destructor TGrymPlugin.Destroy;
begin
FreeAndNil(Self.FRestrictedCities);
inherited;
end;
procedure TGrymPlugin.FillRibbon(Manager: TGrymControlsManager);
begin
Manager.Fill(Self.BaseViewThread.GetFrame.GetMainRibbonBar);
end;
function TGrymPlugin.Initialize(const pRoot: IGrym;
const pBaseView: IBaseViewThread): HResult;
var
pCont: IConnectionPointContainer;
begin
Result := S_OK;
try
Application.ModalPopupMode := TPopupMode.pmAuto;
Self.InnerInitialize(pRoot, pBaseView);
OleCheck(Self.BaseViewThread.GetFrame.GetInterface
.QueryInterface(IConnectionPointContainer, pCont));
if Assigned(pCont) then
begin
{$WARN SYMBOL_PLATFORM OFF}
pCont.FindConnectionPoint(_IBaseViewFrameEvents, Self.FPoint);
{$WARN SYMBOL_PLATFORM ON}
if Assigned(Self.FPoint) then
begin
FPoint.Advise(Self as IUnknown, Self.FCookie);
end;
end;
try
Self.RefreshInstance;
if not Self.FIsRestricted and Assigned(Self.FRunController) then
begin
Self.FRunController.Execute;
end;
except
on E: EArgumentNilException do
begin
ShowException(ExceptObject, ExceptAddr);
Result := E_INVALIDARG;
end
else
begin
raise;
end;
end;
except
ShowException(ExceptObject, ExceptAddr);
Result := E_FAIL;
end;
end;
procedure TGrymPlugin.InnerInitialize(const Root: IGrym
; const BaseView: IBaseViewThread);
begin
Self.pRoot := TGrym.Create(Root);
Self.fBaseViewThread := TBaseViewThread.Create(BaseView);
Self.FIsRestricted := (Self.FRestrictedCities.Count > 0) and
(Self.FRestrictedCities.IndexOf(Self.BaseViewThread.GetBaseReference.GetName) = -1);
end;
function TGrymPlugin.Terminate: HResult; stdcall;
begin
try
FreeAndNil(Self.FOptionController);
FreeAndNil(Self.FRunController);
FreeAndNil(Self.FRefreshController);
Self.InnerTerminate;
if Assigned(Self.FPoint) then
begin
Self.FPoint.Unadvise(Self.FCookie);
Self.FPoint := nil;
end;
Result := S_OK;
except
ShowException(ExceptObject, ExceptAddr);
Result := E_FAIL;
end;
end;
function TGrymPlugin.Invoke(DispID: Integer; const IID: TGUID;
LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo,
ArgErr: Pointer): HResult;
begin
try
case DispID of
1:
begin
Self.RefreshInstance;
Result := S_OK;
end;
else
begin
Result := E_NOTIMPL;
end;
end;
except
ShowException(ExceptObject, ExceptAddr);
Result := S_FALSE;
end;
end;
function TGrymPlugin.IsInit: Boolean;
begin
Result := not Self.FIsRestricted and Assigned(Self.pRoot) and
Self.fBaseViewThread.IsSet;
end;
constructor TGrymPlugin.Launch(BaseName: string);
var
pRoot: IGrym;
pBaseView: IBaseViewThread;
pBaseCollection: IBaseCollection;
pBaseReference: IBaseReference;
begin
inherited Create;
Self.pRoot := nil;
Self.FRestrictedCities := TStringList.Create;
CoInitialize(nil);
OleCheck(CoCreateInstance(CLASS_GRYM, nil, CLSCTX_INPROC_SERVER or
CLSCTX_LOCAL_SERVER, IID_IGrym, pRoot));
OleCheck(pRoot.Get_BaseCollection(pBaseCollection));
OleCheck(pBaseCollection.FindBase(BaseName, pBaseReference));
OleCheck(pRoot.GetBaseView(pBaseReference, True, False, pBaseView));
Self.FIsRefreshInstance := False;
Self.InnerInitialize(pRoot, pBaseView);
Assert(not Assigned(TGrymPlugin.FPlugin), 'Ýêçåìëÿð 2ÃÈÑ óæå ñîçäàí');
TGrymPlugin.FPlugin := Self;
end;
function TGrymPlugin.OptionDialog(out pRet: WordBool): HResult;
begin
try
if Assigned(Self.FOptionController) then
begin
Self.FOptionController.Execute;
end;
Result := S_OK;
except
ShowException(ExceptObject, ExceptAddr);
Result := S_OK;
end;
end;
function TGrymPlugin.InstallPath: string;
var
TheFileName : array[0..MAX_PATH] of Char;
begin
FillChar(TheFileName, sizeof(TheFileName), #0);
GetModuleFileName(HInstance, TheFileName, sizeof(TheFileName));
Result := ExtractFilePath(TheFileName);
end;
function TGrymPlugin.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
try
if IsEqualGUID(IID, IID_IGrymPluginOptions) then
begin
if not Assigned(Self.FOptionController) then
begin
Exit(E_NOINTERFACE);
end;
end;
if IsEqualGUID(IID, DIID__IBaseViewFrameEvents) then
begin
if GetInterface(IDispatch, Obj) then
begin
Result := S_OK;
end
else
begin
Result := E_NOINTERFACE;
end;
end
else
begin
if GetInterface(IID, Obj) then
begin
Result := 0
end
else
begin
Result := E_NOINTERFACE;
end;
end;
except
ShowException(ExceptObject, ExceptAddr);
Result := S_FALSE;
end;
end;
procedure TGrymPlugin.RefreshInstance;
begin
if Self.FIsRefreshInstance then
begin
TGrymPlugin.FPlugin := Self;
end;
if Assigned(Self.FRefreshController) then
begin
Self.FRefreshController.Execute;
end;
end;
function TGrymPlugin.RestrictCity(Cities: array of string): TGrymPlugin;
var
City: string;
begin
Result := Self;
Self.FRestrictedCities.Clear;
for City in Cities do
begin
Self.FRestrictedCities.Add(City);
end;
end;
function TGrymPlugin.SetOptionsController(
Controller: TBaseController): TGrymPlugin;
begin
Self.FOptionController := Controller;
Result := Self;
end;
function TGrymPlugin.SetRefreshController(
Controller: TBaseController): TGrymPlugin;
begin
Self.FRefreshController := Controller;
Result := Self;
end;
function TGrymPlugin.SetRunController(Controller: TBaseController): TGrymPlugin;
begin
Self.FRunController := Controller;
Result := Self;
end;
procedure TGrymPlugin.InnerTerminate;
begin
FreeAndNil(Self.fBaseViewThread);
Self.pRoot := nil;
end;
function TGrymPlugin.GetID(ID: string; Group: string): string;
var
Str: TStringBuilder;
begin
Str := TStringBuilder.Create;
try
if Self.FOrganization <> EmptyStr then
begin
Str.Append(Self.FOrganization).Append('.');
end;
Str.Append(Self.FTag).Append('.');
if Group <> EmptyStr then
begin
Str.Append(Group).Append('.');
end;
Str.Append(ID);
Result := Str.ToString;
finally
FreeAndNil(Str);
end;
end;
function TGrymPlugin.GetIDsOfNames(const IID: TGUID; Names: Pointer; NameCount,
LocaleID: Integer; DispIDs: Pointer): HResult;
begin
try
Result := E_NOTIMPL;
except
ShowException(ExceptObject, ExceptAddr);
Result := S_FALSE;
end;
end;
class function TGrymPlugin.GetInstance: TGrymPlugin;
begin
Result := TGrymPlugin.FPlugin;
end;
function TGrymPlugin.GetRaster(Resource: WideString): IRaster;
var
SA: SAFEARRAY;
RS: TResourceStream;
begin
RS := TResourceStream.Create(HInstance, Resource, RT_RCDATA);
try
ZeroMemory(@sa, sizeof(sa));
sa.cDims := 1;
sa.cbElements := 1;
sa.pvData := rs.Memory;
sa.rgsabound[0].cElements := rs.Size;
OleCheck(Self.BaseViewThread.GetFactory.GetInterface
.CreateRasterFromMemory(@sa, Result));
finally
FreeAndNil(RS);
end;
end;
function TGrymPlugin.GetTypeInfo(Index, LocaleID: Integer;
out TypeInfo): HResult;
begin
try
Result := E_NOTIMPL;
except
ShowException(ExceptObject, ExceptAddr);
Result := S_FALSE;
end;
end;
function TGrymPlugin.GetTypeInfoCount(out Count: Integer): HResult;
begin
try
if Assigned(@Count) then
begin
Count := 0;
Result := S_OK;
end
else
begin
Result := E_POINTER;
end;
except
ShowException(ExceptObject, ExceptAddr);
Result := S_FALSE;
end;
end;
function TGrymPlugin.Get_XMLInfo(out pData: WideString): HResult;
begin
try
if Assigned(@pData) then
begin
if Self.FIsRestricted then
begin
pData := Format(PLUGIN_INFO_RESTRICTED, [Self.FName, Self.FDescription
, Self.FCopyright, Self.FTag, Self.FLanguages]);
end
else
begin
pData := Format(PLUGIN_INFO, [Self.FName, Self.FDescription
, Self.FCopyright, Self.FTag, Self.FLanguages]);
end;
Result := S_OK;
end
else
begin
Result := E_POINTER;
end;
except
ShowException(ExceptObject, ExceptAddr);
Result := E_FAIL;
end;
end;
end.