Class: SWT::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/swt.rb

Overview

The heart of the SWT module: You may build GUIs with the Builder class

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object



498
499
500
501
502
503
504
505
# File 'lib/swt.rb', line 498

def method_missing(id,*args,&block)
  if id.to_s=~/labeled(\w+)/
    label args.shift
    method($1.downcase).call *args,&block
  else
    old_method_missing id,args,&block
  end
end

Class Method Details

.go(&block) ⇒ Object

takes a block of Builder language code, builds the screen and shows it



376
377
378
379
380
381
382
# File 'lib/swt.rb', line 376

def self.go(&block)
  shell=Builder.new.instance_eval(&block)
  shell.pack
  shell.layout
  shell.open
  run shell
end

.run(shell) ⇒ Object

A helper function for dispatching SWT events. Blocks as long as the given shell is not disposed.



509
510
511
512
513
514
515
# File 'lib/swt.rb', line 509

def Builder.run(shell)
  d=shell.getDisplay
  while(!shell.isDisposed&&shell.isVisible) do
    while(d.readAndDispatch) do end
    d.sleep
  end
end

Instance Method Details

#button(text, style = RealSWT::NONE, &block) ⇒ Object

Create a push button with the specified text If block is given this block will be called if button is pressed



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/swt.rb', line 464

def button(text,style=RealSWT::NONE,&block)
  res=Button.new(@parent,style)
  res.setText text
  init_control res
  if block_given?
    lis=SelectionListener.new
    lis.instance_eval do
      def widgetDefaultSelected(event)
      end
      def widgetSelected (event)
        @block.call event
      end
      @block=block
    end
    res.addSelectionListener lis
  end
  return res
end

#container(columns = 1, opts = {}, &block) ⇒ Object

Create an empty container (does not show anything). It’s only supposed for layouting elements



430
431
432
433
434
435
# File 'lib/swt.rb', line 430

def container(columns=1,opts={},&block)
  opts[:style] or opts[:style]=RealSWT::NONE
  res=Composite.new(@parent,opts[:style])
  init_composite res,columns,opts,&block
  return res
end

#group(title, columns = 1, style = RealSWT::NONE, &block) ⇒ Object

Create a group element with the given title



438
439
440
441
442
443
# File 'lib/swt.rb', line 438

def group(title,columns=1,style=RealSWT::NONE,&block)
  res=Group.new(@parent,style)
  res.setText title
  init_composite res,columns,&block
  return res
end

#label(text, style = RealSWT::NONE) ⇒ Object

Create a label and show the text supplied. You may use any form of ‘labeledText’, ‘labeledButton’…



455
456
457
458
459
460
# File 'lib/swt.rb', line 455

def label(text,style=RealSWT::NONE)
  res=Label.new(@parent,style)
  res.setText text
  init_control res
  return res
end

#old_method_missingObject



495
# File 'lib/swt.rb', line 495

alias :old_method_missing method_missing

#shell(title, columns = 1, style = RealSWT::SHELL_TRIM, &block) ⇒ Object

Create a shell (a toplevel window)



416
417
418
419
420
421
422
423
424
425
426
# File 'lib/swt.rb', line 416

def shell(title,columns=1,style=RealSWT::SHELL_TRIM,&block)
  res=Shell.new(Display.getDefault,style)
  res.setText title
  fl=FillLayout.new
  fl.marginHeight=5
  fl.marginWidth=5
  res.setLayout fl
  @parent=res
  container(columns,&block)
  return res
end

#text(text = nil, style = RealSWT::BORDER) ⇒ Object

Create a text box. Sets the text if supplied



446
447
448
449
450
451
# File 'lib/swt.rb', line 446

def text(text=nil,style=RealSWT::BORDER)
  res=Text.new(@parent,style)
  text and res.setText text
  init_control res
  return res
end

#tree(opts = {:style=>RealSWT::BORDER}, &block) ⇒ Object

Create Tree if block is given it is evaluated as TreeBuilder commands



484
485
486
487
488
489
490
491
492
# File 'lib/swt.rb', line 484

def tree(opts={:style=>RealSWT::BORDER},&block)
  opts[:style] or opts[:style]=RealSWT::BORDER
  res=Tree.new(@parent,opts[:style])
  init_control res,opts
  builder=TreeBuilder.new &block
  builder.run res
  res.pack
  return res
end