# FXDataTarget: am I doing this fine?‏

**URL:** <https://www.ruby-forum.com/t/fxdatatarget-am-i-doing-this-fine/225286>\
**Category:** FXRuby\
**Created:** [December 27, 2012, 1:07pm UTC](https://www.ruby-forum.com/t/fxdatatarget-am-i-doing-this-fine/225286 "2012-12-27T13:07:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [December 27, 2012, 1:07pm UTC](https://www.ruby-forum.com/t/fxdatatarget-am-i-doing-this-fine/225286/1 "2012-12-27T13:07:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DamiSSn\_MSG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@DamiSSn\_MSG](https://www.ruby-forum.com/u/DamiSSn_MSG)\
**Post date:** [December 28, 2012, 1:49pm UTC](https://www.ruby-forum.com/t/fxdatatarget-am-i-doing-this-fine/225286/2 "2012-12-28T13:49:09Z")

</div>

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 (C) 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!! 🙂
