home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 20 / amigaformatcd20.iso / -readerstuff- / steve_glover / source-code / window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-20  |  13.4 KB  |  547 lines

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