Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » swt canvas scrolling(horizontal scrolling canvas content)
swt canvas scrolling [message #1015367] Thu, 28 February 2013 06:21 Go to next message
Chandrayya Kumarswamimath is currently offline Chandrayya Kumarswamimath
Messages: 19
Registered: August 2010
Location: Bangalore
Junior Member
I have drawn some text and rectangles on canvas.
package com.cavium.test.views;

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.PaintEvent;
    import org.eclipse.swt.events.PaintListener;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.graphics.Rectangle;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.widgets.Canvas;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.ScrollBar;
    import org.eclipse.swt.widgets.Shell;

    public class Test2 {
        protected static final int Y_STEP = 20;
        static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND
                | SWT.H_SCROLL | SWT.CLOSE | SWT.V_SCROLL | SWT.RESIZE;
        static int canvasStyle = SWT.NO_REDRAW_RESIZE;// | SWT.H_SCROLL |

        // SWT.V_SCROLL;

        public static void main(String[] args) {
            final Display display = new Display();
            final Shell shell = new Shell(display, shellStyle);
            shell.setLayout(new FillLayout());
            shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
            shell.setText("Canvas Test");
            shell.setSize(300, 200);

            final Canvas canvas = new Canvas(shell, canvasStyle);
            canvas.setLayout(new FillLayout());
            canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));

            final Point origin = new Point(10, 20);
            final ScrollBar hBar = shell.getHorizontalBar();
            Rectangle size = canvas.getBounds();
            hBar.setMaximum(size.width);
            hBar.setMinimum(0);

            final ScrollBar vBar = shell.getVerticalBar();
            hBar.setMaximum(size.height);
            hBar.setMinimum(0);

            // Create a paint handler for the canvas
            canvas.addPaintListener(new PaintListener() {
                @Override
                public void paintControl(PaintEvent e) {
                    // Do some drawing
                    e.gc.drawString("Rows", origin.x, origin.y);
                    e.gc.drawString("Data", 120, 20);
                    for (int i = 0; i < 10; i++) {
                        e.gc.drawString("Row Header" + (i + 1), origin.x, origin.y
                                + (i + 1) * Y_STEP);
                        for (int j = 0; j < 10; j++) {
                            e.gc.drawRectangle(origin.x + 110 + (j * 20), origin.y
                                    + (i + 1) * Y_STEP, 20, 20);
                            e.gc.drawString("C" + (j + 1), origin.x + 110 + 2
                                    + (j * 20), origin.y + (i + 1) * Y_STEP + 1);
                        }
                    }

                }

            });

            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
            display.dispose();

        }
    }


Is it possible to scroll only cells(C1, C2...) on dragging the horizontal scrollbar keeping the Row Header 1, Row Header 2 ...etc..unchanged.
index.php/fa/13603/0/

Also let me know how we can detected the mouse events on scrollbars i.e when user clicks on up/down or left arrow/right arrow buttons, click and drag on thumb, clicks on the area between thumb and right arrow or left arrow button?
Re: swt canvas scrolling [message #1015714 is a reply to message #1015367] Fri, 01 March 2013 16:21 Go to previous messageGo to next message
Grant Gayed is currently offline Grant Gayed
Messages: 2148
Registered: July 2009
Senior Member
For an example of painting on a Canvas and reacting to scrollbar events
see
http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet48.java
..

Grant


On 2/28/2013 6:21 AM, Chandrayya Mising name wrote:
> I have drawn some text and rectangles on canvas.
>
> package com.cavium.test.views;
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.events.PaintEvent;
> import org.eclipse.swt.events.PaintListener;
> import org.eclipse.swt.graphics.Point;
> import org.eclipse.swt.graphics.Rectangle;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.widgets.Canvas;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.ScrollBar;
> import org.eclipse.swt.widgets.Shell;
>
> public class Test2 {
> protected static final int Y_STEP = 20;
> static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND
> | SWT.H_SCROLL | SWT.CLOSE | SWT.V_SCROLL | SWT.RESIZE;
> static int canvasStyle = SWT.NO_REDRAW_RESIZE;// | SWT.H_SCROLL |
>
> // SWT.V_SCROLL;
>
> public static void main(String[] args) {
> final Display display = new Display();
> final Shell shell = new Shell(display, shellStyle);
> shell.setLayout(new FillLayout());
> shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
> shell.setText("Canvas Test");
> shell.setSize(300, 200);
>
> final Canvas canvas = new Canvas(shell, canvasStyle);
> canvas.setLayout(new FillLayout());
> canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
>
> final Point origin = new Point(10, 20);
> final ScrollBar hBar = shell.getHorizontalBar();
> Rectangle size = canvas.getBounds();
> hBar.setMaximum(size.width);
> hBar.setMinimum(0);
>
> final ScrollBar vBar = shell.getVerticalBar();
> hBar.setMaximum(size.height);
> hBar.setMinimum(0);
>
> // Create a paint handler for the canvas
> canvas.addPaintListener(new PaintListener() {
> @Override
> public void paintControl(PaintEvent e) {
> // Do some drawing
> e.gc.drawString("Rows", origin.x, origin.y);
> e.gc.drawString("Data", 120, 20);
> for (int i = 0; i < 10; i++) {
> e.gc.drawString("Row Header" + (i + 1), origin.x, origin.y
> + (i + 1) * Y_STEP);
> for (int j = 0; j < 10; j++) {
> e.gc.drawRectangle(origin.x + 110 + (j * 20), origin.y
> + (i + 1) * Y_STEP, 20, 20);
> e.gc.drawString("C" + (j + 1), origin.x + 110 + 2
> + (j * 20), origin.y + (i + 1) * Y_STEP + 1);
> }
> }
>
> }
>
> });
>
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) {
> display.sleep();
> }
> }
> display.dispose();
>
> }
> }
>
>
> Is it possible to scroll only cells(C1, C2...) on dragging the horizontal scrollbar keeping the Row Header 1, Row Header 2 ...etc..unchanged.
>
>
> Also let me know how we can detected the mouse events on scrollbars i.e when user clicks on up/down or left arrow/right arrow buttons, click and drag on thumb, clicks on the area between thumb and right arrow or left arrow button?
>
Re: swt canvas scrolling [message #1015887 is a reply to message #1015714] Mon, 04 March 2013 02:55 Go to previous messageGo to next message
Chandrayya Kumarswamimath is currently offline Chandrayya Kumarswamimath
Messages: 19
Registered: August 2010
Location: Bangalore
Junior Member
Hello Grant Gayed, I have seen this example but not helpful. I wrote some code to address this requirement as shown below.
package com.cavium.test.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;

public class Test2 {
    private static final int CYCLE_OFFSET = 120;
    protected static final int Y_STEP = 20;
    static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
    static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display, shellStyle);
        shell.setLayout(new FillLayout());
        shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
        shell.setText("Canvas Test");
        shell.setSize(400, 300);

        Composite composite = new Composite(shell, SWT.NONE);
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        composite.setLayout(new GridLayout(1, false));

        final Canvas canvas = new Canvas(composite, canvasStyle);
        canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        final Point origin = new Point(0, 0);
        final ScrollBar hBar = canvas.getHorizontalBar();
        hBar.setMaximum(2120);
        hBar.setMinimum(0);
        hBar.addListener(SWT.Selection, new Listener() {

            @Override
            public void handleEvent(Event event) {
                switch (event.detail) {
                case SWT.DRAG:
                    System.out.println("drag");
                    break;
                case SWT.ARROW_DOWN:
                    System.out.println(" arrow down");
                    break;
                case SWT.ARROW_UP:
                    System.out.println("arrow up");
                    break;
                case SWT.ARROW_LEFT:
                    System.out.println("arrow left");
                    break;
                case SWT.ARROW_RIGHT:
                    System.out.println("arrow right");
                    break;
                }
                System.out.println("In hori");
                int hSelection = hBar.getSelection();
                int destX = -hSelection - origin.x;
                System.out.println("origin.x " + origin.x + "hSelection " + hSelection + " destX " + destX);
                canvas.scroll(140 + destX, 0, 140, 0, 2120, 2000, false);
                origin.x = -hSelection;
                System.out.println("origin.x " + origin.x);
            }

        });
        final ScrollBar vBar = canvas.getVerticalBar();
        vBar.setMaximum(2000);
        vBar.setMinimum(0);
        vBar.addListener(SWT.Selection, new Listener() {

            @Override
            public void handleEvent(Event event) {
                System.out.println("In ver");
                int vSelection = vBar.getSelection();
                int destY = -vSelection - origin.y;
                canvas.scroll(0, destY, 0, 0, 2120, 2000, false);
                origin.y = -vSelection;
            }

        });
        canvas.addListener(SWT.Resize, new Listener() {
            @Override
            public void handleEvent(Event e) {
                System.out.println("In resize");
                Rectangle client = canvas.getClientArea();
                hBar.setMaximum(2120);
                vBar.setMaximum(2000);
                hBar.setThumb(Math.min(2120, client.width));
                hBar.setPageIncrement(Math.min(2120, client.width));
                vBar.setThumb(Math.min(2000, client.height));
                vBar.setPageIncrement(Math.min(2000, client.height));
                hBar.setIncrement(20);
                vBar.setIncrement(20);
                int hPage = 2120 - client.width;
                int vPage = 2000 - client.height;
                int hSelection = hBar.getSelection();
                int vSelection = vBar.getSelection();
                if (hSelection >= hPage) {
                    if (hPage <= 0)
                        hSelection = 0;
                    origin.x = -hSelection;
                }
                if (vSelection >= vPage) {
                    if (vPage <= 0)
                        vSelection = 0;
                    origin.y = -vSelection;
                }
                System.out.println("hPage " + hPage + " hSelection " + hSelection + " origin.x " + origin.x + " origin.y "
                        + origin.y);
                canvas.redraw();
            }
        });
        // Create a paint handler for the canvas
        canvas.addPaintListener(new PaintListener() {
            @Override
            public void paintControl(PaintEvent e) {
                System.out.println("In paint");
                System.out.println(origin.x);
                for (int i = 0; i < 100; i++) {
                    e.gc.drawString("Row Header" + i, origin.x, origin.y + i * Y_STEP);
                    for (int j = 0; j < 100; j++) {
                        e.gc.drawRectangle(origin.x + CYCLE_OFFSET + j * 20, origin.y + i * Y_STEP, 20, 20);
                        e.gc.drawString(new Integer(j).toString(), origin.x + CYCLE_OFFSET + 2 + j * 20, origin.y + i * Y_STEP
                                + 1);
                    }
                }

            }

        });
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();

    }
}

But facing the following issues:
1. In horizontal scrollbar listener am not able to detect the events on scrollbar. Nothing is printing here.
2. When I drag the scrollbar thumb or click on the area between thumb and LEFT or RIGHT arrow button or LEFT ARROW button, the code is not working.

Can anyone point out the bug in this code? I am not understood how scrolling is happening. Can Somebody please provide me the links,tutorials/screencasts related to SWT,Canvas and scrolling.
Re: swt canvas scrolling [message #1016894 is a reply to message #1015887] Fri, 08 March 2013 01:26 Go to previous message
Chandrayya Kumarswamimath is currently offline Chandrayya Kumarswamimath
Messages: 19
Registered: August 2010
Location: Bangalore
Junior Member
After invenstigation it is because of a bug in GTK+. See here https://bugs.eclipse.org/bugs/show_bug.cgi?id=51995 . It was fixed. But I dont know how to resolve it on my machine any idea?
Previous Topic:Text truncated in Table
Next Topic:SWT, Linux 6.3, and xulrunner 10
Goto Forum:
  


Current Time: Sun Apr 20 04:25:09 EDT 2014

Powered by FUDForum. Page generated in 0.05164 seconds