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

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