1 {
2  *****************************************************************************
3   See the file COPYING.modifiedLGPL.txt, included in this distribution,
4   for details about the license.
5  *****************************************************************************
6 
7   Author: Michael W. Vogel
8 
9   Abstract:
10     Dialog for the TPage property editor.
11 }
12 unit PagesPropEditDlg;
13 
14 {$mode objfpc}{$H+}
15 
16 interface
17 
18 uses
19   Classes, SysUtils,
20   // LCL
21   Forms, ButtonPanel, StdCtrls, Dialogs, ExtCtrls, Controls,
22   // IdeIntf
23   ObjInspStrConsts, IDEDialogs;
24 
25 type
26 
27   { TPagesPropEditorFrm }
28 
29   TPagesPropEditorFrm = class(TForm)
30     BtnPanel: TButtonPanel;
31     TextGroupBox: TGroupBox;
32     ListBox: TListBox;
33     AddButton: TButton;
34     RenameButton: TButton;
35     DeleteButton: TButton;
36     MoveUpButton: TButton;
37     MoveDownButton: TButton;
38     procedure AddButtonClick(Sender: TObject);
39     procedure DeleteButtonClick(Sender: TObject);
40     procedure FormCreate(Sender: TObject);
41     procedure FormShow(Sender: TObject);
42     procedure ListBoxSelectionChange(Sender: TObject; User: boolean);
43     procedure MoveDownButtonClick(Sender: TObject);
44     procedure MoveUpButtonClick(Sender: TObject);
45     procedure RenameButtonClick(Sender: TObject);
46   private
GetNextPageNamenull47     function GetNextPageName: String;
48     procedure InvalidateButtons;
49   end;
50 
51 
52 implementation
53 
54 {$R *.lfm}
55 
56 uses
57   PropEdits;
58 
59 { TPagesPropEditorFrm }
60 
61 procedure TPagesPropEditorFrm.AddButtonClick(Sender: TObject);
62 var
63   aName: String;
64 begin
65   try
66     aName := GetNextPageName;
67     if not InputQuery(oisAddPage, oisInsertPageName, aName) then Exit;
68     if not IsValidIdent(aName) then
69       raise Exception.Create(Format(oisComponentNameIsNotAValidIdentifier, [aName]));
70     if ListBox.Items.IndexOf(aName) >= 0 then
71       raise Exception.Create(Format(oisComponentNameIsNotAValidIdentifier, [aName]));
72     ListBox.AddItem(aName, nil);
73   except
74     on e: Exception do
75       ShowMessage(e.Message);
76   end;
77   InvalidateButtons;
78 end;
79 
80 procedure TPagesPropEditorFrm.FormCreate(Sender: TObject);
81 begin
82   Caption := oisPagesEditorDialog;
83   TextGroupBox.Caption := oisPages;
84   AddButton.Caption := oisAdd;
85   RenameButton.Caption := oisRename;
86   DeleteButton.Caption := oisDelete;
87   MoveUpButton.Caption := rscdMoveUp;
88   MoveDownButton.Caption := rscdMoveDown;
89 end;
90 
91 procedure TPagesPropEditorFrm.DeleteButtonClick(Sender: TObject);
92 begin
93   if IDEQuestionDialog(nbcesDeletePage, oisDeletePageQuestion,
94     mtConfirmation, [mrYes, mrNo, mrCancel]) <> mrYes then Exit;
95   ListBox.DeleteSelected;
96   InvalidateButtons;
97 end;
98 
99 procedure TPagesPropEditorFrm.FormShow(Sender: TObject);
100 begin
101   InvalidateButtons;
102 end;
103 
104 procedure TPagesPropEditorFrm.ListBoxSelectionChange(Sender: TObject; User: boolean);
105 begin
106   if not User then Exit;
107   InvalidateButtons;
108 end;
109 
110 procedure TPagesPropEditorFrm.MoveDownButtonClick(Sender: TObject);
111 var
112   Index: Integer;
113 begin
114   Index := ListBox.ItemIndex;
115   ListBox.Items.Move(Index, Index + 1);
116   ListBox.ItemIndex := Index + 1;
117 end;
118 
119 procedure TPagesPropEditorFrm.MoveUpButtonClick(Sender: TObject);
120 var
121   Index: Integer;
122 begin
123   Index := ListBox.ItemIndex;
124   ListBox.Items.Move(Index, Index - 1);
125   ListBox.ItemIndex := Index - 1;
126 end;
127 
128 procedure TPagesPropEditorFrm.RenameButtonClick(Sender: TObject);
129 var
130   aName: String;
131 begin
132   try
133     aName := ListBox.Items[ListBox.ItemIndex];
134     if not InputQuery(oisRenamePage, oisInsertPageName, aName) then Exit;
135     if aName = ListBox.Items[ListBox.ItemIndex] then Exit;
136     if not IsValidIdent(aName) then
137       raise Exception.Create(Format(oisComponentNameIsNotAValidIdentifier, [aName]));
138     if ListBox.Items.IndexOf(aName) >= 0 then
139       raise Exception.Create(Format(oisComponentNameIsNotAValidIdentifier, [aName]));
140     ListBox.Items[ListBox.ItemIndex] := aName;
141   except
142     on e: Exception do
143       ShowMessage(e.Message);
144   end;
145 end;
146 
TPagesPropEditorFrm.GetNextPageNamenull147 function TPagesPropEditorFrm.GetNextPageName: String;
148 var
149   i, j: integer;
150 begin
151   // same as TCustomFormEditor.CreateUniqueComponentName
152   i := 1;
153   while True do begin
154     j := ListBox.Items.Count - 1;
155     Result := ClassNameToComponentName(TPage.ClassName);
156     if Result[Length(Result)] in ['0'..'9'] then
157       Result := Result + '_';
158     Result := Result + IntToStr(i);
159     while (j >= 0)
160     and (CompareText(Result, ListBox.Items[j]) <> 0) do
161       dec(j);
162     if j < 0 then Exit;
163     inc(i);
164   end;
165 end;
166 
167 procedure TPagesPropEditorFrm.InvalidateButtons;
168 begin
169   if ListBox.Count = 0 then
170   begin
171     RenameButton.Enabled := False;
172     DeleteButton.Enabled := False;
173     MoveUpButton.Enabled := False;
174     MoveDownButton.Enabled := False;
175     Exit;
176   end;
177 
178   if ListBox.Count = 1 then
179   begin
180     RenameButton.Enabled := True;
181     DeleteButton.Enabled := True;
182     MoveUpButton.Enabled := False;
183     MoveDownButton.Enabled := False;
184     ListBox.ItemIndex := 0;
185     Exit;
186   end;
187 
188   RenameButton.Enabled := True;
189   DeleteButton.Enabled := True;
190   if ListBox.ItemIndex < 0 then
191     ListBox.ItemIndex := 0;
192   if ListBox.ItemIndex = 0 then
193     MoveUpButton.Enabled := False
194   else
195     MoveUpButton.Enabled := True;
196   if ListBox.ItemIndex = ListBox.Count - 1 then
197     MoveDownButton.Enabled := False
198   else
199     MoveDownButton.Enabled := True;
200 end;
201 
202 end.
203 
204