On 02/03/12 15:08, testalucida wrote:
> That's my layout by column of Fl_Output widgets in a Fl_Group:
> |_|          <--- measurement returns w = 0 ==> BAD

        Hmm, when add a printf() to show the wi/he values
        from the code Ian posted, it always printed non-zero values,
        so I guess I'm not able to get that w=0.

        Here's what I get when I run his example on linux
        with the printf() added:

$ ./iantest
label: Label 1:
label: Label 2:
label: Label 3:
WI/HE=33 14     <-- label #1's width
WI/HE=72 14     <-- label #2's width
WI/HE=47 14     <-- label #3's width

        Still, I think you can/should rewrite this class
        so as to avoid using resize() inside draw().

        If I were writing this, I'd remove the draw() code completely,
        because it seems the goal of the app is to auto-size the widget
        based on the value() passed to it, not change how the widget draws 
itself.

        I'd suggest overloading value() instead, to measure the string
        and resize the widget whenever the string's contents is changed.

        This avoids having resize() inside draw(), which many
        folks here agree should be avoided.

        Here's what I'd recommend, and again, not getting w==0 when I run it:

// testalucida's measure with Ian's changes,
// Erco added changes to remove draw() and repurpose value() instead.
// fltk-config --compile measure-test.cxx

#include <FL/Fl.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Output.H>
#include <FL/fl_draw.H>

#include <string>
#include <vector>

class MyDisplay : public Fl_Output {

public:
    MyDisplay(int x, int y, int w, int h, const char *lbl) : 
Fl_Output(x,y,w,h,lbl) {
        textsize(11);
    }

    // Set the value, auto-size the widget to the width of the string
    void value(const char *val) {
        fl_font(textfont(), textsize());
        int wi=0, he=0;
        fl_measure(val, wi, he, 0);
        printf("WI/HE=%d %d\n", wi,he);         // DEBUGGING
        resize(x(), y(), wi+6, h());
        Fl_Output::value(val);
    }
};

class TestMyDisplay : public Fl_Group {
public:
    TestMyDisplay(int X, int Y, int W, int H):Fl_Group(X, Y, W, H) {
        end();
    }
    void setData(std::vector<std::string> &labels,
                 std::vector<std::string> &values) {
        begin();
        for (int i=0, imax=labels.size(); i<imax; i++) {
            fprintf(stderr, "label: %s\n", labels[i].c_str());
            MyDisplay *pDy = new MyDisplay(150, y()+3+i*30, 150, 25, 
labels[i].c_str());
            pDy->value(values[i].c_str());
        }
        end();
    }
};

int main() {
    Fl_Double_Window *pWin = new Fl_Double_Window(100, 100, 300, 300, 
"TestMyDisplay");
    TestMyDisplay *pDisp = new TestMyDisplay(0, 0, 300, 300);
    pWin->show();

    std::vector<std::string> labels, values;
    labels.push_back("Label 1: ");
    labels.push_back("Label 2: ");
    labels.push_back("Label 3: ");

    values.push_back("Val 1: ");
    values.push_back("TestValue 2: ");
    values.push_back("Value 3: ");

    pDisp->setData(labels, values);

    return Fl::run();
}
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to