home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue66 / Shell / FileAssocU.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-11-14  |  5.9 KB  |  230 lines

  1. unit FileAssocU;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   DdeMan, StdCtrls, ComCtrls, Menus, ToolWin, ActnList, ImgList, StdActns;
  8.  
  9. type
  10.   TMainForm = class(TForm)
  11.     System: TDdeServerConv;
  12.     MainMenu1: TMainMenu;
  13.     ActionList1: TActionList;
  14.     ImageList1: TImageList;
  15.     actNew: TAction;
  16.     actOpen: TAction;
  17.     actClose: TAction;
  18.     actPrint: TAction;
  19.     actExit: TAction;
  20.     ToolBar1: TToolBar;
  21.     File1: TMenuItem;
  22.     New1: TMenuItem;
  23.     Close1: TMenuItem;
  24.     Close2: TMenuItem;
  25.     N1: TMenuItem;
  26.     N2: TMenuItem;
  27.     Print1: TMenuItem;
  28.     Exit1: TMenuItem;
  29.     ToolButton1: TToolButton;
  30.     ToolButton2: TToolButton;
  31.     ToolButton3: TToolButton;
  32.     ToolButton4: TToolButton;
  33.     ToolButton5: TToolButton;
  34.     ToolButton6: TToolButton;
  35.     ToolButton7: TToolButton;
  36.     dlgOpenFile: TOpenDialog;
  37.     Edit1: TMenuItem;
  38.     EditCopy1: TEditCopy;
  39.     EditCut1: TEditCut;
  40.     EditDelete1: TEditDelete;
  41.     EditPaste1: TEditPaste;
  42.     EditSelectAll1: TEditSelectAll;
  43.     EditUndo1: TEditUndo;
  44.     Undo1: TMenuItem;
  45.     N3: TMenuItem;
  46.     Cut1: TMenuItem;
  47.     Copy1: TMenuItem;
  48.     Paste1: TMenuItem;
  49.     Delete1: TMenuItem;
  50.     SelectAll1: TMenuItem;
  51.     ToolButton8: TToolButton;
  52.     ToolButton9: TToolButton;
  53.     ToolButton10: TToolButton;
  54.     ToolButton11: TToolButton;
  55.     ToolButton12: TToolButton;
  56.     ToolButton13: TToolButton;
  57.     ToolButton14: TToolButton;
  58.     Window1: TMenuItem;
  59.     WindowArrange1: TWindowArrange;
  60.     WindowCascade1: TWindowCascade;
  61.     WindowMinimizeAll1: TWindowMinimizeAll;
  62.     WindowTileHorizontal1: TWindowTileHorizontal;
  63.     WindowTileVertical1: TWindowTileVertical;
  64.     Arrange1: TMenuItem;
  65.     Cascade1: TMenuItem;
  66.     MinimizeAll1: TMenuItem;
  67.     N4: TMenuItem;
  68.     TileHorizontally1: TMenuItem;
  69.     MinimizeAll2: TMenuItem;
  70.     procedure SystemExecuteMacro(Sender: TObject; Msg: TStrings);
  71.     procedure actNewExecute(Sender: TObject);
  72.     procedure actOpenExecute(Sender: TObject);
  73.     procedure actCloseExecute(Sender: TObject);
  74.     procedure actCloseUpdate(Sender: TObject);
  75.     procedure actPrintExecute(Sender: TObject);
  76.     procedure actPrintUpdate(Sender: TObject);
  77.     procedure actExitExecute(Sender: TObject);
  78.   private
  79.     { Private declarations }
  80.   public
  81.     { Public declarations }
  82.   end;
  83.  
  84. var
  85.   MainForm: TMainForm;
  86.  
  87. implementation
  88.  
  89. uses FileAssocU2;
  90.  
  91. {$R *.DFM}
  92.  
  93. //Turn string containing possibly many commands in
  94. //square brackets into multiple strings, each
  95. //containing one command, without square brackets
  96. procedure MassageCmds(Cmds: TStrings);
  97. var
  98.   S: String;
  99.   OpenCmd, CloseCmd: Integer;
  100. begin
  101.   S := Trim(Cmds.Text);
  102.   Cmds.Clear;
  103.   while Length(S) > 0 do
  104.   begin
  105.     OpenCmd := Pos('[', S);
  106.     CloseCmd := Pos(']', S);
  107.     if (OpenCmd < CloseCmd) and (OpenCmd >= 1) then
  108.     begin
  109.       Cmds.Add(Trim(Copy(S, OpenCmd+1, CloseCmd-OpenCmd-1)));
  110.       Delete(S, OpenCmd, CloseCmd-OpenCmd+1);
  111.       S := Trim(S)
  112.     end
  113.     else
  114.       Break
  115.   end;
  116. end;
  117.  
  118. type
  119.   TCommandType = (ctNone, ctOpen, ctPrint, ctClose, ctExit);
  120.  
  121. var
  122.   Commands: array[TCommandType] of String = ('', 'Open', 'Print', 'Close', 'Exit');
  123.  
  124. //Return a TCommandType value corresponding to a specified command string
  125. function StrToCommand(const Cmd: String): TCommandType;
  126. var
  127.   Idx: TCommandType;
  128. begin
  129.   for Idx := Succ(Low(TCommandType)) to High(TCommandType) do
  130.     if CompareText(Cmd, Commands[Idx]) = 0 then
  131.     begin
  132.       Result := Idx;
  133.       Exit
  134.     end;
  135.   Result := ctNone;
  136.   MessageDlg(Format('Unknown DDE command "%s"', [Cmd]),
  137.     mtError, [mbCancel], 0)
  138. end;
  139.  
  140. //For a given command string, return the command
  141. //type and specified filename, if present
  142. function GetCommandAndParameter(CmdText: String; var Command: TCommandType;
  143.   var Parameter: String): Boolean;
  144. var
  145.   OpenParens, CloseParens: Integer;
  146. begin
  147.   Result := True;
  148.   OpenParens := Pos('(', CmdText);
  149.   CloseParens := Pos(')', CmdText);
  150.   if (OpenParens < CloseParens) and (OpenParens > 1) then
  151.   begin
  152.     Command := StrToCommand(Trim(Copy(CmdText, 1, OpenParens - 1)));
  153.     if Command = ctNone then
  154.       Result:= False
  155.     else
  156.     begin
  157.       Parameter := Copy(CmdText, OpenParens+1, CloseParens-OpenParens-1);
  158.       Parameter := Trim(StringReplace(Parameter, '"', '', [rfReplaceAll]));
  159.     end
  160.   end
  161. end;
  162.  
  163. procedure TMainForm.SystemExecuteMacro(Sender: TObject; Msg: TStrings);
  164. var
  165.   I: Integer;
  166.   Cmd: TCommandType;
  167.   Parameter: String;
  168. begin
  169.   //Here is where we parse the DDE Message (or macro) and act on its commands
  170.   MassageCmds(Msg);
  171.   for I := 0 to Msg.Count - 1 do
  172.     if GetCommandAndParameter(Msg[I], Cmd, Parameter) then
  173.     begin
  174.       case Cmd of
  175.         ctOpen:
  176.         begin
  177.           dlgOpenFile.FileName := Parameter;
  178.           actOpen.Execute
  179.         end;
  180.         ctPrint: actPrint.Execute;
  181.         ctClose: actClose.Execute;
  182.         ctExit: actExit.Execute
  183.       end
  184.     end
  185. end;
  186.  
  187. procedure TMainForm.actNewExecute(Sender: TObject);
  188. begin
  189.   TChildForm.Create(Application, '')
  190. end;
  191.  
  192. procedure TMainForm.actOpenExecute(Sender: TObject);
  193. begin
  194.   if dlgOpenFile.FileName = '' then
  195.   begin
  196.     if not dlgOpenFile.Execute then
  197.       Exit
  198.   end;
  199.   TChildForm.Create(Application, dlgOpenFile.FileName);
  200.   dlgOpenFile.FileName := ''
  201. end;
  202.  
  203. procedure TMainForm.actCloseExecute(Sender: TObject);
  204. begin
  205.   ActiveMDIChild.Close
  206. end;
  207.  
  208. procedure TMainForm.actCloseUpdate(Sender: TObject);
  209. begin
  210.   actClose.Enabled := Assigned(ActiveMDIChild)
  211. end;
  212.  
  213. procedure TMainForm.actPrintExecute(Sender: TObject);
  214. begin
  215.   ShowMessageFmt('Printing "%s" not supported',
  216.     [(ActiveMDIChild as TChildForm).FileName])
  217. end;
  218.  
  219. procedure TMainForm.actPrintUpdate(Sender: TObject);
  220. begin
  221.   actPrint.Enabled := Assigned(ActiveMDIChild)
  222. end;
  223.  
  224. procedure TMainForm.actExitExecute(Sender: TObject);
  225. begin
  226.   Close
  227. end;
  228.  
  229. end.
  230.