001: /*
002: * MouseHandler.java - jEdit's text component
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1999, 2005 Slava Pestov
007: * Portions copyright (C) 2000 Ollie Rutherfurd
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
022: */
023:
024: package org.gjt.sp.jedit.textarea;
025:
026: //{{{ Imports
027: import java.awt.event.*;
028:
029: import org.gjt.sp.jedit.Registers;
030: import org.gjt.sp.jedit.OperatingSystem;
031:
032: //}}}
033:
034: /**
035: * The mouseHandler used for jEdit.
036: */
037: public class MouseHandler extends TextAreaMouseHandler {
038: //{{{ MouseHandler constructor
039: public MouseHandler(JEditTextArea textArea) {
040: super (textArea);
041: this .textArea = textArea;
042: } //}}}
043:
044: //{{{ mousePressed() method
045: public void mousePressed(MouseEvent evt) {
046: showCursor();
047:
048: control = (OperatingSystem.isMacOS() && evt.isMetaDown())
049: || (!OperatingSystem.isMacOS() && evt.isControlDown());
050:
051: ctrlForRectangularSelection = textArea
052: .isCtrlForRectangularSelection();
053:
054: // so that Home <mouse click> Home is not the same
055: // as pressing Home twice in a row
056: textArea.getInputHandler().resetLastActionCount();
057:
058: quickCopyDrag = (textArea.isQuickCopyEnabled() && isMiddleButton(evt
059: .getModifiers()));
060:
061: if (!quickCopyDrag) {
062: textArea.requestFocus();
063: TextArea.focusedComponent = textArea;
064: }
065:
066: if (textArea.getBuffer().isLoading())
067: return;
068:
069: int x = evt.getX();
070: int y = evt.getY();
071:
072: dragStart = textArea
073: .xyToOffset(x, y, !(textArea.getPainter()
074: .isBlockCaretEnabled() || textArea
075: .isOverwriteEnabled()));
076: dragStartLine = textArea.getLineOfOffset(dragStart);
077: dragStartOffset = dragStart
078: - textArea.getLineStartOffset(dragStartLine);
079:
080: if (isPopupTrigger(evt)
081: && textArea.getRightClickPopup() != null) {
082: if (textArea.isRightClickPopupEnabled())
083: textArea.handlePopupTrigger(evt);
084: return;
085: }
086:
087: dragged = false;
088:
089: textArea.blink = true;
090: textArea.invalidateLine(textArea.getCaretLine());
091:
092: clickCount = evt.getClickCount();
093:
094: if (textArea.isDragEnabled()
095: && textArea.selectionManager.insideSelection(x, y)
096: && clickCount == 1 && !evt.isShiftDown()) {
097: maybeDragAndDrop = true;
098: textArea.moveCaretPosition(dragStart, false);
099: return;
100: }
101:
102: maybeDragAndDrop = false;
103:
104: if (quickCopyDrag) {
105: // ignore double clicks of middle button
106: doSingleClick(evt);
107: } else {
108: switch (clickCount) {
109: case 1:
110: doSingleClick(evt);
111: break;
112: case 2:
113: doDoubleClick();
114: break;
115: default: //case 3:
116: doTripleClick();
117: break;
118: }
119: }
120: } //}}}
121:
122: //{{{ mouseReleased() method
123: public void mouseReleased(MouseEvent evt) {
124: // middle mouse button drag inserts selection
125: // at caret position
126: Selection sel = textArea.getSelectionAtOffset(dragStart);
127: if (dragged && sel != null) {
128: Registers.setRegister('%', textArea.getSelectedText(sel));
129: if (quickCopyDrag) {
130: textArea.removeFromSelection(sel);
131: Registers.paste(TextArea.focusedComponent, '%',
132: sel instanceof Selection.Rect);
133:
134: TextArea.focusedComponent.requestFocus();
135: }
136: } else if (!dragged && textArea.isQuickCopyEnabled()
137: && isMiddleButton(evt.getModifiers())) {
138: textArea.requestFocus();
139: TextArea.focusedComponent = textArea;
140:
141: textArea.setCaretPosition(dragStart, false);
142: if (!textArea.isEditable())
143: textArea.getToolkit().beep();
144: else
145: Registers.paste(textArea, '%', control);
146: } else if (maybeDragAndDrop
147: && !textArea.isMultipleSelectionEnabled()) {
148: textArea.selectNone();
149: }
150:
151: dragged = false;
152: } //}}}
153:
154: //{{{ Private members
155: private JEditTextArea textArea;
156: //}}}
157: }
|