File: pagespropeditdlg.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (203 lines) | stat: -rw-r--r-- 5,215 bytes parent folder | download | duplicates (6)
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
{
 *****************************************************************************
  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************

  Author: Michael W. Vogel

  Abstract:
    Dialog for the TPage property editor.
}
unit PagesPropEditDlg;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,
  // LCL
  Forms, ButtonPanel, StdCtrls, Dialogs, ExtCtrls, Controls,
  // IdeIntf
  ObjInspStrConsts, IDEDialogs;
  
type

  { TPagesPropEditorFrm }

  TPagesPropEditorFrm = class(TForm)
    BtnPanel: TButtonPanel;
    TextGroupBox: TGroupBox;
    ListBox: TListBox;
    AddButton: TButton;
    RenameButton: TButton;
    DeleteButton: TButton;
    MoveUpButton: TButton;
    MoveDownButton: TButton;
    procedure AddButtonClick(Sender: TObject);
    procedure DeleteButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ListBoxSelectionChange(Sender: TObject; User: boolean);
    procedure MoveDownButtonClick(Sender: TObject);
    procedure MoveUpButtonClick(Sender: TObject);
    procedure RenameButtonClick(Sender: TObject);
  private
    function GetNextPageName: String;
    procedure InvalidateButtons;
  end;


implementation

{$R *.lfm}

uses
  PropEdits;

{ TPagesPropEditorFrm }

procedure TPagesPropEditorFrm.AddButtonClick(Sender: TObject);
var
  aName: String;
begin
  try
    aName := GetNextPageName;
    if not InputQuery(oisAddPage, oisInsertPageName, aName) then Exit;
    if not IsValidIdent(aName) then
      raise Exception.Create(Format(oisComponentNameIsNotAValidIdentifier, [aName]));
    if ListBox.Items.IndexOf(aName) >= 0 then
      raise Exception.Create(Format(oisComponentNameIsNotAValidIdentifier, [aName]));
    ListBox.AddItem(aName, nil);
  except
    on e: Exception do
      ShowMessage(e.Message);
  end;
  InvalidateButtons;
end;

procedure TPagesPropEditorFrm.FormCreate(Sender: TObject);
begin
  Caption := oisPagesEditorDialog;
  TextGroupBox.Caption := oisPages;
  AddButton.Caption := oisAdd;
  RenameButton.Caption := oisRename;
  DeleteButton.Caption := oisDelete;
  MoveUpButton.Caption := rscdMoveUp;
  MoveDownButton.Caption := rscdMoveDown;
end;

procedure TPagesPropEditorFrm.DeleteButtonClick(Sender: TObject);
begin
  if IDEQuestionDialog(nbcesDeletePage, oisDeletePageQuestion,
    mtConfirmation, [mrYes, mrNo, mrCancel]) <> mrYes then Exit;
  ListBox.DeleteSelected;
  InvalidateButtons;
end;

procedure TPagesPropEditorFrm.FormShow(Sender: TObject);
begin
  InvalidateButtons;
end;

procedure TPagesPropEditorFrm.ListBoxSelectionChange(Sender: TObject; User: boolean);
begin
  if not User then Exit;
  InvalidateButtons;
end;

procedure TPagesPropEditorFrm.MoveDownButtonClick(Sender: TObject);
var
  Index: Integer;
begin
  Index := ListBox.ItemIndex;
  ListBox.Items.Move(Index, Index + 1);
  ListBox.ItemIndex := Index + 1;
end;

procedure TPagesPropEditorFrm.MoveUpButtonClick(Sender: TObject);
var
  Index: Integer;
begin
  Index := ListBox.ItemIndex;
  ListBox.Items.Move(Index, Index - 1);
  ListBox.ItemIndex := Index - 1;
end;

procedure TPagesPropEditorFrm.RenameButtonClick(Sender: TObject);
var
  aName: String;
begin
  try
    aName := ListBox.Items[ListBox.ItemIndex];
    if not InputQuery(oisRenamePage, oisInsertPageName, aName) then Exit;
    if aName = ListBox.Items[ListBox.ItemIndex] then Exit;
    if not IsValidIdent(aName) then
      raise Exception.Create(Format(oisComponentNameIsNotAValidIdentifier, [aName]));
    if ListBox.Items.IndexOf(aName) >= 0 then
      raise Exception.Create(Format(oisComponentNameIsNotAValidIdentifier, [aName]));
    ListBox.Items[ListBox.ItemIndex] := aName;
  except
    on e: Exception do
      ShowMessage(e.Message);
  end;
end;

function TPagesPropEditorFrm.GetNextPageName: String;
var
  i, j: integer;
begin
  // same as TCustomFormEditor.CreateUniqueComponentName
  i := 1;
  while True do begin
    j := ListBox.Items.Count - 1;
    Result := ClassNameToComponentName(TPage.ClassName);
    if Result[Length(Result)] in ['0'..'9'] then
      Result := Result + '_';
    Result := Result + IntToStr(i);
    while (j >= 0)
    and (CompareText(Result, ListBox.Items[j]) <> 0) do
      dec(j);
    if j < 0 then Exit;
    inc(i);
  end;
end;

procedure TPagesPropEditorFrm.InvalidateButtons;
begin
  if ListBox.Count = 0 then
  begin
    RenameButton.Enabled := False;
    DeleteButton.Enabled := False;
    MoveUpButton.Enabled := False;
    MoveDownButton.Enabled := False;
    Exit;
  end;

  if ListBox.Count = 1 then
  begin
    RenameButton.Enabled := True;
    DeleteButton.Enabled := True;
    MoveUpButton.Enabled := False;
    MoveDownButton.Enabled := False;
    ListBox.ItemIndex := 0;
    Exit;
  end;

  RenameButton.Enabled := True;
  DeleteButton.Enabled := True;
  if ListBox.ItemIndex < 0 then
    ListBox.ItemIndex := 0;
  if ListBox.ItemIndex = 0 then
    MoveUpButton.Enabled := False
  else
    MoveUpButton.Enabled := True;
  if ListBox.ItemIndex = ListBox.Count - 1 then
    MoveDownButton.Enabled := False
  else
    MoveDownButton.Enabled := True;
end;

end.