Frage

I have basic confusion in understanding of IUP events system. Now I am talking about matrix.

This is how it is created:

Ihandle *create_mat(void)
{
mat = IupMatrix(NULL);

IupSetAttribute(mat, "READONLY", "YES");
IupSetCallback(mat, "CLICK_CB", (Icallback)click);
IupSetCallback(mat, "BUTTON_CB", (Icallback)button);
return mat;
}

Here are callbacks:

int click(Ihandle *mat, int lin, int col)
{
char* value = IupMatGetAttribute(mat, "", lin, col);
if (!value) value = "NULL";
printf("click_cb(%d, %d)\n", lin, col);
return IUP_DEFAULT;
}

int button(Ihandle *mat, int button, int pressed, int x, int y, char* status)
{
printf("button %d, %d, %d, %d %s\n", button, pressed, x, y, status);
return IUP_DEFAULT;
}

Problem is in that I need both callbacks active but in showed situation CLICK event isn't fired.
If I disable BUTTON_CB then CLICK event is fired. But I need both, for click, left button doubleclick, right button release etc...

Is this normal behavior that BUTTON_CB excludes CLICK_CB or I do something wrong?

Actually, how would I get "lin" and "col" from inside BUTTON_CB or WHEEL_CB handler of matrix if CLICK_CB, ENTERITEM_CB and LEAVEITEM_CB which gives lin and col is not available (not fired in described situation)?

And more, how would I get "active control" (name, type of control with focus) from event handlers used on form's level?

War es hilfreich?

Lösung

Is this normal behavior that BUTTON_CB excludes CLICK_CB or I do something wrong?

Yes, it is. Because the BUTTON_CB is a IupCanvas callback and CLICK_CB is a IupMatrix callback. Remeber that IupMatrix inherits from IupCanvas. So internally IupMatrix is using the BUTTON_CB callback to implement several features.

So in this case what you have to do is to save the previous callback before assigning a new one, and call the old one from inside your own. Something like this:

old_callback = IupGetCallback(mat, "BUTTON_CB");
IupSetCallback(mat, "BUTTON_CB", new_callback);

int new_callback(...)
{
  return old_callback(...)
}

Actually, how would I get "lin" and "col" from inside BUTTON_CB or WHEEL_CB handler of matrix if CLICK_CB, ENTERITEM_CB and LEAVEITEM_CB which gives lin and col is not available (not fired in described situation)?

Use the function pos = IupConvertXYToPos(mat, x, y) where pos=lin*numcol + col. To compute lin and col is quite simple considering they are integer values.

And more, how would I get "active control" (name, type of control with focus) from event handlers used on form's level?

I don't fully undestood your question. But I think that IupGetFocus and IupGetClassName could be the functions you want.

Andere Tipps

I will answer to my own question by using Antonio's advices so other people interesting for IUP can have benefit from those posts.

If I understand well, what is not likely, here is how I make BUTTON_CB handler for my matrix:

int button(Ihandle *mat, int button, int pressed, int x, int y, char* status)
{
//actually 'name' is Ihandle
//and class name is a 'type'
//in compare with other toolkits
char* name = IupGetClassName(mat);

//so since we have handle already
//we can't be here if we are not in concrete matrix
//and this comparision is not needed
if (strncmp(name, "matrix", sizeof(name)) == 0)
{
    //if left mouse button is down
    if (button == IUP_BUTTON1 && pressed == 1)
    {
        //my calculation is not 100% correct
        //but good enough for this sample
        int pos = IupConvertXYToPos(mat, x, y);
        _line = pos/numcol;
        _col = pos%numcol;

        //if is doubleclick
        if (status[5] == 'D')
        {
            //press ENTER key
            //and open another modal dialog
            //with argument 'sel'
            k_any(mat, K_CR);

            printf("Doubleclick\n");
            //say HANDLED for IUP
            //but not matter when READONLY is "YES"
            return IUP_IGNORE;
        }

        //calculate (public) sel
        //for select a clicked line
        sel = _line + from - 1;
        refreshl(from, sel);

        printf("Click\n");
        return IUP_IGNORE;
    }
}
return IUP_DEFAULT;
}

This work's as expected.
Please further suggestion if something is not OK.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top