home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / stdwin / Ports / mac / stdwin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-29  |  8.7 KB  |  444 lines  |  [TEXT/????]

  1. /* MAC STDWIN -- BASIC ROUTINES. */
  2.  
  3. #include "macwin.h"
  4. #ifdef MPW
  5. #include <Fonts.h>
  6. #include <Menus.h>
  7. #include <TextEdit.h>
  8. #include <Dialogs.h>
  9. #include <OSUtils.h>
  10. #include <SegLoad.h>
  11. #endif
  12.  
  13.  
  14. /* Parameters for top left corner choice algorithm in wopen() */
  15.  
  16. #define LEFT    20    /* Initial left */
  17. #define TOP    40    /* Initial top */
  18. #define HINCR    20    /* Increment for left */
  19. #define VINCR    16    /* Increment for top */
  20.  
  21.  
  22. /* GLOBAL DATA. */
  23.  
  24.             /* XXX choose less obvious names */
  25. GrafPtr screen;        /* The Mac Window Manager's GrafPort */
  26. TEXTATTR wattr;        /* Current or default text attributes */
  27.  
  28. static int def_left = 0;
  29. static int def_top = 0;
  30. static int def_width = 0;
  31. static int def_height = 0;
  32. static int def_horscroll = 0;
  33. static int def_verscroll = 1;
  34. static int next_left = LEFT;    /* For default placement algorithm */
  35. static int next_top = TOP;
  36.  
  37.  
  38. /* INITIALIZATION AND TERMINATION. */
  39.  
  40. /* Initialization */
  41.  
  42. int std_open_hook();
  43. STATIC pascal resume _ARGS((void));
  44.  
  45. /* Initialize, using and updating argc/argv: */
  46. void
  47. winitargs(pargc, pargv)
  48.     int *pargc;
  49.     char ***pargv;
  50. {
  51.     wargs(pargc, pargv);
  52.     winit();
  53. }
  54.  
  55. /* Initialize without touching argc/argv */
  56. void
  57. winit()
  58. {
  59. #ifndef NO_STDIO
  60.     /* Tell the THINK C stdio library we have initialized already */
  61.     Stdio_MacInit(TRUE);
  62. #endif
  63.  
  64. #ifdef THINK_C
  65. #ifndef THINK_C_3_0
  66.     /* 
  67.        THINK C 4.0 may have done these initializations for us when
  68.        the application has already used the console in any way.
  69.        Doing them again is not a good idea.
  70.        The THINK library avoids initializing the world if it appears
  71.        that it has already been initialized, but in that case it
  72.        will only allow output (all input requests return EOF).
  73.        Thus, the application has two options:
  74.        - call winit() or winitargs() first, then use the console
  75.          only for (debugging) output; or
  76.        - print at least one character to stdout first, then
  77.          stdwin menu bar may not function properly.
  78.        From inspection of the THINK library source it appears that
  79.        when the console is initialized, stdin->std is cleared,
  80.        so the test below suffices to skip initializations.
  81.     */
  82.     if (stdin->std)
  83. #endif
  84. #endif
  85.     {
  86.         MaxApplZone();
  87.         InitGraf(&QD(thePort));
  88.         InitFonts();
  89.         InitWindows();
  90.         TEInit();
  91.         InitDialogs(resume);
  92.         InitMenus();
  93.         InitCursor();
  94.         setup_menus();
  95.     }
  96.     GetWMgrPort(&screen);
  97.     initwattr();
  98. #ifdef MPW
  99.     set_open_hook(std_open_hook);
  100. #endif
  101.     set_watch();
  102. }
  103.  
  104. void
  105. wdone()
  106. {
  107. }
  108.  
  109. void
  110. wgetscrsize(pwidth, pheight)
  111.     int *pwidth, *pheight;
  112. {
  113.     Rect r;
  114.     r= screen->portRect;
  115.     *pwidth= r.right - r.left;
  116.     *pheight= r.bottom - r.top - MENUBARHEIGHT;
  117. }
  118.  
  119. void
  120. wgetscrmm(pmmwidth, pmmheight)
  121.     int *pmmwidth, *pmmheight;
  122. {
  123.     int width, height;
  124.     wgetscrsize(&width, &height);
  125.     /* XXX Three pixels/mm is an approximation of the truth at most */
  126.     *pmmwidth= width / 3;
  127.     *pmmheight= height / 3;
  128. }
  129.  
  130. int
  131. wgetmouseconfig()
  132. {
  133.     return WM_BUTTON1 | WM_SHIFT | WM_META | WM_LOCK | WM_OPTION;
  134.     /* XXX Should figure out if we have a Control button as well */
  135. }
  136.  
  137. /* Routine called by "Resume" button in bomb box (passed to InitDialogs).
  138.    I have yet to see a program crash where an attempted exit to the
  139.    Finder caused any harm, so I think it's safe.
  140.    Anyway, it's tremendously useful during debugging. */
  141.  
  142. static pascal
  143. resume()
  144. {
  145.     ExitToShell();
  146. }
  147.  
  148.  
  149. /* WINDOWS. */
  150.  
  151. /* Find the WINDOW pointer corresponding to a WindowPtr. */
  152.  
  153. WINDOW *
  154. whichwin(w)
  155.     WindowPtr w;
  156. {
  157.     if (((WindowPeek)w)->windowKind < userKind)
  158.         return NULL; /* Not an application-created window */
  159.     else {
  160.         WINDOW *win;
  161.         
  162.         win= (WINDOW *) GetWRefCon(w);
  163.         if (win != NULL && win->w == w)
  164.             return win;
  165.         else
  166.             return NULL;
  167.     }
  168. }
  169.  
  170. WINDOW *
  171. wopen(title, drawproc)
  172.     char *title;
  173.     void (*drawproc)();
  174. {
  175.     WINDOW *win= ALLOC(WINDOW);
  176.     Rect r;
  177.     int width, height;        /* As seen by the user */
  178.     int tot_width, tot_height;    /* Including slop & scroll bars */
  179.     int left, top;            /* Window corner as seen by the user */
  180.     
  181.     if (win == NULL) {
  182.         dprintf("wopen: ALLOC failed");
  183.         return NULL;
  184.     }
  185.     
  186.     /* Determine window size.
  187.        If the program specified a size, use that, within reason --
  188.        sizes are clipped to 0x7000 to avoid overflow in QuickDraw's
  189.        calculations.
  190.        Otherwise, use two-third of the screen size, but at most
  191.        512x342 (to avoid creating gigantic windows by default on
  192.        large screen Macs). */
  193.     
  194.     if (def_width <= 0) {
  195.         width = screen->portRect.right * 2/3;
  196.         CLIPMAX(width, 512);
  197.     }
  198.     else {
  199.         width = def_width;
  200.         CLIPMAX(width, 0x7000);
  201.     }
  202.     if (def_horscroll)
  203.         CLIPMIN(width, 3*BAR);
  204.     tot_width = width + LSLOP + RSLOP;
  205.     if (def_verscroll)
  206.         tot_width += BAR;
  207.     
  208.     if (def_height <= 0) {
  209.         height= screen->portRect.bottom * 2/3;
  210.         CLIPMAX(height, 342);
  211.     }
  212.     else {
  213.         height = def_height;
  214.         CLIPMAX(height, 0x7000);
  215.     }
  216.     if (def_verscroll)
  217.         CLIPMIN(height, 3*BAR);
  218.     tot_height = height;
  219.     if (def_horscroll)
  220.         tot_height += BAR;
  221.     
  222.     /* Determine window position.
  223.        It the program specified a position, use that, but make sure
  224.        that at least a small piece of the title bar is visible --
  225.        so the user can recover a window that "fell off the screen".
  226.        Exception: the title bar may hide completely under the menu
  227.        bar, since this is the only way to get an (almost) full
  228.        screen window.
  229.        Otherwise, place the window a little to the right and below
  230.        the last window placed; it it doesn't fit, move it up.
  231.        With default placement, the window will never hide under the
  232.        title bar. */
  233.     
  234.     if (def_left <= 0) {
  235.         left = next_left;
  236.         if (left + tot_width >= screen->portRect.right) {
  237.             left = LEFT;
  238.             CLIPMAX(left, screen->portRect.right - tot_width);
  239.             CLIPMIN(left, 0);
  240.         }
  241.     }
  242.     else {
  243.         left = def_left - LSLOP;
  244.         CLIPMAX(left, screen->portRect.right - BAR);
  245.         CLIPMIN(left, BAR - tot_width);
  246.     }
  247.     
  248.     if (def_top <= 0) {
  249.         top = next_top;
  250.         if (top + tot_height >= screen->portRect.bottom) {
  251.             top = TOP;
  252.             CLIPMAX(top, screen->portRect.bottom - tot_height);
  253.             CLIPMIN(top, MENUBARHEIGHT + TITLEBARHEIGHT);
  254.         }
  255.     }
  256.     else {
  257.         top = def_top;
  258.         CLIPMAX(top, screen->portRect.bottom);
  259.         CLIPMIN(top, MENUBARHEIGHT);
  260.     }
  261.     
  262.     next_left = left + HINCR;
  263.     next_top = top + VINCR;
  264.     
  265.     /* Create the window now and initialize its attributes */
  266.     
  267.     SetRect(&r, left, top, left + tot_width, top + tot_height);
  268.     win->w= NewWindow((Ptr)NULL, &r, PSTRING(title), TRUE, zoomDocProc,
  269.         (WindowPtr)(-1), TRUE, 0L);
  270.     SetWRefCon(win->w, (long)win);
  271.     
  272.     win->tag= 0;
  273.     win->drawproc= drawproc;
  274.     win->hcaret= win->vcaret= -1;
  275.     win->caret_on= FALSE;
  276.     win->attr= wattr;
  277.     win->hbar= win->vbar= NULL;
  278.     win->docwidth= 0;
  279.     win->docheight= 0;
  280.     win->orgh= -LSLOP;
  281.     win->orgv= 0;
  282.     win->timer= 0;
  283.     win->cursor = NULL;
  284.     win->fgcolor = _w_fgcolor;
  285.     win->bgcolor = _w_bgcolor;
  286.     
  287.     SetPort(win->w);
  288.     _w_usefgcolor(win->fgcolor);
  289.     _w_usebgcolor(win->bgcolor);
  290.     
  291.     initmbar(&win->mbar);
  292.     makescrollbars(win, def_horscroll, def_verscroll);
  293.     
  294.     return win;
  295. }
  296.  
  297. void
  298. wclose(win)
  299.     WINDOW *win;
  300. {
  301.     if (win == active) {
  302.         rmlocalmenus(win);
  303.         active= NULL;
  304.     }
  305.     killmbar(&win->mbar);
  306.     DisposeWindow(win->w);
  307.     FREE(win);
  308. }
  309.  
  310. void
  311. wgetwinsize(win, pwidth, pheight)
  312.     WINDOW *win;
  313.     int *pwidth, *pheight;
  314. {
  315.     Rect r;
  316.     
  317.     getwinrect(win, &r);
  318.     *pwidth= r.right - r.left - LSLOP - RSLOP;
  319.     *pheight= r.bottom - r.top;
  320. }
  321.  
  322. void
  323. wsetwinsize(win, width, height)
  324.     WINDOW *win;
  325.     int width, height;
  326. {
  327.     /* XXX not yet implemented */
  328. }
  329.  
  330. void
  331. wgetwinpos(win, ph, pv)
  332.     WINDOW *win;
  333.     int *ph, *pv;
  334. {
  335.     Point p;
  336.     GrafPtr saveport;
  337.     
  338.     GetPort(&saveport);
  339.     
  340.     SetPort(win->w);
  341.     p.h = win->w->portRect.left + LSLOP;
  342.     p.v = win->w->portRect.top;
  343.     LocalToGlobal(&p);
  344.     *ph = p.h;
  345.     *pv = p.v;
  346.     
  347.     SetPort(saveport);
  348. }
  349.  
  350. void
  351. wsetwinpos(win, left, top)
  352.     WINDOW *win;
  353.     int left, top;
  354. {
  355.     /* XXX not yet implemented */
  356. }
  357.  
  358. void
  359. wsettitle(win, title)
  360.     WINDOW *win;
  361.     char *title;
  362. {
  363.     SetWTitle(win->w, PSTRING(title));
  364. }
  365.  
  366. char *
  367. wgettitle(win)
  368.     WINDOW *win;
  369. {
  370.     static char title[256];
  371.     GetWTitle(win->w, title);
  372. #ifndef CLEVERGLUE
  373.     PtoCstr(title);
  374. #endif
  375.     return title;
  376. }
  377.  
  378. void
  379. wfleep()
  380. {
  381.     SysBeep(5);
  382. }
  383.  
  384. void
  385. wsetmaxwinsize(width, height)
  386.     int width, height;
  387. {
  388.     /* Not supported yet (should be stored in the window struct
  389.        and used by do_grow). */
  390.     /* XXX This procedure should disappear completely, it was
  391.        only invented for the Whitechapel which allocates bitmap
  392.        memory to the window when it is first created! */
  393.     /* XXX Well, maybe it has some use.  In fact both min and max
  394.        window size are sometimes useful... */
  395. }
  396.  
  397. void
  398. wsetdefwinpos(h, v)
  399.     int h, v;
  400. {
  401.     def_left = h;
  402.     def_top = v;
  403. }
  404.  
  405. void
  406. wgetdefwinpos(ph, pv)
  407.     int *ph, *pv;
  408. {
  409.     *ph = def_left;
  410.     *pv = def_top;
  411. }
  412.  
  413. void
  414. wsetdefwinsize(width, height)
  415.     int width, height;
  416. {
  417.     def_width= width;
  418.     def_height= height;
  419. }
  420.  
  421. void
  422. wgetdefwinsize(pwidth, pheight)
  423.     int *pwidth, *pheight;
  424. {
  425.     *pwidth = def_width;
  426.     *pheight = def_height;
  427. }
  428.  
  429. void
  430. wsetdefscrollbars(hor, ver)
  431.     int hor, ver;
  432. {
  433.     def_horscroll = hor;
  434.     def_verscroll = ver;
  435. }
  436.  
  437. void
  438. wgetdefscrollbars(phor, pver)
  439.     int *phor, *pver;
  440. {
  441.     *phor = def_horscroll;
  442.     *pver = def_verscroll;
  443. }
  444.