home *** CD-ROM | disk | FTP | other *** search
/ The Best of Mecomp Multimedia 1 / Mecomp-CD.iso / amiga / grafic / cyberblanker / source-code / window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-05  |  13.4 KB  |  545 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.    D1( printf("window.c: setupWindow() enter\n") );
  149.  
  150.    if(window)
  151.    {
  152.       D1( printf("window.c: setupWindow() WindowToFront()\n") );
  153.       WindowToFront( window );
  154.       return;      /* already setup, nothing to re-init   */
  155.    }
  156.    if( ! myscreen)
  157.    {
  158.       if( ! (myscreen=LockPubScreen(NULL)))
  159.       {
  160.          D1( printf("window.c: setupWindow() could not LockPubScreen()\n") );
  161.          return;
  162.       }
  163.    }
  164.    D1( printf("window.c: setupWindow() LockPubScreen(NULL) = %lx\n",myscreen) );
  165.  
  166.    if( ! mydi)
  167.    {
  168.       if( ! (mydi=GetScreenDrawInfo(myscreen)))
  169.       {
  170.          D1( printf("window.c: setupWindow() could not GetScreenDrawInfo()\n") );
  171.          return;
  172.       }
  173.    }
  174.    D1( printf("window.c: setupWindow() GetScreenDrawInfo(0x%lx) = %lx\n",myscreen,mydi) );
  175.  
  176.    topborder=myscreen->WBorTop + (myscreen->Font->ta_YSize +1);
  177.    D1( printf("window.c: setupWindow() topborder = %ld\n",topborder) );
  178.  
  179.    if( ! vi)
  180.    {
  181.       if( ! (vi=GetVisualInfo(myscreen,TAG_DONE)))
  182.       {
  183.          D1( printf("window.c: setupWindow() could not GetVisualInfo()\n") );
  184.          goto EXIT;
  185.       }
  186.    }
  187.    D1( printf("window.c: setupWindow() GetVisualInfo() = %lx\n",vi) );
  188.  
  189.    if ( ! (window=getNewWindow()))
  190.    {
  191.       D1( printf("window.c: setupWindow() could not getNewWindow()\n") );
  192.       goto EXIT;
  193.    }
  194.    D1( printf("window.c: setupWindow() getNewWindow() = %lx\n",window) );
  195.  
  196.    iport    = window->UserPort;
  197.    isigflag = 1L << iport->mp_SigBit;
  198.  
  199.    addGadgets(window);
  200.  
  201.  
  202.    setupCustomMenu();
  203.    if(menu)
  204.    {
  205.       if( ! LayoutMenus(menu,vi,
  206.             GTMN_NewLookMenus, TRUE,
  207.             TAG_DONE))
  208.  
  209.  
  210.       {
  211.          D1( printf("window.c: setupWindow() could not LayoutMenus()\n") );
  212.       }
  213.    }
  214.    menuattached=SetMenuStrip(window,menu);
  215.    D1( printf("window.c: setupWindow() SetMenuStrip() = %lx\n",menuattached) );
  216.  
  217.    refreshWindow();
  218.  
  219. EXIT: ;
  220. }
  221. /****i* Blank.ld/shutdownWindow() ******************************************
  222. *
  223. *   NAME
  224. *        shutdownWindow -- Perform the steps necessary to close the window.
  225. *
  226. *   SYNOPSIS
  227. *        shutdownWindow()
  228. *
  229. *        VOID shutdownWindow(VOID);
  230. *
  231. *   FUNCTION
  232. *        Closes the window and remembers its position for the next open.
  233. *
  234. *   INPUTS
  235. *
  236. *   RESULT
  237. *
  238. *   EXAMPLE
  239. *
  240. *   NOTES
  241. *
  242. *   BUGS
  243. *
  244. *   SEE ALSO
  245. *
  246. *****************************************************************************
  247. *
  248. */
  249. VOID shutdownWindow()
  250. {
  251.    WORD   *wp;
  252.  
  253.    D1( printf("window.c: shutdownWindow() enter\n") );
  254.  
  255.    if ( ! window )
  256.    {
  257.       D1( printf("window.c: shutdownWindow() window not open!\n") );
  258.       return;
  259.    }
  260.  
  261.    /* save window position   */
  262.    wp    = savewindow;
  263.    *wp++ = window->LeftEdge;
  264.    *wp++ = window->TopEdge;
  265.    *wp++ = window->Width;
  266.    *wp   = window->Height;
  267.  
  268.    if(menuattached)
  269.    {
  270.       ClearMenuStrip(window);
  271.       menuattached=NULL;
  272.    }
  273.    if(menu)
  274.    {
  275.       FreeMenus(menu);
  276.       menu=NULL;
  277.    }
  278.  
  279.    D1( printf("window.c: shutdownWindow() ClosingWindow(%lx)\n",window) );
  280.    CloseWindow(window);
  281.    D1( printf("window.c: shutdownWindow() after CloseWindow()\n") );
  282.    window   = NULL;
  283.    iport    = NULL;
  284.    isigflag = NULL;
  285.  
  286.    removeGadgets();
  287.    D1( printf("window.c: shutdownWindow() after removeGadgets()\n") );
  288.  
  289.    if(vi)
  290.    {
  291.       D1( printf("window.c: shutdownWindow() FreeVisualInfo(%lx)\n",vi) );
  292.       FreeVisualInfo(vi);
  293.       vi=NULL;
  294.    }
  295.    if(mydi)
  296.    {
  297.       D1( printf("window.c: shutdownWindow() FreeScreenDrawInfo(%lx)\n",mydi) );
  298.       FreeScreenDrawInfo(myscreen,mydi);
  299.       mydi=NULL;
  300.    }
  301.    if(myscreen)
  302.    {
  303.       D1( printf("window.c: shutdownWindow() UnlockPubScreen(%lx)\n",myscreen) );
  304.       UnlockPubScreen(NULL,myscreen);
  305.       myscreen=NULL;
  306.    }
  307. }
  308.  
  309. /****i* Blank.ld/getNewWindow() ******************************************
  310. *
  311. *   NAME
  312. *        getNewWindow -- Open the window remembering the old postition
  313. *                        if reopening.
  314. *
  315. *   SYNOPSIS
  316. *        window = getNewWindow();
  317. *
  318. *        struct Window *getNewWindow(VOID);
  319. *
  320. *   FUNCTION
  321. *        This function opens the commodities window. It automatically
  322. *        sets the window title to reflect the current HotKey. If the
  323. *        window has already been opened once then the previous position
  324. *        and size (if sizing is enabled) are used for this open.
  325. *
  326. *   INPUTS
  327. *        None.
  328. *
  329. *   RESULT
  330. *        Returns a pointer to the opened window or NULL on error.
  331. *
  332. *   EXAMPLE
  333. *
  334. *   NOTES
  335. *
  336. *   BUGS
  337. *
  338. *   SEE ALSO
  339. *        setupWindow();
  340. *        shutdownWindow();
  341. *
  342. *****************************************************************************
  343. *
  344. */
  345. struct  Window *getNewWindow()
  346. {
  347.    struct  NewWindow   nw;
  348.    WORD   *wp = savewindow;
  349.    D1( printf("window.c: getNewWindow() enter\n") );
  350.  
  351.    sprintf(WindowTitle,"%s:Hotkey = %s",COM_TITLE,hotkeybuff);
  352.  
  353.    nw.LeftEdge    = *wp++;
  354.    nw.TopEdge     = *wp++;
  355.    nw.Width       = *wp++;  
  356.    nw.Height      = *wp++;
  357.    nw.DetailPen   = (UBYTE) -1;
  358.    nw.BlockPen    = (UBYTE) -1;
  359.    nw.IDCMPFlags  = IFLAGS;
  360.    nw.Flags       = WFLAGS;
  361.    nw.FirstGadget = NULL;
  362.    nw.CheckMark   = NULL;
  363.    nw.Title       = WindowTitle;
  364.    nw.Screen      = myscreen;   
  365.    nw.BitMap      = NULL;
  366.    nw.MinWidth    = WINDOW_MIN_WIDTH;
  367.    nw.MinHeight   = WINDOW_MIN_HEIGHT;
  368.    /* work around bug  */
  369.    nw.MaxWidth    = WINDOW_MAX_WIDTH;
  370.    nw.MaxHeight   = WINDOW_MAX_HEIGHT;
  371.    nw.Type        = PUBLICSCREEN;
  372.  
  373.   
  374.    D1( printf("window.c: getNewWindow() before OpenWindowTags()\n") );
  375.  
  376.   
  377.                                    /**************************/
  378.  
  379.  
  380.  
  381.    return ((struct Window *) OpenWindowTags( &nw,
  382.                                        /*  WA_Left,WINDOW_LEFT,
  383.                                          WA_Top,WINDOW_TOP,
  384.                                          WA_Width,WINDOW_WIDTH,
  385.                                          WA_Height,WINDOW_HEIGHT, */
  386.                                          WA_AutoAdjust,TRUE,
  387.                              CYCLEIDCMP,
  388.                               WA_Gadgets,  
  389.                               WA_NewLookMenus, TRUE,
  390.                               GTMN_NewLookMenus, TRUE,
  391.                               WA_PubScreenName, (LONG) start_on_publicscreen,
  392.                               WA_PubScreenFallBack, TRUE,
  393.                               TAG_DONE));
  394. }
  395.  
  396.  
  397. /****i* Blank.ld/addGadgets() ******************************************
  398. *
  399. *   NAME
  400. *        addGadgets -- Add all the standard and application specific
  401. *                      gadgets to the window.
  402. *
  403. *   SYNOPSIS
  404. *        result = addGadgets(window);
  405. *
  406. *        int addGadgets(struct Window *window);
  407. *
  408. *   FUNCTION
  409. *        Sets up the environment for gadget toolkit gadgets and calls
  410. *        addCustomGadgets() to add the application specific gadgets
  411. *        to the window.
  412. *
  413. *   INPUTS
  414. *        window = Pointer to the window.
  415. *
  416. *   RESULT
  417. *        Returns TRUE if all went well, FALSE otherwise.
  418. *
  419. *   EXAMPLE
  420. *
  421. *   NOTES
  422. *
  423. *   BUGS
  424. *
  425. *   SEE ALSO
  426. *        setupCustomGadgets();
  427. *        removeGadgets();
  428. *
  429. *****************************************************************************
  430. *
  431. */
  432. int addGadgets(struct Window *window)
  433. {
  434.    struct Gadget *gad;
  435.  
  436.    D1( printf("window.c: addGadgets() enter\n") );
  437.   /*******************************/
  438.    {
  439.     LONG longval;
  440.  
  441.         customtattr.ta_Style = 0;
  442.         customtattr.ta_Flags = 0;
  443.         customtattr.ta_Name = fontname_tool_type;
  444.         stcd_l(fontsize_tool_type, &longval);
  445.         customtattr.ta_YSize = longval;
  446.         mydesiredfont = &customtattr;
  447.      }
  448.  
  449.     
  450.    /****************************/
  451.    /*  Open desired font: */
  452.    if( ! customfont )
  453.    {
  454.       D1( printf("window.c: addGadgets() Opening font\n") );
  455.       if (!(customfont = OpenFont(mydesiredfont)))
  456.       {
  457.  
  458.         ErrorF ("Could not open font %s %ld\n",
  459.                 customtattr.ta_Name,
  460.                 customtattr.ta_YSize);
  461.            terminate();
  462.       }
  463.    }
  464.  
  465.    gad = CreateContext(&glist);
  466.  
  467.    setupCustomGadgets(&gad);
  468.  
  469.    if(!gad)
  470.    {
  471.       D1( printf("window.c: addGadgets() error adding gadgets\n") );
  472.       if(glist)
  473.       {
  474.          FreeGadgets(glist);
  475.          glist=NULL;
  476.       }
  477.  
  478.     
  479.       if(customfont)
  480.       {
  481.          CloseFont(customfont);
  482.          customfont=NULL;
  483.       }
  484.       return(FALSE);
  485.    }
  486.  
  487.    AddGList(window, glist, ((UWORD) -1), ((UWORD) -1), NULL);
  488.    RefreshGList(window->FirstGadget, window, NULL, ((UWORD) -1));
  489.    GT_RefreshWindow(window,NULL);
  490.    return(TRUE);
  491. }
  492. /****i* Blank.ld/removeGadgets() ******************************************
  493. *
  494. *   NAME
  495. *        removeGadgets -- Remove and deallocate all standard and
  496. *                         application gadgets from the window.
  497. *
  498. *   SYNOPSIS
  499. *        removeGadgets()
  500. *
  501. *        VOID removeGadgets(VOID);
  502. *
  503. *   FUNCTION
  504. *        This function performs gadget cleanup. It is responsible for
  505. *        deallocating and removing all gadgets from the window before
  506. *        it is closed.
  507. *
  508. *   INPUTS
  509. *        None.
  510. *
  511. *   RESULT
  512. *        All gadgets are freed and removed from the window.
  513. *
  514. *   EXAMPLE
  515. *
  516. *   NOTES
  517. *        Uses the global variable glist which is a linked list off all
  518. *        the gadget toolkit gadgets.
  519. *
  520. *   BUGS
  521. *
  522. *   SEE ALSO
  523. *
  524. *****************************************************************************
  525. *
  526. */
  527. void removeGadgets()
  528. {
  529.    if(glist)
  530.    {
  531.       D1( kprintf("window.c: removeGadgets() FreeingGadgets(%lx)\n",glist) );
  532.       FreeGadgets(glist);
  533.       glist=NULL;
  534.    }
  535.  
  536.    if (customfont)
  537.    {
  538.       D1( kprintf("window.c: removeGadgets() Closing font %lx\n", font) );
  539.       CloseFont(customfont);
  540.       customfont=NULL;
  541.      
  542.    }
  543. }
  544. #endif /* WINDOW */
  545.