home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / progut~1 / stdwin.zoo / atari / window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-28  |  9.1 KB  |  531 lines

  1. #include <stdio.h>
  2. #include "window.h"
  3. #include "trees.h"
  4. #include <osbind.h>
  5. #include "atari_proto.h"
  6. #undef P
  7.  
  8. #define MIN_WIDTH    100
  9. #define MIN_HEIGHT    100
  10.  
  11. WINDOW    **winlist ;
  12. int    nrwin = 0 ;
  13. WINDOW    *active = NULL ;
  14.  
  15. int    vdi_handle = -1 ;
  16.  
  17. static Rect    def ;
  18.  
  19. static int    max_width ;
  20. static int    max_height ;
  21.  
  22. static int    pix_width ;
  23. static int    pix_height ;
  24.  
  25. int    scr_width ;
  26. int    scr_height ;
  27.  
  28. static int    lborder ;
  29. static int    tborder ;
  30. static int    rborder ;
  31. static int    bborder ;
  32.  
  33. static int    chwidth ;
  34. int    chheight ;
  35. static int    bxwidth ;
  36. static int    bxheight ;
  37.  
  38. /* variables used by the vdi library */
  39. #ifndef __GNUC__
  40. int    contrl[12] ;
  41. int    intin[128] ;
  42. int    intout[128] ;
  43. int    ptsin[128] ;
  44. int    ptsout[128] ;
  45. #endif
  46.  
  47. extern char    *strdup () ;
  48.  
  49. void
  50. winit ()
  51. {
  52.     Rect    inside ;
  53.     Rect    outside ;
  54.     int    i ;
  55.     int    workin[11] ;
  56.     int    workout[57] ;
  57.     int    dummy ;
  58.  
  59.     if (appl_init () < 0)
  60.         return ;
  61.  
  62.     /* Get the size of characters and the screen size */
  63.  
  64.     vdi_handle = graf_handle (&chwidth, &chheight, &bxwidth, &bxheight) ;
  65.  
  66.     workin[0] = Getrez() + 2;
  67.     for (i = 1; i < 10; i++)
  68.         workin[i] = 1 ;
  69.     workin[10] = 2 ;
  70.     v_opnvwk (workin, &vdi_handle, workout) ;
  71.  
  72.     scr_width = workout[0] + 1 ;
  73.     scr_height = workout[1] + 1 ;
  74.  
  75.     pix_width = workout[3] ;
  76.     pix_height = workout[4] ;
  77.  
  78.     /*
  79.     ** Some atributes for drawing...
  80.     */
  81.     vsf_perimeter (vdi_handle, 1) ;
  82.  
  83.     vsl_color (vdi_handle, 1) ;
  84.     vsl_type (vdi_handle, 1) ;
  85.  
  86.     vst_color (vdi_handle, 1) ;
  87.     vst_alignment (vdi_handle, 0, 5, &dummy, &dummy) ;
  88.  
  89.     outside.x = 0 ;
  90.     outside.y = BAR_HEIGHT ;
  91.     outside.w = scr_width ;
  92.     outside.h = scr_height - BAR_HEIGHT ;
  93.  
  94. /*    wind_calc (1, DEF_OPT, outside, &inside.x, &inside.y, &inside.w,
  95.                                 &inside.h) ;
  96. */
  97.     wind_calc (1, DEF_OPT, outside.x, outside.y, outside.w,
  98.            outside.h, &inside.x, &inside.y, &inside.w, &inside.h) ;
  99.  
  100.     lborder = inside.x - outside.x ;
  101.     tborder = inside.y - outside.y ;
  102.     rborder = outside.x + outside.w - (inside.x + inside.w) ;
  103.     bborder = outside.y + outside.h - (inside.y + inside.h) ;
  104.  
  105.     initglobmenus () ;
  106.  
  107.     /* Set window positions and sizes */
  108.  
  109.     wsetdefwinpos (0, 0) ;
  110.     wsetmaxwinsize (scr_width, scr_height) ;
  111.     wsetdefwinsize (scr_width * 2/3, scr_height * 2/3) ;
  112.  
  113.     wsetplain () ;
  114.  
  115.     if (!graf_mouse (BUSY_BEE, &dummy))
  116.         wdebug ("winit: mouse busy bee") ;
  117. }
  118.  
  119. void
  120. winitnew (pargc, pargv)
  121.     int    *pargc ;
  122.     char    **pargv[] ;
  123. {
  124.     /*
  125.     ** Dummy call because some versions of STDWIN want acces to
  126.     ** argc and argv
  127.     */
  128.  
  129.     winit () ;
  130. }
  131.  
  132. void
  133. wdone ()
  134. {
  135.     int    i ;
  136.  
  137.     for (i = nrwin ; --i >= 0 ; ) {
  138.         wclose (winlist[i]) ;
  139.     }
  140.  
  141.     delmenubar () ;
  142.  
  143.     v_clsvwk (vdi_handle) ;
  144.     appl_exit () ;
  145.  
  146. }
  147.  
  148. WINDOW
  149. *wopen (title, drawproc)
  150.     char    *title ;
  151.     void    (*drawproc)() ;
  152. {
  153.     WINDOW    *new ;
  154.     Rect    maxsize ;
  155.     int    left ;
  156.     int    top ;
  157.     int    right ;
  158.     int    bottom ;
  159.     int    handle ;
  160.     int    dummy = 0 ;
  161.  
  162.     wind_update (BEG_UPDATE) ;
  163.  
  164.     rmcaret () ;
  165.  
  166.     maxsize.x = 0 ;
  167.     maxsize.y = BAR_HEIGHT ;
  168.     maxsize.w = max_width + lborder + rborder ;
  169.     maxsize.h = max_height + tborder + bborder ;
  170.     
  171. /*    handle = wind_create (DEF_OPT, maxsize) ; */
  172.     handle = wind_create (DEF_OPT, maxsize.x, maxsize.y,
  173.                   maxsize.w, maxsize.h) ;
  174.     if (handle < 0) {
  175.         wdebug ("wind_create failed") ;
  176.         return (NULL) ;
  177.     }
  178.  
  179.     new = ALLOC (WINDOW) ;
  180.     if (new == (WINDOW *) NULL) {
  181.         wdebug ("ALLOC (WINDOW) failed") ;
  182.         wind_delete (handle) ;
  183.         return (NULL) ;
  184.     }
  185.  
  186.     new->tag    = 0 ;
  187.     new->handle    = handle ;
  188.     new->title    = strdup (title) ;
  189.     new->drawproc    = drawproc ;
  190.     new->h         = def.x + lborder ;
  191.     new->v        = def.y + tborder ;
  192.     new->width    = def.w - lborder - rborder ;
  193.     new->height    = def.h - tborder - bborder ;
  194.     new->careton    = FALSE ;
  195.     new->caret_h = new->caret_v = -1 ;
  196.  
  197.     initmbar (new) ;
  198.     wgettextattr (&new->attr) ;
  199.     wsetorigin (new, 0, 0) ;
  200.     wsetdocsize (new, 0, 0) ;
  201.     wsettimer (new, 0) ;
  202.  
  203.     if (!wind_set (handle, WF_NAME, new->title, dummy, dummy, dummy)) {
  204.         wdebug ("wind_set title") ;
  205.     }
  206.  
  207. /*    if (!wind_open (handle, def)) { */
  208.     if (!wind_open (handle, def.x,def.y,def.w,def.h)) {
  209.         wdebug ("wind_open") ;
  210.         wclose (new) ;
  211.         return (NULL) ;
  212.     }
  213.  
  214.     setscrollbars (new) ;
  215.  
  216.     L_APPEND (nrwin, winlist, WINDOW *, new) ;
  217.  
  218.     left = new->orgh ;
  219.     top = new->orgv ;
  220.     right = left + new->width ;
  221.     bottom = top + new->height ;
  222.     wchange (new, left, top, right, bottom) ;
  223.  
  224.     active = new ;
  225.  
  226.     showcaret () ;
  227.  
  228.     wind_update (END_UPDATE) ;
  229.  
  230.     wupdate (new) ;
  231.  
  232.     def.x += 50 ;
  233.     if (def.x + def.w >= scr_width) {
  234.         def.x -= scr_width ;
  235.         if (def.x < 0)
  236.             def.x = 0 ;
  237.     }
  238.  
  239.     def.y += 30 ;
  240.     if (def.y + def.h >= scr_height) {
  241.         def.y -= scr_width ;
  242.         if (def.y < BAR_HEIGHT)
  243.             def.y = BAR_HEIGHT ;
  244.     }
  245.  
  246.     return (new) ;
  247. }
  248.  
  249. WINDOW    *getwin () ;
  250.  
  251. void
  252. wclose (win)
  253.     WINDOW    *win ;
  254. {
  255.     int    i ;
  256.     int    j ;
  257.     int    newtop ;
  258.     int    dummy = 0 ;
  259.  
  260.     if (active == win)
  261.         active = NULL ;
  262.  
  263.     for (i = 0; i < nrwin; i++) {
  264.         if (winlist[i] == win) {
  265.             wind_close (winlist[i]->handle) ;
  266.             wind_delete (winlist[i]->handle) ;
  267.             for (j = win->mbar.nrmenus ; --j >= 0 ;)
  268.                 wmenudetach (win, win->mbar.menulist[j]) ;
  269.             L_REMOVE (nrwin, winlist, WINDOW *, i) ;
  270.             break ;
  271.         }
  272.     }
  273.  
  274.     if (nrwin > 0) {
  275.         wind_get (dummy, WF_TOP, &newtop, &dummy, &dummy, &dummy) ;
  276.         active = getwin (newtop) ;
  277.         if (active == NULL)
  278.             wdebug ("wclose: cannot find new active window") ;
  279.         showcaret () ;
  280.     }
  281. }
  282.  
  283. WINDOW
  284. *getwin (handle)
  285.     int    handle ;
  286. {
  287.     int    i ;
  288.  
  289.     for (i = 0; i < nrwin; i++) {
  290.         if (winlist[i]->handle == handle)
  291.             return (winlist[i]) ;
  292.     }
  293.  
  294.     return (NULL) ;
  295. }
  296.  
  297. void
  298. wsetactive (win)
  299.     WINDOW    *win ;
  300. {
  301.     int    dummy = 0 ;
  302.  
  303.     if (win == NULL || win == active)
  304.         return ;
  305.  
  306.     rmcaret () ;
  307.  
  308.     wind_set (win->handle, WF_TOP, win->handle, dummy, dummy, dummy) ;
  309.  
  310.     active = win ;
  311.  
  312.     showcaret () ;
  313. }
  314.  
  315. WINDOW
  316. *wgetactive ()
  317. {
  318.     return (active) ;
  319. }
  320.  
  321. void
  322. wgetscrsize (pwidth, pheight)
  323.     int    *pwidth ;
  324.     int    *pheight ;
  325. {
  326.     *pwidth = scr_width ;
  327.     *pheight = scr_height ;
  328. }
  329.  
  330. void
  331. wgetscrmm (pmmhor, pmmvert)
  332.     int    *pmmhor ;
  333.     int    *pmmvert ;
  334. {
  335.     *pmmhor = (long)pix_width * scr_width / 1000L ;
  336.     *pmmvert = (long)pix_height * scr_height / 1000L ;
  337. }
  338.  
  339. void
  340. wsettitle (win, title)
  341.     WINDOW    *win ;
  342.     char    *title ;
  343. {
  344.     int    dummy = 0 ;
  345.  
  346.     FREE (win->title) ;
  347.     win->title = strdup (title) ;
  348.     wind_set (win->handle, WF_NAME, win->title, dummy, dummy, dummy) ;
  349. }
  350.  
  351. void
  352. wsetdefwinsize (width, height)
  353.     int    width ;
  354.     int    height ;
  355. {
  356.     if (width < MIN_WIDTH)
  357.         width = MIN_WIDTH ; 
  358.     if (width > scr_width - lborder - rborder)
  359.         width = scr_width - lborder - rborder ;
  360.  
  361.     if (height < MIN_HEIGHT)
  362.         height = MIN_HEIGHT ;
  363.     if (height > scr_height - (BAR_HEIGHT + tborder + bborder))
  364.         height = scr_height - (BAR_HEIGHT + tborder + bborder) ;
  365.  
  366.     def.w = width + lborder + rborder ;
  367.     def.h = height + tborder + bborder ;
  368.  
  369.     if (max_width < def.w - lborder - rborder)
  370.         max_width = def.w - lborder - rborder ;
  371.     if (max_height < def.h - tborder - bborder)
  372.         max_height = def.h - tborder - bborder ;
  373. }
  374.  
  375. void
  376. wsetmaxwinsize (width, height)
  377.     int    width ;
  378.     int    height ;
  379. {
  380.     if (width < MIN_WIDTH)
  381.         width = MIN_WIDTH ;
  382.     if (width > scr_width - lborder - rborder)
  383.         width = scr_width - lborder - rborder ;
  384.  
  385.     if (height < MIN_HEIGHT)
  386.         height = MIN_HEIGHT ;
  387.     if (height > scr_height - BAR_HEIGHT - tborder - bborder)
  388.         height = scr_height - BAR_HEIGHT - tborder - bborder ;
  389.  
  390.     max_width = width ;
  391.     max_height = height ;
  392.  
  393.     if (def.w > max_width + lborder + rborder)
  394.         def.w = max_width + lborder + rborder ;
  395.     if (def.h > max_height + tborder + bborder)
  396.         def.h = max_height + tborder + bborder ;
  397. }
  398.  
  399. void
  400. wsetdefwinpos (h, v)
  401.     int    h;
  402.     int    v;
  403. {
  404.     if (h < 0)
  405.         h = 0 ;
  406.     if (h > scr_width - 1)
  407.         h = scr_width - 1 ;
  408.  
  409.     if (v < BAR_HEIGHT)
  410.         v = BAR_HEIGHT ;
  411.     if (v > scr_height - 1)
  412.         v = scr_height - 1 ;
  413.  
  414.     def.x = h ;
  415.     def.y = v ;
  416. }
  417.  
  418. void
  419. wgetwinsize (win, pwidth, pheight)
  420.     WINDOW    *win ;
  421.     int    *pwidth ;
  422.     int    *pheight ;
  423. {
  424.     if (win == NULL) {
  425.         wdebug ("wgetwinsize: illegal window pointer") ;
  426.         return ;
  427.     }
  428.  
  429.     *pwidth = win->width ;
  430.     *pheight = win->height ;
  431. }
  432.  
  433. void
  434. _setsize (win, h, v, width, height)
  435.     WINDOW    *win ;
  436.     int    h ;
  437.     int    v ;
  438.     int    width ;
  439.     int    height ;
  440. {
  441.     win->h = h + lborder ;
  442.     win->v = v + tborder ;
  443.     win->width = width - lborder - rborder ;
  444.     win->height = height - tborder - bborder ;
  445. }
  446.  
  447. void
  448. wsettimer (win, dtime)
  449.     WINDOW    *win ;
  450.     int    dtime ;
  451. {
  452.     if (win == NULL) {
  453.         wdebug ("wsettimer: illegal window pointer") ;
  454.         return ;
  455.     }
  456.  
  457.     if (dtime > 0) {
  458.         msec    currtime ;
  459.  
  460.         currtime = MSTIME () ;
  461.         win->alarm = currtime + (dtime * 100L) ;
  462.     }
  463.     else {
  464.         if (dtime != 0)
  465.             wdebug ("timer negative") ;
  466.         win->alarm = 0L ;
  467.     }
  468. }
  469.  
  470. void
  471. wfleep ()
  472. {
  473.     write (1, "\07\07", 2) ;
  474. }
  475.  
  476. void
  477. wdebug (fmt, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
  478.     char    *fmt ;
  479.      int a0, a1, a2, a3, a4, a5, a6, a7, a8, a9;
  480. {
  481.     char    buf[256] ;
  482.  
  483.     strcpy (buf, "[3][__") ;
  484.     sprintf (buf+4, fmt, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) ;
  485.     strcat (buf, "__][abort|cont]") ;
  486.  
  487.     if (form_alert (2, buf) == 1) {
  488.         wdone () ;
  489.         exit (1) ;
  490.     }
  491. }
  492.  
  493. int
  494. wlineheight ()
  495. {
  496.     return (chheight) ;
  497. }
  498.  
  499. int
  500. wtextwidth (str, len)
  501.     char    *str ;
  502.     int    len ;
  503. {
  504.     int    dummy ;
  505.     int    sizes[5] ;
  506.     int    adjust[3] ;
  507.     int    str_len = len == -1 ? (int)strlen (str) : len ;
  508.  
  509.     /*
  510.     ** The width of one character on the ATARI with a monochrome display
  511.     ** and default attributes is "chwidth" pixels. The only thing we have to 
  512.     ** calculate is the adjustment for the 'special effects'.
  513.     */
  514.  
  515.     vqt_font_info (vdi_handle, &dummy, &dummy, sizes, &dummy, adjust) ;
  516.  
  517.     return (str_len * (chwidth + adjust[0])) ;
  518. }
  519.     
  520. int
  521. wcharwidth (c)
  522.     int    c ;
  523. {
  524.     char    buf[2] ;
  525.  
  526.     buf[0] = c ;
  527.     buf[1] = 0 ;
  528.  
  529.     return (wtextwidth (buf, 1)) ;
  530. }
  531.