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 "nsITimer.h"
12 #include "nsCOMPtr.h"
13 #include "nsString.h"
14 #ifdef MOZ_XUL
15 #  include "XULTreeElement.h"
16 #endif
17 #include "nsIWeakReferenceUtils.h"
18 #include "mozilla/Attributes.h"
19 
20 class nsIContent;
21 class nsTreeColumn;
22 
23 namespace mozilla {
24 namespace dom {
25 class Event;
26 class MouseEvent;
27 }  // namespace dom
28 }  // namespace mozilla
29 
30 class nsXULTooltipListener final : public nsIDOMEventListener {
31  public:
32   NS_DECL_ISUPPORTS
33   NS_DECL_NSIDOMEVENTLISTENER
34 
35   void MouseOut(mozilla::dom::Event* aEvent);
36   void MouseMove(mozilla::dom::Event* aEvent);
37 
38   void AddTooltipSupport(nsIContent* aNode);
39   void RemoveTooltipSupport(nsIContent* aNode);
GetInstance()40   static nsXULTooltipListener* GetInstance() {
41     if (!sInstance) sInstance = new nsXULTooltipListener();
42     return sInstance;
43   }
44 
45  protected:
46   nsXULTooltipListener();
47   ~nsXULTooltipListener();
48 
49   // pref callback for when the "show tooltips" pref changes
50   static bool sShowTooltips;
51 
52   void KillTooltipTimer();
53 
54 #ifdef MOZ_XUL
55   void CheckTreeBodyMove(mozilla::dom::MouseEvent* aMouseEvent);
56   mozilla::dom::XULTreeElement* GetSourceTree();
57 #endif
58 
59   nsresult ShowTooltip();
60   void LaunchTooltip();
61   nsresult HideTooltip();
62   nsresult DestroyTooltip();
63   // This method tries to find a tooltip for aTarget.
64   nsresult FindTooltip(nsIContent* aTarget, nsIContent** aTooltip);
65   // This method calls FindTooltip and checks that the tooltip
66   // can be really used (i.e. tooltip is not a menu).
67   nsresult GetTooltipFor(nsIContent* aTarget, nsIContent** aTooltip);
68 
69   static nsXULTooltipListener* sInstance;
70   static void ToolbarTipsPrefChanged(const char* aPref, void* aClosure);
71 
72   nsWeakPtr mSourceNode;
73   nsWeakPtr mTargetNode;
74   nsWeakPtr mCurrentTooltip;
75 
76   // a timer for showing the tooltip
77   nsCOMPtr<nsITimer> mTooltipTimer;
78   static void sTooltipCallback(nsITimer* aTimer, void* aListener);
79 
80   // screen coordinates of the last mousemove event, stored so that the
81   // tooltip can be opened at this location.
82   int32_t mMouseScreenX, mMouseScreenY;
83 
84   // various constants for tooltips
85   enum {
86     kTooltipMouseMoveTolerance = 7  // 7 pixel tolerance for mousemove event
87   };
88 
89   // flag specifying if the tooltip has already been displayed by a MouseMove
90   // event. The flag is reset on MouseOut so that the tooltip will display
91   // the next time the mouse enters the node (bug #395668).
92   bool mTooltipShownOnce;
93 
94 #ifdef MOZ_XUL
95   // special members for handling trees
96   bool mIsSourceTree;
97   bool mNeedTitletip;
98   int32_t mLastTreeRow;
99   RefPtr<nsTreeColumn> mLastTreeCol;
100 #endif
101 };
102 
103 #endif  // nsXULTooltipListener
104