home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / BPASCAL.700 / D12 / CHESSOWL.ZIP / CHESSDLG.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-01  |  14.5 KB  |  466 lines

  1. { Dialogs for the OWL Chess program. }
  2.  
  3. unit chessdlg;
  4.  
  5. interface
  6.  
  7. uses Winprocs, Wintypes, OWindows, ODialogs, Validate, ChessDLL, ChConst;
  8.  
  9. type
  10.  
  11.   PChessInfoWindow = ^TChessInfoWindow;
  12.   TChessInfoWindow = object(TDlgWindow)
  13.     Msg: PStatic;
  14.     constructor Init(AParent: PWindowsObject; AName: PChar);
  15.     function GetClassName: PChar; virtual;
  16.     procedure GetWindowClass(var WC: TWndClass); virtual;
  17.     procedure Update(Game: HChess; WhiteTime, BlackTime: Longint);
  18.   end;
  19.  
  20.   PSettingsXferRec = ^TSettingsXferRec;
  21.   TSettingsXferRec = record
  22.     LimitGame,
  23.     LimitTurn,
  24.     MatchUser,
  25.     NoLimit:  WordBool;
  26.     GameTime,
  27.     TurnTime: Longint;
  28.     ShowAttacks,
  29.     ShowJeopardies,
  30.     ShowBestLine,
  31.     RightClickHints,
  32.     OnePlayer,
  33.     TwoPlayer: WordBool;
  34.     ThinkTime: TScrollBarTransferRec;
  35.     RefreshRate: Word;   { not used by dialog, keep at bottom of record }
  36.   end;
  37.  
  38.   PSettingsDlg = ^TSettingsDlg;
  39.   TSettingsDlg = object(TDialog)
  40.     constructor Init(AParent: PWindowsObject; AName: PChar;
  41.                      var XferBuf: TSettingsXferRec);
  42.     procedure SetupWindow; virtual;
  43.     procedure EnableSet(Game, Turn: Boolean);
  44.     procedure ShowSet(Game, Turn: Boolean);
  45.     procedure LimitGameTime(var Msg: TMessage);
  46.       virtual id_First + idLimitGameTime;
  47.     procedure LimitTurnTime(var Msg: TMessage);
  48.       virtual id_First + idLimitTurnTime;
  49.     procedure MatchUserTime(var Msg: TMessage);
  50.       virtual id_First + idMatchUserTime;
  51.     procedure NoTimeLimit(var Msg: TMessage);
  52.       virtual id_First + idNoTimeLimit;
  53.   end;
  54.  
  55. var
  56.   ChessSettings: TSettingsXferRec;
  57.  
  58. procedure LoadINISettings;
  59. procedure SaveINISettings;
  60.  
  61. implementation
  62.  
  63. uses AppUtils, Strings, CTimers;
  64.  
  65.   { The LockWindowUpdate function will eliminate all flicker caused by
  66.     switching between the two edit controls set up in the Settings dialog.
  67.     This function is only available in Windows 3.1, though, so in order
  68.     to allow this program to run (with some flicker) in Windows 3.0,
  69.     this program should:
  70.  
  71.       1) Never call LockWindowUpdate when running under Windows 3.0
  72.       2) Avoid using static declarations (like Win31.pas) to import
  73.          the function, since Windows 3.0 won't load an app if the app
  74.          contains static references to DLL functions Windows 3.0
  75.          doesn't have.
  76.  
  77.     The following code uses a function variable and GetProcAddress to
  78.     request the address of the LockWindowUpdate function.  Windows 3.0
  79.     will return a nil function address if you ask for a function that
  80.     doesn't exist in the indicated DLL.  Before each use of the
  81.     function variable, test it for nil using the Assigned function.
  82.   }
  83.  
  84. type
  85.   Win31LockWindowUpdateFunc = function (Wnd: HWnd): Bool;
  86.  
  87. const
  88.   Win31LockWindowUpdate: Win31LockWindowUpdateFunc = nil;
  89.   AM_InfoUpdate = wm_User + 502;
  90.  
  91. type
  92.   PUpdateRec = ^TUpdateRec;
  93.   TUpdateRec = record
  94.     Time : array [cWhite..cBlack] of Longint;
  95.   end;
  96.  
  97.   PTurnDisplay = ^TTurnDisplay;
  98.   TTurnDisplay = object(TWindow)
  99.     Color: TColor;
  100.     Tag: array [cWhite..cBlack] of PChar;
  101.     constructor InitResource(AParent: PWindowsObject; ResID: Integer);
  102.     destructor Done; virtual;
  103.     procedure Paint(DC: HDC; var PS: TPaintStruct); virtual;
  104.     procedure AMInfoUpdate(var Msg: TMessage);
  105.       virtual wm_First + am_InfoUpdate;
  106.   end;
  107.  
  108.   PTimeDisplay = ^TTimeDisplay;
  109.   TTimeDisplay = object(TStatic)
  110.     Color : TColor;
  111.     constructor InitResource(AParent: PWindowsObject; ResID: Integer;
  112.                              ATextLen: Word; AColor: TColor);
  113.     procedure AMInfoUpdate(var Msg: TMessage);
  114.       virtual wm_First + am_InfoUpdate;
  115.   end;
  116.  
  117.   PBestLine = ^TBestLine;
  118.   TBestLine = object(TStatic)
  119.     CurrentLine: array [0..100] of Char;
  120.     constructor InitResource(AParent: PWindowsObject; ResID: Integer);
  121.     procedure AMInfoUpdate(var Msg: TMessage);
  122.       virtual wm_First + am_InfoUpdate;
  123.   end;
  124.  
  125.   PValueLine = ^TValueLine;
  126.   TValueLine = object(TStatic)
  127.     CurrentValue: Integer;
  128.     constructor InitResource(AParent: PWindowsObject; ResID: Integer);
  129.     procedure AMInfoUpdate(var Msg: TMessage);
  130.       virtual wm_First + am_InfoUpdate;
  131.   end;
  132.  
  133. constructor TTurnDisplay.InitResource(AParent: PWindowsObject;
  134.                                       ResID: Integer);
  135. begin
  136.   inherited InitResource(AParent, ResID);
  137.   Color := cWhite;
  138.   StrNewRes(Tag[cWhite], PChar(strWhite));
  139.   StrNewRes(Tag[cBlack], PChar(strBlack));
  140. end;
  141.  
  142. destructor TTurnDisplay.Done;
  143. begin
  144.   StrDispose(Tag[cWhite]);
  145.   StrDispose(Tag[cBlack]);
  146.   inherited Done;
  147. end;
  148.  
  149. procedure TTurnDisplay.Paint(DC: HDC; var PS: TPaintStruct);
  150. var
  151.   R: TRect;
  152.   TE: Integer;
  153. begin
  154.   SaveDC(DC);
  155.   GetClientRect(HWindow, R);
  156.   if Color = cBlack then
  157.   begin
  158.     SetTextColor(DC, RGB(255,255,255));
  159.     SetBkColor(DC, RGB(0,0,0));
  160.     PatBlt(DC, R.Left, R.Top, R.Right, R.Bottom, Blackness);
  161.   end
  162.   else
  163.   begin
  164.     SetTextColor(DC, RGB(0,0,0));
  165.     SetBkColor(DC, RGB(255,255,255));
  166.     PatBlt(DC, R.Left, R.Top, R.Right, R.Bottom, Whiteness);
  167.   end;
  168.   TE := GetTextExtent(DC, Tag[Color], StrLen(Tag[Color]));
  169.   TextOut(DC, (R.Right div 2) - (LoWord(TE) div 2), 0,
  170.                  Tag[Color], StrLen(Tag[Color]));
  171.   RestoreDC(DC, -1);
  172. end;
  173.  
  174. procedure TTurnDisplay.AMInfoUpdate(var Msg: TMessage);
  175. begin
  176.   if GetPlayer(HChess(Msg.WParam)) <> Color then
  177.   begin
  178.     Color := GetPlayer(HChess(Msg.WParam));
  179.     InvalidateRect(HWindow,nil,False);
  180.   end;
  181. end;
  182.  
  183. constructor TTimeDisplay.InitResource(AParent: PWindowsObject;
  184.                                       ResID: Integer;
  185.                                       ATextLen: Word;
  186.                                       AColor: TColor);
  187. begin
  188.   inherited InitResource(AParent, ResID, ATextLen);
  189.   Color := AColor;
  190. end;
  191.  
  192. procedure TTimeDisplay.AMInfoUpdate(var Msg: TMessage);
  193. var
  194.   s: array [0..20] of Char;
  195.   P: array [0..3] of Word;
  196. begin
  197.  if GetPlayer(HChess(Msg.WParam)) = Color then
  198.  begin
  199.    ConvertTicks(PUpdateRec(Msg.LParam)^.Time[Color],P[0],P[1],P[2],P[3]);
  200.    WVSprintf(S, '%02i:%02i:%02i.%03i', P);
  201.    SetText(S);
  202.  end;
  203. end;
  204.  
  205. constructor TBestLine.InitResource(AParent: PWindowsObject; ResID: Integer);
  206. begin
  207.   inherited InitResource(AParent, ResID, 100);
  208.   CurrentLine[0] := #0;
  209. end;
  210.  
  211. procedure TBestLine.AMInfoUpdate(var Msg: TMessage);
  212. var
  213.   Value: Integer;
  214.   Line: array [0..23] of TMove;
  215.   S: array [0..8] of Char;
  216.   NewLine : array [0..100] of Char;
  217.   X, L: Integer;
  218. begin
  219.   NewLine[0] := #0;
  220.   if ChessSettings.ShowBestLine then
  221.   begin
  222.     GetMainLine(HChess(Msg.WParam), Value, Line);
  223.     X := 0;
  224.     L := 0;
  225.     while (X <= High(Line))
  226.       and (Line[X].Change.Piece <> pEmpty)
  227.       and (L <= (High(NewLine) - High(S))) do
  228.     begin
  229.       MoveToStr(Line[X],S);
  230.       StrCopy(@NewLine[L],StrCat(S, ' '));
  231.       Inc(L, StrLen(S));
  232.       Inc(X);
  233.     end;
  234.   end;
  235.   if StrComp(CurrentLine, NewLine) <> 0 then
  236.   begin
  237.     SetText(NewLine);
  238.     StrCopy(CurrentLine, NewLine);
  239.   end;
  240. end;
  241.  
  242. constructor TValueLine.InitResource(AParent: PWindowsObject;
  243.                                     ResID: Integer);
  244. begin
  245.   inherited InitResource(AParent, ResID, 10);
  246.   CurrentValue := 0;
  247. end;
  248.  
  249. procedure TValueLine.AMInfoUpdate(var Msg: TMessage);
  250. var
  251.   Value: Integer;
  252.   Move: TMove;
  253.   S: array [0..10] of Char;
  254. begin
  255.   GetMainLine(HChess(Msg.WParam), Value, Move);
  256.   if Value <> CurrentValue then
  257.   begin
  258.     Str(Value, S);
  259.     SetText(S);
  260.     CurrentValue := Value;
  261.   end;
  262. end;
  263.  
  264.  
  265. constructor TChessInfoWindow.Init(AParent: PWindowsObject; AName: PChar);
  266. var
  267.   Dummy : PWindowsObject;
  268. begin
  269.   inherited Init(AParent, AName);
  270.   Msg := New(PStatic, InitResource(@Self, idInfoMsg, 50));
  271.   Dummy := New(PValueLine, InitResource(@Self, idInfoValue));
  272.   Dummy := New(PBestLine, InitResource(@Self, idInfoBestLine));
  273.   Dummy := New(PTimeDisplay, InitResource(@Self, idInfoWhite, 30, cWhite));
  274.   Dummy := New(PTimeDisplay, InitResource(@Self, idInfoBlack, 30, cBlack));
  275.   Dummy := New(PTurnDisplay, InitResource(@Self, idInfoTurn));
  276. end;
  277.  
  278. function TChessInfoWindow.GetClassName: PChar;
  279. begin
  280.   GetClassName := 'BorDlg_ChessInfo';
  281. end;
  282.  
  283. procedure TChessInfoWindow.GetWindowClass(var WC: TWndClass);
  284. begin
  285.   inherited GetWindowClass(WC);
  286.   WC.hCursor := 0;      { reflect wm_setcursor back to parent window }
  287. end;
  288.  
  289. procedure TChessInfoWindow.Update(Game: HChess; WhiteTime, BlackTime: Longint);
  290. var
  291.   N: TUpdateRec;
  292.  
  293.   procedure DoUpdate(P: PWindowsObject); far;
  294.   begin
  295.     SendMessage(P^.HWindow, AM_InfoUpdate, Game, Longint(@N));
  296.   end;
  297.  
  298. begin
  299.   N.Time[cWhite] := WhiteTime;
  300.   N.Time[cBlack] := BlackTime;
  301.   ForEach(@DoUpdate);
  302. end;
  303.  
  304.  
  305. constructor TSettingsDlg.Init(AParent: PWindowsObject;
  306.                               AName: PChar;
  307.                               var XferBuf: TSettingsXferRec);
  308. var
  309.   P : PWindowsObject;
  310. begin
  311.   inherited Init(AParent, AName);
  312.   P := New(PRadioButton, InitResource(@Self, idLimitGameTime));
  313.   P := New(PRadioButton, InitResource(@Self, idLimitTurnTime));
  314.   P := New(PRadioButton, InitResource(@Self, idMatchUserTime));
  315.   P := New(PRadioButton, InitResource(@Self, idNoTimeLimit));
  316.   P := New(PEdit, InitResource(@Self, idLimitGameTimeInput, TimeLimitInputLen));
  317.   PEdit(P)^.SetValidator(New(PRangeValidator, Init(1, 600)));
  318.   with PEdit(P)^.Validator^ do
  319.     Options := Options or voTransfer;
  320.   P := New(PEdit, InitResource(@Self, idLimitTurnTimeInput, TimeLimitInputLen));
  321.   PEdit(P)^.SetValidator(New(PRangeValidator, Init(1, 36000)));
  322.   with PEdit(P)^.Validator^ do
  323.     Options := Options or voTransfer;
  324.   P := New(PCheckBox, InitResource(@Self, idShowAttacks));
  325.   P := New(PCheckBox, InitResource(@Self, idShowJeopardies));
  326.   P := New(PCheckBox, InitResource(@Self, idShowBestLine));
  327.   P := New(PCheckBox, InitResource(@Self, idRightClickQueries));
  328.   P := New(PRadioButton, InitResource(@Self, idSinglePlayer));
  329.   P := New(PRadioButton, InitResource(@Self, idTwoPlayer));
  330.   P := New(PScrollbar, InitResource(@Self, idThinkTime));
  331.   P^.EnableTransfer;
  332.   TransferBuffer := @XferBuf;
  333. end;
  334.  
  335. procedure TSettingsDlg.SetupWindow;
  336. begin
  337.   inherited SetupWindow;
  338.   with PSettingsXferRec(TransferBuffer)^ do
  339.     ShowSet(LimitGame, LimitTurn);
  340. end;
  341.  
  342. procedure TSettingsDlg.EnableSet(Game, Turn: Boolean);
  343. begin
  344.   EnableWindow(GetItemHandle(idLimitTurnTimeLabel), Turn);
  345.   EnableWindow(GetItemHandle(idLimitTurnTimeInput), Turn);
  346.   EnableWindow(GetItemHandle(idTurnTimeUnit), Turn);
  347.   EnableWindow(GetItemHandle(idLimitGameTimeLabel), Game);
  348.   EnableWindow(GetItemHandle(idLimitGameTimeInput), Game);
  349.   EnableWindow(GetItemHandle(idGameTimeUnit), Game);
  350. end;
  351.  
  352. procedure TSettingsDlg.ShowSet(Game, Turn: Boolean);
  353. const
  354.   sw : array [False..True] of Word = (sw_Hide, sw_Show);
  355. begin
  356.   if Assigned(Win31LockWindowUpdate) then
  357.     Win31LockWindowUpdate(HWindow);
  358.   ShowWindow(GetItemHandle(idLimitTurnTimeInput), sw[Turn]);
  359.   ShowWindow(GetItemHandle(idLimitTurnTimeLabel), sw[Turn]);
  360.   ShowWindow(GetItemHandle(idTurnTimeUnit), sw[Turn]);
  361.   ShowWindow(GetItemHandle(idLimitGameTimeInput), sw[Game]);
  362.   ShowWindow(GetItemHandle(idLimitGameTimeLabel), sw[Game]);
  363.   ShowWindow(GetItemHandle(idGameTimeUnit), sw[Game]);
  364.   if Assigned(Win31LockWindowUpdate) then
  365.     Win31LockWindowUpdate(0);
  366.   EnableSet(Game, Turn);
  367. end;
  368.  
  369. procedure TSettingsDlg.LimitGameTime(var Msg: TMessage);
  370. begin
  371.   DefWndProc(Msg);
  372.   if Msg.LParamHi = BN_Clicked then
  373.     ShowSet(True, False);
  374. end;
  375.  
  376. procedure TSettingsDlg.LimitTurnTime(var Msg: TMessage);
  377. begin
  378.   DefWndProc(Msg);
  379.   if Msg.LParamHi = BN_Clicked then
  380.     ShowSet(False, True);
  381. end;                                           
  382.  
  383. procedure TSettingsDlg.MatchUserTime(var Msg: TMessage);
  384. begin
  385.   DefWndProc(Msg);
  386.   if Msg.LParamHi = BN_Clicked then
  387.     EnableSet(False, False);
  388. end;
  389.  
  390. procedure TSettingsDlg.NoTimeLimit(var Msg: TMessage);
  391. begin
  392.   DefWndProc(Msg);
  393.   if Msg.LParamHi = BN_Clicked then
  394.     EnableSet(False, False);
  395. end;
  396.  
  397.  
  398. procedure LoadINISettings;
  399. var
  400.   I: Longint;
  401. begin
  402.   FillChar(ChessSettings, SizeOf(ChessSettings), 0);
  403.   with ChessSettings, XApp^ do
  404.   begin
  405.     I := GetAppProfileLongint('Settings','TimeLimitType',2);
  406.     case I of
  407.       1: LimitGame := True;
  408.       2: LimitTurn := True;
  409.       4: MatchUser := True;
  410.       8: NoLimit   := True;
  411.     else
  412.       {!! Display error msg }
  413.       LimitTurn := True;
  414.     end;
  415.     TurnTime := GetAppProfileLongint('Settings','SecsPerTurn',60);
  416.     GameTime := GetAppProfileLongint('Settings','MinsPerGame',30);
  417.     ShowAttacks := GetAppProfileBoolean('Settings','ShowAttacks',True);
  418.     ShowJeopardies := GetAppProfileBoolean('Settings',
  419.                                            'ShowJeopardies',True);
  420.     ShowBestLine := GetAppProfileBoolean('Settings','ShowBestLine',True);
  421.     RightClickHints := GetAppProfileBoolean('Settings',
  422.                                             'RightClickHints',True);
  423.     TwoPlayer := GetAppProfileBoolean('Settings','TwoPlayers',False);
  424.     OnePlayer := not TwoPlayer;
  425.     with ThinkTime do
  426.     begin
  427.       LowValue := 1;
  428.       HighValue := 36;
  429.       Position := Integer(GetAppProfileLongint('Settings','TicsPerThink',2));
  430.     end;
  431.     RefreshRate := Word(GetAppProfileLongint('Settings','RefreshRate',500));
  432.   end;
  433. end;
  434.  
  435. procedure SaveINISettings;
  436. var
  437.   X: Longint;
  438. begin
  439.   with ChessSettings, XApp^ do
  440.   begin
  441.     X := Word(LimitGame) +
  442.          Word(LimitTurn) shl 1 +
  443.          Word(MatchUser) shl 2 +
  444.          Word(NoLimit) shl 3;
  445.     WriteAppProfileLongint('Settings','TimeLimitType',X);
  446.     WriteAppProfileLongint('Settings','SecsPerTurn',TurnTime);
  447.     WriteAppProfileLongint('Settings','MinsPerGame',GameTime);
  448.     WriteAppProfileBoolean('Settings','ShowAttacks',ShowAttacks);
  449.     WriteAppProfileBoolean('Settings','ShowJeopardies',ShowJeopardies);
  450.     WriteAppProfileBoolean('Settings','ShowBestLine',ShowBestLine);
  451.     WriteAppProfileBoolean('Settings','RightClickHints',RightClickHints);
  452.     WriteAppProfileBoolean('Settings','TwoPlayers',TwoPlayer);
  453.     WriteAppProfileLongint('Settings','TicsPerThink',ThinkTime.Position);
  454.     WriteAppProfileLongint('Settings','RefreshRate',RefreshRate);
  455.   end;
  456. end;
  457.  
  458.  
  459. begin
  460.       { In Windows 3.0, the following GetProcAddress call will return nil,
  461.         but not cause a critical error message.  Any code that uses
  462.         this function variable should always test it first, with
  463.         the Assigned system function. }
  464.   @Win31LockWindowUpdate := GetProcAddress(
  465.                              GetModuleHandle('User'), PChar(294));
  466. end.