Created
November 28, 2015 06:12
-
-
Save jasonrclark/bd2ac61b77f86bc04b45 to your computer and use it in GitHub Desktop.
Scrolling Composites in SWT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env jruby | |
require 'swt' | |
# swt gem doesn't appear to import the ScrolledComposite we need | |
# Looking at docs it's plenty old enough to be in any Java version we support | |
module Swt | |
include_package 'org.eclipse.swt.custom' | |
module Custom | |
import org.eclipse.swt.custom.ScrolledComposite | |
end | |
end | |
class SwtExample | |
def initialize | |
display = Swt::Widgets::Display.new | |
red = display.get_system_color ::Swt::SWT::COLOR_RED | |
blue = display.get_system_color ::Swt::SWT::COLOR_BLUE | |
@shell = Swt::Widgets::Shell.new | |
@shell.text = "Example" | |
# This puts us in absolute mode, so we need to set everyone's position | |
# explicitly. Fortunately, that's what we actually want since as a library | |
# with our own positioning rules, we *know* where everything ought to be. | |
@shell.layout = nil | |
@shell.set_size 200, 200 | |
# Call our method to set up the necessary scrollable composites | |
scroll_composite1, inner_composite1 = create_composite "yo", red | |
# As mentioned before, we're responsible for all positioning | |
# Outer composite is positioned relative to the main app | |
scroll_composite1.set_bounds 0, 0, 100, 200 | |
# Inner composite positioning relative to the scrolling container | |
inner_composite1.set_bounds 0, 0, 100, 1000 | |
# Repeat same steps to make a second scrollable composite | |
scroll_composite2, inner_composite2 = create_composite "dawg", blue | |
scroll_composite2.set_bounds 100, 0, 100, 200 | |
inner_composite2.set_bounds 0, 0, 100, 1000 | |
@shell.open | |
end | |
def create_composite(string, color) | |
# Outer composite that actually responds to scrolling | |
scroll = Swt::Custom::ScrolledComposite.new @shell, ::Swt::SWT::V_SCROLL | |
# Inner composite "content" that we'll append our controls into | |
composite = Swt::Widgets::Composite.new scroll, ::Swt::SWT::NONE | |
# Setting layout to nil puts us in charge of absolute positioning | |
# If we *don't* set the bounds with this layout, we get blank screens. Fun. | |
composite.layout = nil | |
# Set inner composite as the content of the scrolling composite | |
scroll.content = composite | |
# Adding controls and colors just for display purposes here... reaminder of | |
# this method isn't critical to the core functionality. | |
composite.background = color | |
text = Swt::Widgets::Text.new composite, ::Swt::SWT::MULTI | ::Swt::SWT::WRAP | |
text.text = "#{string} " * 10 | |
text.set_bounds 10, 60, 80, 50 | |
button = Swt::Widgets::Button.new composite, ::Swt::SWT::NONE | |
button.text = string | |
button.set_bounds 10, 10, 80, 50 | |
button.add_listener ::Swt::SWT::Selection do | |
# Inch our scrolling along. This will be accessible probably via scroll_top or | |
# some related thing... | |
point = scroll.get_origin | |
point.y += 10 | |
scroll.set_origin point | |
end | |
# Return so outer method here can tweak the positioning on both pieces | |
[scroll, composite] | |
end | |
# This is the main gui event loop | |
def start | |
display = Swt::Widgets::Display.get_current | |
# until the window (the Shell) has been closed | |
while [email protected] | |
# check for and dispatch new gui events | |
display.sleep unless display.read_and_dispatch | |
end | |
display.dispose | |
end | |
end | |
Swt::Widgets::Display.set_app_name "Example" | |
app = SwtExample.new | |
app.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment