FXDataTarget: am I doing this fine?‏

Hi guys. Have a question to do, been trying to use FXDataTarget to
handle a group of #FXLabel showing information. Imagine a group of
buttons, each one represents one day in the current month, so will be
around 30 of them. When the user press a button, the groups of labels
will show information, different depending of which button was pressed
last. So the first solution that I though is: “okay… let’s make use of
FXDataTarget… for the first time”. I did something like this:

@data_target = FXDataTarget.new

@buttons = {}

31.times do │time│
@buttons[time] = FXButton.new(some_packer, time.to_s,
target: @data_target, selector: FXDataTarget::ID_OPTION.+(time))
end

@data_target.connect(SEL_COMMAND) do │sender, selector, data│
some_label.text=(@data_target.value)
end

This doesn’t work, in the label appear a nil value, I also printed it
in the console, and of course it prints nil. Am I doing something wrong?
I expected to the label text change to “1” 'till “31”, but doesn’t.
Thanks for your time.

The thread go on…

Ok, I’m not 100% up on Ruby binding particulars, but some
observations:

  1. FXDataTarget should be connected to a variable (not a value) that will >will
    not
    go out of scope sooner that FXDataTarget itself does. The reason is
    FXDataTarget
    keeps a pointer to this variable, so it can modify or read its value.

  2. A SEL_COMMAND (or in some cases, a SEL_CHANGED) from a widget can change the
    value of the connected variable (if there is one).

This works in either of two ways:

a) an ID_VALUE message is received from the widget; the FXDataTarget will
send ID_GETINTVALUE, ID_GETREALVALUE, or ID_GETSTRINGVALUE to the widget,
depending on the type of the connected variable.

b) an ID_OPTION + i message is received from the widget. In this case, >the
value is expected to be an integer type, and is plucked from the message
itself by subtracting the ID_OPTION. So the value is i in this case.

  1. A SEL_UPDATE from a widget causes the widget’s value to be updated >through
    the datatarget, from the value of its connected variable:

a) if the message is ID_VALUE, a message is sent to the widget; the
message id depends on the type of variable connected to the datatarget:
ID_SETINTVALUE is sent for small integer variables, ID_SETLONGVALUE is
sent for long integers, ID_SETREALVALUE is sent for floating point
variables, and ID_SETSTRINGVALUE is sent for string variables.

b) if the message is ID_OPTION + i, the variable connected to the data-
target is compared to i, and the widget is sent ID_CHECK or ID_UNCHECK
if the value is equal or unequal to i.

Hope this helps,

– JVZ

±----------------------------------------------------------------------->----+
| Copyright © 11:40 12/27/2012 Jeroen van der Zijp. All Rights >Reserved. |
±----------------------------------------------------------------------->----+

Okay, the explanation is very clean. I don’t know how to interpret the
point 3, since that is not easy to see how it works, because this
happen:

@datatarget = FXDataTarget.new
@textwidget = FXTextField.new(parent, 10, target: @datatarget,
selector: FXDataTarget::ID_VALUE)

#so if I do

@datatarget.value=(“Hello”)

#it is supposed that the @textwidget will be filled with “Hello” right?
#because of the selector established, right? the ID_VALUE. It happens.
#so what happen if I change the selector to an ID_OPTION

@textwidget.selector=(FXDataTarget::ID_OPTION.+(1))

#what happen now?

@datatarget.value=(“Hi there”)

#nothing happens
#I’ve tried with another widget

@checkbutton = FXCheckButton.new(parent, nil, target: @datatarget,
selector: FXDataTarget::ID_VALUE)
@datatarget.value=(“Hi”)

#nothing happens

@datatarget.value=(true)

#it becomes checked!!
#if I change the selector to an ID_OPTION the widget will only activate
#when it receives the number asigned to it in his ‘selector’

@checkbutton.selector=(FXDataTarget::ID_OPTION.+(1))
@datatarget.value=(“House”)

#nothing happens, but…

@datatarget.value=(1)

#the widget becomes checked, interesting right? Is not a simple
behaviour, but now I have it more clear

So, what I wanted to acomplish was not right with FXDataTargets, I just
found another solution: I give each button a different selector:
FXWindow::ID_LAST.+(1), and go on…
Then I didn’t establish any target for them, so the message is catched
by them own. So to each button I did something like this:

@a_button.connect(SEL_COMMAND) do │sender, selector, data│
do_an_action(FXSELID(selector).-(FXWindow::ID_LAST))
end

So in that way I can identify which button was pressed an do some
action related.
I’m very thankfull for the explanation, see you around!! :slight_smile: