--- chimera/ContentClickListener.mm 2002/03/07 18:35:48 1.1 +++ chimera/ContentClickListener.mm 2002/03/08 01:43:01 1.3 @@ -37,6 +37,18 @@ #include "nsCOMPtr.h" #include "ContentClickListener.h" +#include "nsIDOMEventTarget.h" +#include "nsIContent.h" +#include "nsIAtom.h" +#include "nsIDOMElement.h" +#include "nsString.h" +#include "nsUnicharUtils.h" +#include "nsIPrefBranch.h" +#include "nsIDOMMouseEvent.h" +#include "nsEmbedAPI.h" +#include "nsIDOMHTMLAnchorElement.h" +#include "nsIDOMHTMLAreaElement.h" +#include "nsIDOMHTMLLinkElement.h" NS_IMPL_ISUPPORTS2(ContentClickListener, nsIDOMMouseListener, nsIDOMEventListener); @@ -54,6 +66,106 @@ ContentClickListener::~ContentClickListe NS_IMETHODIMP ContentClickListener::MouseClick(nsIDOMEvent* aEvent) { - printf("You clicked in the content area! Good for you!\n"); + nsCOMPtr target; + aEvent->GetTarget(getter_AddRefs(target)); + if (!target) + return NS_OK; + nsCOMPtr content(do_QueryInterface(target)); + nsAutoString localName; + if (content) + content->GetLocalName(localName); + + nsCOMPtr linkContent; + ToLowerCase(localName); + nsAutoString href; + if (localName.Equals(NS_LITERAL_STRING("a")) || + localName.Equals(NS_LITERAL_STRING("area")) || + localName.Equals(NS_LITERAL_STRING("link"))) { + PRBool hasAttr; + content->HasAttribute(NS_LITERAL_STRING("href"), &hasAttr); + if (hasAttr) { + linkContent = content; + nsCOMPtr anchor(do_QueryInterface(linkContent)); + if (anchor) + anchor->GetHref(href); + else { + nsCOMPtr area(do_QueryInterface(linkContent)); + if (area) + area->GetHref(href); + else { + nsCOMPtr link(do_QueryInterface(linkContent)); + if (link) + link->GetHref(href); + } + } + } + } + else { + // XXXdwh Handle prefilling of forms (input fields) on a click. + nsCOMPtr curr(do_QueryInterface(target)); + nsCOMPtr temp = curr; + temp->GetParentNode(getter_AddRefs(curr)); + while (curr) { + content = do_QueryInterface(curr); + if (!content) + break; + content->GetLocalName(localName); + ToLowerCase(localName); + if (localName.Equals(NS_LITERAL_STRING("a"))) { + PRBool hasAttr; + content->HasAttribute(NS_LITERAL_STRING("href"), &hasAttr); + if (hasAttr) { + linkContent = content; + nsCOMPtr anchor(do_QueryInterface(linkContent)); + if (anchor) + anchor->GetHref(href); + } + else + linkContent = nsnull; // Links can't be nested. + break; + } + + temp = curr; + temp->GetParentNode(getter_AddRefs(curr)); + } + } + + // XXXdwh Handle simple XLINKs if we want to be compatible with Mozilla, but who + // really uses these anyway? :) + if (!linkContent || href.IsEmpty()) + return NS_OK; + + NSString* hrefStr = [NSString stringWithCharacters: href.get() length:nsCRT::strlen(href.get())]; + NSURL* urlToLoad = [NSURL URLWithString: hrefStr]; + + nsCOMPtr pref(do_GetService("@mozilla.org/preferences-service;1")); + if (!pref) + return NS_OK; // Something bad happened if we can't get prefs. + + PRUint16 button; + nsCOMPtr mouseEvent(do_QueryInterface(aEvent)); + mouseEvent->GetButton(&button); + switch (button) { + case 0: { + PRBool metaKey, shiftKey; + mouseEvent->GetMetaKey(&metaKey); + mouseEvent->GetShiftKey(&shiftKey); + if (metaKey) { + // The command key is down. Open the link in a new window or tab. + PRBool useTab; + pref->GetBoolPref("browser.tabs.opentabfor.middleclick", &useTab); + PRBool loadInBackground; + pref->GetBoolPref("browser.tabs.loadInBackground", &loadInBackground); + if (shiftKey) + loadInBackground = !loadInBackground; + if (useTab) { + + } + else + [mBrowserController openNewWindowWithURL: urlToLoad loadInBackground: loadInBackground]; + } + } + } + return NS_OK; }