Menu

[r2058]: / trunk / Src / FrDetail.pas  Maximize  Restore  History

Download this file

414 lines (353 with data), 10.2 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
{
* FrDetail.pas
*
* Title frame that displays, and manages user interaction with, the detail pane
* tabs.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is FrDetail.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2005-2012 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit FrDetail;
interface
uses
// Delphi
Forms, ComCtrls, ExtCtrls, Controls, StdCtrls, Classes, ActiveX, ActnList,
ImgList, Generics.Collections,
// Project
Compilers.UGlobals, FrTitled, FrBrowserBase, FrDetailView, IntfFrameMgrs,
IntfNotifier, UCommandBars, UView;
type
{
TDetailFrame:
Title frame that displays and manages user interaction with the detail
pane tabs. The frame implements display manager interfaces and notifies
application of user-initiated events via a notifier object.
}
TDetailFrame = class(TTitledFrame,
ITabbedDisplayMgr,
IPaneInfo,
IDetailPaneDisplayMgr,
IWBCustomiser,
ISetNotifier,
ICommandBarConfig,
ISelectionMgr,
IClipboardMgr
)
frmDetailView: TDetailViewFrame;
tcViews: TTabControl;
procedure tcViewsChange(Sender: TObject);
procedure tcViewsMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
strict private
type
TCommandBarItem = record
Action: TCustomAction;
ID: TCommandBarID;
constructor Create(AAction: TCustomAction; AID: TCommandBarID);
end;
type
TCommandBarItems = TList<TCommandBarItem>;
strict private
fNotifier: INotifier;
fCommandBarItems: TCommandBarItems;
fDisplayedView: IView;
fViews: TList<IView>;
function TabCount: Integer;
procedure InternalSelectTab(TabIdx: Integer);
procedure InternalDisplay(View: IView; ForceReload: Boolean);
procedure InternalDeleteTab(TabIdx: Integer);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// ITabbedDisplayMgr
procedure SelectTab(const TabIdx: Integer);
function SelectedTab: Integer;
procedure NextTab;
procedure PreviousTab;
// IPaneInfo
function IsInteractive: Boolean;
// IDetailPaneDisplayMgr
function SelectedView: IView;
procedure IDetailPaneDisplayMgr.SelectTab = SelectTab;
function FindTab(ViewKey: IViewKey): Integer;
procedure Display(View: IView; const TabIdx: Integer);
procedure Reload;
procedure Clear;
function CreateTab(View: IView): Integer;
function IsEmptyTabSet: Boolean;
procedure CloseTab(const TabIdx: Integer);
procedure CloseMultipleTabs(const KeepSelected: Boolean);
// IWBCustomiser
procedure SetExternalObj(Obj: IDispatch);
procedure SetDragDropHandler(Obj: IDropTarget);
// ISetNotifier
procedure SetNotifier(const Notifier: INotifier);
// ICommandBarConfig
procedure AddAction(const Action: TCustomAction; const ID: TCommandBarID);
procedure AddSpacer(const ID: TCommandBarID);
procedure SetImages(const Images: TCustomImageList);
// ISelectionMgr
function CanSelectAll: Boolean;
procedure SelectAll;
// IClipboardMgr
function CanCopy: Boolean;
procedure CopyToClipboard;
end;
implementation
uses
// Delphi
SysUtils, Generics.Defaults,
// Project
UExceptions;
{$R *.dfm}
{ TDetailFrame }
procedure TDetailFrame.AddAction(const Action: TCustomAction;
const ID: TCommandBarID);
begin
(frmDetailView as ICommandBarConfig).AddAction(Action, ID);
end;
procedure TDetailFrame.AddSpacer(const ID: TCommandBarID);
begin
(frmDetailView as ICommandBarConfig).AddSpacer(ID);
end;
function TDetailFrame.CanCopy: Boolean;
begin
Result := not IsEmptyTabSet and (frmDetailView as IClipboardMgr).CanCopy;
end;
function TDetailFrame.CanSelectAll: Boolean;
begin
Result := not IsEmptyTabSet and (frmDetailView as ISelectionMgr).CanSelectAll;
end;
procedure TDetailFrame.Clear;
begin
InternalDisplay(TViewFactory.CreateNulView, False);
end;
procedure TDetailFrame.CloseMultipleTabs(const KeepSelected: Boolean);
var
Idx: Integer;
SelectedIdx: Integer;
begin
if IsEmptyTabSet then
Exit;
SelectedIdx := SelectedTab;
for Idx := Pred(TabCount) downto 0 do
begin
if (Idx = SelectedIdx) and KeepSelected then
Continue;
InternalDeleteTab(Idx);
end;
if KeepSelected then
tcViews.TabIndex := 0 // only selected tab remains: new index
else
tcViews.TabIndex := -1;
end;
procedure TDetailFrame.CloseTab(const TabIdx: Integer);
var
SelectedIdx: Integer;
ClosingSelectedIdx: Boolean;
begin
ClosingSelectedIdx := SelectedTab = TabIdx;
SelectedIdx := SelectedTab;
InternalDeleteTab(TabIdx);
if ClosingSelectedIdx then
begin
if SelectedIdx = TabCount then
Dec(SelectedIdx);
tcViews.TabIndex := SelectedIdx;
end
else
begin
if TabIdx < SelectedIdx then
// keeps existing selected tab displayed
tcViews.TabIndex := Pred(SelectedIdx);
end;
end;
procedure TDetailFrame.CopyToClipboard;
begin
if IsEmptyTabSet then
Exit;
(frmDetailView as IClipboardMgr).CopyToClipboard;
end;
constructor TDetailFrame.Create(AOwner: TComponent);
begin
inherited;
fCommandBarItems := TCommandBarItems.Create;
fViews := TList<IView>.Create;
fDisplayedView := TViewFactory.CreateNulView;
end;
function TDetailFrame.CreateTab(View: IView): Integer;
begin
Result := tcViews.Tabs.Add(View.Description);
fViews.Add(View);
InternalDisplay(View, False); // stores View in fViews again
end;
destructor TDetailFrame.Destroy;
begin
fViews.Free;
fCommandBarItems.Free;
inherited;
end;
procedure TDetailFrame.Display(View: IView; const TabIdx: Integer);
begin
Assert(Assigned(View), ClassName + '.Display: View is nil');
fViews[TabIdx] := TViewFactory.Clone(View);
tcViews.Tabs[TabIdx] := View.Description;
if TabIdx = SelectedTab then
InternalDisplay(fViews[TabIdx], False);
end;
function TDetailFrame.FindTab(ViewKey: IViewKey): Integer;
var
Idx: Integer;
begin
for Idx := 0 to Pred(fViews.Count) do
if ViewKey.IsEqual(fViews[Idx].GetKey) then
Exit(Idx);
Result := -1;
end;
procedure TDetailFrame.InternalDeleteTab(TabIdx: Integer);
begin
tcViews.Tabs.Delete(TabIdx);
fViews.Delete(TabIdx);
end;
procedure TDetailFrame.InternalDisplay(View: IView; ForceReload: Boolean);
begin
(frmDetailView as IViewItemDisplayMgr).Display(View, ForceReload);
end;
procedure TDetailFrame.InternalSelectTab(TabIdx: Integer);
begin
tcViews.TabIndex := TabIdx;
InternalDisplay(SelectedView, False); // SelectedView allows for TabIdx = -1
end;
function TDetailFrame.IsEmptyTabSet: Boolean;
begin
Result := TabCount = 0;
end;
function TDetailFrame.IsInteractive: Boolean;
var
Idx: Integer;
begin
if IsEmptyTabSet then
Exit(False);
if (frmDetailView as IPaneInfo).IsInteractive then
Exit(True);
for Idx := 0 to Pred(ComponentCount) do
if (Components[Idx] is TWinControl)
and (Components[Idx] as TWinControl).Focused then
Exit(True);
Result := False;
end;
procedure TDetailFrame.NextTab;
begin
if IsEmptyTabSet then
Exit;
if SelectedTab = Pred(TabCount) then
InternalSelectTab(0)
else
InternalSelectTab(Succ(SelectedTab));
end;
procedure TDetailFrame.PreviousTab;
begin
if IsEmptyTabSet then
Exit;
if SelectedTab = 0 then
InternalSelectTab(Pred(TabCount))
else
InternalSelectTab(Pred(SelectedTab));
end;
procedure TDetailFrame.Reload;
begin
InternalDisplay(SelectedView, True); // SelectedView allows for TabIdx = -1
end;
procedure TDetailFrame.SelectAll;
begin
if IsEmptyTabSet then
Exit;
(frmDetailView as ISelectionMgr).SelectAll;
end;
function TDetailFrame.SelectedTab: Integer;
begin
if IsEmptyTabSet then
Exit(-1);
Result := tcViews.TabIndex;
end;
function TDetailFrame.SelectedView: IView;
begin
if IsEmptyTabSet then
Result := TViewFactory.CreateNulView
else
Result := fViews[SelectedTab];
end;
procedure TDetailFrame.SelectTab(const TabIdx: Integer);
begin
InternalSelectTab(TabIdx);
end;
procedure TDetailFrame.SetDragDropHandler(Obj: IDropTarget);
begin
(frmDetailView as IWBCustomiser).SetDragDropHandler(Obj);
end;
procedure TDetailFrame.SetExternalObj(Obj: IDispatch);
begin
(frmDetailView as IWBCustomiser).SetExternalObj(Obj);
end;
procedure TDetailFrame.SetImages(const Images: TCustomImageList);
begin
(frmDetailView as ICommandBarConfig).SetImages(Images);
end;
procedure TDetailFrame.SetNotifier(const Notifier: INotifier);
begin
fNotifier := Notifier;
end;
function TDetailFrame.TabCount: Integer;
begin
Result := tcViews.Tabs.Count;
Assert(Result = fViews.Count,
ClassName + '.TabCount: Tabs.Count differs from fViews.Count');
end;
procedure TDetailFrame.tcViewsChange(Sender: TObject);
begin
// User changed tab
// notify program via notifier: this will result in instruction back to select
// the tab - this allows for other controls to change the tab
Assert(tcViews.TabIndex >= 0, ClassName + '.tcViewsChange: tab index < 0');
if Assigned(fNotifier) then
fNotifier.ChangeDetailPane(tcViews.TabIndex);
end;
procedure TDetailFrame.tcViewsMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if htOnItem in tcViews.GetHitTestInfoAt(X, Y) then
tcViews.SetFocus;
end;
{ TDetailFrame.TCommandBarItem }
constructor TDetailFrame.TCommandBarItem.Create(AAction: TCustomAction;
AID: TCommandBarID);
begin
Action := AAction;
ID := AID;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.