home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / kolekce / d6 / RX275D6.ZIP / Units / PGMNGRED.PAS < prev    next >
Pascal/Delphi Source File  |  2001-06-24  |  12KB  |  454 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {         Delphi VCL Extensions (RX)                    }
  4. {                                                       }
  5. {         Copyright (c) 1996 AO ROSNO                   }
  6. {                                                       }
  7. {*******************************************************}
  8.  
  9. unit PgMngrEd;
  10.  
  11. {$I RX.INC}
  12.  
  13. interface
  14.  
  15. uses
  16. {$IFDEF WIN32}
  17.   Windows,
  18. {$ELSE}
  19.   WinTypes, WinProcs,
  20. {$ENDIF}
  21.   SysUtils, Messages, Classes, Graphics, Controls, Forms, Dialogs, Grids,
  22.   RTLConsts, DesignIntf, DesignEditors, VCLEditors, PageMngr, StdCtrls, Placemnt, ExtCtrls,
  23.   VCLUtils, DesignWindows;
  24.  
  25. type
  26.   TProxyEditor = class(TDesignWindow)
  27.     FormStorage: TFormStorage;
  28.     BtnPanel: TPanel;
  29.     CloseBtn: TButton;
  30.     DeleteBtn: TButton;
  31.     ProxyGrid: TDrawGrid;
  32.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  33.     procedure FormShow(Sender: TObject);
  34.     procedure ProxyGridDrawCell(Sender: TObject; Col, Row: Longint; Rect: TRect; State: TGridDrawState);
  35.     procedure ProxyGridSelectCell(Sender: TObject; Col, Row: Longint; var CanSelect: Boolean);
  36.     procedure CloseBtnClick(Sender: TObject);
  37.     procedure DeleteBtnClick(Sender: TObject);
  38.     procedure ProxyGridKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  39.     procedure FormResize(Sender: TObject);
  40.     procedure FormCreate(Sender: TObject);
  41.   private
  42.     FPageManager: TPageManager;
  43.     FDeleting: Boolean;
  44.     procedure SetPageManager(Value: TPageManager);
  45.     function GetForm: TCustomForm;
  46.     procedure UpdateData;
  47.     function CheckPageManager: Boolean;
  48.     procedure SelectProxy(Proxy: TPageProxy);
  49.     function ProxyByRow(Row: Integer): TPageProxy;
  50.   protected
  51.     function UniqueName(Component: TComponent): string; override;
  52.     procedure Activated; override;
  53.   public
  54.     procedure NameProxy(Sender: TObject);
  55.     procedure ItemsModified(const Designer: IDesigner); override;
  56.     procedure DesignerClosed(const Designer: IDesigner; AGoingDormant: Boolean); override;
  57.     function GetEditState: TEditState; override;
  58.     procedure ItemDeleted(const ADesigner: IDesigner; Item: TPersistent); override;
  59.     property PageManager: TPageManager read FPageManager write SetPageManager;
  60.     property OwnerForm: TCustomForm read GetForm;
  61.   end;
  62.  
  63. { TProxyListProperty }
  64.  
  65.   TProxyListProperty = class(TPropertyEditor)
  66.     function GetAttributes: TPropertyAttributes; override;
  67.     function GetValue: string; override;
  68.     procedure Edit; override;
  69.   end;
  70.  
  71. { TPageManagerEditor }
  72.  
  73.   TPageManagerEditor = class(TComponentEditor)
  74.     procedure ExecuteVerb(Index: Integer); override;
  75.     function GetVerb(Index: Integer): string; override;
  76.     function GetVerbCount: Integer; override;
  77.   end;
  78.  
  79. { TPageNameProperty }
  80.  
  81.   TPageNameProperty = class(TStringProperty)
  82.     function GetAttributes: TPropertyAttributes; override;
  83.     procedure GetValues(Proc: TGetStrProc); override;
  84.   end;
  85.  
  86. { TPageBtnProperty }
  87.  
  88.   TPageBtnProperty = class(TComponentProperty)
  89.     procedure GetValues(Proc: TGetStrProc); override;
  90.   end;
  91.  
  92. implementation
  93.  
  94. uses Consts, Buttons, RxCtrls, RXConst, RXLConst, RxDsgn;
  95.  
  96. {$R *.DFM}
  97.  
  98. {$IFDEF WIN32}
  99.  {$D-}
  100. {$ENDIF}
  101.  
  102. {$IFDEF RX_D4}
  103. type
  104.   TDesigner = IDesigner;
  105.   TFormDesigner = IDesigner;
  106. {$ENDIF}
  107.  
  108. function FindEditor(Manager: TPageManager): TProxyEditor;
  109. var
  110.   I: Integer;
  111. begin
  112.   Result := nil;
  113.   for I := 0 to Screen.FormCount - 1 do begin
  114.     if Screen.Forms[I] is TProxyEditor then begin
  115.       if TProxyEditor(Screen.Forms[I]).PageManager = Manager then
  116.       begin
  117.         Result := TProxyEditor(Screen.Forms[I]);
  118.         Break;
  119.       end;
  120.     end;
  121.   end;
  122. end;
  123.  
  124. procedure ShowProxyEditor(Designer: TDesigner; Manager: TPageManager);
  125. var
  126.   Editor: TProxyEditor;
  127. begin
  128.   if Manager = nil then Exit;
  129.   Editor := FindEditor(Manager);
  130.   if Editor <> nil then begin
  131.     Editor.Show;
  132.     if Editor.WindowState = wsMinimized then Editor.WindowState := wsNormal;
  133.   end
  134.   else begin
  135.     Editor := TProxyEditor.Create(Application);
  136.     try
  137.       Editor.Designer := TFormDesigner(Designer);
  138.       Editor.PageManager := Manager;
  139.       Editor.Show;
  140.     except
  141.       Editor.Free;
  142.       raise;
  143.     end;
  144.   end;
  145. end;
  146.  
  147. { TProxyListProperty }
  148.  
  149. function TProxyListProperty.GetAttributes: TPropertyAttributes;
  150. begin
  151.   Result := [paDialog, paReadOnly];
  152. end;
  153.  
  154. function TProxyListProperty.GetValue: string;
  155. var
  156.   List: TList;
  157. begin
  158.   List := TList(Pointer(GetOrdValue));
  159.   if (List = nil) or (List.Count = 0) then
  160.     Result := ResStr(srNone)
  161.   else FmtStr(Result, '(%s)', [GetPropType^.Name]);
  162. end;
  163.  
  164. procedure TProxyListProperty.Edit;
  165. begin
  166.   ShowProxyEditor(Designer, TPageManager(GetComponent(0)));
  167. end;
  168.  
  169. { TPageBtnProperty }
  170.  
  171. procedure TPageBtnProperty.GetValues(Proc: TGetStrProc);
  172. var
  173.   I: Integer;
  174.   Component: TComponent;
  175. begin
  176.   for I := 0 to Designer.Root.ComponentCount - 1 do begin
  177.     Component := Designer.Root.Components[I];
  178.     if (Component.InheritsFrom(TButtonControl) or 
  179.       Component.InheritsFrom(TSpeedButton) or 
  180.       Component.InheritsFrom(TRxSpeedButton)) and 
  181.       (Component.Name <> '') then Proc(Component.Name);
  182.   end;
  183. end;
  184.  
  185. { TPageNameProperty }
  186.  
  187. function TPageNameProperty.GetAttributes: TPropertyAttributes;
  188. begin
  189.   Result := [paValueList];
  190. end;
  191.  
  192. procedure TPageNameProperty.GetValues(Proc: TGetStrProc);
  193. var
  194.   PageProxy: TPageProxy;
  195.   I: Integer;
  196. begin
  197.   PageProxy := GetComponent(0) as TPageProxy;
  198.   if (PageProxy <> nil) and (PageProxy.PageManager <> nil) and
  199.     (PageProxy.PageManager.PageOwner <> nil) then
  200.   begin
  201.     for I := 0 to PageProxy.PageManager.PageCount - 1 do begin
  202.       Proc(PageProxy.PageManager.PageNames[I]);
  203.     end;
  204.   end;
  205. end;
  206.  
  207. { TPageManagerEditor }
  208.  
  209. procedure TPageManagerEditor.ExecuteVerb(Index: Integer);
  210. begin
  211.   case Index of
  212.     0: ShowProxyEditor(Designer, TPageManager(Component));
  213.   end;
  214. end;
  215.  
  216. function TPageManagerEditor.GetVerb(Index: Integer): string;
  217. begin
  218.   case Index of
  219.     0: Result := LoadStr(srProxyEditor);
  220.   end;
  221. end;
  222.  
  223. function TPageManagerEditor.GetVerbCount: Integer;
  224. begin
  225.   Result := 1;
  226. end;
  227.  
  228. { TProxyEditor }
  229.  
  230. procedure TProxyEditor.SetPageManager(Value: TPageManager);
  231. begin
  232.   if FPageManager <> Value then begin
  233.     if FPageManager <> nil then FPageManager.OnCheckProxy := nil;
  234.     FPageManager := Value;
  235.     if FPageManager <> nil then FPageManager.OnCheckProxy := NameProxy;
  236.     UpdateData;
  237.   end;
  238. end;
  239.  
  240. function TProxyEditor.UniqueName(Component: TComponent): string;
  241. var
  242.   Temp: string;
  243. {$IFNDEF WIN32}
  244.   I: Integer;
  245.   Comp: TComponent;
  246. {$ENDIF}
  247. begin
  248.   Result := '';
  249.   if (Component <> nil) then Temp := Component.ClassName
  250.   else Temp := TPageProxy.ClassName;
  251.   if (UpCase(Temp[1]) = 'T') and (Length(Temp) > 1) then
  252.     System.Delete(Temp, 1, 1);
  253. {$IFDEF WIN32}
  254.   Result := Designer.UniqueName(Temp);
  255. {$ELSE}
  256.   I := 1;
  257.   repeat
  258.     Result := Temp + IntToStr(I);
  259.     Comp := OwnerForm.FindComponent(Result);
  260.     Inc(I);
  261.   until (Comp = nil) or (Comp = Component);
  262. {$ENDIF}
  263. end;
  264.  
  265. function TProxyEditor.GetEditState: TEditState;
  266. begin
  267.   Result := [];
  268. end;
  269.  
  270. procedure TProxyEditor.NameProxy(Sender: TObject);
  271. begin
  272.   if (Sender is TPageProxy) and (TPageProxy(Sender).Name = '') then
  273.     TPageProxy(Sender).Name := UniqueName(TPageProxy(Sender));
  274. end;
  275.  
  276. procedure TProxyEditor.DesignerClosed(const Designer: IDesigner; AGoingDormant: Boolean);
  277. begin
  278.   if Designer.Root = OwnerForm then Free;
  279. end;
  280.  
  281. procedure TProxyEditor.ItemsModified(const Designer: IDesigner);
  282. begin
  283.   if not (csDestroying in ComponentState) then UpdateData;
  284. end;
  285.  
  286. procedure TProxyEditor.Activated;
  287. begin
  288.   SelectProxy(ProxyByRow(ProxyGrid.Row - 1));
  289. end;
  290.  
  291. procedure TProxyEditor.ItemDeleted(const ADesigner: IDesigner; Item: TPersistent);
  292. begin
  293.   if Item = FPageManager then begin
  294.     FPageManager := nil;
  295.     Close;
  296.   end;
  297. end;
  298.  
  299. procedure TProxyEditor.UpdateData;
  300. var
  301.   ProxyCount: Integer;
  302. begin
  303.   if CheckPageManager then begin
  304.     if not FDeleting then FPageManager.Resync;
  305.     ProxyCount := FPageManager.PageProxies.Count;
  306.     if ProxyCount = 0 then begin
  307.       ProxyGrid.RowCount := 2;
  308.       SelectProxy(nil);
  309.     end
  310.     else begin
  311.       ProxyGrid.RowCount := 1 + ProxyCount;
  312.     end;
  313.     DeleteBtn.Enabled := ProxyCount > 0;
  314.     ProxyGrid.Invalidate;
  315.   end;
  316. end;
  317.  
  318. function TProxyEditor.GetForm: TCustomForm;
  319. begin
  320.   Result := GetParentForm(BtnPanel); //Designer.Form;
  321. end;
  322.  
  323. procedure TProxyEditor.FormClose(Sender: TObject; var Action: TCloseAction);
  324. begin
  325.   Action := caFree;
  326.   if FPageManager <> nil then FPageManager.OnCheckProxy := nil;
  327. end;
  328.  
  329. procedure TProxyEditor.FormShow(Sender: TObject);
  330. begin
  331.   if FPageManager.PageOwner <> nil then begin
  332.     Caption := Format(LoadStr(srPageProxies), [FPageManager.PageOwner.Name]);
  333.   end;
  334. end;
  335.  
  336. function TProxyEditor.CheckPageManager: Boolean;
  337. begin
  338.   Result := (FPageManager <> nil) and (FPageManager.Owner <> nil) and
  339.     (Designer.Root <> nil);
  340. end;
  341.  
  342. procedure TProxyEditor.SelectProxy(Proxy: TPageProxy);
  343. var
  344.   FComponents: IDesignerSelections;
  345. begin
  346.   if CheckPageManager and Active then begin
  347.     FComponents := CreateSelectionList;
  348.     if Proxy <> nil then
  349.       FComponents.Add(Proxy)
  350.     else
  351.       FComponents.Add(FPageManager);
  352.     SetSelection(FComponents);
  353.   end;
  354. end;
  355.  
  356. function TProxyEditor.ProxyByRow(Row: Integer): TPageProxy;
  357. begin
  358.   Result := nil;
  359.   if CheckPageManager and (Row >= 0) and
  360.     (Row < FPageManager.PageProxies.Count) then
  361.   begin
  362.     Result := FPageManager.PageProxies.Items[Row];
  363.   end;
  364. end;
  365.  
  366. procedure TProxyEditor.ProxyGridDrawCell(Sender: TObject; Col,
  367.   Row: Longint; Rect: TRect; State: TGridDrawState);
  368. var
  369.   CellText: string;
  370.   Proxy: TPageProxy;
  371. begin
  372.   CellText := '';
  373.   if gdFixed in State then begin
  374.     case Col of
  375.       0: CellText := LoadStr(srProxyName);
  376.       1: CellText := LoadStr(srPageName);
  377.     end;
  378.   end
  379.   else begin
  380.     Proxy := ProxyByRow(Row - 1);
  381.     if Proxy <> nil then begin
  382.       case Col of
  383.         0: CellText := Proxy.Name;
  384.         1: CellText := Proxy.PageName;
  385.       end;
  386.     end;
  387.   end;
  388.   DrawCellText(ProxyGrid, Col, Row, CellText, Rect, taLeftJustify, vaCenter);
  389. end;
  390.  
  391. procedure TProxyEditor.ProxyGridSelectCell(Sender: TObject; Col,
  392.   Row: Longint; var CanSelect: Boolean);
  393. begin
  394.   SelectProxy(ProxyByRow(Row - 1));
  395. end;
  396.  
  397. procedure TProxyEditor.CloseBtnClick(Sender: TObject);
  398. begin
  399.   Close;
  400. end;
  401.  
  402. procedure TProxyEditor.DeleteBtnClick(Sender: TObject);
  403. var
  404.   Proxy: TPageProxy;
  405. begin
  406.   Proxy := ProxyByRow(ProxyGrid.Row - 1);
  407.   if Proxy <> nil then begin
  408.     Self.ValidateRename(Proxy, Proxy.Name, '');
  409.     FDeleting := True;
  410.     try
  411.       Proxy.Free;
  412.       Designer.Modified;
  413.     finally
  414.       FDeleting := False;
  415.     end;
  416.   end;
  417. end;
  418.  
  419. procedure TProxyEditor.ProxyGridKeyDown(Sender: TObject; var Key: Word;
  420.   Shift: TShiftState);
  421. begin
  422.   if Shift = [] then begin
  423.     case Key of
  424.       VK_RETURN:
  425.         if ProxyByRow(ProxyGrid.Row - 1) <> nil then begin
  426.           ActivateInspector(#0);
  427.         end;
  428.       VK_DELETE:
  429.         DeleteBtnClick(nil);
  430.     end;
  431.   end;
  432. end;
  433.  
  434. procedure TProxyEditor.FormResize(Sender: TObject);
  435. begin
  436.   with ProxyGrid do begin
  437.     DefaultColWidth := (ClientWidth - 1) div 2;
  438.     ColWidths[1] := ClientWidth - ColWidths[0] - 1;
  439.   end;
  440. end;
  441.  
  442. procedure TProxyEditor.FormCreate(Sender: TObject);
  443. begin
  444.   if NewStyleControls then Font.Style := [];
  445. {$IFDEF WIN32}
  446.   with FormStorage do begin
  447.     UseRegistry := True;
  448.     IniFileName := SDelphiKey;
  449.   end;
  450. {$ENDIF}
  451. end;
  452.  
  453. end.
  454.