51 #if defined ( __WIN32__ ) || defined ( MAC_TCL )
56 #define malloc ckalloc
57 #define free( m ) ckfree( (char *) m )
58 #define realloc ckrealloc
59 #define calloc ckcalloc
61 #if defined ( __WIN32__ ) || defined ( MAC_TCL )
70 #define ckalloc malloc
81 PLDLLIMPEXP_DRIVER const char* plD_DEVICE_INFO_tkwin =
"tkwin:New tk driver:1:tkwin:45:tkwin\n";
84 void *
ckcalloc(
size_t nmemb,
size_t size );
95 #define XSynchronize( display, bool ) { display->request++; }
96 #define XSync( display, bool ) { display->request++; }
97 #define XFlush( display )
118 void CopyColour( XColor* from, XColor* to );
120 static int pltk_AreWeGrayscale(
PlPlotter *plf );
133 #define PHYSICAL 0 // Enables physical scaling..
151 #define XWM_COLORS 70
152 #define CMAP0_COLORS 16
153 #define CMAP1_COLORS 50
154 #define MAX_COLORS 256
160 static int sxwm_colors_set;
161 static XColor sxwm_colors[MAX_COLORS];
168 #if !defined ( MAC_TCL ) && !defined ( __WIN32__ )
169 static unsigned char CreatePixmapStatus;
170 static int CreatePixmapErrorHandler( Display *
display, XErrorEvent *
error );
194 #ifdef USING_PLESC_COPY
207 void plD_line_tkwin(
PLStream *,
short,
short,
short,
short );
208 void plD_polyline_tkwin(
PLStream *,
short *,
short *,
PLINT );
219 #ifndef ENABLE_DYNDRIVERS
262 if ( pls->
dev == NULL )
271 dev->
xlen = (short) ( xmax - xmin );
272 dev->
ylen = (short) ( ymax - ymin );
312 if ( pls->
dev != NULL )
313 plwarn(
"plD_open_tkw: device pointer is already set" );
316 if ( pls->
dev == NULL )
317 plexit(
"plD_init_tkw: Out of memory." );
331 if ( tkwDisplay[i] == NULL )
337 dev->
tkwd = tkwDisplay[i];
344 else if ( strcmp( tkwDisplay[i]->displayName, pls->
FileName ) == 0 )
346 dev->
tkwd = tkwDisplay[i];
353 if ( dev->
tkwd == NULL )
356 if ( dev->
tkwd == NULL )
357 plexit(
"Init: Out of memory." );
361 if ( tkwDisplay[i] == NULL )
364 if ( i == PLTKDISPLAYS )
365 plexit(
"Init: Out of tkwDisplay's." );
376 plexit(
"No tk plframe widget to connect to" );
386 #if defined ( MAC_TCL ) || defined ( __WIN32__ )
402 plexit(
"Can't open display" );
408 XSynchronize( tkwd->
display, 1 );
452 plD_line_tkwin(
PLStream *pls,
short x1a,
short y1a,
short x2a,
short y2a )
457 int x1 = x1a, y1 = y1a, x2 = x2a, y2 = y2a;
459 if ( dev->
flags & 1 )
465 x1 = (int) ( x1 * dev->
xscale );
466 x2 = (int) ( x2 * dev->
xscale );
467 y1 = (int) ( y1 * dev->
yscale );
468 y2 = (int) ( y2 * dev->
yscale );
484 plD_polyline_tkwin(
PLStream *pls,
short *xa,
short *ya,
PLINT npts )
493 if ( dev->
flags & 1 )
498 pts = (XPoint *) malloc(
sizeof ( XPoint ) * (size_t) npts );
505 for ( i = 0; i < npts; i++ )
507 pts[i].x = (short) ( dev->
xscale * xa[i] );
508 pts[i].y = (short) ( dev->
yscale * ( dev->
ylen - ya[i] ) );
538 if ( dev->
flags & 1 )
543 ExposeCmd( pls, NULL );
562 if ( dev->
flags & 1 )
587 plwarn(
"WaitForPage: Illegal call --- driver can't find enclosing PlPlotter" );
592 while ( !( dev->
flags ) && !Tcl_InterpDeleted( plf->
interp ) && ( Tk_GetNumMainWindows() > 0 ) )
597 if ( Tcl_InterpDeleted( plf->
interp ) || ( Tk_GetNumMainWindows() <= 0 ) )
619 xrect.x = 0; xrect.y = 0;
620 xrect.width = (
short unsigned) dev->
width;
621 xrect.height = (
short unsigned) dev->
height;
624 if ( dev->
flags & 1 )
666 int ixwd = tkwd->
ixwd;
668 #if !defined ( MAC_TCL ) && !defined ( __WIN32__ )
669 XCloseDisplay( tkwd->
display );
696 if ( dev->
flags & 1 )
705 int icol0 = pls->
icol0;
730 if ( tkwd->
ncol1 == 0 )
733 if ( tkwd->
ncol1 < 2 )
781 if ( dev->
flags & 1 )
797 FillPolygonCmd( pls );
822 #ifdef USING_PLESC_COPY
830 #ifdef USING_PLESC_COPY
841 int x0, w, x1, y0, h, y1;
854 x0, y0, w, h, x1, y1 );
858 x0, y0, w, h, x1, y1 );
880 pts = (XPoint *) malloc(
sizeof ( XPoint ) * (size_t) ( pls->
dev_npts ) );
887 for ( i = 0; i < pls->
dev_npts; i++ )
889 pts[i].x = (short) ( dev->
xscale * pls->
dev_x[i] );
897 pts, pls->
dev_npts, Nonconvex, CoordModeOrigin );
901 pts, pls->
dev_npts, Nonconvex, CoordModeOrigin );
954 plwarn(
"Init: Illegal call --- driver can't find enclosing PlPlotter" );
975 gcValues.background = tkwd->
cmap0[0].pixel;
976 gcValues.foreground = 0xFF;
977 gcValues.function = GXxor;
978 mask = GCForeground | GCBackground | GCFunction;
984 dev->
width = (
unsigned int) Tk_Width( plf->
tkwin );
985 dev->
height = (
unsigned int) Tk_Height( plf->
tkwin );
986 dev->
border = (
unsigned int) Tk_InternalBorderWidth( plf->
tkwin );
987 tkwd->
depth = (
unsigned int) Tk_Depth( plf->
tkwin );
1009 CreatePixmap( pls );
1031 int x, y, width, height;
1039 plwarn(
"ExposeCmd: Illegal call -- driver uninitialized" );
1045 if ( pldis == NULL )
1049 width = (int) dev->
width;
1050 height = (
int) dev->
height;
1056 width = (int) pldis->
width;
1057 height = (
int) pldis->
height;
1067 x, y, (
unsigned int) width, (
unsigned int) height, x, y );
1073 int x0 = x, x1 = x + width, y0 = y, y1 = y + height;
1074 pts[0].x = (short) x0; pts[0].y = (short) y0;
1075 pts[1].x = (short) x1; pts[1].y = (short) y0;
1076 pts[2].x = (short) x1; pts[2].y = (short) y1;
1077 pts[3].x = (short) x0; pts[3].y = (short) y1;
1078 pts[4].x = (short) x0; pts[4].y = (short) y0;
1111 plwarn(
"ResizeCmd: Illegal call -- driver uninitialized" );
1117 if ( pldis == NULL )
1119 plwarn(
"ResizeCmd: Illegal call -- window pointer uninitialized" );
1153 #if defined ( __WIN32__ ) || defined ( MAC_TCL )
1175 CreatePixmap( pls );
1180 plD_bop_tkwin( pls );
1215 plwarn(
"RedrawCmd: Illegal call -- driver uninitialized" );
1224 plD_bop_tkwin( pls );
1254 #if !defined ( MAC_TCL ) && !defined ( __WIN32__ )
1255 int ( *oldErrorHandler )( Display *, XErrorEvent * );
1256 oldErrorHandler = XSetErrorHandler( CreatePixmapErrorHandler );
1257 CreatePixmapStatus = Success;
1262 if ( dev->
width == 0 )
1271 pldebug(
"CreatePixmap",
1272 "creating pixmap: width = %d, height = %d, depth = %d\n",
1289 dev->
pixmap = Tk_GetPixmap( tkwd->
display, Tk_WindowId( tkwin ),
1290 Tk_Width( tkwin ), Tk_Height( tkwin ),
1291 DefaultDepthOfScreen( Tk_Screen( tkwin ) ) );
1293 #if !defined ( MAC_TCL ) && !defined ( __WIN32__ )
1294 if ( CreatePixmapStatus != Success )
1299 fprintf( stderr,
"\n\
1300 Warning: pixmap could not be allocated (insufficient memory on server).\n\
1301 Driver will redraw the entire plot to handle expose events.\n" );
1304 XSetErrorHandler( oldErrorHandler );
1332 tkwd->
depth = (
unsigned int) depth;
1351 unsigned long plane_masks[1], pixels[MAX_COLORS];
1363 if ( XAllocColorCells( tkwd->
display, tkwd->
map, False,
1364 plane_masks, 0, pixels, 1 ) )
1366 tkwd->
cmap0[0].pixel = pixels[0];
1370 plexit(
"couldn't allocate background color cell" );
1375 npixels = MAX_COLORS;
1378 if ( XAllocColorCells( tkwd->
display, tkwd->
map, False,
1379 plane_masks, 0, pixels, npixels ) )
1389 for ( i = 0; i < npixels - 1; i++ )
1391 if ( pixels[i] == ( ~tkwd->
cmap0[0].pixel & 0xFF ) )
1397 tkwd->
fgcolor.pixel = pixels[i];
1398 for ( j = 0; j < npixels; j++ )
1401 XFreeColors( tkwd->
display, tkwd->
map, &pixels[j], 1, 0 );
1419 int gslevbg, gslevfg;
1434 gslevbg = (int) ( ( (
long) pls->
cmap0[0].
r +
1436 (
long) pls->
cmap0[0].
b ) / 3 );
1450 if ( gslevbg > 0x7F )
1455 fgcolor.
r = fgcolor.
g = fgcolor.
b = (
unsigned char) gslevfg;
1498 AllocCustomMap( pls );
1539 XColor xwm_colors[MAX_COLORS];
1543 unsigned long plane_masks[1], pixels[MAX_COLORS];
1550 for ( i = 0; i < MAX_COLORS; i++ )
1552 xwm_colors[i].pixel = (
long unsigned) i;
1555 XQueryColors( tkwd->
display, tkwd->
map, xwm_colors, MAX_COLORS );
1569 tkwd->
visual, AllocNone );
1574 npixels = MAX_COLORS;
1577 if ( XAllocColorCells( tkwd->
display, tkwd->
map, False,
1578 plane_masks, 0, pixels, npixels ) )
1582 plexit(
"couldn't allocate any colors" );
1587 for ( i = 0; i < XWM_COLORS; i++ )
1589 XStoreColor( tkwd->
display, tkwd->
map, &xwm_colors[i] );
1590 pixels[xwm_colors[i].pixel] = 0;
1595 for ( i = 0; i < tkwd->
ncol0; i++ )
1598 pixels[tkwd->
cmap0[i].pixel] = 0;
1607 if ( sxwm_colors_set )
1609 for ( i = 0; i < MAX_COLORS; i++ )
1611 if ( ( xwm_colors[i].red != sxwm_colors[i].red ) ||
1612 ( xwm_colors[i].green != sxwm_colors[i].green ) ||
1613 ( xwm_colors[i].blue != sxwm_colors[i].blue ) )
1615 if ( pixels[i] != 0 )
1617 XStoreColor( tkwd->
display, tkwd->
map, &xwm_colors[i] );
1626 for ( i = 0; i < npixels; i++ )
1628 if ( pixels[i] != 0 )
1629 XFreeColors( tkwd->
display, tkwd->
map, &pixels[i], 1, 0 );
1652 unsigned long plane_masks[1], pixels[MAX_COLORS];
1660 npixels = pls->
ncol0 - 1;
1663 if ( XAllocColorCells( tkwd->
display, tkwd->
map, False,
1664 plane_masks, 0, &pixels[1], npixels ) )
1668 plexit(
"couldn't allocate any colors" );
1671 tkwd->
ncol0 = npixels + 1;
1672 for ( i = 1; i < tkwd->
ncol0; i++ )
1674 tkwd->
cmap0[i].pixel = pixels[i];
1699 unsigned long plane_masks[1], pixels[MAX_COLORS];
1706 npixels =
MAX( 2,
MIN( CMAP1_COLORS, pls->
ncol1 ) );
1710 if ( XAllocColorCells( tkwd->
display, tkwd->
map, False,
1711 plane_masks, 0, pixels, npixels ) )
1722 "Warning: unable to allocate sufficient colors in cmap1\n" );
1727 tkwd->
ncol1 = npixels;
1729 fprintf( stderr,
"AllocCmap1 (xwin.c): Allocated %d colors in cmap1\n", npixels );
1735 for ( j = i = 0; i < tkwd->
ncol1; i++ )
1737 while ( pixels[j] == 0 )
1740 tkwd->
cmap1[i].pixel = pixels[j];
1744 if ( j >= tkwd->
ncol1 )
1748 tkwd->
ncol1 = npixels;
1769 for ( i = 1; i < tkwd->
ncol0; i++ )
1780 void CopyColour( XColor* from, XColor* to )
1782 to->pixel = from->pixel;
1783 to->red = from->red;
1784 to->blue = from->blue;
1785 to->green = from->green;
1786 to->flags = from->flags;
1807 for ( i = 0; i < tkwd->
ncol1; i++ )
1823 XStoreColor( tkwd->
display, tkwd->
map, col );
1828 CopyColour( xc, col );
1840 #define ToXColor( a ) ( ( ( 0xFF & ( a ) ) << 8 ) | ( a ) )
1841 #define ToPLColor( a ) ( ( (U_LONG) a ) >> 8 )
1846 xcolor->red = (
short unsigned) ToXColor( plcolor->
r );
1847 xcolor->green = (
short unsigned) ToXColor( plcolor->
g );
1848 xcolor->blue = (
short unsigned) ToXColor( plcolor->
b );
1849 xcolor->flags = DoRed | DoGreen | DoBlue;
1862 plcolor->
r = (
unsigned char) ToPLColor( xcolor->red );
1863 plcolor->
g = (
unsigned char) ToPLColor( xcolor->green );
1864 plcolor->
b = (
unsigned char) ToPLColor( xcolor->blue );
1881 color = ToPLColor( xcolor->red );
1883 if ( plcolor->
r != color )
1886 plcolor->
r = (
unsigned char) color;
1888 color = ToPLColor( xcolor->green );
1889 if ( plcolor->
g != color )
1892 plcolor->
g = (
unsigned char) color;
1894 color = ToPLColor( xcolor->blue );
1895 if ( plcolor->
b != color )
1898 plcolor->
b = (
unsigned char) color;
1914 #if defined ( __cplusplus ) || defined ( c_plusplus )
1915 #define THING c_class
1922 visual = Tk_Visual( plf->
tkwin );
1923 if ( ( visual->THING != GrayScale ) && ( visual->THING != StaticGray ) )
1929 #if !defined ( MAC_TCL ) && !defined ( __WIN32__ )
1939 CreatePixmapErrorHandler( Display *
display, XErrorEvent *
error )
1941 if ( error->error_code == BadAlloc )
1943 CreatePixmapStatus = error->error_code;
1948 XGetErrorText( display, error->error_code, buffer, 256 );
1949 fprintf( stderr,
"Error in XCreatePixmap: %s.\n", buffer );
1969 ptr = (
long *) malloc( size );
1975 for ( size = ( size /
sizeof (
long ) ) + 1, p = ptr; --size; )
1980 for ( size = ( size /
sizeof (
long ) ) + 1, p = ptr - 1; --size; )
void(* plD_line_fp)(struct PLStream_struct *, short, short, short, short)
void plexit(PLCHAR_VECTOR errormsg)
void PLColor_to_TkColor(PLColor *plcolor, XColor *xcolor)
static int plplot_tkwin_ccmap
void(* plD_eop_fp)(struct PLStream_struct *)
void(* plD_state_fp)(struct PLStream_struct *, PLINT)
void(* plD_tidy_fp)(struct PLStream_struct *)
void * ckcalloc(size_t nmemb, size_t size)
void plcol_interp(PLStream *pls, PLColor *newcolor, int i, int ncol)
void PlplotterAtBop(Tcl_Interp *interp, register PlPlotter *plPlotterPtr)
void(* plD_wait_fp)(struct PLStream_struct *)
void(* plD_polyline_fp)(struct PLStream_struct *, short *, short *, PLINT)
void plD_open_tkwin(PLStream *pls)
void(* plD_esc_fp)(struct PLStream_struct *, PLINT, void *)
int PLColor_from_TkColor_Changed(PLColor *plcolor, XColor *xcolor)
void(* plD_bop_fp)(struct PLStream_struct *)
struct PlPlotter PlPlotter
PLDLLIMPEXP_DRIVER void plD_dispatch_init_tkwin(PLDispatchTable *pdt)
void PlplotterAtEop(Tcl_Interp *interp, register PlPlotter *plPlotterPtr)
void plP_setpxl(PLFLT xpmm, PLFLT ypmm)
#define PLDLLIMPEXP_DRIVER
static PLStream * pls[PL_NSTREAMS]
void plP_setphy(PLINT xmin, PLINT xmax, PLINT ymin, PLINT ymax)
char PLDLLIMPEXP * plstrdup(PLCHAR_VECTOR src)
void plRemakePlot(PLStream *pls)
void PLColor_from_TkColor(PLColor *plcolor, XColor *xcolor)
struct PlPlotter * plPlotterPtr
void plwarn(PLCHAR_VECTOR errormsg)
void pltkwin_setBGFG(PLStream *pls)
static const char * display
static Tcl_Interp * interp
plD_polyline_fp pl_polyline
void Tkw_StoreColor(PLStream *pls, TkwDisplay *tkwd, XColor *col)
void(* plD_init_fp)(struct PLStream_struct *)