From 59eb4ba0946e4d633dd6822ddebde00046ef8227 Mon Sep 17 00:00:00 2001 From: "akkana%netscape.com" Date: Fri, 26 Feb 1999 19:00:17 +0000 Subject: [PATCH] Turn on nsSelectionMgr class for copy/paste git-svn-id: svn://10.0.0.236/trunk@22180 18797224-902f-48f8-a5cc-f745e15eee43 --- .../webshell/tests/viewer/nsBrowserWindow.cpp | 178 ++---------------- mozilla/widget/public/nsIAppShell.h | 7 + mozilla/widget/public/nsISelectionMgr.h | 6 +- mozilla/widget/src/gtk/nsAppShell.cpp | 18 ++ mozilla/widget/src/gtk/nsAppShell.h | 2 + mozilla/widget/src/gtk/nsSelectionMgr.cpp | 6 +- mozilla/widget/src/gtk/nsSelectionMgr.h | 2 +- mozilla/widget/src/gtk/nsWindow.cpp | 4 + mozilla/widget/src/mac/nsAppShell.cpp | 19 ++ mozilla/widget/src/mac/nsAppShell.h | 3 + 10 files changed, 74 insertions(+), 171 deletions(-) diff --git a/mozilla/webshell/tests/viewer/nsBrowserWindow.cpp b/mozilla/webshell/tests/viewer/nsBrowserWindow.cpp index 9f67a9c3a64..0ab0c9a7cab 100644 --- a/mozilla/webshell/tests/viewer/nsBrowserWindow.cpp +++ b/mozilla/webshell/tests/viewer/nsBrowserWindow.cpp @@ -76,32 +76,13 @@ #include "resources.h" -#if defined(WIN32) -#include -#else -#include -#endif - #if defined(WIN32) #include #endif -#if defined (XP_MAC) -#include -#include -#include -#include -#endif - -#ifdef DEBUG_akkana -// Leave this commented out until we make sure it doesn't crash Linux boxes -#if defined (XP_UNIX) -#include -#endif -#endif - // For Copy #include "nsIDOMSelection.h" +#include "nsISelectionMgr.h" // XXX For font setting below #include "nsFont.h" @@ -2080,72 +2061,6 @@ nsBrowserWindow::GetPresShell() return GetPresShellFor(mWebShell); } -#ifdef WIN32 - -static const char* gsAOLFormat = "AOLMAIL"; -static const char* gsHTMLFormat = "text/html"; - -void PlaceHTMLOnClipboard(PRUint32 aFormat, char* aData, int aLength) -{ - HGLOBAL hGlobalMemory; - PSTR pGlobalMemory; - - PRUint32 cf_aol = RegisterClipboardFormat(gsAOLFormat); - PRUint32 cf_html = RegisterClipboardFormat(gsHTMLFormat); - - char* preamble = ""; - char* postamble = ""; - - if (aFormat == cf_aol || aFormat == CF_TEXT) - { - preamble = ""; - postamble = ""; - } - - PRInt32 size = aLength + 1 + strlen(preamble) + strlen(postamble); - - - if (aLength) - { - // Copy text to Global Memory Area - hGlobalMemory = (HGLOBAL)GlobalAlloc(GHND, size); - if (hGlobalMemory != NULL) - { - pGlobalMemory = (PSTR) GlobalLock(hGlobalMemory); - - int i; - - // AOL requires HTML prefix/postamble - char* s = preamble; - PRInt32 len = strlen(s); - for (i=0; i < len; i++) - { - *pGlobalMemory++ = *s++; - } - - s = aData; - len = aLength; - for (i=0;i< len;i++) { - *pGlobalMemory++ = *s++; - } - - - s = postamble; - len = strlen(s); - for (i=0; i < len; i++) - { - *pGlobalMemory++ = *s++; - } - - // Put data on Clipboard - GlobalUnlock(hGlobalMemory); - SetClipboardData(aFormat, hGlobalMemory); - } - } -} -#endif - - void nsBrowserWindow::DoCopy() @@ -2179,35 +2094,18 @@ nsBrowserWindow::DoCopy() rv = NS_New_HTML_ContentSinkStream(&sink,PR_FALSE,PR_FALSE); -// ifdef hell: ostrstream doesn't seem to work on mac (it's -// been deprecated too), but stringstream doesn't seem to -// work in VC 5, so for the moment we have ifdefs -#if defined(XP_MAC) -#define USE_STRINGSTREAM 1 -#endif -#if defined(XP_WIN) -#define USE_STRSTREAM 1 -#endif - // Leave this commented out until we're sure it won't crash Linux -#if defined(XP_UNIX) -#define USE_COUT 1 -#endif + nsISelectionMgr* selectionMgr; + if (!NS_SUCCEEDED(mAppShell->GetSelectionMgr(&selectionMgr)) + || !selectionMgr) + return; -#if defined(USE_STRINGSTREAM) || defined(USE_STRSTREAM) || defined(USE_COUT) - -#if defined(USE_STRINGSTREAM) - stringstream data; -#else -#if defined(USE_STRSTREAM) - ostrstream data; -#else -#if defined(USE_COUT) -#define data cout - data << "Copied text:" << endl << "------------" << endl; -#endif -#endif -#endif - ((nsHTMLContentSinkStream*)sink)->SetOutputStream(data); + ostream* copyStream; + if (!NS_SUCCEEDED(selectionMgr->GetCopyOStream(©Stream))) + { + NS_RELEASE(selectionMgr); + return; + } + ((nsHTMLContentSinkStream*)sink)->SetOutputStream(*copyStream); if (NS_OK == rv) { parser->SetContentSink(sink); @@ -2223,60 +2121,14 @@ nsBrowserWindow::DoCopy() } NS_IF_RELEASE(dtd); NS_IF_RELEASE(sink); -#if defined(USE_STRINGSTREAM) - string theString = data.str(); - PRInt32 len = theString.length(); - const char* str = theString.data(); -#else -#if defined(USE_STRSTREAM) - PRInt32 len = data.pcount(); - char* str = (char*)data.str(); -#endif -#if defined(USE_COUT) -#define data cout - data << "------------ End Copied Text" << endl; -#endif -#endif - -#if defined(WIN32) - PRUint32 cf_aol = RegisterClipboardFormat(gsAOLFormat); - PRUint32 cf_html = RegisterClipboardFormat(gsHTMLFormat); - - if (len) - { - OpenClipboard(NULL); - EmptyClipboard(); - - PlaceHTMLOnClipboard(cf_aol,str,len); - PlaceHTMLOnClipboard(cf_html,str,len); - PlaceHTMLOnClipboard(CF_TEXT,str,len); - - CloseClipboard(); - } - // in ostrstreams if you cal the str() function - // then you are responsible for deleting the string - if (str) delete str; -#endif - -#if defined(XP_MAC) - if (len) - { - char * ptr = NS_CONST_CAST(char*,str); - for (PRInt32 plen = len; plen > 0; plen --, ptr ++) - if (*ptr == '\n') - *ptr = '\r'; - - OSErr err = ::ZeroScrap(); - err = ::PutScrap(len, 'TEXT', str); - ::TEFromScrap(); - } -#endif + selectionMgr->CopyToClipboard(); + NS_RELEASE(selectionMgr); } -#endif /* USE_STRINGSTREAM || USE_STRSTREAM || USE_OSTREAM */ NS_RELEASE(parser); } } + NS_RELEASE(shell); } } diff --git a/mozilla/widget/public/nsIAppShell.h b/mozilla/widget/public/nsIAppShell.h index 9cfa0328261..772f4e9ff06 100644 --- a/mozilla/widget/public/nsIAppShell.h +++ b/mozilla/widget/public/nsIAppShell.h @@ -43,6 +43,7 @@ class nsDispatchListener { }; class nsIWidget; +class nsISelectionMgr; /** * Application shell used for Test applications @@ -96,6 +97,12 @@ public: virtual void* GetNativeData(PRUint32 aDataType) = 0; + /** + * Get the selection manager. + */ + + virtual nsresult GetSelectionMgr(nsISelectionMgr** aSelectionMgr) = 0; + }; diff --git a/mozilla/widget/public/nsISelectionMgr.h b/mozilla/widget/public/nsISelectionMgr.h index b0359b76f69..b330e257e15 100644 --- a/mozilla/widget/public/nsISelectionMgr.h +++ b/mozilla/widget/public/nsISelectionMgr.h @@ -26,7 +26,9 @@ { 0xa6cf90ea, 0x15b3, 0x11d2, \ { 0x93, 0x2e, 0x00, 0x80, 0x5f, 0x8a, 0xdd, 0x32 } } -class ostream; +// We'd like to declare this as a forwarded class, +// but for some reason the Mac won't accept that here. +#include /** * Selection Manager interface. @@ -38,7 +40,7 @@ class nsISelectionMgr : public nsISupports public: static const nsIID& IID() { static nsIID iid = NS_ISELECTIONMGR_IID; return iid; } - + NS_IMETHOD GetCopyOStream(ostream** aStream) = 0; NS_IMETHOD CopyToClipboard() = 0; diff --git a/mozilla/widget/src/gtk/nsAppShell.cpp b/mozilla/widget/src/gtk/nsAppShell.cpp index fef7bbf9a31..c8312b72ef5 100644 --- a/mozilla/widget/src/gtk/nsAppShell.cpp +++ b/mozilla/widget/src/gtk/nsAppShell.cpp @@ -21,6 +21,7 @@ #include "plevent.h" #include "nsIServiceManager.h" #include "nsIEventQueueService.h" +#include "nsSelectionMgr.h" #include "nsXPComCIID.h" #include @@ -37,6 +38,7 @@ nsAppShell::nsAppShell() { mRefCnt = 0; mDispatchListener = 0; + mSelectionMgr = 0; } //------------------------------------------------------------------------- @@ -46,6 +48,7 @@ nsAppShell::nsAppShell() //------------------------------------------------------------------------- nsAppShell::~nsAppShell() { + NS_IF_RELEASE(mSelectionMgr); } //------------------------------------------------------------------------- @@ -94,6 +97,10 @@ NS_METHOD nsAppShell::Create(int* argc, char ** argv) // gtk_rc_init(); + // Create the selection manager + if (!mSelectionMgr) + NS_NewSelectionMgr(&mSelectionMgr); + return NS_OK; } @@ -187,3 +194,14 @@ nsresult nsAppShell::DispatchNativeEvent(void * aEvent) { return NS_ERROR_FAILURE; } + +NS_METHOD +nsAppShell::GetSelectionMgr(nsISelectionMgr** aSelectionMgr) +{ + *aSelectionMgr = mSelectionMgr; + NS_IF_ADDREF(mSelectionMgr); + if (!mSelectionMgr) + return NS_ERROR_NOT_INITIALIZED; + return NS_OK; +} + diff --git a/mozilla/widget/src/gtk/nsAppShell.h b/mozilla/widget/src/gtk/nsAppShell.h index cd5653be3e1..998f20c6c2b 100644 --- a/mozilla/widget/src/gtk/nsAppShell.h +++ b/mozilla/widget/src/gtk/nsAppShell.h @@ -43,6 +43,7 @@ class nsAppShell : public nsIAppShell NS_IMETHOD Exit(); NS_IMETHOD SetDispatchListener(nsDispatchListener* aDispatchListener); virtual void* GetNativeData(PRUint32 aDataType); + NS_IMETHOD GetSelectionMgr(nsISelectionMgr** aSelectionMgr); // XXX temporary for Dialog investigation NS_IMETHOD GetNativeEvent(void *& aEvent, nsIWidget* aWidget, PRBool &aIsInWindow, PRBool &aIsMouseEvent); @@ -50,6 +51,7 @@ class nsAppShell : public nsIAppShell private: nsDispatchListener *mDispatchListener; + nsISelectionMgr *mSelectionMgr; protected: nsIEventQueueService * mEventQService; diff --git a/mozilla/widget/src/gtk/nsSelectionMgr.cpp b/mozilla/widget/src/gtk/nsSelectionMgr.cpp index 272c3f1679f..bd143d3c171 100644 --- a/mozilla/widget/src/gtk/nsSelectionMgr.cpp +++ b/mozilla/widget/src/gtk/nsSelectionMgr.cpp @@ -24,9 +24,6 @@ #include -static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); -static NS_DEFINE_IID(kISelectionMgrIID, NS_ISELECTIONMGR_IID); - NS_IMPL_ADDREF(nsSelectionMgr) NS_IMPL_RELEASE(nsSelectionMgr) @@ -41,14 +38,13 @@ nsresult nsSelectionMgr::QueryInterface(const nsIID& aIID, { return NS_ERROR_NULL_POINTER; } - if (aIID.Equals(kISupportsIID)) + if (aIID.Equals(nsISupports::IID())) { *aInstancePtrResult = (void*)(nsISupports*)this; NS_ADDREF_THIS(); return NS_OK; } if (aIID.Equals(nsISelectionMgr::IID())) - //if (aIID.Equals(kISelectionMgrIID)) { *aInstancePtrResult = (void*)(nsISelectionMgr*)this; NS_ADDREF_THIS(); diff --git a/mozilla/widget/src/gtk/nsSelectionMgr.h b/mozilla/widget/src/gtk/nsSelectionMgr.h index 777b00f887c..e2f9a92aa24 100644 --- a/mozilla/widget/src/gtk/nsSelectionMgr.h +++ b/mozilla/widget/src/gtk/nsSelectionMgr.h @@ -30,7 +30,7 @@ class ostrstream; * Owns the copied text, listens for selection request events. */ -class nsSelectionMgr : nsISelectionMgr +class nsSelectionMgr : public nsISelectionMgr { public: NS_DECL_ISUPPORTS diff --git a/mozilla/widget/src/gtk/nsWindow.cpp b/mozilla/widget/src/gtk/nsWindow.cpp index 9f7050fcc35..2aa90214b61 100644 --- a/mozilla/widget/src/gtk/nsWindow.cpp +++ b/mozilla/widget/src/gtk/nsWindow.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include "nsWindow.h" #include "nsIFontMetrics.h" @@ -34,6 +35,8 @@ #include "nsGtkEventHandler.h" #include "nsAppShell.h" +#include "nsSelectionMgr.h" + #include "stdio.h" //#define DBG 0 @@ -229,6 +232,7 @@ NS_METHOD nsWindow::CreateNative(GtkWidget *parentWidget) GTK_SIGNAL_FUNC(handle_delete_event), this); + nsSelectionMgr::SetTopLevelWidget(mShell); } // Force cursor to default setting diff --git a/mozilla/widget/src/mac/nsAppShell.cpp b/mozilla/widget/src/mac/nsAppShell.cpp index 753b081aaae..4b0ef4019be 100644 --- a/mozilla/widget/src/mac/nsAppShell.cpp +++ b/mozilla/widget/src/mac/nsAppShell.cpp @@ -30,6 +30,7 @@ #include "nsMacMessageSink.h" #include "nsMacMessagePump.h" +#include "nsSelectionMgr.h" #include "nsToolKit.h" #include #include @@ -69,6 +70,11 @@ NS_IMETHODIMP nsAppShell::SetDispatchListener(nsDispatchListener* aDispatchListe NS_IMETHODIMP nsAppShell::Create(int* argc, char ** argv) { mToolKit = auto_ptr( new nsToolkit() ); + + // Create the selection manager + if (!mSelectionMgr) + NS_NewSelectionMgr(&mSelectionMgr); + return NS_OK; } @@ -132,6 +138,7 @@ nsAppShell::nsAppShell() } mRefCnt = 0; mExitCalled = PR_FALSE; + mSelectionMgr = 0; } //------------------------------------------------------------------------- @@ -141,6 +148,7 @@ nsAppShell::nsAppShell() //------------------------------------------------------------------------- nsAppShell::~nsAppShell() { + NS_IF_RELEASE(mSelectionMgr); } //------------------------------------------------------------------------- @@ -171,3 +179,14 @@ nsresult nsAppShell::DispatchNativeEvent(void * aEvent) { return NS_ERROR_FAILURE; } + +NS_METHOD +nsAppShell::GetSelectionMgr(nsISelectionMgr** aSelectionMgr) +{ + *aSelectionMgr = mSelectionMgr; + NS_IF_ADDREF(mSelectionMgr); + if (!mSelectionMgr) + return NS_ERROR_NOT_INITIALIZED; + return NS_OK; +} + diff --git a/mozilla/widget/src/mac/nsAppShell.h b/mozilla/widget/src/mac/nsAppShell.h index e112d51b381..a21bd392360 100644 --- a/mozilla/widget/src/mac/nsAppShell.h +++ b/mozilla/widget/src/mac/nsAppShell.h @@ -41,6 +41,7 @@ class nsAppShell : public nsIAppShell { private: nsDispatchListener *mDispatchListener; // note: we don't own this, but it can be NULL + nsISelectionMgr *mSelectionMgr; auto_ptr mToolKit; auto_ptr mMacPump; nsMacMessageSink *mMacSink; //еее this will be COM, so use scc's COM_auto_ptr @@ -66,6 +67,8 @@ class nsAppShell : public nsIAppShell virtual void* GetNativeData(PRUint32 aDataType); + NS_IMETHOD GetSelectionMgr(nsISelectionMgr** aSelectionMgr); + // XXX temporary for Dialog investigation NS_IMETHOD GetNativeEvent(void *& aEvent, nsIWidget* aWidget, PRBool &aIsInWindow, PRBool &aIsMouseEvent); NS_IMETHOD DispatchNativeEvent(void * aEvent);