home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / gui / CProxyPane.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  10.1 KB  |  364 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. // ===========================================================================
  20. //    CProxyPane.cp
  21. //
  22. // ===========================================================================
  23.  
  24. #include <string>
  25.  
  26. #include "CProxyPane.h"
  27.  
  28. #include <UException.h>
  29. #include <LCaption.h>
  30. #include <LView.h>
  31. #include <LString.h>
  32. #include <UAttachments.h>
  33.  
  34. #include "CStandardFlexTable.h"
  35. #include "CViewUtils.h"
  36. #include "CProxyDragTask.h"
  37. #include "CNetscapeWindow.h"
  38. #include "CBrowserContext.h"
  39. #include "CSuspenderResumer.h"
  40. #include "resgui.h"
  41. #include "mkgeturl.h"
  42. #include "CURLDispatcher.h"
  43. #include "macutil.h"
  44. #include "CURLDragHelper.h"
  45.  
  46. // ---------------------------------------------------------------------------
  47. //        Ñ CProxyPane
  48. // ---------------------------------------------------------------------------
  49.  
  50. CProxyPane::CProxyPane(
  51.     LStream*        inStream)
  52.         :    mProxyView(nil),
  53.             mSendDataUPP(nil),
  54.             mMouseInFrame(false),
  55.             LDragAndDrop(GetMacPort(), this),
  56.             
  57.             super(inStream)
  58. {
  59.     SetIconIDs(kProxyIconNormalID, kProxyIconMouseOverID);
  60. }
  61.  
  62. // ---------------------------------------------------------------------------
  63. //        Ñ ~CProxyPane
  64. // ---------------------------------------------------------------------------
  65.  
  66. CProxyPane::~CProxyPane()
  67. {
  68.     if (mSendDataUPP)
  69.     {
  70.         DisposeRoutineDescriptor(mSendDataUPP);
  71.     }
  72. }
  73.  
  74. // ---------------------------------------------------------------------------
  75. //        Ñ FinishCreateSelf
  76. // ---------------------------------------------------------------------------
  77.  
  78. void
  79. CProxyPane::FinishCreateSelf()
  80. {
  81.     super::FinishCreateSelf();
  82.  
  83.     mSendDataUPP = NewDragSendDataProc(LDropArea::HandleDragSendData);
  84.     ThrowIfNil_(mSendDataUPP);
  85.     
  86.     ThrowIfNil_(mWindow            = dynamic_cast<CNetscapeWindow*>(LWindow::FetchWindowObject(GetMacPort())));
  87.     ThrowIfNil_(mProxyView        = dynamic_cast<LView*>(mWindow->FindPaneByID(kProxyViewID)));
  88.     ThrowIfNil_(mIconText        = dynamic_cast<LCaption*>(mWindow->FindPaneByID(kProxyTitleCaptionID)));
  89.  
  90. }
  91.  
  92. // ---------------------------------------------------------------------------
  93. //        Ñ ListenToMessage
  94. // ---------------------------------------------------------------------------
  95.  
  96. void
  97. CProxyPane::ListenToMessage(
  98.     MessageT        inMessage,
  99.     void*            ioParam)
  100. {
  101.     switch (inMessage)
  102.     {
  103.         case msg_NSCDocTitleChanged: // browser, mail window and message window case
  104.             {
  105.                 const char* theCTitle = static_cast<const char*>(ioParam);
  106.                 
  107.                 if (theCTitle)
  108.                 {
  109.                     const char* tempCaption;
  110.                     URL_Struct* theURL = nil;
  111.                     if (strlen(theCTitle))            // We have a title
  112.                         tempCaption = theCTitle;
  113.                     else
  114.                     {
  115.                         // We don't have a title: use the URL instead of the title                        
  116.                         ThrowIfNil_(mWindow->GetWindowContext());
  117.                         theURL = mWindow->CreateURLForProxyDrag(nil);
  118.                         if (theURL && theURL->address)
  119.                             tempCaption = theURL->address;
  120.                     }
  121.                     
  122.                     // now we at least have something (either title or url). Handle middle
  123.                     // truncation, etc and then set the new title.
  124.                     cstring finalCaption = CURLDragHelper::MakeIconTextValid( tempCaption );
  125.                     if (theURL)
  126.                         NET_FreeURLStruct( theURL ); // we're done with the address string.
  127.                     mIconText->SetDescriptor(LStr255(finalCaption));
  128.                                         
  129.                     Boolean enabled = false;
  130.                     Boolean a; Char16 b; Str255 c;
  131.                     LCommander* target = LCommander::GetTarget();
  132.                     if (target)
  133.                         target->ProcessCommandStatus(cmd_AddToBookmarks, enabled, a,b, c);
  134.                     if (enabled)
  135.                         Enable();
  136.                     else
  137.                         Disable();
  138.  
  139.                 }
  140.                 else
  141.                     Disable();
  142.             }
  143.             break;
  144.     }
  145. }
  146.  
  147. // ---------------------------------------------------------------------------
  148. //        Ñ ToggleIcon
  149. // ---------------------------------------------------------------------------
  150.  
  151. void
  152. CProxyPane::ToggleIcon(
  153.     ResIDT            inResID)
  154. {
  155.     StPortOriginState thePortOriginState(GetMacPort());
  156.  
  157.     SetIconResourceID(inResID);
  158.     LoadIconSuiteHandle();
  159.     
  160.     Refresh();
  161.     UpdatePort();
  162. }
  163.  
  164. // ---------------------------------------------------------------------------
  165. //        Ñ ToggleIconSelected
  166. // ---------------------------------------------------------------------------
  167.  
  168. void
  169. CProxyPane::ToggleIconSelected(
  170.     Boolean            inIconIsSelected)
  171. {
  172.     StPortOriginState thePortOriginState(GetMacPort());
  173.     
  174.     if (inIconIsSelected)
  175.     {
  176.         SetIconTransform(kTransformSelected);
  177.     }
  178.     else
  179.     {
  180.         SetIconTransform(kTransformNone);
  181.     }
  182.     
  183.     Refresh();
  184.     UpdatePort();
  185. }                        
  186.  
  187. // ---------------------------------------------------------------------------
  188. //        Ñ MouseEnter
  189. // ---------------------------------------------------------------------------
  190.  
  191. void
  192. CProxyPane::MouseEnter(
  193.     Point                /*inPortPt*/,
  194.     const EventRecord&    /*inMacEvent*/)
  195. {
  196.     mMouseInFrame = true;
  197.     if (IsEnabled() && !CSuspenderResumer::IsSuspended())
  198.     {
  199.         // We intentionally "animate" the icon even when it's not in
  200.         // a frontmost window. Since the animation is intended to 
  201.         // somehow signify that the icon is draggable, it makes sense
  202.         // for it to animate even in a deactive window. Recall that
  203.         // we use delay select to do the proper drag UI so that icon
  204.         // is really active even in an inactive window.
  205.         
  206.         ToggleIcon(mProxyIconMouseOverID);
  207.     }
  208. }
  209.         
  210. // ---------------------------------------------------------------------------
  211. //        Ñ MouseLeave
  212. // ---------------------------------------------------------------------------
  213.                                     
  214. void
  215. CProxyPane::MouseLeave()
  216. {
  217.     mMouseInFrame = false;
  218.     if (IsEnabled() && !CSuspenderResumer::IsSuspended())
  219.     {
  220.         ToggleIcon(mProxyIconNormalID);
  221.     }
  222. }
  223.  
  224. // ---------------------------------------------------------------------------
  225. //        Ñ Click
  226. // ---------------------------------------------------------------------------
  227. //    We override Click here so we can handle the delay select appropriately for
  228. //    dragging the page proxy.
  229.  
  230. void
  231. CProxyPane::Click(
  232.     SMouseDownEvent&        inMouseDown)
  233. {
  234.     PortToLocalPoint(inMouseDown.whereLocal);
  235.     UpdateClickCount(inMouseDown);
  236.     
  237.     if (ExecuteAttachments(msg_Click, &inMouseDown))
  238.     {
  239.         ClickSelf(inMouseDown);
  240.     }
  241. }
  242.  
  243. // ---------------------------------------------------------------------------
  244. //        Ñ ClickSelf
  245. // ---------------------------------------------------------------------------
  246.  
  247. void
  248. CProxyPane::ClickSelf(
  249.     const SMouseDownEvent&    inMouseDown)
  250. {    
  251.     FocusDraw();
  252.     
  253.     Point theGlobalPoint = inMouseDown.wherePort;
  254.     PortToGlobalPoint(theGlobalPoint);
  255.     
  256.     StToggleIconSelected theToggleIconSelected(*this, true);
  257.     
  258.     if (::WaitMouseMoved(theGlobalPoint))
  259.     {
  260.         if (LDropArea::DragAndDropIsPresent())
  261.         {
  262.             CProxyDragTask theTask(
  263.                 *mProxyView,
  264.                 *this,
  265.                 *mIconText,
  266.                 inMouseDown.macEvent,
  267.                 mWindow->CreateExtraFlavorAdder());
  268.                                                 
  269.             OSErr theErr = ::SetDragSendProc(
  270.                 theTask.GetDragReference(),
  271.                 mSendDataUPP,
  272.                 (LDragAndDrop*)this);
  273.             ThrowIfOSErr_(theErr);
  274.             
  275.             theTask.DoDrag();
  276.         }
  277.     }
  278.     else
  279.     {
  280.         if (GetClickCount() == 2)
  281.         {
  282.             LCommander* target = LCommander::GetTarget();
  283.             if (target)
  284.                 target->ObeyCommand(cmd_AddToBookmarks, nil);
  285.             
  286.             // Provide visual feedback by hilighting the bookmark menu -- mdp
  287.             const long kVisualDelay = 8;        // 8 ticks, as recommended by Apple
  288.             unsigned long temp;
  289.             ::HiliteMenu( cBookmarksMenuID );
  290.             ::Delay( kVisualDelay, &temp );
  291.             ::HiliteMenu( 0 );
  292.             
  293.             // Also put a message in the status window -- mdp
  294.             Str255 mesg;
  295.             MWContext* context = *mWindow->GetWindowContext();
  296.             ::GetIndString( mesg, 7099, 20 );
  297.             FE_Progress( context, CStr255(mesg) );
  298.         }
  299.     }
  300. }
  301.  
  302. // ---------------------------------------------------------------------------
  303. //        Ñ ActivateSelf
  304. // ---------------------------------------------------------------------------
  305.  
  306. void
  307. CProxyPane::ActivateSelf()
  308. {
  309.     // Intentionally blank. The inherited version draws icon enabled (to undo what
  310.     // was done in DeactivateSelf() which is not what we want.
  311. }
  312.  
  313. // ---------------------------------------------------------------------------
  314. //        Ñ DeactivateSelf
  315. // ---------------------------------------------------------------------------
  316.  
  317. void
  318. CProxyPane::DeactivateSelf()
  319. {
  320.     // Intentionally blank. The inherited version draws icon disabled which
  321.     // is not what we want.
  322. }
  323.  
  324.  
  325. // ---------------------------------------------------------------------------
  326. //        Ñ DoDragSendData
  327. // ---------------------------------------------------------------------------
  328.  
  329. void
  330. CProxyPane::DoDragSendData(
  331.     FlavorType        inFlavor,
  332.     ItemReference    inItemRef,
  333.     DragReference    inDragRef)
  334. {
  335.     char title[256];
  336.     URL_Struct* url = mWindow->CreateURLForProxyDrag(title);
  337.     CURLDragHelper::DoDragSendData(url->address, title, inFlavor, inItemRef, 
  338.                                         inDragRef);
  339. } // CProxyPane::DoDragSendData
  340.  
  341.  
  342. //-----------------------------------
  343. void CProxyPane::HandleEnablingPolicy()
  344. //-----------------------------------
  345. {
  346.     // We may be in background, and we need to allow dragging.  So the idea is to
  347.     // enable the icon if it WOULD be enabled when the window was active.  So
  348.     // do nothing (neither enable nor disable) when in the background.
  349.     if (mWindow->IsOnDuty())
  350.     {
  351.         LCommander* target = LCommander::GetTarget();
  352.         if (target)
  353.         {
  354.             Boolean enabled = false;
  355.             Boolean a; Char16 b; Str255 c;
  356.             target->ProcessCommandStatus(cmd_AddToBookmarks, enabled, a,b, c);
  357.             if (enabled)
  358.                 Enable();
  359.             else
  360.                 Disable();    
  361.         }
  362.     }
  363. } // CProxyPane::HandleEnablingPolicy
  364.