home *** CD-ROM | disk | FTP | other *** search
/ PC Open 19 / pcopen19.iso / Zipped / CALMIR21.ZIP / SOURCE.ZIP / SRC / SYSPROP.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-02-20  |  15.4 KB  |  552 lines

  1. {**************************************************************************}
  2. {                                                                          }
  3. {    Calmira shell for Microsoft« Windows(TM) 3.1                          }
  4. {    Source Release 2.1                                                    }
  5. {    Copyright (C) 1997-1998 Li-Hsin Huang                                 }
  6. {                                                                          }
  7. {    This program is free software; you can redistribute it and/or modify  }
  8. {    it under the terms of the GNU General Public License as published by  }
  9. {    the Free Software Foundation; either version 2 of the License, or     }
  10. {    (at your option) any later version.                                   }
  11. {                                                                          }
  12. {    This program is distributed in the hope that it will be useful,       }
  13. {    but WITHOUT ANY WARRANTY; without even the implied warranty of        }
  14. {    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         }
  15. {    GNU General Public License for more details.                          }
  16. {                                                                          }
  17. {    You should have received a copy of the GNU General Public License     }
  18. {    along with this program; if not, write to the Free Software           }
  19. {    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             }
  20. {                                                                          }
  21. {**************************************************************************}
  22.  
  23. unit Sysprop;
  24.  
  25. interface
  26.  
  27. uses WinTypes, WinProcs, Classes, Graphics, Forms, Controls, Buttons,
  28.   StdCtrls, ExtCtrls, Chklist, TabNotBk, Grids, StylSped, Outline, Spin,
  29.   Scrtree, Dialogs, Settings, CalForm;
  30.  
  31. type
  32.   TSysPropDlg = class(TCalForm)
  33.     OKBtn: TBitBtn;
  34.     CancelBtn: TBitBtn;
  35.     Notebook: TTabbedNotebook;
  36.     Bevel1: TBevel;
  37.     CapEdit: TEdit;
  38.     Label2: TLabel;
  39.     PrefList: TCheckList;
  40.     Label1: TLabel;
  41.     Element: TComboBox;
  42.     ColorPick: TComboBox;
  43.     Bevel2: TBevel;
  44.     CustomBtn: TStyleSpeed;
  45.     Sample: TShape;
  46.     Label3: TLabel;
  47.     Label4: TLabel;
  48.     AddBtn: TStyleSpeed;
  49.     RemoveBtn: TStyleSpeed;
  50.     ModifyBtn: TStyleSpeed;
  51.     Bevel3: TBevel;
  52.     Label5: TLabel;
  53.     Label6: TLabel;
  54.     GridWidth: TSpinEdit;
  55.     GridHeight: TSpinEdit;
  56.     Label8: TLabel;
  57.     RowHeight: TSpinEdit;
  58.     Label7: TLabel;
  59.     Label9: TLabel;
  60.     Outline: TScrollTree;
  61.     SaveBtn: TStyleSpeed;
  62.     ColorDialog: TColorDialog;
  63.     DeviceList: TListBox;
  64.     Header: THeader;
  65.     RemoteEdit: TEdit;
  66.     ConnectLabel: TLabel;
  67.     PasswordLabel: TLabel;
  68.     PasswordEdit: TEdit;
  69.     Connect: TStyleSpeed;
  70.     Disconnect: TStyleSpeed;
  71.     RefreshNet: TStyleSpeed;
  72.     HelpBtn: TBitBtn;
  73.     HistoryCombo: TComboBox;
  74.     ClearHistBtn: TStyleSpeed;
  75.     Label12: TLabel;
  76.     Bevel4: TBevel;
  77.     procedure FormCreate(Sender: TObject);
  78.     procedure OKBtnClick(Sender: TObject);
  79.     procedure OutlineDrawItem(Control: TWinControl; Index: Integer;
  80.       Rect: TRect; State: TOwnerDrawState);
  81.     procedure AddBtnClick(Sender: TObject);
  82.     procedure RemoveBtnClick(Sender: TObject);
  83.     procedure ModifyBtnClick(Sender: TObject);
  84.     procedure OutlineClick(Sender: TObject);
  85.     procedure ColorPickChange(Sender: TObject);
  86.     procedure CustomBtnClick(Sender: TObject);
  87.     procedure FormDestroy(Sender: TObject);
  88.     procedure ElementChange(Sender: TObject);
  89.     procedure SaveBtnClick(Sender: TObject);
  90.     procedure NotebookChange(Sender: TObject; NewTab: Integer;
  91.       var AllowChange: Boolean);
  92.     procedure DeviceListDrawItem(Control: TWinControl; Index: Integer;
  93.       Rect: TRect; State: TOwnerDrawState);
  94.     procedure RefreshNetClick(Sender: TObject);
  95.     procedure HeaderSized(Sender: TObject; ASection, AWidth: Integer);
  96.     procedure ConnectClick(Sender: TObject);
  97.     procedure DeviceListClick(Sender: TObject);
  98.     procedure OutlineMouseUp(Sender: TObject; Button: TMouseButton;
  99.       Shift: TShiftState; X, Y: Integer);
  100.     procedure ClearHistBtnClick(Sender: TObject);
  101.     procedure OutlineDblClick(Sender: TObject);
  102.     procedure FormResize(Sender: TObject);
  103.   private
  104.     { Private declarations }
  105.     FilledOutline : Boolean;
  106.     Changes : TSettingChanges;
  107.     AdvancedChanged : Boolean;
  108.     SaveCustomColors : Boolean;
  109.     Histories : TStringList;
  110.     procedure EnumColorProc(const s: string);
  111.   public
  112.     { Public declarations }
  113.     procedure QueryNetwork;
  114.   end;
  115.  
  116. {
  117. var
  118.   SysPropDlg: TSysPropDlg;
  119. }
  120.  
  121.  
  122. implementation
  123.  
  124. uses Drives, MiscUtil, CompSys, SysUtils, Strings, IconSel, Locale, Embed;
  125.  
  126. {$R *.DFM}
  127.  
  128. procedure TSysPropDlg.EnumColorProc(const s: string);
  129. begin
  130.   ColorPick.Items.AddObject(
  131.     System.Copy(s, 3, Length(s)-2), TObject(StringToColor(s)));
  132. end;
  133.  
  134.  
  135. function GetNetConnection(LocalName : string): string;
  136. var
  137.   len: Word;
  138. begin
  139.   len := 254;
  140.   if WNetGetConnection(StringAsPChar(LocalName), @Result[1], @len) = WN_SUCCESS then
  141.     Result[0] := Chr(len)
  142.   else
  143.     Result := SSNoNetConnection;
  144. end;
  145.  
  146.  
  147. procedure TSysPropDlg.QueryNetwork;
  148. var
  149.   d: Char;
  150. begin
  151.   ShowHourGlass;
  152.   DeviceList.Clear;
  153.  
  154.   for d := 'A' to 'Z' do
  155.     DeviceList.Items.AddObject(
  156.       Format(SSDrive, [d, GetNetConnection(d + ':')]),
  157.       TObject(d));
  158.  
  159.   for d := '1' to '3' do
  160.     DeviceList.Items.AddObject(
  161.       Format(SSParallelPort, [d, GetNetConnection('LPT' + d)]),
  162.       TObject(d));
  163.  
  164.   Connect.Enabled := False;
  165.   Disconnect.Enabled := False;
  166. end;
  167.  
  168.  
  169. procedure TSysPropDlg.FormCreate(Sender: TObject);
  170. var
  171.   c: TCalColor;
  172.   i: Integer;
  173. begin
  174.   Changes := [];
  175.   Notebook.PageIndex := 0;
  176.   CapEdit.Text := ComputerCaption;
  177.  
  178.   Histories := TStringList.Create;
  179.  
  180.   ini.ReadSectionValues('History lists', Histories);
  181.   for i := 0 to Histories.Count-1 do
  182.     HistoryCombo.Items.Add(GetStrKey(Histories[i]));
  183.  
  184.   PrefList.SetData(
  185.     [ShowSplash, RestoreSys, SysWinQuit,
  186.      QueryQuit, MsgDialogSounds, 
  187.      TrackThumb, GlobalHotkeys, ShowBrowseBtns,
  188.      NotifyNoIcons, EnableDosScripts, EnableWinScripts,
  189.      ShowDailyTips, CompIconStart, DarkIconStretch]);
  190.  
  191.   for c := Low(TCalColor) to High(TCalColor) do
  192.     Element.Items.AddObject(
  193.       ini.ReadString('Colour descriptions', IntToStr(Ord(c)), ''), Pointer(Colors[c]));
  194.  
  195.   GetColorValues(EnumColorProc);
  196.  
  197.   GridWidth.Value := BrowseGrid.X;
  198.   GridHeight.Value := BrowseGrid.Y;
  199.   RowHeight.Value := LineHeight;
  200.  
  201.   ini.ReadSectionValues('Custom colors', ColorDialog.CustomColors);
  202. end;
  203.  
  204.  
  205. procedure TSysPropDlg.OKBtnClick(Sender: TObject);
  206. var
  207.   c: TCalColor;
  208. begin
  209.   ComputerCaption := CapEdit.Text;
  210.  
  211.   PrefList.GetData(
  212.     [@ShowSplash, @RestoreSys, @SysWinQuit,
  213.      @QueryQuit, @MsgDialogSounds, 
  214.      @TrackThumb, @GlobalHotKeys, @ShowBrowseBtns,
  215.      @NotifyNoIcons, @EnableDosScripts, @EnableWinScripts,
  216.      @ShowDailyTips, @CompIconStart, @DarkIconStretch]);
  217.  
  218.   for c:= Low(TCalColor) to High(TCalColor) do
  219.     Colors[c] := Longint(Element.Items.Objects[Integer(c)]);
  220.  
  221.   BrowseGrid.X := GridWidth.Value;
  222.   BrowseGrid.Y := GridHeight.Value;
  223.   LineHeight := RowHeight.Value;
  224.  
  225.   SaveSystemProp;
  226.  
  227.   if AdvancedChanged and (MsgDialogRes(SQuerySaveAdvanced,
  228.     mtConfirmation, [mbYes, mbNo], 0) = mrYes) then SaveBtn.Click;
  229.  
  230.   Include(Changes, scSystem);
  231.   if CustomBtn.Enabled then Include(Changes, scDisplay);
  232. end;
  233.  
  234.  
  235. procedure TSysPropDlg.OutlineDrawItem(Control: TWinControl; Index: Integer;
  236.   Rect: TRect; State: TOwnerDrawState);
  237. var
  238.   item: TOutlineNode;
  239.   x : Integer;
  240. begin
  241.   with Outline do begin
  242.     index := GetItem(0, Rect.Top);
  243.     item := Items[index];
  244.     x := Rect.Left + 4 + (item.Level-1) * 20;
  245.  
  246.     with Canvas do begin
  247.       FillRect(Rect);
  248.       TextOut(x + 19, Rect.Top+2, item.Text);
  249.  
  250.       if item.Level = 1 then
  251.         if item.Expanded then
  252.           Draw(x+1, Rect.Top+1, PictureOpen)
  253.         else
  254.           Draw(x+1, Rect.Top+1, PictureClosed)
  255.       else
  256.         Draw(x+1, Rect.Top+1, PictureLeaf);
  257.     end;
  258.   end;
  259. end;
  260.  
  261.  
  262. procedure TSysPropDlg.AddBtnClick(Sender: TObject);
  263. var
  264.   s: string;
  265.   item : TOutlineNode;
  266. begin
  267.   s := '';
  268.   if InputQuery(LoadStr(SAddAdvanced), LoadStr(SIdentValue), s) then begin
  269.     AdvancedChanged := True;
  270.     with Outline do
  271.       if SelectedItem > 0 then begin
  272.         item := Items[SelectedItem];
  273.         if item.Level = 1 then AddChild(SelectedItem, s)
  274.         else Insert(SelectedItem, s);
  275.       end
  276.       else
  277.         Add(0, s);
  278.   end;
  279. end;
  280.  
  281.  
  282. procedure TSysPropDlg.RemoveBtnClick(Sender: TObject);
  283. begin
  284.   with Outline do
  285.     if ChildNodeSelected then begin
  286.       Delete(SelectedItem);
  287.       AdvancedChanged := True;
  288.     end;
  289. end;
  290.  
  291.  
  292. procedure TSysPropDlg.ModifyBtnClick(Sender: TObject);
  293. var
  294.   s: string;
  295.   item : TOutlineNode;
  296. begin
  297.   with Outline do begin
  298.     if ChildNodeSelected then begin
  299.       item := Items[SelectedItem];
  300.       s := item.Text;
  301.       if InputQuery(LoadStr(SModifyAdvanced), LoadStr(SIdentValue), s) then begin
  302.         item.Text := s;
  303.         AdvancedChanged := True;
  304.       end;
  305.     end;
  306.   end;
  307. end;
  308.  
  309.  
  310. procedure TSysPropDlg.OutlineClick(Sender: TObject);
  311. begin
  312.   with Outline do
  313.   RemoveBtn.Enabled :=
  314.     (SelectedItem > 0) and (Items[SelectedItem].Level > 1);
  315.   ModifyBtn.Enabled := RemoveBtn.Enabled;
  316. end;
  317.  
  318.  
  319. procedure TSysPropDlg.ColorPickChange(Sender: TObject);
  320. var
  321.   c: TColor;
  322. begin
  323.   with ColorPick do c := TColor(Items.Objects[ItemIndex]);
  324.   with Element do Items.Objects[ItemIndex] := TObject(c);
  325.   Sample.Brush.Color := c;
  326. end;
  327.  
  328.  
  329. procedure TSysPropDlg.CustomBtnClick(Sender: TObject);
  330. var
  331.   c: TColor;
  332. begin
  333.   ColorDialog.Color := Sample.Brush.Color;
  334.   if ColorDialog.Execute then begin
  335.     c := ColorDialog.Color;
  336.     with ColorPick do ItemIndex := Items.IndexOfObject(TObject(c));
  337.     with Element do Items.Objects[ItemIndex] := TObject(c);
  338.     Sample.Brush.Color := c;
  339.     SaveCustomColors := True;
  340.   end;
  341. end;
  342.  
  343.  
  344. procedure TSysPropDlg.FormDestroy(Sender: TObject);
  345. begin
  346.   Histories.Free;
  347.   if SaveCustomColors then
  348.     ini.WriteSectionValues('Custom colors', ColorDialog.CustomColors);
  349.   if Changes <> [] then AnnounceSettingsChanged(Changes);
  350. end;
  351.  
  352.  
  353. procedure TSysPropDlg.ElementChange(Sender: TObject);
  354. var
  355.   c: TColor;
  356. begin
  357.   with Element do c := TColor(Items.Objects[ItemIndex]);
  358.   with ColorPick do begin
  359.     Enabled := True;
  360.     ItemIndex := Items.IndexOfObject(TObject(c));
  361.   end;
  362.   CustomBtn.Enabled := True;
  363.   Sample.Brush.Color := c;
  364. end;
  365.  
  366.  
  367.  
  368. procedure TSysPropDlg.SaveBtnClick(Sender: TObject);
  369. var
  370.   i: Longint;
  371. begin
  372.   ShowHourglass;
  373.   with ini do
  374.     for i := 1 to Outline.ItemCount do
  375.       with Outline.Items[i] do
  376.         if Level = 1 then EraseSection(Text)
  377.         else WriteString(Parent.Text, GetStrKey(Text), GetStrValue(Text));
  378.  
  379.   Include(Changes, scINIFile);
  380.   AdvancedChanged := False;
  381. end;
  382.  
  383.  
  384. procedure TSysPropDlg.NotebookChange(Sender: TObject; NewTab: Integer;
  385.   var AllowChange: Boolean);
  386. var
  387.   sections, strings: TStringList;
  388.   i, j : Integer;
  389.   node : Longint;
  390.   s : string[127];
  391. begin
  392.   if (NewTab = 3) and not FilledOutline then begin
  393.     ShowHourGlass;
  394.     sections := TUniqueStrings.Create;
  395.     ini.ReadStrings('Advanced', sections);
  396.     strings := TStringList.Create;
  397.  
  398.     try
  399.       with Outline do begin
  400.         for i := 0 to sections.Count-1 do Add(0, sections[i]);
  401.  
  402.         BeginUpdate;
  403.         for i := 0 to sections.Count-1 do begin
  404.           s := sections[i];
  405.           node := GetTextItem(s);
  406.           strings.Clear;
  407.           ini.ReadSectionValues(s, strings);
  408.           for j := 0 to strings.Count-1 do AddChild(node, strings[j]);
  409.         end;
  410.         EndUpdate;
  411.         Canvas.Font.Assign(Font);
  412.         ThumbTracking := TrackThumb;
  413.         Click;
  414.       end;
  415.     finally
  416.       sections.Free;
  417.       strings.Free;
  418.       FilledOutline := True;
  419.     end;
  420.   end
  421.  
  422.   else if (NewTab = 2) and (DeviceList.Items.Count = 0) then
  423.     QueryNetwork;
  424.  
  425.   FreePageHandles(Notebook);
  426. end;
  427.  
  428.  
  429. procedure TSysPropDlg.DeviceListDrawItem(Control: TWinControl;
  430.   Index: Integer; Rect: TRect; State: TOwnerDrawState);
  431. var
  432.   local : string[31];
  433.   remote : string;
  434. begin
  435.   with DeviceList do begin
  436.     remote := '';
  437.     Unformat(Items[Index], '%s¼%s', [@local, 31, @remote, 255]);
  438.     Canvas.FillRect(Rect);
  439.     Inc(Rect.Top);
  440.     Canvas.TextOut(Rect.Left + 2, Rect.Top, local);
  441.     Canvas.TextOut(Header.SectionWidth[0], Rect.Top, remote);
  442.   end;
  443. end;
  444.  
  445.  
  446. procedure TSysPropDlg.RefreshNetClick(Sender: TObject);
  447. begin
  448.   QueryNetwork;
  449. end;
  450.  
  451.  
  452. procedure TSysPropDlg.HeaderSized(Sender: TObject; ASection,
  453.   AWidth: Integer);
  454. begin
  455.   DeviceList.Invalidate;
  456. end;
  457.  
  458.  
  459. function NetErrorMessage(n: Word): Word;
  460. begin
  461.   case n of
  462.     WN_SUCCESS       : Result := SNetSuccess;
  463.     WN_NOT_SUPPORTED : Result := SNetNotSupported;
  464.     WN_OUT_OF_MEMORY : Result := SOutOfMemory;
  465.     WN_NET_ERROR     : Result := SNetError;
  466.     WN_BAD_POINTER   : Result := SBadPointer;
  467.     WN_BAD_NETNAME   : Result := SBadNetworkResource;
  468.     WN_BAD_LOCALNAME : Result := SBadLocalName;
  469.     WN_BAD_PASSWORD  : Result := SInvalidPassword;
  470.     WN_ACCESS_DENIED : Result := SAccessDenied;
  471.     WN_OPEN_FILES    : Result := SFilesWereOpen;
  472.     WN_ALREADY_CONNECTED : Result := SAlreadyConnected;
  473.     else Result := SNetOperationFailed;
  474.   end;
  475. end;
  476.  
  477.  
  478. procedure TSysPropDlg.ConnectClick(Sender: TObject);
  479. var
  480.   local : string[7];
  481.   remote, password: string;
  482.   result : Word;
  483. begin
  484.   ShowHourglass;
  485.   with DeviceList do
  486.     if ItemIndex > -1 then begin
  487.       { Get the local device name }
  488.       local := 'X';
  489.       local[1] := Char(Items.Objects[ItemIndex]);
  490.       if local[1] in ['1'..'3'] then local := 'LPT' + local
  491.       else AppendStr(local, ':');
  492.  
  493.       if Sender = Connect then begin
  494.         { attempt connection }
  495.         remote := RemoteEdit.Text;
  496.         password := PasswordEdit.Text;
  497.         Result := WNetAddConnection(StringAsPChar(remote), StringAsPChar(password),
  498.           StringAsPchar(local));
  499.       end
  500.       else
  501.         { attempt disconnection }
  502.         Result := WNetCancelConnection(StringAsPChar(local),
  503.           MsgDialogRes(SQueryCloseOpenFiles, mtWarning, [mbYes, mbNo], 0) = mrYes);
  504.  
  505.       if Result = WN_SUCCESS then QueryNetwork
  506.       else MsgDialogRes(NetErrorMessage(Result), mtError, [mbOK], 0);
  507.       Include(Changes, scDevices);
  508.       DetectDrives;
  509.   end;
  510. end;
  511.  
  512.  
  513. procedure TSysPropDlg.DeviceListClick(Sender: TObject);
  514. begin
  515.   Connect.Enabled := True;
  516.   Disconnect.Enabled := True;
  517. end;
  518.  
  519. procedure TSysPropDlg.OutlineMouseUp(Sender: TObject; Button: TMouseButton;
  520.   Shift: TShiftState; X, Y: Integer);
  521. var
  522.   i: Longint;
  523. begin
  524.   if x < 22 then
  525.   with Outline do begin
  526.     i := GetItem(X, Y);
  527.     if i > 0 then with Items[i] do
  528.       if Level = 1 then Expanded := not Expanded;
  529.   end;
  530. end;
  531.  
  532. procedure TSysPropDlg.ClearHistBtnClick(Sender: TObject);
  533. begin
  534.   ini.EraseSection(Histories.Values[HistoryCombo.Text]);
  535. end;
  536.  
  537. procedure TSysPropDlg.OutlineDblClick(Sender: TObject);
  538. begin
  539.   ModifyBtn.Click;
  540. end;
  541.  
  542. procedure TSysPropDlg.FormResize(Sender: TObject);
  543. begin
  544.   StretchShift([Notebook, Outline, DeviceList], [stWidth, stHeight]);
  545.   StretchShift([PrefList, Header, Bevel1, Bevel2, Bevel3, Bevel4], [stWidth]);
  546.   StretchShift([RefreshNet, Connect, Disconnect, AddBtn,
  547.     RemoveBtn, ModifyBtn, SaveBtn, OKBtn, CancelBtn, HelpBtn,
  548.     RemoteEdit, PasswordEdit, ConnectLabel, PasswordLabel], [stLeft, stTop]);
  549. end;
  550.  
  551. end.
  552.