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
|
{
lazfmlviewer.pas
FML Help Viewer
Copyright (C) 1998 - 2007 Harri Pyy, Chris O'Donnell, Felipe Monteiro de Carvalho
This file is part of Virtual Magnifying Glass.
Virtual Magnifying Glass is free software;
you can redistribute it and/or modify it under the
terms of the GNU General Public License version 2
as published by the Free Software Foundation.
Virtual Magnifying Glass is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
Please note that the General Public License version 2 does not permit
incorporating Virtual Magnifying Glass into proprietary
programs.
AUTHORS: Chris O'Donnell, Felipe Monteiro de Carvalho and Harri Pyy
}
unit lazfmlviewer;
{$IFDEF FPC}
{$MODE DELPHI}{$H+}
{$ENDIF}
interface
uses StrUtils, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls, Buttons, {$ifdef fpc} LCLIntf, LCLType, {$endif}
fmlscan;
type
{ TFMLViewer }
TFMLViewer = class(TCustomControl)
public
bmpOutput: TBitmap;
CurrentPage: string;
CurrentPageIndex: Cardinal;
TopicTokens: array of TFMLTokens;
{ Constructors / Destructors }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
{ Drawing methods }
procedure DrawBackground(ACanvas: TCanvas); virtual;
procedure DrawCurrentPage(ACanvas: TCanvas); virtual;
procedure Paint; override;
procedure UpdateImage;
end;
{ TFMLViewerDialog }
TFMLViewerDialog = class(TForm)
private
procedure DoAddTopic(aTopic: string);
procedure DoAddTokenToTopic(aTopic: Cardinal; AToken: TFMLTokenItem);
procedure DoSetTitle(aTitle: string);
public
{ Components }
fmlViewer: TFMLViewer;
fmlIndex: TTreeView;
currentFileName: string;
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;
procedure LoadFromFile(AFileName: string);
procedure LoadFromTokens(ATokens: TFMLTokens);
{ Event handlers }
procedure HandleIndexSelChanged(ASender: TObject);
procedure HandleRePaint(ASender: TObject);
end;
const
INT_TOPIC_TITLE_MARGIN = 25;
INT_TOPIC_TEXT_MARGIN = 25;
var
vFMLViewerDialog: TFMLViewerDialog;
implementation
{$ifndef fpc}
{$R *.DFM}
{$endif}
{ TFMLViewer }
constructor TFMLViewer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
bmpOutput := TBitmap.Create;
end;
destructor TFMLViewer.Destroy;
begin
bmpOutput.Free;
inherited Destroy;
end;
procedure TFMLViewer.DrawBackground(ACanvas: TCanvas);
begin
ACanvas.Brush.Color := clWhite;
ACanvas.FillRect(Bounds(0, 0, Width, Height));
end;
procedure TFMLViewer.DrawCurrentPage(ACanvas: TCanvas);
var
CurrentToken: Cardinal;
currentPos: TPoint;
Text: string;
TextBox: TRect;
begin
{ First draw the title }
ACanvas.Font.Size := 30;
ACanvas.Font.Underline := True;
ACanvas.TextOut(INT_TOPIC_TITLE_MARGIN,
INT_TOPIC_TITLE_MARGIN * 2 + ACanvas.Font.Height, CurrentPage);
{ Initializes currentPos for writing the contents of the topic }
currentPos.X := INT_TOPIC_TITLE_MARGIN;
currentPos.Y := INT_TOPIC_TITLE_MARGIN * 4 + ACanvas.Font.Height;
{ Now draw the text }
ACanvas.Font.Size := 14;
ACanvas.Font.Underline := False;
for CurrentToken := 0 to Length(TopicTokens[CurrentPageIndex]) - 1 do
begin
case TopicTokens[CurrentPageIndex][CurrentToken].id of
ttText:
begin
Text := TopicTokens[CurrentPageIndex][CurrentToken].Value;
// First calculate the text size then draw it
TextBox := Rect(0, currentPos.Y, Width, High(Integer));
DrawText(ACanvas.Handle, PChar(Text), Length(Text),
TextBox, DT_WORDBREAK or DT_INTERNAL or DT_CALCRECT);
DrawText(ACanvas.Handle, PChar(Text), Length(Text),
TextBox, DT_WORDBREAK or DT_INTERNAL);
currentPos.Y := TextBox.Bottom + INT_TOPIC_TEXT_MARGIN;
end;
end;
end;
end;
procedure TFMLViewer.Paint;
begin
// Copies the buffer bitmap to the canvas
Canvas.Draw(0, 0, bmpOutput);
end;
procedure TFMLViewer.UpdateImage;
begin
bmpOutput.Width := Width;
bmpOutput.Height := Height;
DrawBackground(bmpOutput.Canvas);
DrawCurrentPage(bmpOutput.Canvas);
Invalidate;
end;
{ TFMLViewerDialog }
procedure TFMLViewerDialog.DoAddTopic(aTopic: string);
var
LastTopic: Cardinal;
begin
LastTopic := Length(fmlViewer.TopicTokens);
SetLength(fmlViewer.TopicTokens, LastTopic + 1);
vFMLViewerDialog.fmlIndex.Items.AddChild(nil, aTopic);
end;
procedure TFMLViewerDialog.DoAddTokenToTopic(aTopic: Cardinal;
AToken: TFMLTokenItem);
var
LastToken: Cardinal;
begin
LastToken := Length(fmlViewer.TopicTokens[aTopic]);
SetLength(fmlViewer.TopicTokens[aTopic], LastToken + 1);
fmlViewer.TopicTokens[aTopic][LastToken] := AToken;
end;
procedure TFMLViewerDialog.DoSetTitle(aTitle: string);
begin
vFMLViewerDialog.Caption := '' + aTitle;
end;
constructor TFMLViewerDialog.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
{ Sets initial form properties }
Position := poScreenCenter;
OnWindowStateChange := HandleRePaint;
{ Creates child components }
fmlIndex := TTreeView.Create(Self);
fmlIndex.Parent := Self;
fmlIndex.Align := alLeft;
fmlIndex.Width := 200;
fmlIndex.OnSelectionChanged := HandleIndexSelChanged;
fmlIndex.OnResize := HandleRePaint;
fmlViewer := TFMLViewer.Create(Self);
fmlViewer.Parent := Self;
fmlViewer.Left := fmlIndex.Width;
fmlViewer.Width := Width - fmlIndex.Width;
fmlViewer.Align := alClient;
end;
destructor TFMLViewerDialog.Destroy();
begin
inherited Destroy();
end;
procedure TFMLViewerDialog.LoadFromFile(AFileName: string);
var
AFile: TFileStream;
begin
currentFileName := AFileName;
if not FileExists(AFileName) then
begin
Application.MessageBox(
PChar('File not found: ' + AFileName),
'FML Viewer', MB_OK);
Application.ProcessMessages;
Close;
Exit;
end;
AFile := TFileStream.Create(AFileName, fmOpenRead);
try
FMLScanner.FFileStream := AFile;
FMLScanner.RunLex;
FMLScanner.PrintTokenList;
LoadFromTokens(FMLScanner.Tokens);
finally
AFile.Free;
end;
end;
procedure TFMLViewerDialog.LoadFromTokens(ATokens: TFMLTokens);
var
CurrentToken, State, TopicID: Cardinal;
procedure HandleStructuralToken;
begin
case ATokens[CurrentToken].id of
ttTitle: State := 1;
ttTopic: State := 2;
end;
end;
begin
State := 0;
TopicID := 0;
for CurrentToken := 0 to Length(ATokens) - 1 do
begin
// Looking for structural tokens
if State = 0 then
begin
HandleStructuralToken;
end
// Setting the title of the document
else if State = 1 then
begin
DoSetTitle(ATokens[CurrentToken].Value);
State := 0;
end
// Adding a new topic
else if State = 2 then
begin
DoAddTopic(ATokens[CurrentToken].Value);
State := 10;
end
// Textual tokens inside a topic
else if State = 10 then
begin
if ATokens[CurrentToken].id in Set_Structural_Tokens then
begin
HandleStructuralToken;
Inc(TopicID);
end
else DoAddTokenToTopic(TopicID, ATokens[CurrentToken]);
end;
end;
end;
procedure TFMLViewerDialog.HandleIndexSelChanged(ASender: TObject);
begin
fmlViewer.CurrentPage := fmlIndex.Selected.Text;
fmlViewer.CurrentPageIndex := fmlIndex.Selected.Index;
fmlViewer.UpdateImage;
end;
procedure TFMLViewerDialog.HandleRePaint(ASender: TObject);
begin
fmlViewer.UpdateImage;
end;
end.
|