Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Mirah/SWTSnippets/Snippet9"

(New page: <pre>/******************************************************************************* * Copyright(child 2000, 2011 IBM Corporation and others. * All rights reserved. This program and the...)
(No difference)

Revision as of 12:18, 27 March 2011

/*******************************************************************************
 * Copyright(child 2000, 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

/*
 * Composite example snippet: scroll a child control automatically
 *
 * For a list of SWT example snippets ported to Mirah, see
 * http://wiki.eclipse.org/Mirah/SWTSnippets
 */
import org.eclipse.swt.*
import org.eclipse.swt.events.*
import org.eclipse.swt.graphics.*
import org.eclipse.swt.widgets.*

display = Display.new
shell = Shell.new display, SWT.SHELL_TRIM | SWT.H_SCROLL | SWT.V_SCROLL
composite = Composite.new shell, SWT.BORDER
composite.setSize 700, 600
red = display.getSystemColor SWT.COLOR_RED
composite.addPaintListener do |e|
	e.gc.setBackground red
	e.gc.fillOval 5, 5, 690, 590
end
hBar = shell.getHorizontalBar
hBar.addListener(SWT.Selection) do |e|
	location = composite.getLocation
	location.x = -hBar.getSelection
	composite.setLocation location
end
vBar = shell.getVerticalBar
vBar.addListener(SWT.Selection) do |e|
	location = composite.getLocation
	location.y = -vBar.getSelection
	composite.setLocation location
end
shell.addListener(SWT.Resize) do |e|
	size = composite.getSize
	rect = shell.getClientArea
	hBar.setMaximum size.x
	vBar.setMaximum size.y
	hBar.setThumb Math.min(size.x, rect.width)
	vBar.setThumb Math.min(size.y, rect.height)
	hPage = size.x - rect.width
	vPage = size.y - rect.height
	hSelection = hBar.getSelection
	vSelection = vBar.getSelection
	location = composite.getLocation
	if hSelection >= hPage
		hSelection = 0 if hPage <= 0
		location.x = -hSelection
	end
	if vSelection >= vPage
		vSelection = 0 if vPage <= 0
		location.y = -vSelection
	end
	composite.setLocation location
end
shell.setSize 600, 500
shell.open
until shell.isDisposed
	unless display.readAndDispatch
		display.sleep
	end
end
display.dispose

Copyright © Eclipse Foundation, Inc. All Rights Reserved.