Back to index

lightning-sunbird  0.9+nobinonly
nsTitleBarFrame.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Original Author: Eric J. Burley (ericb@neoplanet.com)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsCOMPtr.h"
00040 #include "nsTitleBarFrame.h"
00041 #include "nsIContent.h"
00042 #include "nsIDocument.h"
00043 #include "nsIDOMXULDocument.h"
00044 #include "nsIDOMNodeList.h"
00045 #include "nsHTMLAtoms.h"
00046 #include "nsIWidget.h"
00047 #include "nsPresContext.h"
00048 #include "nsIDOMWindowInternal.h"
00049 #include "nsIScriptGlobalObject.h"
00050 #include "nsIViewManager.h"
00051 #include "nsGUIEvent.h"
00052 
00053 //
00054 // NS_NewTitleBarFrame
00055 //
00056 // Creates a new TitleBar frame and returns it in |aNewFrame|
00057 //
00058 nsresult
00059 NS_NewTitleBarFrame( nsIPresShell* aPresShell, nsIFrame** aNewFrame )
00060 {
00061   NS_PRECONDITION(aNewFrame, "null OUT ptr");
00062   if (nsnull == aNewFrame) {
00063     return NS_ERROR_NULL_POINTER;
00064   }
00065   nsTitleBarFrame* it = new (aPresShell) nsTitleBarFrame(aPresShell);
00066   if (nsnull == it)
00067     return NS_ERROR_OUT_OF_MEMORY;
00068 
00069   // it->SetFlags(aFlags);
00070   *aNewFrame = it;
00071   return NS_OK;
00072   
00073 } // NS_NewTitleBarFrame
00074 
00075 nsTitleBarFrame::nsTitleBarFrame(nsIPresShell* aPresShell)
00076 :nsBoxFrame(aPresShell, PR_FALSE) 
00077 {
00078        mTrackingMouseMove = PR_FALSE;
00079 }
00080 
00081 
00082 
00083 NS_IMETHODIMP  nsTitleBarFrame::Init(nsPresContext*  aPresContext,
00084                 nsIContent*      aContent,
00085                 nsIFrame*        aParent,
00086                 nsStyleContext*  aContext,
00087                 nsIFrame*        asPrevInFlow)
00088 {
00089        nsresult rv = nsBoxFrame::Init(aPresContext, aContent, aParent, aContext, asPrevInFlow);
00090 
00091        CreateViewForFrame(aPresContext,this,aContext,PR_TRUE);
00092 
00093        return rv;
00094 }
00095 
00096 
00097 
00098 
00099 NS_IMETHODIMP
00100 nsTitleBarFrame::GetMouseThrough(PRBool& aMouseThrough)
00101 {
00102   aMouseThrough = PR_FALSE;
00103   return NS_OK;
00104 }
00105 
00106 NS_IMETHODIMP nsTitleBarFrame::GetFrameForPoint(const nsPoint& aPoint, 
00107                                     nsFramePaintLayer aWhichLayer,
00108                                     nsIFrame**     aFrame)
00109 {
00110   // override, since we don't want children to get events
00111   return nsFrame::GetFrameForPoint(aPoint, aWhichLayer, aFrame);
00112 }
00113 
00114 
00115 
00116 
00117 
00118        
00119 NS_IMETHODIMP
00120 nsTitleBarFrame::HandleEvent(nsPresContext* aPresContext, 
00121                                       nsGUIEvent* aEvent,
00122                                       nsEventStatus* aEventStatus)
00123 {
00124 
00125 
00126   PRBool doDefault = PR_TRUE;
00127 
00128   switch (aEvent->message) {
00129 
00130         case NS_MOUSE_LEFT_BUTTON_DOWN:   {
00131                       
00132                       // we're tracking.
00133                       mTrackingMouseMove = PR_TRUE;
00134                       
00135                       // start capture.          
00136                       CaptureMouseEvents(aPresContext,PR_TRUE);
00137 
00138 
00139                       
00140                       // remember current mouse coordinates.
00141                       mLastPoint = aEvent->refPoint;
00142 
00143                       *aEventStatus = nsEventStatus_eConsumeNoDefault;
00144                       doDefault = PR_FALSE;
00145                }
00146                break;
00147                
00148 
00149         case NS_MOUSE_LEFT_BUTTON_UP: {
00150 
00151                       if(mTrackingMouseMove)
00152                       {
00153                              // we're done tracking.
00154                              mTrackingMouseMove = PR_FALSE;
00155                              
00156                              // end capture
00157                              CaptureMouseEvents(aPresContext,PR_FALSE);
00158 
00159                              *aEventStatus = nsEventStatus_eConsumeNoDefault;
00160                              doDefault = PR_FALSE;
00161                       }
00162                }
00163                break;
00164 
00165         case NS_MOUSE_MOVE: {
00166                       if(mTrackingMouseMove)
00167                       {                                                       
00168                         // get the document and the global script object - should this be cached?
00169                         nsCOMPtr<nsIDOMWindowInternal>
00170            window(do_QueryInterface(aPresContext->PresShell()->GetDocument()->GetScriptGlobalObject()));
00171 
00172          if (window) {
00173            nsPoint nsMoveBy = aEvent->refPoint - mLastPoint;
00174            window->MoveBy(nsMoveBy.x,nsMoveBy.y);
00175          }
00176                              
00177                              *aEventStatus = nsEventStatus_eConsumeNoDefault;                            
00178                              
00179                              doDefault = PR_FALSE;
00180                       }
00181                }
00182                break;
00183 
00184 
00185 
00186     case NS_MOUSE_LEFT_CLICK:
00187       MouseClicked(aPresContext, aEvent);
00188       break;
00189   }
00190   
00191   if ( doDefault )  
00192          return nsBoxFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
00193   else
00194          return NS_OK;
00195 }
00196 
00197 NS_IMETHODIMP 
00198 nsTitleBarFrame::CaptureMouseEvents(nsPresContext* aPresContext,PRBool aGrabMouseEvents)
00199 {
00200        // get its view
00201   nsIView* view = GetView();
00202   PRBool result;
00203 
00204   if (view) {
00205     nsIViewManager* viewMan = view->GetViewManager();
00206     if (viewMan) {
00207       // nsIWidget* widget = view->GetWidget();
00208       if (aGrabMouseEvents) {
00209         viewMan->GrabMouseEvents(view,result);
00210         //mIsCapturingMouseEvents = PR_TRUE;
00211         //widget->CaptureMouse(PR_TRUE);
00212       } else {
00213         viewMan->GrabMouseEvents(nsnull,result);
00214         //mIsCapturingMouseEvents = PR_FALSE;
00215         //widget->CaptureMouse(PR_FALSE);
00216       }
00217     }
00218   }
00219 
00220   return NS_OK;
00221 
00222 }
00223 
00224 
00225 
00226 void 
00227 nsTitleBarFrame::MouseClicked(nsPresContext* aPresContext, nsGUIEvent* aEvent) 
00228 {
00229   // Execute the oncommand event handler.
00230   nsEventStatus status = nsEventStatus_eIgnore;
00231 
00232   nsXULCommandEvent event(aEvent ? NS_IS_TRUSTED_EVENT(aEvent) : PR_FALSE,
00233                           NS_XUL_COMMAND, nsnull);
00234 
00235   mContent->HandleDOMEvent(aPresContext, &event, nsnull, NS_EVENT_FLAG_INIT,
00236                            &status);
00237 }