1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef nsXULTooltipListener_h__
8 #define nsXULTooltipListener_h__
9 
10 #include "nsIDOMEventListener.h"
11 #include "nsIDOMMouseEvent.h"
12 #include "nsIDOMElement.h"
13 #include "nsITimer.h"
14 #include "nsCOMPtr.h"
15 #include "nsString.h"
16 #ifdef MOZ_XUL
17 #include "nsITreeBoxObject.h"
18 #include "nsITreeColumns.h"
19 #endif
20 #include "nsWeakPtr.h"
21 #include "mozilla/Attributes.h"
22 
23 class nsIContent;
24 
25 class nsXULTooltipListener final : public nsIDOMEventListener {
26  public:
27   NS_DECL_ISUPPORTS
28   NS_DECL_NSIDOMEVENTLISTENER
29 
30   void MouseOut(nsIDOMEvent* aEvent);
31   void MouseMove(nsIDOMEvent* aEvent);
32 
33   void AddTooltipSupport(nsIContent* aNode);
34   void RemoveTooltipSupport(nsIContent* aNode);
GetInstance()35   static nsXULTooltipListener* GetInstance() {
36     if (!sInstance) sInstance = new nsXULTooltipListener();
37     return sInstance;
38   }
39 
40  protected:
41   nsXULTooltipListener();
42   ~nsXULTooltipListener();
43 
44   // pref callback for when the "show tooltips" pref changes
45   static bool sShowTooltips;
46 
47   void KillTooltipTimer();
48 
49 #ifdef MOZ_XUL
50   void CheckTreeBodyMove(nsIDOMMouseEvent* aMouseEvent);
51   nsresult GetSourceTreeBoxObject(nsITreeBoxObject** aBoxObject);
52 #endif
53 
54   nsresult ShowTooltip();
55   void LaunchTooltip();
56   nsresult HideTooltip();
57   nsresult DestroyTooltip();
58   // This method tries to find a tooltip for aTarget.
59   nsresult FindTooltip(nsIContent* aTarget, nsIContent** aTooltip);
60   // This method calls FindTooltip and checks that the tooltip
61   // can be really used (i.e. tooltip is not a menu).
62   nsresult GetTooltipFor(nsIContent* aTarget, nsIContent** aTooltip);
63 
64   static nsXULTooltipListener* sInstance;
65   static void ToolbarTipsPrefChanged(const char* aPref, void* aClosure);
66 
67   nsWeakPtr mSourceNode;
68   nsWeakPtr mTargetNode;
69   nsWeakPtr mCurrentTooltip;
70 
71   // a timer for showing the tooltip
72   nsCOMPtr<nsITimer> mTooltipTimer;
73   static void sTooltipCallback(nsITimer* aTimer, void* aListener);
74 
75   // screen coordinates of the last mousemove event, stored so that the
76   // tooltip can be opened at this location.
77   int32_t mMouseScreenX, mMouseScreenY;
78 
79   // various constants for tooltips
80   enum {
81     kTooltipMouseMoveTolerance = 7  // 7 pixel tolerance for mousemove event
82   };
83 
84   // flag specifying if the tooltip has already been displayed by a MouseMove
85   // event. The flag is reset on MouseOut so that the tooltip will display
86   // the next time the mouse enters the node (bug #395668).
87   bool mTooltipShownOnce;
88 
89 #ifdef MOZ_XUL
90   // special members for handling trees
91   bool mIsSourceTree;
92   bool mNeedTitletip;
93   int32_t mLastTreeRow;
94   nsCOMPtr<nsITreeColumn> mLastTreeCol;
95 #endif
96 };
97 
98 #endif  // nsXULTooltipListener
99