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
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : * Author: Eric D Vaughan <evaughan@netscape.com>
24 : * Pierre Phaneuf <pp@ludusdesign.com>
25 : * Dan Rosen <dr@netscape.com>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either of the GNU General Public License Version 2 or later (the "GPL"),
29 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #include "nsCOMPtr.h"
42 : #include "nsPresContext.h"
43 : #include "nsGkAtoms.h"
44 : #include "nsGUIEvent.h"
45 : #include "nsButtonBoxFrame.h"
46 : #include "nsITimer.h"
47 : #include "nsRepeatService.h"
48 :
49 : class nsAutoRepeatBoxFrame : public nsButtonBoxFrame
50 0 : {
51 : public:
52 : NS_DECL_FRAMEARENA_HELPERS
53 :
54 : friend nsIFrame* NS_NewAutoRepeatBoxFrame(nsIPresShell* aPresShell,
55 : nsStyleContext* aContext);
56 :
57 : virtual void DestroyFrom(nsIFrame* aDestructRoot);
58 :
59 : NS_IMETHOD AttributeChanged(PRInt32 aNameSpaceID,
60 : nsIAtom* aAttribute,
61 : PRInt32 aModType);
62 :
63 : NS_IMETHOD HandleEvent(nsPresContext* aPresContext,
64 : nsGUIEvent* aEvent,
65 : nsEventStatus* aEventStatus);
66 :
67 : NS_IMETHOD HandlePress(nsPresContext* aPresContext,
68 : nsGUIEvent* aEvent,
69 : nsEventStatus* aEventStatus);
70 :
71 : NS_IMETHOD HandleRelease(nsPresContext* aPresContext,
72 : nsGUIEvent* aEvent,
73 : nsEventStatus* aEventStatus);
74 :
75 : protected:
76 0 : nsAutoRepeatBoxFrame(nsIPresShell* aPresShell, nsStyleContext* aContext):
77 0 : nsButtonBoxFrame(aPresShell, aContext) {}
78 :
79 0 : void StartRepeat() {
80 0 : if (IsActivatedOnHover()) {
81 : // No initial delay on hover.
82 0 : nsRepeatService::GetInstance()->Start(Notify, this, 0);
83 : } else {
84 0 : nsRepeatService::GetInstance()->Start(Notify, this);
85 : }
86 0 : }
87 0 : void StopRepeat() {
88 0 : nsRepeatService::GetInstance()->Stop(Notify, this);
89 0 : }
90 : void Notify();
91 0 : static void Notify(void* aData) {
92 0 : static_cast<nsAutoRepeatBoxFrame*>(aData)->Notify();
93 0 : }
94 :
95 : bool mTrustedEvent;
96 :
97 : bool IsActivatedOnHover();
98 : };
99 :
100 : nsIFrame*
101 0 : NS_NewAutoRepeatBoxFrame (nsIPresShell* aPresShell, nsStyleContext* aContext)
102 : {
103 0 : return new (aPresShell) nsAutoRepeatBoxFrame (aPresShell, aContext);
104 : }
105 :
106 0 : NS_IMPL_FRAMEARENA_HELPERS(nsAutoRepeatBoxFrame)
107 :
108 : NS_IMETHODIMP
109 0 : nsAutoRepeatBoxFrame::HandleEvent(nsPresContext* aPresContext,
110 : nsGUIEvent* aEvent,
111 : nsEventStatus* aEventStatus)
112 : {
113 0 : NS_ENSURE_ARG_POINTER(aEventStatus);
114 0 : if (nsEventStatus_eConsumeNoDefault == *aEventStatus) {
115 0 : return NS_OK;
116 : }
117 :
118 0 : switch(aEvent->message)
119 : {
120 : // repeat mode may be "hover" for repeating while the mouse is hovering
121 : // over the element, otherwise repetition is done while the element is
122 : // active (pressed).
123 : case NS_MOUSE_ENTER:
124 : case NS_MOUSE_ENTER_SYNTH:
125 0 : if (IsActivatedOnHover()) {
126 0 : StartRepeat();
127 0 : mTrustedEvent = NS_IS_TRUSTED_EVENT(aEvent);
128 : }
129 0 : break;
130 :
131 : case NS_MOUSE_EXIT:
132 : case NS_MOUSE_EXIT_SYNTH:
133 : // always stop on mouse exit
134 0 : StopRepeat();
135 : // Not really necessary but do this to be safe
136 0 : mTrustedEvent = false;
137 0 : break;
138 :
139 : case NS_MOUSE_CLICK:
140 0 : if (NS_IS_MOUSE_LEFT_CLICK(aEvent)) {
141 : // skip button frame handling to prevent click handling
142 0 : return nsBoxFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
143 : }
144 0 : break;
145 : }
146 :
147 0 : return nsButtonBoxFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
148 : }
149 :
150 : NS_IMETHODIMP
151 0 : nsAutoRepeatBoxFrame::HandlePress(nsPresContext* aPresContext,
152 : nsGUIEvent* aEvent,
153 : nsEventStatus* aEventStatus)
154 : {
155 0 : if (!IsActivatedOnHover()) {
156 0 : StartRepeat();
157 0 : mTrustedEvent = NS_IS_TRUSTED_EVENT(aEvent);
158 0 : DoMouseClick(aEvent, mTrustedEvent);
159 : }
160 :
161 0 : return NS_OK;
162 : }
163 :
164 : NS_IMETHODIMP
165 0 : nsAutoRepeatBoxFrame::HandleRelease(nsPresContext* aPresContext,
166 : nsGUIEvent* aEvent,
167 : nsEventStatus* aEventStatus)
168 : {
169 0 : if (!IsActivatedOnHover()) {
170 0 : StopRepeat();
171 : }
172 0 : return NS_OK;
173 : }
174 :
175 : NS_IMETHODIMP
176 0 : nsAutoRepeatBoxFrame::AttributeChanged(PRInt32 aNameSpaceID,
177 : nsIAtom* aAttribute,
178 : PRInt32 aModType)
179 : {
180 0 : if (aAttribute == nsGkAtoms::type) {
181 0 : StopRepeat();
182 : }
183 0 : return NS_OK;
184 : }
185 :
186 : void
187 0 : nsAutoRepeatBoxFrame::Notify()
188 : {
189 0 : DoMouseClick(nsnull, mTrustedEvent);
190 0 : }
191 :
192 : void
193 0 : nsAutoRepeatBoxFrame::DestroyFrom(nsIFrame* aDestructRoot)
194 : {
195 : // Ensure our repeat service isn't going... it's possible that a scrollbar can disappear out
196 : // from under you while you're in the process of scrolling.
197 0 : StopRepeat();
198 0 : nsButtonBoxFrame::DestroyFrom(aDestructRoot);
199 0 : }
200 :
201 : bool
202 0 : nsAutoRepeatBoxFrame::IsActivatedOnHover()
203 : {
204 : return mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::repeat,
205 0 : nsGkAtoms::hover, eCaseMatters);
206 : }
|