home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / CloseWindowSafely.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  2.0 KB  |  76 lines

  1. /* This example shows the CloseWindowSafely() function.  Use this
  2. ** function to close any windows that share an IDCMP port with another
  3. ** window.
  4. **
  5. ** CloseWindowSafely.c
  6. **
  7. ** these functions close an Intuition window that shares a port with other
  8. ** Intuition windows.
  9. **
  10. ** We are careful to set the UserPort to NULL before closing, and to free
  11. ** any messages that it might have been sent.
  12. */
  13. #include "exec/types.h"
  14. #include "exec/nodes.h"
  15. #include "exec/lists.h"
  16. #include "exec/ports.h"
  17. #include "intuition/intuition.h"
  18.  
  19. /*
  20. ** function to remove and reply all IntuiMessages on a port that have been
  21. ** sent to a particular window (note that we don't rely on the ln_Succ
  22. ** pointer of a message after we have replied it)
  23. */
  24. VOID StripIntuiMessages(struct MsgPort *mp, struct Window *win)
  25. {
  26. struct IntuiMessage *msg;
  27. struct Node *succ;
  28.  
  29. msg = (struct IntuiMessage *)mp->mp_MsgList.lh_Head;
  30.  
  31. while (succ = msg->ExecMessage.mn_Node.ln_Succ)
  32.     {
  33.     if (msg->IDCMPWindow == win)
  34.         {
  35.         /* Intuition is about to free this message.
  36.         ** Make sure that we have politely sent it back.
  37.         */
  38.         Remove(msg);
  39.  
  40.         ReplyMsg(msg);
  41.         }
  42.     msg = (struct IntuiMessage *)succ;
  43.     }
  44. }
  45.  
  46. /*
  47. ** Entry point to CloseWindowSafely()
  48. ** Strip all IntuiMessages from an IDCMP which are waiting for a specific
  49. ** window.  When the messages are gone, set the UserPort of the window to
  50. ** NULL and call ModifyIDCMP(win,0).  This will free the Intuition arts of
  51. ** the IDMCMP and trun off message to this port without changing the
  52. ** original UserPort (which may be in use by other windows).
  53. */
  54. VOID CloseWindowSafely(struct Window *win)
  55. {
  56. /* we forbid here to keep out of race conditions with Intuition */
  57. Forbid();
  58.  
  59. /* send back any messages for this window  that have not yet been
  60. ** processed
  61. */
  62. StripIntuiMessages(win->UserPort, win);
  63.  
  64. /* clear UserPort so Intuition will not free it */
  65. win->UserPort = NULL;
  66.  
  67. /* tell Intuition to stop sending more messages */
  68. ModifyIDCMP(win, 0L);
  69.  
  70. /* turn multitasking back on */
  71. Permit();
  72.  
  73. /* Now it's safe to really close the window */
  74. CloseWindow(win);
  75. }
  76.