home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOKAN 17
/
DOKAN17.iso
/
Progs
/
Pjv03dde.zip
/
PJV03DDE
/
SRCCODE
/
SAMPLE3
/
JVIEWU.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1999-08-29
|
23KB
|
806 lines
unit jviewu;
// Pulsar jViewer Source Code (c)1999 Pulsar Studio, & Lord Trancos.
// Please distribute the source code without any modification.
// FREEWARE
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, Printers, Nihongo, StdCtrls;
type
TForm1 = class(TForm)
OpenDlg: TOpenDialog;
PrtDlg: TPrintDialog;
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1 : TForm1;
ChrSiz : word; // Charcter width n height
ChrX : longint; // Left OffSet for Scroll L/R
LinScr : word; // Lines in Screen
ChrScr : word; // Max Char per Line
lAdjust : boolean = true; // Use Line break
PrtChrScr : word; // Max Chr per Line (Printer)
// File vars
FFormat : byte; // File format = 0 -> sjs / 1 -> euc / 2 ->jis
JISON : boolean = false;
FName : string; // Filename
FSize : longint; // File size
FData : pointer; // File in memory
FLoaded : boolean; // File loaded ?
FLin : longint; // First line drawed
FLines : longint; // Used to limit movement
FPos : longint; // File position
FPages : longint; // File Pages
const
EOFDOS = $D; // Do not change
EOFWIN = $A; // Do not change
MINCHR = 20; // Min. Number of Chars. per Line Printed
MAXCHR = 140; // Max. Number of Chars. per Line Printed
EUCEXT = '.EUC'; // default EUC extension
SJSEXT = '.SJS'; // default SJS extension
JISEXT = '.JIS'; // default JIS extension
USEEUC = '-E'; // 2nd parameter, EUC (only required if file ext <> EUCEXT)
USESJS = '-S'; // 2nd parameter, SJS (only required if file ext <> SJSEXT)
USEJIS = '-J'; // 2nd parameter, JIS (only required if file ext <> JISEXT)
ABOUT = 'Pulsar jViewer v0.3d - SJS/EUC/JIS Viewer'+#10+
'(c)1999 Pulsar Studio'+#10+
'Developed by Lord Trancos'+#10+
''+#10+'Keys;'+#10+
'[ESC] - Exit'+#10+
'[F1] - About/Help'+#10+
'[F2] - Open File'+#10+
'[F5] - Enable/Disable Line Break'+#10+
'[F8] - Minimize Window'+#10+
'[F9] - Change Max. Number of Chars. per Line Printed'+#10+
'[F10] - Print File'+#10+
'[UP][DOWN] - Scroll Up/Down'+#10+
'[LEFT][RIGHT] - Scroll Left/Right'+#10+
'[PGUP] - Page Up'+#10+
'[PGDOWN] - Page Down'+#10+
'[SPACEBAR] - Redraw Text'+#10+
{ '[BACKSPACE] - Change Between EUC/SJS'+#10+}#10+
'Greetings to:'+#10+
'Dark Shadow, R⌠nin, Multidimensional Careto,'+#10+
'Takuya, Kanjiman, FidoNet r34.Japones, FIC BBS,'+#10+
'DOKAN and ONELIST.COM MLJ (Mail List Japones)'+#10+#10+
'This program is FREEWARE' + #10+
'Full Source Code is Available for FREE!';
TXT00 = 'Can not open file: ';
TXT01 = 'Can not load file: ';
TXT02 = 'Error while initializing Nihongo Engine!'+#10+
'(Remerber: '+jJAPFNT+' and '+jASCFNT+
' must be in program''s folder!)';
TXT03 = '>> LOADING';
TXT04 = 'File empty: ';
TXT05 = ' PULSAR jVIEWER - ';
TXT06 = 'File format not specified.';
TXT07 = ' - page: ';
TXT08 = ' - line: ';
TXT09 = 'Top/Left: (';
TXT10 = ') - Pos: ';
TXT11 = 'Printing... ';
TXT12 = 'Counting... ';
TXT13 = '%';
TXT15 = 'Lines: ';
TXT16 = 'Skiping ';
TXT17 = 'Printing ';
TXT18 = 'Change number of characters per line';
TXT19 = 'How many (range: 20-140)?';
TXT20 = 'Out of range!';
NHG00 = #$93 + #$FA + #$96 + #$7B + #$8C + #$EA;
implementation
uses jViewu2;
{$R *.DFM}
// -----------------------------------------------------------------------
procedure ErrH(_err: string);
begin
MessageDlg(_err, mtError, [mbOk], 0);
Halt;
end;
// -----------------------------------------------------------------------
procedure IncPtrPox(var _ptr: pointer; var _pos: longint; _val: longint);
begin
inc(longint(_ptr), _val);
inc(_pos, _val);
end;
// -----------------------------------------------------------------------
procedure DecPtrPox(var _ptr: pointer; var _pos: longint; _val: longint);
begin
dec(longint(_ptr), _val);
dec(_pos, _val);
end;
// -----------------------------------------------------------------------
procedure CheckJIS(var _ptr: pointer; var _pos: longint);
begin
if byte(_ptr^) = $1B then
begin
if JISON = true
then JISON := false
else JISON := true;
IncPtrPox(_ptr, _pos, 3);
end;
end;
// -----------------------------------------------------------------------
function GetCurrentLine(var _ptr: pointer; var _pos: longint;
_chrscr: word): string;
var _s: string;
_l: longint;
_b: byte;
_z: word;
begin
_l := 0;
_s := '';
if lAdjust = false then // Whitout Line break
begin
while (byte(_ptr^) <> EOFDOS) and (byte(_ptr^) <> EOFWIN)
and (_pos < FSize) do
begin
inc(_l);
if FFormat = 2 then CheckJIS(_ptr, _pos);
if (byte(_ptr^) <> EOFDOS) and (byte(_ptr^) <> EOFWIN)
and (_l >= ChrX) and (length(_s) <= _chrScr) then
if JISON = false then _s := _s + char(_ptr^)
else _s := _s + char(byte(_ptr^)+128);
IncPtrPox(_ptr, _pos, 1);
end;
end else begin // Whit Line break
while (byte(_ptr^) <> EOFDOS) and (byte(_ptr^) <> EOFWIN)
and (_pos < FSize) and (length(_s) <= _chrScr) do
begin
inc(_l);
if FFormat = 2 then CheckJIS(_ptr, _pos);
if (_l >= ChrX) and (byte(_ptr^) <> EOFDOS)
and (byte(_ptr^) <> EOFWIN)then
if JISON = false then _s := _s + char(_ptr^)
else _s := _s + char(byte(_ptr^)+128);
IncPtrPox(_ptr, _pos, 1);
end;
if (length(_s) = _chrScr + 1)
then DecPtrPox(_ptr, _pos, 1);
if (length(_s) = _chrScr + 1) then
begin
_z := length(_s);
_s := jBreakSJS(_s, _chrScr);
if length(_s) < _z
then DecPtrPox(_ptr, _pos, _z - length(_s) - 1);
end;
if char(_ptr^) = ' ' then IncPtrPox(_ptr, _pos, 1);
end;
case byte(_ptr^) of
EOFDOS : _b := 2;
EOFWIN : _b := 1;
else _b := 0;
end;
if _pos = FSize
then FLines := FLin else IncPtrPox(_ptr, _pos, _b);
GetCurrentLine := _s;
end;
// -----------------------------------------------------------------------
procedure DrawText(_zeta: byte);
var _line : byte;
_s : string;
_point : pointer;
_pos : longint;
_y : longint;
_perc : longint;
begin
_perc := (FPos * 100) div FSize;
_s := TXT05 + FName + ' - ' + TXT09 + IntToStr(FLin) + '/' + IntToStr(ChrX)
+ TXT10 + IntToStr(FPos) + ' (' + IntToStr(_perc) + '%) ';
if lAdjust = true then _s := _s + 'B ';
jWriteSJS(0, 0, ChrSiz, ChrSiz, _s, false, true, Form1.Canvas.handle);
_line := 1;
_pos := FPos;
_point := FData;
inc(longint(_point), _pos);
repeat
_s := GetCurrentLine(_point, _pos, ChrScr);
if (_zeta = 0) or // Draw all
((_zeta = 1) and (_line = 1)) or // Draw only first line
((_zeta = 2) and (_line = LinScr - 1)) // Draw only last line
then begin
_y := _line * ChrSiz;
if FFormat = 0
then jWriteSJS(0, _y, ChrSiz, ChrSiz, _s,
false, false, Form1.Canvas.handle)
else jWriteEUC(0, _y, ChrSiz, ChrSiz, _s,
false, false, Form1.Canvas.handle);
end;
inc(_line);
until _line = LinScr;
end;
// -----------------------------------------------------------------------
procedure LineBack(var _ptr: pointer; var _pos: longint);
begin
if (FPos = 0) or (Flin = 1) then exit;
if (FPos > 2) then
begin
dec(longint(_ptr), 2);
dec(_pos, 2);
end else begin FPos := 0; _ptr := FData; exit; end;
while (byte(_ptr^) <> EOFWIN) and (FPos > 0) do
begin
dec(longint(_ptr));
dec(_pos);
end;
if FPos <> 0 then
begin
inc(longint(_ptr));
inc(_pos);
end;
end;
// -----------------------------------------------------------------------
procedure LineBack2(var _ptr: pointer; var _pos: longint);
var _old : longint;
_z, _c : longint;
begin
_z := 0;
_old := _pos;
LineBack(_ptr, _pos);
while _pos <> _old do
begin
GetCurrentLine(_ptr, _pos, ChrScr);
inc(_z);
end;
LineBack(_ptr, _pos);
if _z > 1 then for _c := 1 to _z - 1 do GetCurrentLine(_ptr, _pos, ChrScr);
end;
// -----------------------------------------------------------------------
function LineDown: boolean;
var _p: pointer;
_x: longint;
begin
LineDown := false;
if Flines <> 0 then if FLin >= FLines then exit;
_x := FPos;
_p := FData;
inc(longint(_p), FPos);
GetCurrentLine(_p, FPos, ChrScr);
if FPos > _x then inc(FLin);
LineDown := true;
end;
// -----------------------------------------------------------------------
function LineUp: boolean;
var _p: pointer;
_x: longint;
begin
LineUp := false;
if FLin = 1 then exit;
_x := FPos;
_p := FData;
inc(longint(_p), FPos);
if lAdjust = false
then LineBack(_p, FPos)
else LineBack2(_p, FPos);
if FPos < _x then dec(FLin);
LineUp := true;
end;
// -----------------------------------------------------------------------
procedure SoftScroll(_down: boolean);
var _pos: integer;
begin
if _down = true
then BitBlt(Form1.Canvas.Handle, 0, ChrSiz, Screen.Width,
(LinScr - 1) * ChrSiz, Form1.Canvas.Handle, 0,
ChrSiz shl 1, SRCCOPY);
if _down = false then
for _pos := LinScr - 2 downto 1 do
if BitBlt(Form1.Canvas.Handle, 0, (_pos + 1) * ChrSiz, Screen.Width,
ChrSiz, Form1.Canvas.Handle, 0, _pos * ChrSiz,
SRCCOPY) = false then exit;
end;
// -----------------------------------------------------------------------
function IdentifyFileType(_fn: string): boolean;
var _ex : string[4];
_r : boolean;
begin
_r := true;
_ex := UpperCase(ExtractFileExt(_fn));
if _ex = SJSEXT then FFormat := 0 else
if _ex = EUCEXT then FFormat := 1 else
if _ex = JISEXT then FFormat := 2 else _r := false;
IdentifyFileType := _r;
end;
// -----------------------------------------------------------------------
procedure ChkFileFormat;
begin
case Form1.OpenDlg.FilterIndex of
1: if IdentifyFileType(Form1.OpenDlg.Filename) = false
then ErrH(TXT06);
2: FFormat := 0;
3: FFormat := 1;
4: FFormat := 2;
end;
end;
// -----------------------------------------------------------------------
function UseOpenDialog: string;
begin
if Form1.OpenDlg.Execute = false then Halt;
ChkFileFormat;
UseOpenDialog := Form1.OpenDlg.Filename;
end;
// -----------------------------------------------------------------------
function GetFileSize(_fn: string): longint;
var _r: longint;
_f: file;
begin
if jOpenBinFile(_fn, _f) = false then ErrH(TXT00 + _fn);
_r := filesize(_f);
CloseFile(_f);
GetFileSize := _r;
end;
// -----------------------------------------------------------------------
procedure InitVars1;
begin
FPos := 0;
FLin := 1;
ChrX := 1;
FPages := 0;
FLines := 0;
end;
// -----------------------------------------------------------------------
procedure TForm1.FormCreate(Sender: TObject);
begin
// Init vars
InitVars1;
FLoaded := false;
// check char size n lines in screen
if Screen.Width < 1280 then ChrSiz := 16 else ChrSiz := 32;
ChrScr := Screen.Width div (ChrSiz shr 1);
LinScr := Screen.Height div ChrSiz;
PrtChrScr := ChrScr;
// init n display form
Form1.BorderStyle := bsNone;
Form1.Color := clBlack;
Form1.Canvas.Brush.Color := clWhite;
Form1.Top := (Screen.Height shr 1) - 30;
Form1.Left := (Screen.Width shr 1) - 50;
Form1.Height := 60;
Form1.Width := 100;
ShowWindow(Form1.handle, SW_SHOW);
// init nihongo engine
if jInitialize = false then ErrH(TXT02 + #10 + paramstr(0));
// draw 'Loading...' banner
jWriteSJS(0, 4, 32, 32, NHG00, false, false, Form1.Canvas.handle);
jWriteSJS(8, 40, 16, 16, TXT03, false, false, Form1.Canvas.handle);
// Choose file n file format
FFormat := 0;
if paramcount = 0 then
begin
FName := UseOpenDialog
end else begin
FName := paramstr(1);
if IdentifyFileType(FName) = false then
if (paramcount > 1) then
begin
if UpperCase(paramstr(2)) = USESJS
then FFormat := 0
else if UpperCase(paramstr(2)) = USEEUC
then FFormat := 1
else if UpperCase(paramstr(2)) = USEJIS
then FFormat := 2
else FName := UseOpenDialog;
end else FName := UseOpenDialog;
end;
// draw 'Loading...' banner (yes, another time)
jWriteSJS(0, 4, 32, 32, NHG00, false, false, Form1.Canvas.handle);
jWriteSJS(8, 40, 16, 16, TXT03, false, false, Form1.Canvas.handle);
// check file
FSize := GetFileSize(FName);
if FSize = 0 then ErrH(TXT04 + FName);
// Load file in memory
GetMem(FData, FSize);
if jLoadFile(FName, FData) = false then ErrH(TXT01 + FName);
FLoaded := true;
// Get only the file name
Fname := ExtractFileName(FName);
// Init form
Form1.Top := 0;
Form1.Left := 0;
Form1.Width := Screen.Width;
Form1.Height := Screen.Height;
Form1.rePaint;
// Form1.FormStyle := fsStayOnTop;
end;
// -----------------------------------------------------------------------
procedure CountPages(_chrscr: word);
var _p : pointer;
_l : longint;
_lin : longint;
_chrsiz : word;
_pagelin : word;
begin
_l := 0;
_p := FData;
_lin := 0;
bAbort := false;
Form1.Enabled := false;
Form2.Caption := TXT12;
Form2.Visible := true;
while (_l < FSize) and (bAbort = false) do
begin
if _lin and $08 = $08 then Application.ProcessMessages;
Form2.Caption := TXT12 + IntToStr((_l * 100) div FSize) + TXT13;
Form2.Label1.Caption := TXT15 + IntToStr(_lin);
GetCurrentLine(_p, _l, _chrScr);
inc(_lin);
end;
if bAbort = false then
begin
_chrsiz := Printer.PageWidth div (PRTCHRSCR shr 1); {(Printer.PageWidth * 16) div 640;}
_pagelin := Printer.Pageheight div _chrsiz;
FPages := _lin div _pagelin;
if _lin mod _pagelin <> 0 then inc(FPages);
end;
Form2.Visible := false;
Form1.Enabled := true;
Form1.SetFocus;
end;
// -----------------------------------------------------------------------
procedure PrtTxt(_page, _line: longint; _plus: string);
begin
Form2.Label1.Caption := _plus + TXT07 + IntToStr(_page) + TXT08
+ IntToStr(_line);
end;
// -----------------------------------------------------------------------
procedure PrintIt;
var _pos : longint;
_line : longint;
_page : longint;
_point : pointer;
_s : string;
_chrsiz : word;
_pagelin : word;
_xyz : longint;
_from : longint;
_to : longint;
begin
// Count pages if required
if FPages = 0 then CountPages(PRTCHRSCR);
if FPages = 0 then exit;
// Prt Dialog
Form1.PrtDlg.MinPage := 1;
Form1.PrtDlg.MaxPage := FPages;
Form1.PrtDlg.FromPage := 1;
Form1.PrtDlg.ToPage := FPages;
if Form1.PrtDlg.Execute = false then exit;
// Set _copies, _from & _to
if Form1.PrtDlg.PrintRange = prPageNums then
begin
_from := Form1.PrtDlg.FromPage;
_to := Form1.PrtDlg.ToPage;
end else begin
_from := 1;
_to := FPages;
end;
// Reset Left Pos (ir required)
if ChrX <> 1 then
begin
ChrX := 1;
Form1.RePaint;
end;
// Init some values
bAbort := false;
_pos := 0;
_point := FData;
_page := 1;
_chrsiz := Printer.PageWidth div (PRTCHRSCR shr 1);
_pagelin := Printer.Pageheight div _chrsiz;
Form1.Enabled := false;
Form2.Caption := TXT11;
Form2.Visible := true;
// Print
Printer.BeginDoc;
Printer.Canvas.Pen.Color := clBlack;
Printer.Canvas.Brush.Color := clWhite;
repeat
_line := 1;
repeat // Print current page
Application.ProcessMessages;
if _page < _from
then PrtTxt(_page, _line, TXT16)
else PrtTxt(_page, _line, TXT17);
_xyz := _pos;
_s := GetCurrentLine(_point, _pos, PRTCHRSCR);
if _page >= _from then
if FFormat = 0
then jWriteSJS(0{_chrsiz shr 1}, _line * _chrSiz, _chrSiz,
_chrSiz, _s, false, true, Printer.Canvas.handle)
else jWriteEUC(0{_chrsiz shr 1}, _line * _chrSiz, _chrSiz,
_chrSiz, _s, false, true, Printer.Canvas.handle);
inc(_line);
until (_line = _pagelin) or (_pos = _xyz) or (bAbort = true);
if (_page >= _from) and (_page < _to) then Printer.NewPage;
inc(_page);
until (_pos = _xyz) or (bAbort = true) or (_page > _to);
if bAbort = true then Printer.Abort else Printer.EndDoc;
// Done
Form2.Visible := false;
Form1.Enabled := true;
Form1.SetFocus;
end;
// -----------------------------------------------------------------------
procedure UnloadCurrentFile;
begin
if FLoaded = true then FreeMem(FData, FSize);
end;
// -----------------------------------------------------------------------
procedure OpenAnotherFile;
var _sz : longint;
begin
if Form1.OpenDlg.Execute = false then exit;
_sz := GetFileSize(Form1.OpenDlg.Filename);
if _sz = 0 then
begin
ShowMessage(TXT04 + Form1.OpenDlg.Filename);
exit;
end;
ChkFileFormat;
UnloadCurrentFile;
InitVars1;
FLoaded := false;
FName := Form1.OpenDlg.Filename;
FSize := _sz;
// Load file in memory
GetMem(FData, FSize);
if jLoadFile(FName, FData) = false then ErrH(TXT01 + FName);
FLoaded := true;
// Get only the file name
Fname := ExtractFileName(FName);
// Redraw form
Form1.Repaint;
end;
// -----------------------------------------------------------------------
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var _cnt : word;
_xyz : longint;
_z : longint;
begin
case key of
27 : Application.Terminate; // Esc
112: ShowMessage(ABOUT); // F1
113: OpenAnotherFile; // F2
end;
if FLoaded = true then
case key of
32 : begin
ChrX := 1;
Form1.repaint; // SpaceBar
end;
{ 8 : begin // BackSpace
if FFormat < 2 then inc(FFormat) else FFormat := 0;
Form1.repaint;
end; }
33 : begin // PgUp
_xyz := Fpos;
for _cnt := 3 to LinScr do LineUp;
if FPos <> _xyz then Form1.repaint else Beep;
end;
34 : begin // PgDown
_xyz := Fpos;
for _cnt := 3 to LinScr do LineDown;
if FPos <> _xyz then Form1.repaint else Beep;
end;
38 : if LineUp = true then // Up
begin
SoftScroll(false);
Form1.Canvas.Brush.Color := clBlack;
Form1.Canvas.Rectangle(0, ChrSiz, Width, ChrSiz shl 1);
Form1.Canvas.Brush.Color := clWhite;
DrawText(1);
end else beep;
40 : if LineDown = true then // Down
begin
SoftScroll(true);
Form1.Canvas.Brush.Color := clBlack;
Form1.Canvas.Rectangle(0, (LinScr - 1) * ChrSiz, Width,
Height);
Form1.Canvas.Brush.Color := clWhite;
DrawText(2);
end else beep;
37 : if (lAdjust = false) and (ChrX > 1) then // left
begin
dec(ChrX, ChrScr shr 1);
Form1.repaint;
end;
39 : if lAdjust = false then
begin // right
inc(ChrX, ChrScr shr 1);
Form1.repaint;
end;
116: begin // F5
if lAdjust = false
then lAdjust := true
else lAdjust := false;
InitVars1;
{ FLin := 1;
FPos := 0;
ChrX := 1;
FLines := 0; }
Form1.repaint; // SpaceBar
end;
120: begin
_z := 0;
_z := StrToInt(InputBox(TXT18, TXT19, IntToStr(PrtChrScr)));
if (_z < MINCHR) or (_z > MAXCHR)
then ShowMessage(TXT20)
else if PrtChrScr <> _z then
begin
FPages := 0;
PrtChrScr := _z;
end;
end;
119: ShowWindow(Form1.handle, SW_MINIMIZE);
121: PrintIt; // F10
{else ShowMessage(IntToStr(key)); }
end;
end;
// -----------------------------------------------------------------------
procedure TForm1.FormPaint(Sender: TObject);
begin
if FLoaded = true then DrawText(0);
end;
// -----------------------------------------------------------------------
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UnloadCurrentFile;
jClose;
end;
// -----------------------------------------------------------------------
end.