Annotation of chimera/ContentClickListener.mm, revision 1.4

1.1       hyatt       1: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
                      2: /* ***** BEGIN LICENSE BLOCK *****
                      3:  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
                      4:  *
                      5:  * The contents of this file are subject to the Netscape Public License
                      6:  * Version 1.1 (the "License"); you may not use this file except in
                      7:  * compliance with the License. You may obtain a copy of the License at
                      8:  * http://www.mozilla.org/NPL/
                      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) 2002
                     20:  * the Initial Developer. All Rights Reserved.
                     21:  *
                     22:  * Contributor(s):
                     23:  *
                     24:  * Alternatively, the contents of this file may be used under the terms of
                     25:  * either the GNU General Public License Version 2 or later (the "GPL"), or 
                     26:  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
                     27:  * in which case the provisions of the GPL or the LGPL are applicable instead
                     28:  * of those above. If you wish to allow use of your version of this file only
                     29:  * under the terms of either the GPL or the LGPL, and not to allow others to
                     30:  * use your version of this file under the terms of the NPL, indicate your
                     31:  * decision by deleting the provisions above and replace them with the notice
                     32:  * and other provisions required by the GPL or the LGPL. If you do not delete
                     33:  * the provisions above, a recipient may use your version of this file under
                     34:  * the terms of any one of the NPL, the GPL or the LGPL.
                     35:  *
                     36:  * ***** END LICENSE BLOCK ***** */
                     37: 
                     38: #include "nsCOMPtr.h"
                     39: #include "ContentClickListener.h"
1.2       hyatt      40: #include "nsIDOMEventTarget.h"
                     41: #include "nsIContent.h"
                     42: #include "nsIAtom.h"
                     43: #include "nsIDOMElement.h"
                     44: #include "nsString.h"
                     45: #include "nsUnicharUtils.h"
1.3       hyatt      46: #include "nsIPrefBranch.h"
                     47: #include "nsIDOMMouseEvent.h"
                     48: #include "nsEmbedAPI.h"
                     49: #include "nsIDOMHTMLAnchorElement.h"
                     50: #include "nsIDOMHTMLAreaElement.h"
                     51: #include "nsIDOMHTMLLinkElement.h"
1.1       hyatt      52: 
                     53: NS_IMPL_ISUPPORTS2(ContentClickListener, nsIDOMMouseListener, nsIDOMEventListener);
                     54: 
                     55: ContentClickListener::ContentClickListener(id aBrowserController)
                     56: :mBrowserController(aBrowserController)
                     57: {
                     58:   NS_INIT_ISUPPORTS();
                     59: }
                     60: 
                     61: ContentClickListener::~ContentClickListener()
                     62: {
                     63: 
                     64: }
                     65: 
                     66: NS_IMETHODIMP
                     67: ContentClickListener::MouseClick(nsIDOMEvent* aEvent)
                     68: {
1.2       hyatt      69:   nsCOMPtr<nsIDOMEventTarget> target;
                     70:   aEvent->GetTarget(getter_AddRefs(target));
                     71:   if (!target)
                     72:     return NS_OK;
                     73:   nsCOMPtr<nsIDOMElement> content(do_QueryInterface(target));
                     74:   nsAutoString localName;
                     75:   if (content)
                     76:     content->GetLocalName(localName);
                     77:   
1.3       hyatt      78:   nsCOMPtr<nsIDOMElement> linkContent;
1.2       hyatt      79:   ToLowerCase(localName);
1.3       hyatt      80:   nsAutoString href;
                     81:   if (localName.Equals(NS_LITERAL_STRING("a")) ||
                     82:       localName.Equals(NS_LITERAL_STRING("area")) ||
                     83:       localName.Equals(NS_LITERAL_STRING("link"))) {
                     84:     PRBool hasAttr;
                     85:     content->HasAttribute(NS_LITERAL_STRING("href"), &hasAttr);
                     86:     if (hasAttr) {
                     87:       linkContent = content;
                     88:       nsCOMPtr<nsIDOMHTMLAnchorElement> anchor(do_QueryInterface(linkContent));
                     89:       if (anchor)
                     90:         anchor->GetHref(href);
                     91:       else {
                     92:         nsCOMPtr<nsIDOMHTMLAreaElement> area(do_QueryInterface(linkContent));
                     93:         if (area)
                     94:           area->GetHref(href);
                     95:         else {
                     96:           nsCOMPtr<nsIDOMHTMLLinkElement> link(do_QueryInterface(linkContent));
                     97:           if (link)
                     98:             link->GetHref(href);
                     99:         }
                    100:       }
                    101:     }
                    102:   }
                    103:   else {
                    104:     // XXXdwh Handle prefilling of forms (input fields) on a click.
                    105:     nsCOMPtr<nsIDOMNode> curr(do_QueryInterface(target));
                    106:     nsCOMPtr<nsIDOMNode> temp = curr;
                    107:     temp->GetParentNode(getter_AddRefs(curr));
                    108:     while (curr) {
                    109:       content = do_QueryInterface(curr);
                    110:       if (!content)
                    111:         break;
                    112:       content->GetLocalName(localName);
                    113:       ToLowerCase(localName);
                    114:       if (localName.Equals(NS_LITERAL_STRING("a"))) {
                    115:         PRBool hasAttr;
                    116:         content->HasAttribute(NS_LITERAL_STRING("href"), &hasAttr);
                    117:         if (hasAttr) {
                    118:           linkContent = content;
                    119:           nsCOMPtr<nsIDOMHTMLAnchorElement> anchor(do_QueryInterface(linkContent));
                    120:           if (anchor)
                    121:             anchor->GetHref(href);
                    122:         }
                    123:         else
                    124:           linkContent = nsnull; // Links can't be nested.
                    125:         break;
                    126:       }
                    127:       
                    128:       temp = curr;
                    129:       temp->GetParentNode(getter_AddRefs(curr));
                    130:     }
                    131:   }
1.2       hyatt     132:   
1.3       hyatt     133:   // XXXdwh Handle simple XLINKs if we want to be compatible with Mozilla, but who
                    134:   // really uses these anyway? :)
                    135:   if (!linkContent || href.IsEmpty())
                    136:     return NS_OK;
                    137: 
                    138:   NSString* hrefStr = [NSString stringWithCharacters: href.get() length:nsCRT::strlen(href.get())];
                    139:   NSURL* urlToLoad = [NSURL URLWithString: hrefStr];
1.2       hyatt     140:   
1.3       hyatt     141:   nsCOMPtr<nsIPrefBranch> pref(do_GetService("@mozilla.org/preferences-service;1"));
                    142:   if (!pref)
                    143:     return NS_OK; // Something bad happened if we can't get prefs.
                    144:     
                    145:   PRUint16 button;
                    146:   nsCOMPtr<nsIDOMMouseEvent> mouseEvent(do_QueryInterface(aEvent));
                    147:   mouseEvent->GetButton(&button);
                    148:   switch (button) {
                    149:     case 0: {
                    150:       PRBool metaKey, shiftKey;
                    151:       mouseEvent->GetMetaKey(&metaKey);
                    152:       mouseEvent->GetShiftKey(&shiftKey);
                    153:       if (metaKey) {
                    154:         // The command key is down.  Open the link in a new window or tab.
                    155:         PRBool useTab;
                    156:         pref->GetBoolPref("browser.tabs.opentabfor.middleclick", &useTab);
                    157:         PRBool loadInBackground;
                    158:         pref->GetBoolPref("browser.tabs.loadInBackground", &loadInBackground);
                    159:         if (shiftKey)
                    160:           loadInBackground = !loadInBackground;
1.4     ! hyatt     161:         if (useTab)
        !           162:           [mBrowserController openNewTabWithURL: urlToLoad loadInBackground: loadInBackground];
1.3       hyatt     163:         else
                    164:           [mBrowserController openNewWindowWithURL: urlToLoad loadInBackground: loadInBackground];
                    165:       }
                    166:     }
                    167:   }
                    168:     
1.1       hyatt     169:   return NS_OK;
                    170: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>