Check-in [6357bf12d8]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Horizontal scrolling position now also remembered and restored. Silently ignore exceptions while deleting temporary files.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | v1.2
Files: files | file ages | folders
SHA1:6357bf12d836e8c3980c0234094634e834d11818
User & Date: Martijn 2013-01-22 20:56:30
Context
2013-01-23
14:16
Merged the v1.1.1 bugfix check-in: 9bb5fa830b user: MCO tags: v1.2
2013-01-22
20:56
Horizontal scrolling position now also remembered and restored. Silently ignore exceptions while deleting temporary files. check-in: 6357bf12d8 user: Martijn tags: v1.2
17:14
Implemented a fix for ticket [cfc74baf4b] (remembering the scrolling position in a document). check-in: b835262d4a user: MCO tags: v1.2
Changes

Changes to src/F_PreviewHTML.pas.

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
...
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
...
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
...
342
343
344
345
346
347
348

349
350
351
352
353
354
355
...
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
  IsHTML, IsXML, IsCustom: Boolean;
  Size: WPARAM;
  Filename: nppString;
  Content: UTF8String;
  HTML: string;
  HeadStart: Integer;
  FilterName: string;
  docEl: IHTMLElement2;
begin
  try
    SaveScrollPos;

    SendMessage(Self.Npp.NppData.NppHandle, NPPM_GETCURRENTSCINTILLA, 0, LPARAM(@View));
    if View = 0 then begin
      hScintilla := Self.Npp.NppData.ScintillaMainHandle;
................................................................................
      SetLength(Content, Size);
      SendMessage(hScintilla, SCI_GETTEXT, Size, LPARAM(PAnsiChar(Content)));
      Content := UTF8String(PAnsiChar(Content));
      HTML := string(Content);
    end;

    if IsCustom then begin
      {--- MCO 22-01-2013: execute the custom filter, and assign the output to the HTML variable ---}
      HTML := ExecuteCustomFilter(FilterName, HTML);
      IsHTML := Length(HTML) > 0;
    end else if IsXML then begin
      HTML := TransformXMLToHTML(HTML);
      IsHTML := Length(HTML) > 0;
    end;

................................................................................
    end;
  end;
end {TfrmHTMLPreview.btnRefreshClick};

{ ------------------------------------------------------------------------------------------------ }
procedure TfrmHTMLPreview.SaveScrollPos;
var
  Index, ScrollTop: Integer;

begin
  if FBufferID = -1 then
    Exit;

  if Assigned(wbIE.Document) and Assigned((wbIE.Document as IHTMLDocument3).documentElement) then begin
    ScrollTop := ((wbIE.Document as IHTMLDocument3).documentElement AS IHTMLElement2).scrollTop;


  end else begin
    ScrollTop := -1;

  end;
  Index := FScrollPositions.IndexOfObject(TObject(FBufferID));
  if Index = -1 then
    FScrollPositions.AddObject(IntToStr(ScrollTop), TObject(FBufferID))
  else
    FScrollPositions[Index] := IntToStr(ScrollTop);
  {$MESSAGE HINT 'TODO: We need to watch for closing documents, so we can remove those buffer IDs from our list  MCO 22-01-2013'}
end {TfrmHTMLPreview.SaveScrollPos};
{ ------------------------------------------------------------------------------------------------ }
procedure TfrmHTMLPreview.RestoreScrollPos(const BufferID: NativeInt);
var
  Index, ScrollTop: Integer;

begin
  {--- MCO 22-01-2013: Look up this buffer's scroll position; if we know one, wait for the page
                          to finish loading, then restore the scroll position. ---}
  {$MESSAGE HINT 'TODO: This would be better if done in the browsercontrol's DocumentComplete event  MCO 22-01-2013'}
  Index := FScrollPositions.IndexOfObject(TObject(BufferID));
  if Index > -1 then begin
    ScrollTop := StrToInt(FScrollPositions[Index]);

    if ScrollTop <> -1 then begin


      while not wbIE.ReadyState in [READYSTATE_INTERACTIVE, READYSTATE_COMPLETE] do begin
        Forms.Application.ProcessMessages;
        Sleep(0);
      end;
      if Assigned(wbIE.Document) and Assigned((wbIE.Document as IHTMLDocument3).documentElement) then begin
        ((wbIE.Document as IHTMLDocument3).documentElement as IHTMLElement2).scrollTop := ScrollTop;


      end;
    end;
  end;
  FBufferID := BufferID;
end {TfrmHTMLPreview.RestoreScrollPos};

{ ------------------------------------------------------------------------------------------------ }
function TfrmHTMLPreview.DetermineCustomFilter: string;
var
  DocFileName, ConfigDir: TFileName;
  Filters: TIniFile;
  Names: TStringList;
  i: Integer;
  Match: Boolean;
  Extension, Language, DocLanguage: string;
  DocLangType, LangType: Integer;
begin
  ConfigDir := StringOfChar(#0, MAX_PATH);
  SendMessage(Npp.NppData.NppHandle, NPPM_GETPLUGINSCONFIGDIR, WPARAM(Length(ConfigDir)), LPARAM(PChar(ConfigDir)));
  ConfigDir := string(PChar(ConfigDir));

  DocFileName := StringOfChar(#0, MAX_PATH);
  SendMessage(Npp.NppData.NppHandle, NPPM_GETFILENAME, WPARAM(Length(DocFileName)), LPARAM(PChar(DocFileName)));
  DocFileName := string(PChar(DocFileName));

//ShowMessage(Format('ConfigDir: "%s"; DocFileName: "%s"', [ConfigDir, DocFileName]));

  ForceDirectories(ConfigDir + '\PreviewHTML');
  Filters := TIniFile.Create(ConfigDir + '\PreviewHTML\Filters.ini');
  Names := TStringList.Create;
  try
    Filters.ReadSections(Names);
    for i := 0 to Names.Count - 1 do begin
................................................................................
      end;
    end else begin
      // The document is unmodified, so we can just reference the original file
      InFile := DocFile;
    end;
    try
      {$MESSAGE HINT 'TODO: substitute the temp file name, execute the command with pipe, and read the output  MCO 22-01-2013'}


      {$MESSAGE WARN 'TODO: REPLACE THIS TEMPORARY CODE!!!  MCO 22-01-2013'}
      Command := StringReplace(Command, '%1', '"' + InFile + '"', [rfReplaceAll]);
      OutFile := TPath.GetTempFileName;
      TFile.Delete(OutFile);
//ShowMessage(Format('InFile: "%s"; Command: "%s", OutFile: "%s"', [InFile, Command, OutFile]));
      ShellExecute(Npp.NppData.NppHandle, nil, 'cmd.exe', PChar('/c ' + Command + ' > "' + OutFile + '"'), PChar(ExtractFilePath(OutFile)), SW_HIDE);
................................................................................
      finally
        SS.Free;
      end;
      {--- MCO 22-01-2013: TODO: ways of transferring the HTML to the filter exe: on the input stream; write to file and pass the file name ---}
      {--- MCO 22-01-2013: TODO: ways of transferring the result from the filter exe: read from the output stream, read the changed input file, or read a (separate) output file ---}
    finally
      if InFile <> DocFile then begin
        TFile.Delete(InFile);
      end;
    end;
  finally
    Filters.Free;
  end;
end {TfrmHTMLPreview.ExecuteCustomFilter};








<







 







<







 







|
>





|
>
>


>



|

|





|
>



<


|
>

>
>





|
>
>









|








|
|
>

|
|
<
<







 







>







 







|







99
100
101
102
103
104
105

106
107
108
109
110
111
112
...
130
131
132
133
134
135
136

137
138
139
140
141
142
143
...
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
...
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
...
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
  IsHTML, IsXML, IsCustom: Boolean;
  Size: WPARAM;
  Filename: nppString;
  Content: UTF8String;
  HTML: string;
  HeadStart: Integer;
  FilterName: string;

begin
  try
    SaveScrollPos;

    SendMessage(Self.Npp.NppData.NppHandle, NPPM_GETCURRENTSCINTILLA, 0, LPARAM(@View));
    if View = 0 then begin
      hScintilla := Self.Npp.NppData.ScintillaMainHandle;
................................................................................
      SetLength(Content, Size);
      SendMessage(hScintilla, SCI_GETTEXT, Size, LPARAM(PAnsiChar(Content)));
      Content := UTF8String(PAnsiChar(Content));
      HTML := string(Content);
    end;

    if IsCustom then begin

      HTML := ExecuteCustomFilter(FilterName, HTML);
      IsHTML := Length(HTML) > 0;
    end else if IsXML then begin
      HTML := TransformXMLToHTML(HTML);
      IsHTML := Length(HTML) > 0;
    end;

................................................................................
    end;
  end;
end {TfrmHTMLPreview.btnRefreshClick};

{ ------------------------------------------------------------------------------------------------ }
procedure TfrmHTMLPreview.SaveScrollPos;
var
  Index, ScrollTop, ScrollLeft: Integer;
  docEl: IHTMLElement2;
begin
  if FBufferID = -1 then
    Exit;

  if Assigned(wbIE.Document) and Assigned((wbIE.Document as IHTMLDocument3).documentElement) then begin
    docEl := (wbIE.Document as IHTMLDocument3).documentElement AS IHTMLElement2;
    ScrollTop := docEl.scrollTop;
    ScrollLeft := docEl.scrollLeft;
  end else begin
    ScrollTop := -1;
    ScrollLeft := -1;
  end;
  Index := FScrollPositions.IndexOfObject(TObject(FBufferID));
  if Index = -1 then
    FScrollPositions.AddObject(IntToStr(ScrollTop) + '=' + IntToStr(ScrollLeft), TObject(FBufferID))
  else
    FScrollPositions[Index] := IntToStr(ScrollTop) + '=' + IntToStr(ScrollLeft);
  {$MESSAGE HINT 'TODO: We need to watch for closing documents, so we can remove those buffer IDs from our list  MCO 22-01-2013'}
end {TfrmHTMLPreview.SaveScrollPos};
{ ------------------------------------------------------------------------------------------------ }
procedure TfrmHTMLPreview.RestoreScrollPos(const BufferID: NativeInt);
var
  Index, ScrollTop, ScrollLeft: Integer;
  docEl: IHTMLElement2;
begin
  {--- MCO 22-01-2013: Look up this buffer's scroll position; if we know one, wait for the page
                          to finish loading, then restore the scroll position. ---}

  Index := FScrollPositions.IndexOfObject(TObject(BufferID));
  if Index > -1 then begin
    ScrollTop := StrToInt(FScrollPositions.Names[Index]);
    ScrollLeft := StrToInt(FScrollPositions.ValueFromIndex[Index]);
    if ScrollTop <> -1 then begin
      {$MESSAGE HINT 'TODO: This would be better if done in the browsercontrol's DocumentComplete event,
                            so as to prevent blocking Notepad++  MCO 22-01-2013'}
      while not wbIE.ReadyState in [READYSTATE_INTERACTIVE, READYSTATE_COMPLETE] do begin
        Forms.Application.ProcessMessages;
        Sleep(0);
      end;
      if Assigned(wbIE.Document) and Assigned((wbIE.Document as IHTMLDocument3).documentElement) then begin
        docEl := (wbIE.Document as IHTMLDocument3).documentElement as IHTMLElement2;
        docEl.scrollTop := ScrollTop;
        docEl.scrollLeft := ScrollLeft;
      end;
    end;
  end;
  FBufferID := BufferID;
end {TfrmHTMLPreview.RestoreScrollPos};

{ ------------------------------------------------------------------------------------------------ }
function TfrmHTMLPreview.DetermineCustomFilter: string;
var
  DocFileName, ConfigDir: nppString;
  Filters: TIniFile;
  Names: TStringList;
  i: Integer;
  Match: Boolean;
  Extension, Language, DocLanguage: string;
  DocLangType, LangType: Integer;
begin
  ConfigDir := StringOfChar(#0, MAX_PATH);
  SendMessage(Npp.NppData.NppHandle, NPPM_GETPLUGINSCONFIGDIR, WPARAM(Length(ConfigDir)), LPARAM(nppPChar(ConfigDir)));
  ConfigDir := nppString(nppPChar(ConfigDir));

  DocFileName := StringOfChar(#0, MAX_PATH);
  SendMessage(Npp.NppData.NppHandle, NPPM_GETFILENAME, WPARAM(Length(DocFileName)), LPARAM(nppPChar(DocFileName)));
  DocFileName := nppString(nppPChar(DocFileName));



  ForceDirectories(ConfigDir + '\PreviewHTML');
  Filters := TIniFile.Create(ConfigDir + '\PreviewHTML\Filters.ini');
  Names := TStringList.Create;
  try
    Filters.ReadSections(Names);
    for i := 0 to Names.Count - 1 do begin
................................................................................
      end;
    end else begin
      // The document is unmodified, so we can just reference the original file
      InFile := DocFile;
    end;
    try
      {$MESSAGE HINT 'TODO: substitute the temp file name, execute the command with pipe, and read the output  MCO 22-01-2013'}
      {$MESSAGE HINT 'TODO: perhaps we could wait for the result in a different thread, so as not to block Notepad++  MCO 22-01-2013'}

      {$MESSAGE WARN 'TODO: REPLACE THIS TEMPORARY CODE!!!  MCO 22-01-2013'}
      Command := StringReplace(Command, '%1', '"' + InFile + '"', [rfReplaceAll]);
      OutFile := TPath.GetTempFileName;
      TFile.Delete(OutFile);
//ShowMessage(Format('InFile: "%s"; Command: "%s", OutFile: "%s"', [InFile, Command, OutFile]));
      ShellExecute(Npp.NppData.NppHandle, nil, 'cmd.exe', PChar('/c ' + Command + ' > "' + OutFile + '"'), PChar(ExtractFilePath(OutFile)), SW_HIDE);
................................................................................
      finally
        SS.Free;
      end;
      {--- MCO 22-01-2013: TODO: ways of transferring the HTML to the filter exe: on the input stream; write to file and pass the file name ---}
      {--- MCO 22-01-2013: TODO: ways of transferring the result from the filter exe: read from the output stream, read the changed input file, or read a (separate) output file ---}
    finally
      if InFile <> DocFile then begin
        try TFile.Delete(InFile); except end;
      end;
    end;
  finally
    Filters.Free;
  end;
end {TfrmHTMLPreview.ExecuteCustomFilter};