Menu

[327ada]: / Source / EditorList.pas  Maximize  Restore  History

Download this file

662 lines (579 with data), 20.0 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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
{
This file is part of Dev-C++
Copyright (c) 2004 Bloodshed Software
Dev-C++ is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Dev-C++ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Dev-C++; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
}
unit EditorList;
interface
uses
Windows, SysUtils, Dialogs, StdCtrls, Controls, ComCtrls, Forms, Editor, ExtCtrls,
devrun, version, project, utils, ProjectTypes, Classes, Graphics, Math, Messages;
type
TLayoutShowType = (lstNone, lstLeft, lstRight, lstBoth);
TEditorList = class
private
fLayout: TLayoutShowType;
fLeftPageControl: TPageControl;
fRightPageControl: TPageControl;
fSplitter: TSplitter;
fPanel: TPanel; // ui component that is the base layer for all page controls
fUpdateCount: integer;
function GetForEachEditor(index: integer): TEditor;
function GetPageCount: integer;
function GetFocusedPageControl: TPageControl;
function GetNewEditorPageControl: TPageControl;
procedure ShowLayout(Layout: TLayoutShowType);
public
function NewEditor(const Filename: String; InProject, NewFile: boolean; PageControl: TPageControl = nil; AEncoding: TEncoding = nil):
TEditor;
function FileIsOpen(const FileName: String; ProjectOnly: boolean = FALSE): TEditor;
function GetEditor(PageIndex: integer = -1; PageControl: TPageControl = nil): TEditor;
function GetEditorFromFileName(const FileName: String; AEncoding: TEncoding = nil): TEditor;
function GetEditorFromTag(tag: integer): TEditor;
function GetPreviousEditor(Editor: TEditor): TEditor;
procedure ForceCloseEditor(Editor: TEditor); // close, no questions asked
function CloseEditor(Editor: TEditor): Boolean;
function CloseAll: boolean;
function CloseAllButThis: boolean;
function SwapEditor(Editor: TEditor): boolean;
procedure OnPanelResize(Sender: TObject);
procedure SelectNextPage;
procedure SelectPrevPage;
procedure BeginUpdate; // stops redraws
procedure EndUpdate; // ends redraws
procedure UpdateLayout; // reconfigures layout
procedure GetVisibleEditors(var Left: TEditor; var Right: TEditor);
procedure SetPreferences(TabPosition: TTabPosition; MultiLine: boolean);
property LeftPageControl: TPageControl read fLeftPageControl write fLeftPageControl;
property RightPageControl: TPageControl read fRightPageControl write fRightPageControl;
property Splitter: TSplitter read fSplitter write fSplitter;
property Panel: TPanel read fPanel write fPanel;
property PageCount: integer read GetPageCount;
property Editors[Index: integer]: TEditor read GetForEachEditor; default;
property FocusedPageControl: TPageControl read GetFocusedPageControl;
property Layout: TLayoutShowType read fLayout;
end;
implementation
uses
System.UItypes, main, MultiLangSupport, DataFrm;
function TEditorList.GetPageCount: integer;
begin
Result := fLeftPageControl.PageCount + fRightPageControl.PageCount;
end;
procedure TEditorList.BeginUpdate;
begin
Inc(fUpdateCount);
if fUpdateCount = 1 then
SendMessage(fPanel.Handle, WM_SETREDRAW, 0, 0); // stop drawing, it's slow
end;
procedure TEditorList.EndUpdate;
begin
Dec(fUpdateCount);
if fUpdateCount = 0 then begin
SendMessage(fPanel.Handle, WM_SETREDRAW, 1, 0); // allow drawing again
RedrawWindow(fPanel.Handle, nil, 0, RDW_ERASE or RDW_FRAME or RDW_INVALIDATE or RDW_ALLCHILDREN); // draw once
end;
end;
function TEditorList.GetFocusedPageControl: TPageControl;
var
ActivePage: TTabSheet;
begin
case fLayout of
lstLeft: begin
Result := fLeftPageControl
end;
lstRight: begin
Result := fRightPageControl
end;
lstBoth: begin
// Check if right is focused, otherwise assume left one is focused
ActivePage := fRightPageControl.ActivePage;
if TEditor(ActivePage.Tag).Text.Focused then
Result := fRightPageControl
else
Result := fLeftPageControl; // no focus -> left one
end;
lstNone: begin
Result := nil;
end;
else
Result := nil;
end;
end;
function TEditorList.GetForEachEditor(index: integer): TEditor;
begin
// Is it within range of the first one?
if (index >= 0) and (index < fLeftPageControl.PageCount) then begin
result := TEditor(fLeftPageControl.Pages[index].Tag);
Exit;
end;
// Nope? Check second one
Dec(index, fLeftPageControl.PageCount);
if (index >= 0) and (index < fRightPageControl.PageCount) then begin
result := TEditor(fRightPageControl.Pages[index].Tag);
Exit;
end;
Result := nil;
end;
function TEditorList.GetNewEditorPageControl;
begin
case fLayout of
lstNone: begin
Result := fLeftPageControl; // first editor should be shown in the leftmost control
end;
lstLeft: begin
Result := fLeftPageControl;
end;
lstRight: begin
Result := fRightPageControl;
end;
lstBoth: begin
Result := GetFocusedPageControl; // depends on the current keyboard focus
end;
else
Result := nil;
end;
end;
function TEditorList.NewEditor(const Filename: String; InProject, NewFile: boolean; PageControl: TPageControl =
nil; AEncoding: TEncoding = nil):
TEditor;
var
ParentPageControl: TPageControl;
begin
BeginUpdate;
try
if PageControl = nil then
ParentPageControl := GetNewEditorPageControl
else
ParentPageControl := PageControl;
Result := TEditor.Create(FileName, InProject, NewFile, ParentPageControl, AEncoding);
// Force layout update when creating, destroying or moving editors
UpdateLayout;
finally
EndUpdate; // redraw once
end;
end;
function TEditorList.FileIsOpen(const FileName: String; ProjectOnly: boolean = FALSE): TEditor;
var
e: TEditor;
I: integer;
begin
// Check first page control
for I := 0 to fLeftPageControl.PageCount - 1 do begin
e := GetEditor(I, fLeftPageControl);
if SameFileName(e.FileName, FileName) then
// Accept the file if it's in a project OR if it doesn't have to be in a project
if (not ProjectOnly) or e.InProject then begin
Result := e;
Exit;
end;
end;
// Same for the right page control
for I := 0 to fRightPageControl.PageCount - 1 do begin
e := GetEditor(I, fRightPageControl);
if SameFileName(e.FileName, FileName) then
// Accept the file if it's in a project OR if it doesn't have to be in a project
if (not ProjectOnly) or e.InProject then begin
Result := e;
Exit;
end;
end;
Result := nil;
end;
function TEditorList.GetEditor(PageIndex: integer = -1; PageControl: TPageControl = nil): TEditor;
var
SelectedPageControl: TPageControl;
TabSheet: TTabSheet;
begin
Result := nil;
// Select page control
if PageControl = nil then
SelectedPageControl := GetFocusedPageControl
else
SelectedPageControl := PageControl;
if not Assigned(SelectedPageControl) then
Exit;
// Select tab in selected pagecontrol
case PageIndex of
-1: TabSheet := SelectedPageControl.ActivePage;
else
TabSheet := SelectedPageControl.Pages[PageIndex];
end;
if not Assigned(TabSheet) then
Exit;
Result := TEditor(TabSheet.Tag);
end;
function TEditorList.GetPreviousEditor(Editor: TEditor): TEditor;
var
I: integer;
EditorPageControl: TPageControl;
PrevNaturalPage: TTabSheet;
e: TEditor;
begin
result := nil;
if not Assigned(Editor) then
Exit;
// Determine what to view next
EditorPageControl := Editor.PageControl;
if assigned(EditorPageControl) then
with EditorPageControl do begin
// If we are closing the active tab, open the tab that was opened when this tab was created
if ActivePage = Editor.TabSheet then begin // this is the current page...
// Find the first tab in the history list that is still open
for I := Editor.PreviousEditors.Count - 1 downto 0 do begin
e := GetEditorFromTag(Integer(Editor.PreviousEditors[i]));
if Assigned(e) then begin
Result := e;
Exit;
end;
end;
// All history items are gone or this was the first tab to open which has no history
// Select the editor that would appear naturally when closing this one
PrevNaturalPage := FindNextPage(Editor.TabSheet, False, True);
if Assigned(PrevNaturalPage) and (PrevNaturalPage <> Editor.TabSheet) then begin
Result := GetEditor(PrevNaturalPage.TabIndex, EditorPageControl);
Exit;
end;
// All editors in the current page control are gone. Try the other page control
if fLayout = lstBoth then begin
if EditorPageControl = LeftPageControl then begin
Result := GetEditor(-1, RightPageControl);
Exit;
end else begin
Result := GetEditor(-1, LeftPageControl);
Exit;
end;
end;
// If we are not closing the active tab, don't change focus
end else begin
Result := GetEditor(-1, EditorPageControl);
end;
end;
end;
procedure TEditorList.ForceCloseEditor(Editor: TEditor);
begin
BeginUpdate;
try
FreeAndNil(Editor);
// Force layout update when creating, destroying or moving editors
UpdateLayout;
finally
EndUpdate; // redraw once
end;
end;
function TEditorList.CloseEditor(Editor: TEditor): Boolean;
var
projindex: integer;
PrevEditor: TEditor;
begin
Result := False;
if not Assigned(Editor) then
Exit;
// Ask user if he wants to save
if Editor.Text.Modified and not Editor.Text.Text.IsEmpty then begin
case MessageDlg(Format(Lang[ID_MSG_ASKSAVECLOSE], [Editor.FileName]), mtConfirmation, mbYesNoCancel, 0) of
mrYes:
if not Editor.Save then
Exit;
mrCancel:
begin
Exit; // stop closing
end;
end;
end;
// Select editor to open when this one closes
PrevEditor := GetPreviousEditor(Editor);
if PrevEditor = Editor then
PrevEditor := nil;
BeginUpdate;
try
// We're allowed to close...
Result := True;
if Editor.InProject and Assigned(MainForm.Project) then begin
projindex := MainForm.Project.Units.IndexOf(Editor);
if projindex <> -1 then
begin
MainForm.Project.CloseUnit(projindex); // calls ForceCloseEditor
end;
end else begin
dmMain.AddtoHistory(Editor.FileName);
FreeAndNil(Editor);
// Force layout update when creating, destroying or moving editors
UpdateLayout;
end;
// Show new editor after forcing a layout update
if Assigned(PrevEditor) then
PrevEditor.Activate;
finally
EndUpdate; // redraw once
end;
end;
function TEditorList.CloseAll: boolean;
begin
Result := False;
// Redraw once after the whole ordeal
BeginUpdate;
try
// Keep closing the first one to prevent redrawing
while fLeftPageControl.PageCount > 0 do
if not CloseEditor(GetEditor(0, fLeftPageControl)) then
Exit;
// Same for the right page control
while fRightPageControl.PageCount > 0 do
if not CloseEditor(GetEditor(0, fRightPageControl)) then
Exit;
finally
EndUpdate;
end;
Result := True;
end;
function TEditorList.CloseAllButThis: boolean;
var
I: integer;
ActiveEditor, Editor: TEditor;
begin
Result := False;
// Redraw once after the whole ordeal
BeginUpdate;
try
// Keep closing the first one to prevent redrawing
ActiveEditor := GetEditor(-1, fLeftPageControl);
for I := fLeftPageControl.PageCount - 1 downto 0 do begin
Editor := GetEditor(I, fLeftPageControl);
if Assigned(Editor) and (Editor <> ActiveEditor) then
if not CloseEditor(Editor) then
Exit;
end;
// Keep closing the first one to prevent redrawing
ActiveEditor := GetEditor(-1, fRightPageControl);
for I := fRightPageControl.PageCount - 1 downto 0 do begin
Editor := GetEditor(I, fRightPageControl);
if Assigned(Editor) and (Editor <> ActiveEditor) then
if not CloseEditor(Editor) then
Exit;
end;
finally
EndUpdate;
end;
Result := True;
end;
function TEditorList.GetEditorFromFileName(const FileName: String; AEncoding: TEncoding = nil): TEditor;
var
FullFileName: String;
I: integer;
e: TEditor;
begin
Result := nil;
// ExpandFileName reduces all the "\..\" in the path
FullFileName := ExpandFileName(FileName);
// First, check wether the file is already open
for I := 0 to fLeftPageControl.PageCount - 1 do begin
e := GetEditor(I, fLeftPageControl);
if Assigned(e) then begin
if SameFileName(e.FileName, FullFileName) then begin
Result := e;
Exit;
end;
end;
end;
// Same for the right page control
for I := 0 to fRightPageControl.PageCount - 1 do begin
e := GetEditor(I, fRightPageControl);
if Assigned(e) then begin
if SameFileName(e.FileName, FullFileName) then begin
Result := e;
Exit;
end;
end;
end;
// Then check the project...
if Assigned(MainForm.Project) then begin
I := MainForm.Project.GetUnitFromString(FullFileName);
if I <> -1 then begin
result := MainForm.Project.OpenUnit(I);
Exit;
end;
end;
// Else, just open from disk
if FileExists(FullFileName) then
Result := NewEditor(FullFileName, False, False, nil, AEncoding);
end;
function TEditorList.GetEditorFromTag(tag: integer): TEditor;
var
I: integer;
begin
result := nil;
// First, check wether the file is already open
for I := 0 to fLeftPageControl.PageCount - 1 do begin
if fLeftPageControl.Pages[i].Tag = tag then begin
result := TEditor(fLeftPageControl.Pages[i].Tag);
break;
end;
end;
// Same for the right page control
for I := 0 to fRightPageControl.PageCount - 1 do begin
if fRightPageControl.Pages[i].Tag = tag then begin
result := TEditor(fRightPageControl.Pages[i].Tag);
break;
end;
end;
end;
function TEditorList.SwapEditor(Editor: TEditor): boolean;
var
FromPageControl: TPageControl;
FromPageControlPrevTab: TTabSheet;
begin
Result := True;
// Redraw once after the whole ordeal
BeginUpdate;
try
// Remember old index
FromPageControl := Editor.PageControl;
FromPageControlPrevTab := Editor.PageControl.FindNextPage(Editor.TabSheet, False, True);
// Determine how to swap
if Editor.PageControl = fLeftPageControl then
Editor.PageControl := fRightPageControl
else
Editor.PageControl := fLeftPageControl;
// Switch to previous editor in the other one
FromPageControl.ActivePage := FromPageControlPrevTab;
// Force layout update when creating, destroying or moving editors
UpdateLayout;
// Move editor focus too
Editor.Activate;
finally
EndUpdate;
end;
end;
procedure TEditorList.UpdateLayout;
begin
if (fLeftPageControl.PageCount = 0) and (fRightPageControl.PageCount = 0) then
ShowLayout(lstNone)
else if (fLeftPageControl.PageCount > 0) and (fRightPageControl.PageCount = 0) then
ShowLayout(lstLeft)
else if (fLeftPageControl.PageCount = 0) and (fRightPageControl.PageCount > 0) then
ShowLayout(lstRight)
else if (fLeftPageControl.PageCount > 0) and (fRightPageControl.PageCount > 0) then
ShowLayout(lstBoth);
end;
procedure TEditorList.ShowLayout(Layout: TLayoutShowType);
begin
if Layout = fLayout then
Exit;
fLayout := Layout;
// Apply widths if layout does not change
case fLayout of
lstLeft: begin
fLeftPageControl.Align := alClient;
fLeftPageControl.Visible := True;
// Hide other components
fRightPageControl.Visible := False;
fRightPageControl.Width := 0;
fSplitter.Visible := False;
fSplitter.Width := 0;
end;
lstRight: begin
fRightPageControl.Align := alClient;
fRightPageControl.Visible := True;
// Hide other components
fLeftPageControl.Visible := False;
fLeftPageControl.Width := 0;
fSplitter.Visible := False;
fSplitter.Width := 0;
end;
lstBoth: begin
// Align to the left, assign 50% area
fLeftPageControl.Align := alClient;
fLeftPageControl.Visible := True;
fLeftPageControl.Width := (fPanel.Width - 3) div 2;
fLeftPageControl.Left := 0;
// Put splitter in between
fSplitter.Align := alRight;
fSplitter.Visible := True;
fSplitter.Width := 3;
fSplitter.Left := fLeftPageControl.Width;
// Align other one to the right
fRightPageControl.Align := alRight;
fRightPageControl.Visible := True;
fRightPageControl.Width := (fPanel.Width - 3) div 2;
fRightPageControl.Left := fLeftPageControl.Width + 3;
end;
lstNone: begin
fLeftPageControl.Visible := False;
fLeftPageControl.Width := 0;
fRightPageControl.Visible := False;
fRightPageControl.Width := 0;
fSplitter.Visible := False;
fSplitter.Width := 0;
// Normally, change events are triggered by editor focus, but since there's no one left, fake it
// Notify of change AFTER we change the official layout
fLeftPageControl.OnChange(fLeftPageControl);
end;
end;
end;
procedure TEditorList.SelectNextPage;
var
PageControl: TPageControl;
begin
PageControl := GetFocusedPageControl;
if Assigned(PageControl) then
PageControl.SelectNextPage(True);
end;
procedure TEditorList.SelectPrevPage;
var
PageControl: TPageControl;
begin
PageControl := GetFocusedPageControl;
if Assigned(PageControl) then
PageControl.SelectNextPage(False);
end;
procedure TEditorList.GetVisibleEditors(var Left: TEditor; var Right: TEditor);
begin
case fLayout of
lstNone: begin
Left := nil;
Right := nil;
end;
lstLeft: begin
Left := GetEditor(-1, fLeftPageControl);
Right := nil;
end;
lstRight: begin
Left := nil;
Right := GetEditor(-1, fRightPageControl);
end;
lstBoth: begin
Left := GetEditor(-1, fLeftPageControl);
Right := GetEditor(-1, fLeftPageControl);
end;
end;
end;
procedure TEditorList.SetPreferences(TabPosition: TTabPosition; MultiLine: boolean);
begin
LeftPageControl.TabPosition := TabPosition;
LeftPageControl.MultiLine := MultiLine;
RightPageControl.TabPosition := TabPosition;
RightPageControl.MultiLine := MultiLine;
end;
procedure TEditorList.OnPanelResize(Sender: TObject);
//var
// LeftPageWidthPercent : integer;
begin
if fLayout = lstBoth then begin
// Force 50% layout
// TODO: better option would be to remember pagecontrol width percentages of parent panel
fLayout := lstNone;
ShowLayout(lstBoth);
end;
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.