home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 February / DPPCPRO0299.ISO / February / Delphi / Install / DATA.Z / DLG.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-06-11  |  11.3 KB  |  444 lines

  1.  
  2. unit Dlg;
  3.  
  4. interface
  5.  
  6. uses
  7.   SysUtils, Windows, Messages, Classes, Graphics, Controls,
  8.   Forms, Dialogs, ExtCtrls, StdCtrls, Buttons, ToolIntf, ComCtrls;
  9.  
  10. type
  11.  
  12.   { These are the set of flags which determine the type of dialog to create }
  13.   TDlgAttr = (daNothing, daMultPg, daBtnsH, daBtnsV);
  14.   TDlgAttrs = set of TDlgAttr;
  15.  
  16.   TDlgExpert = class(TForm)
  17.     Sample: TPaintBox;
  18.     CancelBtn: TButton;
  19.     PrevButton: TButton;
  20.     NextButton: TButton;
  21.     PageControl: TPageControl;
  22.     Style: TTabSheet;
  23.     Label1: TLabel;
  24.     rbSinglePage: TRadioButton;
  25.     rbMultPg: TRadioButton;
  26.     Pages: TTabSheet;
  27.     Label3: TLabel;
  28.     PageNames: TMemo;
  29.     Buttons: TTabSheet;
  30.     Label2: TLabel;
  31.     RadioButton1: TRadioButton;
  32.     rbBtnsV: TRadioButton;
  33.     rbBtnsH: TRadioButton;
  34.     procedure SamplePaint(Sender: TObject);
  35.     procedure FormCreate(Sender: TObject);
  36.     procedure FormDestroy(Sender: TObject);
  37.     procedure StyleClick(Sender: TObject);
  38.     procedure BtnClick(Sender: TObject);
  39.     procedure CancelClick(Sender: TObject);
  40.     procedure PrevClick(Sender: TObject);
  41.     procedure NextClick(Sender: TObject);
  42.   private
  43.     { Private declarations }
  44.     Definition: TDlgAttrs;
  45.     DrawBitmap: TBitmap;
  46.     SourceBuffer: PChar;
  47.     procedure RefreshButtons;
  48.     procedure FmtWrite(Stream: TStream; Fmt: PChar; const Args: array of const);
  49.     function DoFormCreation(const FormIdent: string): TForm;
  50.     function CreateSource(const UnitIdent, FormIdent: string): TMemoryStream;
  51.     function CreateForm(const FormIdent: string): TMemoryStream;
  52.   public
  53.     { Public declarations }
  54.   end;
  55.  
  56. procedure DialogExpert(ToolServices: TIToolServices);
  57. var
  58.   DlgExpert: TDlgExpert;
  59.  
  60. implementation
  61.  
  62. uses Proxies, VirtIntf, IStreams, ExConst;
  63.  
  64. {$R *.DFM}
  65.  
  66. const
  67.   { page numbers }
  68.   pgStyle       = 0;  { multi vs. single page dialog }
  69.   pgPages       = 1;  { page names }
  70.   pgButtons     = 2;  { button layouts }
  71.  
  72.   SourceBufferSize = 1024;
  73.  
  74.  
  75. { TDlgExpert }
  76.  
  77. { Paint the sample pane based on the currently selected options }
  78. procedure TDlgExpert.SamplePaint(Sender: TObject);
  79. var
  80.   X, Y: Integer;
  81. begin
  82.   { always paint the background dialog }
  83.   DrawBitmap.Handle := LoadBitmap(HInstance, 'DIALOG');
  84.   Sample.Canvas.Draw(0, 0, DrawBitmap);
  85.  
  86.   if daMultPg in Definition then
  87.   begin
  88.     DrawBitmap.Handle := LoadBitmap(HInstance, 'MULTPG');
  89.     Sample.Canvas.Draw(4, 16, DrawBitmap);
  90.   end;
  91.  
  92.   if daBtnsV in Definition then
  93.   begin
  94.     DrawBitmap.Handle := LoadBitmap(HInstance, 'BTNSV');
  95.     X := 75;
  96.     Y := 22;
  97.  
  98.     if daMultPg in Definition then
  99.     begin
  100.       Dec(X, 2);
  101.       Inc(Y, 4);
  102.     end;
  103.  
  104.     Sample.Canvas.Draw(X, Y, DrawBitmap);
  105.   end;
  106.  
  107.   if daBtnsH in Definition then
  108.   begin
  109.     DrawBitmap.Handle := LoadBitmap(HInstance, 'BTNSH');
  110.     X := 50;
  111.     Y := 55;
  112.  
  113.     if daMultPg in Definition then Dec(Y, 4);
  114.  
  115.     Sample.Canvas.Draw(X, Y, DrawBitmap);
  116.   end;
  117. end;
  118.  
  119. procedure TDlgExpert.FormCreate(Sender: TObject);
  120. begin
  121.   DrawBitmap := TBitmap.Create;
  122.   PrevClick(Self);
  123.   RefreshButtons;
  124. end;
  125.  
  126. procedure TDlgExpert.FormDestroy(Sender: TObject);
  127. begin
  128.   DrawBitmap.Free;
  129. end;
  130.  
  131. procedure TDlgExpert.StyleClick(Sender: TObject);
  132. begin
  133.   if rbMultPg.Checked then Include(Definition, daMultPg)
  134.   else Exclude(Definition, daMultPg);
  135.   SamplePaint(Self);
  136. end;
  137.  
  138. procedure TDlgExpert.BtnClick(Sender: TObject);
  139. begin
  140.   if rbBtnsV.Checked then Include(Definition, daBtnsV)
  141.   else Exclude(Definition, daBtnsV);
  142.   if rbBtnsH.Checked then Include(Definition, daBtnsH)
  143.   else Exclude(Definition, daBtnsH);
  144.   SamplePaint(Self);
  145. end;
  146.  
  147. procedure TDlgExpert.CancelClick(Sender: TObject);
  148. begin
  149.   Close;
  150. end;
  151.  
  152. procedure TDlgExpert.PrevClick(Sender: TObject);
  153. begin
  154.   case PageControl.ActivePage.PageIndex of
  155.     pgStyle: Exit;
  156.     pgPages: PageControl.ActivePage := PageControl.Pages[pgStyle];
  157.     pgButtons: if (daMultPg in Definition) then
  158.       PageControl.ActivePage := PageControl.Pages[pgPages]
  159.       else PageControl.ActivePage := PageControl.Pages[pgStyle];
  160.   end;
  161.   RefreshButtons;
  162. end;
  163.  
  164. procedure TDlgExpert.NextClick(Sender: TObject);
  165. begin
  166.   case PageControl.ActivePage.PageIndex of
  167.     pgStyle: if (daMultPg in Definition) then
  168.       PageControl.ActivePage := PageControl.Pages[pgPages]
  169.       else PageControl.ActivePage := PageControl.Pages[pgButtons];
  170.     pgPages: PageControl.ActivePage := PageControl.Pages[pgButtons];
  171.     pgButtons:
  172.       begin
  173.         ModalResult := mrOK;
  174.         Exit;
  175.       end;
  176.   end;
  177.   RefreshButtons;
  178. end;
  179.  
  180. procedure TDlgExpert.RefreshButtons;
  181. begin
  182.   PrevButton.Enabled := PageControl.ActivePage.PageIndex > 0;
  183.   if PageControl.ActivePage.PageIndex = pgButtons then
  184.     NextButton.Caption := LoadStr(sFinish)
  185.   else
  186.     NextButton.Caption := LoadStr(sNext);
  187. end;
  188.  
  189. { Create the dialog defined by the user }
  190. function TDlgExpert.DoFormCreation(const FormIdent: string): TForm;
  191. var
  192.   BtnPos: TPoint;
  193.   Method: TMethod;
  194.   PgCtrl: TPageControl;
  195.   I: Integer;
  196. begin
  197.   Result := TForm.Create(nil);
  198.   Proxies.CreateSubClass(Result, 'T' + FormIdent, TForm);
  199.   with Result do
  200.   begin
  201.     BorderStyle := bsDialog;
  202.     Width := 400;
  203.     Height := 250;
  204.     Position := poScreenCenter;
  205.     Name := FormIdent;
  206.     Caption := FormIdent;
  207.  
  208.     with Font do
  209.     begin
  210.       Name := 'MS Sans Serif';
  211.       Size := 8;
  212.     end;
  213.  
  214.     { create controls }
  215.     if daMultPg in Definition then
  216.     begin
  217.       PgCtrl := TPageControl.Create(Result);
  218.       with PgCtrl do
  219.       begin
  220.         Parent := Result;
  221.         Name := 'PageControl1';
  222.         Align := alClient;
  223.       end;
  224.  
  225.       if PageNames.Lines.Count > 0 then
  226.         for I := 0 to PageNames.Lines.Count - 1 do
  227.           with TTabSheet.Create(Result) do
  228.           begin
  229.             PageControl := PgCtrl;
  230.             Caption := PageNames.Lines[I];
  231.             Name := Format('TabSheet%d', [I + 1]);
  232.           end;
  233.     end;
  234.  
  235.     if (daBtnsH in Definition) or (daBtnsV in Definition) then
  236.     begin
  237.  
  238.       { get the starting point for the buttons }
  239.       if daBtnsH in Definition then
  240.         BtnPos := Point(ClientWidth - (77 * 3) - (5 * 3),
  241.           ClientHeight - 27 - 5)
  242.       else
  243.         BtnPos := Point(ClientWidth - 77 - 5, 30);
  244.  
  245.       { finalize positions }
  246.       if daMultPg in Definition then
  247.       begin
  248.         Dec(BtnPos.X, 5);
  249.         if daBtnsV in Definition then Inc(BtnPos.Y, 5)
  250.         else Dec(BtnPos.Y, 5);
  251.       end;
  252.  
  253.       { OK }
  254.       with TButton.Create(Result) do
  255.       begin
  256.         Parent := Result;
  257.         Left := BtnPos.X;
  258.         Top := BtnPos.Y;
  259.         Height := 25;
  260.         Width := 75;
  261.         Caption := LoadStr(sOKButton);
  262.         Name := 'Button1';
  263.         Default := True;
  264.         ModalResult := mrOk;
  265.       end;
  266.  
  267.       { move the next button position }
  268.       if daBtnsH in Definition then Inc(BtnPos.X, 75 + 5)
  269.       else Inc(BtnPos.Y, 25 + 5);
  270.  
  271.       { Cancel }
  272.       with TButton.Create(Result) do
  273.       begin
  274.         Parent := Result;
  275.         Left := BtnPos.X;
  276.         Top := BtnPos.Y;
  277.         Height := 25;
  278.         Width := 75;
  279.         Name := 'Button2';
  280.         Caption := LoadStr(sCancelButton);
  281.         Cancel := True;
  282.         ModalResult := mrCancel;
  283.       end;
  284.  
  285.       { move the next button position }
  286.       if daBtnsH in Definition then Inc(BtnPos.X, 75 + 5)
  287.       else Inc(BtnPos.Y, 25 + 5);
  288.  
  289.       { Help }
  290.       with TButton.Create(Result) do
  291.       begin
  292.         Parent := Result;
  293.         Left := BtnPos.X;
  294.         Top := BtnPos.Y;
  295.         Height := 25;
  296.         Width := 75;
  297.         Name := 'Button3';
  298.         Caption := LoadStr(sHelpButton);
  299.       end;
  300.     end;
  301.   end;
  302. end;
  303.  
  304. procedure TDlgExpert.FmtWrite(Stream: TStream; Fmt: PChar;
  305.   const Args: array of const);
  306. begin
  307.   if (Stream <> nil) and (SourceBuffer <> nil) then
  308.   begin
  309.     StrLFmt(SourceBuffer, SourceBufferSize, Fmt, Args);
  310.     Stream.Write(SourceBuffer[0], StrLen(SourceBuffer));
  311.   end;
  312. end;
  313.  
  314. function TDlgExpert.CreateSource(const UnitIdent, FormIdent: string): TMemoryStream;
  315. const
  316.   CRLF = #13#10;
  317. var
  318.   I: Integer;
  319. begin
  320.   SourceBuffer := StrAlloc(SourceBufferSize);
  321.   try
  322.     Result := TMemoryStream.Create;
  323.     try
  324.  
  325.       { unit header and uses clause }
  326.       FmtWrite(Result,
  327.         'unit %s;' + CRLF + CRLF +
  328.         'interface' + CRLF + CRLF +
  329.         'uses'#13#10 +
  330.         '  SysUtils, Windows, Messages, Classes, Graphics, Controls,'#13#10 +
  331.         '  StdCtrls, ExtCtrls, Forms', [UnitIdent]);
  332.  
  333.       { additional units that may be needed }
  334.       if daMultPg in Definition then FmtWrite(Result, ', ComCtrls', [nil]);
  335.  
  336.       FmtWrite(Result, ';' + CRLF + CRLF, [nil]);
  337.  
  338.       { begin the class declaration }
  339.       FmtWrite(Result,
  340.         'type'#13#10 +
  341.         '  T%s = class(TForm)'#13#10, [FormIdent]);
  342.  
  343.       { add variable declarations }
  344.       if (daBtnsH in Definition) or (daBtnsV in Definition) then
  345.       begin
  346.         FmtWrite(Result,
  347.           '    Button1: TButton;' + CRLF +
  348.           '    Button2: TButton;' + CRLF +
  349.           '    Button3: TButton;' + CRLF, [nil]);
  350.        end;
  351.  
  352.       if daMultPg in Definition then
  353.       begin
  354.         FmtWrite(Result, '    PageControl1: TPageControl;' + CRLF, [nil]);
  355.         if PageNames.Lines.Count > 0 then
  356.           for I := 0 to PageNames.Lines.Count - 1 do
  357.             FmtWrite(Result, '    TabSheet%d: TTabSheet;'#13#10, [I + 1]);
  358.       end;
  359.  
  360.       FmtWrite(Result,
  361.         '  end;' + CRLF + CRLF +
  362.         'var' + CRLF +
  363.         '  %s: T%s;' + CRLF + CRLF +
  364.         'implementation' + CRLF + CRLF +
  365.         '{$R *.DFM}' + CRLF + CRLF, [FormIdent, FormIdent]);
  366.  
  367.       FmtWrite(Result, 'end.' + CRLF, [nil]);
  368.       Result.Position := 0;
  369.  
  370.     except
  371.       Result.Free;
  372.       raise;
  373.     end;
  374.  
  375.   finally
  376.     StrDispose(SourceBuffer);
  377.   end;
  378. end;
  379.  
  380. function TDlgExpert.CreateForm(const FormIdent: string): TMemoryStream;
  381. var
  382.   DlgForm: TForm;
  383. begin
  384.   Result := nil;
  385.  
  386.   DlgForm := DoFormCreation(FormIdent);
  387.   try
  388.     Result := TMemoryStream.Create;
  389.     Result.WriteComponentRes(FormIdent, DlgForm);
  390.     Result.Position := 0;
  391.   finally
  392.     DlgForm.Free;
  393.   end;
  394. end;
  395.  
  396. procedure DialogExpert(ToolServices: TIToolServices);
  397. var
  398.   D: TDlgExpert;
  399.   ISourceStream, IFormStream: TIMemoryStream;
  400.   UnitIdent, FormIdent: string;
  401.   FileName: TFileName;
  402. begin
  403.   if ToolServices = nil then Exit;
  404.   if ToolServices.GetNewModuleName(UnitIdent, FileName) then
  405.   begin
  406.     D := TDlgExpert.Create(Application);
  407.     try
  408.       if D.ShowModal = mrOK then
  409.       begin
  410.         UnitIdent := LowerCase(UnitIdent);
  411.         UnitIdent[1] := Upcase(UnitIdent[1]);
  412.         FormIdent := 'Form' + Copy(UnitIdent, 5, 255);
  413.  
  414.         IFormStream := TIMemoryStream.Create(D.CreateForm(FormIdent));
  415.         try
  416.           IFormStream.AddRef;
  417.           ISourceStream := TIMemoryStream.Create(D.CreateSource(UnitIdent,
  418.             FormIdent));
  419.           try
  420.             ISourceStream.AddRef;
  421.             ToolServices.CreateModule(FileName, ISourceStream, IFormStream,
  422.               [cmAddToProject, cmShowSource, cmShowForm, cmUnNamed,
  423.               cmMarkModified]);
  424.           finally
  425.             ISourceStream.OwnStream := True;
  426.             ISourceStream.Free;
  427.           end;
  428.  
  429.         finally
  430.           IFormStream.OwnStream := True;
  431.           IFormStream.Free;
  432.         end;
  433.  
  434.       end;
  435.     finally
  436.       D.Free;
  437.     end;
  438.   end;
  439. end;
  440.  
  441. end.
  442.  
  443.  
  444.