Deleting a widget from inside its callback (was RE: [Plib-devel] PUI questions)
Brought to you by:
sjbaker
From: John F. F. <joh...@eg...> - 2000-08-02 13:20:34
|
I had the problem of the demo program crashing when I tried to close the dialog box. (I use MSVC 6.0, mea culpa.) I fixed it by adding a linked list of "puObjects" which I called "objects_to_delete". Then I made one function "puDeleteObject" which simply added the object (and any children objects if it was a puGroup) to the "objects_to_delete" list. Finally, I made a function "puCleanUpJunk" which deleted everything on the "objects_to_delete" list. The "puDeleteObject" function is called from within the widget's callback and the "puCleanUpJunk" function is called in "puKeyboard" after the "checkKey" call and in both "puMouse" functions after the "checkHit" calls. Here is the new code: puObject *objects_to_delete = NULL; // Pointer to linked list of objects to delete // as a result of keyboarding or mouse clicking void puDeleteObject ( puObject *ob ) { puGroup *parent = ob->getParent () ; if ( parent != ob && parent != NULL ) // Remove from parent interface parent -> remove ( ob ) ; ob -> prev = NULL ; // Add to linked list to be deleted ob -> next = objects_to_delete ; objects_to_delete = ob ; ob -> setParent ( NULL ) ; } void puCleanUpJunk () { // Step through the linked list of objects to delete, removing them. if (objects_to_delete) { for ( ; objects_to_delete != NULL ; ) { puObject *next_ob = objects_to_delete ->next ; delete objects_to_delete ; objects_to_delete = next_ob ; } } } Here are the changes to puKeyboard and puMouse: int puKeyboard ( int key, int updown ) { int return_value = puGetBaseLiveInterface () -> checkKey ( key, updown ) ; puCleanUpJunk () ; return return_value ; } static int last_buttons = 0 ; int puMouse ( int button, int updown, int x, int y ) { puCursor ( x, y ) ; int h = puGetWindowHeight () ; if ( updown == PU_DOWN ) last_buttons |= ( 1 << button ) ; else last_buttons &= ~( 1 << button ) ; int return_value = puGetBaseLiveInterface () -> checkHit ( button, updown, x, h - y ) ; puCleanUpJunk () ; return return_value ; } int puMouse ( int x, int y ) { puCursor ( x, y ) ; if ( last_buttons == 0 ) return FALSE ; int button = (last_buttons & (1<<PU_LEFT_BUTTON )) ? PU_LEFT_BUTTON : (last_buttons & (1<<PU_MIDDLE_BUTTON)) ? PU_MIDDLE_BUTTON : (last_buttons & (1<<PU_RIGHT_BUTTON )) ? PU_RIGHT_BUTTON : -1 ; int h = puGetWindowHeight () ; int return_value = puGetBaseLiveInterface () -> checkHit ( button, PU_DRAG, x, h - y ) ; puCleanUpJunk () ; return return_value ; } John F. Fay joh...@eg... -----Original Message----- From: Dave McClurg [mailto:dp...@ef...] Sent: Tuesday, August 01, 2000 21:38 To: pli...@li... Subject: RE: [Plib-devel] PUI questions <snip> any thoughts on how to deal with deleting widgets inside a callback? it seems so natural to get rid of widgets inside the callback when a button is pressed. i'll figure *something* out if there are no approaches you already have in mind. <snip> --Dave _______________________________________________ plib-devel mailing list pli...@li... http://lists.sourceforge.net/mailman/listinfo/plib-devel |