From 93cc772d77b72606d85585032937e539a5dad75e Mon Sep 17 00:00:00 2001 From: "akkana%netscape.com" Date: Fri, 7 May 1999 19:22:38 +0000 Subject: [PATCH] Middle-mouse paste; also, fix a bunch of warnings git-svn-id: svn://10.0.0.236/trunk@30696 18797224-902f-48f8-a5cc-f745e15eee43 --- .../editor/base/nsEditorEventListeners.cpp | 69 ++++++++++++++----- mozilla/editor/base/nsTextEditor.cpp | 7 +- .../libeditor/text/nsEditorEventListeners.cpp | 69 ++++++++++++++----- 3 files changed, 107 insertions(+), 38 deletions(-) diff --git a/mozilla/editor/base/nsEditorEventListeners.cpp b/mozilla/editor/base/nsEditorEventListeners.cpp index dd16c10e433..9b73ec13e30 100644 --- a/mozilla/editor/base/nsEditorEventListeners.cpp +++ b/mozilla/editor/base/nsEditorEventListeners.cpp @@ -22,6 +22,7 @@ #include "nsIDOMDocument.h" #include "nsIDOMElement.h" +#include "nsIDOMSelection.h" #include "nsIDOMCharacterData.h" #include "nsIEditProperty.h" #include "nsISupportsArray.h" @@ -370,10 +371,11 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr { printf("Trying to insert HTML\n"); aProcessed=PR_TRUE; - if (mEditor) + nsCOMPtr htmlEditor (do_QueryInterface(mEditor)); + if (htmlEditor) { nsString nsstr ("This is bold and emphasized"); - nsresult res = mEditor->Insert(nsstr); + nsresult res = htmlEditor->Insert(nsstr); if (!NS_SUCCEEDED(res)) printf("nsTextEditor::Insert(string) failed\n"); } @@ -485,9 +487,9 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr { // XXX: move this logic down into texteditor rules delegate // should just call mEditor->ChangeTextProperty(prop) - PRBool any = PR_FALSE; - PRBool all = PR_FALSE; - PRBool first = PR_FALSE; + //PRBool any = PR_FALSE; + //PRBool all = PR_FALSE; + //PRBool first = PR_FALSE; nsAutoString prop = "SIZE"; nsAutoString value = "+2"; mEditor->SetTextProperty(nsIEditProperty::font, &prop, &value); @@ -504,9 +506,9 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr { // XXX: move this logic down into texteditor rules delegate // should just call mEditor->ChangeTextProperty(prop) - PRBool any = PR_FALSE; - PRBool all = PR_FALSE; - PRBool first = PR_FALSE; + //PRBool any = PR_FALSE; + //PRBool all = PR_FALSE; + //PRBool first = PR_FALSE; nsAutoString prop = "SIZE"; nsAutoString value = "-2"; mEditor->SetTextProperty(nsIEditProperty::font, &prop, &value); @@ -523,9 +525,9 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr { // XXX: move this logic down into texteditor rules delegate // should just call mEditor->ChangeTextProperty(prop) - PRBool any = PR_FALSE; - PRBool all = PR_FALSE; - PRBool first = PR_FALSE; + //PRBool any = PR_FALSE; + //PRBool all = PR_FALSE; + //PRBool first = PR_FALSE; nsAutoString prop = "FACE"; nsAutoString value = "helvetica"; mEditor->SetTextProperty(nsIEditProperty::font, &prop, &value); @@ -542,9 +544,9 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr { // XXX: move this logic down into texteditor rules delegate // should just call mEditor->ChangeTextProperty(prop) - PRBool any = PR_FALSE; - PRBool all = PR_FALSE; - PRBool first = PR_FALSE; + //PRBool any = PR_FALSE; + //PRBool all = PR_FALSE; + //PRBool first = PR_FALSE; nsAutoString prop = "FACE"; nsAutoString value = "times"; mEditor->SetTextProperty(nsIEditProperty::font, &prop, &value); @@ -766,12 +768,43 @@ nsTextEditorMouseListener::HandleEvent(nsIDOMEvent* aEvent) nsresult nsTextEditorMouseListener::MouseDown(nsIDOMEvent* aMouseEvent) { + if (!aMouseEvent) + return NS_OK; // NS_OK means "we didn't process the event". Go figure. + + // We only do anything special for middle-mouse click (paste); + // ignore all other events. + PRUint32 button = 0; + aMouseEvent->GetButton(&button); + if (button != 2) + return NS_OK; + + nsCOMPtr editor (do_QueryInterface(mEditor)); + if (!editor) + return NS_ERROR_FAILURE; + + // There's currently no way to set the selection to the mouse position + // outside of liblayout -- nsIDOMEvent has an API to get the node under + // the mouse, but not to get the offset within the node. +#ifdef PASTE_TO_MOUSE_POSITION nsCOMPtr target; - if (NS_OK == aMouseEvent->GetTarget(getter_AddRefs(target))) { -// nsSetCurrentNode(aTarget); + if (NS_SUCCEEDED(aMouseEvent->GetTarget(getter_AddRefs(target)))) + { + nsCOMPtr selection; + if (NS_SUCCEEDED(editor->GetSelection(getter_AddRefs(selection)))) + { + if (NS_SUCCEEDED(selection->Collapse(target, 0))) + { + editor->Paste(); + } + } } - //Should not be error. Need a new way to do return values - return NS_ERROR_BASE; +#else /* PASTE_TO_MOUSE_POSITION */ + // Until we can get node AND offset, + // it's better to paste to the current selection position: + editor->Paste(); +#endif /* PASTE_TO_MOUSE_POSITION */ + + return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". } diff --git a/mozilla/editor/base/nsTextEditor.cpp b/mozilla/editor/base/nsTextEditor.cpp index 16c9ae6f04a..8c3f323b15a 100644 --- a/mozilla/editor/base/nsTextEditor.cpp +++ b/mozilla/editor/base/nsTextEditor.cpp @@ -258,6 +258,9 @@ NS_IMETHODIMP nsTextEditor::Init(nsIDOMDocument *aDoc, nsIPresShell *aPresShell) } result = NS_NewEditorMouseListener(getter_AddRefs(mMouseListenerP), this); if (NS_OK != result) { +#ifdef DEBUG_akkana + printf("Couldn't get mouse listener\n"); +#endif // drop the key listener if we couldn't get a mouse listener. mKeyListenerP = do_QueryInterface(0); return result; @@ -308,8 +311,8 @@ NS_IMETHODIMP nsTextEditor::Init(nsIDOMDocument *aDoc, nsIPresShell *aPresShell) } //cmanske: Shouldn't we check result from this? erP->AddEventListenerByIID(mKeyListenerP, kIDOMKeyListenerIID); - //erP->AddEventListener(mDragListenerP, kIDOMDragListenerIID); - //erP->AddEventListener(mMouseListenerP, kIDOMMouseListenerIID); + //erP->AddEventListenerByIID(mDragListenerP, kIDOMDragListenerIID); + erP->AddEventListenerByIID(mMouseListenerP, kIDOMMouseListenerIID); erP->AddEventListenerByIID(mTextListenerP,kIDOMTextListenerIID); erP->AddEventListenerByIID(mCompositionListenerP,kIDOMCompositionListenerIID); diff --git a/mozilla/editor/libeditor/text/nsEditorEventListeners.cpp b/mozilla/editor/libeditor/text/nsEditorEventListeners.cpp index dd16c10e433..9b73ec13e30 100644 --- a/mozilla/editor/libeditor/text/nsEditorEventListeners.cpp +++ b/mozilla/editor/libeditor/text/nsEditorEventListeners.cpp @@ -22,6 +22,7 @@ #include "nsIDOMDocument.h" #include "nsIDOMElement.h" +#include "nsIDOMSelection.h" #include "nsIDOMCharacterData.h" #include "nsIEditProperty.h" #include "nsISupportsArray.h" @@ -370,10 +371,11 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr { printf("Trying to insert HTML\n"); aProcessed=PR_TRUE; - if (mEditor) + nsCOMPtr htmlEditor (do_QueryInterface(mEditor)); + if (htmlEditor) { nsString nsstr ("This is bold and emphasized"); - nsresult res = mEditor->Insert(nsstr); + nsresult res = htmlEditor->Insert(nsstr); if (!NS_SUCCEEDED(res)) printf("nsTextEditor::Insert(string) failed\n"); } @@ -485,9 +487,9 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr { // XXX: move this logic down into texteditor rules delegate // should just call mEditor->ChangeTextProperty(prop) - PRBool any = PR_FALSE; - PRBool all = PR_FALSE; - PRBool first = PR_FALSE; + //PRBool any = PR_FALSE; + //PRBool all = PR_FALSE; + //PRBool first = PR_FALSE; nsAutoString prop = "SIZE"; nsAutoString value = "+2"; mEditor->SetTextProperty(nsIEditProperty::font, &prop, &value); @@ -504,9 +506,9 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr { // XXX: move this logic down into texteditor rules delegate // should just call mEditor->ChangeTextProperty(prop) - PRBool any = PR_FALSE; - PRBool all = PR_FALSE; - PRBool first = PR_FALSE; + //PRBool any = PR_FALSE; + //PRBool all = PR_FALSE; + //PRBool first = PR_FALSE; nsAutoString prop = "SIZE"; nsAutoString value = "-2"; mEditor->SetTextProperty(nsIEditProperty::font, &prop, &value); @@ -523,9 +525,9 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr { // XXX: move this logic down into texteditor rules delegate // should just call mEditor->ChangeTextProperty(prop) - PRBool any = PR_FALSE; - PRBool all = PR_FALSE; - PRBool first = PR_FALSE; + //PRBool any = PR_FALSE; + //PRBool all = PR_FALSE; + //PRBool first = PR_FALSE; nsAutoString prop = "FACE"; nsAutoString value = "helvetica"; mEditor->SetTextProperty(nsIEditProperty::font, &prop, &value); @@ -542,9 +544,9 @@ nsTextEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aPr { // XXX: move this logic down into texteditor rules delegate // should just call mEditor->ChangeTextProperty(prop) - PRBool any = PR_FALSE; - PRBool all = PR_FALSE; - PRBool first = PR_FALSE; + //PRBool any = PR_FALSE; + //PRBool all = PR_FALSE; + //PRBool first = PR_FALSE; nsAutoString prop = "FACE"; nsAutoString value = "times"; mEditor->SetTextProperty(nsIEditProperty::font, &prop, &value); @@ -766,12 +768,43 @@ nsTextEditorMouseListener::HandleEvent(nsIDOMEvent* aEvent) nsresult nsTextEditorMouseListener::MouseDown(nsIDOMEvent* aMouseEvent) { + if (!aMouseEvent) + return NS_OK; // NS_OK means "we didn't process the event". Go figure. + + // We only do anything special for middle-mouse click (paste); + // ignore all other events. + PRUint32 button = 0; + aMouseEvent->GetButton(&button); + if (button != 2) + return NS_OK; + + nsCOMPtr editor (do_QueryInterface(mEditor)); + if (!editor) + return NS_ERROR_FAILURE; + + // There's currently no way to set the selection to the mouse position + // outside of liblayout -- nsIDOMEvent has an API to get the node under + // the mouse, but not to get the offset within the node. +#ifdef PASTE_TO_MOUSE_POSITION nsCOMPtr target; - if (NS_OK == aMouseEvent->GetTarget(getter_AddRefs(target))) { -// nsSetCurrentNode(aTarget); + if (NS_SUCCEEDED(aMouseEvent->GetTarget(getter_AddRefs(target)))) + { + nsCOMPtr selection; + if (NS_SUCCEEDED(editor->GetSelection(getter_AddRefs(selection)))) + { + if (NS_SUCCEEDED(selection->Collapse(target, 0))) + { + editor->Paste(); + } + } } - //Should not be error. Need a new way to do return values - return NS_ERROR_BASE; +#else /* PASTE_TO_MOUSE_POSITION */ + // Until we can get node AND offset, + // it's better to paste to the current selection position: + editor->Paste(); +#endif /* PASTE_TO_MOUSE_POSITION */ + + return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event". }