On 18.10.2010, at 12:49, Paul R wrote:

I first got the seg fault calling activate() on the input fields directly 
within the callback for the button., i only added the reset function to try and 
get around the issue or simply see if any difference.
will this seg fault be heirarchy related also do you think ?

Yes, it's the same for both callbacks (BTW. Ian, well-done,
I overlooked that inheritance issue at the first look).

I append a modified and completetd version of your program,
with all (I hope) essential changes marked with "***". I tried
to do only minimal changes w/o rewriting the widget hierarchy,
as Ian did. You may compare both versions and see what fits
your intentions better (no competition intended ;-) ).

Mainly I changed parent() to window(), so that you could later
add intermediate groups w/o needing to change the program. Since
the window can't be cast to SetupTest, I stored the "this" pointer
in the window's user_data() member. That's not how I would do it
in a working program, however, it's just to show how it *could*
be done. YMMV.

Albrecht
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Int_Input.H>
#include <stdlib.h>
#include <stdio.h>

class SetupTest
{
    public:
    Fl_Window* SetupWindow;
    Fl_Button* ResetButton;     // ***
    Fl_Int_Input *inputA;
    Fl_Int_Input *inputB;

    bool state;
    int valCount;
    void InputCb_i(Fl_Widget* wgt, void* v);
    static void InputCb(Fl_Widget* wgt, void* v);

        void ButtonCb_i(Fl_Widget* wgt, void* v);
    static void ButtonCb(Fl_Widget* wgt, void* v);

        void Reset();

    SetupTest();
};

SetupTest::SetupTest()
{
    SetupWindow = new Fl_Window(300, 200);
    SetupWindow->user_data(this);       // ***
    ResetButton = new Fl_Button(226, 20, 64, 20, "Reset"); // ***
    inputA = new Fl_Int_Input(215, 50, 20, 24, "InputA");
    inputA->tooltip("enter number A");
    inputA->type(2);
    inputA->when(FL_WHEN_RELEASE);
    inputB = new Fl_Int_Input(215, 80, 20, 24, "InputB");
    inputB->tooltip("enter number B");
    inputB->type(2);
    inputB->when(FL_WHEN_RELEASE);

    valCount = 0;
    state = false;

    inputA->callback((Fl_Callback*) InputCb);
    inputB->callback((Fl_Callback*) InputCb);
    ResetButton->callback((Fl_Callback*) ButtonCb);

    SetupWindow->end ();
    SetupWindow->show ();
}

void SetupTest::InputCb_i(Fl_Widget* w, void* v)
{
    Fl_Int_Input* inp = (Fl_Int_Input*) w;
    char* p;
    long int longI = 0;

    if (inp->changed())
    {
        inp->clear_changed();
        longI = strtol(inp->value(), &p, 10);
        printf("%s '%s'\n",inp->label(),inp->value());

        printf("%ld\n",longI);
        inp->deactivate();
        valCount++;
    }

    if(valCount == 2)
    {
        printf("READY\n");
        valCount = 0;
    }

}
void SetupTest::InputCb(Fl_Widget* wgt, void* v)
{
    ((SetupTest*)(wgt->window()->user_data()))->InputCb_i(wgt, v); // ***
}
void SetupTest::ButtonCb_i(Fl_Widget* w, void* v)
{
    Fl_Button* button = (Fl_Button*) w;
    if(!state)
    {
        Reset();
        state = true;
    }
        else
                state = false;
}
void SetupTest::ButtonCb(Fl_Widget* wgt, void* v)
{
    ((SetupTest*)(wgt->window()->user_data()))->ButtonCb_i(wgt, v); // ***
}
void SetupTest::Reset()
{
        printf("Reset!\n");
        inputB->activate();
        inputA->activate();
}
int main (int argc, char ** argv)
{
    setvbuf (stdout,NULL,_IONBF,0); // stdout unbuffered for Cygwin/MinGW
    SetupTest testObj;

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

Reply via email to