Bug 695032 - [GTK/X11] selecting text in scratchpad doesn't place it on the X primary selection; r=rcampbell
authorMihai Sucan <mihai.sucan@gmail.com>
Mon, 12 Dec 2011 20:32:35 +0200
changeset 82555 5bd81a413dea2cf855d41a946bf5c7ca70f85f20
parent 82554 7cf966da2e9321b45a70a2da094cc2fa47b9a461
child 82556 8e9120a2a1213ec1f53c2f123df7935efd59ea0a
push id21661
push userrcampbell@mozilla.com
push dateWed, 14 Dec 2011 14:29:15 +0000
treeherdermozilla-central@80bb6b209ea7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrcampbell
bugs695032
milestone11.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 695032 - [GTK/X11] selecting text in scratchpad doesn't place it on the X primary selection; r=rcampbell
browser/devtools/sourceeditor/source-editor-orion.jsm
browser/devtools/sourceeditor/test/browser_bug695035_middle_click_paste.js
--- a/browser/devtools/sourceeditor/source-editor-orion.jsm
+++ b/browser/devtools/sourceeditor/source-editor-orion.jsm
@@ -37,16 +37,21 @@
  * ***** END LICENSE BLOCK *****/
 
 "use strict";
 
 const Cu = Components.utils;
 const Ci = Components.interfaces;
 
 Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+XPCOMUtils.defineLazyServiceGetter(this, "clipboardHelper",
+                                   "@mozilla.org/widget/clipboardhelper;1",
+                                   "nsIClipboardHelper");
 
 const ORION_SCRIPT = "chrome://browser/content/orion.js";
 const ORION_IFRAME = "data:text/html;charset=utf8,<!DOCTYPE html>" +
   "<html style='height:100%' dir='ltr'>" +
   "<body style='height:100%;margin:0;overflow:hidden'>" +
   "<div id='editor' style='height:100%'></div>" +
   "</body></html>";
 
@@ -91,16 +96,18 @@ var EXPORTED_SYMBOLS = ["SourceEditor"];
  */
 function SourceEditor() {
   // Update the SourceEditor defaults from user preferences.
 
   SourceEditor.DEFAULTS.TAB_SIZE =
     Services.prefs.getIntPref(SourceEditor.PREFS.TAB_SIZE);
   SourceEditor.DEFAULTS.EXPAND_TAB =
     Services.prefs.getBoolPref(SourceEditor.PREFS.EXPAND_TAB);
+
+  this._onOrionSelection = this._onOrionSelection.bind(this);
 }
 
 SourceEditor.prototype = {
   _view: null,
   _iframe: null,
   _model: null,
   _undoStack: null,
   _linesRuler: null,
@@ -210,16 +217,19 @@ SourceEditor.prototype = {
     });
 
     let onOrionLoad = function() {
       this._view.removeEventListener("Load", onOrionLoad);
       this._onOrionLoad();
     }.bind(this);
 
     this._view.addEventListener("Load", onOrionLoad);
+    if (Services.appinfo.OS == "Linux") {
+      this._view.addEventListener("Selection", this._onOrionSelection);
+    }
 
     let KeyBinding = window.require("orion/textview/keyBinding").KeyBinding;
     let TextDND = window.require("orion/textview/textDND").TextDND;
     let LineNumberRuler = window.require("orion/textview/rulers").LineNumberRuler;
     let UndoStack = window.require("orion/textview/undoStack").UndoStack;
     let AnnotationModel = window.require("orion/textview/annotations").AnnotationModel;
 
     this._annotationModel = new AnnotationModel(this._model);
@@ -407,16 +417,35 @@ SourceEditor.prototype = {
     }
 
     this.setText(this.getLineDelimiter() + prefix, selection.start,
                  selection.end);
     return true;
   },
 
   /**
+   * Orion Selection event handler for the X Window System users. This allows
+   * one to select text and have it copied into the X11 PRIMARY.
+   *
+   * @private
+   * @param object aEvent
+   *        The Orion Selection event object.
+   */
+  _onOrionSelection: function SE__onOrionSelection(aEvent)
+  {
+    let text = this.getText(aEvent.newValue.start, aEvent.newValue.end);
+    if (!text) {
+      return;
+    }
+
+    clipboardHelper.copyStringToClipboard(text,
+                                          Ci.nsIClipboard.kSelectionClipboard);
+  },
+
+  /**
    * Get the editor element.
    *
    * @return nsIDOMElement
    *         In this implementation a xul:iframe holds the editor.
    */
   get editorElement() {
     return this._iframe;
   },
@@ -812,16 +841,21 @@ SourceEditor.prototype = {
     return this._view.getOptions("readonly");
   },
 
   /**
    * Destroy/uninitialize the editor.
    */
   destroy: function SE_destroy()
   {
+    if (Services.appinfo.OS == "Linux") {
+      this._view.removeEventListener("Selection", this._onOrionSelection);
+    }
+    this._onOrionSelection = null;
+
     this._view.destroy();
     this.parentElement.removeChild(this._iframe);
     this.parentElement = null;
     this._iframeWindow = null;
     this._iframe = null;
     this._undoStack = null;
     this._styler = null;
     this._linesRuler = null;
--- a/browser/devtools/sourceeditor/test/browser_bug695035_middle_click_paste.js
+++ b/browser/devtools/sourceeditor/test/browser_bug695035_middle_click_paste.js
@@ -53,26 +53,39 @@ function editorLoaded()
                           getService(Ci.nsIClipboardHelper);
     clipboardHelper.copyStringToClipboard(expectedString,
                                           Ci.nsIClipboard.kSelectionClipboard);
   };
 
   let onCopy = function() {
     editor.addEventListener(SourceEditor.EVENTS.TEXT_CHANGED, onPaste);
 
-    EventUtils.synthesizeMouse(editor.editorElement, 2, 2, {}, testWin);
-    EventUtils.synthesizeMouse(editor.editorElement, 3, 3, {button: 1}, testWin);
+    EventUtils.synthesizeMouse(editor.editorElement, 10, 10, {}, testWin);
+    EventUtils.synthesizeMouse(editor.editorElement, 11, 11, {button: 1}, testWin);
   };
 
   let onPaste = function() {
     editor.removeEventListener(SourceEditor.EVENTS.TEXT_CHANGED, onPaste);
 
-    is(editor.getText(), expectedString + initialText, "middle-click paste works");
+    let text = editor.getText();
+    isnot(text.indexOf(expectedString), -1, "middle-click paste works");
+    isnot(text, initialText, "middle-click paste works (confirmed)");
+
+    executeSoon(doTestBug695032);
+  };
 
-    executeSoon(testEnd);
+  let doTestBug695032 = function() {
+    info("test for bug 695032 - editor selection should be placed in the X11 primary selection buffer");
+
+    let text = "foobarBug695032 test me, test me!";
+    editor.setText(text);
+
+    waitForSelection(text, function() {
+      EventUtils.synthesizeKey("a", {accelKey: true}, testWin);
+    }, testEnd, testEnd);
   };
 
   waitForSelection(expectedString, doCopy, onCopy, testEnd);
 }
 
 function testEnd()
 {
   editor.destroy();