home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 10.ddi / CHESS.ZIP / OWUTILS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  7.6 KB  |  274 lines

  1. {************************************************}
  2. {                                                }
  3. {   ObjectWindows Chess Demo                     }
  4. {   Utility objects and procedures               }
  5. {   Copyright (c) 1992 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. unit OWUtils;
  10.  
  11. interface
  12.  
  13. uses WinProcs, WinTypes, Objects, OWindows;
  14.  
  15. type
  16.   PXtendedApp = ^TXtendedApp;
  17.   TXtendedApp = object (TApplication)
  18.     INIFileName: PChar;
  19.     ProfileWriteEnabled: Boolean;
  20.     constructor Init(AppName: PChar);
  21.     destructor Done; virtual;
  22.     procedure InitINIFileName; virtual;
  23.     procedure EnableAppProfileWrite(Enable: Boolean);
  24.     function  GetAppProfileString(Section, Key, Default,
  25.                      Result: PChar; Size: Word): Integer;
  26.     procedure WriteAppProfileString(Section, Key, S: PChar);
  27.     function  GetAppProfileLongint(Section, Key: PChar;
  28.                      Default: Longint): Longint;
  29.     procedure WriteAppProfileLongint(Section, Key: PChar; Data: Longint);
  30.     function  GetAppProfileRGB(Section, Key: PChar;
  31.                      Default: TColorRef): TColorRef;
  32.     procedure WriteAppProfileRGB(Section, Key: PChar; Data: TColorRef);
  33.     function  GetAppProfileBoolean(Section, Key: PChar;
  34.                                    Default: Boolean): Boolean;
  35.     procedure WriteAppProfileBoolean(Section, Key: PChar; Data: Boolean);
  36.   end;
  37.  
  38. { Context returns a composite string id if the MsgCode is non zero,
  39.   otherwise returns zero.  This is handy for mapping multiple error
  40.   code sets with overlapping codes into a non-overlapping set of context
  41.   string ids, as long as they all use zero to indicate "A-Ok, no msg
  42.   needed". The non-overlapped set of context numbers is what you need
  43.   for string resources.  Build your string table entries using a
  44.   context base constant + the error/msg code for each msg string,
  45.   changing the base constant for each group of codes that conflict.  }
  46. function  Context(Ctx, MsgCode: Integer): Integer;
  47.  
  48. { StrResMessageBox will accept string ids (typecast to PChars) as well
  49.   as true string pointers.  If the high word of the pointer is zero,
  50.   the low word is used as the string id in a call to LoadString, otherwise
  51.   the pointer is used as-is. The normal MessageBox function is called
  52.   after any string fixups.
  53.  
  54.   This function makes it convenient to use string id constants and
  55.   string resources instead of string constants in your program.}
  56. function  StrResMessageBox(Parent: HWnd; Txt, Caption: PChar;
  57.   Flags: Word): Integer;
  58.  
  59. { StrNewAny:  If Source is a regular string pointer, StrNew is called.
  60.   If Source is a string id, LoadString is called and the result is passed
  61.   to StrNew.  Either way, you get back a pointer which needs to be
  62.   StrDisposed with you're through with it. }
  63. function  StrNewAny(Source: PChar): PChar;
  64.  
  65. { StrNewRes:  Load a string resource and StrNew it. }
  66. function  StrNewRes(Source: Word): PChar;
  67.  
  68. { StrLoadRes:  Load a string resource into the provided buffer. }
  69. function  StrLoadRes(var Dest: array of Char; Source: Word): PChar;
  70.  
  71. var
  72.   XApp : PXtendedApp;
  73.  
  74. implementation
  75.  
  76. uses Strings;
  77.  
  78. var
  79.   OldMessageBox : TMessageBox;
  80.  
  81. constructor TXtendedApp.Init(AppName: PChar);
  82. begin
  83.   InitINIFileName;
  84.   XApp := @Self;
  85.   ProfileWriteEnabled := True;
  86.   inherited Init(AppName);
  87. end;
  88.  
  89. destructor TXtendedApp.Done;
  90. begin
  91.   inherited Done;
  92.   if INIFileName <> nil then
  93.     StrDispose(INIFileName);
  94. end;
  95.  
  96. procedure TXtendedApp.InitINIFileName;
  97. var
  98.   Buf: array [0..80] of Char;
  99.   B, E: PChar;
  100. begin
  101.   GetModuleFileName(HInstance, Buf, SizeOf(Buf));
  102.   B := StrRScan(Buf, '\');
  103.   if B = nil then
  104.     B := Buf
  105.   else
  106.     Inc(B);
  107.   E := StrScan(B, '.');
  108.   if E = nil then
  109.     StrCat(B,'.INI')
  110.   else
  111.     StrCopy(E, '.INI');
  112.   INIFileName := StrNew(B);
  113. end;
  114.  
  115. procedure TXtendedApp.EnableAppProfileWrite(Enable: Boolean);
  116. begin
  117.   ProfileWriteEnabled := Enable;
  118. end;
  119.  
  120. function TXtendedApp.GetAppProfileString(Section, Key, Default,
  121.   Result: PChar; Size: Word): Integer;
  122. begin
  123.   GetAppProfileString := GetPrivateProfileString(Section, Key, Default,
  124.     Result, Size, INIFileName);
  125. end;
  126.  
  127. procedure TXtendedApp.WriteAppProfileString(Section, Key, S: PChar);
  128. begin
  129.   if ProfileWriteEnabled then
  130.     WritePrivateProfileString(Section, Key, S, INIFileName);
  131. end;
  132.  
  133. function  TXtendedApp.GetAppProfileLongint(Section, Key: PChar;
  134.   Default: Longint): Longint;
  135. var
  136.   S: array [0..20] of Char;
  137.   Temp : Longint;
  138.   Code : Integer;
  139. begin
  140.   GetAppProfileLongint := Default;
  141.   GetAppProfileString(Section, Key, '', S, SizeOf(S));
  142.   if S[0] = #0 then  Exit;
  143.   Val(S, Temp, Code);
  144.   if Code <> 0 then  Exit;
  145.   GetAppProfileLongint := Temp;
  146. end;
  147.  
  148. procedure TXtendedApp.WriteAppProfileLongint(Section, Key: PChar;
  149.   Data: Longint);
  150. var
  151.   Temp: String[15];
  152. begin
  153.   Temp := '';
  154.   Str(Data, Temp);
  155.   Temp := Temp + #0;
  156.   WriteAppProfileString(Section, Key, @Temp[1]);
  157. end;
  158.  
  159. function TXtendedApp.GetAppProfileRGB(Section, Key: PChar;
  160.   Default: Longint): Longint;
  161. var
  162.   S: array[0..15] of Char;
  163.   P,Q: PChar;
  164.   Code: Integer;
  165.   R,G,B: Byte;
  166. begin
  167.   GetAppProfileRGB := Default;
  168.   S[0] := #0;
  169.   GetAppProfileString(Section, Key, '', S, Sizeof(S)-1);
  170.   if S[0] = #0 then Exit;
  171.   P := StrScan(S, ',');
  172.   if P = nil then Exit;
  173.   P[0] := #0;
  174.   Val(S, R, Code);
  175.  
  176.   Q := P + 1;
  177.   P := StrScan(Q, ',');
  178.   if P = nil then Exit;
  179.   P[0] := #0;
  180.   Val(Q, G, Code);
  181.  
  182.   Q := P + 1;
  183.   Val(Q, B, Code);
  184.  
  185.   GetAppProfileRGB := RGB(R,G,B);
  186. end;
  187.  
  188. procedure TXtendedApp.WriteAppProfileRGB(Section, Key: PChar;
  189.   Data: TColorRef);
  190. var
  191.   Temp: String[5];
  192.   S: array [0..15] of Char;
  193. begin
  194.   Str(GetRValue(Data), Temp);
  195.   StrCat(StrPCopy(S, Temp), ',');
  196.   Str(GetGValue(Data), Temp);
  197.   Temp := Temp + #0;
  198.   StrCat(StrCat(S, @Temp[1]), ',');
  199.   Str(GetBValue(Data), Temp);
  200.   Temp := Temp + #0;
  201.   StrCat(S, @Temp[1]);
  202.   WriteAppProfileString(Section, Key, S);
  203. end;
  204.  
  205. function  TXtendedApp.GetAppProfileBoolean(Section, Key: PChar;
  206.   Default: Boolean): Boolean;
  207. var
  208.   S: array [0..5] of Char;
  209. begin
  210.   if Default then
  211.     S[0] := 'Y'
  212.   else
  213.     S[0] := 'N';
  214.   S[1] := #0;
  215.   GetAppProfileString(Section, Key, S, S, SizeOf(S));
  216.   GetAppProfileBoolean := S[0] in ['Y','1'];
  217. end;
  218.  
  219. procedure TXtendedApp.WriteAppProfileBoolean(Section, Key: PChar;
  220.   Data: Boolean);
  221. begin
  222.   if Data then
  223.     WriteAppProfileString(Section, Key, 'Y')
  224.   else
  225.     WriteAppProfileString(Section, Key, 'N');
  226. end;
  227.  
  228. function Context(Ctx, MsgCode: Integer): Integer;
  229. begin
  230.   Context := 0;
  231.   if MsgCode <> 0 then
  232.     Context := Ctx + MsgCode;
  233. end;
  234.  
  235. function StrNewAny(Source: PChar): PChar;
  236. begin
  237.   StrNewAny := nil;
  238.   if Source <> nil then
  239.     if PtrRec(Source).Seg = 0 then
  240.       StrNewAny := StrNewRes(PtrRec(Source).Ofs)
  241.     else
  242.       StrNewAny := StrNew(Source);
  243. end;
  244.  
  245. function StrNewRes(Source: Word): PChar;
  246. var
  247.   Temp : array [0..255] of Char;
  248. begin
  249.   StrNewRes := StrNew(StrLoadRes(Temp, Source));
  250. end;
  251.  
  252. function StrLoadRes(var Dest: array of Char; Source: Word): PChar;
  253. begin
  254.   Dest[0] := #0;
  255.   LoadString(HInstance, Source, @Dest, High(Dest));
  256.   StrLoadRes := @Dest;
  257. end;
  258.  
  259. function  StrResMessageBox(Parent: HWnd;
  260.                            Txt, Caption: PChar;
  261.                            Flags: Word): Integer;
  262. begin
  263.   Txt := StrNewAny(Txt);
  264.   Caption := StrNewAny(Caption);
  265.   StrResMessageBox := OldMessageBox(Parent, Txt, Caption, Flags);
  266.   StrDispose(Txt);
  267.   StrDispose(Caption);
  268. end;
  269.  
  270.  
  271. begin
  272.   @OldMessageBox := @MessageBox;
  273.   @MessageBox := @StrResMessageBox;
  274. end.