summaryrefslogtreecommitdiff
path: root/docview/src/frm_bookmarks.pas
blob: 4180b74f1a0d57acf2f07cb0ee16cb0b88c1594a (plain)
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
unit frm_bookmarks;

{$mode objfpc}{$H+}

interface

uses
  SysUtils,
  Classes,
  fpg_base,
  fpg_main,
  fpg_form,
  fpg_listbox,
  fpg_button,
  HelpBookmark;

type
  TBookmarkCallback = procedure(Bookmark: TBookmark) of object;

  TBookmarksForm = class(TfpgForm)
  private
    {@VFD_HEAD_BEGIN: BookmarksForm}
    lbBookmarks: TfpgListBox;
    btnRename: TfpgButton;
    btnDelete: TfpgButton;
    btnGoTo: TfpgButton;
    btnHelp: TfpgButton;
    btnClose: TfpgButton;
    {@VFD_HEAD_END: BookmarksForm}
    FBookmarkList: TList;
    FOnBookmarksChanged: TNotifyEvent;
    FOnGotoBookmark: TBookmarkCallback;
    procedure lbBookmarksKeyPressed(Sender: TObject; var KeyCode: word; var ShiftState: TShiftState; var Consumed: boolean);
    procedure FormShow(Sender: TObject);
    procedure lbBookmarksDoubleClicked(Sender: TObject; AButton: TMouseButton; AShift: TShiftState; const AMousePos: TPoint);
    procedure btnRenameClicked(Sender: TObject);
    procedure btnDeleteClicked(Sender: TObject);
    procedure btnGotoClicked(Sender: TObject);
    procedure btnHelpClicked(Sender: TObject);
    procedure btnCloseClicked(Sender: TObject);
    function SelectedObject(ListBox: TfpgListBox): TObject;
    procedure UpdateControls;
    function GetSelectedBookmark: TBookmark;
    procedure GotoSelectedBookmark;
  public
    procedure AfterCreate; override;
    procedure RefreshList;
  published
    property BookmarkList: TList read FBookmarkList write FBookmarkList;
    property OnBookmarksChanged: TNotifyEvent read FOnBookmarksChanged write FOnBookmarksChanged;
    property OnGotoBookmark: TBookmarkCallback read FOnGotoBookmark write FOnGotoBookmark;
  end;

{@VFD_NEWFORM_DECL}

implementation

uses
  fpg_dialogs;

{@VFD_NEWFORM_IMPL}

procedure TBookmarksForm.lbBookmarksKeyPressed(Sender: TObject;
  var KeyCode: word; var ShiftState: TShiftState; var Consumed: boolean);
begin
  if (KeyCode = keyEnter) or (KeyCode = keyPEnter) then
  begin
    GotoSelectedBookmark;
    Close;
  end;
end;

procedure TBookmarksForm.FormShow(Sender: TObject);
begin
  RefreshList;
  lbBookmarks.SetFocus;
end;

procedure TBookmarksForm.lbBookmarksDoubleClicked(Sender: TObject;
  AButton: TMouseButton; AShift: TShiftState; const AMousePos: TPoint);
begin
  GotoSelectedBookmark;
  Close;
end;

procedure TBookmarksForm.btnRenameClicked(Sender: TObject);
var
  Bookmark: TBookmark;
  lName: TfpgString;
begin
  Bookmark := GetSelectedBookmark;
  if Bookmark = nil then
    exit;

  lName := Bookmark.Name;
  if fpgInputQuery( 'Rename Bookmark', 'Enter the new name of the bookmark', lName ) then
  begin
    Bookmark.Name := lName;
    if Assigned(OnBookmarksChanged) then
      OnBookmarksChanged(self);
    // redisplay name in list
    lbBookmarks.Items[lbBookmarks.FocusItem] := Bookmark.Name;
    lbBookmarks.Invalidate;
  end;
end;

procedure TBookmarksForm.btnDeleteClicked(Sender: TObject);
var
  Bookmark: TBookmark;
  BookmarkIndex: integer;
begin
  Bookmark := GetSelectedBookmark;
  if Bookmark = nil then
    exit;

  if TfpgMessageDialog.Question('Delete Bookmark',
      Format('Delete the bookmark named "%s"?', [Bookmark.Name])) = mbYes then
  begin
    BookmarkIndex := BookmarkList.IndexOf( Bookmark );
    lbBookmarks.Items.Delete( BookmarkIndex );
    BookmarkList.Delete( BookmarkIndex );

    if BookmarkIndex > BookmarkList.Count - 1 then
      BookmarkIndex := BookmarkList.Count - 1;

    lbBookmarks.FocusItem := BookmarkIndex;

    Bookmark.Free;
    if Assigned(OnBookmarksChanged) then
      OnBookmarksChanged(self);
    lbBookmarks.Invalidate;

    UpdateControls;
  end;
end;

procedure TBookmarksForm.btnGotoClicked(Sender: TObject);
begin
  GotoSelectedBookmark;
end;

procedure TBookmarksForm.btnHelpClicked(Sender: TObject);
begin
  InvokeHelp;
end;

procedure TBookmarksForm.btnCloseClicked(Sender: TObject);
begin
  Close;
end;

function TBookmarksForm.SelectedObject(ListBox: TfpgListBox): TObject;
begin
  if (ListBox.FocusItem >= 0) and (ListBox.FocusItem < ListBox.Items.Count) then
    Result := ListBox.Items.Objects[ListBox.FocusItem]
  else
    Result := nil;
end;

procedure TBookmarksForm.UpdateControls;
var
  Selected: Boolean;
begin
  Selected := GetSelectedBookmark <> nil;
  btnRename.Enabled := Selected;
  btnDelete.Enabled := Selected;
  btnGoto.Enabled := Selected;
  if not btnGoto.Enabled then
    btnGoto.Default := false;
end;

function TBookmarksForm.GetSelectedBookmark: TBookmark;
begin
  if SelectedObject(lbBookmarks) = nil then
    result := nil
  else
    result := SelectedObject(lbBookmarks) as TBookmark;
end;

procedure TBookmarksForm.GotoSelectedBookmark;
begin
  if Assigned(FOnGotoBookmark) then
    if GetSelectedBookmark <> nil then
      FOnGotoBookmark(GetSelectedBookmark);
end;

procedure TBookmarksForm.AfterCreate;
begin
  {%region 'Auto-generated GUI code' -fold}
  {@VFD_BODY_BEGIN: BookmarksForm}
  Name := 'BookmarksForm';
  SetPosition(553, 246, 393, 247);
  WindowTitle := 'Bookmarks';
  Hint := '';
  HelpType := htContext;
  HelpContext := 8;
  OnShow := @FormShow;

  lbBookmarks := TfpgListBox.Create(self);
  with lbBookmarks do
  begin
    Name := 'lbBookmarks';
    SetPosition(8, 12, 272, 227);
    Anchors := [anLeft,anRight,anTop,anBottom];
    FontDesc := '#List';
    Hint := '';
    TabOrder := 1;
    OnDoubleClick := @lbBookmarksDoubleClicked;
    OnKeyPress := @lbBookmarksKeyPressed;
  end;

  btnRename := TfpgButton.Create(self);
  with btnRename do
  begin
    Name := 'btnRename';
    SetPosition(288, 16, 96, 23);
    Anchors := [anRight,anTop];
    Text := 'Rename...';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 2;
    OnClick := @btnRenameClicked;
  end;

  btnDelete := TfpgButton.Create(self);
  with btnDelete do
  begin
    Name := 'btnDelete';
    SetPosition(288, 44, 96, 23);
    Anchors := [anRight,anTop];
    Text := 'Delete';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 3;
    OnClick := @btnDeleteClicked;
  end;

  btnGoTo := TfpgButton.Create(self);
  with btnGoTo do
  begin
    Name := 'btnGoTo';
    SetPosition(288, 72, 96, 23);
    Anchors := [anRight,anTop];
    Text := 'Goto';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 4;
    Default := True;
    OnClick := @btnGotoClicked;
  end;

  btnHelp := TfpgButton.Create(self);
  with btnHelp do
  begin
    Name := 'btnHelp';
    SetPosition(288, 100, 96, 23);
    Anchors := [anRight,anTop];
    Text := 'Help';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 5;
    OnClick := @btnHelpClicked;
  end;

  btnClose := TfpgButton.Create(self);
  with btnClose do
  begin
    Name := 'btnClose';
    SetPosition(288, 217, 96, 23);
    Anchors := [anRight,anBottom];
    Text := 'Close';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 6;
    OnClick := @btnCloseClicked;
  end;

  {@VFD_BODY_END: BookmarksForm}
  {%endregion}
end;

procedure TBookmarksForm.RefreshList;
var
  i: integer;
  Bookmark: TBookmark;
Begin
  lbBookmarks.Items.BeginUpdate;

  lbBookmarks.Items.Clear;

  if not Assigned(BookmarkList) then
    exit;

  for i := 0 to BookmarkList.Count - 1 do
  begin
    Bookmark := TBookmark(BookmarkList[i]);
    lbBookmarks.Items.AddObject(Bookmark.Name, Bookmark);
  end;

  if lbBookmarks.Items.Count > 0 then
    lbBookmarks.FocusItem := 0;

  lbBookmarks.Items.EndUpdate;
  UpdateControls;
End;

end.