Michèle Garoche
2008-Nov-11 17:25 UTC
[fxruby-users] FXTextField and number of digits after decimal point
Hello, Here''s the context: I read a number either pure integer or float with eventually up to 12 digits after the decimal point from a YAML file. The number is injected into a FXTextField with the following code: @value_sel = FXDataTarget.new(@value.to_s) @value_value = FXTextField.new(@matrix_mem_value, 20, at value_sel, :opts =>TEXTFIELD_NORMAL|JUSTIFY_RIGHT, :selector => FXDataTarget::ID_VALUE) where @value is the number as read from the YAML file. If I use a TEXTFIELD_NORMAL, I get all the digits after the decimal point, but then it becomes difficult to test if the user entry thereafter will be correct. I I use a TEXTFIELD_INTEGER, like below, I get only three digits after the decimal point. @value_sel = FXDataTarget.new(@value) @value_value = FXTextField.new(@matrix_mem_value, 20, at value_sel, :opts =>TEXTFIELD_INTEGER|JUSTIFY_RIGHT, :selector => FXDataTarget::ID_VALUE) If I use a TEXTFIELD_REAL, I get only seven digits after the decimal point. My question is: Is there a way to use either a TEXTFIELD_INTEGER or a TEXTFIELD_REAL without losing digits after the decimal point? Note: the number I use for testing purpose is 400.12345678. Thanks in advance for any hint. Cheers, Mich?le <http://micmacfr.homeunix.org> ? Cheers, Mich?le <http://micmacfr.homeunix.org> -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/fxruby-users/attachments/20081111/ea9d0087/attachment-0002.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: 36C471DED4B09EEB30A0281F2608DB2FE6F9E147.gpgkey Type: application/octet-stream Size: 1744 bytes Desc: not available URL: <http://rubyforge.org/pipermail/fxruby-users/attachments/20081111/ea9d0087/attachment-0001.obj> -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/fxruby-users/attachments/20081111/ea9d0087/attachment-0003.html>
Lyle Johnson
2008-Nov-12 00:58 UTC
[fxruby-users] FXTextField and number of digits after decimal point
On Nov 11, 2008, at 11:25 AM, Mich?le Garoche wrote:> My question is: Is there a way to use either a TEXTFIELD_INTEGER or > a TEXTFIELD_REAL without losing digits after the decimal point?The lack of display precision has to do with the fact that you''re using an FXDataTarget to update the value of the text field. When the FXTextField sets its text from the double-precision float value that it gets from the FXDataTarget, it uses a fixed format like: sprintf(str, "%.*G", 6, value); To use a custom formatting, you''d need to stop using FXDataTarget and instead use a combination of SEL_COMMAND and SEL_UPDATE, e.g. @value_value.connect(SEL_COMMAND) do @value = @value_value.text.to_f end @value_value.connect(SEL_UPDATE) do @value_value.text = @value.to_s # or however you want to format it end Hope this helps, Lyle
Joel VanderWerf
2008-Dec-06 00:31 UTC
[fxruby-users] FXTextField and number of digits after decimal point
Lyle Johnson wrote:> > On Nov 11, 2008, at 11:25 AM, Mich?le Garoche wrote: > >> My question is: Is there a way to use either a TEXTFIELD_INTEGER or a >> TEXTFIELD_REAL without losing digits after the decimal point? > > The lack of display precision has to do with the fact that you''re using > an FXDataTarget to update the value of the text field. > > When the FXTextField sets its text from the double-precision float value > that it gets from the FXDataTarget, it uses a fixed format like: > > sprintf(str, "%.*G", 6, value); > > To use a custom formatting, you''d need to stop using FXDataTarget and > instead use a combination of SEL_COMMAND and SEL_UPDATE, e.g. > > @value_value.connect(SEL_COMMAND) do > @value = @value_value.text.to_f > end > > @value_value.connect(SEL_UPDATE) do > @value_value.text = @value.to_s # or however you want to format it > end > > Hope this helps, > > LyleSorry, this is a late response, but for the record: If you don''t want to give up the functionality of data targets, you could do this using FoxTails (http://redshift.sourceforge.net/foxtails/). The "observable" attribute and the #field method combine to give you similar functionality: attrs that sync with their gui representation, and which can be observed by your own handlers. Specifying precision is just a bonus. Here''s an example and screenshot: require ''foxtails'' include Fox include FoxTails class Thing extend Observable observable :x, :y end class FieldsWindow < FXMainWindow include FTField observable :z, :msg def initialize(*args) super thing = Thing.new thing.x = 1.23456 thing.y = 1.23456789 self.z = 1.23456789 field("x = %6.2f and y = %12.8f and z = %f", [thing, :x], [thing, :y], :z) field("message: %60s", :msg) thing.when_y CHANGES do |val, old_val| if old_val self.msg = "You changed y from #{old_val} to #{val}" end end end def create super show end end class FieldsApp < FTApp def initialize super("Fields", "TEST") FieldsWindow.new(self, "Fields") end end FieldsApp.new.run -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407 -------------- next part -------------- A non-text attachment was scrubbed... Name: fields.png Type: image/png Size: 14333 bytes Desc: not available URL: <http://rubyforge.org/pipermail/fxruby-users/attachments/20081205/177a5104/attachment.png>
Michèle Garoche
2008-Dec-06 06:55 UTC
[fxruby-users] FXTextField and number of digits after decimal point
Le 6 d?c. 2008 ? 01:31, Joel VanderWerf a ?crit :> Lyle Johnson wrote: >> On Nov 11, 2008, at 11:25 AM, Mich?le Garoche wrote: >>> My question is: Is there a way to use either a TEXTFIELD_INTEGER >>> or a TEXTFIELD_REAL without losing digits after the decimal point? >> The lack of display precision has to do with the fact that you''re >> using an FXDataTarget to update the value of the text field. >> When the FXTextField sets its text from the double-precision float >> value that it gets from the FXDataTarget, it uses a fixed format >> like: >> sprintf(str, "%.*G", 6, value); >> To use a custom formatting, you''d need to stop using FXDataTarget >> and instead use a combination of SEL_COMMAND and SEL_UPDATE, e.g. >> @value_value.connect(SEL_COMMAND) do >> @value = @value_value.text.to_f >> end >> @value_value.connect(SEL_UPDATE) do >> @value_value.text = @value.to_s # or however you want to >> format it >> end >> Hope this helps, >> Lyle > > Sorry, this is a late response, but for the record: > > If you don''t want to give up the functionality of data targets, you > could do this using FoxTails (http://redshift.sourceforge.net/ > foxtails/). > > The "observable" attribute and the #field method combine to give > you similar functionality: attrs that sync with their gui > representation, and which can be observed by your own handlers. > Specifying precision is just a bonus. Here''s an example and > screenshot: > > require ''foxtails'' > > include Fox > include FoxTails > > class Thing > extend Observable > observable :x, :y > end > > class FieldsWindow < FXMainWindow > include FTField > observable :z, :msg > > def initialize(*args) > super > > thing = Thing.new > thing.x = 1.23456 > thing.y = 1.23456789 > > self.z = 1.23456789 > > field("x = %6.2f and y = %12.8f and z = %f", > [thing, :x], [thing, :y], :z) > > field("message: %60s", :msg) > > thing.when_y CHANGES do |val, old_val| > if old_val > self.msg = "You changed y from #{old_val} to #{val}" > end > end > end > > def create > super > show > end > end > > class FieldsApp < FTApp > def initialize > super("Fields", "TEST") > FieldsWindow.new(self, "Fields") > end > end > > FieldsApp.new.runThank you very much. Cheers, Mich?le <http://micmacfr.homeunix.org> -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/fxruby-users/attachments/20081206/8880dd96/attachment-0001.html>