home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / glview.sit / glview.src / xlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-31  |  11.4 KB  |  572 lines

  1. /*
  2.   xlib.c
  3.     mac hack at x window lib
  4.     oct91
  5. */
  6.  
  7. #pragma segment XLib
  8.  
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. #include <string.h>
  12.  
  13. #include "xlib.h"
  14.  
  15. #include <Quickdraw.h>
  16. #include <Memory.h>
  17. #include <OSUtils.h>
  18. #include <ToolUtils.h>
  19. #include <Windows.h>
  20. #include <Palettes.h>
  21. #include    <dialogs.h>
  22. #include    <StandardFile.h>
  23. #include    <Events.h>
  24. //#include <Menus.h>
  25. //#include <Fonts.h>
  26.  
  27. #define    True 1
  28. #define False 0
  29.  
  30. typedef unsigned short u_short;
  31. typedef unsigned long  u_long;
  32.  
  33.  
  34. XImage *XCreateImage(
  35.     Display *dsp, 
  36.     Visual *vis, 
  37.     unsigned int depth, 
  38.     int format, 
  39.     int offset, 
  40.     char *data, 
  41.     unsigned int width, 
  42.     unsigned int height, 
  43.     int bitmap_pad, 
  44.     int bytes_per_line)
  45. {
  46.     XImage             *xim;
  47.     int              rowbytes;
  48.     unsigned int     hi1 = height;
  49.     
  50.     width  -= 1;
  51.     height -= 1; // ??? fixes extra junk 
  52.     
  53.     // -- this is how we made data ptr
  54.     //datasize = bpsl * im->h;
  55.     //data = (u_char *) malloc(datasize);
  56.     //bytes_per_line == bpsl
  57.     //heigth == im->h
  58.     
  59.     // fix for Mac bitmap -- bytes_per_line must be word-aligned (2-byte)
  60.     rowbytes = bytes_per_line; 
  61.     if (bytes_per_line & 1) {
  62.         char     *tbuf;
  63.         unsigned int i;
  64.         
  65.         rowbytes= bytes_per_line+1;
  66.         tbuf = (char*) NewPtrClear( rowbytes*hi1 + 4);
  67.         if ((u_long)tbuf & 3 !=0) 
  68.           (u_long)tbuf = (((u_long)tbuf & 0xfffffffc) + 4); // make it long-aligned ?!
  69.         for (i=0; i<hi1; i++)  
  70.           memcpy( tbuf +(i*rowbytes), data +(i*bytes_per_line), bytes_per_line);
  71.         free(data);
  72.         data = tbuf;
  73.         }
  74.  
  75.     xim = (XImage *) malloc(sizeof(XImage));
  76.     //use vis to set rgb, ZPixmap stuff    
  77.     xim->width = width;
  78.     xim->height = height;
  79.     xim->format = format;
  80.     xim->data = data;
  81.     xim->depth = depth;
  82.     xim->xoffset = offset;
  83.     xim->bitmap_pad = bitmap_pad;
  84.     xim->bytes_per_line = rowbytes; //was bytes_per_line; << fix for mac
  85.     return xim;
  86. }
  87.  
  88. void XPutImage(
  89.     Display *dsp, 
  90.     Drawable win, 
  91.     GC gc, 
  92.     XImage *image, 
  93.     int src_x, 
  94.     int src_y,
  95.     int dest_x, 
  96.     int dest_y, 
  97.     unsigned int width, 
  98.     unsigned int height)
  99. {
  100. // display image
  101.  
  102. // !! this is BAD, win is not &variable, value is not returned !!!
  103.     win = image->data;
  104. }
  105.  
  106.  
  107.  
  108. #define UseColorWin     1
  109.  
  110. static PixMapHandle     srcpixmap, winpixmap;
  111. static CTabHandle        theColorTab;
  112.  
  113. void XCopyArea(
  114.     Display *dsp, 
  115.     Drawable src, 
  116.     Drawable dest, 
  117.     GC gc, 
  118.     int src_x, 
  119.     int src_y,
  120.     unsigned int width, 
  121.     unsigned int height,
  122.     int dest_x, 
  123.     int dest_y) 
  124. {
  125.    Rect          sr, dr;
  126.    int            i,dx,dy;
  127. #define cw        ((WindowPtr)dsp)
  128. #define xim        ((XImage *)src)
  129.  
  130.     if (src==NULL) return;
  131.     dx = (cw->portRect).left + dest_x;
  132.     dy = (cw->portRect).top + dest_y;    
  133.     SetRect( &dr, dx, dy, dx+width, dy+height);
  134.     SetRect( &sr, src_x, src_y, src_x+width, src_y+height);
  135.        
  136. #ifdef UseColorWin
  137. #define db    ((BitMap*) (*winpixmap))
  138. #define sp  ((PixMapPtr) (*srcpixmap))
  139. #define sb    ((BitMap*) (*srcpixmap))
  140. #define pixmapFlag    0x8000
  141.  
  142.     sp->pixelSize = xim->depth;
  143.     sp->cmpSize = sp->pixelSize;
  144.     sp->pmTable = theColorTab;
  145.     sp->baseAddr = (Ptr) xim->data;
  146.     sp->rowBytes = pixmapFlag | xim->bytes_per_line;  
  147.     SetRect( &sp->bounds, 0, 0, xim->width, xim->height);
  148.     
  149.     CopyBits( sb, db, &sr, &dr, srcCopy, NULL);
  150.     
  151. #else
  152. #define db    (&(cw->portBits))
  153.     BitMap* sb = (BitMap*) NewPtr(sizeof(BitMap));
  154.     
  155.     sb->baseAddr = (Ptr) xim->data;
  156.     sb->rowBytes = xim->bytes_per_line; 
  157.     SetRect( &sb->bounds, 0, 0, xim->width, xim->height);
  158.     CopyBits( sb, db, &sr, &dr, srcCopy, NULL);
  159.     DisposPtr((Ptr) sb);
  160. #endif
  161.  
  162. #undef cw
  163. #undef xim
  164. #undef db
  165. #undef sb
  166. #undef sp
  167. }
  168.  
  169.  
  170. Pixmap XCreatePixmap(
  171.     Display *dsp,
  172.     Drawable win,
  173.     unsigned int width,
  174.     unsigned int height,
  175.     unsigned int depth)
  176. {
  177.     //Pixmap *pix = NewPtr(sizeof(Pixmap));
  178.     //return *pix;
  179.     return NULL;
  180. }
  181.  
  182. void  XFreePixmap(
  183.     Display *dsp,
  184.     Pixmap pix)
  185. {
  186.     // DisposPtr((ptr)pix);
  187. }
  188.  
  189.  
  190.  
  191. Display *XOpenDisplay( char* name)
  192. {
  193.    WindowPtr  cw;    
  194.    Rect  bounds;
  195.    const Bool  Visible = True;
  196.    
  197.    SetRect(&bounds, 2, 40, 322, 240);
  198. #ifdef UseColorWin 
  199.     cw= newcwindow( NULL, &bounds, name, Visible, 0, NULL, False, 0);
  200.     srcpixmap = NewPixMap();
  201.     winpixmap = ((CWindowPtr) cw)->portPixMap;
  202.     CopyPixMap( winpixmap, srcpixmap); 
  203.     theColorTab = (**winpixmap).pmTable; // set default
  204. #else
  205.     cw= newwindow( NULL, &bounds, name, Visible, 0, NULL, False, 0);
  206. #endif
  207.  
  208.    SelectWindow( cw);
  209.    SetPort(cw);   
  210.    MoveTo( 9, 40); drawstring("Working...");
  211.    MoveTo( 9, 90); drawstring("(command-period to halt)");
  212.    return (Display*) cw;
  213. }
  214.  
  215.  
  216. XResizeWindow( dsp, win,  width, height)
  217.     Display *dsp; 
  218.     Window win; 
  219.     unsigned int width, height;
  220. {
  221.    SizeWindow( (WindowPtr) dsp, width, height, True); 
  222. }
  223.  
  224.  
  225.  
  226. #define MAXPALCOLS     256
  227.  
  228. Colormap XCreateColormap(     
  229.     Display *dsp,
  230.     Window win,
  231.     Visual *vis, 
  232.     int allocflag)
  233. {
  234.    CTabHandle    cmap;     // CSpecArray == ColorSpec[]
  235. #define  ctab    (**cmap).ctTable
  236.    int            i;
  237.    int            acolor;
  238.    const int    black = 0;
  239.    const int    white = 0xffff;
  240.    
  241.    cmap = (CTabHandle) NewHandle(sizeof(ColorTable) + MAXPALCOLS*sizeof(ColorSpec));
  242.    (**cmap).ctSeed = GetCTSeed();
  243.    (**cmap).ctFlags = 0x0000;
  244.    (**cmap).ctSize = MAXPALCOLS-1;
  245.    acolor = white;
  246.    for (i=0;i<MAXPALCOLS; i++) {
  247.      ctab[i].value = i;
  248.      ctab[i].rgb.red = ctab[i].rgb.green = ctab[i].rgb.blue = acolor;
  249.      acolor = black;
  250.      }
  251.    return (Colormap) cmap;
  252. #undef  ctab 
  253. }
  254.  
  255. void XStoreColors(  
  256.     Display *dsp,
  257.     Colormap  cmap,
  258.     XColor color[],
  259.     int  ncolors)
  260. {
  261. #define ctab    (**(CTabHandle)cmap).ctTable
  262.   int        i;
  263.   Bool        ciused[MAXPALCOLS];
  264.   Bool        done;
  265.   
  266.   for (i=0; i<MAXPALCOLS; i++) ciused[i]=False;
  267.   (**(CTabHandle)cmap).ctSeed = GetCTSeed(); // notice that we have new colors??? 
  268.   if (ncolors>MAXPALCOLS) ncolors=MAXPALCOLS;
  269.   for (i=0; i<ncolors; i++) {
  270.     ctab[i].rgb.red     =  color[i].red;
  271.     ctab[i].rgb.green     =  color[i].green;
  272.     ctab[i].rgb.blue     =  color[i].blue;
  273.     }
  274.      
  275. /*******
  276.     // need to set indices to Device/Std CLUT so we aren't totally mangling 
  277.     // out-of-window colors on SetColormap...
  278.     // Can we assume this is called when Std CLUT is in place ?? -- probably not
  279.     if (RealColor(&ctab[i].rgb)) {
  280.       short cind = (short) Color2Index(&ctab[i].rgb);
  281.       ctab[i].value = cind;
  282.       if (cind < MAXPALCOLS) ciused[cind] = True;
  283.       //color[i].pixel = cind;
  284.       }
  285.     else {
  286.       ctab[i].value = MAXPALCOLS; // ?? key we need to force clut ?
  287.       }
  288.  *******/  
  289.  /*********
  290.   for (i=0; i<ncolors; i++) {
  291.     if (ctab[i].value == MAXPALCOLS) {
  292.         int        j;
  293.         Bool    done;
  294.         for (j=0, done=False; (!done) && (j<MAXPALCOLS); j++) {
  295.             ctab[i].value = i;
  296.             ciused[j] = True;
  297.             done=True;
  298.             }
  299.         if (!done) 
  300.             ctab[i].value = (short) Color2Index(&ctab[i].rgb);
  301.         }
  302.     }
  303.  ************/
  304.  
  305. #undef  ctab 
  306. }
  307.  
  308.  
  309.  
  310. void XSetWindowColormap(
  311.     Display *dsp,
  312.     Drawable win,
  313.     Colormap cmap)
  314. {
  315. #ifdef UseColorWin 
  316.  
  317.     CTabHandle    devCmap;
  318.     int            i;
  319.     Bool          ciused[MAXPALCOLS];
  320.     Bool          cineed[MAXPALCOLS];
  321. #define ctab    (**(CTabHandle)cmap).ctTable
  322. #define dtab    (**(CTabHandle)devCmap).ctTable
  323.     
  324.     theColorTab = (CTabHandle)cmap;
  325.     (**winpixmap).pmTable = theColorTab;
  326.  
  327. // !need this when ColorTab includes colors not in standard Color Lookup Table
  328.     HLock((Handle)theColorTab);
  329.     SetEntries( 0, (**theColorTab).ctSize, (**theColorTab).ctTable); 
  330.     HUnlock((Handle)theColorTab);
  331.  
  332. // use by-index installation (-1) in Device CLUT, so we preserve out-of-window colors
  333. // as much as possible ?????
  334. //     SetEntries( -1, (**theColorTab).ctSize, (**theColorTab).ctTable); 
  335.     
  336. /***********
  337.     // identify colors in current CLUT (ciused) & those not in (cineed)
  338.     for (i=0; i<MAXPALCOLS; i++) ciused[i]= False;
  339.     for (i=0; i<<=(**theColorTab).ctSize; i++) cineed[i]= False;
  340.     for (i=0; i<=(**theColorTab).ctSize; i++) 
  341.         if (RealColor(&ctab[i].rgb)) {
  342.           short cind = (short) Color2Index(&ctab[i].rgb);
  343.           ciused[cind]= True;
  344.           }
  345.         else {
  346.           cineed[i]= True;
  347.           }
  348.             
  349.     // copy current CLUT
  350.     devCmap = (CTabHandle) NewHandle(sizeof(ColorTable) + MAXPALCOLS*sizeof(ColorSpec));
  351.     (**devCmap).ctSize = MAXPALCOLS-1;
  352.     (**devCmap).ctSeed = GetCTSeed();
  353.     (**devCmap).ctFlags = 0x0000;
  354.     // ?? use this --
  355.     for (i=0; i<MAXPALCOLS; i++) { Index2Color( i, &dtab[i].rgb); dtab[i].value=i; }
  356.     
  357.     // replace entries in current CLUT w/ new colors that are needed
  358.     for (i=0; i<=(**theColorTab).ctSize; i++) 
  359.         if (cineed[i]) {
  360.           int    j;
  361.           Bool    done;
  362.           for (j=0, done=False; (!done) && (j<MAXPALCOLS); j++)
  363.             if (!ciused[j]) {
  364.               dtab[j].rgb = ctab[i].rgb;
  365.               done= True;
  366.               }
  367.           }
  368.     
  369.     // store revised CLUT
  370.     HLock((Handle)devCmap);
  371.     SetEntries( 0, (**devCmap).ctSize, (**devCmap).ctTable); 
  372.     HUnlock((Handle)devCmap);
  373.     DisposHandle((Handle)devCmap);
  374. *********/
  375.  
  376. #undef ctab
  377. #undef dtab
  378.  
  379. #endif
  380. }
  381.  
  382. int
  383. XAllocColorCells( dsp, cmap, contig, plane_masks_return,
  384.           nplanes, pixels_return, npixels) 
  385.     Display *dsp;
  386.     Colormap  cmap;
  387.     Bool  contig;
  388.     unsigned long plane_masks_return[];
  389.     unsigned int nplanes;
  390.     unsigned long pixels_return[];
  391.     unsigned int npixels;
  392. {
  393.   return 0;
  394. }
  395.  
  396.  
  397.  
  398.  
  399.  
  400. void XSetForeground(  
  401.     Display *dsp,
  402.     GC gc,
  403.     unsigned long foreground)
  404. {
  405.    PmForeColor( foreground);   
  406. }
  407.  
  408. void XSetBackground( 
  409.     Display *dsp,
  410.     GC gc,
  411.     unsigned long background)
  412. {
  413.    PmBackColor( background);
  414. }
  415.  
  416.  
  417. void XFillRectangle( 
  418.     Display *dsp,
  419.     Drawable win,
  420.     GC gc,
  421.     int x,
  422.     int y,
  423.     int width,
  424.     int height) 
  425. {
  426.     WindowPtr    aPort;
  427.     Rect        aRec;
  428.     
  429.     GetPort( &aPort);
  430.     SetPort((WindowPtr)dsp);
  431.     SetRect( &aRec, x,y,x+width,y+height);
  432.     PaintRect(&aRec);
  433.     SetPort(aPort);
  434. }
  435.  
  436.  
  437. void    XFillRectangles(
  438.     Display *dsp,
  439.     Drawable win,
  440.     GC gc,
  441.     XRectangle *rects,
  442.     int nrects)
  443. {
  444. #define setxrects(ar, r) \
  445.     SetRect( &ar, (r).x, (r).y, (r).x+(r).width, (r).y+(r).height) 
  446.  
  447.     WindowPtr    aPort;
  448.     Rect        aRec;
  449.     int            i;
  450.     
  451.     GetPort( &aPort);
  452.     SetPort((WindowPtr)dsp);
  453.     for (i=0; i<nrects; i++) {
  454.       setxrects(aRec, rects[i]);
  455.       PaintRect(&aRec);
  456.       }
  457.     SetPort(aPort);
  458. }
  459.  
  460.  
  461.  
  462.  
  463. Bool StopKey()
  464. {    
  465. /******
  466.      KeyMap    kmap;
  467.     
  468.     GetKeys(&kmap);   
  469.     return BitTst(kmap, sizeof(KeyMap)-55) && BitTst(kmap,sizeof(KeyMap)-47);
  470. ********/
  471.     EventRecord    ev;
  472.  
  473.     if (EventAvail( keyDownMask+autoKeyMask, &ev)) {
  474.       if (  (ev.modifiers & cmdKey)  
  475.        && ((char)(ev.message & charCodeMask) == '.') ) {
  476.             SysBeep(1);
  477.           (void) GetNextEvent( keyDownMask+autoKeyMask, &ev);
  478.           return True;
  479.           }
  480.       }
  481.     return False;
  482. }
  483.  
  484. Bool cmdKeyIsDown()
  485. {    KeyMap    kmap;
  486.     GetKeys(&kmap);   
  487.     return BitTst(kmap, 55);
  488. }
  489.  
  490. Bool shiftKeyIsDown()
  491. {   KeyMap    kmap;
  492.     GetKeys(&kmap);   
  493.     return BitTst(kmap, 56);
  494. }
  495.  
  496. Bool capsLockIsDown()
  497. {    KeyMap    kmap;
  498.     GetKeys(&kmap);  
  499.     return BitTst(kmap, 57);
  500. }
  501.  
  502. Bool optionKeyIsDown()
  503. {    KeyMap    kmap;
  504.     GetKeys(&kmap);  
  505.     return BitTst(kmap, 58);
  506. }
  507.  
  508. Bool MouseButton()
  509. {
  510.     return Button();
  511. }
  512.  
  513. Bool Keypress()
  514. {    EventRecord    ev;
  515.     return EventAvail( keyDownMask+keyUpMask+autoKeyMask, &ev);
  516. }
  517.  
  518.  
  519.  
  520. char *StdGetFile(
  521.     char*  prompt, 
  522.     OSType fileTypes[],
  523.     int       nFileTypes) 
  524. {
  525.     Point            wher;             /*where to display dialog*/
  526.     SFReply               reply;           /*reply record*/
  527.     short              len;
  528.     static char        filename[80] = "\0";
  529.     
  530.     wher.h = 80;
  531.     wher.v = 90;
  532.     if (optionKeyIsDown()) nFileTypes=0;
  533.  
  534.     SFGetFile(wher, prompt, nil, nFileTypes, fileTypes, nil, &reply);
  535.  
  536.     if (reply.good) {
  537.           len = SetVol(nil, reply.vRefNum);
  538.         len = reply.fName[0];
  539.         strncpy(filename, (char *)(&reply.fName[1]), len); 
  540.         filename[len]= '\0';
  541.         return filename;
  542.         }
  543.     else
  544.         return NULL;
  545. }
  546.  
  547.  
  548. //pascal void Delay(long numTicks,long *finalTicks) = {0xA03B,0x2280}; 
  549. //pascal unsigned long TickCount(void) = 0xA975; 
  550.  
  551. int usleep( unsigned long usec)
  552. {
  553. // usleep(10000);    == sleep for 1/100th of a second  
  554.     long finalTicks = 0;
  555.  
  556. /*******
  557.     return 0;
  558. *****/
  559.     // usec /= 16667;
  560.     usec >>= 14;
  561.     if (usec < 1) Delay( 1, &finalTicks);
  562.     else Delay( usec, &finalTicks);
  563.     return 0;
  564. }
  565.  
  566. long hundredthsofseconds()
  567. {
  568.   //return  (long) (100 * TickCount() / 60);  /* == 100ths of seconds since startup */
  569.   return  TickCount() << 1;  /* == 60ths of seconds since startup */
  570. }
  571.  
  572.