home *** CD-ROM | disk | FTP | other *** search
/ PC Format Collection 48 / SENT14D.ISO / tech / delphi / disk15 / experts.pak / DLG.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-08-24  |  12.9 KB  |  494 lines

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