Search code examples
iup

IUP, mouse events on matrix


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?


Solution

  • 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.