1 /**********************************************************************
2 * Copyright (c) 2005, 2008 IBM Corporation and others.
3 * Copyright (c) 2011, 2012 Ericsson.
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * which accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
11 * IBM - Initial API and implementation
12 * Bernd Hufmann - Updated for TMF
13 **********************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
;
16 import java
.util
.Timer
;
17 import java
.util
.TimerTask
;
19 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.util
.SDMessages
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.events
.ControlListener
;
22 import org
.eclipse
.swt
.events
.FocusEvent
;
23 import org
.eclipse
.swt
.events
.FocusListener
;
24 import org
.eclipse
.swt
.events
.KeyEvent
;
25 import org
.eclipse
.swt
.events
.KeyListener
;
26 import org
.eclipse
.swt
.events
.MouseEvent
;
27 import org
.eclipse
.swt
.events
.MouseListener
;
28 import org
.eclipse
.swt
.events
.MouseMoveListener
;
29 import org
.eclipse
.swt
.events
.MouseTrackListener
;
30 import org
.eclipse
.swt
.events
.PaintEvent
;
31 import org
.eclipse
.swt
.events
.PaintListener
;
32 import org
.eclipse
.swt
.events
.SelectionEvent
;
33 import org
.eclipse
.swt
.events
.SelectionListener
;
34 import org
.eclipse
.swt
.events
.TypedEvent
;
35 import org
.eclipse
.swt
.graphics
.Color
;
36 import org
.eclipse
.swt
.graphics
.Cursor
;
37 import org
.eclipse
.swt
.graphics
.GC
;
38 import org
.eclipse
.swt
.graphics
.ImageData
;
39 import org
.eclipse
.swt
.graphics
.PaletteData
;
40 import org
.eclipse
.swt
.graphics
.Point
;
41 import org
.eclipse
.swt
.graphics
.RGB
;
42 import org
.eclipse
.swt
.graphics
.Rectangle
;
43 import org
.eclipse
.swt
.widgets
.Button
;
44 import org
.eclipse
.swt
.widgets
.Canvas
;
45 import org
.eclipse
.swt
.widgets
.Composite
;
46 import org
.eclipse
.swt
.widgets
.Control
;
47 import org
.eclipse
.swt
.widgets
.Display
;
48 import org
.eclipse
.swt
.widgets
.Layout
;
49 import org
.eclipse
.swt
.widgets
.ScrollBar
;
50 import org
.eclipse
.swt
.widgets
.Scrollable
;
51 import org
.eclipse
.swt
.widgets
.Shell
;
54 * ScrollView widget provides a scrolling area with on-demand scroll bars.
55 * Overview scrollable panel can be used (@see setOverviewEnabled()).
57 * @author Eric Miravete
60 public class ScrollView
extends Composite
{
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
66 // Value for scroll bar mode, default is AUTO
68 * Scroll bar mode AUTO
70 public static final int AUTO
= 0;
72 * Scroll bar mode ALWAYS_ON
74 public static final int ALWAYS_ON
= 1;
76 * Scroll bar mode ALWAYS_OFF
78 public static final int ALWAYS_OFF
= 2;
80 * Bit mask for visible vertical scroll bar
82 public static final int VBAR
= 0x01;
84 * Bit mask for visible horizontal scroll bar
86 public static final int HBAR
= 0x02;
88 * Value of the contents height property.
90 protected int fContentsHeight
= 0;
92 * Value of the contents width property.
94 protected int fContentsWidth
= 0;
96 * Value of the contents x coordinate property
98 protected int fContentsX
= 0;
100 * Value of the contents y coordinate property
102 protected int fContentsY
= 0;
104 * Scroll bar mode of horizontal scroll bar.
106 protected int fHorScrollbarMode
= AUTO
;
108 * Scroll bar mode of vertical scroll bar.
110 protected int fVertScrollbarMode
= AUTO
;
112 * Increment for the horizontal scroll bar.
114 protected int fHorScrollbarIncrement
= 10;
116 * Increment for the vertical scroll bar.
118 protected int fVertScrollbarIncrement
= 10;
120 * Flag whether auto scroll is enabled or not.
122 protected boolean fAutoScrollEnabled
= true;
124 * Value of the auto scroll period.
126 protected int fAutoScrollPeriod
= 75;
128 * The local paint listener reference.
130 protected PaintListener fLocalPaintListener
= null;
132 * The local mouse move listener reference.
134 protected MouseMoveListener fLocalMouseMoveListener
= null;
136 * The local mouse listener reference.
138 protected MouseListener fLocalMouseListener
= null;
140 * The local control listener reference.
142 protected ControlListener fLocalControlListener
= null;
144 * The local key listener reference.
146 protected KeyListener fLocalKeyListener
= null;
147 // Canvas for vertical/horizontal Scroll Bar only ... because new ScrollBar() does works.
149 * Canvas for horizontal scroll bar.
151 protected Canvas fHorScrollBar
;
153 * Canvas for vertical scroll bar.
155 protected Canvas fVertScrollBar
;
157 * Canvas for the view control.
159 protected Canvas fViewControl
;
161 * Control used in the bottom right corner @see setCornerControl() and @see setOverviewEnabled(true)
163 protected Control fCornerControl
;
165 * Size of overview widget.
167 protected int fOverviewSize
= 100; // default size for overview
169 * Timer for auto_scroll feature
171 protected AutoScroll fAutoScroll
= null;
173 * TimerTask for auto_scroll feature !=null when auto scroll is running
175 protected Timer fAutoScrollTimer
= null;
177 * where mouse down appear on contents area (x coordinate)
179 protected int fMouseDownX
= -1;
181 * where mouse down appear on contents area (y coordinate)
183 protected int fMousDownY
= -1;
186 // ------------------------------------------------------------------------
188 // ------------------------------------------------------------------------
191 * Create a ScrollView, child of composite c. Both scroll bar have the mode AUTO. Auto scroll feature is enabled
192 * using a delay of 250ms. Overview feature is not enabled by default (use setOverviewEnabled()).
194 * @param c The parent composite
195 * @param style The SWT style bits @see SWT
197 public ScrollView(Composite c
, int style
) {
198 this(c
, style
, true);
202 * Create a ScrollView, child of composite c. Both scroll bar have the mode AUTO. Auto scroll feature is enabled
203 * using a delay of 250ms. Overview feature is not enabled by default (use setOverviewEnabled()).
205 * @param c The parent composite.
206 * @param style The SWT style bits @see SWT
207 * @param mouseWheel Flag to force scrollView to handles mouse wheel
209 public ScrollView(Composite c
, int style
, boolean mouseWheel
) {
210 super(c
, SWT
.NONE
); // style&(~(SWT.H_SCROLL|SWT.V_SCROLL)));
212 fHorScrollBar
= new Canvas(this, SWT
.H_SCROLL
);
214 // force scroll bar to get mouse wheel, those scrollbar will be hidden
215 fViewControl
= new Canvas(this, style
| SWT
.H_SCROLL
| SWT
.V_SCROLL
);
217 fViewControl
= new Canvas(this, style
);
219 fViewControl
.setBackground(getBackground());
220 // hide scroll bar as their are replaced by fHorScrollBar and fVertScrollBar.
222 fViewControl
.getVerticalBar().setVisible(false);
223 fViewControl
.getHorizontalBar().setVisible(false);
225 fVertScrollBar
= new Canvas(this, SWT
.V_SCROLL
);
226 // make fVerScrollBar able to receive mouse wheel
227 // does not help as we can't set a MouseListener on fVerScrollBar.getVerticalBar()
228 // to set focus on viewcontrol_
229 // fVerScrollBar.addKeyListener( new KeyAdapter() {});
231 setLayout(new SVLayout());
233 fLocalPaintListener
= new PaintListener() {
235 public void paintControl(PaintEvent event
) {
236 // use clipping, to reduce cost of paint.
237 Rectangle r
= event
.gc
.getClipping();
238 int cx
= viewToContentsX(r
.x
);
239 int cy
= viewToContentsY(r
.y
);
240 drawContents(event
.gc
, cx
, cy
, r
.width
, r
.height
);
243 fViewControl
.addPaintListener(fLocalPaintListener
);
245 fLocalMouseMoveListener
= new MouseMoveListener() {
247 public void mouseMove(MouseEvent e
) {
248 int ox
= e
.x
, oy
= e
.y
;
249 e
.x
= viewToContentsX(e
.x
);
250 e
.y
= viewToContentsY(e
.y
);
251 contentsMouseMoveEvent(e
);
257 fViewControl
.addMouseMoveListener(fLocalMouseMoveListener
);
259 MouseTrackListener localMouseTrackListener
= new MouseTrackListener() {
261 public void mouseEnter(MouseEvent e
) {
262 int ox
= e
.x
, oy
= e
.y
;
263 e
.x
= viewToContentsX(e
.x
);
264 e
.y
= viewToContentsY(e
.y
);
265 contentsMouseEnter(e
);
271 public void mouseHover(MouseEvent e
) {
272 int ox
= e
.x
, oy
= e
.y
;
273 e
.x
= viewToContentsX(e
.x
);
274 e
.y
= viewToContentsY(e
.y
);
275 contentsMouseHover(e
);
281 public void mouseExit(MouseEvent e
) {
282 int ox
= e
.x
, oy
= e
.y
;
283 e
.x
= viewToContentsX(e
.x
);
284 e
.y
= viewToContentsY(e
.y
);
285 contentsMouseExit(e
);
292 fViewControl
.addMouseTrackListener(localMouseTrackListener
);
294 fLocalMouseListener
= new MouseListener() {
296 public void mouseDoubleClick(MouseEvent e
) {
297 int ox
= e
.x
, oy
= e
.y
;
298 e
.x
= viewToContentsX(e
.x
);
299 e
.y
= viewToContentsY(e
.y
);
300 contentsMouseDoubleClickEvent(e
);
306 public void mouseDown(MouseEvent e
) {
307 int ox
= e
.x
, oy
= e
.y
;
308 e
.x
= fMouseDownX
= viewToContentsX(e
.x
);
309 e
.y
= fMousDownY
= viewToContentsY(e
.y
);
310 contentsMouseDownEvent(e
);
316 public void mouseUp(MouseEvent e
) {
317 int ox
= e
.x
, oy
= e
.y
;
318 e
.x
= viewToContentsX(e
.x
);
319 e
.y
= viewToContentsY(e
.y
);
320 contentsMouseUpEvent(e
);
323 // here because class extenting me can catch mouse Up and want to scroll...
324 fMouseDownX
= fMousDownY
= -1;
327 fViewControl
.addMouseListener(fLocalMouseListener
);
329 fLocalKeyListener
= new KeyListener() {
331 public void keyPressed(KeyEvent e
) {
336 public void keyReleased(KeyEvent e
) {
341 fViewControl
.addKeyListener(fLocalKeyListener
);
343 getVerticalBar().addSelectionListener(new SelectionListener() {
345 public void widgetSelected(SelectionEvent e
) {
346 setContentsPos(fContentsX
, getVerticalBar().getSelection());
347 // need to change "hidden" vertical bar value ?
348 // force focus on fViewControl so we got future mouse wheel's scroll events
349 if (!fViewControl
.isFocusControl()) {
350 fViewControl
.setFocus();
355 public void widgetDefaultSelected(SelectionEvent e
) {
359 if (fViewControl
.getVerticalBar() != null) {
360 // add fViewControl hidden scrollbar listener to get mouse wheel ...
361 fViewControl
.getVerticalBar().addSelectionListener(new SelectionListener() {
363 public void widgetSelected(SelectionEvent e
) {
364 ScrollBar b
= fViewControl
.getVerticalBar();
365 setContentsPos(fContentsX
, b
.getSelection());
366 // change "real" vertical bar selection too
367 getVerticalBar().setSelection(b
.getSelection());
371 public void widgetDefaultSelected(SelectionEvent e
) {
375 getHorizontalBar().addSelectionListener(new SelectionListener() {
377 public void widgetSelected(SelectionEvent e
) {
378 setContentsPos(getHorizontalBar().getSelection(), fContentsY
);
379 // need to change "real" horizontal bar too ?
380 // force focus on fViewControl so we got future mouse wheel's scroll events
381 if (!fViewControl
.isFocusControl()) {
382 fViewControl
.setFocus();
387 public void widgetDefaultSelected(SelectionEvent e
) {
390 if (fViewControl
.getHorizontalBar() != null) {
391 fViewControl
.getHorizontalBar().addSelectionListener(new SelectionListener() {
393 public void widgetSelected(SelectionEvent e
) {
394 ScrollBar b
= fViewControl
.getHorizontalBar();
395 setContentsPos(b
.getSelection(), fContentsY
);
396 // change "real" vertical bar selection too
397 getHorizontalBar().setSelection(b
.getSelection());
401 public void widgetDefaultSelected(SelectionEvent e
) {
407 // ------------------------------------------------------------------------
409 // ------------------------------------------------------------------------
413 * @see org.eclipse.swt.widgets.Composite#setFocus()
416 public boolean setFocus() {
417 return fViewControl
.forceFocus();
422 * @see org.eclipse.swt.widgets.Control#setCursor(org.eclipse.swt.graphics.Cursor)
425 public void setCursor(Cursor cursor
) {
426 fViewControl
.setCursor(cursor
);
430 * Dispose controls used in scroll view
433 * @see org.eclipse.swt.widgets.Widget#dispose()
436 public void dispose() {
437 if (fAutoScroll
!= null) {
438 fAutoScroll
.cancel();
441 if (fViewControl
!= null) {
442 fViewControl
.dispose();
445 if (fVertScrollBar
!= null) {
446 fVertScrollBar
.dispose();
448 fVertScrollBar
= null;
449 if (fHorScrollBar
!= null) {
450 fHorScrollBar
.dispose();
452 fHorScrollBar
= null;
453 if (fCornerControl
!= null) {
454 Object data
= fCornerControl
.getData();
455 if (data
instanceof Overview
) {
456 ((Overview
) data
).dispose();
458 fCornerControl
.dispose();
459 fCornerControl
= null;
466 * @see org.eclipse.swt.widgets.Composite#getClientArea()
469 public Rectangle
getClientArea() {
470 return fViewControl
.getClientArea();
475 * @see org.eclipse.swt.widgets.Control#setBackground(org.eclipse.swt.graphics.Color)
478 public void setBackground(Color c
) {
479 super.setBackground(c
);
480 fViewControl
.setBackground(c
);
485 * @see org.eclipse.swt.widgets.Control#setToolTipText(java.lang.String)
488 public void setToolTipText(String text
) {
489 fViewControl
.setToolTipText(text
);
493 * Draw overview area, @see setOverviewEnabled. By default draw a rectangle corresponding to the visible area of
494 * scroll view. You can redefine this method to draw the contents as drawContents does... ...in an other magnify
497 * @param gc GC to used to draw.
498 * @param r Rectangle corresponding to the client area of overview.
500 protected void drawOverview(GC gc
, Rectangle r
) {
501 int x
= (int) (r
.width
* fContentsX
/ (float) fContentsWidth
);
502 int y
= (int) (r
.height
* fContentsY
/ (float) fContentsHeight
);
503 int vw
= getVisibleWidth();
504 int vh
= getVisibleHeight();
506 if (fContentsWidth
> vw
) {
507 w
= (int) (r
.width
* vw
/ (float) fContentsWidth
);
509 int h
= r
.height
- 1;
510 if (fContentsHeight
> vh
) {
511 h
= (int) (r
.height
* vh
/ (float) fContentsHeight
);
514 gc
.setForeground(getForeground());
515 // too small rectangle ?
516 if (w
< 5 || h
< 5) {
518 gc
.drawLine(x
, 0, x
, r
.height
);
519 gc
.drawLine(0, y
, r
.width
, y
);
521 gc
.drawRectangle(x
, y
, w
, h
);
526 * Remove the local Listener and add the new listener.
528 * @param nlistener the new listener
530 public void replaceControlListener(ControlListener nlistener
) {
531 if (fLocalControlListener
!= null) {
532 removeControlListener(fLocalControlListener
);
533 fLocalControlListener
= null;
535 addControlListener(nlistener
);
539 * Remove the local Listener and add the new listener.
541 * @param nlistener the new listener
543 public void replaceKeyListener(KeyListener nlistener
) {
544 if (fLocalKeyListener
!= null) {
545 removeKeyListener(fLocalKeyListener
);
546 fLocalKeyListener
= null;
548 addKeyListener(nlistener
);
552 * Remove the local Listener and add the new listener.
554 * @param nlistener the new listener
556 public void replaceMouseListener(MouseListener nlistener
) {
557 if (fLocalMouseListener
!= null) {
558 removeMouseListener(fLocalMouseListener
);
559 fLocalMouseListener
= null;
561 fViewControl
.addMouseListener(nlistener
);
565 * Remove the local Listener and add the new listener.
567 * @param nlistener the new listener
569 public void replaceMouseMoveListener(MouseMoveListener nlistener
) {
570 if (fLocalMouseMoveListener
!= null) {
571 removeMouseMoveListener(fLocalMouseMoveListener
);
572 fLocalMouseMoveListener
= null;
574 fViewControl
.addMouseMoveListener(nlistener
);
578 * Remove the local Listener and add the new listener.
580 * @param nlistener the new listener
582 public void replacePaintListener(PaintListener nlistener
) {
583 if (fLocalPaintListener
!= null) {
584 removePaintListener(fLocalPaintListener
);
585 fLocalPaintListener
= null;
587 fViewControl
.addPaintListener(nlistener
);
591 * Access method for the contentsHeight property.
593 * @return the current value of the contentsHeight property
595 public int getContentsHeight() {
596 return fContentsHeight
;
600 * Access method for the contentsWidth property.
602 * @return the current value of the contentsWidth property
604 public int getContentsWidth() {
605 return fContentsWidth
;
609 * Access method for the contentsX property.
611 * @return the current value of the contentsX property
613 public int getContentsX() {
618 * Access method for the contentsY property.
620 * @return the current value of the contentsY property
622 public int getContentsY() {
627 * Determines if the dragAutoScroll property is true.
629 * @return <code>true<code> if the dragAutoScroll property is true
631 public boolean isDragAutoScroll() {
632 return fAutoScrollEnabled
;
636 * Sets the value of the dragAutoScroll property.
638 * @param aDragAutoScroll the new value of the dragAutoScroll property
640 public void setDragAutoScroll(boolean aDragAutoScroll
) {
641 fAutoScrollEnabled
= aDragAutoScroll
;
642 if (!fAutoScrollEnabled
&& (fAutoScroll
!= null)) {
643 fAutoScroll
.cancel();
649 * Change delay (in millisec) used for auto scroll feature.
651 * @param period new period between to auto scroll
653 public void setDragAutoScrollPeriod(int period
) {
654 fAutoScrollPeriod
= Math
.max(0, period
);
658 * Return auto scroll period.
662 public int getDragAutoScrollPeriod() {
663 return fAutoScrollPeriod
;
667 * Access method for the hScrollBarMode property.
669 * @return the current value of the hScrollBarMode property
671 public int getHScrollBarMode() {
672 return fHorScrollbarMode
;
676 * Sets the value of the hScrollBarMode property.
678 * @param aHScrollBarMode the new value of the hScrollBarMode property
680 public void setHScrollBarMode(int aHScrollBarMode
) {
681 fHorScrollbarMode
= aHScrollBarMode
;
685 * Access method for the vScrollBarMode property.
687 * @return the current value of the vScrollBarMode property
689 public int getVScrollBarMode() {
690 return fVertScrollbarMode
;
694 * Sets the value of the vScrollBarMode property.
696 * @param aVScrollBarMode the new value of the vScrollBarMode property
698 public void setVScrollBarMode(int aVScrollBarMode
) {
699 fVertScrollbarMode
= aVScrollBarMode
;
703 * Return horizontal scroll bar increment, default:1
705 * @return The increment
707 public int getHScrollBarIncrement() {
708 return fHorScrollbarIncrement
;
712 * Return vertical scroll bar increment, default:1
714 * @return The increment
716 public int getVScrollBarIncrement() {
717 return fVertScrollbarIncrement
;
721 * Change horizontal scroll bar increment, minimum:1. Page increment is
722 * always set to visible width.
725 * Increment value to set
727 public void setHScrollBarIncrement(int inc
) {
728 fHorScrollbarIncrement
= Math
.max(1, inc
);
732 * Change vertical scroll bar increment, minimum:1. Page increment is always
733 * set to visible height.
736 * Increment value to set
738 public void setVScrollBarIncrement(int inc
) {
739 fVertScrollbarIncrement
= Math
.max(1, inc
);
743 * Enable or disable overview feature. Enabling overview, dispose and replace existing corner control by a button.
744 * Clicking in it open overview, move mouse cursor holding button to move scroll view and release button to hide
745 * overview. Tip: hold control and/or shift key while moving mouse when overview is open made fine scroll.
747 * @param value true to engage overview feature
749 public void setOverviewEnabled(boolean value
) {
750 if (isOverviewEnabled() == value
) {
756 Button b
= new Button(this, SWT
.NONE
);
757 b
.setText("+"); //$NON-NLS-1$
758 Overview ovr
= new Overview();
762 b
.setToolTipText(SDMessages
._78
);
764 setCornerControl(cc
);
768 * Change overview size (at ratio 1:1), default is 100
773 public void setOverviewSize(int size
) {
774 fOverviewSize
= Math
.abs(size
);
778 * Returns whether the overview is enabled or not.
780 * @return true is overview feature is enabled
782 public boolean isOverviewEnabled() {
783 if (fCornerControl
instanceof Button
) {
784 Object data
= ((Button
) fCornerControl
).getData();
786 if (data
instanceof Overview
) {
794 * Returns the overview size at ratio 1:1.
796 * @return current overview size at ratio 1:1
798 public int getOverviewSize() {
799 return fOverviewSize
;
803 * Returns control used to display view (might not be this object). Use this control to add/remove listener on the
806 * @return control used to display view (might not be this object).
808 public Control
getViewControl() {
813 * Called when the mouse enter the ScrollView area
818 protected void contentsMouseExit(MouseEvent e
) {
822 * Called when the mouse enter the ScrollView area after and system defined
828 protected void contentsMouseHover(MouseEvent e
) {
832 * Called when the mouse enter the ScrollView area
837 protected void contentsMouseEnter(MouseEvent e
) {
841 * Called when user double on contents area.
846 protected void contentsMouseDoubleClickEvent(MouseEvent e
) {
850 * Called when mouse is on contents area and button is pressed.
855 protected void contentsMouseDownEvent(MouseEvent e
) {
861 * TimerTask for auto scroll feature.
863 protected static class AutoScroll
extends TimerTask
{
871 /** ScrollView object */
872 public ScrollView scrollView
;
878 * ScrollView object to use
884 public AutoScroll(ScrollView sv
, int dx
, int dy
) {
892 Display
.getDefault().asyncExec(new Runnable() {
895 scrollView
.scrollBy(deltaX
, deltaY
);
902 * Called when mouse is on contents area and mode.
907 protected void contentsMouseMoveEvent(MouseEvent event
) {
908 if ((event
.stateMask
& SWT
.BUTTON_MASK
) != 0) {
909 if (!fAutoScrollEnabled
) {
910 scrollBy(-(event
.x
- fMouseDownX
), -(event
.y
- fMousDownY
));
916 int v_right
= getContentsX() + getVisibleWidth();
917 int v_bottom
= getContentsY() + getVisibleHeight();
920 if (event
.x
< getContentsX()) {
921 sx
= (getContentsX() - event
.x
);
922 fMouseDownX
= getContentsX();
923 } else if (event
.x
> v_right
) {
924 sx
= -event
.x
+ v_right
;
925 fMouseDownX
= v_right
;
927 if (event
.y
< getContentsY()) {
928 sy
= (getContentsY() - event
.y
);
929 fMousDownY
= getContentsY();
930 } else if (event
.y
> v_bottom
) {
931 sy
= -event
.y
+ v_bottom
;
932 fMousDownY
= v_bottom
;
935 if (sx
!= 0 || sy
!= 0) {
936 // start auto scroll...
937 if (fAutoScroll
== null) {
938 if (fAutoScrollTimer
== null) {
939 fAutoScrollTimer
= new Timer(true);
941 fAutoScroll
= new AutoScroll(this, sx
, sy
);
942 fAutoScrollTimer
.schedule(fAutoScroll
, 0, fAutoScrollPeriod
);
944 fAutoScroll
.deltaX
= sx
;
945 fAutoScroll
.deltaY
= sy
;
948 if (fAutoScroll
!= null) {
949 fAutoScroll
.cancel();
953 scrollBy(-(event
.x
- fMouseDownX
), -(event
.y
- fMousDownY
));
959 * Called when mouse is on contents area and button is released
964 protected void contentsMouseUpEvent(MouseEvent event
) {
965 // reset auto scroll if it's engaged
966 if (fAutoScroll
!= null) {
967 fAutoScroll
.cancel();
973 * Responsible to draw contents area. At least rectangle clipX must be
974 * redrawn. This rectangle is given in contents coordinates. By default, no
988 protected void drawContents(GC gc
, int clipx
, int clipy
, int clipw
, int cliph
) {
992 * Change the size of the contents area.
994 * @param width new width of the area.
995 * @param height new height of the area.
997 public void resizeContents(int width
, int height
) {
998 int localWidth
= width
;
999 int localHeight
= height
;
1001 if (localWidth
< 0) {
1004 if (localHeight
< 0) {
1008 int oldW
= fContentsWidth
;
1009 int oldH
= fContentsHeight
;
1011 if (localWidth
== oldW
&& localHeight
== oldH
) {
1015 fContentsWidth
= localWidth
;
1016 fContentsHeight
= localHeight
;
1018 if (oldW
> localWidth
) {
1024 int vis_width
= getVisibleWidth();
1025 int vis_height
= getVisibleHeight();
1026 if (oldW
< vis_width
) {
1027 if (localWidth
> vis_width
) {
1028 localWidth
= vis_width
;
1030 fViewControl
.redraw(getContentsX() + oldW
, 0, localWidth
- oldW
, vis_height
, true);
1033 if (oldH
> localHeight
) {
1034 int s
= localHeight
;
1039 if (oldH
< vis_height
) {
1040 if (localHeight
> vis_height
) {
1041 localHeight
= vis_height
;
1043 fViewControl
.redraw(0, getContentsY() + oldH
, vis_width
, localHeight
- oldH
, true);
1045 if (updateScrollBarVisiblity()) {
1048 updateScrollBarsValues();
1052 // redefined for internal use ..
1054 public void redraw() {
1056 // ..need to redraw this already:
1057 fViewControl
.redraw();
1061 * @param delataX The delta in X
1062 * @param deltaY the delta in Y
1064 public void scrollBy(int delataX
, int deltaY
) {
1065 setContentsPos(getContentsX() + delataX
, getContentsY() + deltaY
);
1069 * Scroll to ensure point(in contents coordinates) is visible.
1071 * @param px Point's x position
1072 * @param py Point's y position
1074 public void ensureVisible(int px
, int py
) {
1075 int cx
= getContentsX(), cy
= getContentsY();
1076 int right
= getContentsX() + getVisibleWidth();
1077 int bottom
= getContentsY() + getVisibleHeight();
1078 if (px
< getContentsX()) {
1080 } else if (px
> right
) {
1081 cx
= px
- getVisibleWidth();
1083 if (py
< getContentsY()) {
1085 } else if (py
> bottom
) {
1086 cy
= py
- getVisibleHeight();
1088 setContentsPos(cx
, cy
);
1092 * Make rectangle (x,y,w,h, in contents coordinates) visible. if rectangle cannot be completely visible, use
1095 * @param xValue x contents coordinates of rectangle.
1096 * @param yValue y contents coordinates of rectangle.
1097 * @param width width of rectangle.
1098 * @param height height of rectangle.
1099 * @param align bit or'ed SWT flag like SWT.LEFT,RIGHT,CENTER,TOP,BOTTOM,VERTICAL used only for bigger rectangle
1100 * than visible area. By default CENTER/VERTICAL
1102 public void ensureVisible(int xValue
, int yValue
, int width
, int height
, int align
) {
1103 ensureVisible(xValue
, yValue
, width
, height
, align
, false);
1107 * Make rectangle (xValue,yValue,width,height, in contents coordinates) visible. if rectangle cannot be completely visible, use
1110 * @param xValue x contents coordinates of rectangle.
1111 * @param yValue y contents coordinates of rectangle.
1112 * @param width width of rectangle.
1113 * @param height height of rectangle.
1114 * @param align bit or'ed SWT flag like SWT.LEFT,RIGHT,CENTER,TOP,BOTTOM,VERTICAL used only for bigger rectangle
1115 * than visible area. By default CENTER/VERTICAL
1116 * @param forceAlign force alignment for rectangle smaller than the visible area
1118 protected void ensureVisible(int xValue
, int yValue
, int width
, int height
, int align
, boolean forceAlign
) {
1120 int localX
= xValue
;
1121 int localY
= yValue
;
1122 int localWidth
= width
;
1123 int localHeight
= height
;
1125 if (localWidth
< 0) {
1126 localX
= localX
+ localWidth
;
1127 localWidth
= -localWidth
;
1129 if (localHeight
< 0) {
1130 localY
= localY
+ localHeight
;
1131 localHeight
= -localHeight
;
1133 int hbar
= getHorizontalBarHeight();
1134 int vbar
= getVerticalBarWidth();
1135 int cx
= getContentsX();
1136 int cy
= getContentsY();
1137 int right
= getContentsX() + getVisibleWidth() - vbar
;
1138 int bottom
= getContentsY() + getVisibleHeight() - hbar
;
1139 boolean align_h
= false, align_v
= false;
1141 if (localX
< getContentsX()) {
1143 } else if (localX
+ localWidth
> right
) {
1144 cx
= localX
- localWidth
;
1146 if (localY
< getContentsY()) {
1148 } else if (localY
+ localHeight
> bottom
) {
1149 cy
= localY
- localHeight
;
1152 if (localWidth
> getVisibleWidth()) {
1155 if (localHeight
> getVisibleHeight()) {
1158 // compute alignment on visible area horizontally
1159 if (align_h
|| (forceAlign
&& localX
+ localWidth
> right
)) {
1161 if ((align
& SWT
.LEFT
) != 0) {
1163 } else if ((align
& SWT
.RIGHT
) != 0) {
1164 cx
= right
- localWidth
;
1166 cx
= localX
+ (localWidth
- getVisibleWidth()) / 2;
1169 // compute alignment on visible area vertically
1170 if (align_v
|| (forceAlign
&& localY
+ localHeight
> bottom
)) {
1172 if ((align
& SWT
.TOP
) != 0) {
1174 } else if ((align
& SWT
.BOTTOM
) != 0) {
1175 cy
= bottom
- localHeight
;
1177 cy
= localY
+ (localHeight
- getVisibleHeight()) / 2;
1180 setContentsPos(cx
, cy
);
1184 * Returns true if point is visible (expressed in contents coordinates).
1186 * @param px Point's x position
1187 * @param py Point's y position
1188 * @return true if point is visible (expressed in contents coordinates)
1190 public boolean isVisible(int px
, int py
) {
1191 if (px
< getContentsX()) {
1194 if (py
< getContentsY()) {
1197 if (px
> (getContentsX() + getVisibleWidth())) {
1200 if (py
> (getContentsY() + getVisibleHeight())) {
1207 * Returns true if rectangle if partially visible.
1209 * @param xValue x contents coordinates of rectangle.
1210 * @param yValue y contents coordinates of rectangle.
1211 * @param width width of rectangle.
1212 * @param height height of rectangle.
1213 * @return true if rectangle if partially visible.
1215 public boolean isVisible(int xValue
, int yValue
, int width
, int height
) {
1216 if (xValue
+ width
< getContentsX()) {
1219 if (yValue
+ height
< getContentsY()) {
1222 int vr
= getContentsX() + getVisibleWidth();
1223 int vb
= getContentsY() + getVisibleHeight();
1234 * Returns visible part of rectangle, or null if rectangle is not visible.
1235 * Rectangle is expressed in contents coordinates.
1238 * x contents coordinates of rectangle.
1240 * y contents coordinates of rectangle.
1242 * width of rectangle.
1244 * height of rectangle.
1245 * @return visible part of rectangle, or null if rectangle is not visible.
1247 public Rectangle
getVisiblePart(int xValue
, int yValue
, int width
, int height
) {
1248 if (xValue
+ width
< getContentsX()) {
1251 if (yValue
+ height
< getContentsY()) {
1254 int vr
= getContentsX() + getVisibleWidth();
1255 int vb
= getContentsY() + getVisibleHeight();
1262 int rr
= xValue
+ width
, rb
= yValue
+ height
;
1263 int nl
= Math
.max(xValue
, getContentsX()), nt
= Math
.max(yValue
, getContentsY()), nr
= Math
.min(rr
, vr
), nb
= Math
.min(rb
, vb
);
1264 return new Rectangle(nl
, nt
, nr
- nl
, nb
- nt
);
1268 * Returns the visible part for given rectangle.
1270 * @param rect A rectangle
1272 * @return gets visible part of rectangle (or <code>null</code>)
1274 public final Rectangle
getVisiblePart(Rectangle rect
) {
1278 return getVisiblePart(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
1282 * Change top left position of visible area. Check if the given point is inside contents area.
1284 * @param xValue x contents coordinates of visible area.
1285 * @param yValue y contents coordinates of visible area.
1286 * @return true if view really moves
1288 public boolean setContentsPos(int xValue
, int yValue
) {
1289 int nx
= xValue
, ny
= yValue
;
1290 if (getVisibleWidth() >= getContentsWidth()) {
1295 } else if (xValue
+ getVisibleWidth() > getContentsWidth()) {
1296 nx
= getContentsWidth() - getVisibleWidth();
1299 if (getVisibleHeight() >= getContentsHeight()) {
1304 } else if (yValue
+ getVisibleHeight() > getContentsHeight()) {
1305 ny
= getContentsHeight() - getVisibleHeight();
1309 if (nx
== fContentsX
&& ny
== fContentsY
) {
1314 updateScrollBarsValues();
1315 // ? find smallest area to redraw only them ?
1316 fViewControl
.redraw();
1320 // redefined to return our vertical bar
1323 * @see org.eclipse.swt.widgets.Scrollable#getVerticalBar()
1326 public ScrollBar
getVerticalBar() {
1327 return fVertScrollBar
.getVerticalBar();
1330 // redefined to return out horizontal bar
1333 * @see org.eclipse.swt.widgets.Scrollable#getHorizontalBar()
1336 public ScrollBar
getHorizontalBar() {
1337 return fHorScrollBar
.getHorizontalBar();
1341 * Compute visibility of vertical/horizontal bar using given width/height and current visibility (i.e. is bar size are already in
1343 * @param forWidth width of foreground
1344 * @param forHeight height of foreground
1345 * @param currHorVis The current visibility state of horizontal scroll bar
1346 * @param currVertvis The current visibility state of vertical scroll bar
1347 * @return <code>true</code> if visibility changed else <code>false</code>
1349 public int computeBarVisibility(int forWidth
, int forHeight
, boolean currHorVis
, boolean currVertvis
) {
1351 int localForWidth
= forWidth
;
1353 switch (fVertScrollbarMode
) {
1360 if (getContentsHeight() > forHeight
) {
1362 // v bar size is already in for_width.
1363 if (!currVertvis
) {// (curr_vis&0x01)==0)
1364 localForWidth
-= getVerticalBarWidth();
1372 switch (fHorScrollbarMode
) {
1379 if (getContentsWidth() > localForWidth
) {
1381 // h bar is not in for_height
1382 if ((!currHorVis
) && (getContentsHeight() > (forHeight
- getHorizontalBarHeight()))) {// (curr_vis&0x02)==0 )
1394 * Setup scroll bars visibility.
1396 * @return True if one of visibility changed.
1398 protected boolean updateScrollBarVisiblity() {
1399 boolean change
= false;
1401 boolean currVertVis
= fVertScrollBar
.getVisible();
1402 boolean currHorVis
= fHorScrollBar
.getVisible();
1403 int barNewVis
= computeBarVisibility(getVisibleWidth(), getVisibleHeight(), currHorVis
, currVertVis
);
1404 boolean newVertVis
= (barNewVis
& VBAR
) != 0;
1405 boolean newHorVis
= (barNewVis
& HBAR
) != 0;
1406 if (currVertVis ^ newVertVis
) { // vertsb_.getVisible() )
1407 fVertScrollBar
.setVisible(newVertVis
);
1410 if (currHorVis ^ newHorVis
) {
1411 fHorScrollBar
.setVisible(newHorVis
);
1415 // update corner control visibility:
1416 if (fCornerControl
!= null && change
) {
1417 boolean vis
= newVertVis
|| newHorVis
;
1418 if (vis ^ fCornerControl
.getVisible()) {
1419 fCornerControl
.setVisible(vis
);
1420 change
= true; // but must be already the case
1427 * Setup scroll bar using contents, visible and scroll bar mode properties.
1429 protected void updateScrollBarsValues() {
1430 /* update vertical scrollbar */
1431 ScrollBar b
= getVerticalBar();
1434 b
.setMaximum(getContentsHeight());
1435 b
.setThumb(getVisibleHeight());
1436 b
.setPageIncrement(getVisibleHeight());
1437 b
.setIncrement(fVertScrollbarIncrement
);
1438 b
.setSelection(getContentsY());
1441 // update "hidden" vertical bar too
1442 b
= fViewControl
.getVerticalBar();
1445 b
.setMaximum(getContentsHeight());
1446 b
.setThumb(getVisibleHeight());
1447 b
.setPageIncrement(getVisibleHeight());
1448 b
.setIncrement(fVertScrollbarIncrement
);
1449 b
.setSelection(getContentsY());
1452 /* update horizontal scrollbar */
1453 b
= getHorizontalBar();
1456 b
.setMaximum(getContentsWidth());
1457 b
.setThumb(getVisibleWidth());
1458 b
.setSelection(getContentsX());
1459 b
.setPageIncrement(getVisibleWidth());
1460 b
.setIncrement(fHorScrollbarIncrement
);
1462 // update "hidden" horizontal bar too
1463 b
= fViewControl
.getHorizontalBar();
1466 b
.setMaximum(getContentsWidth());
1467 b
.setThumb(getVisibleWidth());
1468 b
.setSelection(getContentsX());
1469 b
.setPageIncrement(getVisibleWidth());
1470 b
.setIncrement(fHorScrollbarIncrement
);
1475 * Change the control used in the bottom right corner (between two scrollbar), if control is null reset previous
1476 * corner control. This control is visible only if at least one scrollbar is visible. Given control will be disposed
1477 * by ScrollView, at dispose() time, at next setCornetControl() call or when calling setOverviewEnabled(). Pay
1478 * attention calling this reset overview feature until setOverviewEnabled(true) if called.
1479 * @param control The control for the overview
1481 public void setCornerControl(Control control
) {
1482 if (fCornerControl
!= null) {
1483 fCornerControl
.dispose();
1485 fCornerControl
= control
;
1486 if (fCornerControl
!= null) {
1487 ScrollBar vb
= getVerticalBar();
1488 ScrollBar hb
= getHorizontalBar();
1489 boolean vis
= vb
.getVisible() || hb
.getVisible();
1490 fCornerControl
.setVisible(vis
);
1495 * Transform (x,y) point in widget coordinates to contents coordinates.
1497 * @param x The x widget coordinate.
1498 * @param y The y widget coordinate.
1499 * @return org.eclipse.swt.graphics.Point with content coordinates.
1501 public final Point
viewToContents(int x
, int y
) {
1502 return new Point(viewToContentsX(x
), viewToContentsY(y
));
1506 * Transform x in widget coordinates to contents coordinates
1508 * @param x The y widget coordinate.
1509 * @return the x content coordinate.
1511 public int viewToContentsX(int x
) {
1512 return fContentsX
+ x
;
1516 * Transform y in widget coordinates to contents coordinates
1518 * @param y The y widget coordinate.
1519 * @return the y content coordinate.
1521 public int viewToContentsY(int y
) {
1522 return fContentsY
+ y
;
1526 * Transform (x,y) point from contents coordinates, to widget coordinates.
1528 * @param x The x content coordinate.
1529 * @param y The y content coordinate.
1530 * @return coordinates widget area as.
1532 public final Point
contentsToView(int x
, int y
) {
1533 return new Point(contentsToViewX(x
), contentsToViewY(y
));
1537 * Transform X axis coordinates from contents to widgets.
1539 * @param x contents coordinate to transform.
1540 * @return x coordinate in widget area
1542 public int contentsToViewX(int x
) {
1543 return x
- fContentsX
;
1547 * Transform Y axis coordinates from contents to widgets.
1549 * @param y contents coordinate to transform
1550 * @return y coordinate in widget area
1552 public int contentsToViewY(int y
) {
1553 return y
- fContentsY
;
1557 * Return the visible height of scroll view, might be > contentsHeight
1559 * @return the visible height of scroll view, might be > contentsHeight()
1561 public int getVisibleHeight() {
1562 return fViewControl
.getClientArea().height
;
1566 * Return int the visible width of scroll view, might be > contentsWidth().
1568 * @return int the visible width of scroll view, might be > contentsWidth()
1570 public int getVisibleWidth() {
1571 return fViewControl
.getClientArea().width
;
1575 * Add support for arrow key, scroll the ... scroll view. But you can
1576 * redefine this method for your convenience.
1581 protected void keyPressedEvent(KeyEvent event
) {
1582 switch (event
.keyCode
) {
1584 scrollBy(0, -getVisibleHeight());
1586 case SWT
.ARROW_DOWN
:
1587 scrollBy(0, +getVisibleHeight());
1589 case SWT
.ARROW_LEFT
:
1590 scrollBy(-getVisibleWidth(), 0);
1592 case SWT
.ARROW_RIGHT
:
1593 scrollBy(+getVisibleWidth(), 0);
1601 * Redefine this method at your convenience
1603 * @param event The key event.
1605 protected void keyReleasedEvent(KeyEvent event
) {
1609 * Returns vertical bar width, even if bar isn't visible.
1611 * @return vertical bar width, even if bar isn't visible
1613 public int getVerticalBarWidth() {
1614 // include vertical bar width and trimming of scrollable used
1615 int bw
= fVertScrollBar
.computeTrim(0, 0, 0, 0).width
;
1620 * Returns horizontal bar height even if bar isn't visible.
1622 * @return horizontal bar height even if bar isn't visible
1624 public int getHorizontalBarHeight() {
1625 // include horiz. bar height and trimming of scrollable used
1626 int bh
= fHorScrollBar
.computeTrim(0, 0, 0, 0).height
;
1627 // +1 because win32 H.bar need 1 pixel canvas size to appear ! (strange no ?)
1633 * @see org.eclipse.swt.widgets.Scrollable#computeTrim(int, int, int, int)
1636 public Rectangle
computeTrim(int x
, int y
, int w
, int h
) {
1637 Rectangle r
= new Rectangle(x
, y
, w
, h
);
1638 int bar_vis
= computeBarVisibility(w
, h
, false, false);
1639 if ((bar_vis
& VBAR
) != 0) {
1640 r
.width
+= getVerticalBarWidth();
1642 if ((bar_vis
& HBAR
) != 0) {
1643 r
.height
+= getHorizontalBarHeight();
1649 * Internal layout for ScrollView, handle scrollbars, drawzone and corner control
1651 protected class SVLayout
extends Layout
{
1657 * The do-it-not flag
1659 boolean dontLayout
= false;
1663 * @see org.eclipse.swt.widgets.Layout#computeSize(org.eclipse.swt.widgets.Composite, int, int, boolean)
1666 protected Point
computeSize(Composite composite
, int wHint
, int hHint
, boolean flushCache
) {
1667 Point p
= new Point(250, 250);
1668 if (fContentsWidth
< p
.x
) {
1669 p
.x
= fContentsWidth
;
1671 if (fContentsHeight
< p
.y
) {
1672 p
.y
= fContentsHeight
;
1679 * @see org.eclipse.swt.widgets.Layout#layout(org.eclipse.swt.widgets.Composite, boolean)
1682 protected void layout(Composite composite
, boolean flushCache
) {
1691 Point cs
= composite
.getSize();
1692 int bar_vis
= computeBarVisibility(cs
.x
, cs
.y
, false, false);
1693 boolean vb_vis
= (bar_vis
& VBAR
) != 0;
1694 boolean hb_vis
= (bar_vis
& HBAR
) != 0;
1695 fVertScrollBar
.setVisible(vb_vis
);
1696 fHorScrollBar
.setVisible(hb_vis
);
1697 int vbw
= getVerticalBarWidth();
1698 int hbh
= getHorizontalBarHeight();
1699 int wb
= vb_vis ? vbw
: 0;
1700 int hb
= hb_vis ? hbh
: 0;
1701 int cww
= 0, cwh
= 0;
1703 if (fCornerControl
!= null && (vb_vis
|| hb_vis
)) { // corner_control_.getVisible())
1704 fCornerControl
.setVisible(true);
1713 } else if (vb_vis
&& hb_vis
) {
1714 if (fCornerControl
!= null) {
1715 fCornerControl
.setVisible(false);
1720 if (vb_vis
|| hb_vis
) {
1721 updateScrollBarsValues();
1724 int vw
= cs
.x
- (vb_vis ? vbw
: 0);
1725 int vh
= cs
.y
- (hb_vis ? hbh
: 0);
1726 int vbx
= cs
.x
- wb
;
1727 int hby
= cs
.y
- hb
;
1729 fViewControl
.setBounds(0, 0, vw
, vh
);
1732 fVertScrollBar
.setBounds(vbx
, 0, wb
, cs
.y
- cwh
);
1735 fHorScrollBar
.setBounds(0, hby
, cs
.x
- cww
, hb
);
1737 if (fCornerControl
!= null && fCornerControl
.getVisible()) {
1738 fCornerControl
.setBounds(vbx
, hby
, vbw
, hbh
);
1740 updateScrollBarsValues();
1749 // static must take place here... cursor is created once.
1750 volatile static Cursor fOverviewCursor
;
1752 /** Support for click-and-see overview shell on this ScrollView */
1753 protected class Overview
{
1756 * factor for X from real and overview sizes, for mouse move speed.
1758 protected float fOverviewFactorX
;
1761 * factor for Y from real and overview sizes, for mouse move speed.
1763 protected float fOverviewFactorY
;
1765 * shell use to show overview
1767 protected Shell fOverview
;
1769 * save mouse X cursor location for disappear();
1771 protected int fSaveCursorX
;
1773 * save mouse Y cursor location for disappear();
1775 protected int fSaveCursorY
;
1778 * Apply overview support on a control. Replace existing corner_widget
1781 * The control to use
1783 public void useControl(Control control
) {
1784 final Point pos
= control
.getLocation();
1785 control
.addMouseListener(new MouseListener() {
1787 public void mouseDoubleClick(MouseEvent e
) {
1791 public void mouseDown(MouseEvent e
) {
1792 overviewAppear(e
.x
, e
.y
);
1796 public void mouseUp(MouseEvent e
) {
1797 overviewDisappear();
1801 control
.addFocusListener(new FocusListener() {
1804 public void focusGained(FocusEvent e
) {
1808 public void focusLost(FocusEvent e
) {
1809 if (overviewing()) {
1810 overviewDisappear(false);
1815 control
.addKeyListener(new KeyListener() {
1818 public void keyPressed(KeyEvent event
) {
1819 if (event
.keyCode
== 32 && !overviewing()) {
1820 overviewAppear(pos
.x
, pos
.y
);
1821 } else if (event
.keyCode
== 32) {
1822 overviewDisappear();
1824 if (event
.keyCode
== SWT
.ARROW_DOWN
) {
1825 overviewMove(0, 1, event
);
1828 if (event
.keyCode
== SWT
.ARROW_UP
) {
1829 overviewMove(0, -1, event
);
1832 if (event
.keyCode
== SWT
.ARROW_RIGHT
) {
1833 overviewMove(1, 0, event
);
1836 if (event
.keyCode
== SWT
.ARROW_LEFT
) {
1837 overviewMove(-1, 0, event
);
1842 public void keyReleased(KeyEvent e
) {
1845 control
.addMouseMoveListener(new MouseMoveListener() {
1846 private int refReshCount
= 0;
1848 public void mouseMove(MouseEvent event
) {
1849 if (overviewing()) {
1850 // Slow down the refresh
1851 if (refReshCount
% 4 == 0) {
1852 overviewMove(event
);
1861 * Dispose controls of overview
1863 public void dispose() {
1864 if (fOverview
!= null) {
1865 fOverview
.dispose();
1870 * @return true if overview is currently on screen
1872 protected boolean overviewing() {
1873 return (fOverview
!= null && fOverview
.isVisible());
1877 * Process overview appear
1884 protected void overviewAppear(int mx
, int my
) {
1885 if (fOverview
== null) {
1886 fOverview
= new Shell(getShell(), SWT
.ON_TOP
| SWT
.NO_BACKGROUND
);
1887 fOverview
.addPaintListener(new PaintListener() {
1889 public void paintControl(PaintEvent e
) {
1890 drawOverview(e
.gc
, fOverview
.getClientArea());
1894 // always the same..
1895 // overview.setBackground( viewcontrol_.getBackground() );
1896 fOverview
.setForeground(fViewControl
.getForeground());
1898 // get location of shell (in screeen coordinates)
1899 Point p
= toGlobalCoordinates(fCornerControl
, 0, 0);
1903 w
= h
= fOverviewSize
;
1904 Rectangle scr
= getDisplay().getBounds();
1905 Point ccs
= fCornerControl
.getSize();
1907 if (fContentsWidth
> fContentsHeight
) {
1908 float ratio
= fContentsHeight
/ (float) fContentsWidth
;
1909 h
= (int) (w
* ratio
);
1912 } else if (h
>= scr
.height
/ 2) {
1916 float ratio
= fContentsWidth
/ (float) fContentsHeight
;
1917 w
= (int) (h
* ratio
);
1920 } else if (w
>= scr
.width
/ 2) {
1924 fOverviewFactorX
= fContentsWidth
/ (float) w
;
1925 fOverviewFactorY
= fContentsHeight
/ (float) h
;
1927 // no contents size set ?
1928 catch (java
.lang
.ArithmeticException e
) {
1931 // try pop-up on button, extending to bottom right,
1932 // if outside screen, extend pop-up to top left
1933 // if( x+w > scr.width ) x = scr.width-w; //x += corner_control_.getSize().x-w;
1934 // if( y+h > scr.height ) y = scr.height-h;//y += corner_control_.getSize().y-h;
1943 fOverview
.setBounds(x
, y
, w
, h
);
1944 fOverview
.setVisible(true);
1946 // mouse cursor disappear, so set invisible mouse cursor ...
1947 if (fOverviewCursor
== null) {
1948 RGB rgb
[] = { new RGB(0, 0, 0), new RGB(255, 0, 0) };
1949 PaletteData palette
= new PaletteData(rgb
);
1951 byte src
[] = new byte[s
* s
];
1952 byte msk
[] = new byte[s
* s
];
1953 for (int i
= 0; i
< s
* s
; ++i
) {
1954 src
[i
] = (byte) 0xFF;
1956 ImageData i_src
= new ImageData(s
, s
, 1, palette
, 1, src
);
1957 ImageData i_msk
= new ImageData(s
, s
, 1, palette
, 1, msk
);
1958 fOverviewCursor
= new Cursor(null, i_src
, i_msk
, 0, 0);
1960 fCornerControl
.setCursor(fOverviewCursor
);
1961 // convert to global coordinates
1962 p
= toGlobalCoordinates(fCornerControl
, mx
, my
);
1966 Rectangle r
= fOverview
.getClientArea();
1967 int cx
= (int) (r
.width
* fContentsX
/ (float) fContentsWidth
);
1968 int cy
= (int) (r
.height
* fContentsY
/ (float) fContentsHeight
);
1970 // cx,cy to display's global coordinates
1971 p
= toGlobalCoordinates(fOverview
.getParent(), cx
, cy
);
1975 * Process disappear of overview
1977 protected void overviewDisappear() {
1978 overviewDisappear(true);
1982 * Process disappear of overview
1983 * @param restoreCursorLoc A flag to restore cursor location
1985 protected void overviewDisappear(boolean restoreCursorLoc
) {
1986 if (fOverview
== null) {
1989 fOverview
.setVisible(false);
1990 fCornerControl
.setCursor(null);
1991 if (restoreCursorLoc
) {
1992 getDisplay().setCursorLocation(fSaveCursorX
, fSaveCursorY
);
1994 fOverview
.dispose();
1999 * Process mouse move in overview
2000 * @param event The mouse event
2002 protected void overviewMove(MouseEvent event
) {
2003 Point p
= toGlobalCoordinates(fCornerControl
, event
.x
, event
.y
);
2004 int dx
= p
.x
- fSaveCursorX
;
2005 int dy
= p
.y
- fSaveCursorY
;
2006 overviewMove(dx
, dy
, event
);
2010 * Process mouse move event when overviewing
2012 * @param dx The x coordinates delta
2013 * @param dy The y coordinates delta
2014 * @param event The typed event
2016 protected void overviewMove(int dx
, int dy
, TypedEvent event
) {
2017 boolean ctrl
= false;
2018 boolean shift
= false;
2020 if (event
instanceof MouseEvent
) {
2021 MouseEvent e
= (MouseEvent
) event
;
2022 getDisplay().setCursorLocation(fSaveCursorX
, fSaveCursorY
);
2023 ctrl
= (e
.stateMask
& SWT
.CONTROL
) != 0;
2024 shift
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2025 } else if (event
instanceof KeyEvent
) {
2026 KeyEvent e
= (KeyEvent
) event
;
2027 ctrl
= (e
.stateMask
& SWT
.CONTROL
) != 0;
2028 shift
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2031 int cx
= fContentsX
;
2032 int cy
= fContentsY
;
2033 float fx
= fOverviewFactorX
;
2034 float fy
= fOverviewFactorY
;
2036 if (ctrl
&& shift
) {
2037 if ((fx
* 0.25f
> 1) && (fy
* 0.25 > 1)) {
2050 scrollBy((int) (fx
* dx
), (int) (fy
* dy
));
2051 if (cx
!= fContentsX
|| cy
!= fContentsY
) {
2053 fOverview
.update(); // draw now !
2058 * Convert overview coordinates to global coordinates.
2061 * the control reference
2063 * The x coordinate to convert
2065 * The y coordinate to convert
2066 * @return The new converted Point
2068 protected Point
toGlobalCoordinates(Control loc
, int x
, int y
) {
2069 Point p
= new Point(x
, y
);
2070 for (Control c
= loc
; c
!= null; c
= c
.getParent()) {
2071 // control might have client area with 'decorations'
2072 int trim_x
= 0, trim_y
= 0;
2073 // other kind of widget with trimming ??
2074 if (c
instanceof Scrollable
) {
2075 Scrollable s
= (Scrollable
) c
;
2076 Rectangle rr
= s
.getClientArea();
2077 Rectangle tr
= s
.computeTrim(rr
.x
, rr
.y
, rr
.width
, rr
.height
);
2078 trim_x
= rr
.x
- tr
.x
;
2079 trim_y
= rr
.y
- tr
.y
;
2081 p
.x
+= c
.getLocation().x
+ trim_x
;
2082 p
.y
+= c
.getLocation().y
+ trim_y
;