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
817 protected void contentsMouseExit(MouseEvent e
) {
821 * Called when the mouse enter the ScrollView area after and system defined time
825 protected void contentsMouseHover(MouseEvent e
) {
829 * Called when the mouse enter the ScrollView area
833 protected void contentsMouseEnter(MouseEvent e
) {
837 * Called when user double on contents area.
841 protected void contentsMouseDoubleClickEvent(MouseEvent e
) {
845 * Called when mouse is on contents area and button is pressed.
849 protected void contentsMouseDownEvent(MouseEvent e
) {
855 * TimerTask for auto scroll feature.
857 protected static class AutoScroll
extends TimerTask
{
860 public ScrollView scrollView
;
862 public AutoScroll(ScrollView sv
, int dx
, int dy
) {
870 Display
.getDefault().asyncExec(new Runnable() {
873 scrollView
.scrollBy(deltaX
, deltaY
);
880 * Called when mouse is on contents area and mode.
884 protected void contentsMouseMoveEvent(MouseEvent event
) {
885 if ((event
.stateMask
& SWT
.BUTTON_MASK
) != 0) {
886 if (!fAutoScrollEnabled
) {
887 scrollBy(-(event
.x
- fMouseDownX
), -(event
.y
- fMousDownY
));
893 int v_right
= getContentsX() + getVisibleWidth();
894 int v_bottom
= getContentsY() + getVisibleHeight();
897 if (event
.x
< getContentsX()) {
898 sx
= (getContentsX() - event
.x
);
899 fMouseDownX
= getContentsX();
900 } else if (event
.x
> v_right
) {
901 sx
= -event
.x
+ v_right
;
902 fMouseDownX
= v_right
;
904 if (event
.y
< getContentsY()) {
905 sy
= (getContentsY() - event
.y
);
906 fMousDownY
= getContentsY();
907 } else if (event
.y
> v_bottom
) {
908 sy
= -event
.y
+ v_bottom
;
909 fMousDownY
= v_bottom
;
912 if (sx
!= 0 || sy
!= 0) {
913 // start auto scroll...
914 if (fAutoScroll
== null) {
915 if (fAutoScrollTimer
== null) {
916 fAutoScrollTimer
= new Timer(true);
918 fAutoScroll
= new AutoScroll(this, sx
, sy
);
919 fAutoScrollTimer
.schedule(fAutoScroll
, 0, fAutoScrollPeriod
);
921 fAutoScroll
.deltaX
= sx
;
922 fAutoScroll
.deltaY
= sy
;
925 if (fAutoScroll
!= null) {
926 fAutoScroll
.cancel();
930 scrollBy(-(event
.x
- fMouseDownX
), -(event
.y
- fMousDownY
));
936 * Called when mouse is on contents area and button is released
940 protected void contentsMouseUpEvent(MouseEvent event
) {
941 // reset auto scroll if it's engaged
942 if (fAutoScroll
!= null) {
943 fAutoScroll
.cancel();
949 * Responsible to draw contents area. At least rectangle clipX must be redrawn. This rectangle is given in contents
950 * coordinates. By default, no paint is produced.
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 vis_width
= getVisibleWidth();
995 int vis_height
= getVisibleHeight();
996 if (oldW
< vis_width
) {
997 if (localWidth
> vis_width
) {
998 localWidth
= vis_width
;
1000 fViewControl
.redraw(getContentsX() + oldW
, 0, localWidth
- oldW
, vis_height
, true);
1003 if (oldH
> localHeight
) {
1004 int s
= localHeight
;
1009 if (oldH
< vis_height
) {
1010 if (localHeight
> vis_height
) {
1011 localHeight
= vis_height
;
1013 fViewControl
.redraw(0, getContentsY() + oldH
, vis_width
, 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 align_h
= false, align_v
= 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 (align_h
|| (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 (align_v
|| (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();
1290 // redefined to return our vertical bar
1293 * @see org.eclipse.swt.widgets.Scrollable#getVerticalBar()
1296 public ScrollBar
getVerticalBar() {
1297 return fVertScrollBar
.getVerticalBar();
1300 // redefined to return out horizontal bar
1303 * @see org.eclipse.swt.widgets.Scrollable#getHorizontalBar()
1306 public ScrollBar
getHorizontalBar() {
1307 return fHorScrollBar
.getHorizontalBar();
1311 * Compute visibility of vertical/horizontal bar using given width/height and current visibility (i.e. is bar size are already in
1313 * @param forWidth width of foreground
1314 * @param forHeight height of foreground
1315 * @param currHorVis The current visibility state of horizontal scroll bar
1316 * @param currVertvis The current visibility state of vertical scroll bar
1317 * @return <code>true</code> if visibility changed else <code>false</code>
1319 public int computeBarVisibility(int forWidth
, int forHeight
, boolean currHorVis
, boolean currVertvis
) {
1321 int localForWidth
= forWidth
;
1323 switch (fVertScrollbarMode
) {
1330 if (getContentsHeight() > forHeight
) {
1332 // v bar size is already in for_width.
1333 if (!currVertvis
) {// (curr_vis&0x01)==0)
1334 localForWidth
-= getVerticalBarWidth();
1342 switch (fHorScrollbarMode
) {
1349 if (getContentsWidth() > localForWidth
) {
1351 // h bar is not in for_height
1352 if ((!currHorVis
) && (getContentsHeight() > (forHeight
- getHorizontalBarHeight()))) {// (curr_vis&0x02)==0 )
1364 * setup scroll bars visibility, return true if one of visibility changed.
1366 protected boolean updateScrollBarVisiblity() {
1367 boolean change
= false;
1369 boolean currVertVis
= fVertScrollBar
.getVisible();
1370 boolean currHorVis
= fHorScrollBar
.getVisible();
1371 int barNewVis
= computeBarVisibility(getVisibleWidth(), getVisibleHeight(), currHorVis
, currVertVis
);
1372 boolean newVertVis
= (barNewVis
& VBAR
) != 0;
1373 boolean newHorVis
= (barNewVis
& HBAR
) != 0;
1374 if (currVertVis ^ newVertVis
) { // vertsb_.getVisible() )
1375 fVertScrollBar
.setVisible(newVertVis
);
1378 if (currHorVis ^ newHorVis
) {
1379 fHorScrollBar
.setVisible(newHorVis
);
1383 // update corner control visibility:
1384 if (fCornerControl
!= null && change
) {
1385 boolean vis
= newVertVis
|| newHorVis
;
1386 if (vis ^ fCornerControl
.getVisible()) {
1387 fCornerControl
.setVisible(vis
);
1388 change
= true; // but must be already the case
1395 * Setup scroll bar using contents, visible and scroll bar mode properties.
1397 protected void updateScrollBarsValues() {
1398 /* update vertical scrollbar */
1399 ScrollBar b
= getVerticalBar();
1402 b
.setMaximum(getContentsHeight());
1403 b
.setThumb(getVisibleHeight());
1404 b
.setPageIncrement(getVisibleHeight());
1405 b
.setIncrement(fVertScrollbarIncrement
);
1406 b
.setSelection(getContentsY());
1409 // update "hidden" vertical bar too
1410 b
= fViewControl
.getVerticalBar();
1413 b
.setMaximum(getContentsHeight());
1414 b
.setThumb(getVisibleHeight());
1415 b
.setPageIncrement(getVisibleHeight());
1416 b
.setIncrement(fVertScrollbarIncrement
);
1417 b
.setSelection(getContentsY());
1420 /* update horizontal scrollbar */
1421 b
= getHorizontalBar();
1424 b
.setMaximum(getContentsWidth());
1425 b
.setThumb(getVisibleWidth());
1426 b
.setSelection(getContentsX());
1427 b
.setPageIncrement(getVisibleWidth());
1428 b
.setIncrement(fHorScrollbarIncrement
);
1430 // update "hidden" horizontal bar too
1431 b
= fViewControl
.getHorizontalBar();
1434 b
.setMaximum(getContentsWidth());
1435 b
.setThumb(getVisibleWidth());
1436 b
.setSelection(getContentsX());
1437 b
.setPageIncrement(getVisibleWidth());
1438 b
.setIncrement(fHorScrollbarIncrement
);
1443 * Change the control used in the bottom right corner (between two scrollbar), if control is null reset previous
1444 * corner control. This control is visible only if at least one scrollbar is visible. Given control will be disposed
1445 * by ScrollView, at dispose() time, at next setCornetControl() call or when calling setOverviewEnabled(). Pay
1446 * attention calling this reset overview feature until setOverviewEnabled(true) if called.
1447 * @param control The control for the overview
1449 public void setCornerControl(Control control
) {
1450 if (fCornerControl
!= null) {
1451 fCornerControl
.dispose();
1453 fCornerControl
= control
;
1454 if (fCornerControl
!= null) {
1455 ScrollBar vb
= getVerticalBar();
1456 ScrollBar hb
= getHorizontalBar();
1457 boolean vis
= vb
.getVisible() || hb
.getVisible();
1458 fCornerControl
.setVisible(vis
);
1463 * Transform (x,y) point in widget coordinates to contents coordinates.
1465 * @param x The x widget coordinate.
1466 * @param y The y widget coordinate.
1467 * @return org.eclipse.swt.graphics.Point with content coordinates.
1469 public final Point
viewToContents(int x
, int y
) {
1470 return new Point(viewToContentsX(x
), viewToContentsY(y
));
1474 * Transform x in widget coordinates to contents coordinates
1476 * @param x The y widget coordinate.
1477 * @return the x content coordinate.
1479 public int viewToContentsX(int x
) {
1480 return fContentsX
+ x
;
1484 * Transform y in widget coordinates to contents coordinates
1486 * @param y The y widget coordinate.
1487 * @return the y content coordinate.
1489 public int viewToContentsY(int y
) {
1490 return fContentsY
+ y
;
1494 * Transform (x,y) point from contents coordinates, to widget coordinates.
1496 * @param x The x content coordinate.
1497 * @param y The y content coordinate.
1498 * @return coordinates widget area as.
1500 public final Point
contentsToView(int x
, int y
) {
1501 return new Point(contentsToViewX(x
), contentsToViewY(y
));
1505 * Transform X axis coordinates from contents to widgets.
1507 * @param x contents coordinate to transform.
1508 * @return x coordinate in widget area
1510 public int contentsToViewX(int x
) {
1511 return x
- fContentsX
;
1515 * Transform Y axis coordinates from contents to widgets.
1517 * @param y contents coordinate to transform
1518 * @return y coordinate in widget area
1520 public int contentsToViewY(int y
) {
1521 return y
- fContentsY
;
1525 * Return the visible height of scroll view, might be > contentsHeight
1527 * @return the visible height of scroll view, might be > contentsHeight()
1529 public int getVisibleHeight() {
1530 return fViewControl
.getClientArea().height
;
1534 * Return int the visible width of scroll view, might be > contentsWidth().
1536 * @return int the visible width of scroll view, might be > contentsWidth()
1538 public int getVisibleWidth() {
1539 return fViewControl
.getClientArea().width
;
1543 * Add support for arrow key, scroll the ... scroll view. But you can redefine this method for your convenience.
1545 protected void keyPressedEvent(KeyEvent event
) {
1546 switch (event
.keyCode
) {
1548 scrollBy(0, -getVisibleHeight());
1550 case SWT
.ARROW_DOWN
:
1551 scrollBy(0, +getVisibleHeight());
1553 case SWT
.ARROW_LEFT
:
1554 scrollBy(-getVisibleWidth(), 0);
1556 case SWT
.ARROW_RIGHT
:
1557 scrollBy(+getVisibleWidth(), 0);
1565 * Redefine this method at your convenience
1567 * @param event The key event.
1569 protected void keyReleasedEvent(KeyEvent event
) {
1573 * Returns vertical bar width, even if bar isn't visible.
1575 * @return vertical bar width, even if bar isn't visible
1577 public int getVerticalBarWidth() {
1578 // include vertical bar width and trimming of scrollable used
1579 int bw
= fVertScrollBar
.computeTrim(0, 0, 0, 0).width
;
1584 * Returns horizontal bar height even if bar isn't visible.
1586 * @return horizontal bar height even if bar isn't visible
1588 public int getHorizontalBarHeight() {
1589 // include horiz. bar height and trimming of scrollable used
1590 int bh
= fHorScrollBar
.computeTrim(0, 0, 0, 0).height
;
1591 // +1 because win32 H.bar need 1 pixel canvas size to appear ! (strange no ?)
1597 * @see org.eclipse.swt.widgets.Scrollable#computeTrim(int, int, int, int)
1600 public Rectangle
computeTrim(int x
, int y
, int w
, int h
) {
1601 Rectangle r
= new Rectangle(x
, y
, w
, h
);
1602 int bar_vis
= computeBarVisibility(w
, h
, false, false);
1603 if ((bar_vis
& VBAR
) != 0) {
1604 r
.width
+= getVerticalBarWidth();
1606 if ((bar_vis
& HBAR
) != 0) {
1607 r
.height
+= getHorizontalBarHeight();
1613 * Internal layout for ScrollView, handle scrollbars, drawzone and corner control
1615 protected class SVLayout
extends Layout
{
1621 * The do-it-not flag
1623 boolean dontLayout
= false;
1627 * @see org.eclipse.swt.widgets.Layout#computeSize(org.eclipse.swt.widgets.Composite, int, int, boolean)
1630 protected Point
computeSize(Composite composite
, int wHint
, int hHint
, boolean flushCache
) {
1631 Point p
= new Point(250, 250);
1632 if (fContentsWidth
< p
.x
) {
1633 p
.x
= fContentsWidth
;
1635 if (fContentsHeight
< p
.y
) {
1636 p
.y
= fContentsHeight
;
1643 * @see org.eclipse.swt.widgets.Layout#layout(org.eclipse.swt.widgets.Composite, boolean)
1646 protected void layout(Composite composite
, boolean flushCache
) {
1655 Point cs
= composite
.getSize();
1656 int bar_vis
= computeBarVisibility(cs
.x
, cs
.y
, false, false);
1657 boolean vb_vis
= (bar_vis
& VBAR
) != 0;
1658 boolean hb_vis
= (bar_vis
& HBAR
) != 0;
1659 fVertScrollBar
.setVisible(vb_vis
);
1660 fHorScrollBar
.setVisible(hb_vis
);
1661 int vbw
= getVerticalBarWidth();
1662 int hbh
= getHorizontalBarHeight();
1663 int wb
= vb_vis ? vbw
: 0;
1664 int hb
= hb_vis ? hbh
: 0;
1665 int cww
= 0, cwh
= 0;
1667 if (fCornerControl
!= null && (vb_vis
|| hb_vis
)) { // corner_control_.getVisible())
1668 fCornerControl
.setVisible(true);
1677 } else if (vb_vis
&& hb_vis
) {
1678 if (fCornerControl
!= null) {
1679 fCornerControl
.setVisible(false);
1684 if (vb_vis
|| hb_vis
) {
1685 updateScrollBarsValues();
1688 int vw
= cs
.x
- (vb_vis ? vbw
: 0);
1689 int vh
= cs
.y
- (hb_vis ? hbh
: 0);
1690 int vbx
= cs
.x
- wb
;
1691 int hby
= cs
.y
- hb
;
1693 fViewControl
.setBounds(0, 0, vw
, vh
);
1696 fVertScrollBar
.setBounds(vbx
, 0, wb
, cs
.y
- cwh
);
1699 fHorScrollBar
.setBounds(0, hby
, cs
.x
- cww
, hb
);
1701 if (fCornerControl
!= null && fCornerControl
.getVisible()) {
1702 fCornerControl
.setBounds(vbx
, hby
, vbw
, hbh
);
1704 updateScrollBarsValues();
1713 // static must take place here... cursor is created once.
1714 volatile static Cursor fOverviewCursor
;
1716 /** Support for click-and-see overview shell on this ScrollView */
1717 protected class Overview
{
1720 * factor for X from real and overview sizes, for mouse move speed.
1722 protected float fOverviewFactorX
;
1725 * factor for Y from real and overview sizes, for mouse move speed.
1727 protected float fOverviewFactorY
;
1729 * shell use to show overview
1731 protected Shell fOverview
;
1733 * save mouse X cursor location for disappear();
1735 protected int fSaveCursorX
;
1737 * save mouse Y cursor location for disappear();
1739 protected 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
% 4 == 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
1843 protected void overviewAppear(int mx
, int my
) {
1844 if (fOverview
== null) {
1845 fOverview
= new Shell(getShell(), SWT
.ON_TOP
| SWT
.NO_BACKGROUND
);
1846 fOverview
.addPaintListener(new PaintListener() {
1848 public void paintControl(PaintEvent e
) {
1849 drawOverview(e
.gc
, fOverview
.getClientArea());
1853 // always the same..
1854 // overview.setBackground( viewcontrol_.getBackground() );
1855 fOverview
.setForeground(fViewControl
.getForeground());
1857 // get location of shell (in screeen coordinates)
1858 Point p
= toGlobalCoordinates(fCornerControl
, 0, 0);
1862 w
= h
= fOverviewSize
;
1863 Rectangle scr
= getDisplay().getBounds();
1864 Point ccs
= fCornerControl
.getSize();
1866 if (fContentsWidth
> fContentsHeight
) {
1867 float ratio
= fContentsHeight
/ (float) fContentsWidth
;
1868 h
= (int) (w
* ratio
);
1871 } else if (h
>= scr
.height
/ 2) {
1875 float ratio
= fContentsWidth
/ (float) fContentsHeight
;
1876 w
= (int) (h
* ratio
);
1879 } else if (w
>= scr
.width
/ 2) {
1883 fOverviewFactorX
= fContentsWidth
/ (float) w
;
1884 fOverviewFactorY
= fContentsHeight
/ (float) h
;
1886 // no contents size set ?
1887 catch (java
.lang
.ArithmeticException e
) {
1890 // try pop-up on button, extending to bottom right,
1891 // if outside screen, extend pop-up to top left
1892 // if( x+w > scr.width ) x = scr.width-w; //x += corner_control_.getSize().x-w;
1893 // if( y+h > scr.height ) y = scr.height-h;//y += corner_control_.getSize().y-h;
1902 fOverview
.setBounds(x
, y
, w
, h
);
1903 fOverview
.setVisible(true);
1905 // mouse cursor disappear, so set invisible mouse cursor ...
1906 if (fOverviewCursor
== null) {
1907 RGB rgb
[] = { new RGB(0, 0, 0), new RGB(255, 0, 0) };
1908 PaletteData palette
= new PaletteData(rgb
);
1910 byte src
[] = new byte[s
* s
];
1911 byte msk
[] = new byte[s
* s
];
1912 for (int i
= 0; i
< s
* s
; ++i
) {
1913 src
[i
] = (byte) 0xFF;
1915 ImageData i_src
= new ImageData(s
, s
, 1, palette
, 1, src
);
1916 ImageData i_msk
= new ImageData(s
, s
, 1, palette
, 1, msk
);
1917 fOverviewCursor
= new Cursor(null, i_src
, i_msk
, 0, 0);
1919 fCornerControl
.setCursor(fOverviewCursor
);
1920 // convert to global coordinates
1921 p
= toGlobalCoordinates(fCornerControl
, mx
, my
);
1925 Rectangle r
= fOverview
.getClientArea();
1926 int cx
= (int) (r
.width
* fContentsX
/ (float) fContentsWidth
);
1927 int cy
= (int) (r
.height
* fContentsY
/ (float) fContentsHeight
);
1929 // cx,cy to display's global coordinates
1930 p
= toGlobalCoordinates(fOverview
.getParent(), cx
, cy
);
1934 * Process disappear of overview
1936 protected void overviewDisappear() {
1937 overviewDisappear(true);
1941 * Process disappear of overview
1942 * @param restoreCursorLoc A flag to restore cursor location
1944 protected void overviewDisappear(boolean restoreCursorLoc
) {
1945 if (fOverview
== null) {
1948 fOverview
.setVisible(false);
1949 fCornerControl
.setCursor(null);
1950 if (restoreCursorLoc
) {
1951 getDisplay().setCursorLocation(fSaveCursorX
, fSaveCursorY
);
1953 fOverview
.dispose();
1958 * Process mouse move in overview
1959 * @param event The mouse event
1961 protected void overviewMove(MouseEvent event
) {
1962 Point p
= toGlobalCoordinates(fCornerControl
, event
.x
, event
.y
);
1963 int dx
= p
.x
- fSaveCursorX
;
1964 int dy
= p
.y
- fSaveCursorY
;
1965 overviewMove(dx
, dy
, event
);
1969 * Process mouse move event when overviewing
1971 * @param dx The x coordinates delta
1972 * @param dy The y coordinates delta
1973 * @param event The typed event
1975 protected void overviewMove(int dx
, int dy
, TypedEvent event
) {
1976 boolean ctrl
= false;
1977 boolean shift
= false;
1979 if (event
instanceof MouseEvent
) {
1980 MouseEvent e
= (MouseEvent
) event
;
1981 getDisplay().setCursorLocation(fSaveCursorX
, fSaveCursorY
);
1982 ctrl
= (e
.stateMask
& SWT
.CONTROL
) != 0;
1983 shift
= (e
.stateMask
& SWT
.SHIFT
) != 0;
1984 } else if (event
instanceof KeyEvent
) {
1985 KeyEvent e
= (KeyEvent
) event
;
1986 ctrl
= (e
.stateMask
& SWT
.CONTROL
) != 0;
1987 shift
= (e
.stateMask
& SWT
.SHIFT
) != 0;
1990 int cx
= fContentsX
;
1991 int cy
= fContentsY
;
1992 float fx
= fOverviewFactorX
;
1993 float fy
= fOverviewFactorY
;
1995 if (ctrl
&& shift
) {
1996 if ((fx
* 0.25f
> 1) && (fy
* 0.25 > 1)) {
2009 scrollBy((int) (fx
* dx
), (int) (fy
* dy
));
2010 if (cx
!= fContentsX
|| cy
!= fContentsY
) {
2012 fOverview
.update(); // draw now !
2017 * Convert overview coordinates to global coordinates.
2019 * @param loc the control reference
2020 * @param x The x coordinate to convert
2021 * @param y The y coordinate to convert
2024 protected Point
toGlobalCoordinates(Control loc
, int x
, int y
) {
2025 Point p
= new Point(x
, y
);
2026 for (Control c
= loc
; c
!= null; c
= c
.getParent()) {
2027 // control might have client area with 'decorations'
2028 int trim_x
= 0, trim_y
= 0;
2029 // other kind of widget with trimming ??
2030 if (c
instanceof Scrollable
) {
2031 Scrollable s
= (Scrollable
) c
;
2032 Rectangle rr
= s
.getClientArea();
2033 Rectangle tr
= s
.computeTrim(rr
.x
, rr
.y
, rr
.width
, rr
.height
);
2034 trim_x
= rr
.x
- tr
.x
;
2035 trim_y
= rr
.y
- tr
.y
;
2037 p
.x
+= c
.getLocation().x
+ trim_x
;
2038 p
.y
+= c
.getLocation().y
+ trim_y
;