Right-justify value in Fl_Value_Output

已查看 22 次
跳至第一个未读帖子

Fady Megally

未读,
2021年5月31日 11:05:192021/5/31
收件人 fltk.general
I apologize if I am missing something obvious here but does Fl_Value_Output allow for changing the justification of the value out of the box ? trying to right justify the 7-seg oil temp number here. will have other value below and want the decimals to be aligned.

Untitled window_013.png

Thanks !

Albrecht Schlosser

未读,
2021年5月31日 11:55:532021/5/31
收件人 fltkg...@googlegroups.com
On 5/31/21 4:04 PM Fady Megally wrote:
> I apologize if I am missing something obvious here but does
> Fl_Value_Output allow for changing the justification of the value out
> of the box ? trying to right justify the 7-seg oil temp number here.
> will have other value below and want the decimals to be aligned.

This is a good question (nothing to apologize). I don't think that we
have an option to draw the contents (value()) of any Fl_Input or
Fl_Output widgets right aligned.

However, you could also use an Fl_Box widget with its label aligned
inside the box (FL_ALIGN_INSIDE | FL_ALIGN_RIGHT) which might (almost)
do what you want. It would align the label at the right side of the box
but not the decimal points in one line unless you always have the same
number of decimals and you are using a fixed font (which might be true
in your case).

Other than that you might need to derive your own class and write your
own draw() method. fl_measure() or fl_text_extents() may help you to get
the (partial) string sizes and position the strings accordingly (left
and right of the decimal point). Or you measure the part before and
including the decimal point and draw it right aligned to some pixel
offset and the decimals right of that. Or ... well, there are lots of
options, but that's likely some code you need to write yourself. Unless
I'm missing something.

fl_measure:
https://www.fltk.org/doc-1.4/group__fl__drawings.html#gaccd92d0c0521b0aac30dcb856d438dcd

fl_text_extents:
https://www.fltk.org/doc-1.4/group__fl__attributes.html#ga14cb75a92b6cdd576f9512b38a208f8b

Ian MacArthur

未读,
2021年5月31日 16:02:392021/5/31
收件人 fltkg...@googlegroups.com
On 31 May 2021, at 15:04, Fady Megally wrote:
>
> I apologize if I am missing something obvious here but does Fl_Value_Output allow for changing the justification of the value out of the box ? trying to right justify the 7-seg oil temp number here. will have other value below and want the decimals to be aligned.


As Albrecht says, I don't think there’s specific support for this in the fltk widgets, if you actually want the digits to line up neatly.
And (FWIW) right-aligning LtoR text and numbers can often turn out to be surprisingly awkward, especially if the font face you are using is proportional.

TBH, when I was doing something broadly similar to this (well, it had a “7-segment LED” type effect anyway) I ended up deriving my own class from Fl_Box that was a single digit, then making groups of those for the various displays.
In that way it was trivial to control the exact position of each digit and ensure they always stayed aligned.

In any case, to get a nice effect you are probably best to derive your own class, so that you can have full control of the rendering and get better control of the digit positions.


Greg Ercolano

未读,
2021年6月1日 22:38:342021/6/1
收件人 fltkg...@googlegroups.com

On 5/31/21 7:04 AM, Fady Megally wrote:

I apologize if I am missing something obvious here but does Fl_Value_Output allow for changing the justification of the value out of the box ? trying to right justify the 7-seg oil temp number here. will have other value below and want the decimals to be aligned.

    If you use an Fl_Output with a fixed width font like FL_COURIER, you can use sprintf()
    to pad the floating point value to a fixed number of digits so that it right justifies, e.g.:

        sprintf(s, "%6.1f", float_val);
        output->value(s);

    ..and make sure the Fl_Value_Output is large enough to show that many digits.
    Complete example below:



#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Output.H>

Fl_Window *G_win = 0;
Fl_Output *G_out = 0;

void Timer_CB(void*)
{
    // Running floating point number that changes each timer tick
    static float f = 0;
    f += 10.3;
    if ( f > 500 ) f = 0;

    // Right padded 6 digit
    char s[80]; sprintf(s, "%6.1f", f);
    G_out->value(s);

    Fl::repeat_timeout(0.5, Timer_CB);
}

int main()
{
    // Window
    G_win = new Fl_Window(640,480);
    G_win->color(0xa0a0a000);   // bg

    // Digit Display
    G_out = new Fl_Output(10,10,120,40,"Oil Temp");
    G_out->align(FL_ALIGN_CENTER|FL_ALIGN_BOTTOM);
    G_out->labelcolor(FL_WHITE);
    G_out->color(FL_BLACK);
    G_out->textcolor(FL_GREEN);
    G_out->textfont(FL_COURIER);
    G_out->textsize(24);

    G_win->show();                 // ADDED
    Fl::add_timeout(0.5, Timer_CB);
    return Fl::run();           // ADDED
}

回复全部
回复作者
转发
0 个新帖子