LCOV - code coverage report
Current view: directory - content/xul/content/src - nsXULContextMenuBuilder.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 119 2 1.7 %
Date: 2012-06-02 Functions: 19 2 10.5 %

       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.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is Mozilla Foundation
      18                 :  * Portions created by the Initial Developer are Copyright (C) 2011
      19                 :  * the Initial Developer. All Rights Reserved.
      20                 :  *
      21                 :  * Contributor(s):
      22                 :  *
      23                 :  * Alternatively, the contents of this file may be used under the terms of
      24                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
      25                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      26                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      27                 :  * of those above. If you wish to allow use of your version of this file only
      28                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      29                 :  * use your version of this file under the terms of the MPL, indicate your
      30                 :  * decision by deleting the provisions above and replace them with the notice
      31                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      32                 :  * the provisions above, a recipient may use your version of this file under
      33                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      34                 :  *
      35                 :  * ***** END LICENSE BLOCK ***** */
      36                 : 
      37                 : #include "nsContentCreatorFunctions.h"
      38                 : #include "nsIDOMHTMLElement.h"
      39                 : #include "nsIDOMHTMLMenuItemElement.h"
      40                 : #include "nsXULContextMenuBuilder.h"
      41                 : 
      42                 : using namespace mozilla;
      43                 : using namespace mozilla::dom;
      44                 : 
      45               0 : nsXULContextMenuBuilder::nsXULContextMenuBuilder()
      46               0 :   : mCurrentGeneratedItemId(0)
      47                 : {
      48               0 : }
      49                 : 
      50               0 : nsXULContextMenuBuilder::~nsXULContextMenuBuilder()
      51                 : {
      52               0 : }
      53                 : 
      54            1464 : NS_IMPL_CYCLE_COLLECTION_CLASS(nsXULContextMenuBuilder)
      55               0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXULContextMenuBuilder)
      56               0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mFragment)
      57               0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mDocument)
      58               0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mCurrentNode)
      59               0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(mElements)
      60               0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
      61                 : 
      62               0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXULContextMenuBuilder)
      63               0 :   NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mFragment)
      64               0 :   NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mDocument)
      65               0 :   NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mCurrentNode)
      66               0 :   NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMARRAY(mElements)
      67               0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
      68                 : 
      69               0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(nsXULContextMenuBuilder)
      70               0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(nsXULContextMenuBuilder)
      71                 : 
      72               0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXULContextMenuBuilder)
      73               0 :   NS_INTERFACE_MAP_ENTRY(nsIMenuBuilder)
      74               0 :   NS_INTERFACE_MAP_ENTRY(nsIXULContextMenuBuilder)
      75               0 :   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMenuBuilder)
      76               0 : NS_INTERFACE_MAP_END
      77                 : 
      78                 : 
      79                 : NS_IMETHODIMP
      80               0 : nsXULContextMenuBuilder::OpenContainer(const nsAString& aLabel)
      81                 : {
      82               0 :   if (!mFragment) {
      83               0 :     return NS_ERROR_NOT_INITIALIZED;
      84                 :   }
      85                 : 
      86               0 :   if (!mCurrentNode) {
      87               0 :     mCurrentNode = mFragment;
      88                 :   } else {
      89               0 :     nsCOMPtr<nsIContent> menu;
      90               0 :     nsresult rv = CreateElement(nsGkAtoms::menu, nsnull, getter_AddRefs(menu));
      91               0 :     NS_ENSURE_SUCCESS(rv, rv);
      92                 : 
      93               0 :     menu->SetAttr(kNameSpaceID_None, nsGkAtoms::label, aLabel, false);
      94                 : 
      95               0 :     nsCOMPtr<nsIContent> menuPopup;
      96                 :     rv = CreateElement(nsGkAtoms::menupopup, nsnull,
      97               0 :                        getter_AddRefs(menuPopup));
      98               0 :     NS_ENSURE_SUCCESS(rv, rv);
      99                 :         
     100               0 :     rv = menu->AppendChildTo(menuPopup, false);
     101               0 :     NS_ENSURE_SUCCESS(rv, rv);
     102                 : 
     103               0 :     rv = mCurrentNode->AppendChildTo(menu, false);
     104               0 :     NS_ENSURE_SUCCESS(rv, rv);
     105                 : 
     106               0 :     mCurrentNode = menuPopup;
     107                 :   }
     108                 : 
     109               0 :   return NS_OK;
     110                 : }
     111                 : 
     112                 : NS_IMETHODIMP
     113               0 : nsXULContextMenuBuilder::AddItemFor(nsIDOMHTMLMenuItemElement* aElement,
     114                 :                                     bool aCanLoadIcon)
     115                 : {
     116               0 :   if (!mFragment) {
     117               0 :     return NS_ERROR_NOT_INITIALIZED;
     118                 :   }
     119                 : 
     120               0 :   nsCOMPtr<nsIContent> menuitem;
     121                 :   nsresult rv = CreateElement(nsGkAtoms::menuitem, aElement,
     122               0 :                               getter_AddRefs(menuitem));
     123               0 :   NS_ENSURE_SUCCESS(rv, rv);
     124                 : 
     125               0 :   nsAutoString type;
     126               0 :   aElement->GetType(type);
     127               0 :   if (type.EqualsLiteral("checkbox") || type.EqualsLiteral("radio")) {
     128                 :     // The menu is only temporary, so we don't need to handle
     129                 :     // the radio type precisely.
     130                 :     menuitem->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
     131               0 :                       NS_LITERAL_STRING("checkbox"), false);
     132                 :     bool checked;
     133               0 :     aElement->GetChecked(&checked);
     134               0 :     if (checked) {
     135                 :       menuitem->SetAttr(kNameSpaceID_None, nsGkAtoms::checked,
     136               0 :                         NS_LITERAL_STRING("true"), false);
     137                 :     }
     138                 :   }
     139                 : 
     140               0 :   nsAutoString label;
     141               0 :   aElement->GetLabel(label);
     142               0 :   menuitem->SetAttr(kNameSpaceID_None, nsGkAtoms::label, label, false);
     143                 : 
     144               0 :   nsAutoString icon;
     145               0 :   aElement->GetIcon(icon);
     146               0 :   if (!icon.IsEmpty()) {
     147                 :     menuitem->SetAttr(kNameSpaceID_None, nsGkAtoms::_class,
     148               0 :                       NS_LITERAL_STRING("menuitem-iconic"), false);
     149               0 :     if (aCanLoadIcon) {
     150               0 :       menuitem->SetAttr(kNameSpaceID_None, nsGkAtoms::image, icon, false);
     151                 :     }
     152                 :   }
     153                 : 
     154                 :   bool disabled;
     155               0 :   aElement->GetDisabled(&disabled);
     156               0 :   if (disabled) {
     157                 :     menuitem->SetAttr(kNameSpaceID_None, nsGkAtoms::disabled,
     158               0 :                       NS_LITERAL_STRING("true"), false);
     159                 :   }
     160                 : 
     161               0 :   return mCurrentNode->AppendChildTo(menuitem, false);
     162                 : }
     163                 : 
     164                 : NS_IMETHODIMP
     165               0 : nsXULContextMenuBuilder::AddSeparator()
     166                 : {
     167               0 :   if (!mFragment) {
     168               0 :     return NS_ERROR_NOT_INITIALIZED;
     169                 :   }
     170                 : 
     171               0 :   nsCOMPtr<nsIContent> menuseparator;
     172                 :   nsresult rv = CreateElement(nsGkAtoms::menuseparator, nsnull,
     173               0 :                               getter_AddRefs(menuseparator));
     174               0 :   NS_ENSURE_SUCCESS(rv, rv);
     175                 : 
     176               0 :   return mCurrentNode->AppendChildTo(menuseparator, false);
     177                 : }
     178                 : 
     179                 : NS_IMETHODIMP
     180               0 : nsXULContextMenuBuilder::UndoAddSeparator()
     181                 : {
     182               0 :   if (!mFragment) {
     183               0 :     return NS_ERROR_NOT_INITIALIZED;
     184                 :   }
     185                 : 
     186               0 :   PRUint32 count = mCurrentNode->GetChildCount();
     187               0 :   if (!count ||
     188               0 :       mCurrentNode->GetChildAt(count - 1)->Tag() != nsGkAtoms::menuseparator) {
     189               0 :     return NS_OK;
     190                 :   }
     191                 : 
     192               0 :   return mCurrentNode->RemoveChildAt(count - 1, false);
     193                 : }
     194                 : 
     195                 : NS_IMETHODIMP
     196               0 : nsXULContextMenuBuilder::CloseContainer()
     197                 : {
     198               0 :   if (!mFragment) {
     199               0 :     return NS_ERROR_NOT_INITIALIZED;
     200                 :   }
     201                 : 
     202               0 :   if (mCurrentNode == mFragment) {
     203               0 :     mCurrentNode = nsnull;
     204                 :   } else {
     205               0 :     nsIContent* parent = mCurrentNode->GetParent();
     206               0 :     mCurrentNode = parent->GetParent();
     207                 :   }
     208                 : 
     209               0 :   return NS_OK;
     210                 : }
     211                 : 
     212                 : 
     213                 : NS_IMETHODIMP
     214               0 : nsXULContextMenuBuilder::Init(nsIDOMDocumentFragment* aDocumentFragment,
     215                 :                               const nsAString& aGeneratedItemIdAttrName)
     216                 : {
     217               0 :   NS_ENSURE_ARG_POINTER(aDocumentFragment);
     218                 : 
     219               0 :   mFragment = do_QueryInterface(aDocumentFragment);
     220               0 :   mDocument = mFragment->GetOwnerDocument();
     221               0 :   mGeneratedItemIdAttr = do_GetAtom(aGeneratedItemIdAttrName);
     222                 : 
     223               0 :   return NS_OK;
     224                 : }
     225                 : 
     226                 : NS_IMETHODIMP
     227               0 : nsXULContextMenuBuilder::Click(const nsAString& aGeneratedItemId)
     228                 : {
     229                 :   PRInt32 rv;
     230               0 :   PRInt32 idx = nsString(aGeneratedItemId).ToInteger(&rv);
     231               0 :   if (NS_SUCCEEDED(rv)) {
     232               0 :     nsCOMPtr<nsIDOMHTMLElement> element = mElements.SafeObjectAt(idx);
     233               0 :     if (element) {
     234               0 :       element->Click();
     235                 :     }
     236                 :   }
     237                 : 
     238               0 :   return NS_OK;
     239                 : }
     240                 : 
     241                 : nsresult
     242               0 : nsXULContextMenuBuilder::CreateElement(nsIAtom* aTag,
     243                 :                                        nsIDOMHTMLElement* aHTMLElement,
     244                 :                                        nsIContent** aResult)
     245                 : {
     246               0 :   *aResult = nsnull;
     247                 : 
     248                 :   nsCOMPtr<nsINodeInfo> nodeInfo = mDocument->NodeInfoManager()->GetNodeInfo(
     249               0 :     aTag, nsnull, kNameSpaceID_XUL, nsIDOMNode::ELEMENT_NODE);
     250               0 :   NS_ENSURE_TRUE(nodeInfo, NS_ERROR_OUT_OF_MEMORY);
     251                 : 
     252               0 :   nsresult rv = NS_NewElement(aResult, nodeInfo.forget(), NOT_FROM_PARSER);
     253               0 :   if (NS_FAILED(rv)) {
     254               0 :     return rv;
     255                 :   }
     256                 : 
     257               0 :   nsAutoString generateditemid;
     258                 : 
     259               0 :   if (aHTMLElement) {
     260               0 :     mElements.AppendObject(aHTMLElement);
     261               0 :     generateditemid.AppendInt(mCurrentGeneratedItemId++);
     262                 :   }
     263                 : 
     264                 :   (*aResult)->SetAttr(kNameSpaceID_None, mGeneratedItemIdAttr, generateditemid,
     265               0 :                       false);
     266                 : 
     267               0 :   return NS_OK;
     268            4392 : }

Generated by: LCOV version 1.7