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

  1. (***************************************************************************
  2.   ModeDialog unit
  3.   A dialog displaying available video modes, supporting routines
  4.   PJB August 30, 1993, CompuServe mail to INTERNET:d91-pbr@nada.kth.se
  5.   Copyright PJB 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.   If you want to omit certain video modes from the list, change the
  10.   AddMode procedure to include a test (e.g. if Columns<80 then Exit...)
  11.  
  12.   Turbo Vision works in 40 columns, but the SelectVideoMode dialog does
  13.   not (it is too wide, selecting Preview will shrink the dialog).
  14.  
  15.   You can overlay this unit and put TSelectVideoModeDialog in a
  16.   resource file. Here is what to do with a resource file:
  17.  
  18.     SetupVideoList;
  19.     SelectVideoMode(PSelectVideoModeDialog(RezFile.Get('VideoModeDialog')));
  20.  
  21.  
  22.   See VIDEOTST.PAS for a demonstration of this unit.
  23.  
  24. ***************************************************************************)
  25. unit ModeDlg;
  26.  
  27. {$I toyCfg}
  28.  
  29. {$B-,O+,X+}
  30.  
  31. interface
  32.  
  33.   uses
  34.     App, Dialogs, Drivers, Objects, Memory, MsgBox, Views,
  35.     toyPrefs, {$I hcFile}
  36.     TVVideo, TVUtils, Video;
  37.  
  38.   type
  39.     PSelectVideoModeDialog = ^TSelectVideoModeDialog;
  40.     TSelectVideoModeDialog =
  41.       object (TDialog)
  42.         VideoListBox : PListBox;
  43.         constructor Init;
  44.         constructor Load(var S:TStream);
  45.         procedure HandleEvent(var Event:TEvent); virtual;
  46.         procedure Store(var S:TStream);
  47.       end;
  48.  
  49.   procedure StoreVideoModes(var S:TStream);
  50.   procedure LoadVideoModes(var S:TStream);
  51.  
  52.   procedure SetupVideoList;
  53.   function  HasToScan:Boolean;
  54.   procedure SelectVideoModeDialog;
  55.   procedure SelectVideoMode(P:PSelectVideoModeDialog);
  56.  
  57.   var
  58.     (* SelectVideoModeDialog GetData/SetData operates on this *)
  59.     VideoModeDataRec :
  60.       record
  61.         VideoListBox : TListboxRec;
  62.       end;
  63.  
  64.     (* The ModeList array contains the actual video modes
  65.        corresponding to the entries in the VideoList listbox *)
  66.     ModeList  : array [0..MaxVideoModes] of Word;
  67.  
  68.  
  69.  {$IFDEF StoreModeData}
  70.   type
  71.     ModeDataRec =
  72.       record
  73.         Columns    : Byte;
  74.         Rows       : Byte;
  75.         CharHeight : Byte;
  76.         Color      : Boolean;
  77.       end;
  78.  
  79.   var
  80.     (* The ModeDataList array contains each video mode's
  81.        width, height and character size for matching purposes *)
  82.     ModeDataList : array [0..MaxVideoModes] of ModeDataRec;
  83.  
  84.   function FindSimilarVideoMode(Columns, Rows:Byte; Color:Boolean):Word;
  85.  {$ENDIF}
  86.  
  87. (***************************************************************************
  88. ***************************************************************************)
  89. implementation
  90.  
  91.  
  92.   var
  93.     (* AddMode adds new lines of video mode information to VideoList *)
  94.     VideoList : PStringCollection;
  95.  
  96.  
  97.   (*******************************************************************
  98.     These routines save mode information on a stream. They are meant
  99.     to be used with an init or configuration file
  100.   *******************************************************************)
  101.   procedure StoreVideoModes;
  102.   begin
  103.     S.Put(VideoList);
  104.     S.Write(ModeList, SizeOf(ModeList));
  105.    {$IFDEF StoreModeData}
  106.     S.Write(ModeDataList, SizeOf(ModeDataList));
  107.    {$ENDIF}
  108.   end;
  109.  
  110.   procedure LoadVideoModes;
  111.   begin
  112.     VideoList:=PStringCollection(S.Get);
  113.     S.Read(ModeList, SizeOf(ModeList));
  114.    {$IFDEF StoreModeData}
  115.     S.Read(ModeDataList, SizeOf(ModeDataList));
  116.    {$ENDIF}
  117.   end;
  118.  
  119.  
  120.  {$IFDEF StoreModeData}
  121.   (*******************************************************************
  122.     Simple example of how to find a reasonably similar video mode
  123.     Tries to weigh Width and Height differently.
  124.   *******************************************************************)
  125.   function FindSimilarVideoMode(Columns, Rows:Byte; Color:Boolean):Word;
  126.     var
  127.       Diff    : Word;
  128.       OldDiff : Word;
  129.       i       : Integer;
  130.   begin
  131.     FindSimilarVideoMode:=ScreenMode;
  132.     OldDiff:=999;
  133.  
  134.     for i:=0 to VideoList^.Count-1 do
  135.     begin
  136.       Diff:=Abs(ModeDataList[i].Rows-Rows)+
  137.             Abs(ModeDataList[i].Columns-Columns) div 2+
  138.             20*Ord(ModeDataList[i].Color<>Color);
  139.       if Diff<OldDiff then
  140.       begin
  141.         OldDiff:=Diff;
  142.         FindSimilarVideoMode:=ModeList[i];
  143.       end;
  144.     end;
  145.   end;
  146.  {$ENDIF}
  147.  
  148.  
  149.   (*******************************************************************
  150.     This procedure will be called by Video.ScanEVGAModes with
  151.     new mode information.
  152.   *******************************************************************)
  153.   procedure AddMode(Mode, Rows, Columns, CharHeight:Word; Color:boolean); far;
  154.     const
  155.       ColorStr : string[5] = 'color';
  156.       MonoStr  : string[4] = 'mono';
  157.       BWStr    : string[3] = 'b/w';
  158.     var
  159.       Params : array [0..4] of Longint;
  160.       Line   : String;
  161.       i      : Integer;
  162.   begin
  163.     if (Columns>=80) and (VideoList^.Count<=MaxVideoModes) then
  164.     begin
  165.       Params[0]:=Mode;
  166.       Params[1]:=Columns;
  167.       Params[2]:=Rows;
  168.       Params[3]:=CharHeight;
  169.  
  170.       if Mode=smBW80 then
  171.         Params[4]:=LongInt(@BWStr)
  172.       else
  173.         if Color then
  174.           Params[4]:=LongInt(@ColorStr)
  175.         else
  176.           Params[4]:=LongInt(@MonoStr);
  177.  
  178.       FormatStr(Line, '%3xh  %3dx%-2d  %2dp  %s', Params);
  179.  
  180.       i:=VideoList^.Count;
  181.       ModeList[i]:=Mode;
  182.  
  183.      {$IFDEF StoreModeData}
  184.       ModeDataList[i].Columns:=Columns;
  185.       ModeDataList[i].Rows:=Rows;
  186.       ModeDataList[i].CharHeight:=CharHeight;
  187.       ModeDataList[i].Color:=Color;
  188.      {$ENDIF}
  189.  
  190.       VideoList^.Insert(NewStr(Line));
  191.     end;
  192.   end;
  193.  
  194.  
  195.   (*******************************************************************
  196.     Scan for video modes and add to VideoList
  197.   *******************************************************************)
  198.   procedure SetupVideoList;
  199.   begin
  200.     if VideoList=Nil then    (* Check for previous list... *)
  201.     begin
  202.       New(VideoList, Init(20,10));
  203.  
  204.      {$IFDEF VesaSupport}
  205.       if VESA.VesaScanningPossible then
  206.       begin
  207.         (************************************************************
  208.           Add standard modes if necessary, Marek Bojarski's idea
  209.         ************************************************************)
  210.         if not VESA.StandardInfoAvailable then
  211.         begin
  212.           HideMouse;
  213.           ScanEVGAModes(0, StandardTextModes, AddMode);
  214.           SetSpecialScreenMode(ScreenMode);
  215.           ShowMouse;
  216.         end;
  217.         VESA.ScanVesaModes(AddMode)
  218.       end
  219.       else
  220.      {$ENDIF}
  221.       begin
  222.         HideMouse;
  223.  
  224.         ScanEVGAModes(0, VGAModes, AddMode);
  225.  
  226.        {$IFDEF VesaSupport}   (* If not VesaScanningPossible *)
  227.         if VESA.VesaVersion<>0 then
  228.           ScanEVGAModes($100, VESAModes, AddMode);
  229.        {$ENDIF}
  230.  
  231.         (* Restore Turbo Vision screen *)
  232.         SetSpecialScreenMode(ScreenMode);
  233.         ShowMouse;
  234.       end;
  235.     end;
  236.     VideoModeDataRec.VideoListBox.List:=VideoList;
  237.   end;
  238.  
  239.  
  240.   (*******************************************************************
  241.     Return True if there is no previous list of video modes
  242.   *******************************************************************)
  243.   function HasToScan:Boolean;
  244.   begin
  245.     HasToScan:=VideoList=Nil;
  246.   end;
  247.  
  248.  
  249.   (*******************************************************************
  250.     Let the user select a video mode
  251.   *******************************************************************)
  252.   procedure SelectVideoModeDialog;
  253.   begin
  254.     SelectVideoMode(New(PSelectVideoModeDialog, Init));
  255.   end;
  256.  
  257.  
  258.   (*******************************************************************
  259.     Dialog already created, now execute it
  260.   *******************************************************************)
  261.   procedure SelectVideoMode(P:PSelectVideoModeDialog);
  262.     var
  263.       i : Integer;
  264.   begin
  265.     for i:=0 to VideoList^.Count-1 do
  266.       if ModeList[i]=ScreenMode then
  267.         VideoModeDataRec.VideoListbox.Selection:=i;
  268.  
  269.     if Application^.ExecuteDialog(P, @VideoModeDataRec)=cmOK then
  270.       if VideoList^.Count>0 then
  271.         SetSpecialScreenMode(ModeList[VideoModeDataRec.VideoListBox.Selection]);
  272.   end;
  273.  
  274.  
  275.  
  276. (***************************************************************************
  277.   Here comes the dialog object
  278. ***************************************************************************)
  279.  
  280.   const                  (* Command number irrelevant since local *)
  281.     cmPreview = 1000;
  282.     cmRescan  = 1001;
  283.  
  284.  
  285.   (*******************************************************************
  286.     This procedure generated by Dialog Design 4.0 available by anonymous
  287.     ftp to garbo.uwasa.fi  /pc/turbovis. Thanks to David Baldwin
  288.   *******************************************************************)
  289.   constructor TSelectVideoModeDialog.Init;
  290.     var
  291.       R : TRect;
  292.       Control : PView;
  293.   begin
  294.     R.Assign(14, 3, 66, 20);
  295.     inherited Init(R, 'Select Video Mode');
  296.     Options := Options or ofCentered;
  297.  
  298.     R.Assign(32, 3, 33, 15);
  299.     Control := New(PScrollBar, Init(R));
  300.     Insert(Control);
  301.  
  302.     R.Assign(5, 3, 32, 15);
  303.     VideoListBox := New(PListBox, Init(R, 1, PScrollbar(Control)));
  304.     VideoListBox^.HelpCtx := hctoyVideoListBox;
  305.     Insert(VideoListBox);
  306.  
  307.     R.Assign(4, 2, 16, 3);
  308.     Insert(New(PLabel, Init(R, '~V~ideo modes', VideoListBox)));
  309.  
  310.     R.Assign(37, 3, 48, 5);
  311.     Control := New(PButton, Init(R, '~P~review', cmPreview, bfDefault));
  312.     Control^.HelpCtx := hctoyVideoPreview;
  313.     Insert(Control);
  314.  
  315.     R.Assign(37, 6, 48, 8);
  316.     Control := New(PButton, Init(R, 'O~K~', cmOK, bfNormal));
  317.     Control^.HelpCtx := hcOK;
  318.     Insert(Control);
  319.  
  320.     R.Assign(37, 8, 48, 10);
  321.     Control := New(PButton, Init(R, 'Cancel', cmCancel, bfNormal));
  322.     Control^.HelpCtx := hcCancel;
  323.     Insert(Control);
  324.  
  325.     R.Assign(37, 11, 48, 13);
  326.     Control := New(PButton, Init(R, '~R~escan', cmRescan, bfNormal));
  327.     Control^.HelpCtx := hctoyVideoRescan;
  328.     Insert(Control);
  329.  
  330.     R.Assign(37, 14, 48, 16);
  331.     Control := New(PButton, Init(R, 'Help', cmHelp, bfNormal));
  332.     Control^.HelpCtx := hctoyVideoDialogHelp;
  333.     Insert(Control);
  334.  
  335.     SelectNext(False);
  336.   end;
  337.  
  338.   constructor TSelectVideoModeDialog.Load;
  339.   begin
  340.     inherited Load(S);
  341.     GetSubViewPtr(S, VideoListBox);
  342.   end;
  343.  
  344.   procedure TSelectVideoModeDialog.HandleEvent;
  345.     var
  346.       OldMode : Word;
  347.   begin
  348.     inherited HandleEvent(Event);
  349.     if (Event.What and evMessage<>0) then
  350.     begin
  351.       case Event.Command of
  352.         cmListItemSelected,      (* Mouse double clicked in list *)
  353.         cmPreview:
  354.           begin
  355.             OldMode:=ScreenMode;
  356.             SetSpecialScreenMode(ModeList[VideoListBox^.Focused]);
  357.             Delay(PreviewTime);
  358.             SetSpecialScreenMode(OldMode);
  359.           end;
  360.         cmRescan:
  361.           begin
  362.             VideoList:=Nil;
  363.             SetupVideoList;
  364.             VideoListBox^.NewList(VideoList);
  365.           end;
  366.         else
  367.           Exit;
  368.       end;
  369.       ClearEvent(Event);
  370.     end;
  371.   end;
  372.  
  373.   procedure TSelectVideoModeDialog.Store;
  374.   begin
  375.     inherited Store(S);
  376.     PutSubViewPtr(S, VideoListBox);
  377.   end;
  378.  
  379.  
  380.     (*******************************************************************
  381.     *******************************************************************)
  382.  
  383. end.
  384.  
  385.  
  386.