home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_PAS / TVTOYS.ZIP / FONTDLG.PAS < prev    next >
Pascal/Delphi Source File  |  1994-01-03  |  10KB  |  345 lines

  1. (***************************************************************************
  2.   FontDlg unit
  3.   Font selection dialog
  4.   PJB November 3, 1993, CompuServe mail to INTERNET:d91-pbr@nada.kth.se
  5.   Copyright 1993, All Rights Reserved
  6.   Free source, use at your own risk.
  7.   If modified, please state so if you pass this around.
  8.  
  9. ***************************************************************************)
  10. unit FontDlg;
  11.  
  12. {$I toyCfg}
  13.  
  14. {$B-,O+,X+}
  15.  
  16. interface
  17.  
  18.   uses
  19.     Dos,
  20.     App, Dialogs, Drivers, MsgBox, Objects, StdDlg, Validate, Views,
  21.     toyPrefs, {$I hcFile}
  22.     DblStr, FontFiles, toyUtils, TVVideo, TVUtils, Video;
  23.  
  24.  
  25.   type
  26.     PSelFontDialog = ^TSelFontDialog;
  27.     TSelFontDialog =
  28.       object (TDialog)
  29.         constructor Init;
  30.         procedure HandleEvent(var Event:TEvent); virtual;
  31.       end;
  32.  
  33.  
  34.   procedure ReloadLastFont;
  35.   (* Where do I put this? *)
  36.   procedure ReloadFontAndPalette;
  37.  
  38.   procedure LoadResFont(ResFile:PResourceFile; const FontRes:String);
  39.   procedure LoadDiskFont(const FileName:String);
  40.  
  41.   procedure ScanFontFiles(const Path:String; Proc:ScanProcedure);
  42.   function  SelectFontDialog(const FontPath:String; ResFile:PResourceFile):Boolean;
  43.   function  SelectFont(List:PDblStringCollection; var Name:String):Boolean;
  44.  
  45.   var
  46.     (* Last disk font loaded or font resource key used *)
  47.     LastFontNameLoaded : PathStr;
  48.     (* Last resource file used, must be open *)
  49.     LastFontResourceFile : PResourceFile;
  50.  
  51.  
  52. (***************************************************************************
  53. ***************************************************************************)
  54. implementation
  55.  
  56.   uses
  57.     TVPal;
  58.  
  59.  
  60.   (*******************************************************************
  61.     Reloads both the palette and the last font
  62.   *******************************************************************)
  63.   procedure ReloadFontAndPalette;
  64.   begin
  65.     ReloadLastFont;
  66.     ReloadPalette;
  67.   end;
  68.  
  69.  
  70. (***************************************************************************
  71. ***************************************************************************)
  72.  
  73.   (*******************************************************************
  74.     Load a disk font
  75.   *******************************************************************)
  76.   procedure LoadDiskFont(const FileName:String);
  77.     var
  78.       Font : TFontFile;
  79.   begin
  80.     if Font.Read(FileName) then
  81.     begin
  82.       Font.Display;
  83.       LastFontNameLoaded:=FExpand(FileName);
  84.       LastFontTypeUsed:=lfDiskFont;
  85.     end;
  86.   end;
  87.  
  88.  
  89.   (*******************************************************************
  90.     Load a font from a resource file
  91.   *******************************************************************)
  92.   procedure LoadResFont(ResFile:PResourceFile; const FontRes:String);
  93.     var
  94.       P : PFontFile;
  95.   begin
  96.     P:=PFontFile(ResFile^.Get(FontRes));
  97.     if P<>Nil then
  98.     begin
  99.       P^.Display;
  100.       Dispose(P, Done);
  101.     end;
  102.  
  103.     LastFontNameLoaded:=FontRes;
  104.     LastFontResourceFile:=ResFile;
  105.     LastFontTypeUsed:=lfResourceFont;
  106.   end;
  107.  
  108.  
  109.   (*******************************************************************
  110.     Reload last font loaded from disk
  111.   *******************************************************************)
  112.   procedure ReloadLastDiskFont;
  113.     var
  114.       Font : TFontFile;
  115.   begin
  116.     if LastFontNameLoaded<>'' then
  117.       if Font.DoRead(LastFontNameLoaded) then
  118.         Font.Display;
  119.   end;
  120.  
  121.  
  122.   (*******************************************************************
  123.     Reload last font from its source
  124.   *******************************************************************)
  125.   procedure ReloadLastFont;
  126.   begin
  127.    {$IFNDEF DiskFonts}
  128.     {$IFNDEF ResFonts}
  129.      {$DEFINE NoFonts}
  130.     {$ENDIF}
  131.    {$ENDIF}
  132.  
  133.    {$IFNDEF NoFonts}
  134.     case TVVideo.LastFontTypeUsed of
  135.      {$IFDEF DiskFonts}
  136.       lfDiskFont: ReloadLastDiskFont;
  137.      {$ENDIF}
  138.      {$IFDEF ResFonts}
  139.       lfResourceFont: LoadResFont(LastFontResourceFile, LastFontNameLoaded);
  140.      {$ENDIF}
  141.     end;
  142.    {$ENDIF}
  143.   end;
  144.  
  145.  
  146. (***************************************************************************
  147. ***************************************************************************)
  148.  
  149.   (*******************************************************************
  150.     Look for font files in a directory
  151.   *******************************************************************)
  152.   procedure ScanFontFiles;
  153.     var
  154.       f : TFontFile;
  155.   begin
  156.     Notice('', ^M^M^C'Searching for font files...');
  157.     f.DiskScan(Path, Proc);
  158.     NoNotice;
  159.   end;
  160.  
  161.  
  162. (***************************************************************************
  163. ***************************************************************************)
  164.  
  165.   (*******************************************************************
  166.     Here we store the font files found
  167.   *******************************************************************)
  168.   var
  169.     FontList  : PDblStringCollection;
  170.  
  171.   (*******************************************************************
  172.     Called by ScanFontFiles
  173.   *******************************************************************)
  174.   procedure SelectFiles(Points:Integer; const Desc, FileName:String); far;
  175.   begin
  176.     if (VideoType=VGA) or (Points<=14) then
  177.       FontList^.Insert(NewDoubleStr(Desc, FileName));
  178.   end;
  179.  
  180.  
  181.   (*******************************************************************
  182.     Let user select a font
  183.     Define DiskFonts to search for disk fonts
  184.     Define ResFonts to search in the resource file parameter
  185.     You can define both to search in both...
  186.  
  187.     The resource file must contain a StringCollection resource saved
  188.     under the key FONTLIST (see TOYPREFS) with the keys to the
  189.     TFontFiles available in the stream. RESTEST contains an example.
  190.   *******************************************************************)
  191.   function SelectFontDialog(const FontPath:String; ResFile:PResourceFile):Boolean;
  192.     var
  193.       FontChosen : String;
  194.       ResFonts   : PStringCollection;
  195.  
  196.     procedure AddFont(const FontRes:PString); far;
  197.       var
  198.         P : PFontFile;
  199.     begin
  200.       P:=PFontFile(ResFile^.Get(FontRes^));
  201.       if P<>Nil then
  202.       begin
  203.         FontList^.Insert(NewDoubleStr(P^.Desc, FontRes^));
  204.         Dispose(P, Done);
  205.       end;
  206.     end;
  207.  
  208.     procedure Load;
  209.     begin
  210.       LoadDiskFont(AddBackslash(FontPath)+FontChosen);
  211.     end;
  212.  
  213.   begin
  214.     SelectFontDialog:=False;
  215.     New(FontList, Init(20, 10));
  216.  
  217.    {$IFDEF DiskFonts}
  218.     ScanFontFiles(FontPath, SelectFiles);
  219.    {$ENDIF}
  220.  
  221.    {$IFDEF ResFonts}
  222.     if ResFile<>Nil then
  223.     begin
  224.       ResFonts:=PStringCollection(ResFile^.Get(toyFontListKey));
  225.       ResFonts^.ForEach(@AddFont);
  226.       Dispose(ResFonts, Done);
  227.     end;
  228.    {$ENDIF}
  229.  
  230.     if FontList^.Count=0 then
  231.       MessageBox(^C'No font files found!', Nil, mfError+mfOKButton)
  232.     else
  233.       if SelectFont(FontList, FontChosen) then
  234.       begin
  235.        {$IFDEF DiskFonts}
  236.          {$IFDEF ResFonts}
  237.           if (Length(FontChosen)>3) and
  238.              MemComp(FontChosen[Length(FontChosen)-3],
  239.                      toyFontExt[1], Length(toyFontExt)) then
  240.             Load
  241.           else
  242.          {$ELSE}
  243.           Load;
  244.          {$ENDIF}
  245.        {$ENDIF}
  246.        {$IFDEF ResFonts}
  247.         LoadResFont(ResFile, FontChosen);
  248.        {$ENDIF}
  249.  
  250.         SelectFontDialog:=True;
  251.       end;
  252.  
  253.     Dispose(FontList, Done);
  254.   end;
  255.  
  256.  
  257. (***************************************************************************
  258. ***************************************************************************)
  259.  
  260.   (*******************************************************************
  261.     This code generated by Dialog Design 4.0
  262.   *******************************************************************)
  263.   constructor TSelFontDialog.Init;
  264.     var
  265.       R : TRect;
  266.       Control : PView;
  267.   begin
  268.     R.Assign(15, 2, 64, 21);
  269.     inherited Init(R, 'Select a Font');
  270.     Options := Options or ofCentered;
  271.  
  272.     R.Assign(44, 3, 45, 15);
  273.     Control := New(PScrollBar, Init(R));
  274.     Insert(Control);
  275.  
  276.     R.Assign(4, 3, 44, 15);
  277.     Control := New(PSortedListBox, Init(R, 1, PScrollbar(Control)));
  278.     Control^.HelpCtx := hctoyFontListbox;
  279.     Insert(Control);
  280.  
  281.     R.Assign(3, 2, 8, 3);
  282.     Insert(New(PLabel, Init(R, '~F~onts', Control)));
  283.  
  284.     R.Assign(7, 16, 17, 18);
  285.     Control := New(PButton, Init(R, 'O~K~', cmOK, bfDefault));
  286.     Control^.HelpCtx := hcOK;
  287.     Insert(Control);
  288.  
  289.     R.Assign(19, 16, 29, 18);
  290.     Control := New(PButton, Init(R, 'Cancel', cmCancel, bfLeftJust));
  291.     Control^.HelpCtx := hcCancel;
  292.     Insert(Control);
  293.  
  294.     R.Assign(31, 16, 41, 18);
  295.     Control := New(PButton, Init(R, 'Help', cmHelp, bfNormal));
  296.     Control^.HelpCtx := hctoyFontDialogHelp;
  297.     Insert(Control);
  298.  
  299.     SelectNext(False);
  300.   end;
  301.  
  302.  
  303.   (*******************************************************************
  304.     Double click in list box acts like Enter key
  305.   *******************************************************************)
  306.   procedure TSelFontDialog.HandleEvent;
  307.   begin
  308.     inherited HandleEvent(Event);
  309.     if (Event.What=evBroadcast) and (Event.Command=cmListItemSelected) then
  310.       EndModal(cmOK);
  311.   end;
  312.  
  313.  
  314. (***************************************************************************
  315. ***************************************************************************)
  316.  
  317.   var
  318.     ListRec :
  319.       record
  320.         List      : PDblStringCollection;
  321.         Selection : Word;
  322.       end;
  323.  
  324.   (*******************************************************************
  325.     Execute font selection dialog
  326.   *******************************************************************)
  327.   function SelectFont(List:PDblStringCollection; var Name:String):Boolean;
  328.   begin
  329.     SelectFont:=False;
  330.     ListRec.List:=List;
  331.     if Application^.ExecuteDialog(New(PSelFontDialog, Init), @ListRec)<>cmCancel then
  332.     begin
  333.       Name:=PString(ListRec.List^.At2nd(ListRec.Selection))^;
  334.       SelectFont:=True;
  335.     end;
  336.   end;
  337.  
  338.  
  339.     (*******************************************************************
  340.     *******************************************************************)
  341.  
  342. end.
  343.  
  344.  
  345.