[comp.windows.x] Solution to Synching EVENTS on Text Items

parn@cyclone.mitre.org (W. L. Parn) (06/10/91)

Funny you should ask, I have been struggling with this problem for several days.
I finally found that Text Items are handled slight different than the standard
Panel Item.  They have a static function that gets called when an event takes
place on them.  So what must be done is that you must save the address to
this static function and reset the current address to your event function.
Now you event function will get called and you can process the events you
want or send them on to the static funciton you have just saved.

Here is some sample code that grabs the arrow keys and sends all other events
on to the static text item functon.

--------------------------------------------------------------------------
#include <xview/xview.h>
#include <xview/panel.h>
#include <xview/textsw.h>

Panel_item text[4];
Frame frame;
Panel panel;

main(argc, argv)
int argc;
char *argv[];
{
    char buf[16];
    int i;
    Panel_setting text_item_event_proc();

    xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, NULL);

    frame = (Frame) xv_create(NULL, FRAME,
        FRAME_LABEL,            argv[0],
        FRAME_SHOW_FOOTER,      TRUE,
        XV_SHOW,                TRUE,
        NULL);

    panel = (Panel)xv_create(frame, PANEL,
        XV_X,   0,
        XV_Y,   0,
        XV_WIDTH,       400,
        XV_HEIGHT,      300,
        XV_SHOW,        TRUE,
        NULL);

   /*... These panel text items have a special event procedure that ...*/
    /*... is a static function.  Save this static fucntion in the    ...*/
    /*... PANEL_CLIENT_DATA pointer so it can be recalled and the    ...*/
    /*... reset the function address using PANEL_EVENT_PROC          ...*/
    for (i=0; i<4; i++)
    {
        text[i] = (Panel_item)xv_create(panel, PANEL_TEXT,
            XV_X,       1,
            XV_Y,       5+i*20,
            PANEL_LABEL_STRING,         "Text Item :",
            PANEL_VALUE_DISPLAY_LENGTH, 20,
            PANEL_VALUE,                        "X",
            NULL);

        xv_set(text[i],
            PANEL_CLIENT_DATA,  (void (*)())xv_get(text[i], PANEL_EVENT_PROC),
            PANEL_EVENT_PROC,           text_item_event_proc,
            NULL);
    }

    xv_main_loop(frame);
}


Panel_setting text_item_event_proc(item, event)
Panel_item      item;
Event           *event;
{
    void (*func)() = (void(*)())xv_get(item, PANEL_CLIENT_DATA);
    int i;

    /*... Our event procedure does not care about up keys            ...*/
    /*... An key is the event of releasing and previously pressed key ..*/

    if (event_is_up(event))
    {
        /*... call Text Items standard static event procedure.       ...*/
        (func)(item, event);
        return;
    }

    if (event_action(event) != '\033')
        printf("[%c] [%d]\n",event_action(event),event_action(event));

    switch (event_action(event)) {
        case 31761: /* Left  Arrow */
        case 31762: /* Right Arrow */
        case 31752: /* Up    Arrow */
        case 31753: /* Down  Arrow */
            fprintf(stderr,"Text Item got arrow key\n");
            return(PANEL_NEXT);
        default:
        {
            /*... call Text Items standard static event procedure.         ...*/
            (func)(item, event);
        }
    }
    fprintf(stderr,"AT END\n");
}

---------------------------------------------------------------------------
This should be the solution to your problem Richard



parn@mwunix.mitre.org