home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / nastroje / d3456 / KBMWABD.ZIP / WABD_FormSecEditor.pas < prev    next >
Pascal/Delphi Source File  |  2001-07-16  |  16KB  |  595 lines

  1. unit WABD_FormSecEditor;
  2.  
  3. interface
  4.  
  5. {$I kbmWABD.inc}
  6.  
  7. {$IFDEF VER100}
  8. {$ASSERTIONS ON}
  9. {$ENDIF}
  10.  
  11. uses
  12. {$ifdef LEVEL6}
  13.   DesignIntf,
  14. {$else}
  15.   DsgnIntf,
  16. {$endif}
  17.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  18.   WABD_Objects, ExtCtrls, Buttons, StdCtrls, ShellAPI;
  19.  
  20. type
  21.    TInvisWin = class(TPanel)
  22.    protected
  23.       FSel      : boolean;
  24.       procedure SetSel(b: boolean);
  25.       procedure WndProc(var Message: TMessage); override;
  26.       procedure CreateParams(var Params: TCreateParams); override;
  27.       procedure PaintSquares(var ps: TPaintStruct);
  28.    public
  29.       Buddy     : TControl;
  30.       property  Selected: boolean read FSel write SetSel;
  31.    end;
  32.  
  33.   TFormSectionEditor = class(TForm)
  34.     ToolPanel: TPanel;
  35.     AnchorSpeed: TSpeedButton;
  36.     ImageSpeed: TSpeedButton;
  37.     LabelSpeed: TSpeedButton;
  38.     MemoSpeed: TSpeedButton;
  39.     ButtonSpeed: TSpeedButton;
  40.     EditSpeed: TSpeedButton;
  41.     ComboSpeed: TSpeedButton;
  42.     RadioSpeed: TSpeedButton;
  43.     ListSpeed: TSpeedButton;
  44.     CheckSpeed: TSpeedButton;
  45.     HTMLEmbedSpeed: TSpeedButton;
  46.     PreviewBut: TButton;
  47.     GridBut: TSpeedButton;
  48.     ScrollBox1: TScrollBox;
  49.     SelectSpeed: TSpeedButton;
  50.     LiveImageSpeed: TSpeedButton;
  51.     pStatus: TPanel;
  52.     HTMLFileEmbedSpeed: TSpeedButton;
  53.     UploadFileSpeed: TSpeedButton;
  54.     procedure PanelMouseDown(Sender: TObject; Button: TMouseButton;
  55.       Shift: TShiftState; X, Y: Integer);
  56.     procedure PanelMouseMove(Sender: TObject; Shift: TShiftState; X,
  57.       Y: Integer);
  58.     procedure PanelMouseUp(Sender: TObject; Button: TMouseButton;
  59.       Shift: TShiftState; X, Y: Integer);
  60.     procedure FormCreate(Sender: TObject);
  61.     procedure SecButSpeedClick(Sender: TObject);
  62.     procedure GridButClick(Sender: TObject);
  63.     procedure FormKeyDown(Sender: TObject; var Key: Word;
  64.       Shift: TShiftState);
  65.     procedure PreviewButClick(Sender: TObject);
  66.     procedure ScrollBox1MouseDown(Sender: TObject; Button: TMouseButton;
  67.       Shift: TShiftState; X, Y: Integer);
  68.     procedure PanelDblClick(Sender: TObject);
  69.   private
  70.     { Private declarations }
  71.   public
  72.     { Public declarations }
  73. {$ifdef LEVEL6}
  74.     MyDesigner   : IDesigner;
  75. {$else}
  76.  {$ifdef LEVEL5}
  77.     MyDesigner   : IFormDesigner;
  78.  {$else}
  79.   {$ifdef LEVEL4}
  80.     MyDesigner   : IFormDesigner;
  81.   {$else}
  82.    {$ifdef LEVEL3}
  83.     MyDesigner   : TFormDesigner; // Delphi 3, C++ Builder 3
  84.    {$endif}
  85.   {$endif}
  86.  {$endif}
  87. {$endif}
  88.  
  89.     FS         : TWABD_FormSection;
  90.     DownX      : integer;
  91.     DownY      : integer;
  92.     Moving     : boolean;
  93.     Sizing     : boolean;
  94.     DetectChanges : boolean;
  95.     OldSel     : TInvisWin;
  96.     ViewPanel  : TPaintPanel;
  97.     LastTag    : integer;
  98.     LookFor    : TWABD_SectionObject;
  99.     procedure  Init;
  100.     function   GetObj(Tag: integer): TWABD_SectionObject;
  101.     procedure  ReflectedNotify(Sender: TObject; AComponent: TComponent; Operation: TOperation);
  102.     procedure  ChildChanged(Sender: TObject);
  103.     procedure  SetPanel(p: TInvisWin; Active: boolean);
  104.     procedure  SetSpecialProperties(c: TControl);
  105.     procedure  SetAllProperties(c: TWinControl);
  106.   end;
  107.  
  108. var
  109.   FormSectionEditor: TFormSectionEditor;
  110.  
  111. implementation
  112.  
  113. uses WABD_FormEditor, TypInfo;
  114.  
  115. {$R *.DFM}
  116.  
  117. function GetRegion(Obj: TWABD_SectionObject; X, Y: integer): integer;
  118. const
  119.    BORDER = 5;
  120. var
  121.    px, py : integer;
  122. begin
  123.    px := 1;
  124.    if X < BORDER then px := 0;
  125.    if X > Obj.Width-BORDER then px := 2;
  126.  
  127.    py := 1;
  128.    if Y < BORDER then py := 0;
  129.    if Y > Obj.Height-BORDER then py := 2;
  130.  
  131.    Result := py * 3 + px + 1;
  132. end;
  133.  
  134. function GetRegCursor(Reg: integer): TCursor;
  135. begin
  136.    Result := crDefault;
  137.    case Reg of
  138.       1, 9  : Result := crSizeNWSE;
  139.       3, 7  : Result := crSizeNESW;
  140.       2, 8  : Result := crSizeNS;
  141.       4, 6  : Result := crSizeWE;
  142.       5     : Result := crDefault;
  143.    end;
  144. end;
  145.  
  146. function TFormSectionEditor.GetObj(Tag: integer): TWABD_SectionObject;
  147. begin
  148.    if (Tag-1 < 0) or (Tag-1 >= FS.ChildCount) then begin
  149.       Result := nil;
  150.       exit;
  151.    end;
  152.    
  153.    Result := fs.Children[Tag-1] as TWABD_SectionObject;
  154.    Assert(Result<>nil, 'Result = nil');
  155. end;
  156.  
  157. procedure TFormSectionEditor.SetPanel(p: TInvisWin; Active: boolean);
  158. begin
  159.    if Active then begin
  160.       p.Color := clActiveCaption;
  161.       p.Font.Color := clCaptionText;
  162.       if OldSel<>nil then SetPanel(OldSel, False);
  163.       OldSel := p;
  164.       (p as TInvisWin).Selected := True;
  165.    end else begin
  166.       p.Color := clInactiveCaption;
  167.       p.Font.Color := clInactiveCaptionText;
  168.       (p as TInvisWin).Selected := False;
  169.    end;
  170. end;
  171.  
  172. procedure TFormSectionEditor.Init;
  173. var
  174.    i     : integer;
  175.    c     : TControl;
  176.    Child : TWABD_Object;
  177. begin
  178.    if not DetectChanges then exit;
  179.  
  180.    OldSel := nil;
  181.    Caption := 'WABD Form Section Editor - ' + fs.Name;
  182.  
  183.    // Remove the old ones first
  184.    for i := ScrollBox1.ControlCount-1 downto 0 do begin
  185.       c := ScrollBox1.Controls[i];
  186.       c.Free;
  187.    end;
  188.  
  189.    FS.OnChange := ChildChanged;
  190.    for i := 0 to FS.ChildCount-1 do begin
  191.       Child          := FS.Children[i] as TWABD_SectionObject;
  192.       Child.OnChange := ChildChanged;
  193.    end;
  194.  
  195.    c := FS.Object_To_Control(ScrollBox1);
  196.    c.Width := 1024;
  197.    c.Height := 1024;
  198.    ViewPanel := c as TPaintPanel;
  199.    ViewPanel.DesignMode := GridBut.Down;
  200.    ViewPanel.OnMouseDown := ScrollBox1MouseDown;
  201.  
  202.    SetAllProperties(ViewPanel);
  203. end;
  204.  
  205. procedure TFormSectionEditor.SetSpecialProperties(c: TControl);
  206. var
  207.    iw  : TInvisWin;
  208.    Sel : boolean;
  209. begin
  210.    if (c is TPaintPanel) then begin
  211.       (c as TPaintPanel).DrawPic := False;
  212.       exit;
  213.    end;
  214.  
  215.    Sel := GetObj(c.Tag) = LookFor;
  216.  
  217.    iw := TInvisWin.Create(Self);
  218.    iw.Parent := c.Parent;
  219.    iw.Buddy  := c;
  220.    iw.Left   := c.Left;
  221.    iw.Top    := c.Top;
  222.    iw.Width  := c.Width;
  223.    iw.Height := c.Height;
  224.    iw.OnMouseDown := PanelMouseDown;
  225.    iw.OnMouseMove := PanelMouseMove;
  226.    iw.OnMouseUp   := PanelMouseUp;
  227.    iw.OnDblClick  := PanelDblClick;
  228.    SetPanel(iw, Sel);
  229. end;
  230.  
  231. procedure TFormSectionEditor.SetAllProperties(c: TWinControl);
  232. var
  233.    i     : integer;
  234.    Child : TControl;
  235. begin
  236.    SetSpecialProperties(c);
  237.    for i := 0 to c.ControlCount-1 do begin
  238.       Child := c.Controls[i];
  239.       if (Child is TWinControl) then begin
  240.          SetAllProperties(Child as TWinControl);
  241.       end else begin
  242.          SetSpecialProperties(Child);
  243.       end;
  244.    end;
  245. end;
  246.  
  247. procedure TFormSectionEditor.PanelMouseDown(Sender: TObject;
  248.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  249. var
  250.    s : TWABD_SectionObject;
  251.    p : TInvisWin;
  252.    x1,y1:integer;
  253. begin
  254.    DownX := X;
  255.    DownY := Y;
  256.  
  257.    // Get component pointed at.
  258.    p := Sender as TInvisWin;
  259.    s := GetObj(p.Buddy.Tag);
  260.    Assert(s<>nil, 's = nil');
  261.    MyDesigner.SelectComponent(s);
  262.  
  263.    // Check if sizing or moving.
  264.    x1 := p.Left + (X-DownX);
  265.    y1 := p.Top  + (Y-DownY);
  266.  
  267.    // Check if moved at all. If not, just exit.
  268.    if (X>=p.Width-5) or (Y>=p.Height-5) then
  269.       Sizing:=true
  270.    else
  271.        Moving:=true;
  272. //ShowMessage(Format('X,Y=%d,%d, p.Left,p.Top=%d,%d pWidth,p.Height=%d,%d',[X,Y,p.Left,p.Top,p.Width,p.Height]));
  273.  
  274.    // Show selection.
  275.    SetPanel(p, True);
  276.    p.Buddy.BringToFront;
  277.    p.BringToFront;
  278.    OldSel := p;
  279. end;
  280.  
  281. procedure TFormSectionEditor.PanelMouseMove(Sender: TObject;
  282.   Shift: TShiftState; X, Y: Integer);
  283. var
  284.    p : TInvisWin;
  285.    s : TWABD_SectionObject;
  286.    x1,y1: integer;
  287.    w, h : integer;
  288. begin
  289.    if not (Moving or Sizing) then exit;
  290.  
  291.    // Calculate placement/size.
  292.    p := Sender as TInvisWin;
  293.    x1 := (X-DownX);
  294.    y1 := (Y-DownY);
  295.    if GridBut.Down then begin
  296.       x1 := (x1 div FS.GridX) * FS.GridX;
  297.       y1 := (y1 div FS.GridY) * FS.GridY;
  298.    end;
  299.  
  300.    // If moving.
  301.    if Moving then
  302.    begin
  303.         // Check if moved at all. If not, just exit.
  304.         x1:=x1+p.Left;
  305.         y1:=y1+p.Top;
  306.         if (x1=p.Left) and (y1=p.Top) then exit;
  307.  
  308.         w:=p.Width;
  309.         h:=p.Height;
  310.    end
  311.    else
  312.    // Sizing.
  313.    begin
  314.         // If not change of size.
  315.         if (p.Width=X) and (p.Height=Y) then exit;
  316.  
  317.         x1:=p.Left;
  318.         y1:=p.Top;
  319.         w:=X;
  320.         h:=Y;
  321.    end;
  322.  
  323.    // OK, moved or sized, update component.
  324.    p.SetBounds(x1, y1, w, h);
  325.    p.Buddy.SetBounds(x1, y1, w, h);
  326.    p.Invalidate;
  327.    p.Buddy.Invalidate;
  328.    s := GetObj(p.Buddy.Tag);
  329.    DetectChanges := False;
  330.    s.LeftPos := p.Left;
  331.    s.TopPos  := p.Top;
  332.    s.Width   := p.Width;
  333.    s.Height  := p.Height;
  334.    p.Buddy.Refresh;
  335.    DetectChanges := True;
  336.    MyDesigner.Modified;
  337. end;
  338.  
  339. procedure TFormSectionEditor.PanelMouseUp(Sender: TObject;
  340.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  341. var
  342.    p : TInvisWin;
  343.    w,h:integer;
  344.    s : TWABD_SectionObject;
  345. begin
  346.    // Check if to resize to grid.
  347.    if Sizing and GridBut.Down then
  348.    begin
  349.         p := Sender as TInvisWin;
  350.         w := (p.Buddy.Width div FS.GridX) * FS.GridX;
  351.         h := (p.Buddy.Height div FS.GridY) * FS.GridY;
  352.         if w<=0 then w:=FS.GridX;
  353.         if h<=0 then h:=FS.GridY;
  354.         s := GetObj(p.Buddy.Tag);
  355.         p.SetBounds(p.Left, p.Top, w, h);
  356.         p.Buddy.SetBounds(p.Left, p.Top, w, h);
  357.         s.Width   := p.Width;
  358.         s.Height  := p.Height;
  359.         p.Invalidate;
  360.         p.Buddy.Invalidate;
  361.    end;
  362.    Sizing := False;
  363.    Moving := False;
  364. end;
  365.  
  366. procedure TFormSectionEditor.FormCreate(Sender: TObject);
  367. begin
  368.    Moving := False;
  369.    DetectChanges := True;
  370.    OldSel := nil;
  371.    ViewPanel := nil;
  372.    LastTag := -1;
  373. end;
  374.  
  375. procedure TFormSectionEditor.ReflectedNotify(Sender: TObject; AComponent: TComponent; Operation: TOperation);
  376. begin
  377.    if (AComponent = FS) and (Operation = opRemove) then begin
  378.       FS := nil;
  379.       Visible := False;
  380.    end;
  381.  
  382.    if AComponent is TWABD_SectionObject then
  383.       if (AComponent as TWABD_SectionObject).Parent = FS then Init;
  384. end;
  385.  
  386. procedure TFormSectionEditor.ChildChanged(Sender: TObject);
  387. begin
  388.    if not DetectChanges then exit;
  389.  
  390.    if (Sender is TWABD_SectionObject) then
  391.       if (Sender as TWABD_SectionObject).Parent = FS then Init;
  392.  
  393.    if Sender = FS then Init;
  394. end;
  395.  
  396. procedure TFormSectionEditor.SecButSpeedClick(Sender: TObject);
  397. begin
  398.    // (Owner as TWABDFormEditor).AddSectionLevel((Sender as TControl).Tag);
  399.    LastTag := (Sender as TControl).Tag;
  400. end;
  401.  
  402.  
  403. procedure TFormSectionEditor.GridButClick(Sender: TObject);
  404. begin
  405.    ViewPanel.DesignMode := GridBut.Down;
  406.    ViewPanel.Invalidate;
  407. end;
  408.  
  409. procedure TFormSectionEditor.FormKeyDown(Sender: TObject; var Key: Word;
  410.   Shift: TShiftState);
  411. var
  412.    c : TWABD_Object;
  413. begin
  414.    if (OldSel<>nil) and (Key = 46) then begin
  415.       c := GetObj(OldSel.Buddy.Tag);
  416.       // if c<>nil then ShowMessage(Format('Name, Class = %s, %s', [c.name, c.ClassName]));
  417.       c.Free;
  418.       MyDesigner.SelectComponent(nil);
  419.    end;
  420. end;
  421.  
  422. procedure TFormSectionEditor.PreviewButClick(Sender: TObject);
  423. var
  424.    f    : string;
  425.    h    : string;
  426.    fstm : TFileStream;
  427.    rc   : integer;
  428. begin
  429.    f := 'LocalTemp.htm';
  430.    h := '<HTML><BODY>' + FS.Object_To_HTML + '</BODY></HTML>';
  431.    fstm := TFileStream.Create(f, fmCreate or fmOpenWrite or fmShareDenyWrite);
  432.    fstm.Write(h[1], Length(h));
  433.    fstm.Free;
  434.  
  435.    rc := ShellExecute(Handle, 'open', PChar(f), nil, nil, SW_SHOWNORMAL);
  436.    if (rc <= 32) then ShowMessage('Unable to execute: ' + f + #13#10 + 'Browser not installed?');
  437. end;
  438.  
  439.  
  440.  
  441. procedure TInvisWin.WndProc(var Message: TMessage);
  442. var
  443.    ps : TPaintStruct;
  444. begin
  445.    case Message.Msg of
  446.       WM_ERASEBKGND: Message.Result := 1;
  447.       WM_PAINT: begin
  448.          BeginPaint(Handle, ps);
  449.          if FSel then PaintSquares(ps);
  450.          EndPaint(Handle, ps);
  451.          Message.Result := 1;
  452.       end;
  453.    else
  454.       inherited WndProc(Message);
  455.    end;
  456. end;
  457.  
  458. procedure TInvisWin.PaintSquares(var ps: TPaintStruct);
  459. var
  460.    c      : TCanvas;
  461.    i      : integer;
  462.    mx, my : integer;
  463.    x, y   : integer;
  464.    R      : TRect;
  465. begin
  466.    c := TCanvas.Create;
  467.    c.Handle := ps.hdc;
  468.  
  469.    c.Brush.Color := clBlack;
  470.    mx := Width div 2;
  471.    my := Height div 2;
  472.  
  473.    for i := 0 to 7 do begin
  474.       x := 0;
  475.       y := 0;
  476.       case i of
  477.          0, 1, 2  : y := 0;
  478.          3, 4     : y := my - 2;
  479.          5, 6, 7  : y := Height - 5;
  480.       end;
  481.       case i of
  482.          0, 3, 5  : x := 0;
  483.          1, 6     : x := mx - 2;
  484.          2, 4, 7  : x := Width - 5;
  485.       end;
  486.       R := Rect(x, y, x+5, y+5);
  487.       c.FillRect(R);
  488.    end;
  489.  
  490.    c.Free;
  491. end;
  492.  
  493. procedure TInvisWin.SetSel(b: boolean);
  494. begin
  495.    FSel := b;
  496.    Invalidate;
  497.    if Buddy<>nil then Buddy.Invalidate;
  498. end;
  499.  
  500. procedure TInvisWin.CreateParams(var Params: TCreateParams);
  501. begin
  502.    inherited;
  503.    Params.ExStyle := Params.ExStyle or WS_EX_TRANSPARENT;
  504. end;
  505.  
  506. procedure TFormSectionEditor.ScrollBox1MouseDown(Sender: TObject;
  507.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  508. var
  509.    wo : TWABD_SectionObject;
  510. begin
  511.    // Check if to select formsection.
  512.    if LastTag = -1 then
  513.    begin
  514.         MyDesigner.SelectComponent(FS);
  515.         if OldSel<>nil then SetPanel(OldSel, False);
  516.         OldSel := nil;
  517.         exit;
  518.    end;
  519.  
  520.    // Else add a component.
  521.    DetectChanges := False;
  522.    wo := (Owner as TWABDFormEditor).AddSectionLevel(LastTag);
  523.    SelectSpeed.Down := True;
  524.    LastTag := -1;
  525.  
  526.    if GridBut.Down then begin
  527.       X := (X div FS.GridX) * FS.GridX;
  528.       Y := (Y div FS.GridY) * FS.GridY;
  529.    end;
  530.  
  531.    wo.LeftPos := X;
  532.    wo.TopPos  := Y;
  533.    LookFor := wo;
  534.    DetectChanges := True;
  535.    Init;
  536. end;
  537.  
  538. procedure TFormSectionEditor.PanelDblClick(Sender: TObject);
  539. var
  540.    p        : TInvisWin;
  541.    s        : TWABD_Object;
  542.    nm       : TMethod;
  543.    MethName : string;
  544.    wb       : TWABD_Button;
  545.    wi       : TWABD_Base_Image;
  546.    wt       : TWABD_Table;
  547. begin
  548.    p := Sender as TInvisWin;
  549.    s := GetObj(p.Buddy.Tag);
  550.    Assert(s<>nil, 's = nil');
  551.  
  552.    if s is TWABD_Button then begin
  553.       wb := s as TWABD_Button;
  554.       if Assigned(wb.OnUserClick) then begin
  555.          nm := TMethod(wb.OnUserClick);
  556.          MethName := MyDesigner.GetMethodName(nm);
  557.       end else begin
  558.          MethName := s.Name+'UserClick';
  559.          nm := MyDesigner.CreateMethod(MethName, GetTypeData(TypeInfo(TNotifyEvent)));
  560.          wb.OnUserClick := TNotifyEvent(nm);
  561.       end;
  562.       MyDesigner.ShowMethod(MethName);
  563.    end;
  564.  
  565.    if s is TWABD_Base_Image then begin
  566.       wi := s as TWABD_Base_Image;
  567.       if Assigned(wi.OnMouseDown) then begin
  568.          nm := TMethod(wi.OnMouseDown);
  569.          MethName := MyDesigner.GetMethodName(nm);
  570.          ShowMessage('MethName = ' + MethName);
  571.       end else begin
  572.          MethName := s.Name+'MouseDown';
  573.          nm := MyDesigner.CreateMethod(MethName, GetTypeData(TypeInfo(TWABD_MouseDown)));
  574.          wi.OnMouseDown := TWABD_MouseDown(nm);
  575.       end;
  576.       MyDesigner.ShowMethod(MethName);
  577.    end;
  578.  
  579.    if s is TWABD_Table then begin
  580.       wt := s as TWABD_Table;
  581.       if Assigned(wt.OnUserClick) then begin
  582.          nm := TMethod(wt.OnUserClick);
  583.          MethName := MyDesigner.GetMethodName(nm);
  584.       end else begin
  585.          MethName := s.Name+'UserClick';
  586.          nm := MyDesigner.CreateMethod(MethName, GetTypeData(TypeInfo(TWABDTableClick)));
  587.          wt.OnUserClick := TWABDTableClick(nm);
  588.       end;
  589.       MyDesigner.ShowMethod(MethName);
  590.    end;
  591. end;
  592.  
  593.  
  594. end.
  595.