[comp.windows.x] unstable code

gjw@ANDREAS.WR.USGS.GOV (Gregory J. Woodhouse) (12/28/90)

In my application I have several "menu panes" across the top of my main
window.  When I click on one of them they are repainted with the background
set to a stipple, and when I release the mouse button, a callback function
is executed.  Unfortunately, following Johnson & Reichard's book I chose to
call these things buttons, which is a little confusing.  Anyway, the
following routine examines events which occur on the window of one of these
"buttons".  Yesterday, I thought it worked fine, but when I came in this
morning I had a bit of a rude awakening: the button would be repainted
with the stipple, but that is all.  My application did NOT "freeze"; other
event handling would procede normally.  Now for the weird part:  I put in
two printf statements to examine the mouse_button variable, and it again
worked fine.  I could swear that at one point I commented them out and the
program worked, but if I eldeleted the comments it would not.  At first I
thought that in some cases a printf might cause an XFlush, but I appear to
be getting all my events.  Besides, the following code works (this time).
Presumably, the program works or doesn't work for some unrelated reason.
Do you see what's going on here?

ButtonEvent(display, event)
Display *display;
XEvent *event;

{  /* ButtonEvent -- Determine if event is relevant to a push button
      and dispatch it. */

   int which_button;

   /* These static variables are used to remember the state of the mouse
      between ButtonPress and subsequent ButtonRelease events */
   static Bool pressed = False;
   static int mouse_button;

   switch(event->type) {
       case Expose:
           which_button = ButtonFind(display, event->xexpose.window);
           if ((which_button >= 0) && (which_button < buttons_used)) {
               ButtonRedraw(display, which_button);
               return(True);   /* event dispatched */
           }
           break;
       case ButtonPress:
           which_button = ButtonFind(display, event->xbutton.window);
           if ((which_button >= 0) && (which_button < buttons_used)) {
           pressed = True;
           /* Which button is depressed? */
           mouse_button = event->xbutton.button;
           ButtonHighlight(display, which_button);
               return(True);   /* event dispatched */
           }
           break;
       case ButtonRelease:
           which_button = ButtonFind(display, event->xbutton.window);
           if ((which_button >= 0) && (which_button < buttons_used)) {
               pressed = False;
               if (event->xbutton.button == mouse_button)
                   ButtonExec(display, which_button);
               return(True);   /* event dispatched */
           }
   }

   XFlush(display);
   return(False);      /* event not dispatched */
}


-----------------------------------------------------------------------------
Gregory Woodhouse          |We know that the center of the earth
gjw@andreas.wr.usgs.gov    |is a fiery molten mass...but it's not
(415) 329-4694 (office)    |good to dwell on it.
(415) 325-7802 (home)      |
U.S. Geological Survey / 345 Middlefield Rd. MS 977 / Menlo Park, CA 94025
-----------------------------------------------------------------------------