home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / Commodities / Common / window.c < prev   
Encoding:
C/C++ Source or Header  |  1992-09-01  |  12.0 KB  |  497 lines

  1.  
  2.    /***********************************************************************
  3.    *                                                                      *
  4.    *                            COPYRIGHTS                                *
  5.    *                                                                      *
  6.    *   Copyright (c) 1990  Commodore-Amiga, Inc.  All Rights Reserved.    *
  7.    *                                                                      *
  8.    ***********************************************************************/
  9.  
  10. /* window.c -- Intuition Interface */
  11.  
  12. #include "local.h"
  13.  
  14. static BYTE dummy;
  15.  
  16.  
  17. #if WINDOW  /*##### All the following is disabled if the commodity #####*/
  18.             /*##### does not support a window.                     #####*/
  19.  
  20. struct Window   *window = NULL; /* our window */
  21.  
  22. void            *vi          = NULL;
  23. struct Screen   *myscreen    = NULL;
  24. SHORT           topborder;
  25. struct TextFont *font        = NULL;
  26. struct Gadget   *glist       = NULL;
  27. struct Menu     *menu        = NULL;
  28. BOOL            menuattached = NULL;
  29. struct DrawInfo *mydi        = NULL;
  30. BOOL            IDCMPRefresh = NULL;
  31.  
  32.                  /* save window positions and dims left,top,width,height  */
  33. static WORD savewindow[ 4 ]={WINDOW_LEFT,WINDOW_TOP,WINDOW_WIDTH,WINDOW_HEIGHT};
  34.  
  35. static char WindowTitle[255];  /* buffer to hold cooked window title */
  36.  
  37. /****i* Blank.ld/handleIMsg() ******************************************
  38. *
  39. *   NAME
  40. *        handleIMsg -- Handle window IDCMP messages.
  41. *
  42. *   SYNOPSIS
  43. *        handleIMsg(msg);
  44. *
  45. *        VOID handleIMsg(struct IntuiMessage *msg);
  46. *
  47. *   FUNCTION
  48. *        This function handles all the IntuiMessages for standard
  49. *        commodities functions. If the message is for an application
  50. *        Gadget or Menu the appropriate application function,
  51. *        handleCustomMenu() or HandleGadget(), is called.
  52. *
  53. *   INPUTS
  54. *        msg = The current IntuiMessage.
  55. *
  56. *   RESULT
  57. *        The appropriate action for the message is performed.
  58. *
  59. *   EXAMPLE
  60. *
  61. *   NOTES
  62. *
  63. *   BUGS
  64. *
  65. *   SEE ALSO
  66. *
  67. *****************************************************************************
  68. *
  69. */
  70. VOID handleIMsg(struct IntuiMessage *msg)
  71. {
  72.    ULONG   class;
  73.    UWORD   code;
  74.    struct  Gadget *gaddress;
  75.  
  76.    class    = msg->Class;
  77.    code     = msg->Code;
  78.    gaddress = (struct Gadget *) msg->IAddress;
  79.  
  80.    D1( kprintf("window.c: handleIMsg() enter\n") );
  81.  
  82.    GT_ReplyIMsg( (struct Msg *) msg );
  83.  
  84.    switch ( class )
  85.    {
  86.       case CLOSEWINDOW:
  87.          D1( printf("window.c: handleIMsg(CLOSEWINDOW)\n") );
  88.          shutdownWindow();
  89.          break;         /* not reached   */
  90.       case REFRESHWINDOW:
  91.          D1( printf("window.c: handleIMsg(REFRESHWINODW)\n") );
  92.          IDCMPRefresh=TRUE;
  93.          refreshWindow();
  94.          IDCMPRefresh=FALSE;
  95.          break;
  96.       case MENUPICK:
  97.          D1( printf("window.c: handleIMsg(MENUPICK)\n") );
  98.          handleCustomMenu(code);
  99.          break;
  100.       case GADGETUP:
  101.          D1( printf("window.c: handleIMsg(GADGETUP) GadgetID=%lx\n",gaddress->GadgetID) );
  102.          HandleGadget(gaddress->GadgetID & GADTOOLMASK,code);
  103.          break;
  104.    }
  105. }
  106. /****i* Blank.ld/setupWindow() ******************************************
  107. *
  108. *   NAME
  109. *        setupWindow -- Perform whatever steps necessary to make the
  110. *                       window visible.
  111. *
  112. *   SYNOPSIS
  113. *        setupWindow()
  114. *
  115. *        VOID setupWindow(VOID);
  116. *
  117. *   FUNCTION
  118. *        This function is used to make the window visible. If the window
  119. *        is not opened this function will open it. If the window is already
  120. *        open it will be brought to the front so it is visible. This
  121. *        routine handles all the ugliness of new look and changing window
  122. *        title bar font heights.
  123. *
  124. *   INPUTS
  125. *
  126. *   RESULT
  127. *
  128. *   EXAMPLE
  129. *
  130. *   NOTES
  131. *
  132. *   BUGS
  133. *
  134. *   SEE ALSO
  135. *
  136. *****************************************************************************
  137. *
  138. */
  139. VOID setupWindow()
  140. {
  141.    D1( printf("window.c: setupWindow() enter\n") );
  142.  
  143.    if(window)
  144.    {   
  145.       D1( printf("window.c: setupWindow() WindowToFront()\n") );
  146.       WindowToFront( window );
  147.       return;      /* already setup, nothing to re-init   */
  148.    }
  149.    if( ! myscreen)
  150.    {
  151.       if( ! (myscreen=LockPubScreen(NULL)))
  152.       {
  153.          D1( printf("window.c: setupWindow() could not LockPubScreen()\n") );
  154.          return;
  155.       }
  156.    }
  157.    D1( printf("window.c: setupWindow() LockPubScreen(NULL) = %lx\n",myscreen) );
  158.  
  159.    if( ! mydi)
  160.    {
  161.       if( ! (mydi=GetScreenDrawInfo(myscreen)))
  162.       {
  163.          D1( printf("window.c: setupWindow() could not GetScreenDrawInfo()\n") );
  164.          return;
  165.       }
  166.    }
  167.    D1( printf("window.c: setupWindow() GetScreenDrawInfo(0x%lx) = %lx\n",myscreen,mydi) );
  168.  
  169.    topborder=myscreen->WBorTop + (myscreen->Font->ta_YSize +1);
  170.    D1( printf("window.c: setupWindow() topborder = %ld\n",topborder) );
  171.  
  172.    if( ! vi)
  173.    {
  174.       if( ! (vi=GetVisualInfo(myscreen,TAG_DONE)))
  175.       {
  176.          D1( printf("window.c: setupWindow() could not GetVisualInfo()\n") );
  177.          goto EXIT;
  178.       }
  179.    }
  180.    D1( printf("window.c: setupWindow() GetVisualInfo() = %lx\n",vi) );
  181.  
  182.    if ( ! (window=getNewWindow()))
  183.    {
  184.       D1( printf("window.c: setupWindow() could not getNewWindow()\n") );
  185.       goto EXIT;
  186.    }
  187.    D1( printf("window.c: setupWindow() getNewWindow() = %lx\n",window) );
  188.  
  189.    iport    = window->UserPort;
  190.    isigflag = 1L << iport->mp_SigBit;
  191.  
  192.    addGadgets(window);
  193.  
  194.    setupCustomMenu();
  195.    if(menu)
  196.    {
  197.       if( ! LayoutMenus(menu,vi,TAG_DONE))
  198.       {
  199.          D1( printf("window.c: setupWindow() could not LayoutMenus()\n") );
  200.       }
  201.    }
  202.    menuattached=SetMenuStrip(window,menu);
  203.    D1( printf("window.c: setupWindow() SetMenuStrip() = %lx\n",menuattached) );
  204.  
  205.    refreshWindow();
  206.  
  207. EXIT:
  208. }
  209. /****i* Blank.ld/shutdownWindow() ******************************************
  210. *
  211. *   NAME
  212. *        shutdownWindow -- Perform the steps necessary to close the window.
  213. *
  214. *   SYNOPSIS
  215. *        shutdownWindow()
  216. *
  217. *        VOID shutdownWindow(VOID);
  218. *
  219. *   FUNCTION
  220. *        Closes the window and remembers its position for the next open.
  221. *
  222. *   INPUTS
  223. *
  224. *   RESULT
  225. *
  226. *   EXAMPLE
  227. *
  228. *   NOTES
  229. *
  230. *   BUGS
  231. *
  232. *   SEE ALSO
  233. *
  234. *****************************************************************************
  235. *
  236. */
  237. VOID shutdownWindow()
  238. {
  239.    WORD   *wp;
  240.  
  241.    D1( printf("window.c: shutdownWindow() enter\n") );
  242.  
  243.    if ( ! window )
  244.    {
  245.       D1( printf("window.c: shutdownWindow() window not open!\n") );
  246.       return;
  247.    }
  248.  
  249.    /* save window position   */
  250.    wp    = savewindow;
  251.    *wp++ = window->LeftEdge;
  252.    *wp++ = window->TopEdge;
  253.    *wp++ = window->Width;
  254.    *wp   = window->Height;
  255.  
  256.    if(menuattached)
  257.    {
  258.       ClearMenuStrip(window);
  259.       menuattached=NULL;
  260.    }
  261.    if(menu)
  262.    {
  263.       FreeMenus(menu);
  264.       menu=NULL;
  265.    }
  266.  
  267.    D1( printf("window.c: shutdownWindow() ClosingWindow(%lx)\n",window) );
  268.    CloseWindow(window);
  269.    D1( printf("window.c: shutdownWindow() after CloseWindow()\n") );
  270.    window   = NULL;
  271.    iport    = NULL;
  272.    isigflag = NULL;
  273.  
  274.    removeGadgets();
  275.    D1( printf("window.c: shutdownWindow() after removeGadgets()\n") );
  276.  
  277.    if(vi)
  278.    {
  279.       D1( printf("window.c: shutdownWindow() FreeVisualInfo(%lx)\n",vi) );
  280.       FreeVisualInfo(vi);
  281.       vi=NULL;
  282.    }
  283.    if(mydi)
  284.    {
  285.       D1( printf("window.c: shutdownWindow() FreeScreenDrawInfo(%lx)\n",mydi) );
  286.       FreeScreenDrawInfo(myscreen,mydi);
  287.       mydi=NULL;
  288.    }
  289.    if(myscreen)
  290.    {
  291.       D1( printf("window.c: shutdownWindow() UnlockPubScreen(%lx)\n",myscreen) );
  292.       UnlockPubScreen(NULL,myscreen);
  293.       myscreen=NULL;
  294.    }
  295. }
  296.  
  297. /****i* Blank.ld/getNewWindow() ******************************************
  298. *
  299. *   NAME
  300. *        getNewWindow -- Open the window remembering the old postition
  301. *                        if reopening.
  302. *
  303. *   SYNOPSIS
  304. *        window = getNewWindow();
  305. *
  306. *        struct Window *getNewWindow(VOID);
  307. *
  308. *   FUNCTION
  309. *        This function opens the commodities window. It automatically
  310. *        sets the window title to reflect the current HotKey. If the
  311. *        window has already been opened once then the previous position
  312. *        and size (if sizing is enabled) are used for this open.
  313. *
  314. *   INPUTS
  315. *        None.
  316. *
  317. *   RESULT
  318. *        Returns a pointer to the opened window or NULL on error.
  319. *
  320. *   EXAMPLE
  321. *
  322. *   NOTES
  323. *
  324. *   BUGS
  325. *
  326. *   SEE ALSO
  327. *        setupWindow();
  328. *        shutdownWindow();
  329. *
  330. *****************************************************************************
  331. *
  332. */
  333. struct  Window *getNewWindow()
  334. {
  335.    struct  NewWindow   nw;
  336.    WORD   *wp = savewindow;
  337.  
  338.    D1( printf("window.c: getNewWindow() enter\n") );
  339.  
  340.    sprintf(WindowTitle,"%s: HotKey=%s",COM_TITLE,hotkeybuff);
  341.  
  342.    nw.LeftEdge    = *wp++;
  343.    nw.TopEdge     = *wp++;
  344.    nw.Width       = *wp++;
  345.    nw.Height      = *wp++;
  346.    nw.DetailPen   = (UBYTE) -1;
  347.    nw.BlockPen    = (UBYTE) -1;
  348.    nw.IDCMPFlags  = IFLAGS;
  349.    nw.Flags       = WFLAGS;
  350.    nw.FirstGadget = NULL;
  351.    nw.CheckMark   = NULL;
  352.    nw.Title       = WindowTitle;
  353.    nw.Screen      = NULL;
  354.    nw.BitMap      = NULL;
  355.    nw.MinWidth    = WINDOW_MIN_WIDTH;
  356.    nw.MinHeight   = WINDOW_MIN_HEIGHT;
  357.    /* work around bug  */
  358.    nw.MaxWidth    = WINDOW_MAX_WIDTH;
  359.    nw.MaxHeight   = WINDOW_MAX_HEIGHT;
  360.    nw.Type        = WBENCHSCREEN;
  361.  
  362.    D1( printf("window.c: getNewWindow() before OpenWindowTags()\n") );
  363.  
  364.    return ((struct Window *) OpenWindowTags(&nw,
  365.                              WA_InnerHeight,WINDOW_INNERHEIGHT,
  366.                              WA_AutoAdjust,TRUE,WA_PubScreen,myscreen,TAG_DONE));
  367. }
  368. /****i* Blank.ld/addGadgets() ******************************************
  369. *
  370. *   NAME
  371. *        addGadgets -- Add all the standard and application specific
  372. *                      gadgets to the window.
  373. *
  374. *   SYNOPSIS
  375. *        result = addGadgets(window);
  376. *
  377. *        int addGadgets(struct Window *window);
  378. *
  379. *   FUNCTION
  380. *        Sets up the environment for gadget toolkit gadgets and calls
  381. *        addCustomGadgets() to add the application specific gadgets
  382. *        to the window.
  383. *
  384. *   INPUTS
  385. *        window = Pointer to the window.
  386. *
  387. *   RESULT
  388. *        Returns TRUE if all went well, FALSE otherwise.
  389. *
  390. *   EXAMPLE
  391. *
  392. *   NOTES
  393. *
  394. *   BUGS
  395. *
  396. *   SEE ALSO
  397. *        setupCustomGadgets();
  398. *        removeGadgets();
  399. *
  400. *****************************************************************************
  401. *
  402. */
  403. int addGadgets(struct Window *window)
  404. {
  405.    struct Gadget *gad;
  406.  
  407.    D1( printf("window.c: addGadgets() enter\n") );
  408.  
  409.    /*  Open desired font: */
  410.    if( ! font )
  411.    {
  412.       D1( printf("window.c: addGadgets() Opening font\n") );
  413.       if (!(font = OpenFont(&mydesiredfont)))
  414.       {
  415.          D1( printf("window.c: addGadgets() Could not open font\n") );
  416.          return(FALSE);
  417.       }
  418.    }
  419.  
  420.    gad = CreateContext(&glist);
  421.  
  422.    setupCustomGadgets(&gad);
  423.  
  424.    if(!gad)
  425.    {
  426.       D1( printf("window.c: addGadgets() error adding gadgets\n") );
  427.       if(glist)
  428.       {
  429.          FreeGadgets(glist);
  430.          glist=NULL;
  431.       }
  432.       if(font)
  433.       {
  434.          CloseFont(font);
  435.          font=NULL;
  436.       }
  437.       return(FALSE);
  438.    }
  439.  
  440.    AddGList(window, glist, ((UWORD) -1), ((UWORD) -1), NULL);
  441.    RefreshGList(window->FirstGadget, window, NULL, ((UWORD) -1));
  442.    GT_RefreshWindow(window,NULL);
  443.    return(TRUE);
  444. }
  445. /****i* Blank.ld/removeGadgets() ******************************************
  446. *
  447. *   NAME
  448. *        removeGadgets -- Remove and deallocate all standard and
  449. *                         application gadgets from the window.
  450. *
  451. *   SYNOPSIS
  452. *        removeGadgets()
  453. *
  454. *        VOID removeGadgets(VOID);
  455. *
  456. *   FUNCTION
  457. *        This function performs gadget cleanup. It is responsible for
  458. *        deallocating and removing all gadgets from the window before
  459. *        it is closed.
  460. *
  461. *   INPUTS
  462. *        None.
  463. *
  464. *   RESULT
  465. *        All gadgets are freed and removed from the window.
  466. *
  467. *   EXAMPLE
  468. *
  469. *   NOTES
  470. *        Uses the global variable glist which is a linked list off all
  471. *        the gadget toolkit gadgets.
  472. *
  473. *   BUGS
  474. *
  475. *   SEE ALSO
  476. *
  477. *****************************************************************************
  478. *
  479. */
  480. void removeGadgets()
  481. {
  482.    if(glist)
  483.    {
  484.       D1( kprintf("window.c: removeGadgets() FreeingGadgets(%lx)\n",glist) );
  485.       FreeGadgets(glist);
  486.       glist=NULL;
  487.    }
  488.  
  489.    if (font)
  490.    {
  491.       D1( kprintf("window.c: removeGadgets() Closing font %lx\n", font) );
  492.       CloseFont(font);
  493.       font=NULL;
  494.    }
  495. }
  496. #endif /* WINDOW */
  497.