home *** CD-ROM | disk | FTP | other *** search
/ Delphi Anthology / aDELPHI.iso / Runimage / Delphi50 / Demos / Midas / InternetExpress / InetXCustom / imgbuttons.pas < prev    next >
Pascal/Delphi Source File  |  1999-08-11  |  13KB  |  509 lines

  1. unit ImgButtons;
  2.  
  3. interface
  4.  
  5. uses Classes, HTTPApp, Db, DbClient, Midas,
  6.   XMLBrokr, WebComp, PagItems, MidItems;
  7.  
  8. type
  9.  
  10.   IImgComponent = interface
  11.   ['{15480205-25A4-11D3-B007-00C04FB16EC3}']
  12.     procedure SetPathURL(APath: string);
  13.   end;
  14.  
  15.   TImgDataNavigator = class(TDataNavigator)
  16.   private
  17.     FImagePathURL: string;
  18.     procedure SetImagePathURL(const Value: string);
  19.   protected
  20.     procedure GetDefaultButtons; override;
  21.     function ImplContent(Options: TWebContentOptions; ParentLayout: TLayout): string; override;
  22.   published
  23.     property ImagePathURL: string read FImagePathURL write SetImagePathURL;
  24.   end;
  25.  
  26.   TImgDataSetButton = class(TXMLDisplayReferenceButton, IImgComponent,
  27.     IScriptComponent)
  28.   private
  29.     FSrc: string;
  30.     FAlt: string;
  31.     FBorder: Boolean;
  32.     FPathURL: string;
  33.   protected
  34.     DefaultAlt: string;
  35.     DefaultSrc: string;
  36.     XMLMethodName: string;
  37.      { IScriptComponent }
  38.     procedure AddElements(AddIntf: IAddScriptElements);
  39.     function GetSubComponents: TObject;
  40.    { IWebContent implementation }
  41.     function ImplContent(Options: TWebContentOptions; ParentLayout: TLayout): string; override;
  42.     { IImgComponent }
  43.     procedure SetPathURL(APath: string);
  44.  
  45.     function GetAlt: string;
  46.     function GetSrc: string;
  47.     function GetMethodName: string; virtual;
  48.     function GetSrcUrl: string;
  49.   published
  50.     property XMLComponent;
  51.     property XMLUseParent;
  52.     property Style;
  53.     property Custom;
  54.     property Alt: string read GetAlt write FAlt;
  55.     property StyleRule;
  56.     property Src: string read GetSrc write FSrc;
  57.     property Border: Boolean read FBorder write FBorder;
  58.   end;
  59.  
  60.   TImgFirstButton = class(TImgDataSetButton)
  61.   public
  62.     constructor Create(AOwner: TComponent); override;
  63.   end;
  64.  
  65.   TImgLastButton = class(TImgDataSetButton)
  66.   public
  67.     constructor Create(AOwner: TComponent); override;
  68.   end;
  69.  
  70.   TImgPriorButton = class(TImgDataSetButton)
  71.   public
  72.     constructor Create(AOwner: TComponent); override;
  73.   end;
  74.  
  75.   TImgNextButton = class(TImgDataSetButton)
  76.   public
  77.     constructor Create(AOwner: TComponent); override;
  78.   end;
  79.  
  80.   TImgPriorPageButton = class(TImgDataSetButton)
  81.   public
  82.     constructor Create(AOwner: TComponent); override;
  83.   end;
  84.  
  85.   TImgNextPageButton = class(TImgDataSetButton)
  86.   public
  87.     constructor Create(AOwner: TComponent); override;
  88.   end;
  89.  
  90.   TImgUndoButton = class(TImgDataSetButton)
  91.   public
  92.     constructor Create(AOwner: TComponent); override;
  93.   end;
  94.  
  95.   TImgDeleteButton = class(TImgDataSetButton)
  96.   public
  97.     constructor Create(AOwner: TComponent); override;
  98.   end;
  99.  
  100.   TImgInsertButton = class(TImgDataSetButton)
  101.   public
  102.     constructor Create(AOwner: TComponent); override;
  103.   end;
  104.  
  105.   TImgPostButton = class(TImgDataSetButton)
  106.   public
  107.     constructor Create(AOwner: TComponent); override;
  108.   end;
  109.  
  110.   TImgXMLButton = class(TXMLButton, IImgComponent)
  111.   private
  112.     FPathURL: string;
  113.     FAlt: string;
  114.     FSrc: string;
  115.     FBorder: Boolean;
  116.     function GetAlt: string;
  117.     function GetSrc: string;
  118.   protected
  119.     DefaultAlt: string;
  120.     DefaultSrc: string;
  121.     function GetSrcUrl: string;
  122.     { IImgComponent }
  123.     procedure SetPathURL(APath: string);
  124.   published
  125.     property Custom;
  126.     property Alt: string read GetAlt write FAlt;
  127.     property Style;
  128.     property StyleRule;
  129.     property XMLBroker;
  130.     property XMLUseParent;
  131.     property Src: string read GetSrc write FSrc;
  132.     property Border: Boolean read FBorder write FBorder;
  133.   end;
  134.  
  135.   TImgApplyUpdatesButton = class(TImgXMLButton, IScriptComponent)
  136.   protected
  137.     { IWebContent implementation }
  138.     function ImplContent(Options: TWebContentOptions;
  139.       ParentLayout: TLayout): string; override;
  140.     { IScriptComponent }
  141.     procedure AddElements(AddIntf: IAddScriptElements);
  142.     function GetSubComponents: TObject;
  143.   public
  144.     constructor Create(AOwner: TComponent); override;
  145.   end;
  146.  
  147. implementation
  148.  
  149. uses WebConst, SysUtils, MidProd;
  150.  
  151. const
  152.   DefaultGridButtons: array[0..10] of TWebButtonClass =
  153.   (TImgFirstButton, TImgPriorPageButton, TImgPriorButton, TImgNextButton,
  154.   TImgNextPageButton, TImgLastButton, TImgInsertButton, TImgDeleteButton,
  155.   TImgUndoButton, TImgPostButton, TImgApplyUpdatesButton);
  156.  
  157.   DefaultFormButtons: array[0..8] of TWebButtonClass =
  158.   (TImgFirstButton, TImgPriorButton, TImgNextButton,
  159.   TImgLastButton, TImgInsertButton, TImgDeleteButton,
  160.   TImgUndoButton, TImgPostButton, TImgApplyUpdatesButton);
  161.  
  162. resourcestring
  163.   sFirstButton = 'First';
  164.   sLastButton = 'Last';
  165.   sPriorButton = 'Prior';
  166.   sNextButton = 'Next';
  167.   sPriorPageButton = 'Prior Page';
  168.   sNextPageButton = 'Next Page';
  169.   sDeleteButton = 'Delete';
  170.   sInsertButton = 'Insert';
  171.   sUndoButton = 'Undo';
  172.   sPostButton = 'Post';
  173.   sApplyUpdates = 'Apply Updates';
  174.   sXMLComponentNotDefined = '%s.XMLComponent = nil';
  175.  
  176. procedure TImgDataNavigator.GetDefaultButtons;
  177. var
  178.   XMLDisplay: TComponent;
  179.   Intf: IXMLDisplay;
  180.   Count: Integer;
  181.   Grid: Boolean;
  182. begin
  183.   Grid := False;
  184.   XMLDisplay := GetXMLDisplayComponent;
  185.   if Assigned(XMLDisplay) then
  186.     if XMLDisplay.GetInterface(IXMLDisplay, Intf) then
  187.       Grid := Intf.IsMultipleRecordView;
  188.   if Grid then
  189.     Count := Length(DefaultGridButtons)
  190.   else
  191.     Count := Length(DefaultFormButtons);
  192.   if Assigned(DefaultWebComponents) and
  193.     (DefaultWebComponents.Count <> Count) then
  194.   begin
  195.     DefaultWebComponents.Free;
  196.     DefaultWebComponents := nil;
  197.   end;
  198.   if not Assigned(DefaultWebComponents) then
  199.   begin
  200.     DefaultWebComponents := TWebComponentList.Create(Self);
  201.     if Grid then
  202.       CreateDefaultButtonClasses(DefaultGridButtons, WebFieldControls)
  203.     else
  204.       CreateDefaultButtonClasses(DefaultFormButtons, WebFieldControls)
  205.   end;
  206. end;
  207.  
  208. function TImgDataNavigator.ImplContent(Options: TWebContentOptions;
  209.   ParentLayout: TLayout): string;
  210.  
  211. var
  212.   Path: string;
  213.   Button: TComponent;
  214.   I: Integer;
  215.   ImgComponent: IImgComponent;
  216.   Dispatcher: TCustomWebDispatcher;
  217. begin
  218.   // Initialize image paths
  219.   Path := ImagePathURL;
  220.   if (not (csDesigning in ComponentState)) and (Path = '') then
  221.   begin
  222.     if Path = '' then
  223.     begin
  224.       Dispatcher := FindDispatcher(Self);
  225.       if Assigned(Dispatcher) and Assigned(Dispatcher.Request) then
  226.         Path := PathInfoToRelativePath(Dispatcher.Request.PathInfo);
  227.     end;
  228.   end;
  229.   for I := 0 to VisibleButtons.Count - 1 do
  230.   begin
  231.     Button := VisibleButtons.WebComponents[I];
  232.     if Button.GetInterface(IImgComponent, ImgComponent) then
  233.       ImgComponent.SetPathURL(Path);
  234.   end;
  235.   Result := inherited ImplContent(Options, ParentLayout);
  236. end;
  237.  
  238. procedure TImgDataNavigator.SetImagePathURL(const Value: string);
  239. var
  240.   NewValue: string;
  241. begin
  242.   if Value <> '' then
  243.   begin
  244.     NewValue := DosPathToUnixPath(Value);
  245.     if not IsDelimiter('/', NewValue, Length(NewValue)) then
  246.       NewValue := NewValue + '/';
  247.   end;
  248.   FImagePathURL := NewValue;
  249. end;
  250.  
  251.  
  252. { TImgDataSetButton }
  253.  
  254.  
  255. function TImgDataSetButton.GetAlt: string;
  256. begin
  257.   if FAlt = '' then
  258.     Result := DefaultAlt
  259.   else
  260.     Result := FAlt;
  261. end;
  262.  
  263. function TImgDataSetButton.GetMethodName: string;
  264. begin
  265.   Result := XMLMethodName;
  266. end;
  267.  
  268. function TImgDataSetButton.GetSrc: string;
  269. begin
  270.   if FSrc = '' then
  271.     Result := DefaultSrc
  272.   else
  273.     Result := FSrc;
  274. end;
  275.  
  276. function TImgDataSetButton.GetSrcUrl: string;
  277. begin
  278.   Result := FPathURL + Src;
  279. end;
  280.  
  281. procedure TImgDataSetButton.AddElements(AddIntf: IAddScriptElements);
  282. begin
  283.   if (XMLComponent = nil) and (Self.Name <> '') then
  284.     AddIntf.AddError(Format(sXMLComponentNotDefined, [Self.Name]));
  285. end;
  286.  
  287. function TImgDataSetButton.ImplContent(Options: TWebContentOptions;
  288.   ParentLayout: TLayout): string;
  289. var
  290.   Attrs: string;
  291.   Intf: ILayoutWebContent;
  292. begin
  293.   //AddQuotedAttrib(Attrs, 'NAME', Name);
  294.   AddQuotedAttrib(Attrs, 'STYLE', Style);
  295.   AddQuotedAttrib(Attrs, 'CLASS', StyleRule);
  296.   AddQuotedAttrib(Attrs, 'ALT', Alt);
  297.   AddQuotedAttrib(Attrs, 'SRC', GetSrcUrl);
  298.   AddCustomAttrib(Attrs, Custom);
  299.   if not Border then
  300.     AddStringAttrib(Attrs, 'BORDER', '0');
  301.   if not (coNoScript in Options.Flags) then
  302.   begin
  303.     Result :=
  304.       Format('<A HREF="" onclick=''if(%3:s)%1:s.%2:s();return false;''><IMG %0:s/></A>'#13#10,
  305.         [Attrs, Self.GetXmlDisplayName, Self.GetMethodName, sXMLReadyVar]);
  306.   end
  307.   else
  308.     Result :=
  309.       Format('<A><IMG %0:s/></A>'#13#10,
  310.         [Attrs]);
  311.   if Assigned(ParentLayout) and ParentLayout.GetInterface(ILayoutWebContent, Intf) then
  312.     Result := Intf.LayoutButton(Result, GetLayoutAttributes);
  313. end;
  314.  
  315. procedure TImgDataSetButton.SetPathURL(APath: string);
  316. begin
  317.   FPathURL := APath;
  318. end;
  319.  
  320. function TImgDataSetButton.GetSubComponents: TObject;
  321. begin
  322.   Result := nil;
  323. end;
  324.  
  325. { TImgFirstButton }
  326.  
  327. constructor TImgFirstButton.Create(AOwner: TComponent);
  328. begin
  329.   inherited;
  330.   DefaultAlt := sFirstButton;
  331.   DefaultSrc := 'first.gif';
  332.   XMLMethodName := 'first';
  333. end;
  334.  
  335. { TImgLastButton }
  336.  
  337. constructor TImgLastButton.Create(AOwner: TComponent);
  338. begin
  339.   inherited;
  340.   DefaultAlt := sLastButton;
  341.   DefaultSrc := 'last.gif';
  342.   XMLMethodName := 'last';
  343. end;
  344.  
  345. { TImgPriorButton }
  346.  
  347. constructor TImgPriorButton.Create(AOwner: TComponent);
  348. begin
  349.   inherited;
  350.   DefaultAlt := sPriorButton;
  351.   DefaultSrc := 'prior.gif';
  352.   XMLMethodName := 'up';
  353. end;
  354.  
  355. { TImgNextButton }
  356.  
  357. constructor TImgNextButton.Create(AOwner: TComponent);
  358. begin
  359.   inherited;
  360.   DefaultAlt := sNextButton;
  361.   DefaultSrc := 'next.gif';
  362.   XMLMethodName := 'down';
  363. end;
  364.  
  365. { TImgPriorPageButton }
  366.  
  367. constructor TImgPriorPageButton.Create(AOwner: TComponent);
  368. begin
  369.   inherited;
  370.   DefaultAlt := sPriorPageButton;
  371.   DefaultSrc := 'priorpage.gif';
  372.   XMLMethodName := 'pgup';
  373. end;
  374.  
  375. { TImgNextPageButton }
  376.  
  377. constructor TImgNextPageButton.Create(AOwner: TComponent);
  378. begin
  379.   inherited;
  380.   DefaultAlt := sNextPageButton;
  381.   DefaultSrc := 'nextpage.gif';
  382.   XMLMethodName := 'pgdown';
  383. end;
  384.  
  385. { TImgUndoButton }
  386.  
  387. constructor TImgUndoButton.Create(AOwner: TComponent);
  388. begin
  389.   inherited;
  390.   DefaultAlt := sUndoButton;
  391.   DefaultSrc := 'undo.gif';
  392.   XMLMethodName := 'undo';
  393. end;
  394.  
  395. { TImgDeleteButton }
  396.  
  397. constructor TImgDeleteButton.Create(AOwner: TComponent);
  398. begin
  399.   inherited;
  400.   DefaultAlt := sDeleteButton;
  401.   DefaultSrc := 'delete.gif';
  402.   XMLMethodName := 'removeRow';
  403. end;
  404.  
  405. { TImgInsertButton }
  406.  
  407. constructor TImgInsertButton.Create(AOwner: TComponent);
  408. begin
  409.   inherited;
  410.   DefaultAlt := sInsertButton;
  411.   DefaultSrc := 'insert.gif';
  412.   XMLMethodName := 'newRow';
  413. end;
  414.  
  415. { TImgPostButton }
  416.  
  417. constructor TImgPostButton.Create(AOwner: TComponent);
  418. begin
  419.   inherited;
  420.   DefaultAlt := sPostButton;
  421.   DefaultSrc := 'post.gif';
  422.   XMLMethodName := 'post';
  423. end;
  424.  
  425. { TImgXMLButton }
  426.  
  427. function TImgXMLButton.GetAlt: string;
  428. begin
  429.   if FAlt = '' then
  430.     Result := DefaultAlt
  431.   else
  432.     Result := FAlt;
  433. end;
  434.  
  435. function TImgXMLButton.GetSrc: string;
  436. begin
  437.   if FSrc = '' then
  438.     Result := DefaultSrc
  439.   else
  440.     Result := FSrc;
  441. end;
  442.  
  443. function TImgXMLButton.GetSrcUrl: string;
  444. begin
  445.   Result := FPathURL + Src;
  446. end;
  447.  
  448.  
  449. procedure TImgXMLButton.SetPathURL(APath: string);
  450. begin
  451.   FPathURL := APath;
  452. end;
  453.  
  454. { TImgApplyUpdatesButton }
  455.  
  456. procedure TImgApplyUpdatesButton.AddElements(AddIntf: IAddScriptElements);
  457. begin
  458.   DeclareSubmitForm(Self, XMLData.XMLBroker, AddIntf);
  459. end;
  460.  
  461. constructor TImgApplyUpdatesButton.Create(AOwner: TComponent);
  462. begin
  463.   inherited;
  464.   DefaultAlt := sApplyUpdates;
  465.   DefaultSrc := 'applyupdates.gif';
  466. end;
  467.  
  468. function TImgApplyUpdatesButton.GetSubComponents: TObject;
  469. begin
  470.   Result := nil;
  471. end;
  472.  
  473. function TImgApplyUpdatesButton.ImplContent(Options: TWebContentOptions;
  474.   ParentLayout: TLayout): string;
  475. var
  476.   Attrs: string;
  477.   Intf: ILayoutWebContent;
  478.   FormVarName: string;
  479.   RowSetVarName: string;
  480. begin
  481.   AddQuotedAttrib(Attrs, 'NAME', Name);
  482.   AddQuotedAttrib(Attrs, 'STYLE', Style);
  483.   AddQuotedAttrib(Attrs, 'CLASS', StyleRule);
  484.   AddQuotedAttrib(Attrs, 'ALT', Alt);
  485.   AddQuotedAttrib(Attrs, 'SRC', GetSrcUrl);
  486.   AddCustomAttrib(Attrs, Custom);
  487.   if not Border then
  488.     AddStringAttrib(Attrs, 'BORDER', '0');
  489.   if Assigned(XMLData.XMLBroker) then
  490.   begin
  491.     FormVarName := XMLData.XMLBroker.SubmitFormVarName;
  492.     RowSetVarName := XMLData.XMLBroker.RowSetVarName(nil);  
  493.   end;
  494.   if not (coNoScript in Options.Flags) then
  495.   begin
  496.     Result :=
  497.       Format('<A HREF="" onclick=''if(%3:s)%1:s.Apply(%2:s, %2:s.postdelta);return false;''><IMG %0:s/></A>'#13#10,
  498.         [Attrs, RowSetVarName, FormVarName,sXMLReadyVar]);
  499.   end
  500.   else
  501.     Result :=
  502.       Format('<A><IMG %0:s/></A>'#13#10,
  503.         [Attrs]);
  504.   if Assigned(ParentLayout) and ParentLayout.GetInterface(ILayoutWebContent, Intf) then
  505.     Result := Intf.LayoutButton(Result, GetLayoutAttributes);
  506. end;
  507.  
  508. end.
  509.