home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 January / Chip_1999-01_cd.bin / zkuste / delphi / QDB / VADDBOOK.ZIP / AB_Main.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-06-28  |  14.2 KB  |  493 lines

  1.  
  2. {*****************************************************************************}
  3. {                                                                             }
  4. {                          Address Book Demo                                  }
  5. {                                                                             }
  6. {                     illustrating the use of the                             }
  7. {                                                                             }
  8. {            QDB v2.10 Visual Components for Delphi 1, 2, & 3                 }
  9. {                                                                             }
  10. {        Copyright (c) 1995, 1996, 1997, 1998, Robert R. Marsh, S.J.          }
  11. {             & the British Province of the Society of Jesus                  }
  12. {                                                                             }
  13. {                                                                             }
  14. {       You may use this demonstration application and modify it in           }
  15. {       whatever way you choose. You may not, however, sell it for            }
  16. {       profit unless the changes you have made are substantial (i.e.,        }
  17. {       more than 50% new code), in which case I'd appreciate                 }
  18. {       receiving a copy of your new work.                                    }
  19. {                                                                             }
  20. {       If you like Address Book and find yourself using it please            }
  21. {       consider making a donation to your favorite charity.                  }
  22. {                                                                             }
  23. {    Users of Address Book must accept the following disclaimer of warranty:  }
  24. {                                                                             }
  25. {       Address Book is supplied as is. The author disclaims all warranties,  }
  26. {       expressed or implied, including, without limitation, the              }
  27. {       warranties of merchantability and of fitness for any purpose.         }
  28. {       The author assumes no liability for damages, direct or                }
  29. {       consequential, which may result from the use of Address Book.         }
  30. {                                                                             }
  31. {*****************************************************************************}
  32.  
  33. unit AB_Main;
  34.  
  35. interface
  36.  
  37. uses
  38. {$IFDEF WIN32}Windows, {$ELSE}WinTypes, WinProcs, {$ENDIF}
  39.   Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  40.   StdCtrls, QDBView, ExtCtrls, QDB, Buttons, Menus;
  41.  
  42. type
  43.   TAB_Form = class(TForm)
  44.     MainMenu: TMainMenu;
  45.     FileMenu: TMenuItem;
  46.     FileNewItem: TMenuItem;
  47.     FileOpenItem: TMenuItem;
  48.     FileCloseItem: TMenuItem;
  49.     FileSaveItem: TMenuItem;
  50.     N1: TMenuItem;
  51.     FileExitItem: TMenuItem;
  52.     SpeedBar: TPanel;
  53.     FileOpenSButton: TSpeedButton;
  54.     FileSaveSButton: TSpeedButton;
  55.     FileExitSButton: TSpeedButton;
  56.     FileNewSButton: TSpeedButton;
  57.     FileCloseSButton: TSpeedButton;
  58.     OpenDialog: TOpenDialog;
  59.     SaveDialog: TSaveDialog;
  60.     Panel1: TPanel;
  61.     Label7: TLabel;
  62.     KeyList: TListBox;
  63.     FilterBox: TEdit;
  64.     Label5: TLabel;
  65.     StatusLine: TPanel;
  66.     ButtonPanel: TPanel;
  67.     Image: TImage;
  68.     PostButton: TButton;
  69.     InsertButton: TButton;
  70.     AddressPanel: TPanel;
  71.     Label1: TLabel;
  72.     AddBox: TMemo;
  73.     IsBusiness: TCheckBox;
  74.     Label2: TLabel;
  75.     HPhone: TEdit;
  76.     Label3: TLabel;
  77.     WPhone: TEdit;
  78.     Label4: TLabel;
  79.     FPhone: TEdit;
  80.     Label6: TLabel;
  81.     E_Mail: TEdit;
  82.     Options1: TMenuItem;
  83.     OptCaseItem: TMenuItem;
  84.     OptCompressedItem: TMenuItem;
  85.     OptEncryptedItem: TMenuItem;
  86.     DeleteButton: TButton;
  87.     N2: TMenuItem;
  88.     ToolsMenu: TMenuItem;
  89.     ToolsPackItem: TMenuItem;
  90.     ToolsCompressItem: TMenuItem;
  91.     ToolsExpandItem: TMenuItem;
  92.     EditButton: TButton;
  93.     CancelButton: TButton;
  94.     Label8: TLabel;
  95.     WebURL: TEdit;
  96.     Label9: TLabel;
  97.     OPhone: TEdit;
  98.     Notes: TMemo;
  99.     Label10: TLabel;
  100.     Addresses: TQDBView;
  101.     procedure FormCreate(Sender: TObject);
  102.     procedure DisplayHint(Sender: TObject);
  103.     procedure FileNew(Sender: TObject);
  104.     procedure FileOpen(Sender: TObject);
  105.     procedure FileSave(Sender: TObject);
  106.     procedure FileClose(Sender: TObject);
  107.     procedure FileExit(Sender: TObject);
  108.     procedure PostButtonClick(Sender: TObject);
  109.     procedure InsertButtonClick(Sender: TObject);
  110.     procedure DeleteButtonClick(Sender: TObject);
  111.     procedure KeyListClick(Sender: TObject);
  112.     procedure OptCaseItemClick(Sender: TObject);
  113.     procedure OptCompressedItemClick(Sender: TObject);
  114.     procedure OptEncryptedItemClick(Sender: TObject);
  115.     procedure AddressesDemandPassword(Sender: TObject; var Password: TPassword);
  116.     procedure ToolsPackItemClick(Sender: TObject);
  117.     procedure ToolsCompressItemClick(Sender: TObject);
  118.     procedure ToolsExpandItemClick(Sender: TObject);
  119.     procedure Options1Click(Sender: TObject);
  120.     procedure AddressesWarnNoData(Sender: TObject);
  121.     procedure AddressPanelKey(Sender: TObject; var key: TKey);
  122.     procedure EditButtonClick(Sender: TObject);
  123.     procedure CancelButtonClick(Sender: TObject);
  124.     procedure AddressesFileAssigned(Sender: TObject);
  125.     procedure FilterBoxChange(Sender: TObject);
  126.   private
  127.     procedure ActivateControls;
  128.     procedure DeactivateControls;
  129.     procedure UpdateAddressPanel;
  130.     procedure UpdateControls;
  131.   end;
  132.  
  133. var
  134.   AB_Form: TAB_Form;
  135.  
  136. implementation
  137.  
  138. {$R *.DFM}
  139.  
  140. { Utility routine to invert a name & put the surname first }
  141. { e.g. Robert R. Marsh, S.J. => Marsh, Robert R.           }
  142. { This is used by the AddressPanel OnKey event-handler.       }
  143.  
  144. function NameToKey(Name: string): string;
  145.  
  146.   { return first position of a char after position n }
  147.   function PosCAfter(c: char; s: string; n: integer): integer;
  148.   var
  149.     i: integer;
  150.   begin
  151.     Result := 0;
  152.     for i := n + 1 to Length(s) do
  153.       if s[i] = c then
  154.       begin
  155.         Result := i;
  156.         Break;
  157.       end;
  158.   end;
  159.  
  160. {$IFNDEF WIN32}
  161.   procedure TrimLeft(var s: string);
  162.   begin
  163.     while s[1] = ' ' do Delete(s, 1, 1);
  164.   end;
  165.  
  166.   procedure TrimRight(var s: string);
  167.   begin
  168.     while s[length(s)] = ' ' do delete(s, length(s), 1);
  169.   end;
  170. {$ENDIF}
  171.  
  172. var
  173.   NewName: string;
  174.   CommaPos: Byte;
  175.   SpacePos: Byte;
  176.   LastSpacePos: Byte;
  177. begin
  178.   NewName := Name;
  179.   TrimLeft(NewName);
  180. { then remove anything after a comma, e.g. strip off the S.J.'s}
  181.   CommaPos := PosCAfter(',', NewName, 0);
  182.   if CommaPos <> 0 then
  183.     NewName := Copy(NewName, 1, CommaPos - 1);
  184. { now strip off trailing spaces and see if anything is left }
  185.   TrimRight(NewName);
  186.   if Length(NewName) = 0 then
  187.   begin
  188.     Result := '';
  189.     Exit;
  190.   end;
  191. { finally, find the last space in the name and take it to delimit the surname }
  192.   LastSpacePos := 1;
  193.   SpacePos := 1;
  194.   while SpacePos <> 0 do
  195.   begin
  196.     LastSpacePos := SpacePos;
  197.     SpacePos := PosCAfter(' ', NewName, SpacePos);
  198.   end;
  199. { then just invert the order }
  200.   if LastSpacePos = 1 then
  201.     Result := NewName
  202.   else
  203.     Result := Copy(NewName, LastSpacePos + 1, high(integer)) + ', ' + Copy(NewName, 1, LastSpacePos - 1);
  204. end;
  205.  
  206. const
  207.   BaseTitle = 'Address Book Demo';
  208.  
  209. procedure TAB_Form.FormCreate(Sender: TObject);
  210. begin
  211.   Application.OnHint := DisplayHint;
  212.   AB_Form.Caption := BaseTitle;
  213.   DeactivateControls;
  214. end;
  215.  
  216. procedure TAB_Form.DisplayHint(Sender: TObject);
  217. begin
  218.   StatusLine.Caption := Application.Hint;
  219. end;
  220.  
  221. procedure TAB_Form.FileNew(Sender: TObject);
  222. var
  223.   NewFileName: string;
  224. begin
  225.   if SaveDialog.Execute then
  226.   begin
  227.     NewFileName := SaveDialog.FileName;
  228.     if FileExists(NewFileName) then
  229.     begin
  230.       Addresses.FileName := '';
  231.       SysUtils.DeleteFile(NewFileName);
  232.     end;
  233.     Addresses.FileName := NewFileName;
  234.     AB_Form.Caption := BaseTitle + ' - ' + ExtractFileName(Addresses.FileName);
  235.     KeyList.Items.Clear;
  236.     UpdateAddressPanel;
  237.     ActivateControls;
  238.   end;
  239. end;
  240.  
  241. procedure TAB_Form.FileOpen(Sender: TObject);
  242. begin
  243.   if OpenDialog.Execute then
  244.   begin
  245.     Addresses.FileName := OpenDialog.FileName;
  246.     OptCaseItem.Checked := Addresses.KeyCaseSensitive;
  247.     Addresses.AssignKeyList(KeyList.Items);
  248.     if KeyList.Items.Count > 0 then
  249.     begin
  250.       KeyList.ItemIndex := 0;
  251.       UpdateAddressPanel;
  252.     end;
  253.     AB_Form.Caption := BaseTitle + ' - ' + ExtractFileName(Addresses.FileName);
  254.     ActivateControls;
  255.   end;
  256. end;
  257.  
  258. procedure TAB_Form.FileSave(Sender: TObject);
  259. begin
  260.   if Addresses.Ready then Addresses.Save;
  261. end;
  262.  
  263. procedure TAB_Form.FileClose(Sender: TObject);
  264. begin
  265.   if Addresses.Ready then
  266.   begin
  267.     Addresses.Save;
  268.     Addresses.FileName := '';
  269.     KeyList.Items.Clear;
  270.     AB_Form.Caption := BaseTitle;
  271.     DeactivateControls;
  272.   end;
  273. end;
  274.  
  275. procedure TAB_Form.FileExit(Sender: TObject);
  276. begin
  277.   if Addresses.Ready then
  278.   begin
  279.     Addresses.Save;
  280.     Addresses.FileName := '';
  281.   end;
  282.   Close;
  283. end;
  284.  
  285. procedure TAB_Form.ActivateControls;
  286. begin
  287.   PostButton.Enabled := true;
  288.   CancelButton.Enabled := true;
  289.   DeleteButton.Enabled := true;
  290.   EditButton.Enabled := true;
  291.   InsertButton.Enabled := true;
  292.   UpdateControls;
  293.   OptCaseItem.Enabled := true;
  294.   OptCaseItem.Checked := Addresses.KeyCaseSensitive;
  295.   OptEncryptedItem.Enabled := true;
  296. end;
  297.  
  298. procedure TAB_Form.DeactivateControls;
  299. begin
  300.   PostButton.Enabled := false;
  301.   CancelButton.Enabled := false;
  302.   DeleteButton.Enabled := false;
  303.   EditButton.Enabled := false;
  304.   InsertButton.Enabled := false;
  305.   OptCaseItem.Checked := false;
  306.   OptCaseItem.Enabled := false;
  307.   OptEncryptedItem.Enabled := false;
  308. end;
  309.  
  310. procedure TAB_Form.UpdateControls;
  311. begin
  312.   InsertButton.Enabled := not Addresses.Editing;
  313.   EditButton.Enabled := not Addresses.Editing;
  314.   PostButton.Enabled := Addresses.Editing or Addresses.Inserting;
  315.   DeleteButton.Enabled := (Addresses.Ready and (Addresses.Count > 0));
  316. end;
  317.  
  318. procedure TAB_Form.UpdateAddressPanel;
  319. var
  320.   CurrentKey: TKey;
  321. begin
  322. { Find which key is selected in the KeyList }
  323.   if (KeyList.Items.Count > 0) and (KeyList.ItemIndex >= 0) then
  324.     CurrentKey := KeyList.Items[KeyList.ItemIndex]
  325.   else
  326.     CurrentKey := '';
  327. { if the key is valid load the corresponding address }
  328.   if Addresses.ExactMatch(CurrentKey) then
  329.     Addresses.Refresh
  330.   else
  331.     Addresses.Clear;
  332. end;
  333.  
  334. { The QDBPanel.Post method needs this event handled to }
  335. { provide a key by which to store the panel item. Here }
  336. { we invert the name line of the address.              }
  337.  
  338. procedure TAB_Form.AddressPanelKey(Sender: TObject; var key: TKey);
  339. begin
  340.   if IsBusiness.Checked then
  341.     key := AddBox.Lines[0]
  342.   else
  343.     key := NameToKey(AddBox.Lines[0]);
  344. end;
  345.  
  346. procedure TAB_Form.AddressesDemandPassword(Sender: TObject;
  347.   var Password: TPassword);
  348. var
  349.   NewPassword: string;
  350. begin
  351.   NewPassword := Password;
  352.   if InputQuery('Addresses', 'Enter the password:', NewPassword) then
  353.     Password := Copy(NewPassword, 1, SizeOf(TPassword))
  354.   else
  355.     Password := '';
  356. end;
  357.  
  358. procedure TAB_Form.PostButtonClick(Sender: TObject);
  359. begin
  360.   Addresses.Post;
  361.   UpdateControls;
  362.   Addresses.AssignKeyList(KeyList.Items);
  363.   KeyList.ItemIndex := KeyList.Items.IndexOf(Addresses.Key);
  364. end;
  365.  
  366. procedure TAB_Form.InsertButtonClick(Sender: TObject);
  367. begin
  368.   Addresses.Insert;
  369.   UpdateControls;
  370. end;
  371.  
  372. procedure TAB_Form.CancelButtonClick(Sender: TObject);
  373. begin
  374.   Addresses.Cancel;
  375.   UpdateControls;
  376. end;
  377.  
  378. procedure TAB_Form.EditButtonClick(Sender: TObject);
  379. begin
  380.   Addresses.Edit;
  381.   UpdateControls;
  382. end;
  383.  
  384. procedure TAB_Form.DeleteButtonClick(Sender: TObject);
  385. var
  386.   ThisItem: integer;
  387.   KeyToGo: string;
  388. begin
  389.   ThisItem := KeyList.ItemIndex;
  390.   if ThisItem <> -1 then
  391.   begin
  392.     KeyToGo := KeyList.Items[ThisItem];
  393.     if Addresses.ExactMatch(KeyToGo) then
  394.     begin
  395.       Addresses.Delete;
  396.       UpdateControls;
  397.       Addresses.AssignKeyList(KeyList.Items);
  398.     end;
  399.   end;
  400.   if ThisItem >= KeyList.Items.Count then
  401.   begin
  402.     dec(ThisItem);
  403.   end;
  404.   KeyList.ItemIndex := ThisItem;
  405.   UpdateAddressPanel;
  406. end;
  407.  
  408. procedure TAB_Form.KeyListClick(Sender: TObject);
  409. begin
  410.   UpdateAddressPanel;
  411. end;
  412.  
  413. procedure TAB_Form.OptCaseItemClick(Sender: TObject);
  414. begin
  415.   if Addresses.Ready then
  416.   begin
  417.     OptCaseItem.Checked := not OptCaseItem.Checked;
  418.     Addresses.KeyCaseSensitive := OptCaseItem.Checked;
  419.     Addresses.AssignKeyList(KeyList.Items);
  420.   end;
  421. end;
  422.  
  423. procedure TAB_Form.OptCompressedItemClick(Sender: TObject);
  424. begin
  425.   if Addresses.Ready then
  426.   begin
  427.     OptCompressedItem.Checked := not OptCompressedItem.Checked;
  428.     Addresses.Compression := OptCaseItem.Checked;
  429.   end;
  430. end;
  431.  
  432. procedure TAB_Form.OptEncryptedItemClick(Sender: TObject);
  433. var
  434.   CurrentPassword: string;
  435. begin
  436.   CurrentPassword := Addresses.PassWord;
  437.   CurrentPassword := InputBox('Addresses', 'Enter Password:', CurrentPassword);
  438.   Addresses.PassWord := CurrentPassword;
  439. end;
  440.  
  441. procedure TAB_Form.ToolsPackItemClick(Sender: TObject);
  442. begin
  443.   Addresses.Pack;
  444. end;
  445.  
  446. procedure TAB_Form.ToolsCompressItemClick(Sender: TObject);
  447. begin
  448.   Addresses.Compress;
  449. end;
  450.  
  451. procedure TAB_Form.ToolsExpandItemClick(Sender: TObject);
  452. begin
  453.   Addresses.Expand;
  454. end;
  455.  
  456. procedure TAB_Form.Options1Click(Sender: TObject);
  457. begin
  458.   OptCompressedItem.Checked := Addresses.Compression;
  459. end;
  460.  
  461. procedure TAB_Form.AddressesWarnNoData(Sender: TObject);
  462. begin
  463. { This event-handler does nothing -- except suppress the }
  464. { exception which would occur in its absence.            }
  465. { Note that there has to be something here even if it's  }
  466. { only a comment.                                        }
  467. end;
  468.  
  469. { Whenever a new filename is set we check to see if it }
  470. { matches the panel's layout. An empty file gets       }
  471. { marked with the current layout.                      }
  472.  
  473. procedure TAB_Form.AddressesFileAssigned(Sender: TObject);
  474. var
  475.   NewFileName: string;
  476. begin
  477.   NewFileName := Addresses.FileName;
  478. end;
  479.  
  480. procedure TAB_Form.FilterBoxChange(Sender: TObject);
  481. begin
  482.   try
  483.     Addresses.Filter := FilterBox.Text;
  484.     Addresses.AssignKeyList(KeyList.Items);
  485.     UpdateAddressPanel;
  486.   except
  487.     ShowMessage('The filter expression is invalid');
  488.   end;
  489. end;
  490.  
  491. end.
  492.  
  493.