File:  [mozdev] / chimera / ContentClickListener.mm
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Sat Mar 9 03:34:51 2002 UTC (17 years, 8 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Fix chimera build bustage.  You'll need to update to current trunk of mozilla now. Sigh.

    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"
   40: #include "nsIDOMEventTarget.h"
   41: #include "nsIContent.h"
   42: #include "nsIAtom.h"
   43: #include "nsIDOMElement.h"
   44: #include "nsString.h"
   45: #include "nsUnicharUtils.h"
   46: #include "nsIPrefBranch.h"
   47: #include "nsIDOMMouseEvent.h"
   48: #include "nsEmbedAPI.h"
   49: #include "nsIDOMHTMLAnchorElement.h"
   50: #include "nsIDOMHTMLAreaElement.h"
   51: #include "nsIDOMHTMLLinkElement.h"
   52: #include "nsIDOMHTMLImageElement.h"
   53: #include "nsIDOMCharacterData.h"
   54: 
   55: // Common helper routines (also used by the context menu code)
   56: #include "GeckoUtils.h"
   57: 
   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: {
   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:   
   83:   nsCOMPtr<nsIDOMElement> linkContent;
   84:   ToLowerCase(localName);
   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:   }
  137:   
  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];
  145:   
  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);
  153:   switch (button) {
  154:     case 0: {
  155:       PRBool metaKey, shiftKey, altKey;
  156:       mouseEvent->GetMetaKey(&metaKey);
  157:       mouseEvent->GetShiftKey(&shiftKey);
  158:       mouseEvent->GetAltKey(&altKey);
  159:       if (metaKey) {
  160:         // The command key is down.  Open the link in a new window or tab.
  161:         PRBool useTab;
  162:         pref->GetBoolPref("browser.tabs.opentabfor.middleclick", &useTab);
  163:         PRBool loadInBackground;
  164:         pref->GetBoolPref("browser.tabs.loadInBackground", &loadInBackground);
  165:         if (shiftKey)
  166:           loadInBackground = !loadInBackground;
  167:         if (useTab)
  168:           [mBrowserController openNewTabWithURL: urlToLoad loadInBackground: loadInBackground];
  169:         else
  170:           [mBrowserController openNewWindowWithURL: urlToLoad loadInBackground: loadInBackground];
  171:       }
  172:       else if (altKey) {
  173:         // The user wants to save this link.
  174:         nsAutoString text;
  175:         GeckoUtils::GatherTextUnder(content, text);
  176:         
  177:       }
  178:     }
  179:   }
  180:     
  181:   return NS_OK;
  182: }

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