[fxruby-users] FXDataTarget: am I doing this fine?

7 views
Skip to first unread message

Damián M. González

unread,
Dec 27, 2012, 5:40:17 AM12/27/12
to Fox C++ users list, FXRuby User List
 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) {锟斤拷sender, selector, data锟斤拷 some_label.text=(@data_target.value)}

 This doe sn'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. 

Damián M. González

unread,
Dec 28, 2012, 7:48:00 AM12/28/12
to Jeroen, FXRuby User List, Fox C++ users list
> 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.
>
> 3) 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
>
>
>
&g t; +----------------------------------------------------------------------------+
> | 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 simples 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 it 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!! :) 

Reply all
Reply to author
Forward
0 new messages