Subversion Repositories fastphp

Rev

Rev 3 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
unit Unit1;
2
 
3
// TODO: localize
4
 
5
// TODO: wieso geht copy paste im twebbrowser nicht???
6
// Wieso dauert webbrowser1 erste kompilierung so lange???
7
 
8
// Future ideas
9
// - ToDo list
10
// - Open/Save real files
11
// - configurable scraps dir. multiple scraps?
12
// - verschiedene php versionen?
13
// - webbrowser1 nur laden, wenn man den tab anwählt?
14
// - doppelklick auf tab soll diesen schließen
15
 
16
interface
17
 
18
uses
19
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
20
  Dialogs, StdCtrls, OleCtrls, SHDocVw, ComCtrls, ExtCtrls, ToolWin, IniFiles;
21
 
22
type
23
  TForm1 = class(TForm)
24
    PageControl1: TPageControl;
25
    PlaintextTabSheet: TTabSheet;
26
    HtmlTabSheet: TTabSheet;
27
    Memo2: TMemo;
28
    WebBrowser1: TWebBrowser;
29
    Splitter1: TSplitter;
30
    PageControl2: TPageControl;
31
    TabSheet3: TTabSheet;
32
    HelpTabsheet: TTabSheet;
33
    WebBrowser2: TWebBrowser;
34
    Memo1: TMemo;
35
    OpenDialog1: TOpenDialog;
36
    Panel1: TPanel;
37
    OpenDialog2: TOpenDialog;
38
    OpenDialog3: TOpenDialog;
39
    procedure Run(Sender: TObject);
40
    procedure FormShow(Sender: TObject);
41
    procedure Memo1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
42
    procedure FormCreate(Sender: TObject);
43
    procedure FormDestroy(Sender: TObject);
44
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
45
    procedure PageControl2Changing(Sender: TObject; var AllowChange: Boolean);
46
  private
47
    CurSearchTerm: string;
48
    HlpPrevPageIndex: integer;
49
    procedure Help;
50
    procedure ApplicationOnMessage(var Msg: tagMSG; var Handled: Boolean);
51
  protected
52
    FastPHPConfig: TMemIniFile;
53
    ChmIndex: TMemIniFile;
54
    function GetScrapFile: string;
55
  end;
56
 
57
var
58
  Form1: TForm1;
59
 
60
implementation
61
 
62
{$R *.dfm}
63
 
64
uses
65
  Functions;
66
 
67
procedure TForm1.ApplicationOnMessage(var Msg: tagMSG; var Handled: Boolean);
68
begin                    
69
  case Msg.message of
70
    WM_KEYUP:
71
    begin
72
      case Msg.wParam of
73
        VK_ESCAPE:
74
        begin
75
          // It is necessary to use Application.OnMessage, because Form1.KeyPreview does not work when TWebBrowser has the focus
76
          if (HlpPrevPageIndex <> -1) and (PageControl2.ActivePage = HelpTabSheet) and
77
             (HelpTabsheet.TabVisible) then
78
          begin
79
            PageControl2.ActivePageIndex := HlpPrevPageIndex;
80
            HelpTabsheet.TabVisible := false;
81
          end;
82
          Handled := true;
83
        end;
84
      end;
85
    end;
86
  end;
87
end;
88
 
89
procedure TForm1.Run(Sender: TObject);
90
var
91
  phpExe: string;
92
begin
93
  if not FileExists(phpExe) then
94
  begin
95
    phpExe := FastPHPConfig.ReadString('Paths', 'PHPInterpreter', '');
96
    if not FileExists(phpExe) then
97
    begin
98
      if not OpenDialog2.Execute then exit;
99
      if not FileExists(OpenDialog2.FileName) then exit;
100
      phpExe := OpenDialog2.FileName;
101
 
102
      if not IsValidPHPExe(phpExe) then
103
      begin
104
        ShowMessage('This is not a valid PHP executable.');
105
        exit;
106
      end;
107
 
108
      FastPHPConfig.WriteString('Paths', 'PHPInterpreter', phpExe);
109
      FastPHPConfig.UpdateFile;
110
    end;
111
  end;
112
 
113
  memo1.Lines.SaveToFile(GetScrapFile);
114
 
115
  memo2.Lines.Text := GetDosOutput('"'+phpExe+'" "'+GetScrapFile+'"', ExtractFileDir(Application.ExeName));
116
 
117
  BrowseContent(Webbrowser1, memo2.Lines.Text);
118
 
119
  if IsTextHTML(memo2.lines.text) then
120
    PageControl1.ActivePage := HtmlTabSheet
121
  else
122
    PageControl1.ActivePage := PlaintextTabSheet;
123
end;
124
 
125
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
126
begin
127
  Memo1.Lines.SaveToFile(GetScrapFile);
128
end;
129
 
130
procedure TForm1.FormCreate(Sender: TObject);
131
begin
132
  HlpPrevPageIndex := -1;
133
  CurSearchTerm := '';
134
  Application.OnMessage := ApplicationOnMessage;
135
 
136
  FastPHPConfig := TMemIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
137
end;
138
 
139
procedure TForm1.FormDestroy(Sender: TObject);
140
begin
141
  if Assigned(ChmIndex) then
142
  begin
143
    FreeAndNil(ChmIndex);
144
  end;
145
 
146
  FastPHPConfig.UpdateFile;
147
  FreeAndNil(FastPHPConfig);
148
end;
149
 
150
procedure TForm1.FormShow(Sender: TObject);
151
var
152
  ScrapFile: string;
153
begin
154
  ScrapFile := GetScrapFile;
155
  if ScrapFile = '' then
156
  begin
157
    Close;
158
    exit;
159
  end;
160
  Memo1.Lines.LoadFromFile(ScrapFile);
161
 
162
  PageControl1.ActivePage := PlaintextTabSheet;
163
 
164
  PageControl2.ActivePageIndex := 0; // Scraps
165
  HelpTabsheet.TabVisible := false;
166
end;
167
 
168
function TForm1.GetScrapFile: string;
169
begin
170
  result := FastPHPConfig.ReadString('Paths', 'ScrapFile', '');
171
  if not FileExists(result) then
172
  begin
173
    if not OpenDialog3.Execute then
174
    begin
175
      result := '';
176
      exit;
177
    end;
178
 
179
    result := OpenDialog3.FileName;
180
 
181
    if not DirectoryExists(ExtractFilePath(result)) then
182
    begin
183
      ShowMessage('Path does not exist!');
184
      result := '';
185
      exit;
186
    end;
187
 
188
    Memo1.Lines.Clear;
189
    Memo1.Lines.SaveToFile(result);
190
 
191
    FastPHPConfig.WriteString('Paths', 'ScrapFile', result);
192
  end;
193
end;
194
 
195
procedure TForm1.Help;
196
var
197
  IndexFile, chmFile, w, url: string;
198
  internalHtmlFile: string;
199
begin
200
  if not Assigned(ChmIndex) then
201
  begin
202
    IndexFile := FastPHPConfig.ReadString('Paths', 'HelpIndex', '');
203
    IndexFile := ChangeFileExt(IndexFile, '.ini'); // Just to be sure. Maybe someone wrote manually the ".chm" file in there
204
    if FileExists(IndexFile) then
205
    begin
206
      ChmIndex := TMemIniFile.Create(IndexFile);
207
    end;
208
  end;
209
 
210
  if Assigned(ChmIndex) then
211
  begin
212
    IndexFile := FastPHPConfig.ReadString('Paths', 'HelpIndex', '');
213
    // We don't check if IndexFile still exists. It is not important since we have ChmIndex pre-loaded in memory
214
 
215
    chmFile := ChangeFileExt(IndexFile, '.chm');
216
    if not FileExists(chmFile) then
217
    begin
218
      FreeAndNil(ChmIndex);
219
    end;
220
  end;
221
 
222
  if not Assigned(ChmIndex) then
223
  begin
224
    if not OpenDialog1.Execute then exit;
225
 
226
    chmFile := OpenDialog1.FileName;
227
    if not FileExists(chmFile) then exit;
228
 
229
    IndexFile := ChangeFileExt(chmFile, '.ini');
230
 
231
    if not FileExists(IndexFile) then
232
    begin
233
      Panel1.Align := alClient;
234
      Panel1.Visible := true;
235
      Panel1.BringToFront;
236
      Screen.Cursor := crHourGlass;
237
      Application.ProcessMessages;
238
      try
239
        if not ParseCHM(chmFile) then
240
        begin
241
          ShowMessage('The CHM file is not a valid PHP documentation. Cannot use help.');
242
          exit;
243
        end;
244
      finally
245
        Screen.Cursor := crDefault;
246
        Panel1.Visible := false;
247
      end;
248
 
249
      if not FileExists(IndexFile) then
250
      begin
251
        ShowMessage('Unknown error. Cannot use help.');
252
        exit;
253
      end;
254
    end;
255
 
256
    FastPHPConfig.WriteString('Paths', 'HelpIndex', IndexFile);
257
    FastPHPConfig.UpdateFile;
258
 
259
    ChmIndex := TMemIniFile.Create(IndexFile);
260
  end;
261
 
262
  w := GetWordUnderCaret(Memo1);
263
  if w = '' then exit;
264
  if w[1] in ['0'..'9'] then exit;  
265
  w := StringReplace(w, '_', '-', [rfReplaceAll]);
266
  w := LowerCase(w);
267
  CurSearchTerm := w;
268
 
269
  internalHtmlFile := ChmIndex.ReadString('_HelpWords_', CurSearchTerm, '');
270
  if internalHtmlFile = '' then
271
  begin
272
    HelpTabsheet.TabVisible := false;
273
    HlpPrevPageIndex := -1;
274
    ShowMessage('No help for "'+CurSearchTerm+'" available');
275
    Exit;
276
  end;
277
 
278
  url := 'mk:@MSITStore:'+ChmFile+'::'+internalHtmlFile;
279
 
280
  HlpPrevPageIndex := PageControl2.ActivePageIndex; // Return by pressing ESC
281
  HelpTabsheet.TabVisible := true;
282
  PageControl2.ActivePage := HelpTabsheet;
283
  BrowseURL(WebBrowser2, url);
284
end;
285
 
286
procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
287
  Shift: TShiftState);
288
begin
289
  if (Key = VK_F9) or (Key = VK_F5) then
290
    Run(Sender)
291
  else if Key = VK_F1 then
292
    Help;
293
end;
294
 
295
procedure TForm1.PageControl2Changing(Sender: TObject;
296
  var AllowChange: Boolean);
297
begin
298
  if PageControl2.ActivePage = HelpTabsheet then
299
    HlpPrevPageIndex := -1
300
  else
301
    HlpPrevPageIndex := PageControl2.ActivePageIndex;
302
 
303
  AllowChange := true;
304
end;
305
 
306
end.