summaryrefslogtreecommitdiff
path: root/examples/apps/nanoedit/mainfrm.pas
blob: f652e1798b68a06f4d5aa869f76f9626a89ed0af (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
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
unit mainfrm;

{$mode objfpc}{$H+}

interface

uses
  SysUtils, Classes, fpg_base, fpg_main,
  fpg_form, fpg_button, fpg_menu, fpg_textedit, fpg_panel,
  fpg_label;

type

  TMainForm = class(TfpgForm)
  private
    {@VFD_HEAD_BEGIN: MainFrom}
    menu: TfpgMenuBar;
    mnuFile: TfpgPopupMenu;
    mnuEdit: TfpgPopupMenu;
    mnuSearch: TfpgPopupMenu;
    memEditor: TfpgTextEdit;
    btnGO: TfpgButton;
    bevStatusBar: TfpgBevel;
    lblStatusText: TfpgLabel;
    {@VFD_HEAD_END: MainFrom}
    FTextToFind: TfpgString;
    FFindOptions: TfpgFindOptions;
    FIsForward: boolean;
    FFilename: TfpgString;
    procedure FormShow(Sender: TObject);
    procedure miOpenClick(Sender: TObject);
    procedure miSaveClick(Sender: TObject);
    procedure miSaveAsClick(Sender: TObject);
    procedure miGoToLineClick(Sender: TObject);
    procedure miFindClick(Sender: TObject);
    procedure miFindNextClick(Sender: TObject);
    procedure miQuitClick(Sender: TObject);
    procedure miCutClicked(Sender: TObject);
    procedure miCopyClicked(Sender: TObject);
    procedure miPasteClicked(Sender: TObject);
    procedure btnGOClick(Sender: TObject);
    procedure memEditorChanged(Sender: TObject);
    procedure UpdateStatus(const AMessage: TfpgString);
  public
    procedure AfterCreate; override;
  end;

{@VFD_NEWFORM_DECL}

implementation

uses
  elastictabstops,
  fpg_dialogs,
  frm_find;

{@VFD_NEWFORM_IMPL}

procedure TMainForm.FormShow(Sender: TObject);
var
  s: string;
begin
  if ParamCount > 0 then
  begin
//    ShowMessage(ParamStr(1));
    s := ParamStr(1);
    if Pos('file://', s) > 0 then
      s := StringReplace(s, 'file://', '', []);
    memEditor.LoadFromFile(s);
    FFilename := s;
    UpdateStatus(FFilename);
  end;
end;

procedure TMainForm.miOpenClick(Sender: TObject);
var
  dlg: TfpgFileDialog;
begin
  dlg := TfpgFileDialog.Create(nil);
  try
    if dlg.RunOpenFile then
    begin
      memEditor.LoadFromFile(dlg.FileName);
      FFileName := dlg.FileName;
      UpdateStatus(FFileName);
    end;
  finally
    dlg.Free;
  end;
end;

procedure TMainForm.miSaveClick(Sender: TObject);
var
  dlg: TfpgFileDialog;
begin
  dlg := TfpgFileDialog.Create(nil);
  try
    if FFilename <> '' then
      dlg.FileName := FFilename;
    if dlg.RunSaveFile then
    begin
      memEditor.SaveToFile(dlg.FileName);
      UpdateStatus(Format('<%s> successfully saved.', [FFileName]));
    end;
  finally
    dlg.Free;
  end;
end;

procedure TMainForm.miSaveAsClick(Sender: TObject);
var
  dlg: TfpgFileDialog;
begin
  dlg := TfpgFileDialog.Create(nil);
  try
    if FFilename <> '' then
      dlg.FileName := FFilename;
    if dlg.RunSaveFile then
    begin
      memEditor.Lines.SaveToFile(dlg.FileName);
      FFilename := dlg.FileName;
      UpdateStatus(Format('<%s> successfully saved.', [FFileName]));
    end;
  finally
    dlg.Free;
  end;
end;

procedure TMainForm.miGoToLineClick(Sender: TObject);
var
  sValue: string;
  i: integer;
begin
  if fpgInputQuery('Go to line', 'Go to line number?', sValue) then
  begin
    try
      i := StrToInt(sValue);
      memEditor.GotoLine(i);
    except
      on E: Exception do
         ShowMessage('Invalid line number.' + LineEnding + E.Message);
    end;
  end;
end;

procedure TMainForm.miFindClick(Sender: TObject);
var
  dlg: TFindForm;
begin
  FTextToFind := '';
  dlg := TFindForm.Create(nil);
  try
    if dlg.Execute then
    begin
      FTextToFind := dlg.TextToFind;
      FFindOptions := dlg.FindOptions;
      FIsForward := dlg.IsForward;
      miFindNextClick(nil);
    end;
  finally
    dlg.Free;
  end;
end;

procedure TMainForm.miFindNextClick(Sender: TObject);
begin
  if FTextToFind <> '' then
    memEditor.FindText(FTextToFind, FFindOptions, FIsForward);
end;

procedure TMainForm.miQuitClick(Sender: TObject);
begin
  Close;
end;

procedure TMainForm.miCutClicked(Sender: TObject);
begin
  memEditor.CutToClipboard;
end;

procedure TMainForm.miCopyClicked(Sender: TObject);
begin
  memEditor.CopyToClipboard;
end;

procedure TMainForm.miPasteClicked(Sender: TObject);
begin
  memEditor.PasteFromClipboard;
end;

procedure TMainForm.btnGOClick(Sender: TObject);
var
  ftr: TElasticTabstopsDocFilter;
begin
{
  ftr := TElasticTabstopsDocFilter.Create(memEditor);
  try
    try
      ftr.StretchTabstops;
    finally
      ftr.Free;
    end;
  except
    on E: exception do
      ShowMessage(e.Message);
  end;
}
end;

procedure TMainForm.memEditorChanged(Sender: TObject);
var
  ftr: TElasticTabstopsDocFilter;
begin
{
  ftr := TElasticTabstopsDocFilter.Create(memEditor);
  try
    try
      ftr.StretchTabstops;
    finally
      ftr.Free;
    end;
  except
    on E: exception do
      ShowMessage(e.Message);
  end;
}
end;

procedure TMainForm.UpdateStatus(const AMessage: TfpgString);
begin
  lblStatusText.Text := AMessage;
end;

procedure TMainForm.AfterCreate;
begin
  {@VFD_BODY_BEGIN: MainFrom}
  Name := 'MainFrom';
  SetPosition(326, 207, 717, 410);
  WindowTitle := 'fpGUI nanoedit';
  Hint := '';
  WindowPosition := wpScreenCenter;
  OnShow  := @FormShow;

  menu := TfpgMenuBar.Create(self);
  with menu do
  begin
    Name := 'menu';
    SetPosition(0, 0, 717, 24);
    Anchors := [anLeft,anRight,anTop];
  end;

  mnuFile := TfpgPopupMenu.Create(self);
  with mnuFile do
  begin
    Name := 'mnuFile';
    SetPosition(340, 6, 120, 20);
    AddMenuItem('Open...', 'Ctrl+O', @miOpenClick);
    AddMenuItem('Save', 'Ctrl+S', @miSaveClick);
    AddMenuItem('Save as...', 'Ctrl+Shift+S', @miSaveAsClick);
    AddMenuItem('-', '', nil);
    AddMenuItem('Quit', 'Ctrl+Q', @miQuitClick);
  end;

  mnuEdit := TfpgPopupMenu.Create(self);
  with mnuEdit do
  begin
    Name := 'mnuEdit';
    SetPosition(340, 44, 120, 20);
    AddMenuItem('Cut', 'Ctrl+X', @miCutClicked);
    AddMenuItem('Copy', 'Ctrl+C', @miCopyClicked);
    AddMenuItem('Paste', 'Ctrl+V', @miPasteClicked);
  end;

  mnuSearch := TfpgPopupMenu.Create(self);
  with mnuSearch do
  begin
    Name := 'mnuSearch';
    SetPosition(340, 25, 120, 20);
    AddMenuItem('Find...', 'Ctrl+F', @miFindClick);
    AddMenuItem('Find next', 'F3', @miFindNextClick);
    AddMenuItem('Replace', 'Ctrl+R', nil).Enabled := False;
    AddMenuitem('-', '', nil);
    AddMenuItem('Go to line...', 'Ctrl+G', @miGoToLineClick);
  end;

  memEditor := TfpgTextEdit.Create(self);
  with memEditor do
  begin
    Name := 'memEditor';
    SetPosition(0, 52, 717, 332);
    Anchors := [anLeft,anRight,anTop,anBottom];
    FontDesc := '#edit2';
    GutterVisible := True;
    RightEdge := True;
  end;

  btnGO := TfpgButton.Create(self);
  with btnGO do
  begin
    Name := 'btnGO';
    SetPosition(260, 26, 75, 24);
    Text := 'GO';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 4;
    OnClick := @btnGOClick;
  end;

  bevStatusBar := TfpgBevel.Create(self);
  with bevStatusBar do
  begin
    Name := 'bevStatusBar';
    SetPosition(1, 387, 715, 22);
    Anchors := [anLeft,anRight,anBottom];
    Hint := '';
    Style := bsLowered;
  end;

  lblStatusText := TfpgLabel.Create(bevStatusBar);
  with lblStatusText do
  begin
    Name := 'lblStatusText';
    SetPosition(4, 4, 704, 15);
    Anchors := [anLeft,anRight,anTop];
    FontDesc := '#Label1';
    Hint := '';
    Text := '';
  end;

  {@VFD_BODY_END: MainFrom}

  menu.AddMenuItem('&File', nil).SubMenu := mnuFile;
  menu.AddMenuItem('&Edit', nil).SubMenu := mnuEdit;
  menu.AddMenuItem('&Search', nil).SubMenu := mnuSearch;
end;


end.