home *** CD-ROM | disk | FTP | other *** search
/ Delphi Programming Unleashed / Delphi_Programming_Unleashed_SAMS_Publishing_1995.iso / misc / dskfile / main.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-03-21  |  9.9 KB  |  360 lines

  1. unit Main;
  2.  
  3. { Program copyright (c) 1995 by Charles Calvert }
  4. { Project Name: DSKFILE }
  5.  
  6. { Use this program to view and edit INI files.
  7.  
  8.   The code shown here was originally meant to help you arrange DSK
  9.   files. DSK files are used by Delphi to remember where the
  10.   windows used at design time are located. For instance, the
  11.   Editor, Objector Inspector, Watch window, etc, can all
  12.   have their locations saved to this file if you choose the
  13.   correct options from the Preferences page of the Options |
  14.   Environment window. This program was originally meant to
  15.   allow you to automatically arrange all these windows so that
  16.   when you opened a project it would be orderly in appearance.
  17.   The code shown here still performs that function, but I have
  18.   found it to be only minimally useful.
  19.  
  20.   I decided to keep the program on the CD, however, because it
  21.   can also be used to browse any existing INI file, include
  22.   DELPHI.INI or SYSTEM.INI. As such it provides a useful
  23.   function and an interesting example of how to use the
  24.   INIFILES.PAS unit that ships with Delphi. }
  25.  
  26. interface
  27.  
  28. uses
  29.    WinTypes, WinProcs, Classes,
  30.    Graphics, Forms, Controls,
  31.    IniFiles, StdCtrls, Menus,
  32.    Dialogs, ExtCtrls;
  33.  
  34. type
  35.   TAry = array[1..7] of LongInt;
  36.   TSize=  (sSmall, sMedium, sLarge);
  37.  
  38. const
  39.   DefaultCaption = 'INI File Manager';
  40.   pi800X600: TAry = (1, 1, 0, 0, 115, 221, 392);
  41.   ww800X600: TAry = (1, 1, 0, 0, 507, 801, 91);
  42.   ew800X600: TAry = (1, 1, 0, 220, 115, 581, 392);
  43.   pi640X480: TAry = (1, 1, 0, 0, 97, 198, 308);
  44.   ww640X480: TAry = (1, 1, 0, 0, 404, 636, 76);
  45.   ew640X480: TAry = (1, 1, 0, 199, 97, 441, 308);
  46.   pi1024x768: TAry = (1, 1, 0, 0, 97, 198, 308);
  47.   ww1024x768: TAry = (1, 1, 0, 0, 404, 636, 76);
  48.   ew1024x768: TAry = (1, 1, 0, 199, 97, 441, 308);
  49.   CurScreen: TSize = sMedium;
  50.  
  51. type
  52.   tGetDem = (gdLeft, gdTop, gdWidth, gdHeight);
  53.  
  54.   TForm1 = class(TForm)
  55.     ListBox1: TListBox;
  56.     MainMenu1: TMainMenu;
  57.     File1: TMenuItem;
  58.     Select1: TMenuItem;
  59.     OpenDialog1: TOpenDialog;
  60.     N1: TMenuItem;
  61.     Exit1: TMenuItem;
  62.     ECurItem: TEdit;
  63.     DSKFiles1: TMenuItem;
  64.     SetWatch1: TMenuItem;
  65.     SetPropInspector1: TMenuItem;
  66.     SetEditWindow1: TMenuItem;
  67.     SectionBox: TListBox;
  68.     Label2: TLabel;
  69.     Label3: TLabel;
  70.     Label1: TLabel;
  71.     procedure CurSectionClick(Sender: TObject);
  72.     procedure FormCreate(Sender: TObject);
  73.     procedure ListBox1Click(Sender: TObject);
  74.     procedure Select1Click(Sender: TObject);
  75.     procedure FormDestroy(Sender: TObject);
  76.     procedure BSetWatchClick(Sender: TObject);
  77.     procedure bSetPropInspectClick(Sender: TObject);
  78.     procedure bSetEditWindowClick(Sender: TObject);
  79.     procedure OnExit(Sender: TObject);
  80.     procedure ECurItemKeyDown(Sender: TObject; var Key: Word;
  81.       Shift: TShiftState);
  82.   private
  83.     IniOpen: Boolean;
  84.     FileName: String;
  85.     IniFile: TIniFile;
  86.     CurSection: string;
  87.     CurItem: string;
  88.     procedure GetSections;
  89.     procedure ShowSection(S: string);
  90.     function OkToSet: Boolean;
  91.   end;
  92.  
  93. var
  94.   Form1: TForm1;
  95.  
  96. implementation
  97.  
  98. uses
  99.   StrBox, SysUtils;
  100.  
  101. {$R *.DFM}
  102.  
  103. procedure TForm1.ECurItemKeyDown(Sender: TObject; var Key: Word;
  104.   Shift: TShiftState);
  105. var
  106.   Value: string;
  107. begin
  108.   if Key = vk_F2 then begin
  109.     Value := ECurItem.Text;
  110.     IniFile.WriteString(CurSection, CurItem, Value);
  111.     ShowSection(CurSection);
  112.   end;
  113. end;
  114.  
  115. procedure TForm1.ShowSection(S: String);
  116. var
  117.   S1, Value: string;
  118.   i: Integer;
  119.   SL: TStringList;
  120. begin
  121.   ListBox1.Clear;
  122.   IniFile.ReadSectionValues(S, ListBox1.Items);
  123. end;
  124.  
  125. procedure TForm1.CurSectionClick(Sender: TObject);
  126. begin
  127.   if not IniOpen then Exit;
  128.   CurSection := SectionBox.Items.Strings[SectionBox.ItemIndex];
  129.   ECurItem.Text := '';
  130.   ShowSection(CurSection);
  131. end;
  132.  
  133. procedure TForm1.FormCreate(Sender: TObject);
  134. var
  135.   a, b: Integer;
  136. begin
  137.   FileName := '';
  138.   IniOpen := False;
  139.   Caption := DefaultCaption;
  140.   a := GetSystemMetrics(sm_cxScreen);
  141.   b := GetSystemMetrics(sm_cyScreen);
  142.   if (A = 800) and (B = 600) then
  143.     CurScreen := sMedium
  144.   else if (A = 1024) and (B = 768) then
  145.     CurScreen := sLarge
  146.   else
  147.     CurScreen := sSmall;
  148. end;
  149.  
  150. procedure TForm1.ListBox1Click(Sender: TObject);
  151. var
  152.   Value: String;
  153. begin
  154.   if not IniOpen then Exit;
  155.   CurItem := ListBox1.Items.Strings[ListBox1.ItemIndex];
  156.   CurItem := StripLastToken(CurItem, '=');
  157.   Value := IniFile.ReadString(CurSection, CurItem, 'Foo');
  158.   ECurItem.Text := Value;
  159. end;
  160.  
  161. procedure TForm1.GetSections;
  162. var
  163.   F: System.Text;
  164.   S1, S: string;
  165.   SL: TStringList;
  166. begin
  167.   System.Assign(F, FileName);
  168.   System.Reset(F);
  169.   SL := TStringList.Create;
  170.   while not EOF(F) do begin
  171.     ReadLn(F, S);
  172.     S1 := ReverseStr(S);
  173.     if (S[1] = '[') and (S1[1] = ']') then begin
  174.       S := Shorten(S1, 1);
  175.       S := ReverseStr(S);
  176.       S := Shorten(S, 1);
  177.       SL.Add(S);
  178.     end;
  179.   end;
  180.   SectionBox.Items := SL;
  181.   SL.Free;
  182.   System.Close(F);
  183. end;
  184.  
  185. procedure TForm1.Select1Click(Sender: TObject);
  186. begin
  187.   if OpenDialog1.Execute then begin
  188.     IniOpen := True;
  189.     ListBox1.Clear;
  190.     SectionBox.Clear;
  191.     ECurItem.Text := '';
  192.     FileName := OpenDialog1.FileName;
  193.     Caption := DefaultCaption + ' - ' + FileName;
  194.     IniFile.Free;
  195.     IniFile := TIniFile.Create(FileName);
  196.     GetSections;
  197.   end;
  198. end;
  199.  
  200. procedure TForm1.FormDestroy(Sender: TObject);
  201. begin
  202.   IniFile.Free;
  203. end;
  204.  
  205. function ww(gdType: tGetDem): LongInt;
  206. begin
  207.   if CurScreen = sSmall then
  208.     case gdType of
  209.       gdLeft: ww := ww640x480[4];
  210.       gdTop: ww := ww640x480[5];
  211.       gdWidth: ww := ww640x480[6];
  212.       gdHeight: ww := ww640x480[7];
  213.     end
  214.   else if CurScreen = sMedium then
  215.     case gdType of
  216.       gdLeft: ww := ww800x600[4];
  217.       gdTop: ww := ww800x600[5];
  218.       gdWidth: ww := ww800x600[6];
  219.       gdHeight: ww := ww800x600[7];
  220.     end
  221.   else if CurScreen = sLarge then
  222.     case gdType of
  223.       gdLeft: ww := ww1024x768[4];
  224.       gdTop: ww := ww1024x768[5];
  225.       gdWidth: ww := ww1024x768[6];
  226.       gdHeight: ww := ww1024x768[7];
  227.     end;
  228. end;
  229.  
  230. function pi(gdType: tGetDem): LongInt;
  231. begin
  232.   if CurScreen = sSmall then
  233.     case gdType of
  234.       gdLeft: pi := pi640x480[4];
  235.       gdTop: pi := pi640x480[5];
  236.       gdWidth: pi := pi640x480[6];
  237.       gdHeight: pi := pi640x480[7];
  238.     end
  239.   else if CurScreen = sMedium then
  240.     case gdType of
  241.       gdLeft: pi := pi800x600[4];
  242.       gdTop: pi := pi800x600[5];
  243.       gdWidth: pi := pi800x600[6];
  244.       gdHeight: pi := pi800x600[7];
  245.     end
  246.   else if CurScreen = sLarge then
  247.     case gdType of
  248.       gdLeft: pi := pi1024x768[4];
  249.       gdTop: pi := pi1024x768[5];
  250.       gdWidth: pi := pi1024x768[6];
  251.       gdHeight: pi := pi1024x768[7];
  252.     end;
  253. end;
  254.  
  255. function ew(gdType: tGetDem): LongInt;
  256. begin
  257.   if CurScreen = sSmall then
  258.     case gdType of
  259.       gdLeft: ew := ew640x480[4];
  260.       gdTop: ew := ew640x480[5];
  261.       gdWidth: ew := ew640x480[6];
  262.       gdHeight: ew := ew640x480[7];
  263.     end
  264.   else if CurScreen = sMedium then
  265.     case gdType of
  266.       gdLeft: ew := ew800x600[4];
  267.       gdTop: ew := ew800x600[5];
  268.       gdWidth: ew := ew800x600[6];
  269.       gdHeight: ew := ew800x600[7];
  270.     end
  271.   else if CurScreen = sLarge then
  272.     case gdType of
  273.       gdLeft: ew := ew1024x768[4];
  274.       gdTop: ew := ew1024x768[5];
  275.       gdWidth: ew := ew1024x768[6];
  276.       gdHeight: ew := ew1024x768[7];
  277.     end;
  278. end;
  279.  
  280. function TForm1.OkToSet: Boolean;
  281. var
  282.   S: string;
  283. begin
  284.   OkToSet := False;
  285.   S := ExtractFileExt(FileName);
  286.   if UpperCase(S) = '.DSK' then
  287.     OkToSet := True
  288.   else
  289.     MessageDlg('DSK file not loaded', mtInformation, [mbOk], 0);
  290. end;
  291.  
  292. procedure TForm1.BSetWatchClick(Sender: TObject);
  293. var
  294.   Section: String;
  295. begin
  296.   if not OKToSet then Exit;
  297.   Section := 'WatchWindow';
  298.   IniFile.WriteInteger(Section, 'Create', 1);
  299.   IniFile.WriteInteger(Section, 'Visible', 1);
  300.   IniFile.WriteInteger(Section, 'State', 0);
  301.   IniFile.WriteInteger(Section, 'Left', 0);
  302.   IniFile.WriteInteger(Section, 'Top', ww(gdTop));
  303.   IniFile.WriteInteger(Section, 'Width', ww(gdWidth));
  304.   IniFile.WriteInteger(Section, 'Height', ww(gdHeight));
  305. end;
  306.  
  307. procedure TForm1.bSetPropInspectClick(Sender: TObject);
  308. var
  309.   Section: String;
  310. begin
  311.   if not OKToSet then Exit;
  312.   Section := 'PropertyInspector';
  313.   IniFile.WriteInteger(Section, 'Create', 1);
  314.   IniFile.WriteInteger(Section, 'Visible', 1);
  315.   IniFile.WriteInteger(Section, 'State', 0);
  316.   IniFile.WriteInteger(Section, 'Left', pi(gdLeft));
  317.   IniFile.WriteInteger(Section, 'Top', pi(gdTop));
  318.   IniFile.WriteInteger(Section, 'Width', pi(gdWidth));
  319.   IniFile.WriteInteger(Section, 'Height', pi(gdHeight));
  320. end;
  321.  
  322. procedure TForm1.bSetEditWindowClick(Sender: TObject);
  323. var
  324.   Section: String;
  325.   TempName: String;
  326. begin
  327.   if not OKToSet then Exit;
  328.   Section := 'EditWindow0';
  329.   IniFile.WriteInteger(Section, 'Create', 1);
  330.   IniFile.WriteInteger(Section, 'Visible', 1);
  331.   IniFile.WriteInteger(Section, 'State', 0);
  332.   IniFile.WriteInteger(Section, 'Left', ew(gdLeft));
  333.   IniFile.WriteInteger(Section, 'Top', ew(gdTop));
  334.   IniFile.WriteInteger(Section, 'Width', ew(gdWidth));
  335.   IniFile.WriteInteger(Section, 'Height', ew(gdHeight));
  336.   IniFile.WriteInteger(Section, 'ViewCount', 1);
  337.   IniFile.WriteInteger(Section, 'CurrentView', 1);
  338.   IniFile.WriteInteger(Section, 'View0', 0);
  339.   Section := 'View0';
  340.   IniFile.WriteInteger(Section, 'Module', 0);
  341.   IniFile.WriteInteger(Section, 'CursorX', 1);
  342.   IniFile.WriteInteger(Section, 'CursorY', 1);
  343.   IniFile.WriteInteger(Section, 'TopLine', 1);
  344.   IniFile.WriteInteger(Section, 'LeftCol', 1);
  345.   Section := 'Modules';
  346.   IniFile.WriteInteger(Section, 'Count', 1);
  347.   IniFile.WriteInteger(Section, 'EditWindowCount', 1);
  348.   TempName := FileName;
  349.   TempName[0] := Chr(Ord(TempName[0]) - 3);
  350.   TempName := TempName  + 'DPR';
  351.   IniFile.WriteString(Section, 'Module0', TempName);
  352. end;
  353.  
  354. procedure TForm1.OnExit(Sender: TObject);
  355. begin
  356.   Close;
  357. end;
  358.  
  359. end.
  360.