File: dockedsourcepagecontrol.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (380 lines) | stat: -rw-r--r-- 11,519 bytes parent folder | download | duplicates (4)
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
{
 *****************************************************************************
  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************

 Authors: Maciej Izak
          Michael W. Vogel

 The PageControl shown in source editor window.
 Every unit got a own pagecontrol

}

unit DockedSourcePageControl;

{$mode objfpc}{$H+}
{ $define DEBUGDOCKEDFORMEDITOR}

interface

uses
  // RTL
  Classes, SysUtils, fgl,
  // LCL
  Forms, ComCtrls, Controls, LCLProc,
  // IDEIntf
  SrcEditorIntf, FormEditingIntf, LazIDEIntf,
  // DockedFormEditor
  DockedDesignForm, DockedResizer, DockedOptionsIDE, DockedAnchorDesigner,
  {%H-}DockedTools, DockedStrConsts;

type

  { TSourcePageControl }

  TSourcePageControl = class(TPageControl)
  private
    FDesignerSetFocusAsyncCount: Integer;
    FDesignForm: TDesignForm;
    FResizer: TResizer;
    FSourceEditor: TSourceEditorInterface;
    FTabSheetAnchors: TTabSheet;
    FTabSheetCode: TTabSheet;
    FTabSheetDesigner: TTabSheet;
    procedure AsyncDesignerSetFocus({%H-}Data: PtrInt);
    function  GetActiveTabDisplayState: TTabDisplayState;
    procedure SourcePageControlMouseUp(Sender: TObject; {%H-}Button: TMouseButton; {%H-}Shift: TShiftState; {%H-}X, {%H-}Y: Integer);
    procedure OnAdjustPage(Sender: TObject);
  protected
    procedure SetDesignForm(const AValue: TDesignForm); virtual;
  public
    constructor Create(ASourceEditor: TSourceEditorInterface); reintroduce;
    destructor Destroy; override;
    procedure AdjustPage;
    function  AnchorPageActive: Boolean;
    procedure CreateResizer;
    procedure CreateTabSheetAnchors;
    procedure CreateTabSheetDesigner;
    procedure DesignerSetFocus;
    procedure DesignerSetFocusAsync;
    function  DesignerPageActive: Boolean;
    function  FormPageActive: Boolean;
    procedure RemoveDesignPages;
    procedure RemoveTabSheetAnchors;
    procedure InitPage;
    procedure RefreshResizer;
    procedure ShowCode;
    procedure ShowDesigner(AIndex: Integer = 0);
  public
    property ActiveTabDisplayState: TTabDisplayState read GetActiveTabDisplayState;
    property DesignForm: TDesignForm read FDesignForm write SetDesignForm;
    property Resizer: TResizer read FResizer;
    property SourceEditor: TSourceEditorInterface read FSourceEditor;
  end;

  { TSourcePageControls }

  TSourcePageControls = class(specialize TFPGList<TSourcePageControl>)
  private
    function GetPageControl(ASrcEditor: TSourceEditorInterface): TSourcePageControl;
    function GetSourceEditor(APageControl: TSourcePageControl): TSourceEditorInterface;
  public
    function Contains(APageControl: TSourcePageControl): Boolean;
    function Contains(ASrcEditor: TSourceEditorInterface): Boolean;
    function IndexOf(APageControl: TSourcePageControl): Integer; overload;
    function IndexOf(ASrcEditor: TSourceEditorInterface): Integer; overload;
    procedure Remove(ASrcEditor: TSourceEditorInterface); overload;
  public
    property PageControl[ASrcEditor: TSourceEditorInterface]: TSourcePageControl read GetPageControl;
    property SourceEditor[APageControl: TSourcePageControl]: TSourceEditorInterface read GetSourceEditor;
  end;

implementation

{ TSourcePageControl }

procedure TSourcePageControl.OnAdjustPage(Sender: TObject);
begin
  AdjustPage;
end;

procedure TSourcePageControl.SourcePageControlMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if DesignerPageActive then
    DesignerSetFocus;
end;

procedure TSourcePageControl.AsyncDesignerSetFocus(Data: PtrInt);
begin
  {$IFDEF DEBUGDOCKEDFORMEDITOR} DebugLn('TSourcePageControl.AsyncDesignerSetFocus'); {$ENDIF}
  DesignerSetFocus;
  FDesignerSetFocusAsyncCount := 0;
end;

function TSourcePageControl.GetActiveTabDisplayState: TTabDisplayState;
begin
  Result := tdsNone;
  if ActivePage = FTabSheetCode then Exit(tdsCode)
  else if Assigned(FTabSheetDesigner) and (ActivePage = FTabSheetDesigner) then Exit(tdsDesign)
  else if Assigned(FTabSheetAnchors)  and (ActivePage = FTabSheetAnchors)  then Exit(tdsOther);
end;

procedure TSourcePageControl.SetDesignForm(const AValue: TDesignForm);
begin
  if (AValue = FDesignForm) then
    // for show lfm code, if we want after editing lfm go back to form without any error
    // (when we restart IDE some error can be raised )
    if (FResizer = nil)
    or ((AValue <> nil) and (FResizer.DesignForm = AValue)) then
      Exit;

  {$IFDEF DEBUGDOCKEDFORMEDITOR} DebugLn('TSourcePageControl.SetDesignForm: ', DbgSName(AValue)); {$ENDIF}

  FDesignForm := AValue;
  if AValue = nil then
  begin
    if Assigned(FResizer) then
      FResizer.DesignForm := nil;
  end else begin
    FDesignForm.OnAdjustPageNeeded := @OnAdjustPage;
    AValue.LastActiveSourceWindow := Owner as TSourceEditorWindowInterface;
    if Assigned(FResizer) then
      FResizer.DesignForm := AValue;
    AdjustPage;
  end;
end;

constructor TSourcePageControl.Create(ASourceEditor: TSourceEditorInterface);
var
  LParent: TWinControl;
begin
  {$IFDEF DEBUGDOCKEDFORMEDITOR} DebugLn('TSourcePageControl.Create'); {$ENDIF}

  inherited Create(ASourceEditor.EditorControl.Owner);
  FSourceEditor := ASourceEditor;
  FDesignerSetFocusAsyncCount := 0;
  FResizer := nil;

  TabPosition := DockedOptions.TabPosition;
  Align := alClient;
  ShowTabs := False;
  OnMouseUp := @SourcePageControlMouseUp;

  FTabSheetCode := TTabSheet.Create(Self);
  FTabSheetCode.PageControl := Self;
  FTabSheetCode.Caption := SCode;

  // place SynEdit into code tab
  LParent := ASourceEditor.EditorControl.Parent;
  ASourceEditor.EditorControl.Parent := FTabSheetCode;
  Parent := LParent;
end;

destructor TSourcePageControl.Destroy;
begin
  DesignForm := nil;
  inherited Destroy;
end;

procedure TSourcePageControl.AdjustPage;
begin
  if not DesignerPageActive then Exit;
  {$IFDEF DEBUGDOCKEDFORMEDITOR} DebugLn('TSourcePageControl.AdjustPage'); {$ENDIF}
  if Assigned(FResizer) then
    FResizer.AdjustResizer(nil);
end;

function TSourcePageControl.AnchorPageActive: Boolean;
begin
  Result := ActivePage = FTabSheetAnchors;
end;

procedure TSourcePageControl.CreateResizer;
begin
  {$IFDEF DEBUGDOCKEDFORMEDITOR} DebugLn('TSourcePageControl.CreateResizer'); {$ENDIF}
  if Assigned(FResizer) then
    raise Exception.Create('TSourcePageControl.CreateResizer: Resizer already created');
  FResizer := TResizer.Create(Self);
  if not Assigned(FTabSheetDesigner) then
    CreateTabSheetDesigner;
  FResizer.Parent := FTabSheetDesigner;
end;

procedure TSourcePageControl.CreateTabSheetAnchors;
begin
  if not DockedOptions.AnchorTabVisible then Exit;
  if Assigned(FTabSheetAnchors) then Exit;
  {$IFDEF DEBUGDOCKEDFORMEDITOR} DebugLn('TSourcePageControl.CreateTabSheetAnchors'); {$ENDIF}
  FTabSheetAnchors := TTabSheet.Create(Self);
  FTabSheetAnchors.PageControl := Self;
  FTabSheetAnchors.Caption := SAnchors;
end;

procedure TSourcePageControl.CreateTabSheetDesigner;
begin
  if Assigned(FTabSheetDesigner) then Exit;
  {$IFDEF DEBUGDOCKEDFORMEDITOR} DebugLn('TSourcePageControl.CreateTabSheetDesigner'); {$ENDIF}
  FTabSheetDesigner := TTabSheet.Create(Self);
  FTabSheetDesigner.PageControl := Self;
  FTabSheetDesigner.Caption := SDesigner;
end;

procedure TSourcePageControl.DesignerSetFocus;
begin
  if not Assigned(Resizer) then Exit;
  if not Assigned(DesignForm) then Exit;
  {$IFDEF DEBUGDOCKEDFORMEDITOR} DebugLn('TSourcePageControl.DesignerSetFocus'); {$ENDIF}
  Resizer.DesignerSetFocus;
end;

procedure TSourcePageControl.DesignerSetFocusAsync;
begin
  if FDesignerSetFocusAsyncCount = 0 then
    Application.QueueAsyncCall(@AsyncDesignerSetFocus, 0);
  Inc(FDesignerSetFocusAsyncCount);
end;

function TSourcePageControl.DesignerPageActive: Boolean;
begin
  Result := (ActivePage = FTabSheetDesigner) or
            (ActivePage = FTabSheetAnchors);
end;

function TSourcePageControl.FormPageActive: Boolean;
begin
  Result := ActivePage = FTabSheetDesigner;
end;

procedure TSourcePageControl.RemoveDesignPages;
begin
  {$IFDEF DEBUGDOCKEDFORMEDITOR} DebugLn('TSourcePageControls.RemoveDesignPages'); {$ENDIF}
  RemoveTabSheetAnchors;
  FreeAndNil(FTabSheetDesigner);
  ShowTabs := False;
end;

procedure TSourcePageControl.RemoveTabSheetAnchors;
begin
  if not Assigned(FTabSheetAnchors) then Exit;
  FreeAndNil(FTabSheetAnchors);
end;

procedure TSourcePageControl.InitPage;
begin
  ShowTabs := PageCount > 1;
  {$IFDEF DEBUGDOCKEDFORMEDITOR} DebugLn('TSourcePageControls.InitPage: ShowTabs[' + ShowTabs.ToString(TUseBoolStrs.True) + ']'); {$ENDIF}
  if ActivePage = FTabSheetDesigner then
  begin
    Resizer.Parent := FTabSheetDesigner;
    Resizer.ResizeControl.FormClient.Visible := True;
    Resizer.ResizeControl.AnchorContainer.Visible := False;
  end
  else if ActivePage = FTabSheetAnchors then
  begin
    Resizer.Parent := FTabSheetAnchors;
    Resizer.ResizeControl.FormClient.Visible := False;
    Resizer.ResizeControl.AnchorContainer.Visible := True;
    if not Assigned(DesignForm.AnchorDesigner) then
    begin
      DesignForm.AnchorDesigner := TAnchorDesigner.Create(DesignForm, Resizer.ResizeControl.AnchorContainer);
      DesignForm.AnchorDesigner.OnDesignerSetFocus := @DesignerSetFocus;
    end;
    DesignForm.AnchorDesigner.Refresh;
  end;
end;

procedure TSourcePageControl.RefreshResizer;
begin
  if not Assigned(FResizer) then Exit;
  {$IFDEF DEBUGDOCKEDFORMEDITOR} DebugLn('TSourcePageControls.RefreshResizer'); {$ENDIF}
  FreeAndNil(FResizer);
  CreateResizer;
end;

procedure TSourcePageControl.ShowCode;
begin
  {$IFDEF DEBUGDOCKEDFORMEDITOR} DebugLn('TSourcePageControls.ShowCode'); {$ENDIF}
  PageIndex := 0;
  InitPage;
end;

procedure TSourcePageControl.ShowDesigner(AIndex: Integer);
begin
  {$IFDEF DEBUGDOCKEDFORMEDITOR} DebugLn('TSourcePageControls.ShowDesigner'); {$ENDIF}
  if (AIndex = 0) or not (Pages[AIndex].TabVisible) then
    AIndex := 1;
  if PageCount <= AIndex then Exit;
  if not Pages[AIndex].TabVisible then Exit;
  PageIndex := AIndex;
  InitPage;
  OnChange(Self);
end;

{ TSourcePageControls }

function TSourcePageControls.GetPageControl(ASrcEditor: TSourceEditorInterface): TSourcePageControl;
var
  LIndex: Integer;
begin
  LIndex := IndexOf(ASrcEditor);
  if LIndex >= 0 then
    Result := Items[LIndex]
  else
    Result := nil;
end;

function TSourcePageControls.GetSourceEditor(APageControl: TSourcePageControl): TSourceEditorInterface;
var
  LIndex: Integer;
begin
  LIndex := IndexOf(APageControl);
  if LIndex >= 0 then
    Result := Items[LIndex].SourceEditor
  else
    Result := nil;
end;

function TSourcePageControls.Contains(APageControl: TSourcePageControl): Boolean;
begin
  Result := IndexOf(APageControl) >= 0;
end;

function TSourcePageControls.Contains(ASrcEditor: TSourceEditorInterface): Boolean;
begin
  Result := IndexOf(ASrcEditor) >= 0;
end;

function TSourcePageControls.IndexOf(APageControl: TSourcePageControl): Integer;
var
  i: Integer;
begin
  Result := -1;
  for i := 0 to Count - 1 do
    if Items[i] = APageControl then
      Exit(i);
end;

function TSourcePageControls.IndexOf(ASrcEditor: TSourceEditorInterface): Integer;
var
  i: Integer;
begin
  Result := -1;
  for i := 0 to Count - 1 do
    if Items[i].SourceEditor = ASrcEditor then
      Exit(i);
end;

procedure TSourcePageControls.Remove(ASrcEditor: TSourceEditorInterface);
var
  LIndex: Integer;
begin
  LIndex := IndexOf(ASrcEditor);
  if LIndex < 0 then Exit;
  Delete(LIndex);
end;

end.