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 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either of the GNU General Public License Version 2 or later (the "GPL"),
27 : * or 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.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 : #include "nsISupportsUtils.h"
39 : #include "nsIMenuBoxObject.h"
40 : #include "nsBoxObject.h"
41 : #include "nsIFrame.h"
42 : #include "nsGUIEvent.h"
43 : #include "nsIDOMNSEvent.h"
44 : #include "nsMenuBarFrame.h"
45 : #include "nsMenuBarListener.h"
46 : #include "nsMenuFrame.h"
47 : #include "nsMenuPopupFrame.h"
48 :
49 : class nsMenuBoxObject : public nsIMenuBoxObject,
50 : public nsBoxObject
51 : {
52 : public:
53 : NS_DECL_ISUPPORTS_INHERITED
54 : NS_DECL_NSIMENUBOXOBJECT
55 :
56 : nsMenuBoxObject();
57 : virtual ~nsMenuBoxObject();
58 : };
59 :
60 0 : nsMenuBoxObject::nsMenuBoxObject()
61 : {
62 0 : }
63 :
64 0 : nsMenuBoxObject::~nsMenuBoxObject()
65 : {
66 0 : }
67 :
68 0 : NS_IMPL_ISUPPORTS_INHERITED1(nsMenuBoxObject, nsBoxObject, nsIMenuBoxObject)
69 :
70 : /* void openMenu (in boolean openFlag); */
71 0 : NS_IMETHODIMP nsMenuBoxObject::OpenMenu(bool aOpenFlag)
72 : {
73 0 : nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
74 0 : if (pm) {
75 0 : nsIFrame* frame = GetFrame(false);
76 0 : if (frame) {
77 0 : if (aOpenFlag) {
78 0 : nsCOMPtr<nsIContent> content = mContent;
79 0 : pm->ShowMenu(content, false, false);
80 : }
81 : else {
82 0 : if (frame->GetType() == nsGkAtoms::menuFrame) {
83 0 : nsMenuPopupFrame* popupFrame = (static_cast<nsMenuFrame *>(frame))->GetPopup();
84 0 : if (popupFrame)
85 0 : pm->HidePopup(popupFrame->GetContent(), false, true, false);
86 : }
87 : }
88 : }
89 : }
90 :
91 0 : return NS_OK;
92 : }
93 :
94 0 : NS_IMETHODIMP nsMenuBoxObject::GetActiveChild(nsIDOMElement** aResult)
95 : {
96 0 : *aResult = nsnull;
97 0 : nsIFrame* frame = GetFrame(false);
98 0 : if (frame && frame->GetType() == nsGkAtoms::menuFrame)
99 0 : return static_cast<nsMenuFrame *>(frame)->GetActiveChild(aResult);
100 0 : return NS_OK;
101 : }
102 :
103 0 : NS_IMETHODIMP nsMenuBoxObject::SetActiveChild(nsIDOMElement* aResult)
104 : {
105 0 : nsIFrame* frame = GetFrame(false);
106 0 : if (frame && frame->GetType() == nsGkAtoms::menuFrame)
107 0 : return static_cast<nsMenuFrame *>(frame)->SetActiveChild(aResult);
108 0 : return NS_OK;
109 : }
110 :
111 : /* boolean handleKeyPress (in nsIDOMKeyEvent keyEvent); */
112 0 : NS_IMETHODIMP nsMenuBoxObject::HandleKeyPress(nsIDOMKeyEvent* aKeyEvent, bool* aHandledFlag)
113 : {
114 0 : *aHandledFlag = false;
115 0 : NS_ENSURE_ARG(aKeyEvent);
116 :
117 0 : nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
118 0 : if (!pm)
119 0 : return NS_OK;
120 :
121 : // if event has already been handled, bail
122 0 : nsCOMPtr<nsIDOMNSEvent> domNSEvent = do_QueryInterface(aKeyEvent);
123 0 : if (!domNSEvent)
124 0 : return NS_OK;
125 :
126 0 : bool eventHandled = false;
127 0 : domNSEvent->GetPreventDefault(&eventHandled);
128 0 : if (eventHandled)
129 0 : return NS_OK;
130 :
131 0 : if (nsMenuBarListener::IsAccessKeyPressed(aKeyEvent))
132 0 : return NS_OK;
133 :
134 0 : nsIFrame* frame = GetFrame(false);
135 0 : if (!frame || frame->GetType() != nsGkAtoms::menuFrame)
136 0 : return NS_OK;
137 :
138 0 : nsMenuPopupFrame* popupFrame = static_cast<nsMenuFrame *>(frame)->GetPopup();
139 0 : if (!popupFrame)
140 0 : return NS_OK;
141 :
142 : PRUint32 keyCode;
143 0 : aKeyEvent->GetKeyCode(&keyCode);
144 0 : switch (keyCode) {
145 : case NS_VK_UP:
146 : case NS_VK_DOWN:
147 : case NS_VK_HOME:
148 : case NS_VK_END:
149 : {
150 : nsNavigationDirection theDirection;
151 0 : theDirection = NS_DIRECTION_FROM_KEY_CODE(popupFrame, keyCode);
152 : *aHandledFlag =
153 0 : pm->HandleKeyboardNavigationInPopup(popupFrame, theDirection);
154 0 : return NS_OK;
155 : }
156 : default:
157 0 : *aHandledFlag = pm->HandleShortcutNavigation(aKeyEvent, popupFrame);
158 0 : return NS_OK;
159 : }
160 : }
161 :
162 : NS_IMETHODIMP
163 0 : nsMenuBoxObject::GetOpenedWithKey(bool* aOpenedWithKey)
164 : {
165 0 : *aOpenedWithKey = false;
166 :
167 0 : nsIFrame* frame = GetFrame(false);
168 0 : if (!frame || frame->GetType() != nsGkAtoms::menuFrame)
169 0 : return NS_OK;
170 :
171 0 : frame = frame->GetParent();
172 0 : while (frame) {
173 0 : if (frame->GetType() == nsGkAtoms::menuBarFrame) {
174 0 : *aOpenedWithKey = (static_cast<nsMenuBarFrame *>(frame))->IsActiveByKeyboard();
175 0 : return NS_OK;
176 : }
177 0 : frame = frame->GetParent();
178 : }
179 :
180 0 : return NS_OK;
181 : }
182 :
183 :
184 : // Creation Routine ///////////////////////////////////////////////////////////////////////
185 :
186 : nsresult
187 0 : NS_NewMenuBoxObject(nsIBoxObject** aResult)
188 : {
189 0 : *aResult = new nsMenuBoxObject;
190 0 : if (!*aResult)
191 0 : return NS_ERROR_OUT_OF_MEMORY;
192 0 : NS_ADDREF(*aResult);
193 0 : return NS_OK;
194 : }
195 :
|