home *** CD-ROM | disk | FTP | other *** search
- /*
- xlib.c
- mac hack at x window lib
- oct91
- */
-
- #pragma segment XLib
-
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
-
- #include "xlib.h"
-
- #include <Quickdraw.h>
- #include <Memory.h>
- #include <OSUtils.h>
- #include <ToolUtils.h>
- #include <Windows.h>
- #include <Palettes.h>
- #include <dialogs.h>
- #include <StandardFile.h>
- #include <Events.h>
- //#include <Menus.h>
- //#include <Fonts.h>
-
- #define True 1
- #define False 0
-
- typedef unsigned short u_short;
- typedef unsigned long u_long;
-
-
- XImage *XCreateImage(
- Display *dsp,
- Visual *vis,
- unsigned int depth,
- int format,
- int offset,
- char *data,
- unsigned int width,
- unsigned int height,
- int bitmap_pad,
- int bytes_per_line)
- {
- XImage *xim;
- int rowbytes;
- unsigned int hi1 = height;
-
- width -= 1;
- height -= 1; // ??? fixes extra junk
-
- // -- this is how we made data ptr
- //datasize = bpsl * im->h;
- //data = (u_char *) malloc(datasize);
- //bytes_per_line == bpsl
- //heigth == im->h
-
- // fix for Mac bitmap -- bytes_per_line must be word-aligned (2-byte)
- rowbytes = bytes_per_line;
- if (bytes_per_line & 1) {
- char *tbuf;
- unsigned int i;
-
- rowbytes= bytes_per_line+1;
- tbuf = (char*) NewPtrClear( rowbytes*hi1 + 4);
- if ((u_long)tbuf & 3 !=0)
- (u_long)tbuf = (((u_long)tbuf & 0xfffffffc) + 4); // make it long-aligned ?!
- for (i=0; i<hi1; i++)
- memcpy( tbuf +(i*rowbytes), data +(i*bytes_per_line), bytes_per_line);
- free(data);
- data = tbuf;
- }
-
- xim = (XImage *) malloc(sizeof(XImage));
- //use vis to set rgb, ZPixmap stuff
- xim->width = width;
- xim->height = height;
- xim->format = format;
- xim->data = data;
- xim->depth = depth;
- xim->xoffset = offset;
- xim->bitmap_pad = bitmap_pad;
- xim->bytes_per_line = rowbytes; //was bytes_per_line; << fix for mac
- return xim;
- }
-
- void XPutImage(
- Display *dsp,
- Drawable win,
- GC gc,
- XImage *image,
- int src_x,
- int src_y,
- int dest_x,
- int dest_y,
- unsigned int width,
- unsigned int height)
- {
- // display image
-
- // !! this is BAD, win is not &variable, value is not returned !!!
- win = image->data;
- }
-
-
-
- #define UseColorWin 1
-
- static PixMapHandle srcpixmap, winpixmap;
- static CTabHandle theColorTab;
-
- void XCopyArea(
- Display *dsp,
- Drawable src,
- Drawable dest,
- GC gc,
- int src_x,
- int src_y,
- unsigned int width,
- unsigned int height,
- int dest_x,
- int dest_y)
- {
- Rect sr, dr;
- int i,dx,dy;
- #define cw ((WindowPtr)dsp)
- #define xim ((XImage *)src)
-
- if (src==NULL) return;
- dx = (cw->portRect).left + dest_x;
- dy = (cw->portRect).top + dest_y;
- SetRect( &dr, dx, dy, dx+width, dy+height);
- SetRect( &sr, src_x, src_y, src_x+width, src_y+height);
-
- #ifdef UseColorWin
- #define db ((BitMap*) (*winpixmap))
- #define sp ((PixMapPtr) (*srcpixmap))
- #define sb ((BitMap*) (*srcpixmap))
- #define pixmapFlag 0x8000
-
- sp->pixelSize = xim->depth;
- sp->cmpSize = sp->pixelSize;
- sp->pmTable = theColorTab;
- sp->baseAddr = (Ptr) xim->data;
- sp->rowBytes = pixmapFlag | xim->bytes_per_line;
- SetRect( &sp->bounds, 0, 0, xim->width, xim->height);
-
- CopyBits( sb, db, &sr, &dr, srcCopy, NULL);
-
- #else
- #define db (&(cw->portBits))
- BitMap* sb = (BitMap*) NewPtr(sizeof(BitMap));
-
- sb->baseAddr = (Ptr) xim->data;
- sb->rowBytes = xim->bytes_per_line;
- SetRect( &sb->bounds, 0, 0, xim->width, xim->height);
- CopyBits( sb, db, &sr, &dr, srcCopy, NULL);
- DisposPtr((Ptr) sb);
- #endif
-
- #undef cw
- #undef xim
- #undef db
- #undef sb
- #undef sp
- }
-
-
- Pixmap XCreatePixmap(
- Display *dsp,
- Drawable win,
- unsigned int width,
- unsigned int height,
- unsigned int depth)
- {
- //Pixmap *pix = NewPtr(sizeof(Pixmap));
- //return *pix;
- return NULL;
- }
-
- void XFreePixmap(
- Display *dsp,
- Pixmap pix)
- {
- // DisposPtr((ptr)pix);
- }
-
-
-
- Display *XOpenDisplay( char* name)
- {
- WindowPtr cw;
- Rect bounds;
- const Bool Visible = True;
-
- SetRect(&bounds, 2, 40, 322, 240);
- #ifdef UseColorWin
- cw= newcwindow( NULL, &bounds, name, Visible, 0, NULL, False, 0);
- srcpixmap = NewPixMap();
- winpixmap = ((CWindowPtr) cw)->portPixMap;
- CopyPixMap( winpixmap, srcpixmap);
- theColorTab = (**winpixmap).pmTable; // set default
- #else
- cw= newwindow( NULL, &bounds, name, Visible, 0, NULL, False, 0);
- #endif
-
- SelectWindow( cw);
- SetPort(cw);
- MoveTo( 9, 40); drawstring("Working...");
- MoveTo( 9, 90); drawstring("(command-period to halt)");
- return (Display*) cw;
- }
-
-
- XResizeWindow( dsp, win, width, height)
- Display *dsp;
- Window win;
- unsigned int width, height;
- {
- SizeWindow( (WindowPtr) dsp, width, height, True);
- }
-
-
-
- #define MAXPALCOLS 256
-
- Colormap XCreateColormap(
- Display *dsp,
- Window win,
- Visual *vis,
- int allocflag)
- {
- CTabHandle cmap; // CSpecArray == ColorSpec[]
- #define ctab (**cmap).ctTable
- int i;
- int acolor;
- const int black = 0;
- const int white = 0xffff;
-
- cmap = (CTabHandle) NewHandle(sizeof(ColorTable) + MAXPALCOLS*sizeof(ColorSpec));
- (**cmap).ctSeed = GetCTSeed();
- (**cmap).ctFlags = 0x0000;
- (**cmap).ctSize = MAXPALCOLS-1;
- acolor = white;
- for (i=0;i<MAXPALCOLS; i++) {
- ctab[i].value = i;
- ctab[i].rgb.red = ctab[i].rgb.green = ctab[i].rgb.blue = acolor;
- acolor = black;
- }
- return (Colormap) cmap;
- #undef ctab
- }
-
- void XStoreColors(
- Display *dsp,
- Colormap cmap,
- XColor color[],
- int ncolors)
- {
- #define ctab (**(CTabHandle)cmap).ctTable
- int i;
- Bool ciused[MAXPALCOLS];
- Bool done;
-
- for (i=0; i<MAXPALCOLS; i++) ciused[i]=False;
- (**(CTabHandle)cmap).ctSeed = GetCTSeed(); // notice that we have new colors???
- if (ncolors>MAXPALCOLS) ncolors=MAXPALCOLS;
- for (i=0; i<ncolors; i++) {
- ctab[i].rgb.red = color[i].red;
- ctab[i].rgb.green = color[i].green;
- ctab[i].rgb.blue = color[i].blue;
- }
-
- /*******
- // need to set indices to Device/Std CLUT so we aren't totally mangling
- // out-of-window colors on SetColormap...
- // Can we assume this is called when Std CLUT is in place ?? -- probably not
- if (RealColor(&ctab[i].rgb)) {
- short cind = (short) Color2Index(&ctab[i].rgb);
- ctab[i].value = cind;
- if (cind < MAXPALCOLS) ciused[cind] = True;
- //color[i].pixel = cind;
- }
- else {
- ctab[i].value = MAXPALCOLS; // ?? key we need to force clut ?
- }
- *******/
- /*********
- for (i=0; i<ncolors; i++) {
- if (ctab[i].value == MAXPALCOLS) {
- int j;
- Bool done;
- for (j=0, done=False; (!done) && (j<MAXPALCOLS); j++) {
- ctab[i].value = i;
- ciused[j] = True;
- done=True;
- }
- if (!done)
- ctab[i].value = (short) Color2Index(&ctab[i].rgb);
- }
- }
- ************/
-
- #undef ctab
- }
-
-
-
- void XSetWindowColormap(
- Display *dsp,
- Drawable win,
- Colormap cmap)
- {
- #ifdef UseColorWin
-
- CTabHandle devCmap;
- int i;
- Bool ciused[MAXPALCOLS];
- Bool cineed[MAXPALCOLS];
- #define ctab (**(CTabHandle)cmap).ctTable
- #define dtab (**(CTabHandle)devCmap).ctTable
-
- theColorTab = (CTabHandle)cmap;
- (**winpixmap).pmTable = theColorTab;
-
- // !need this when ColorTab includes colors not in standard Color Lookup Table
- HLock((Handle)theColorTab);
- SetEntries( 0, (**theColorTab).ctSize, (**theColorTab).ctTable);
- HUnlock((Handle)theColorTab);
-
- // use by-index installation (-1) in Device CLUT, so we preserve out-of-window colors
- // as much as possible ?????
- // SetEntries( -1, (**theColorTab).ctSize, (**theColorTab).ctTable);
-
- /***********
- // identify colors in current CLUT (ciused) & those not in (cineed)
- for (i=0; i<MAXPALCOLS; i++) ciused[i]= False;
- for (i=0; i<<=(**theColorTab).ctSize; i++) cineed[i]= False;
- for (i=0; i<=(**theColorTab).ctSize; i++)
- if (RealColor(&ctab[i].rgb)) {
- short cind = (short) Color2Index(&ctab[i].rgb);
- ciused[cind]= True;
- }
- else {
- cineed[i]= True;
- }
-
- // copy current CLUT
- devCmap = (CTabHandle) NewHandle(sizeof(ColorTable) + MAXPALCOLS*sizeof(ColorSpec));
- (**devCmap).ctSize = MAXPALCOLS-1;
- (**devCmap).ctSeed = GetCTSeed();
- (**devCmap).ctFlags = 0x0000;
- // ?? use this --
- for (i=0; i<MAXPALCOLS; i++) { Index2Color( i, &dtab[i].rgb); dtab[i].value=i; }
-
- // replace entries in current CLUT w/ new colors that are needed
- for (i=0; i<=(**theColorTab).ctSize; i++)
- if (cineed[i]) {
- int j;
- Bool done;
- for (j=0, done=False; (!done) && (j<MAXPALCOLS); j++)
- if (!ciused[j]) {
- dtab[j].rgb = ctab[i].rgb;
- done= True;
- }
- }
-
- // store revised CLUT
- HLock((Handle)devCmap);
- SetEntries( 0, (**devCmap).ctSize, (**devCmap).ctTable);
- HUnlock((Handle)devCmap);
- DisposHandle((Handle)devCmap);
- *********/
-
- #undef ctab
- #undef dtab
-
- #endif
- }
-
- int
- XAllocColorCells( dsp, cmap, contig, plane_masks_return,
- nplanes, pixels_return, npixels)
- Display *dsp;
- Colormap cmap;
- Bool contig;
- unsigned long plane_masks_return[];
- unsigned int nplanes;
- unsigned long pixels_return[];
- unsigned int npixels;
- {
- return 0;
- }
-
-
-
-
-
- void XSetForeground(
- Display *dsp,
- GC gc,
- unsigned long foreground)
- {
- PmForeColor( foreground);
- }
-
- void XSetBackground(
- Display *dsp,
- GC gc,
- unsigned long background)
- {
- PmBackColor( background);
- }
-
-
- void XFillRectangle(
- Display *dsp,
- Drawable win,
- GC gc,
- int x,
- int y,
- int width,
- int height)
- {
- WindowPtr aPort;
- Rect aRec;
-
- GetPort( &aPort);
- SetPort((WindowPtr)dsp);
- SetRect( &aRec, x,y,x+width,y+height);
- PaintRect(&aRec);
- SetPort(aPort);
- }
-
-
- void XFillRectangles(
- Display *dsp,
- Drawable win,
- GC gc,
- XRectangle *rects,
- int nrects)
- {
- #define setxrects(ar, r) \
- SetRect( &ar, (r).x, (r).y, (r).x+(r).width, (r).y+(r).height)
-
- WindowPtr aPort;
- Rect aRec;
- int i;
-
- GetPort( &aPort);
- SetPort((WindowPtr)dsp);
- for (i=0; i<nrects; i++) {
- setxrects(aRec, rects[i]);
- PaintRect(&aRec);
- }
- SetPort(aPort);
- }
-
-
-
-
- Bool StopKey()
- {
- /******
- KeyMap kmap;
-
- GetKeys(&kmap);
- return BitTst(kmap, sizeof(KeyMap)-55) && BitTst(kmap,sizeof(KeyMap)-47);
- ********/
- EventRecord ev;
-
- if (EventAvail( keyDownMask+autoKeyMask, &ev)) {
- if ( (ev.modifiers & cmdKey)
- && ((char)(ev.message & charCodeMask) == '.') ) {
- SysBeep(1);
- (void) GetNextEvent( keyDownMask+autoKeyMask, &ev);
- return True;
- }
- }
- return False;
- }
-
- Bool cmdKeyIsDown()
- { KeyMap kmap;
- GetKeys(&kmap);
- return BitTst(kmap, 55);
- }
-
- Bool shiftKeyIsDown()
- { KeyMap kmap;
- GetKeys(&kmap);
- return BitTst(kmap, 56);
- }
-
- Bool capsLockIsDown()
- { KeyMap kmap;
- GetKeys(&kmap);
- return BitTst(kmap, 57);
- }
-
- Bool optionKeyIsDown()
- { KeyMap kmap;
- GetKeys(&kmap);
- return BitTst(kmap, 58);
- }
-
- Bool MouseButton()
- {
- return Button();
- }
-
- Bool Keypress()
- { EventRecord ev;
- return EventAvail( keyDownMask+keyUpMask+autoKeyMask, &ev);
- }
-
-
-
- char *StdGetFile(
- char* prompt,
- OSType fileTypes[],
- int nFileTypes)
- {
- Point wher; /*where to display dialog*/
- SFReply reply; /*reply record*/
- short len;
- static char filename[80] = "\0";
-
- wher.h = 80;
- wher.v = 90;
- if (optionKeyIsDown()) nFileTypes=0;
-
- SFGetFile(wher, prompt, nil, nFileTypes, fileTypes, nil, &reply);
-
- if (reply.good) {
- len = SetVol(nil, reply.vRefNum);
- len = reply.fName[0];
- strncpy(filename, (char *)(&reply.fName[1]), len);
- filename[len]= '\0';
- return filename;
- }
- else
- return NULL;
- }
-
-
- //pascal void Delay(long numTicks,long *finalTicks) = {0xA03B,0x2280};
- //pascal unsigned long TickCount(void) = 0xA975;
-
- int usleep( unsigned long usec)
- {
- // usleep(10000); == sleep for 1/100th of a second
- long finalTicks = 0;
-
- /*******
- return 0;
- *****/
- // usec /= 16667;
- usec >>= 14;
- if (usec < 1) Delay( 1, &finalTicks);
- else Delay( usec, &finalTicks);
- return 0;
- }
-
- long hundredthsofseconds()
- {
- //return (long) (100 * TickCount() / 60); /* == 100ths of seconds since startup */
- return TickCount() << 1; /* == 60ths of seconds since startup */
- }
-
-