Annotation of chimera/ContentClickListener.mm, revision 1.7

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.6       hyatt      52: #include "nsIDOMHTMLImageElement.h"
                     53: #include "nsIDOMCharacterData.h"
1.1       hyatt      54: 
1.5       hyatt      55: // Common helper routines (also used by the context menu code)
                     56: #include "GeckoUtils.h"
                     57: 
1.1       hyatt      58: NS_IMPL_ISUPPORTS2(ContentClickListener, nsIDOMMouseListener, nsIDOMEventListener);
                     59: 
                     60: ContentClickListener::ContentClickListener(id aBrowserController)
                     61: :mBrowserController(aBrowserController)
                     62: {
                     63:   NS_INIT_ISUPPORTS();
                     64: }
                     65: 
                     66: ContentClickListener::~ContentClickListener()
                     67: {
                     68: 
                     69: }
                     70: 
                     71: NS_IMETHODIMP
                     72: ContentClickListener::MouseClick(nsIDOMEvent* aEvent)
                     73: {
1.2       hyatt      74:   nsCOMPtr<nsIDOMEventTarget> target;
                     75:   aEvent->GetTarget(getter_AddRefs(target));
                     76:   if (!target)
                     77:     return NS_OK;
                     78:   nsCOMPtr<nsIDOMElement> content(do_QueryInterface(target));
                     79:   nsAutoString localName;
                     80:   if (content)
                     81:     content->GetLocalName(localName);
                     82:   
1.3       hyatt      83:   nsCOMPtr<nsIDOMElement> linkContent;
1.2       hyatt      84:   ToLowerCase(localName);
1.3       hyatt      85:   nsAutoString href;
                     86:   if (localName.Equals(NS_LITERAL_STRING("a")) ||
                     87:       localName.Equals(NS_LITERAL_STRING("area")) ||
                     88:       localName.Equals(NS_LITERAL_STRING("link"))) {
                     89:     PRBool hasAttr;
                     90:     content->HasAttribute(NS_LITERAL_STRING("href"), &hasAttr);
                     91:     if (hasAttr) {
                     92:       linkContent = content;
                     93:       nsCOMPtr<nsIDOMHTMLAnchorElement> anchor(do_QueryInterface(linkContent));
                     94:       if (anchor)
                     95:         anchor->GetHref(href);
                     96:       else {
                     97:         nsCOMPtr<nsIDOMHTMLAreaElement> area(do_QueryInterface(linkContent));
                     98:         if (area)
                     99:           area->GetHref(href);
                    100:         else {
                    101:           nsCOMPtr<nsIDOMHTMLLinkElement> link(do_QueryInterface(linkContent));
                    102:           if (link)
                    103:             link->GetHref(href);
                    104:         }
                    105:       }
                    106:     }
                    107:   }
                    108:   else {
                    109:     // XXXdwh Handle prefilling of forms (input fields) on a click.
                    110:     nsCOMPtr<nsIDOMNode> curr(do_QueryInterface(target));
                    111:     nsCOMPtr<nsIDOMNode> temp = curr;
                    112:     temp->GetParentNode(getter_AddRefs(curr));
                    113:     while (curr) {
                    114:       content = do_QueryInterface(curr);
                    115:       if (!content)
                    116:         break;
                    117:       content->GetLocalName(localName);
                    118:       ToLowerCase(localName);
                    119:       if (localName.Equals(NS_LITERAL_STRING("a"))) {
                    120:         PRBool hasAttr;
                    121:         content->HasAttribute(NS_LITERAL_STRING("href"), &hasAttr);
                    122:         if (hasAttr) {
                    123:           linkContent = content;
                    124:           nsCOMPtr<nsIDOMHTMLAnchorElement> anchor(do_QueryInterface(linkContent));
                    125:           if (anchor)
                    126:             anchor->GetHref(href);
                    127:         }
                    128:         else
                    129:           linkContent = nsnull; // Links can't be nested.
                    130:         break;
                    131:       }
                    132:       
                    133:       temp = curr;
                    134:       temp->GetParentNode(getter_AddRefs(curr));
                    135:     }
                    136:   }
1.2       hyatt     137:   
1.3       hyatt     138:   // XXXdwh Handle simple XLINKs if we want to be compatible with Mozilla, but who
                    139:   // really uses these anyway? :)
                    140:   if (!linkContent || href.IsEmpty())
                    141:     return NS_OK;
                    142: 
                    143:   NSString* hrefStr = [NSString stringWithCharacters: href.get() length:nsCRT::strlen(href.get())];
                    144:   NSURL* urlToLoad = [NSURL URLWithString: hrefStr];
1.2       hyatt     145:   
1.3       hyatt     146:   nsCOMPtr<nsIPrefBranch> pref(do_GetService("@mozilla.org/preferences-service;1"));
                    147:   if (!pref)
                    148:     return NS_OK; // Something bad happened if we can't get prefs.
                    149:     
                    150:   PRUint16 button;
                    151:   nsCOMPtr<nsIDOMMouseEvent> mouseEvent(do_QueryInterface(aEvent));
                    152:   mouseEvent->GetButton(&button);
1.7     ! hyatt     153: 
        !           154:   PRBool metaKey, shiftKey, altKey;
        !           155:   mouseEvent->GetMetaKey(&metaKey);
        !           156:   mouseEvent->GetShiftKey(&shiftKey);
        !           157:   mouseEvent->GetAltKey(&altKey);
        !           158:   if ((metaKey && button == 0) || button == 1) {
        !           159:     // The command key is down or we got a middle click.  Open the link in a new window or tab.
        !           160:     PRBool useTab;
        !           161:     pref->GetBoolPref("browser.tabs.opentabfor.middleclick", &useTab);
        !           162:     PRBool loadInBackground;
        !           163:     pref->GetBoolPref("browser.tabs.loadInBackground", &loadInBackground);
        !           164:     if (shiftKey)
        !           165:       loadInBackground = !loadInBackground;
        !           166:     if (useTab)
        !           167:       [mBrowserController openNewTabWithURL: urlToLoad loadInBackground: loadInBackground];
        !           168:     else
        !           169:       [mBrowserController openNewWindowWithURL: urlToLoad loadInBackground: loadInBackground];
1.3       hyatt     170:   }
1.7     ! hyatt     171:   else if (altKey) {
        !           172:     // The user wants to save this link.
        !           173:     nsAutoString text;
        !           174:     GeckoUtils::GatherTextUnder(content, text);
        !           175:   }
        !           176: 
1.1       hyatt     177:   return NS_OK;
                    178: }

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