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 Communicator client 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 : * Original Author: David W. Hyatt (hyatt@netscape.com)
24 : * Blake Ross (blakeross@telocity.com)
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 : #include "nsCOMPtr.h"
40 : #include "nsButtonBoxFrame.h"
41 : #include "nsIContent.h"
42 : #include "nsIDocument.h"
43 : #include "nsIDOMDocument.h"
44 : #include "nsIDOMNodeList.h"
45 : #include "nsIDOMXULButtonElement.h"
46 : #include "nsGkAtoms.h"
47 : #include "nsINameSpaceManager.h"
48 : #include "nsPresContext.h"
49 : #include "nsIPresShell.h"
50 : #include "nsGUIEvent.h"
51 : #include "nsEventStateManager.h"
52 : #include "nsIDOMElement.h"
53 : #include "nsDisplayList.h"
54 : #include "nsContentUtils.h"
55 : #include "mozilla/dom/Element.h"
56 :
57 :
58 : //
59 : // NS_NewXULButtonFrame
60 : //
61 : // Creates a new Button frame and returns it
62 : //
63 : nsIFrame*
64 0 : NS_NewButtonBoxFrame (nsIPresShell* aPresShell, nsStyleContext* aContext)
65 : {
66 0 : return new (aPresShell) nsButtonBoxFrame(aPresShell, aContext);
67 : }
68 :
69 0 : NS_IMPL_FRAMEARENA_HELPERS(nsButtonBoxFrame)
70 :
71 : NS_IMETHODIMP
72 0 : nsButtonBoxFrame::BuildDisplayListForChildren(nsDisplayListBuilder* aBuilder,
73 : const nsRect& aDirtyRect,
74 : const nsDisplayListSet& aLists)
75 : {
76 : // override, since we don't want children to get events
77 0 : if (aBuilder->IsForEventDelivery())
78 0 : return NS_OK;
79 0 : return nsBoxFrame::BuildDisplayListForChildren(aBuilder, aDirtyRect, aLists);
80 : }
81 :
82 : NS_IMETHODIMP
83 0 : nsButtonBoxFrame::HandleEvent(nsPresContext* aPresContext,
84 : nsGUIEvent* aEvent,
85 : nsEventStatus* aEventStatus)
86 : {
87 0 : NS_ENSURE_ARG_POINTER(aEventStatus);
88 0 : if (nsEventStatus_eConsumeNoDefault == *aEventStatus) {
89 0 : return NS_OK;
90 : }
91 :
92 0 : switch (aEvent->message) {
93 : case NS_KEY_DOWN:
94 0 : if (NS_KEY_EVENT == aEvent->eventStructType) {
95 0 : nsKeyEvent* keyEvent = (nsKeyEvent*)aEvent;
96 0 : if (NS_VK_SPACE == keyEvent->keyCode) {
97 0 : nsEventStateManager *esm = aPresContext->EventStateManager();
98 : // :hover:active state
99 0 : esm->SetContentState(mContent, NS_EVENT_STATE_HOVER);
100 0 : esm->SetContentState(mContent, NS_EVENT_STATE_ACTIVE);
101 : }
102 : }
103 0 : break;
104 :
105 : // On mac, Return fires the defualt button, not the focused one.
106 : #ifndef XP_MACOSX
107 : case NS_KEY_PRESS:
108 0 : if (NS_KEY_EVENT == aEvent->eventStructType) {
109 0 : nsKeyEvent* keyEvent = (nsKeyEvent*)aEvent;
110 0 : if (NS_VK_RETURN == keyEvent->keyCode) {
111 0 : nsCOMPtr<nsIDOMXULButtonElement> buttonEl(do_QueryInterface(mContent));
112 0 : if (buttonEl) {
113 0 : MouseClicked(aPresContext, aEvent);
114 0 : *aEventStatus = nsEventStatus_eConsumeNoDefault;
115 : }
116 : }
117 : }
118 0 : break;
119 : #endif
120 :
121 : case NS_KEY_UP:
122 0 : if (NS_KEY_EVENT == aEvent->eventStructType) {
123 0 : nsKeyEvent* keyEvent = (nsKeyEvent*)aEvent;
124 0 : if (NS_VK_SPACE == keyEvent->keyCode) {
125 : // only activate on keyup if we're already in the :hover:active state
126 0 : NS_ASSERTION(mContent->IsElement(), "How do we have a non-element?");
127 0 : nsEventStates buttonState = mContent->AsElement()->State();
128 0 : if (buttonState.HasAllStates(NS_EVENT_STATE_ACTIVE |
129 0 : NS_EVENT_STATE_HOVER)) {
130 : // return to normal state
131 0 : nsEventStateManager *esm = aPresContext->EventStateManager();
132 0 : esm->SetContentState(nsnull, NS_EVENT_STATE_ACTIVE);
133 0 : esm->SetContentState(nsnull, NS_EVENT_STATE_HOVER);
134 0 : MouseClicked(aPresContext, aEvent);
135 : }
136 : }
137 : }
138 0 : break;
139 :
140 : case NS_MOUSE_CLICK:
141 0 : if (NS_IS_MOUSE_LEFT_CLICK(aEvent)) {
142 0 : MouseClicked(aPresContext, aEvent);
143 : }
144 0 : break;
145 : }
146 :
147 0 : return nsBoxFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
148 : }
149 :
150 : void
151 0 : nsButtonBoxFrame::DoMouseClick(nsGUIEvent* aEvent, bool aTrustEvent)
152 : {
153 : // Don't execute if we're disabled.
154 0 : if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
155 0 : nsGkAtoms::_true, eCaseMatters))
156 0 : return;
157 :
158 : // Execute the oncommand event handler.
159 0 : bool isShift = false;
160 0 : bool isControl = false;
161 0 : bool isAlt = false;
162 0 : bool isMeta = false;
163 0 : if(aEvent) {
164 0 : isShift = ((nsInputEvent*)(aEvent))->isShift;
165 0 : isControl = ((nsInputEvent*)(aEvent))->isControl;
166 0 : isAlt = ((nsInputEvent*)(aEvent))->isAlt;
167 0 : isMeta = ((nsInputEvent*)(aEvent))->isMeta;
168 : }
169 :
170 : // Have the content handle the event, propagating it according to normal DOM rules.
171 0 : nsCOMPtr<nsIPresShell> shell = PresContext()->GetPresShell();
172 0 : if (shell) {
173 : nsContentUtils::DispatchXULCommand(mContent,
174 : aEvent ?
175 : NS_IS_TRUSTED_EVENT(aEvent) : aTrustEvent,
176 : nsnull, shell,
177 0 : isControl, isAlt, isShift, isMeta);
178 : }
179 : }
|