Bug 439354 - OS X toolbar background doesn't have a good gradient. Part 1 (widget...
[mozilla-central.git] / widget / src / xpwidgets / nsBaseWidget.cpp
blob01a46159cb97b4f303e5b9a5372c9d7074a225e6
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Dean Tessman <dean_tessman@hotmail.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsBaseWidget.h"
40 #include "nsIDeviceContext.h"
41 #include "nsCOMPtr.h"
42 #include "nsGfxCIID.h"
43 #include "nsWidgetsCID.h"
44 #include "nsIFullScreen.h"
45 #include "nsServiceManagerUtils.h"
46 #include "nsIScreenManager.h"
47 #include "nsAppDirectoryServiceDefs.h"
48 #include "nsISimpleEnumerator.h"
49 #include "nsIContent.h"
51 #ifdef DEBUG
52 #include "nsIServiceManager.h"
53 #include "nsIPrefService.h"
54 #include "nsIPrefBranch2.h"
55 #include "nsIObserver.h"
57 static void debug_RegisterPrefCallbacks();
59 static PRBool debug_InSecureKeyboardInputMode = PR_FALSE;
60 #endif
62 #ifdef NOISY_WIDGET_LEAKS
63 static PRInt32 gNumWidgets;
64 #endif
66 nsIContent* nsBaseWidget::mLastRollup = nsnull;
68 // nsBaseWidget
69 NS_IMPL_ISUPPORTS1(nsBaseWidget, nsIWidget)
72 nsAutoRollup::nsAutoRollup()
74 // remember if mLastRollup was null, and only clear it upon destruction
75 // if so. This prevents recursive usage of nsAutoRollup from clearing
76 // mLastRollup when it shouldn't.
77 wasClear = !nsBaseWidget::mLastRollup;
80 nsAutoRollup::~nsAutoRollup()
82 if (nsBaseWidget::mLastRollup && wasClear) {
83 NS_RELEASE(nsBaseWidget::mLastRollup);
87 //-------------------------------------------------------------------------
89 // nsBaseWidget constructor
91 //-------------------------------------------------------------------------
93 nsBaseWidget::nsBaseWidget()
94 : mClientData(nsnull)
95 , mEventCallback(nsnull)
96 , mContext(nsnull)
97 , mToolkit(nsnull)
98 , mEventListener(nsnull)
99 , mCursor(eCursor_standard)
100 , mWindowType(eWindowType_child)
101 , mBorderStyle(eBorderStyle_none)
102 , mIsShiftDown(PR_FALSE)
103 , mIsControlDown(PR_FALSE)
104 , mIsAltDown(PR_FALSE)
105 , mIsDestroying(PR_FALSE)
106 , mOnDestroyCalled(PR_FALSE)
107 , mBounds(0,0,0,0)
108 , mOriginalBounds(nsnull)
109 , mZIndex(0)
110 , mSizeMode(nsSizeMode_Normal)
112 #ifdef NOISY_WIDGET_LEAKS
113 gNumWidgets++;
114 printf("WIDGETS+ = %d\n", gNumWidgets);
115 #endif
117 #ifdef DEBUG
118 debug_RegisterPrefCallbacks();
119 #endif
123 //-------------------------------------------------------------------------
125 // nsBaseWidget destructor
127 //-------------------------------------------------------------------------
128 nsBaseWidget::~nsBaseWidget()
130 #ifdef NOISY_WIDGET_LEAKS
131 gNumWidgets--;
132 printf("WIDGETS- = %d\n", gNumWidgets);
133 #endif
135 NS_IF_RELEASE(mToolkit);
136 NS_IF_RELEASE(mContext);
137 if (mOriginalBounds)
138 delete mOriginalBounds;
142 //-------------------------------------------------------------------------
144 // Basic create.
146 //-------------------------------------------------------------------------
147 void nsBaseWidget::BaseCreate(nsIWidget *aParent,
148 const nsRect &aRect,
149 EVENT_CALLBACK aHandleEventFunction,
150 nsIDeviceContext *aContext,
151 nsIAppShell *aAppShell,
152 nsIToolkit *aToolkit,
153 nsWidgetInitData *aInitData)
155 if (nsnull == mToolkit) {
156 if (nsnull != aToolkit) {
157 mToolkit = (nsIToolkit*)aToolkit;
158 NS_ADDREF(mToolkit);
160 else {
161 if (nsnull != aParent) {
162 mToolkit = aParent->GetToolkit();
163 NS_IF_ADDREF(mToolkit);
165 // it's some top level window with no toolkit passed in.
166 // Create a default toolkit with the current thread
167 #if !defined(USE_TLS_FOR_TOOLKIT)
168 else {
169 static NS_DEFINE_CID(kToolkitCID, NS_TOOLKIT_CID);
171 nsresult res;
172 res = CallCreateInstance(kToolkitCID, &mToolkit);
173 NS_ASSERTION(NS_SUCCEEDED(res), "Can not create a toolkit in nsBaseWidget::Create");
174 if (mToolkit)
175 mToolkit->Init(PR_GetCurrentThread());
177 #else /* USE_TLS_FOR_TOOLKIT */
178 else {
179 nsresult rv;
181 rv = NS_GetCurrentToolkit(&mToolkit);
183 #endif /* USE_TLS_FOR_TOOLKIT */
188 // save the event callback function
189 mEventCallback = aHandleEventFunction;
191 // keep a reference to the device context
192 if (aContext) {
193 mContext = aContext;
194 NS_ADDREF(mContext);
196 else {
197 nsresult res;
199 static NS_DEFINE_CID(kDeviceContextCID, NS_DEVICE_CONTEXT_CID);
201 res = CallCreateInstance(kDeviceContextCID, &mContext);
203 if (NS_SUCCEEDED(res))
204 mContext->Init(nsnull);
207 if (nsnull != aInitData) {
208 PreCreateWidget(aInitData);
211 if (aParent) {
212 aParent->AddChild(this);
216 NS_IMETHODIMP nsBaseWidget::CaptureMouse(PRBool aCapture)
218 return NS_OK;
221 NS_IMETHODIMP nsBaseWidget::Validate()
223 return NS_OK;
226 NS_IMETHODIMP nsBaseWidget::InvalidateRegion(const nsIRegion *aRegion, PRBool aIsSynchronous)
228 return NS_ERROR_FAILURE;
231 //-------------------------------------------------------------------------
233 // Accessor functions to get/set the client data
235 //-------------------------------------------------------------------------
237 NS_IMETHODIMP nsBaseWidget::GetClientData(void*& aClientData)
239 aClientData = mClientData;
240 return NS_OK;
243 NS_IMETHODIMP nsBaseWidget::SetClientData(void* aClientData)
245 mClientData = aClientData;
246 return NS_OK;
249 //-------------------------------------------------------------------------
251 // Close this nsBaseWidget
253 //-------------------------------------------------------------------------
254 NS_METHOD nsBaseWidget::Destroy()
256 // Just in case our parent is the only ref to us
257 nsCOMPtr<nsIWidget> kungFuDeathGrip(this);
259 // disconnect from the parent
260 nsIWidget *parent = GetParent();
261 if (parent) {
262 parent->RemoveChild(this);
264 // disconnect listeners.
265 NS_IF_RELEASE(mEventListener);
267 return NS_OK;
271 //-------------------------------------------------------------------------
273 // Set this nsBaseWidget's parent
275 //-------------------------------------------------------------------------
276 NS_IMETHODIMP nsBaseWidget::SetParent(nsIWidget* aNewParent)
278 return NS_ERROR_NOT_IMPLEMENTED;
282 //-------------------------------------------------------------------------
284 // Get this nsBaseWidget parent
286 //-------------------------------------------------------------------------
287 nsIWidget* nsBaseWidget::GetParent(void)
289 return nsnull;
292 //-------------------------------------------------------------------------
294 // Get this nsBaseWidget top level widget
296 //-------------------------------------------------------------------------
297 nsIWidget* nsBaseWidget::GetTopLevelWidget(PRInt32* aLevelsUp)
299 nsIWidget *topLevelWidget, *widget = this;
300 if (aLevelsUp)
301 *aLevelsUp = -1;
302 while (widget) {
303 topLevelWidget = widget;
304 widget = widget->GetParent();
305 if (aLevelsUp)
306 ++*aLevelsUp;
308 return topLevelWidget;
311 //-------------------------------------------------------------------------
313 // Get this nsBaseWidget's top (non-sheet) parent (if it's a sheet)
315 //-------------------------------------------------------------------------
316 nsIWidget* nsBaseWidget::GetSheetWindowParent(void)
318 return nsnull;
321 //-------------------------------------------------------------------------
323 // Add a child to the list of children
325 //-------------------------------------------------------------------------
326 void nsBaseWidget::AddChild(nsIWidget* aChild)
328 NS_PRECONDITION(!aChild->GetNextSibling() && !aChild->GetPrevSibling(),
329 "aChild not properly removed from its old child list");
331 if (!mFirstChild) {
332 mFirstChild = mLastChild = aChild;
333 } else {
334 // append to the list
335 NS_ASSERTION(mLastChild, "Bogus state");
336 NS_ASSERTION(!mLastChild->GetNextSibling(), "Bogus state");
337 mLastChild->SetNextSibling(aChild);
338 aChild->SetPrevSibling(mLastChild);
339 mLastChild = aChild;
344 //-------------------------------------------------------------------------
346 // Remove a child from the list of children
348 //-------------------------------------------------------------------------
349 void nsBaseWidget::RemoveChild(nsIWidget* aChild)
351 NS_ASSERTION(aChild->GetParent() == this, "Not one of our kids!");
353 if (mLastChild == aChild) {
354 mLastChild = mLastChild->GetPrevSibling();
356 if (mFirstChild == aChild) {
357 mFirstChild = mFirstChild->GetNextSibling();
360 // Now remove from the list. Make sure that we pass ownership of the tail
361 // of the list correctly before we have aChild let go of it.
362 nsIWidget* prev = aChild->GetPrevSibling();
363 nsIWidget* next = aChild->GetNextSibling();
364 if (prev) {
365 prev->SetNextSibling(next);
367 if (next) {
368 next->SetPrevSibling(prev);
371 aChild->SetNextSibling(nsnull);
372 aChild->SetPrevSibling(nsnull);
376 //-------------------------------------------------------------------------
378 // Sets widget's position within its parent's child list.
380 //-------------------------------------------------------------------------
381 NS_IMETHODIMP nsBaseWidget::SetZIndex(PRInt32 aZIndex)
383 // Hold a ref to ourselves just in case, since we're going to remove
384 // from our parent.
385 nsCOMPtr<nsIWidget> kungFuDeathGrip(this);
387 mZIndex = aZIndex;
389 // reorder this child in its parent's list.
390 nsBaseWidget* parent = static_cast<nsBaseWidget*>(GetParent());
391 if (parent) {
392 parent->RemoveChild(this);
393 // Scope sib outside the for loop so we can check it afterward
394 nsIWidget* sib = parent->GetFirstChild();
395 for ( ; sib; sib = sib->GetNextSibling()) {
396 PRInt32 childZIndex;
397 if (NS_SUCCEEDED(sib->GetZIndex(&childZIndex))) {
398 if (aZIndex < childZIndex) {
399 // Insert ourselves before sib
400 nsIWidget* prev = sib->GetPrevSibling();
401 mNextSibling = sib;
402 mPrevSibling = prev;
403 sib->SetPrevSibling(this);
404 if (prev) {
405 prev->SetNextSibling(this);
406 } else {
407 NS_ASSERTION(sib == parent->mFirstChild, "Broken child list");
408 // We've taken ownership of sib, so it's safe to have parent let
409 // go of it
410 parent->mFirstChild = this;
412 PlaceBehind(eZPlacementBelow, sib, PR_FALSE);
413 break;
417 // were we added to the list?
418 if (!sib) {
419 parent->AddChild(this);
422 return NS_OK;
425 //-------------------------------------------------------------------------
427 // Gets widget's position within its parent's child list.
429 //-------------------------------------------------------------------------
430 NS_IMETHODIMP nsBaseWidget::GetZIndex(PRInt32* aZIndex)
432 *aZIndex = mZIndex;
433 return NS_OK;
436 //-------------------------------------------------------------------------
438 // Places widget behind the given widget (platforms must override)
440 //-------------------------------------------------------------------------
441 NS_IMETHODIMP nsBaseWidget::PlaceBehind(nsTopLevelWidgetZPlacement aPlacement,
442 nsIWidget *aWidget, PRBool aActivate)
444 return NS_OK;
447 //-------------------------------------------------------------------------
449 // Maximize, minimize or restore the window. The BaseWidget implementation
450 // merely stores the state.
452 //-------------------------------------------------------------------------
453 NS_IMETHODIMP nsBaseWidget::SetSizeMode(PRInt32 aMode) {
455 if (aMode == nsSizeMode_Normal || aMode == nsSizeMode_Minimized ||
456 aMode == nsSizeMode_Maximized) {
458 mSizeMode = (nsSizeMode) aMode;
459 return NS_OK;
461 return NS_ERROR_ILLEGAL_VALUE;
464 //-------------------------------------------------------------------------
466 // Get the size mode (minimized, maximized, that sort of thing...)
468 //-------------------------------------------------------------------------
469 NS_IMETHODIMP nsBaseWidget::GetSizeMode(PRInt32* aMode) {
471 *aMode = mSizeMode;
472 return NS_OK;
475 //-------------------------------------------------------------------------
477 // Get the foreground color
479 //-------------------------------------------------------------------------
480 nscolor nsBaseWidget::GetForegroundColor(void)
482 return mForeground;
486 //-------------------------------------------------------------------------
488 // Set the foreground color
490 //-------------------------------------------------------------------------
491 NS_METHOD nsBaseWidget::SetForegroundColor(const nscolor &aColor)
493 mForeground = aColor;
494 return NS_OK;
498 //-------------------------------------------------------------------------
500 // Get the background color
502 //-------------------------------------------------------------------------
503 nscolor nsBaseWidget::GetBackgroundColor(void)
505 return mBackground;
508 //-------------------------------------------------------------------------
510 // Set the background color
512 //-------------------------------------------------------------------------
513 NS_METHOD nsBaseWidget::SetBackgroundColor(const nscolor &aColor)
515 mBackground = aColor;
516 return NS_OK;
519 //-------------------------------------------------------------------------
521 // Get this component cursor
523 //-------------------------------------------------------------------------
524 nsCursor nsBaseWidget::GetCursor()
526 return mCursor;
529 NS_METHOD nsBaseWidget::SetCursor(nsCursor aCursor)
531 mCursor = aCursor;
532 return NS_OK;
535 NS_IMETHODIMP nsBaseWidget::SetCursor(imgIContainer* aCursor,
536 PRUint32 aHotspotX, PRUint32 aHotspotY)
538 return NS_ERROR_NOT_IMPLEMENTED;
541 //-------------------------------------------------------------------------
543 // Get the window type for this widget
545 //-------------------------------------------------------------------------
546 NS_IMETHODIMP nsBaseWidget::GetWindowType(nsWindowType& aWindowType)
548 aWindowType = mWindowType;
549 return NS_OK;
552 NS_IMETHODIMP nsBaseWidget::SetWindowType(nsWindowType aWindowType)
554 mWindowType = aWindowType;
555 return NS_OK;
558 //-------------------------------------------------------------------------
560 // Window transparency methods
562 //-------------------------------------------------------------------------
564 void nsBaseWidget::SetTransparencyMode(nsTransparencyMode aMode) {
567 nsTransparencyMode nsBaseWidget::GetTransparencyMode() {
568 return eTransparencyOpaque;
571 //-------------------------------------------------------------------------
573 // Hide window borders/decorations for this widget
575 //-------------------------------------------------------------------------
576 NS_IMETHODIMP nsBaseWidget::HideWindowChrome(PRBool aShouldHide)
578 return NS_ERROR_NOT_IMPLEMENTED;
581 //-------------------------------------------------------------------------
583 // Put the window into full-screen mode
585 //-------------------------------------------------------------------------
586 NS_IMETHODIMP nsBaseWidget::MakeFullScreen(PRBool aFullScreen)
588 HideWindowChrome(aFullScreen);
590 nsCOMPtr<nsIFullScreen> fullScreen = do_GetService("@mozilla.org/browser/fullscreen;1");
592 if (aFullScreen) {
593 if (!mOriginalBounds)
594 mOriginalBounds = new nsRect();
595 GetScreenBounds(*mOriginalBounds);
597 // Move to top-left corner of screen and size to the screen dimensions
598 nsCOMPtr<nsIScreenManager> screenManager;
599 screenManager = do_GetService("@mozilla.org/gfx/screenmanager;1");
600 NS_ASSERTION(screenManager, "Unable to grab screenManager.");
601 if (screenManager) {
602 nsCOMPtr<nsIScreen> screen;
603 screenManager->ScreenForRect(mOriginalBounds->x, mOriginalBounds->y,
604 mOriginalBounds->width, mOriginalBounds->height,
605 getter_AddRefs(screen));
606 if (screen) {
607 PRInt32 left, top, width, height;
608 if (NS_SUCCEEDED(screen->GetRect(&left, &top, &width, &height))) {
609 SetSizeMode(nsSizeMode_Normal);
610 Resize(left, top, width, height, PR_TRUE);
612 // Hide all of the OS chrome
613 if (fullScreen)
614 fullScreen->HideAllOSChrome();
619 } else if (mOriginalBounds) {
620 Resize(mOriginalBounds->x, mOriginalBounds->y, mOriginalBounds->width,
621 mOriginalBounds->height, PR_TRUE);
623 // Show all of the OS chrome
624 if (fullScreen)
625 fullScreen->ShowAllOSChrome();
628 return NS_OK;
631 //-------------------------------------------------------------------------
633 // Create a rendering context from this nsBaseWidget
635 //-------------------------------------------------------------------------
636 nsIRenderingContext* nsBaseWidget::GetRenderingContext()
638 nsresult rv;
639 nsCOMPtr<nsIRenderingContext> renderingCtx;
641 if (mOnDestroyCalled)
642 return nsnull;
644 rv = mContext->CreateRenderingContextInstance(*getter_AddRefs(renderingCtx));
645 if (NS_SUCCEEDED(rv)) {
646 gfxASurface* surface = GetThebesSurface();
647 NS_ENSURE_TRUE(surface, nsnull);
648 rv = renderingCtx->Init(mContext, surface);
649 if (NS_SUCCEEDED(rv)) {
650 nsIRenderingContext *ret = renderingCtx;
651 /* Increment object refcount that the |ret| object is still a valid one
652 * after we leave this function... */
653 NS_ADDREF(ret);
654 return ret;
656 else {
657 NS_WARNING("GetRenderingContext: nsIRenderingContext::Init() failed.");
660 else {
661 NS_WARNING("GetRenderingContext: Cannot create RenderingContext.");
664 return nsnull;
667 //-------------------------------------------------------------------------
669 // Return the toolkit this widget was created on
671 //-------------------------------------------------------------------------
672 nsIToolkit* nsBaseWidget::GetToolkit()
674 return mToolkit;
678 //-------------------------------------------------------------------------
680 // Return the used device context
682 //-------------------------------------------------------------------------
683 nsIDeviceContext* nsBaseWidget::GetDeviceContext()
685 return mContext;
688 //-------------------------------------------------------------------------
690 // Get the thebes surface
692 //-------------------------------------------------------------------------
693 gfxASurface *nsBaseWidget::GetThebesSurface()
695 // in theory we should get our parent's surface,
696 // clone it, and set a device offset before returning
697 return nsnull;
701 //-------------------------------------------------------------------------
703 // Destroy the window
705 //-------------------------------------------------------------------------
706 void nsBaseWidget::OnDestroy()
708 // release references to device context, toolkit, and app shell
709 NS_IF_RELEASE(mContext);
710 NS_IF_RELEASE(mToolkit);
713 NS_METHOD nsBaseWidget::SetWindowClass(const nsAString& xulWinType)
715 return NS_ERROR_NOT_IMPLEMENTED;
718 NS_METHOD nsBaseWidget::SetBorderStyle(nsBorderStyle aBorderStyle)
720 mBorderStyle = aBorderStyle;
721 return NS_OK;
726 * Sets the event listener for a widget
729 NS_METHOD nsBaseWidget::AddEventListener(nsIEventListener * aListener)
731 NS_PRECONDITION(mEventListener == nsnull, "Null event listener");
732 NS_IF_RELEASE(mEventListener);
733 NS_ADDREF(aListener);
734 mEventListener = aListener;
735 return NS_OK;
739 * If the implementation of nsWindow supports borders this method MUST be overridden
742 NS_METHOD nsBaseWidget::GetClientBounds(nsRect &aRect)
744 return GetBounds(aRect);
748 * If the implementation of nsWindow supports borders this method MUST be overridden
751 NS_METHOD nsBaseWidget::GetBounds(nsRect &aRect)
753 aRect = mBounds;
754 return NS_OK;
758 * If the implementation of nsWindow uses a local coordinate system within the window,
759 * this method must be overridden
762 NS_METHOD nsBaseWidget::GetScreenBounds(nsRect &aRect)
764 return GetBounds(aRect);
771 NS_METHOD nsBaseWidget::SetBounds(const nsRect &aRect)
773 mBounds = aRect;
775 return NS_OK;
781 * Calculates the border width and height
784 NS_METHOD nsBaseWidget::GetBorderSize(PRInt32 &aWidth, PRInt32 &aHeight)
786 nsRect rectWin;
787 nsRect rect;
788 GetBounds(rectWin);
789 GetClientBounds(rect);
791 aWidth = (rectWin.width - rect.width) / 2;
792 aHeight = (rectWin.height - rect.height) / 2;
794 return NS_OK;
797 NS_IMETHODIMP nsBaseWidget::ScrollWidgets(PRInt32 aDx, PRInt32 aDy)
799 return NS_ERROR_FAILURE;
802 NS_IMETHODIMP nsBaseWidget::ScrollRect(nsRect &aRect, PRInt32 aDx, PRInt32 aDy)
804 return NS_ERROR_FAILURE;
807 NS_METHOD nsBaseWidget::EnableDragDrop(PRBool aEnable)
809 return NS_OK;
812 NS_METHOD nsBaseWidget::SetModal(PRBool aModal)
814 return NS_ERROR_FAILURE;
817 // generic xp assumption is that events should be processed
818 NS_METHOD nsBaseWidget::ModalEventFilter(PRBool aRealEvent, void *aEvent,
819 PRBool *aForWindow)
821 *aForWindow = PR_TRUE;
822 return NS_OK;
825 NS_IMETHODIMP
826 nsBaseWidget::GetAttention(PRInt32 aCycleCount) {
827 return NS_OK;
830 NS_IMETHODIMP
831 nsBaseWidget::GetLastInputEventTime(PRUint32& aTime) {
832 return NS_ERROR_NOT_IMPLEMENTED;
835 NS_IMETHODIMP
836 nsBaseWidget::SetIcon(const nsAString&)
838 return NS_OK;
841 NS_IMETHODIMP
842 nsBaseWidget::BeginSecureKeyboardInput()
844 #ifdef DEBUG
845 NS_ASSERTION(!debug_InSecureKeyboardInputMode, "Attempting to nest call to BeginSecureKeyboardInput!");
846 debug_InSecureKeyboardInputMode = PR_TRUE;
847 #endif
848 return NS_OK;
851 NS_IMETHODIMP
852 nsBaseWidget::EndSecureKeyboardInput()
854 #ifdef DEBUG
855 NS_ASSERTION(debug_InSecureKeyboardInputMode, "Calling EndSecureKeyboardInput when it hasn't been enabled!");
856 debug_InSecureKeyboardInputMode = PR_FALSE;
857 #endif
858 return NS_OK;
861 NS_IMETHODIMP
862 nsBaseWidget::SetWindowTitlebarColor(nscolor aColor, PRBool aActive)
864 return NS_ERROR_NOT_IMPLEMENTED;
867 PRBool
868 nsBaseWidget::ShowsResizeIndicator(nsIntRect* aResizerRect)
870 return PR_FALSE;
875 * Modifies aFile to point at an icon file with the given name and suffix. The
876 * suffix may correspond to a file extension with leading '.' if appropriate.
877 * Returns true if the icon file exists and can be read.
879 static PRBool
880 ResolveIconNameHelper(nsILocalFile *aFile,
881 const nsAString &aIconName,
882 const nsAString &aIconSuffix)
884 aFile->Append(NS_LITERAL_STRING("icons"));
885 aFile->Append(NS_LITERAL_STRING("default"));
886 aFile->Append(aIconName + aIconSuffix);
888 PRBool readable;
889 return NS_SUCCEEDED(aFile->IsReadable(&readable)) && readable;
893 * Resolve the given icon name into a local file object. This method is
894 * intended to be called by subclasses of nsBaseWidget. aIconSuffix is a
895 * platform specific icon file suffix (e.g., ".ico" under Win32).
897 * If no file is found matching the given parameters, then null is returned.
899 void
900 nsBaseWidget::ResolveIconName(const nsAString &aIconName,
901 const nsAString &aIconSuffix,
902 nsILocalFile **aResult)
904 *aResult = nsnull;
906 nsCOMPtr<nsIProperties> dirSvc = do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID);
907 if (!dirSvc)
908 return;
910 // first check auxilary chrome directories
912 nsCOMPtr<nsISimpleEnumerator> dirs;
913 dirSvc->Get(NS_APP_CHROME_DIR_LIST, NS_GET_IID(nsISimpleEnumerator),
914 getter_AddRefs(dirs));
915 if (dirs) {
916 PRBool hasMore;
917 while (NS_SUCCEEDED(dirs->HasMoreElements(&hasMore)) && hasMore) {
918 nsCOMPtr<nsISupports> element;
919 dirs->GetNext(getter_AddRefs(element));
920 if (!element)
921 continue;
922 nsCOMPtr<nsILocalFile> file = do_QueryInterface(element);
923 if (!file)
924 continue;
925 if (ResolveIconNameHelper(file, aIconName, aIconSuffix)) {
926 NS_ADDREF(*aResult = file);
927 return;
932 // then check the main app chrome directory
934 nsCOMPtr<nsILocalFile> file;
935 dirSvc->Get(NS_APP_CHROME_DIR, NS_GET_IID(nsILocalFile),
936 getter_AddRefs(file));
937 if (file && ResolveIconNameHelper(file, aIconName, aIconSuffix))
938 NS_ADDREF(*aResult = file);
941 NS_IMETHODIMP
942 nsBaseWidget::BeginResizeDrag(nsGUIEvent* aEvent, PRInt32 aHorizontal, PRInt32 aVertical)
944 return NS_ERROR_NOT_IMPLEMENTED;
947 #ifdef DEBUG
948 //////////////////////////////////////////////////////////////
950 // Convert a GUI event message code to a string.
951 // Makes it a lot easier to debug events.
953 // See gtk/nsWidget.cpp and windows/nsWindow.cpp
954 // for a DebugPrintEvent() function that uses
955 // this.
957 //////////////////////////////////////////////////////////////
958 /* static */ nsAutoString
959 nsBaseWidget::debug_GuiEventToString(nsGUIEvent * aGuiEvent)
961 NS_ASSERTION(nsnull != aGuiEvent,"cmon, null gui event.");
963 nsAutoString eventName(NS_LITERAL_STRING("UNKNOWN"));
965 #define _ASSIGN_eventName(_value,_name)\
966 case _value: eventName.AssignWithConversion(_name) ; break
968 switch(aGuiEvent->message)
970 _ASSIGN_eventName(NS_BLUR_CONTENT,"NS_BLUR_CONTENT");
971 _ASSIGN_eventName(NS_CONTROL_CHANGE,"NS_CONTROL_CHANGE");
972 _ASSIGN_eventName(NS_CREATE,"NS_CREATE");
973 _ASSIGN_eventName(NS_DESTROY,"NS_DESTROY");
974 _ASSIGN_eventName(NS_DRAGDROP_GESTURE,"NS_DND_GESTURE");
975 _ASSIGN_eventName(NS_DRAGDROP_DROP,"NS_DND_DROP");
976 _ASSIGN_eventName(NS_DRAGDROP_ENTER,"NS_DND_ENTER");
977 _ASSIGN_eventName(NS_DRAGDROP_EXIT,"NS_DND_EXIT");
978 _ASSIGN_eventName(NS_DRAGDROP_OVER,"NS_DND_OVER");
979 _ASSIGN_eventName(NS_FOCUS_CONTENT,"NS_FOCUS_CONTENT");
980 _ASSIGN_eventName(NS_FORM_SELECTED,"NS_FORM_SELECTED");
981 _ASSIGN_eventName(NS_FORM_CHANGE,"NS_FORM_CHANGE");
982 _ASSIGN_eventName(NS_FORM_INPUT,"NS_FORM_INPUT");
983 _ASSIGN_eventName(NS_FORM_RESET,"NS_FORM_RESET");
984 _ASSIGN_eventName(NS_FORM_SUBMIT,"NS_FORM_SUBMIT");
985 _ASSIGN_eventName(NS_GOTFOCUS,"NS_GOTFOCUS");
986 _ASSIGN_eventName(NS_IMAGE_ABORT,"NS_IMAGE_ABORT");
987 _ASSIGN_eventName(NS_LOAD_ERROR,"NS_LOAD_ERROR");
988 _ASSIGN_eventName(NS_KEY_DOWN,"NS_KEY_DOWN");
989 _ASSIGN_eventName(NS_KEY_PRESS,"NS_KEY_PRESS");
990 _ASSIGN_eventName(NS_KEY_UP,"NS_KEY_UP");
991 _ASSIGN_eventName(NS_LOSTFOCUS,"NS_LOSTFOCUS");
992 _ASSIGN_eventName(NS_MENU_SELECTED,"NS_MENU_SELECTED");
993 _ASSIGN_eventName(NS_MOUSE_ENTER,"NS_MOUSE_ENTER");
994 _ASSIGN_eventName(NS_MOUSE_EXIT,"NS_MOUSE_EXIT");
995 _ASSIGN_eventName(NS_MOUSE_BUTTON_DOWN,"NS_MOUSE_BUTTON_DOWN");
996 _ASSIGN_eventName(NS_MOUSE_BUTTON_UP,"NS_MOUSE_BUTTON_UP");
997 _ASSIGN_eventName(NS_MOUSE_CLICK,"NS_MOUSE_CLICK");
998 _ASSIGN_eventName(NS_MOUSE_DOUBLECLICK,"NS_MOUSE_DBLCLICK");
999 _ASSIGN_eventName(NS_MOUSE_MOVE,"NS_MOUSE_MOVE");
1000 _ASSIGN_eventName(NS_MOVE,"NS_MOVE");
1001 _ASSIGN_eventName(NS_LOAD,"NS_LOAD");
1002 _ASSIGN_eventName(NS_PAGE_UNLOAD,"NS_PAGE_UNLOAD");
1003 _ASSIGN_eventName(NS_PAINT,"NS_PAINT");
1004 _ASSIGN_eventName(NS_XUL_BROADCAST, "NS_XUL_BROADCAST");
1005 _ASSIGN_eventName(NS_XUL_COMMAND_UPDATE, "NS_XUL_COMMAND_UPDATE");
1006 _ASSIGN_eventName(NS_SCROLLBAR_LINE_NEXT,"NS_SB_LINE_NEXT");
1007 _ASSIGN_eventName(NS_SCROLLBAR_LINE_PREV,"NS_SB_LINE_PREV");
1008 _ASSIGN_eventName(NS_SCROLLBAR_PAGE_NEXT,"NS_SB_PAGE_NEXT");
1009 _ASSIGN_eventName(NS_SCROLLBAR_PAGE_PREV,"NS_SB_PAGE_PREV");
1010 _ASSIGN_eventName(NS_SCROLLBAR_POS,"NS_SB_POS");
1011 _ASSIGN_eventName(NS_SIZE,"NS_SIZE");
1013 #undef _ASSIGN_eventName
1015 default:
1017 char buf[32];
1019 sprintf(buf,"UNKNOWN: %d",aGuiEvent->message);
1021 eventName.AssignWithConversion(buf);
1023 break;
1026 return nsAutoString(eventName);
1028 //////////////////////////////////////////////////////////////
1030 // Code to deal with paint and event debug prefs.
1032 //////////////////////////////////////////////////////////////
1033 struct PrefPair
1035 const char * name;
1036 PRBool value;
1039 static PrefPair debug_PrefValues[] =
1041 { "nglayout.debug.crossing_event_dumping", PR_FALSE },
1042 { "nglayout.debug.event_dumping", PR_FALSE },
1043 { "nglayout.debug.invalidate_dumping", PR_FALSE },
1044 { "nglayout.debug.motion_event_dumping", PR_FALSE },
1045 { "nglayout.debug.paint_dumping", PR_FALSE },
1046 { "nglayout.debug.paint_flashing", PR_FALSE }
1049 static PRUint32 debug_NumPrefValues =
1050 (sizeof(debug_PrefValues) / sizeof(debug_PrefValues[0]));
1053 //////////////////////////////////////////////////////////////
1054 static PRBool debug_GetBoolPref(nsIPrefBranch * aPrefs,const char * aPrefName)
1056 NS_ASSERTION(nsnull != aPrefName,"cmon, pref name is null.");
1057 NS_ASSERTION(nsnull != aPrefs,"cmon, prefs are null.");
1059 PRBool value = PR_FALSE;
1061 if (aPrefs)
1063 aPrefs->GetBoolPref(aPrefName,&value);
1066 return value;
1068 //////////////////////////////////////////////////////////////
1069 PRBool
1070 nsBaseWidget::debug_GetCachedBoolPref(const char * aPrefName)
1072 NS_ASSERTION(nsnull != aPrefName,"cmon, pref name is null.");
1074 for (PRUint32 i = 0; i < debug_NumPrefValues; i++)
1076 if (strcmp(debug_PrefValues[i].name, aPrefName) == 0)
1078 return debug_PrefValues[i].value;
1082 return PR_FALSE;
1084 //////////////////////////////////////////////////////////////
1085 static void debug_SetCachedBoolPref(const char * aPrefName,PRBool aValue)
1087 NS_ASSERTION(nsnull != aPrefName,"cmon, pref name is null.");
1089 for (PRUint32 i = 0; i < debug_NumPrefValues; i++)
1091 if (strcmp(debug_PrefValues[i].name, aPrefName) == 0)
1093 debug_PrefValues[i].value = aValue;
1095 return;
1099 NS_ASSERTION(PR_FALSE, "cmon, this code is not reached dude.");
1102 //////////////////////////////////////////////////////////////
1103 class Debug_PrefObserver : public nsIObserver {
1104 public:
1105 NS_DECL_ISUPPORTS
1106 NS_DECL_NSIOBSERVER
1109 NS_IMPL_ISUPPORTS1(Debug_PrefObserver, nsIObserver)
1111 NS_IMETHODIMP
1112 Debug_PrefObserver::Observe(nsISupports* subject, const char* topic,
1113 const PRUnichar* data)
1115 nsCOMPtr<nsIPrefBranch> branch(do_QueryInterface(subject));
1116 NS_ASSERTION(branch, "must implement nsIPrefBranch");
1118 NS_ConvertUTF16toUTF8 prefName(data);
1120 PRBool value = PR_FALSE;
1121 branch->GetBoolPref(prefName.get(), &value);
1122 debug_SetCachedBoolPref(prefName.get(), value);
1123 return NS_OK;
1126 //////////////////////////////////////////////////////////////
1127 /* static */ void
1128 debug_RegisterPrefCallbacks()
1130 static PRBool once = PR_TRUE;
1132 if (once)
1134 once = PR_FALSE;
1136 nsCOMPtr<nsIPrefBranch2> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
1138 NS_ASSERTION(prefs, "Prefs services is null.");
1140 if (prefs)
1142 nsCOMPtr<nsIObserver> obs(new Debug_PrefObserver());
1143 for (PRUint32 i = 0; i < debug_NumPrefValues; i++)
1145 // Initialize the pref values
1146 debug_PrefValues[i].value =
1147 debug_GetBoolPref(prefs,debug_PrefValues[i].name);
1149 if (obs) {
1150 // Register callbacks for when these change
1151 prefs->AddObserver(debug_PrefValues[i].name, obs, PR_FALSE);
1157 //////////////////////////////////////////////////////////////
1158 static PRInt32
1159 _GetPrintCount()
1161 static PRInt32 sCount = 0;
1163 return ++sCount;
1165 //////////////////////////////////////////////////////////////
1166 /* static */ PRBool
1167 nsBaseWidget::debug_WantPaintFlashing()
1169 return debug_GetCachedBoolPref("nglayout.debug.paint_flashing");
1171 //////////////////////////////////////////////////////////////
1172 /* static */ void
1173 nsBaseWidget::debug_DumpEvent(FILE * aFileOut,
1174 nsIWidget * aWidget,
1175 nsGUIEvent * aGuiEvent,
1176 const nsCAutoString & aWidgetName,
1177 PRInt32 aWindowID)
1179 // NS_PAINT is handled by debug_DumpPaintEvent()
1180 if (aGuiEvent->message == NS_PAINT)
1181 return;
1183 if (aGuiEvent->message == NS_MOUSE_MOVE)
1185 if (!debug_GetCachedBoolPref("nglayout.debug.motion_event_dumping"))
1186 return;
1189 if (aGuiEvent->message == NS_MOUSE_ENTER ||
1190 aGuiEvent->message == NS_MOUSE_EXIT)
1192 if (!debug_GetCachedBoolPref("nglayout.debug.crossing_event_dumping"))
1193 return;
1196 if (!debug_GetCachedBoolPref("nglayout.debug.event_dumping"))
1197 return;
1199 nsCAutoString tempString; tempString.AssignWithConversion(debug_GuiEventToString(aGuiEvent).get());
1201 fprintf(aFileOut,
1202 "%4d %-26s widget=%-8p name=%-12s id=%-8p refpt=%d,%d\n",
1203 _GetPrintCount(),
1204 tempString.get(),
1205 (void *) aWidget,
1206 aWidgetName.get(),
1207 (void *) (aWindowID ? aWindowID : 0x0),
1208 aGuiEvent->refPoint.x,
1209 aGuiEvent->refPoint.y);
1211 //////////////////////////////////////////////////////////////
1212 /* static */ void
1213 nsBaseWidget::debug_DumpPaintEvent(FILE * aFileOut,
1214 nsIWidget * aWidget,
1215 nsPaintEvent * aPaintEvent,
1216 const nsCAutoString & aWidgetName,
1217 PRInt32 aWindowID)
1219 NS_ASSERTION(nsnull != aFileOut,"cmon, null output FILE");
1220 NS_ASSERTION(nsnull != aWidget,"cmon, the widget is null");
1221 NS_ASSERTION(nsnull != aPaintEvent,"cmon, the paint event is null");
1223 if (!debug_GetCachedBoolPref("nglayout.debug.paint_dumping"))
1224 return;
1226 fprintf(aFileOut,
1227 "%4d PAINT widget=%p name=%-12s id=%-8p rect=",
1228 _GetPrintCount(),
1229 (void *) aWidget,
1230 aWidgetName.get(),
1231 (void *) aWindowID);
1233 if (aPaintEvent->rect)
1235 fprintf(aFileOut,
1236 "%3d,%-3d %3d,%-3d",
1237 aPaintEvent->rect->x,
1238 aPaintEvent->rect->y,
1239 aPaintEvent->rect->width,
1240 aPaintEvent->rect->height);
1242 else
1244 fprintf(aFileOut,"none");
1247 fprintf(aFileOut,"\n");
1249 //////////////////////////////////////////////////////////////
1250 /* static */ void
1251 nsBaseWidget::debug_DumpInvalidate(FILE * aFileOut,
1252 nsIWidget * aWidget,
1253 const nsRect * aRect,
1254 PRBool aIsSynchronous,
1255 const nsCAutoString & aWidgetName,
1256 PRInt32 aWindowID)
1258 if (!debug_GetCachedBoolPref("nglayout.debug.invalidate_dumping"))
1259 return;
1261 NS_ASSERTION(nsnull != aFileOut,"cmon, null output FILE");
1262 NS_ASSERTION(nsnull != aWidget,"cmon, the widget is null");
1264 fprintf(aFileOut,
1265 "%4d Invalidate widget=%p name=%-12s id=%-8p",
1266 _GetPrintCount(),
1267 (void *) aWidget,
1268 aWidgetName.get(),
1269 (void *) aWindowID);
1271 if (aRect)
1273 fprintf(aFileOut,
1274 " rect=%3d,%-3d %3d,%-3d",
1275 aRect->x,
1276 aRect->y,
1277 aRect->width,
1278 aRect->height);
1280 else
1282 fprintf(aFileOut,
1283 " rect=%-15s",
1284 "none");
1287 fprintf(aFileOut,
1288 " sync=%s",
1289 (const char *) (aIsSynchronous ? "yes" : "no "));
1291 fprintf(aFileOut,"\n");
1293 //////////////////////////////////////////////////////////////
1295 #endif // DEBUG