Ecere SDK/eC Forums
http://ec-lang.org/community/
Print view

Numeric check
http://ec-lang.org/community/viewtopic.php?f=1&t=414
Page 1 of 1
Author:  shakeshuck [ Wed Dec 16, 2015 9:21 am ]
Post subject:  Numeric check

Hi again,

Is there an isNumeric for strings or something similar? FloatFromString needs some input vetting before it's used.

I can do it by hand, but just thought I'd ask as I would've thought a lot of folk would use it?
If I'm the first then of course I could be wrong... :lol:
Author:  jerome [ Wed Dec 16, 2015 9:57 am ]
Post subject:  Re: Numeric check

Hi shakeshuck,

There are some options here.
eC being a C super-set, anything C will work (e.g. strtod(), strtol()).

The current eC way is probably calling OnGetDataFromString() on your numeric type:

Code: Select all

float x;
if(a.OnGetDataFromString(string))
   PrintLn("Successfully parsed float value: ", x);
This will however return success even if there are further characters after a numeric value.
Use strtol() or strtod() for finer control, which returns a pointer saying where the parsing ended.

You may also want to look into using a SavingDataBox with a data type set to float or double, which will automatically convert to numbers and reject invalid input.

Regards,

-Jerome
Author:  shakeshuck [ Wed Dec 16, 2015 11:04 am ]
Post subject:  Re: Numeric check

jerome wrote:You may also want to look into using a SavingDataBox with a data type set to float or double, which will automatically convert to numbers and reject invalid input.
The input fields are ListBoxes; I did initially try setting the data type, but then discovered alignment doesn't work if you do. :(
(With reference to the other thread on number alignment, in this case I wanted the value centered).
Use strtol() or strtod() for finer control, which returns a pointer saying where the parsing ended.
I'll take a look at these, thanks.
Author:  jerome [ Wed Dec 16, 2015 1:34 pm ]
Post subject:  Re: Numeric check

If you set the data type to e.g. float for a ListBox field, the data will normally be displayed with the alignment you setup for that field. But only when editing that data, it will be left-aligned.

This could be a work around for now, if you want the editing to be e.g. right-aligned as well.

Code: Select all

class MyFloat : float
{
   Window OnEdit(DataBox dataBox, DataBox obsolete, int x, int y, int w, int h, void * userData)
   {
      EditBox editor = (EditBox)class::OnEdit(dataBox, obsolete, x, y, w, h, userData);
      editor.anchor = { top = 2, right = -4 };
      editor.autoSize = true;
      editor.OnPostCreate();
      return editor;
   }
}
However, this requires a tweak in DataBox.ec, otherwise the DataBox will currently unset that autoSize property if autoSize is not set on the DataBox itself (and auto-size DataBox don't work well with the ListBox):

Code: Select all

if(autoSize) // Need to add this if check
   ((EditBox)editor).autoSize = autoSize;
This is the line in the source:
https://github.com/ecere/ecere-sdk/blob ... ox.ec#L132

Then this should work:

Code: Select all

DataField fld3 { dataType = class(MyFloat), alignment = right, width = 120, editable = true };
Author:  shakeshuck [ Wed Dec 16, 2015 4:16 pm ]
Post subject:  Re: Numeric check

jerome wrote:If you set the data type to e.g. float for a ListBox field, the data will normally be displayed with the alignment you setup for that field. But only when editing that data, it will be left-aligned.
That's what I thought, but it isn't doing that for me; with alignment set to center, centering (at display time) is not occurring if I change the dataType to int or float.
Of course it's highly possible I've unset something somewhere else that's causing this to happen... :roll:
Author:  jerome [ Thu Dec 17, 2015 12:03 am ]
Post subject:  Re: Numeric check

Please post some sample code to compare.
If you follow my example above it should work.
(But you will need the custom type and the libecere tweak to get the 'edited' data box to follow the alignment as well)
Author:  shakeshuck [ Thu Dec 17, 2015 5:10 am ]
Post subject:  Re: Numeric check

Video sent, using same MakeActive sample as before...

Cheers. :)
Author:  jerome [ Fri Dec 18, 2015 1:41 am ]
Post subject:  Re: Numeric check

Hi shakeshuck,

I see what you mean.
What happens is that the data types have a 'default alignment', and setting the dataType property after the alignment will set it to that default alignment. If you change the code to set the alignment after the dataType it will work again.

Regards,

Jerome
All times are UTC-05:00 Page 1 of 1