diff options
author | Markus Keller | 2015-11-12 19:10:27 +0000 |
---|---|---|
committer | Markus Keller | 2015-11-18 18:42:05 +0000 |
commit | a56140d40f573ac7967c3e16425dc7a30d49b546 (patch) | |
tree | ee8ec92328a2c2c5dee503e731037c94497d9434 | |
parent | 6f1ed70f6ffa1db02ab73e9bc2c1c071f2c549ac (diff) | |
download | eclipse.platform.text-a56140d40f573ac7967c3e16425dc7a30d49b546.tar.gz eclipse.platform.text-a56140d40f573ac7967c3e16425dc7a30d49b546.tar.xz eclipse.platform.text-a56140d40f573ac7967c3e16425dc7a30d49b546.zip |
Bug 396949: Pinch to change font size in editor
Change-Id: I7b996bef9f4e9425220563ecac9cd86d3243fd28
4 files changed, 75 insertions, 49 deletions
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/CompositeRuler.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/CompositeRuler.java index 656cff99844..25785012f6a 100644 --- a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/CompositeRuler.java +++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/CompositeRuler.java @@ -24,6 +24,7 @@ import org.eclipse.swt.events.ControlListener; import org.eclipse.swt.events.DisposeEvent; import org.eclipse.swt.events.DisposeListener; import org.eclipse.swt.events.FocusListener; +import org.eclipse.swt.events.GestureListener; import org.eclipse.swt.events.HelpListener; import org.eclipse.swt.events.KeyListener; import org.eclipse.swt.events.MouseListener; @@ -215,6 +216,10 @@ public class CompositeRuler implements IVerticalRuler, IVerticalRulerExtension, control. addDisposeListener((DisposeListener) listener); return; } + if (GestureListener.class.equals(clazz)) { + control. addGestureListener((GestureListener) listener); + return; + } } /** @@ -430,6 +435,14 @@ public class CompositeRuler implements IVerticalRuler, IVerticalRulerExtension, } /* + * @see Control#removeGestureListener(GestureListener) + */ + public void removeGestureListener(GestureListener listener) { + removeListener(GestureListener.class, listener); + super.removeGestureListener(listener); + } + + /* * @seeControl#addControlListener(ControlListener) */ public void addControlListener(ControlListener listener) { @@ -508,6 +521,14 @@ public class CompositeRuler implements IVerticalRuler, IVerticalRulerExtension, super.addDisposeListener(listener); addListener(DisposeListener.class, listener); } + + /* + * @see Control#addGestureListener(GestureListener) + */ + public void addGestureListener(GestureListener listener) { + super.addGestureListener(listener); + addListener(GestureListener.class, listener); + } } /** The ruler's viewer */ diff --git a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java index 0ce3ba56c8d..16d640a3070 100644 --- a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java +++ b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java @@ -44,6 +44,8 @@ import org.eclipse.swt.dnd.DropTargetEvent; import org.eclipse.swt.dnd.DropTargetListener; import org.eclipse.swt.dnd.TextTransfer; import org.eclipse.swt.dnd.Transfer; +import org.eclipse.swt.events.GestureEvent; +import org.eclipse.swt.events.GestureListener; import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.events.KeyListener; import org.eclipse.swt.events.MouseEvent; @@ -3461,54 +3463,6 @@ public abstract class AbstractTextEditor extends EditorPart implements ITextEdit initializeDragAndDrop(fSourceViewer); StyledText styledText= fSourceViewer.getTextWidget(); - - /* gestures commented out until proper solution (i.e. preference page) can be found - * for bug # 28417: - * - final Map gestureMap= new HashMap(); - - gestureMap.put("E", IWorkbenchCommandConstants.NAVIGATE_FORWARDHISTORY); //$NON-NLS-1$ - gestureMap.put("N", IWorkbenchCommandConstants.FILE_SAVE); //$NON-NLS-1$ - gestureMap.put("NW", IWorkbenchCommandConstants.FILE_SAVEALL); //$NON-NLS-1$ - gestureMap.put("S", IWorkbenchCommandConstants.FILE_CLOSE); //$NON-NLS-1$ - gestureMap.put("SW", IWorkbenchCommandConstants.FILE_CLOSEALL); //$NON-NLS-1$ - gestureMap.put("W", IWorkbenchCommandConstants.NAVIGATE_BACKWARDHISTORY); //$NON-NLS-1$ - gestureMap.put("EN", IWorkbenchCommandConstants.EDIT_COPY); //$NON-NLS-1$ - gestureMap.put("ES", IWorkbenchCommandConstants.EDIT_PASTE); //$NON-NLS-1$ - gestureMap.put("EW", IWorkbenchCommandConstants.EDIT_CUT); //$NON-NLS-1$ - - Capture capture= Capture.create(); - capture.setControl(styledText); - - capture.addCaptureListener(new CaptureListener() { - public void gesture(Gesture gesture) { - if (gesture.getPen() == 3) { - String actionId= (String) gestureMap.get(Util.recognize(gesture.getPoints(), 20)); - - if (actionId != null) { - IKeyBindingService keyBindingService= getEditorSite().getKeyBindingService(); - - if (keyBindingService instanceof KeyBindingService) { - IAction action= ((KeyBindingService) keyBindingService).getAction(actionId); - - if (action != null) { - if (action instanceof IUpdate) - ((IUpdate) action).update(); - - if (action.isEnabled()) - action.run(); - } - } - - return; - } - - fTextContextMenu.setVisible(true); - } - }; - }); - */ - styledText.addMouseListener(getCursorListener()); styledText.addKeyListener(getCursorListener()); @@ -3530,7 +3484,6 @@ public abstract class AbstractTextEditor extends EditorPart implements ITextEdit manager.addMenuListener(getContextMenuListener()); fTextContextMenu= manager.createContextMenu(styledText); - // comment this line if using gestures, above. styledText.setMenu(fTextContextMenu); if (fEditorContextMenuId != null) @@ -3575,6 +3528,8 @@ public abstract class AbstractTextEditor extends EditorPart implements ITextEdit if (fRulerContextMenuId == null) fRulerContextMenuId= DEFAULT_RULER_CONTEXT_MENU_ID; + + initializeZoomGestures(rulerControl, fSourceViewer); getSite().setSelectionProvider(getSelectionProvider()); @@ -3936,6 +3891,52 @@ public abstract class AbstractTextEditor extends EditorPart implements ITextEdit } } + private void initializeZoomGestures(Control rulerControl, final ISourceViewer sourceViewer) { + final StyledText styledText= sourceViewer.getTextWidget(); + GestureListener gestureListener= new GestureListener() { + private Font fMagnificationStartFont; + private int fLastHeight= -1; + + public void gesture(GestureEvent e) { + if (e.detail == SWT.GESTURE_BEGIN) { + fMagnificationStartFont= styledText.getFont(); + } else if (e.detail == SWT.GESTURE_END) { + fMagnificationStartFont= null; + updateStatusField(ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION); + } else if (e.detail == SWT.GESTURE_ROTATE) { + if (Math.abs(e.rotation) > 45) { + fMagnificationStartFont= null; // don't observe magnify events after reset + initializeViewerFont(fSourceViewer); + updateCaret(); + IStatusField statusField= getStatusField(ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION); + if (statusField != null) { + int newHeight= styledText.getFont().getFontData()[0].getHeight(); + statusField.setText(NLSUtility.format(EditorMessages.Editor_font_reset_message, new Integer(newHeight))); + } + } + } else if (e.detail == SWT.GESTURE_MAGNIFY && fMagnificationStartFont != null) { + FontData fontData= fMagnificationStartFont.getFontData()[0]; + int startHeight= fontData.getHeight(); + int newHeight= Math.max(1, (int) (startHeight * e.magnification)); + if (newHeight != fLastHeight) { + fLastHeight= newHeight; + fontData.setHeight(newHeight); + Font newFont= new Font(fMagnificationStartFont.getDevice(), fontData); + setFont(sourceViewer, newFont); + disposeFont(); + updateCaret(); + IStatusField statusField= getStatusField(ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION); + if (statusField != null) { + statusField.setText(NLSUtility.format(EditorMessages.Editor_font_zoom_message, new Object[] { new Integer(startHeight), new Integer(newHeight) })); + } + } + } + } + }; + styledText.addGestureListener(gestureListener); + rulerControl.addGestureListener(gestureListener); + } + /** * Creates a color from the information stored in the given preference store. * Returns <code>null</code> if there is no such information available. diff --git a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/EditorMessages.java b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/EditorMessages.java index faac4e9075b..7d387101452 100644 --- a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/EditorMessages.java +++ b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/EditorMessages.java @@ -120,6 +120,8 @@ final class EditorMessages extends NLS { public static String FindReplaceDialog_read_only; public static String Editor_MoveLines_IllegalMove_status; public static String Editor_error_clipboard_copy_failed_message; + public static String Editor_font_reset_message; + public static String Editor_font_zoom_message; static { NLS.initializeMessages(BUNDLE_NAME, EditorMessages.class); diff --git a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/EditorMessages.properties b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/EditorMessages.properties index 205ba0049a3..7d5eeee32f0 100644 --- a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/EditorMessages.properties +++ b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/EditorMessages.properties @@ -119,3 +119,5 @@ FindReplaceDialog_read_only=Cannot replace. File is read-only. Editor_MoveLines_IllegalMove_status= Move not possible - Uncheck "Show Source of Selected Element Only" to see the entire document Editor_error_clipboard_copy_failed_message= Copy to clipboard failed. +Editor_font_reset_message=reset \u2192 {0} +Editor_font_zoom_message={0} \u2192 {1} |