1 /**********************************************************************
2 * Copyright (c) 2005, 2013 IBM Corporation, Ericsson
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM - Initial API and implementation
10 * Bernd Hufmann - Updated for TMF
11 **********************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
;
15 import java
.util
.Timer
;
16 import java
.util
.TimerTask
;
18 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.util
.Messages
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.events
.ControlListener
;
21 import org
.eclipse
.swt
.events
.FocusEvent
;
22 import org
.eclipse
.swt
.events
.FocusListener
;
23 import org
.eclipse
.swt
.events
.KeyEvent
;
24 import org
.eclipse
.swt
.events
.KeyListener
;
25 import org
.eclipse
.swt
.events
.MouseEvent
;
26 import org
.eclipse
.swt
.events
.MouseListener
;
27 import org
.eclipse
.swt
.events
.MouseMoveListener
;
28 import org
.eclipse
.swt
.events
.MouseTrackListener
;
29 import org
.eclipse
.swt
.events
.PaintEvent
;
30 import org
.eclipse
.swt
.events
.PaintListener
;
31 import org
.eclipse
.swt
.events
.SelectionEvent
;
32 import org
.eclipse
.swt
.events
.SelectionListener
;
33 import org
.eclipse
.swt
.events
.TypedEvent
;
34 import org
.eclipse
.swt
.graphics
.Color
;
35 import org
.eclipse
.swt
.graphics
.Cursor
;
36 import org
.eclipse
.swt
.graphics
.GC
;
37 import org
.eclipse
.swt
.graphics
.ImageData
;
38 import org
.eclipse
.swt
.graphics
.PaletteData
;
39 import org
.eclipse
.swt
.graphics
.Point
;
40 import org
.eclipse
.swt
.graphics
.RGB
;
41 import org
.eclipse
.swt
.graphics
.Rectangle
;
42 import org
.eclipse
.swt
.widgets
.Button
;
43 import org
.eclipse
.swt
.widgets
.Canvas
;
44 import org
.eclipse
.swt
.widgets
.Composite
;
45 import org
.eclipse
.swt
.widgets
.Control
;
46 import org
.eclipse
.swt
.widgets
.Display
;
47 import org
.eclipse
.swt
.widgets
.Layout
;
48 import org
.eclipse
.swt
.widgets
.ScrollBar
;
49 import org
.eclipse
.swt
.widgets
.Scrollable
;
50 import org
.eclipse
.swt
.widgets
.Shell
;
53 * ScrollView widget provides a scrolling area with on-demand scroll bars.
54 * Overview scrollable panel can be used (@see setOverviewEnabled()).
56 * @author Eric Miravete
59 public class ScrollView
extends Composite
{
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
66 * Scroll bar mode AUTO
68 public static final int AUTO
= 0;
70 * Scroll bar mode ALWAYS_ON
72 public static final int ALWAYS_ON
= 1;
74 * Scroll bar mode ALWAYS_OFF
76 public static final int ALWAYS_OFF
= 2;
78 * Bit mask for visible vertical scroll bar
80 public static final int VBAR
= 0x01;
82 * Bit mask for visible horizontal scroll bar
84 public static final int HBAR
= 0x02;
86 * Value of the contents height property.
88 private int fContentsHeight
= 0;
90 * Value of the contents width property.
92 private int fContentsWidth
= 0;
94 * Value of the contents x coordinate property
96 private int fContentsX
= 0;
98 * Value of the contents y coordinate property
100 private int fContentsY
= 0;
102 * Scroll bar mode of horizontal scroll bar.
104 private int fHorScrollbarMode
= AUTO
;
106 * Scroll bar mode of vertical scroll bar.
108 private int fVertScrollbarMode
= AUTO
;
110 * Increment for the horizontal scroll bar.
112 private int fHorScrollbarIncrement
= 10;
114 * Increment for the vertical scroll bar.
116 private int fVertScrollbarIncrement
= 10;
118 * Flag whether auto scroll is enabled or not.
120 private boolean fAutoScrollEnabled
= true;
122 * Value of the auto scroll period.
124 private int fAutoScrollPeriod
= 75;
126 * The local paint listener reference.
128 private PaintListener fLocalPaintListener
= null;
130 * The local mouse move listener reference.
132 private MouseMoveListener fLocalMouseMoveListener
= null;
134 * The local mouse listener reference.
136 private MouseListener fLocalMouseListener
= null;
138 * The local control listener reference.
140 private ControlListener fLocalControlListener
= null;
142 * The local key listener reference.
144 private KeyListener fLocalKeyListener
= null;
145 // Canvas for vertical/horizontal Scroll Bar only ... because new ScrollBar() does works.
147 * Canvas for horizontal scroll bar.
149 private Canvas fHorScrollBar
;
151 * Canvas for vertical scroll bar.
153 private Canvas fVertScrollBar
;
155 * Canvas for the view control.
157 private Canvas fViewControl
;
159 * Control used in the bottom right corner @see setCornerControl() and @see setOverviewEnabled(true)
161 private Control fCornerControl
;
163 * Size of overview widget.
165 private int fOverviewSize
= 100; // default size for overview
167 * Timer for auto_scroll feature
169 private AutoScroll fAutoScroll
= null;
171 * TimerTask for auto_scroll feature !=null when auto scroll is running
173 private Timer fAutoScrollTimer
= null;
175 * where mouse down appear on contents area (x coordinate)
177 private int fMouseDownX
= -1;
179 * where mouse down appear on contents area (y coordinate)
181 private int fMousDownY
= -1;
183 // ------------------------------------------------------------------------
185 // ------------------------------------------------------------------------
188 * Create a ScrollView, child of composite c. Both scroll bar have the mode AUTO. Auto scroll feature is enabled
189 * using a delay of 250ms. Overview feature is not enabled by default (use setOverviewEnabled()).
191 * @param c The parent composite
192 * @param style The SWT style bits @see SWT
194 public ScrollView(Composite c
, int style
) {
195 this(c
, style
, true);
199 * Create a ScrollView, child of composite c. Both scroll bar have the mode AUTO. Auto scroll feature is enabled
200 * using a delay of 250ms. Overview feature is not enabled by default (use setOverviewEnabled()).
202 * @param c The parent composite.
203 * @param style The SWT style bits @see SWT
204 * @param mouseWheel Flag to force scrollView to handles mouse wheel
206 public ScrollView(Composite c
, int style
, boolean mouseWheel
) {
209 fHorScrollBar
= new Canvas(this, SWT
.H_SCROLL
);
211 // force scroll bar to get mouse wheel, those scrollbar will be hidden
212 fViewControl
= new Canvas(this, style
| SWT
.H_SCROLL
| SWT
.V_SCROLL
);
214 fViewControl
= new Canvas(this, style
);
216 fViewControl
.setBackground(getBackground());
217 // hide scroll bar as their are replaced by fHorScrollBar and fVertScrollBar.
219 fViewControl
.getVerticalBar().setVisible(false);
220 fViewControl
.getHorizontalBar().setVisible(false);
222 fVertScrollBar
= new Canvas(this, SWT
.V_SCROLL
);
224 setLayout(new SVLayout());
226 fLocalPaintListener
= new PaintListener() {
228 public void paintControl(PaintEvent event
) {
229 // use clipping, to reduce cost of paint.
230 Rectangle r
= event
.gc
.getClipping();
231 int cx
= viewToContentsX(r
.x
);
232 int cy
= viewToContentsY(r
.y
);
233 drawContents(event
.gc
, cx
, cy
, r
.width
, r
.height
);
236 fViewControl
.addPaintListener(fLocalPaintListener
);
238 fLocalMouseMoveListener
= new MouseMoveListener() {
240 public void mouseMove(MouseEvent e
) {
241 int ox
= e
.x
, oy
= e
.y
;
242 e
.x
= viewToContentsX(e
.x
);
243 e
.y
= viewToContentsY(e
.y
);
244 contentsMouseMoveEvent(e
);
250 fViewControl
.addMouseMoveListener(fLocalMouseMoveListener
);
252 MouseTrackListener localMouseTrackListener
= new MouseTrackListener() {
254 public void mouseEnter(MouseEvent e
) {
255 int ox
= e
.x
, oy
= e
.y
;
256 e
.x
= viewToContentsX(e
.x
);
257 e
.y
= viewToContentsY(e
.y
);
258 contentsMouseEnter(e
);
264 public void mouseHover(MouseEvent e
) {
265 int ox
= e
.x
, oy
= e
.y
;
266 e
.x
= viewToContentsX(e
.x
);
267 e
.y
= viewToContentsY(e
.y
);
268 contentsMouseHover(e
);
274 public void mouseExit(MouseEvent e
) {
275 int ox
= e
.x
, oy
= e
.y
;
276 e
.x
= viewToContentsX(e
.x
);
277 e
.y
= viewToContentsY(e
.y
);
278 contentsMouseExit(e
);
285 fViewControl
.addMouseTrackListener(localMouseTrackListener
);
287 fLocalMouseListener
= new MouseListener() {
289 public void mouseDoubleClick(MouseEvent e
) {
290 int ox
= e
.x
, oy
= e
.y
;
291 e
.x
= viewToContentsX(e
.x
);
292 e
.y
= viewToContentsY(e
.y
);
293 contentsMouseDoubleClickEvent(e
);
299 public void mouseDown(MouseEvent e
) {
300 int ox
= e
.x
, oy
= e
.y
;
301 e
.x
= viewToContentsX(e
.x
);
303 e
.y
= viewToContentsY(e
.y
);
305 contentsMouseDownEvent(e
);
311 public void mouseUp(MouseEvent e
) {
312 int ox
= e
.x
, oy
= e
.y
;
313 e
.x
= viewToContentsX(e
.x
);
314 e
.y
= viewToContentsY(e
.y
);
315 contentsMouseUpEvent(e
);
318 // here because class extending me can catch mouse Up and want to scroll...
323 fViewControl
.addMouseListener(fLocalMouseListener
);
325 fLocalKeyListener
= new KeyListener() {
327 public void keyPressed(KeyEvent e
) {
332 public void keyReleased(KeyEvent e
) {
337 fViewControl
.addKeyListener(fLocalKeyListener
);
339 getVerticalBar().addSelectionListener(new SelectionListener() {
341 public void widgetSelected(SelectionEvent e
) {
342 setContentsPos(fContentsX
, getVerticalBar().getSelection());
343 // need to change "hidden" vertical bar value ?
344 // force focus on fViewControl so we got future mouse wheel's scroll events
345 if (!fViewControl
.isFocusControl()) {
346 fViewControl
.setFocus();
351 public void widgetDefaultSelected(SelectionEvent e
) {
355 if (fViewControl
.getVerticalBar() != null) {
356 // add fViewControl hidden scrollbar listener to get mouse wheel ...
357 fViewControl
.getVerticalBar().addSelectionListener(new SelectionListener() {
359 public void widgetSelected(SelectionEvent e
) {
360 ScrollBar b
= fViewControl
.getVerticalBar();
361 setContentsPos(fContentsX
, b
.getSelection());
362 // change "real" vertical bar selection too
363 getVerticalBar().setSelection(b
.getSelection());
367 public void widgetDefaultSelected(SelectionEvent e
) {
371 getHorizontalBar().addSelectionListener(new SelectionListener() {
373 public void widgetSelected(SelectionEvent e
) {
374 setContentsPos(getHorizontalBar().getSelection(), fContentsY
);
375 // need to change "real" horizontal bar too ?
376 // force focus on fViewControl so we got future mouse wheel's scroll events
377 if (!fViewControl
.isFocusControl()) {
378 fViewControl
.setFocus();
383 public void widgetDefaultSelected(SelectionEvent e
) {
386 if (fViewControl
.getHorizontalBar() != null) {
387 fViewControl
.getHorizontalBar().addSelectionListener(new SelectionListener() {
389 public void widgetSelected(SelectionEvent e
) {
390 ScrollBar b
= fViewControl
.getHorizontalBar();
391 setContentsPos(b
.getSelection(), fContentsY
);
392 // change "real" vertical bar selection too
393 getHorizontalBar().setSelection(b
.getSelection());
397 public void widgetDefaultSelected(SelectionEvent e
) {
403 // ------------------------------------------------------------------------
405 // ------------------------------------------------------------------------
408 public boolean setFocus() {
409 return fViewControl
.forceFocus();
413 public void setCursor(Cursor cursor
) {
414 fViewControl
.setCursor(cursor
);
418 public void dispose() {
419 if (fAutoScroll
!= null) {
420 fAutoScroll
.cancel();
423 if (fViewControl
!= null) {
424 fViewControl
.dispose();
427 if (fVertScrollBar
!= null) {
428 fVertScrollBar
.dispose();
430 fVertScrollBar
= null;
431 if (fHorScrollBar
!= null) {
432 fHorScrollBar
.dispose();
434 fHorScrollBar
= null;
435 if (fCornerControl
!= null) {
436 Object data
= fCornerControl
.getData();
437 if (data
instanceof Overview
) {
438 ((Overview
) data
).dispose();
440 fCornerControl
.dispose();
441 fCornerControl
= null;
447 public Rectangle
getClientArea() {
448 return fViewControl
.getClientArea();
452 public void setBackground(Color c
) {
453 super.setBackground(c
);
454 fViewControl
.setBackground(c
);
458 public void setToolTipText(String text
) {
459 fViewControl
.setToolTipText(text
);
463 * Draw overview area, @see setOverviewEnabled. By default draw a rectangle corresponding to the visible area of
464 * scroll view. You can redefine this method to draw the contents as drawContents does... ...in an other magnify
467 * @param gc GC to used to draw.
468 * @param r Rectangle corresponding to the client area of overview.
470 protected void drawOverview(GC gc
, Rectangle r
) {
471 int x
= (int) (r
.width
* fContentsX
/ (float) fContentsWidth
);
472 int y
= (int) (r
.height
* fContentsY
/ (float) fContentsHeight
);
473 int vw
= getVisibleWidth();
474 int vh
= getVisibleHeight();
476 if (fContentsWidth
> vw
) {
477 w
= (int) (r
.width
* vw
/ (float) fContentsWidth
);
479 int h
= r
.height
- 1;
480 if (fContentsHeight
> vh
) {
481 h
= (int) (r
.height
* vh
/ (float) fContentsHeight
);
484 gc
.setForeground(getForeground());
485 // too small rectangle ?
486 if (w
< 5 || h
< 5) {
488 gc
.drawLine(x
, 0, x
, r
.height
);
489 gc
.drawLine(0, y
, r
.width
, y
);
491 gc
.drawRectangle(x
, y
, w
, h
);
496 * Remove the local Listener and add the new listener.
498 * @param nlistener the new listener
500 public void replaceControlListener(ControlListener nlistener
) {
501 if (fLocalControlListener
!= null) {
502 removeControlListener(fLocalControlListener
);
503 fLocalControlListener
= null;
505 addControlListener(nlistener
);
509 * Remove the local Listener and add the new listener.
511 * @param nlistener the new listener
513 public void replaceKeyListener(KeyListener nlistener
) {
514 if (fLocalKeyListener
!= null) {
515 removeKeyListener(fLocalKeyListener
);
516 fLocalKeyListener
= null;
518 addKeyListener(nlistener
);
522 * Remove the local Listener and add the new listener.
524 * @param nlistener the new listener
526 public void replaceMouseListener(MouseListener nlistener
) {
527 if (fLocalMouseListener
!= null) {
528 removeMouseListener(fLocalMouseListener
);
529 fLocalMouseListener
= null;
531 fViewControl
.addMouseListener(nlistener
);
535 * Remove the local Listener and add the new listener.
537 * @param nlistener the new listener
539 public void replaceMouseMoveListener(MouseMoveListener nlistener
) {
540 if (fLocalMouseMoveListener
!= null) {
541 removeMouseMoveListener(fLocalMouseMoveListener
);
542 fLocalMouseMoveListener
= null;
544 fViewControl
.addMouseMoveListener(nlistener
);
548 * Remove the local Listener and add the new listener.
550 * @param nlistener the new listener
552 public void replacePaintListener(PaintListener nlistener
) {
553 if (fLocalPaintListener
!= null) {
554 removePaintListener(fLocalPaintListener
);
555 fLocalPaintListener
= null;
557 fViewControl
.addPaintListener(nlistener
);
561 * Access method for the contentsHeight property.
563 * @return the current value of the contentsHeight property
565 public int getContentsHeight() {
566 return fContentsHeight
;
570 * Access method for the contentsWidth property.
572 * @return the current value of the contentsWidth property
574 public int getContentsWidth() {
575 return fContentsWidth
;
579 * Access method for the contentsX property.
581 * @return the current value of the contentsX property
583 public int getContentsX() {
588 * Access method for the contentsY property.
590 * @return the current value of the contentsY property
592 public int getContentsY() {
597 * Determines if the dragAutoScroll property is true.
599 * @return <code>true<code> if the dragAutoScroll property is true
601 public boolean isDragAutoScroll() {
602 return fAutoScrollEnabled
;
606 * Sets the value of the dragAutoScroll property.
608 * @param aDragAutoScroll the new value of the dragAutoScroll property
610 public void setDragAutoScroll(boolean aDragAutoScroll
) {
611 fAutoScrollEnabled
= aDragAutoScroll
;
612 if (!fAutoScrollEnabled
&& (fAutoScroll
!= null)) {
613 fAutoScroll
.cancel();
619 * Change delay (in millisec) used for auto scroll feature.
621 * @param period new period between to auto scroll
623 public void setDragAutoScrollPeriod(int period
) {
624 fAutoScrollPeriod
= Math
.max(0, period
);
628 * Return auto scroll period.
632 public int getDragAutoScrollPeriod() {
633 return fAutoScrollPeriod
;
637 * Access method for the hScrollBarMode property.
639 * @return the current value of the hScrollBarMode property
641 public int getHScrollBarMode() {
642 return fHorScrollbarMode
;
646 * Sets the value of the hScrollBarMode property.
648 * @param aHScrollBarMode the new value of the hScrollBarMode property
650 public void setHScrollBarMode(int aHScrollBarMode
) {
651 fHorScrollbarMode
= aHScrollBarMode
;
655 * Access method for the vScrollBarMode property.
657 * @return the current value of the vScrollBarMode property
659 public int getVScrollBarMode() {
660 return fVertScrollbarMode
;
664 * Sets the value of the vScrollBarMode property.
666 * @param aVScrollBarMode the new value of the vScrollBarMode property
668 public void setVScrollBarMode(int aVScrollBarMode
) {
669 fVertScrollbarMode
= aVScrollBarMode
;
673 * Return horizontal scroll bar increment, default:1
675 * @return The increment
677 public int getHScrollBarIncrement() {
678 return fHorScrollbarIncrement
;
682 * Return vertical scroll bar increment, default:1
684 * @return The increment
686 public int getVScrollBarIncrement() {
687 return fVertScrollbarIncrement
;
691 * Change horizontal scroll bar increment, minimum:1. Page increment is
692 * always set to visible width.
695 * Increment value to set
697 public void setHScrollBarIncrement(int inc
) {
698 fHorScrollbarIncrement
= Math
.max(1, inc
);
702 * Change vertical scroll bar increment, minimum:1. Page increment is always
703 * set to visible height.
706 * Increment value to set
708 public void setVScrollBarIncrement(int inc
) {
709 fVertScrollbarIncrement
= Math
.max(1, inc
);
713 * Enable or disable overview feature. Enabling overview, dispose and replace existing corner control by a button.
714 * Clicking in it open overview, move mouse cursor holding button to move scroll view and release button to hide
715 * overview. Tip: hold control and/or shift key while moving mouse when overview is open made fine scroll.
717 * @param value true to engage overview feature
719 public void setOverviewEnabled(boolean value
) {
720 if (isOverviewEnabled() == value
) {
726 Button b
= new Button(this, SWT
.NONE
);
727 b
.setText("+"); //$NON-NLS-1$
728 Overview ovr
= new Overview();
732 b
.setToolTipText(Messages
.SequenceDiagram_OpenOverviewTooltip
);
734 setCornerControl(cc
);
738 * Change overview size (at ratio 1:1), default is 100
743 public void setOverviewSize(int size
) {
744 fOverviewSize
= Math
.abs(size
);
748 * Returns whether the overview is enabled or not.
750 * @return true is overview feature is enabled
752 public boolean isOverviewEnabled() {
753 if (fCornerControl
instanceof Button
) {
754 Object data
= ((Button
) fCornerControl
).getData();
756 if (data
instanceof Overview
) {
764 * Returns the overview size at ratio 1:1.
766 * @return current overview size at ratio 1:1
768 public int getOverviewSize() {
769 return fOverviewSize
;
773 * Returns control used to display view (might not be this object). Use this control to add/remove listener on the
776 * @return control used to display view (might not be this object).
778 public Control
getViewControl() {
783 * Called when the mouse enter the ScrollView area
788 protected void contentsMouseExit(MouseEvent e
) {
792 * Called when the mouse enter the ScrollView area after and system defined
798 protected void contentsMouseHover(MouseEvent e
) {
802 * Called when the mouse enter the ScrollView area
807 protected void contentsMouseEnter(MouseEvent e
) {
811 * Called when user double on contents area.
816 protected void contentsMouseDoubleClickEvent(MouseEvent e
) {
820 * Called when mouse is on contents area and button is pressed.
825 protected void contentsMouseDownEvent(MouseEvent e
) {
831 * TimerTask for auto scroll feature.
833 protected static class AutoScroll
extends TimerTask
{
841 /** ScrollView object */
842 private ScrollView scrollView
;
848 * ScrollView object to use
854 public AutoScroll(ScrollView sv
, int dx
, int dy
) {
862 Display
.getDefault().asyncExec(new Runnable() {
865 scrollView
.scrollBy(deltaX
, deltaY
);
872 * Called when mouse is on contents area and mode.
877 protected void contentsMouseMoveEvent(MouseEvent event
) {
878 if ((event
.stateMask
& SWT
.BUTTON_MASK
) != 0) {
879 if (!fAutoScrollEnabled
) {
880 scrollBy(-(event
.x
- fMouseDownX
), -(event
.y
- fMousDownY
));
886 int vRight
= getContentsX() + getVisibleWidth();
887 int vBottom
= getContentsY() + getVisibleHeight();
890 if (event
.x
< getContentsX()) {
891 sx
= (getContentsX() - event
.x
);
892 fMouseDownX
= getContentsX();
893 } else if (event
.x
> vRight
) {
894 sx
= -event
.x
+ vRight
;
895 fMouseDownX
= vRight
;
897 if (event
.y
< getContentsY()) {
898 sy
= (getContentsY() - event
.y
);
899 fMousDownY
= getContentsY();
900 } else if (event
.y
> vBottom
) {
901 sy
= -event
.y
+ vBottom
;
902 fMousDownY
= vBottom
;
905 if (sx
!= 0 || sy
!= 0) {
906 // start auto scroll...
907 if (fAutoScroll
== null) {
908 if (fAutoScrollTimer
== null) {
909 fAutoScrollTimer
= new Timer(true);
911 fAutoScroll
= new AutoScroll(this, sx
, sy
);
912 fAutoScrollTimer
.schedule(fAutoScroll
, 0, fAutoScrollPeriod
);
914 fAutoScroll
.deltaX
= sx
;
915 fAutoScroll
.deltaY
= sy
;
918 if (fAutoScroll
!= null) {
919 fAutoScroll
.cancel();
923 scrollBy(-(event
.x
- fMouseDownX
), -(event
.y
- fMousDownY
));
929 * Called when mouse is on contents area and button is released
934 protected void contentsMouseUpEvent(MouseEvent event
) {
935 // reset auto scroll if it's engaged
936 if (fAutoScroll
!= null) {
937 fAutoScroll
.cancel();
943 * Responsible to draw contents area. At least rectangle clipX must be
944 * redrawn. This rectangle is given in contents coordinates. By default, no
958 protected void drawContents(GC gc
, int clipx
, int clipy
, int clipw
, int cliph
) {
962 * Change the size of the contents area.
964 * @param width new width of the area.
965 * @param height new height of the area.
967 public void resizeContents(int width
, int height
) {
968 int localWidth
= width
;
969 int localHeight
= height
;
971 if (localWidth
< 0) {
974 if (localHeight
< 0) {
978 int oldW
= fContentsWidth
;
979 int oldH
= fContentsHeight
;
981 if (localWidth
== oldW
&& localHeight
== oldH
) {
985 fContentsWidth
= localWidth
;
986 fContentsHeight
= localHeight
;
988 if (oldW
> localWidth
) {
994 int visWidth
= getVisibleWidth();
995 int visHeight
= getVisibleHeight();
996 if (oldW
< visWidth
) {
997 if (localWidth
> visWidth
) {
998 localWidth
= visWidth
;
1000 fViewControl
.redraw(getContentsX() + oldW
, 0, localWidth
- oldW
, visHeight
, true);
1003 if (oldH
> localHeight
) {
1004 int s
= localHeight
;
1009 if (oldH
< visHeight
) {
1010 if (localHeight
> visHeight
) {
1011 localHeight
= visHeight
;
1013 fViewControl
.redraw(0, getContentsY() + oldH
, visWidth
, localHeight
- oldH
, true);
1015 if (updateScrollBarVisiblity()) {
1018 updateScrollBarsValues();
1022 // redefined for internal use ..
1024 public void redraw() {
1026 // ..need to redraw this already:
1027 fViewControl
.redraw();
1031 * @param delataX The delta in X
1032 * @param deltaY the delta in Y
1034 public void scrollBy(int delataX
, int deltaY
) {
1035 setContentsPos(getContentsX() + delataX
, getContentsY() + deltaY
);
1039 * Scroll to ensure point(in contents coordinates) is visible.
1041 * @param px Point's x position
1042 * @param py Point's y position
1044 public void ensureVisible(int px
, int py
) {
1045 int cx
= getContentsX(), cy
= getContentsY();
1046 int right
= getContentsX() + getVisibleWidth();
1047 int bottom
= getContentsY() + getVisibleHeight();
1048 if (px
< getContentsX()) {
1050 } else if (px
> right
) {
1051 cx
= px
- getVisibleWidth();
1053 if (py
< getContentsY()) {
1055 } else if (py
> bottom
) {
1056 cy
= py
- getVisibleHeight();
1058 setContentsPos(cx
, cy
);
1062 * Make rectangle (x,y,w,h, in contents coordinates) visible. if rectangle cannot be completely visible, use
1065 * @param xValue x contents coordinates of rectangle.
1066 * @param yValue y contents coordinates of rectangle.
1067 * @param width width of rectangle.
1068 * @param height height of rectangle.
1069 * @param align bit or'ed SWT flag like SWT.LEFT,RIGHT,CENTER,TOP,BOTTOM,VERTICAL used only for bigger rectangle
1070 * than visible area. By default CENTER/VERTICAL
1072 public void ensureVisible(int xValue
, int yValue
, int width
, int height
, int align
) {
1073 ensureVisible(xValue
, yValue
, width
, height
, align
, false);
1077 * Make rectangle (xValue,yValue,width,height, in contents coordinates) visible. if rectangle cannot be completely visible, use
1080 * @param xValue x contents coordinates of rectangle.
1081 * @param yValue y contents coordinates of rectangle.
1082 * @param width width of rectangle.
1083 * @param height height of rectangle.
1084 * @param align bit or'ed SWT flag like SWT.LEFT,RIGHT,CENTER,TOP,BOTTOM,VERTICAL used only for bigger rectangle
1085 * than visible area. By default CENTER/VERTICAL
1086 * @param forceAlign force alignment for rectangle smaller than the visible area
1088 protected void ensureVisible(int xValue
, int yValue
, int width
, int height
, int align
, boolean forceAlign
) {
1090 int localX
= xValue
;
1091 int localY
= yValue
;
1092 int localWidth
= width
;
1093 int localHeight
= height
;
1095 if (localWidth
< 0) {
1096 localX
= localX
+ localWidth
;
1097 localWidth
= -localWidth
;
1099 if (localHeight
< 0) {
1100 localY
= localY
+ localHeight
;
1101 localHeight
= -localHeight
;
1103 int hbar
= getHorizontalBarHeight();
1104 int vbar
= getVerticalBarWidth();
1105 int cx
= getContentsX();
1106 int cy
= getContentsY();
1107 int right
= getContentsX() + getVisibleWidth() - vbar
;
1108 int bottom
= getContentsY() + getVisibleHeight() - hbar
;
1109 boolean alignH
= false, alignV
= false;
1111 if (localX
< getContentsX()) {
1113 } else if (localX
+ localWidth
> right
) {
1114 cx
= localX
- localWidth
;
1116 if (localY
< getContentsY()) {
1118 } else if (localY
+ localHeight
> bottom
) {
1119 cy
= localY
- localHeight
;
1122 if (localWidth
> getVisibleWidth()) {
1125 if (localHeight
> getVisibleHeight()) {
1128 // compute alignment on visible area horizontally
1129 if (alignH
|| (forceAlign
&& localX
+ localWidth
> right
)) {
1131 if ((align
& SWT
.LEFT
) != 0) {
1133 } else if ((align
& SWT
.RIGHT
) != 0) {
1134 cx
= right
- localWidth
;
1136 cx
= localX
+ (localWidth
- getVisibleWidth()) / 2;
1139 // compute alignment on visible area vertically
1140 if (alignV
|| (forceAlign
&& localY
+ localHeight
> bottom
)) {
1142 if ((align
& SWT
.TOP
) != 0) {
1144 } else if ((align
& SWT
.BOTTOM
) != 0) {
1145 cy
= bottom
- localHeight
;
1147 cy
= localY
+ (localHeight
- getVisibleHeight()) / 2;
1150 setContentsPos(cx
, cy
);
1154 * Returns true if point is visible (expressed in contents coordinates).
1156 * @param px Point's x position
1157 * @param py Point's y position
1158 * @return true if point is visible (expressed in contents coordinates)
1160 public boolean isVisible(int px
, int py
) {
1161 if (px
< getContentsX()) {
1164 if (py
< getContentsY()) {
1167 if (px
> (getContentsX() + getVisibleWidth())) {
1170 if (py
> (getContentsY() + getVisibleHeight())) {
1177 * Returns true if rectangle if partially visible.
1179 * @param xValue x contents coordinates of rectangle.
1180 * @param yValue y contents coordinates of rectangle.
1181 * @param width width of rectangle.
1182 * @param height height of rectangle.
1183 * @return true if rectangle if partially visible.
1185 public boolean isVisible(int xValue
, int yValue
, int width
, int height
) {
1186 if (xValue
+ width
< getContentsX()) {
1189 if (yValue
+ height
< getContentsY()) {
1192 int vr
= getContentsX() + getVisibleWidth();
1193 int vb
= getContentsY() + getVisibleHeight();
1204 * Returns visible part of rectangle, or null if rectangle is not visible.
1205 * Rectangle is expressed in contents coordinates.
1208 * x contents coordinates of rectangle.
1210 * y contents coordinates of rectangle.
1212 * width of rectangle.
1214 * height of rectangle.
1215 * @return visible part of rectangle, or null if rectangle is not visible.
1217 public Rectangle
getVisiblePart(int xValue
, int yValue
, int width
, int height
) {
1218 if (xValue
+ width
< getContentsX()) {
1221 if (yValue
+ height
< getContentsY()) {
1224 int vr
= getContentsX() + getVisibleWidth();
1225 int vb
= getContentsY() + getVisibleHeight();
1232 int rr
= xValue
+ width
, rb
= yValue
+ height
;
1233 int nl
= Math
.max(xValue
, getContentsX()), nt
= Math
.max(yValue
, getContentsY()), nr
= Math
.min(rr
, vr
), nb
= Math
.min(rb
, vb
);
1234 return new Rectangle(nl
, nt
, nr
- nl
, nb
- nt
);
1238 * Returns the visible part for given rectangle.
1240 * @param rect A rectangle
1242 * @return gets visible part of rectangle (or <code>null</code>)
1244 public final Rectangle
getVisiblePart(Rectangle rect
) {
1248 return getVisiblePart(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
1252 * Change top left position of visible area. Check if the given point is inside contents area.
1254 * @param xValue x contents coordinates of visible area.
1255 * @param yValue y contents coordinates of visible area.
1256 * @return true if view really moves
1258 public boolean setContentsPos(int xValue
, int yValue
) {
1259 int nx
= xValue
, ny
= yValue
;
1260 if (getVisibleWidth() >= getContentsWidth()) {
1265 } else if (xValue
+ getVisibleWidth() > getContentsWidth()) {
1266 nx
= getContentsWidth() - getVisibleWidth();
1269 if (getVisibleHeight() >= getContentsHeight()) {
1274 } else if (yValue
+ getVisibleHeight() > getContentsHeight()) {
1275 ny
= getContentsHeight() - getVisibleHeight();
1279 if (nx
== fContentsX
&& ny
== fContentsY
) {
1284 updateScrollBarsValues();
1285 // ? find smallest area to redraw only them ?
1286 fViewControl
.redraw();
1291 public ScrollBar
getVerticalBar() {
1292 return fVertScrollBar
.getVerticalBar();
1296 public ScrollBar
getHorizontalBar() {
1297 return fHorScrollBar
.getHorizontalBar();
1301 * Compute visibility of vertical/horizontal bar using given width/height and current visibility (i.e. is bar size are already in
1303 * @param forWidth width of foreground
1304 * @param forHeight height of foreground
1305 * @param currHorVis The current visibility state of horizontal scroll bar
1306 * @param currVertvis The current visibility state of vertical scroll bar
1307 * @return <code>true</code> if visibility changed else <code>false</code>
1309 public int computeBarVisibility(int forWidth
, int forHeight
, boolean currHorVis
, boolean currVertvis
) {
1311 int localForWidth
= forWidth
;
1313 switch (fVertScrollbarMode
) {
1320 if (getContentsHeight() > forHeight
) {
1322 // v bar size is already in for_width.
1323 if (!currVertvis
) {// (curr_vis&0x01)==0)
1324 localForWidth
-= getVerticalBarWidth();
1332 switch (fHorScrollbarMode
) {
1339 if (getContentsWidth() > localForWidth
) {
1341 // h bar is not in for_height
1342 if ((!currHorVis
) && (getContentsHeight() > (forHeight
- getHorizontalBarHeight()))) {// (curr_vis&0x02)==0 )
1354 * Setup scroll bars visibility.
1356 * @return True if one of visibility changed.
1358 protected boolean updateScrollBarVisiblity() {
1359 boolean change
= false;
1361 boolean currVertVis
= fVertScrollBar
.getVisible();
1362 boolean currHorVis
= fHorScrollBar
.getVisible();
1363 int barNewVis
= computeBarVisibility(getVisibleWidth(), getVisibleHeight(), currHorVis
, currVertVis
);
1364 boolean newVertVis
= (barNewVis
& VBAR
) != 0;
1365 boolean newHorVis
= (barNewVis
& HBAR
) != 0;
1366 if (currVertVis ^ newVertVis
) { // vertsb_.getVisible() )
1367 fVertScrollBar
.setVisible(newVertVis
);
1370 if (currHorVis ^ newHorVis
) {
1371 fHorScrollBar
.setVisible(newHorVis
);
1375 // update corner control visibility:
1376 if (fCornerControl
!= null && change
) {
1377 boolean vis
= newVertVis
|| newHorVis
;
1378 if (vis ^ fCornerControl
.getVisible()) {
1379 fCornerControl
.setVisible(vis
);
1380 change
= true; // but must be already the case
1387 * Setup scroll bar using contents, visible and scroll bar mode properties.
1389 protected void updateScrollBarsValues() {
1390 /* update vertical scrollbar */
1391 ScrollBar b
= getVerticalBar();
1394 b
.setMaximum(getContentsHeight());
1395 b
.setThumb(getVisibleHeight());
1396 b
.setPageIncrement(getVisibleHeight());
1397 b
.setIncrement(fVertScrollbarIncrement
);
1398 b
.setSelection(getContentsY());
1401 // update "hidden" vertical bar too
1402 b
= fViewControl
.getVerticalBar();
1405 b
.setMaximum(getContentsHeight());
1406 b
.setThumb(getVisibleHeight());
1407 b
.setPageIncrement(getVisibleHeight());
1408 b
.setIncrement(fVertScrollbarIncrement
);
1409 b
.setSelection(getContentsY());
1412 /* update horizontal scrollbar */
1413 b
= getHorizontalBar();
1416 b
.setMaximum(getContentsWidth());
1417 b
.setThumb(getVisibleWidth());
1418 b
.setSelection(getContentsX());
1419 b
.setPageIncrement(getVisibleWidth());
1420 b
.setIncrement(fHorScrollbarIncrement
);
1422 // update "hidden" horizontal bar too
1423 b
= fViewControl
.getHorizontalBar();
1426 b
.setMaximum(getContentsWidth());
1427 b
.setThumb(getVisibleWidth());
1428 b
.setSelection(getContentsX());
1429 b
.setPageIncrement(getVisibleWidth());
1430 b
.setIncrement(fHorScrollbarIncrement
);
1435 * Change the control used in the bottom right corner (between two scrollbar), if control is null reset previous
1436 * corner control. This control is visible only if at least one scrollbar is visible. Given control will be disposed
1437 * by ScrollView, at dispose() time, at next setCornetControl() call or when calling setOverviewEnabled(). Pay
1438 * attention calling this reset overview feature until setOverviewEnabled(true) if called.
1439 * @param control The control for the overview
1441 public void setCornerControl(Control control
) {
1442 if (fCornerControl
!= null) {
1443 fCornerControl
.dispose();
1445 fCornerControl
= control
;
1446 if (fCornerControl
!= null) {
1447 ScrollBar vb
= getVerticalBar();
1448 ScrollBar hb
= getHorizontalBar();
1449 boolean vis
= vb
.getVisible() || hb
.getVisible();
1450 fCornerControl
.setVisible(vis
);
1455 * Transform (x,y) point in widget coordinates to contents coordinates.
1457 * @param x The x widget coordinate.
1458 * @param y The y widget coordinate.
1459 * @return org.eclipse.swt.graphics.Point with content coordinates.
1461 public final Point
viewToContents(int x
, int y
) {
1462 return new Point(viewToContentsX(x
), viewToContentsY(y
));
1466 * Transform x in widget coordinates to contents coordinates
1468 * @param x The y widget coordinate.
1469 * @return the x content coordinate.
1471 public int viewToContentsX(int x
) {
1472 return fContentsX
+ x
;
1476 * Transform y in widget coordinates to contents coordinates
1478 * @param y The y widget coordinate.
1479 * @return the y content coordinate.
1481 public int viewToContentsY(int y
) {
1482 return fContentsY
+ y
;
1486 * Transform (x,y) point from contents coordinates, to widget coordinates.
1488 * @param x The x content coordinate.
1489 * @param y The y content coordinate.
1490 * @return coordinates widget area as.
1492 public final Point
contentsToView(int x
, int y
) {
1493 return new Point(contentsToViewX(x
), contentsToViewY(y
));
1497 * Transform X axis coordinates from contents to widgets.
1499 * @param x contents coordinate to transform.
1500 * @return x coordinate in widget area
1502 public int contentsToViewX(int x
) {
1503 return x
- fContentsX
;
1507 * Transform Y axis coordinates from contents to widgets.
1509 * @param y contents coordinate to transform
1510 * @return y coordinate in widget area
1512 public int contentsToViewY(int y
) {
1513 return y
- fContentsY
;
1517 * Return the visible height of scroll view, might be > contentsHeight
1519 * @return the visible height of scroll view, might be > contentsHeight()
1521 public int getVisibleHeight() {
1522 return fViewControl
.getClientArea().height
;
1526 * Return int the visible width of scroll view, might be > contentsWidth().
1528 * @return int the visible width of scroll view, might be > contentsWidth()
1530 public int getVisibleWidth() {
1531 return fViewControl
.getClientArea().width
;
1535 * Add support for arrow key, scroll the ... scroll view. But you can
1536 * redefine this method for your convenience.
1541 protected void keyPressedEvent(KeyEvent event
) {
1542 switch (event
.keyCode
) {
1544 scrollBy(0, -getVisibleHeight());
1546 case SWT
.ARROW_DOWN
:
1547 scrollBy(0, +getVisibleHeight());
1549 case SWT
.ARROW_LEFT
:
1550 scrollBy(-getVisibleWidth(), 0);
1552 case SWT
.ARROW_RIGHT
:
1553 scrollBy(+getVisibleWidth(), 0);
1561 * Redefine this method at your convenience
1563 * @param event The key event.
1565 protected void keyReleasedEvent(KeyEvent event
) {
1569 * Returns vertical bar width, even if bar isn't visible.
1571 * @return vertical bar width, even if bar isn't visible
1573 public int getVerticalBarWidth() {
1574 // include vertical bar width and trimming of scrollable used
1575 int bw
= fVertScrollBar
.computeTrim(0, 0, 0, 0).width
;
1580 * Returns horizontal bar height even if bar isn't visible.
1582 * @return horizontal bar height even if bar isn't visible
1584 public int getHorizontalBarHeight() {
1585 // include horiz. bar height and trimming of scrollable used
1586 int bh
= fHorScrollBar
.computeTrim(0, 0, 0, 0).height
;
1587 // +1 because win32 H.bar need 1 pixel canvas size to appear ! (strange no ?)
1592 public Rectangle
computeTrim(int x
, int y
, int w
, int h
) {
1593 Rectangle r
= new Rectangle(x
, y
, w
, h
);
1594 int barVis
= computeBarVisibility(w
, h
, false, false);
1595 if ((barVis
& VBAR
) != 0) {
1596 r
.width
+= getVerticalBarWidth();
1598 if ((barVis
& HBAR
) != 0) {
1599 r
.height
+= getHorizontalBarHeight();
1605 * Internal layout for ScrollView, handle scrollbars, drawzone and corner control
1607 protected class SVLayout
extends Layout
{
1609 private final static int DEFAULT_X
= 250;
1610 private final static int DEFAULT_Y
= 250;
1611 private final static int MAX_SEEK
= 10;
1612 private final static int MIN_SEEK
= 0;
1617 private int seek
= 0;
1619 * The do-it-not flag
1621 private boolean dontLayout
= false;
1624 protected Point
computeSize(Composite composite
, int wHint
, int hHint
, boolean flushCache
) {
1625 Point p
= new Point(DEFAULT_X
, DEFAULT_Y
);
1626 if (fContentsWidth
< p
.x
) {
1627 p
.x
= fContentsWidth
;
1629 if (fContentsHeight
< p
.y
) {
1630 p
.y
= fContentsHeight
;
1636 protected void layout(Composite composite
, boolean flushCache
) {
1641 if (seek
> MAX_SEEK
) {
1645 Point cs
= composite
.getSize();
1646 int barVis
= computeBarVisibility(cs
.x
, cs
.y
, false, false);
1647 boolean vbVis
= (barVis
& VBAR
) != 0;
1648 boolean hbVis
= (barVis
& HBAR
) != 0;
1649 fVertScrollBar
.setVisible(vbVis
);
1650 fHorScrollBar
.setVisible(hbVis
);
1651 int vbw
= getVerticalBarWidth();
1652 int hbh
= getHorizontalBarHeight();
1653 int wb
= vbVis ? vbw
: 0;
1654 int hb
= hbVis ? hbh
: 0;
1655 int cww
= 0, cwh
= 0;
1657 if (fCornerControl
!= null && (vbVis
|| hbVis
)) { // corner_control_.getVisible())
1658 fCornerControl
.setVisible(true);
1667 } else if (vbVis
&& hbVis
) {
1668 if (fCornerControl
!= null) {
1669 fCornerControl
.setVisible(false);
1674 if (vbVis
|| hbVis
) {
1675 updateScrollBarsValues();
1678 int vw
= cs
.x
- (vbVis ? vbw
: 0);
1679 int vh
= cs
.y
- (hbVis ? hbh
: 0);
1680 int vbx
= cs
.x
- wb
;
1681 int hby
= cs
.y
- hb
;
1683 fViewControl
.setBounds(0, 0, vw
, vh
);
1686 fVertScrollBar
.setBounds(vbx
, 0, wb
, cs
.y
- cwh
);
1689 fHorScrollBar
.setBounds(0, hby
, cs
.x
- cww
, hb
);
1691 if (fCornerControl
!= null && fCornerControl
.getVisible()) {
1692 fCornerControl
.setBounds(vbx
, hby
, vbw
, hbh
);
1694 updateScrollBarsValues();
1697 if (seek
== MIN_SEEK
) {
1702 boolean isDontLayout() {
1706 void setSontLayout(boolean dontLayout
) {
1707 this.dontLayout
= dontLayout
;
1711 // static must take place here... cursor is created once.
1712 private volatile static Cursor fOverviewCursor
;
1714 /** Support for click-and-see overview shell on this ScrollView */
1715 protected class Overview
{
1717 private final static int REFRESH_FREQ
= 4;
1720 * factor for X from real and overview sizes, for mouse move speed.
1722 private float fOverviewFactorX
;
1725 * factor for Y from real and overview sizes, for mouse move speed.
1727 private float fOverviewFactorY
;
1729 * shell use to show overview
1731 private Shell fOverview
;
1733 * save mouse X cursor location for disappear();
1735 private int fSaveCursorX
;
1737 * save mouse Y cursor location for disappear();
1739 private int fSaveCursorY
;
1742 * Apply overview support on a control. Replace existing corner_widget
1745 * The control to use
1747 public void useControl(Control control
) {
1748 final Point pos
= control
.getLocation();
1749 control
.addMouseListener(new MouseListener() {
1751 public void mouseDoubleClick(MouseEvent e
) {
1755 public void mouseDown(MouseEvent e
) {
1756 overviewAppear(e
.x
, e
.y
);
1760 public void mouseUp(MouseEvent e
) {
1761 overviewDisappear();
1765 control
.addFocusListener(new FocusListener() {
1768 public void focusGained(FocusEvent e
) {
1772 public void focusLost(FocusEvent e
) {
1773 if (overviewing()) {
1774 overviewDisappear(false);
1779 control
.addKeyListener(new KeyListener() {
1782 public void keyPressed(KeyEvent event
) {
1783 if (event
.keyCode
== 32 && !overviewing()) {
1784 overviewAppear(pos
.x
, pos
.y
);
1785 } else if (event
.keyCode
== 32) {
1786 overviewDisappear();
1788 if (event
.keyCode
== SWT
.ARROW_DOWN
) {
1789 overviewMove(0, 1, event
);
1792 if (event
.keyCode
== SWT
.ARROW_UP
) {
1793 overviewMove(0, -1, event
);
1796 if (event
.keyCode
== SWT
.ARROW_RIGHT
) {
1797 overviewMove(1, 0, event
);
1800 if (event
.keyCode
== SWT
.ARROW_LEFT
) {
1801 overviewMove(-1, 0, event
);
1806 public void keyReleased(KeyEvent e
) {
1809 control
.addMouseMoveListener(new MouseMoveListener() {
1810 private int refReshCount
= 0;
1812 public void mouseMove(MouseEvent event
) {
1813 if (overviewing()) {
1814 // Slow down the refresh
1815 if (refReshCount
% REFRESH_FREQ
== 0) {
1816 overviewMove(event
);
1825 * Dispose controls of overview
1827 public void dispose() {
1828 if (fOverview
!= null) {
1829 fOverview
.dispose();
1834 * @return true if overview is currently on screen
1836 protected boolean overviewing() {
1837 return (fOverview
!= null && fOverview
.isVisible());
1841 * Process overview appear
1848 protected void overviewAppear(int mx
, int my
) {
1849 if (fOverview
== null) {
1850 fOverview
= new Shell(getShell(), SWT
.ON_TOP
| SWT
.NO_BACKGROUND
);
1851 fOverview
.addPaintListener(new PaintListener() {
1853 public void paintControl(PaintEvent e
) {
1854 drawOverview(e
.gc
, fOverview
.getClientArea());
1858 // always the same..
1859 fOverview
.setForeground(fViewControl
.getForeground());
1861 // get location of shell (in screeen coordinates)
1862 Point p
= toGlobalCoordinates(fCornerControl
, 0, 0);
1868 Rectangle scr
= getDisplay().getBounds();
1869 Point ccs
= fCornerControl
.getSize();
1871 if (fContentsWidth
> fContentsHeight
) {
1872 float ratio
= fContentsHeight
/ (float) fContentsWidth
;
1873 h
= (int) (w
* ratio
);
1876 } else if (h
>= scr
.height
/ 2) {
1880 float ratio
= fContentsWidth
/ (float) fContentsHeight
;
1881 w
= (int) (h
* ratio
);
1884 } else if (w
>= scr
.width
/ 2) {
1888 fOverviewFactorX
= fContentsWidth
/ (float) w
;
1889 fOverviewFactorY
= fContentsHeight
/ (float) h
;
1891 // no contents size set ?
1892 catch (java
.lang
.ArithmeticException e
) {
1895 // try pop-up on button, extending to bottom right,
1904 fOverview
.setBounds(x
, y
, w
, h
);
1905 fOverview
.setVisible(true);
1907 // mouse cursor disappear, so set invisible mouse cursor ...
1908 if (fOverviewCursor
== null) {
1909 RGB rgb
[] = { new RGB(0, 0, 0), new RGB(255, 0, 0) };
1910 PaletteData palette
= new PaletteData(rgb
);
1912 byte src
[] = new byte[s
* s
];
1913 byte msk
[] = new byte[s
* s
];
1914 for (int i
= 0; i
< s
* s
; ++i
) {
1915 src
[i
] = (byte) 0xFF;
1917 ImageData i_src
= new ImageData(s
, s
, 1, palette
, 1, src
);
1918 ImageData i_msk
= new ImageData(s
, s
, 1, palette
, 1, msk
);
1919 fOverviewCursor
= new Cursor(null, i_src
, i_msk
, 0, 0);
1921 fCornerControl
.setCursor(fOverviewCursor
);
1922 // convert to global coordinates
1923 p
= toGlobalCoordinates(fCornerControl
, mx
, my
);
1927 Rectangle r
= fOverview
.getClientArea();
1928 int cx
= (int) (r
.width
* fContentsX
/ (float) fContentsWidth
);
1929 int cy
= (int) (r
.height
* fContentsY
/ (float) fContentsHeight
);
1931 // cx,cy to display's global coordinates
1932 p
= toGlobalCoordinates(fOverview
.getParent(), cx
, cy
);
1936 * Process disappear of overview
1938 protected void overviewDisappear() {
1939 overviewDisappear(true);
1943 * Process disappear of overview
1944 * @param restoreCursorLoc A flag to restore cursor location
1946 protected void overviewDisappear(boolean restoreCursorLoc
) {
1947 if (fOverview
== null) {
1950 fOverview
.setVisible(false);
1951 fCornerControl
.setCursor(null);
1952 if (restoreCursorLoc
) {
1953 getDisplay().setCursorLocation(fSaveCursorX
, fSaveCursorY
);
1955 fOverview
.dispose();
1960 * Process mouse move in overview
1961 * @param event The mouse event
1963 protected void overviewMove(MouseEvent event
) {
1964 Point p
= toGlobalCoordinates(fCornerControl
, event
.x
, event
.y
);
1965 int dx
= p
.x
- fSaveCursorX
;
1966 int dy
= p
.y
- fSaveCursorY
;
1967 overviewMove(dx
, dy
, event
);
1971 * Process mouse move event when overviewing
1973 * @param dx The x coordinates delta
1974 * @param dy The y coordinates delta
1975 * @param event The typed event
1977 protected void overviewMove(int dx
, int dy
, TypedEvent event
) {
1978 boolean ctrl
= false;
1979 boolean shift
= false;
1981 if (event
instanceof MouseEvent
) {
1982 MouseEvent e
= (MouseEvent
) event
;
1983 getDisplay().setCursorLocation(fSaveCursorX
, fSaveCursorY
);
1984 ctrl
= (e
.stateMask
& SWT
.CONTROL
) != 0;
1985 shift
= (e
.stateMask
& SWT
.SHIFT
) != 0;
1986 } else if (event
instanceof KeyEvent
) {
1987 KeyEvent e
= (KeyEvent
) event
;
1988 ctrl
= (e
.stateMask
& SWT
.CONTROL
) != 0;
1989 shift
= (e
.stateMask
& SWT
.SHIFT
) != 0;
1992 int cx
= fContentsX
;
1993 int cy
= fContentsY
;
1994 float fx
= fOverviewFactorX
;
1995 float fy
= fOverviewFactorY
;
1997 if (ctrl
&& shift
) {
1998 if ((fx
* 0.25f
> 1) && (fy
* 0.25 > 1)) {
2011 scrollBy((int) (fx
* dx
), (int) (fy
* dy
));
2012 if (cx
!= fContentsX
|| cy
!= fContentsY
) {
2014 fOverview
.update(); // draw now !
2019 * Convert overview coordinates to global coordinates.
2022 * the control reference
2024 * The x coordinate to convert
2026 * The y coordinate to convert
2027 * @return The new converted Point
2029 protected Point
toGlobalCoordinates(Control loc
, int x
, int y
) {
2030 Point p
= new Point(x
, y
);
2031 for (Control c
= loc
; c
!= null; c
= c
.getParent()) {
2032 // control might have client area with 'decorations'
2033 int trimX
= 0, trimY
= 0;
2034 // other kind of widget with trimming ??
2035 if (c
instanceof Scrollable
) {
2036 Scrollable s
= (Scrollable
) c
;
2037 Rectangle rr
= s
.getClientArea();
2038 Rectangle tr
= s
.computeTrim(rr
.x
, rr
.y
, rr
.width
, rr
.height
);
2039 trimX
= rr
.x
- tr
.x
;
2040 trimY
= rr
.y
- tr
.y
;
2042 p
.x
+= c
.getLocation().x
+ trimX
;
2043 p
.y
+= c
.getLocation().y
+ trimY
;