wxRuby_RadioBox_choices

Platform:
Ubuntu 9.04 (up to date), ruby 1.8.7 (2008-08-11 patchlevel 72)
[i486-linux],
wxruby-2.0.1-x86-linux

I am trying to use wxFormBuilder, with the output being an XRC file, to
produce a simple application that I already have working from a single
file, non-XRC approach. To clarify my descriptions, a simple applicaton
uses two Ruby files: the “Gui” file produced by XRCise from the XRC
file, and the “App” file which establishes how the application operates.
The “App” file ‘requires’ the “Gui” file.

I have a simple XRC-based example working correctly. However, one thing
that I’d like to do generates an error.

In wxFormBuilder, I have a RadioBox with four buttons labeled: “A” “B”
“C” “D”.

When I run the final program, everything works. Then, I try to change
the button labels in the “App” file by the expression:
@radiobox1.choices = [ “w”, “x”, “y”, “z” ] which produces an error
stating that ‘choices’ in not a defined method.

Just as a test, the following worked fine: @radiobox1.label = “New
label for Box”

I used :choices in the non-XRC version of this program that was
contained in one file.

I cannot find any reference to “choices” within Wx::RadioBox, such as by
doing the following:
irb(main):027:0> RadioBox.private_instance_methods.sort and several
variations of this approach.

My questions are:

  1. Why can’t I find anything within Wx::RadioBox that references
    :choices?

  2. Is there a way to add a new method to RadioBox from within the “App”
    program when using the XRC approach?

Thanks

On Sun, Nov 22, 2009 at 4:58 PM, Gary H. [email protected]
wrote:

The “App” file ‘requires’ the “Gui” file.
stating that ‘choices’ in not a defined method.
variations of this approach.

My questions are:

  1. Why can’t I find anything within Wx::RadioBox that references
    :choices?

To my knowledge, there is no :choices method on Wx::RadioBox at all. So
why
it works in your self-coded one, and not in XRC one, I can’t explain,
unless
you created the choices method yourself.

  1. Is there a way to add a new method to RadioBox from within the “App”
    program when using the XRC approach?

Yes, there is a way to add new methods to a XRCized created class. You
need
to require the generated file first, before you append methods to the
class,
simply for the fact that if you create a class, before it’s defined, you
can’t subclass it, and the subclassing is already done in the XRCized
generated output.

The Correct Way:

require ‘my_xrc_generated_file’

class MyXrcWindow
def new_method
puts “Hello World from New Method!”
end
end

Keep in mind, that with the current version of XRCise, that if you need
to
define anything to be executed, when the class is created, use the
method
on_init.

EG: If you do:

require ‘my_xrc_generated_file’

class MyXrcWindow
def on_init
puts “This will be fired when an instance is created.”
@@instance_number |= 0
@@instance_number += 1
end
end

hth,

Mario

Thanks

Mario S. wrote:

On Sun, Nov 22, 2009 at 4:58 PM, Gary H. [email protected]
wrote:

The “App” file ‘requires’ the “Gui” file.
stating that ‘choices’ in not a defined method.
variations of this approach.

My questions are:

  1. Why can’t I find anything within Wx::RadioBox that references
    :choices?

To my knowledge, there is no :choices method on Wx::RadioBox at all. So
why
it works in your self-coded one, and not in XRC one, I can’t explain,
unless
you created the choices method yourself.

  1. Is there a way to add a new method to RadioBox from within the “App”
    program when using the XRC approach?

Yes, there is a way to add new methods to a XRCized created class. You
need
to require the generated file first, before you append methods to the
class,
simply for the fact that if you create a class, before it’s defined, you
can’t subclass it, and the subclassing is already done in the XRCized
generated output.

The Correct Way:

require ‘my_xrc_generated_file’

class MyXrcWindow
def new_method
puts “Hello World from New Method!”
end
end

Keep in mind, that with the current version of XRCise, that if you need
to
define anything to be executed, when the class is created, use the
method
on_init.

EG: If you do:

require ‘my_xrc_generated_file’

class MyXrcWindow
def on_init
puts “This will be fired when an instance is created.”
@@instance_number |= 0
@@instance_number += 1
end
end

hth,

Mario

Thanks

Thanks for your help Mario!

Your examples helped me get my program working, and helped my
understanding.

As for there being no :choices within wx::RadioBox, there are two places
that show it. The first is in the official wxRuby documentation, listed
under wx::RadioBox, as follows:

RadioBox.new(Window parent, Integer id, String label,
Point point = DEFAULT_POSITION,
Size size = DEFAULT_SIZE,
Array choices =[],
Integer major_dimension = 0,
Integer style = RA_SPECIFY_COLS,
Validator validator = DEFAULT_VALIDATOR,
String name = “radioBox”)

Constructor, creating and showing a radiobox.
Parameters

* parent Parent window. Must not be nil.
* id Window identifier. A value of -1 indicates a default value.
* label Label for the static box surrounding the radio buttons.
* pos Window position. If the position (-1, -1) is specified then a 

default position is chosen.
* size Window size. If the default size (-1, -1) is specified then a
default size is chosen.
* choices An array of String choices with which to initialize the
radiobox.
* major_dimension Specifies the maximum number of rows (if style
contains RA_SPECIFY_ROWS) or columns (if style contains RA_SPECIFY_COLS)
for a two-dimensional radiobox.
* style Window style. See RadioBox.
* validator Window validator.
* name Window name.

The second place is in the tutorial below, which was the non-XRC program
that I was referring to in my original post. See the following excerpt
from that tutorial:

WxRubyWiki__Buttons tutorial contains the following:
Working with radio buttons
#March 29, 2009
wxruby 2.0.0
Ruby 1.8.6
#mac osx 10.4.11
#7stud
require “rubygems”
require “wx”
class MyFrame < Wx::Frame
def initialize
super(nil, #Parent
:title => “RadioBox Example”, #Displays on top of window
:pos => [150, 25], #or Wx::DEFAULT_POSITION
:size => [300, 200] #or Wx::DEFAULT_SIZE
#For the position and size arguments, you don’t need to specify
Point and Size
#objects anymore. See: wxRuby Overview on the doc page
wxruby_intro.html
)
panel = Wx::Panel.new(self) #Parent = self = this Frame
drink_choices = [“coffee”, “tea”, “juice”, “milk”] #labels for
radio buttons
radios = Wx::RadioBox.new(
panel, #Parent
:label => “Drinks”, #Label for box surrounding radio
buttons
:pos => [20, 5],
:size => Wx::DEFAULT_SIZE,
:choices => drink_choices, #The labels for the radio buttons
:major_dimension => 1, #Max number of cols(or rows)–try
changing it to 2
:style => Wx::RA_SPECIFY_COLS #:major_dimension value applies to
columns
#The :major_dimension and :style in combination determine the
layout of the
#RadioBox. In this case, the maximum number of columns is 1,
which limits
#the number of radio buttons to one per line. Because there are
4 radio buttons,
#that means there will be 4 lines, with one radio button per
line.
)
evt_radiobox(radios.get_id()) {|cmd_event|
on_change_radio(cmd_event)}
#When there is an event of type evt_radiobox on the widget whose id
is radios.get_id(),
#the block is called and an “event object” is passed to the block.
The block calls
#the method on_change_radio(), defined below, relaying the event
object to the
#method. The event object contains useful information about the
radio button
#that was selected.
@text_widget = Wx::StaticText.new(
panel, #Parent
:label => “coffee”,
:pos => [150, 25],
:size => Wx::DEFAULT_SIZE
#Store a widget in an instance variable if other
#methods, like the one below, need access to it.
)
show #equivalent to self.show, makes the frame visible
end
def on_change_radio(cmd_event)
selected_drink = cmd_event.string #Selected radio’s label
#Instead of calling methods like cmd_event.get_string() or
cmd_event.set_string() you
#can now call accessor methods with the same name as the property
you are trying to
#get or set. See: wxRuby Overview on the doc page wxruby_intro.html
@text_widget.label = selected_drink
end
end

class MinimalApp < Wx::App
def on_init
MyFrame.new
end
end

MinimalApp.new.main_loop #main_loop() tells wxruby to start reacting
#to events on your App, like radio button
selections,
#button clicks, etc.

So it seems that those ‘choices’ are arguments without associated
methods. I was unable to get ‘choices’ listed as an argument. I only
found ‘choices’ listed as an argument in an error message, where I had
changed ‘choices’ to another word.

By the way, what are your examples supposed to do?
@@instance_number |= 0
@@instance_number += 1
It seems to me that the first one creates a variable with a value of
true.
Executing the second one then generates an error, since it can’t
increment true.

Gary

On Mon, Nov 23, 2009 at 1:29 PM, Gary H. [email protected]
wrote:

  1. Why can’t I find anything within Wx::RadioBox that references
  2. Is there a way to add a new method to RadioBox from within the “App”

Keep in mind, that with the current version of XRCise, that if you need
def on_init
Thanks

Thanks for your help Mario!

Your examples helped me get my program working, and helped my
understanding.

Not a problem.

        Validator validator = DEFAULT_VALIDATOR,
  • size Window size. If the default size (-1, -1) is specified then a

#7stud
#objects anymore. See: wxRuby Overview on the doc page
:size => Wx::DEFAULT_SIZE,
4 radio buttons,
object to the
)
end
selections,
#button clicks, etc

These are examples of Named parameters, as the Official WxWidgets API,
the
construction of a wxRadioBox allows you to pass a wxList of Labels to
it.
There’s no official method in which choices are mapped, though
technically,
all you would need to do, is write the method choices, in a way that
enumerates the children of the wxRadioBox for retrieval, and a method in
which deletes the children, and adds new ones to it.


true.
Executing the second one then generates an error, since it can’t
increment true.

My apologies, I wrote that after I got home from work, when I was tired,
and
naturally, typo’ed the first line. The first line is suppose to read:
@@instance_number ||= 0, which means, unless there’s already a value
stored
in @@instance_number, assign 0 to it. The next line, @@instance_number
+=
1, is suppose to increment the number count of how many controls have
been
created.

In other words, if you have a dynamic control set being setup, and you
want
to monitor how many of the controls are created in a single run of the
Application, you can use that method to see. In any case, hope these
things
help you out further in your development. Also note, that there is one
method that I didn’t mention, as it’s not a method I personally like to
use,
but I wouldn’t be a good developer if I didn’t point it out.

While the on_init method is something that I added to xrcise, as it’s
something that I personally use, and my preferred way of access, and
limiting the number of redundant classes and subclasses around in the
name
space of the application, it may be dropped in future versions of
xrcise,
though I prefer it would remain in the code, as it doesn’t effect
anything
with the generated code.

The other method in which to add methods to the generated class, is to
simply create a new class definition, and subclass it to your XRC
Generated
class.

An Example:

require ‘my_xrc_class’

class MyRubyClass < MyXrcClass
def hello_world
self.set_label(“Hello World!”)
end
end

Going this route, won’t hurt anything, and I believe it is of personal
preference which method to use. Personally, I think the on_init method
is
in tune with the wxRuby framework, as it is used in Wx::App. But keep
in
mind, that the on_init method that is setup to execute from your XRCised
code, is only available in that generated code, it’s not native to any
specific Widget control implemented in wxRuby.

Gary

Posted via http://www.ruby-forum.com/.


wxruby-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wxruby-users

hth,

Mario