53 #if defined ( __WIN32__ ) || defined ( MAC_TCL )
58 #define malloc ckalloc
59 #define free( m ) ckfree( (char *) m )
60 #define realloc ckrealloc
61 #define calloc ckcalloc
63 #if defined ( __WIN32__ ) || defined ( MAC_TCL )
72 #define ckalloc malloc
83 PLDLLIMPEXP_DRIVER const char* plD_DEVICE_INFO_tkwin =
"tkwin:New tk driver:1:tkwin:45:tkwin\n";
86 void *
ckcalloc(
size_t nmemb,
size_t size );
97 #define XSynchronize( display, bool ) { display->request++; }
98 #define XSync( display, bool ) { display->request++; }
99 #define XFlush( display )
120 void CopyColour( XColor* from, XColor* to );
122 static int pltk_AreWeGrayscale(
PlPlotter *plf );
135 #define PHYSICAL 0 // Enables physical scaling..
153 #define XWM_COLORS 70
154 #define CMAP0_COLORS 16
155 #define CMAP1_COLORS 50
156 #define MAX_COLORS 256
162 static int sxwm_colors_set;
163 static XColor sxwm_colors[MAX_COLORS];
170 #if !defined ( MAC_TCL ) && !defined ( __WIN32__ )
171 static unsigned char CreatePixmapStatus;
172 static int CreatePixmapErrorHandler( Display *
display, XErrorEvent *
error );
196 #ifdef USING_PLESC_COPY
209 void plD_line_tkwin(
PLStream *,
short,
short,
short,
short );
210 void plD_polyline_tkwin(
PLStream *,
short *,
short *,
PLINT );
220 #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 );
567 plwarn(
"WaitForPage: Illegal call --- driver can't find enclosing PlPlotter" );
572 while ( !( dev->
flags ) && !Tcl_InterpDeleted( plf->
interp ) && ( Tk_GetNumMainWindows() > 0 ) )
577 if ( Tcl_InterpDeleted( plf->
interp ) || ( Tk_GetNumMainWindows() <= 0 ) )
599 xrect.x = 0; xrect.y = 0;
600 xrect.width = (
short unsigned) dev->
width;
601 xrect.height = (
short unsigned) dev->
height;
604 if ( dev->
flags & 1 )
646 int ixwd = tkwd->
ixwd;
648 #if !defined ( MAC_TCL ) && !defined ( __WIN32__ )
649 XCloseDisplay( tkwd->
display );
676 if ( dev->
flags & 1 )
685 int icol0 = pls->
icol0;
710 if ( tkwd->
ncol1 == 0 )
713 if ( tkwd->
ncol1 < 2 )
761 if ( dev->
flags & 1 )
777 FillPolygonCmd( pls );
802 #ifdef USING_PLESC_COPY
810 #ifdef USING_PLESC_COPY
821 int x0, w, x1, y0, h, y1;
834 x0, y0, w, h, x1, y1 );
838 x0, y0, w, h, x1, y1 );
860 pts = (XPoint *) malloc(
sizeof ( XPoint ) * (size_t) ( pls->
dev_npts ) );
867 for ( i = 0; i < pls->
dev_npts; i++ )
869 pts[i].x = (short) ( dev->
xscale * pls->
dev_x[i] );
877 pts, pls->
dev_npts, Nonconvex, CoordModeOrigin );
881 pts, pls->
dev_npts, Nonconvex, CoordModeOrigin );
934 plwarn(
"Init: Illegal call --- driver can't find enclosing PlPlotter" );
955 gcValues.background = tkwd->
cmap0[0].pixel;
956 gcValues.foreground = 0xFF;
957 gcValues.function = GXxor;
958 mask = GCForeground | GCBackground | GCFunction;
964 dev->
width = (
unsigned int) Tk_Width( plf->
tkwin );
965 dev->
height = (
unsigned int) Tk_Height( plf->
tkwin );
966 dev->
border = (
unsigned int) Tk_InternalBorderWidth( plf->
tkwin );
967 tkwd->
depth = (
unsigned int) Tk_Depth( plf->
tkwin );
1011 int x,
y, width, height;
1019 plwarn(
"ExposeCmd: Illegal call -- driver uninitialized" );
1025 if ( pldis == NULL )
1029 width = (int) dev->
width;
1030 height = (
int) dev->
height;
1036 width = (int) pldis->
width;
1037 height = (
int) pldis->
height;
1047 x, y, (
unsigned int) width, (
unsigned int) height, x, y );
1053 int x0 =
x, x1 = x + width, y0 =
y, y1 = y + height;
1054 pts[0].x = (short) x0; pts[0].y = (short) y0;
1055 pts[1].x = (short) x1; pts[1].y = (short) y0;
1056 pts[2].x = (short) x1; pts[2].y = (short) y1;
1057 pts[3].x = (short) x0; pts[3].y = (short) y1;
1058 pts[4].x = (short) x0; pts[4].y = (short) y0;
1091 plwarn(
"ResizeCmd: Illegal call -- driver uninitialized" );
1097 if ( pldis == NULL )
1099 plwarn(
"ResizeCmd: Illegal call -- window pointer uninitialized" );
1133 #if defined ( __WIN32__ ) || defined ( MAC_TCL )
1155 CreatePixmap( pls );
1160 plD_bop_tkwin( pls );
1195 plwarn(
"RedrawCmd: Illegal call -- driver uninitialized" );
1204 plD_bop_tkwin( pls );
1234 #if !defined ( MAC_TCL ) && !defined ( __WIN32__ )
1235 int ( *oldErrorHandler )( Display *, XErrorEvent * );
1236 oldErrorHandler = XSetErrorHandler( CreatePixmapErrorHandler );
1237 CreatePixmapStatus = Success;
1242 if ( dev->
width == 0 )
1251 pldebug(
"CreatePixmap",
1252 "creating pixmap: width = %d, height = %d, depth = %d\n",
1269 dev->
pixmap = Tk_GetPixmap( tkwd->
display, Tk_WindowId( tkwin ),
1270 Tk_Width( tkwin ), Tk_Height( tkwin ),
1271 DefaultDepthOfScreen( Tk_Screen( tkwin ) ) );
1273 #if !defined ( MAC_TCL ) && !defined ( __WIN32__ )
1274 if ( CreatePixmapStatus != Success )
1279 fprintf( stderr,
"\n\
1280 Warning: pixmap could not be allocated (insufficient memory on server).\n\
1281 Driver will redraw the entire plot to handle expose events.\n" );
1284 XSetErrorHandler( oldErrorHandler );
1312 tkwd->
depth = (
unsigned int) depth;
1331 unsigned long plane_masks[1], pixels[MAX_COLORS];
1343 if ( XAllocColorCells( tkwd->
display, tkwd->
map, False,
1344 plane_masks, 0, pixels, 1 ) )
1346 tkwd->
cmap0[0].pixel = pixels[0];
1350 plexit(
"couldn't allocate background color cell" );
1355 npixels = MAX_COLORS;
1358 if ( XAllocColorCells( tkwd->
display, tkwd->
map, False,
1359 plane_masks, 0, pixels, npixels ) )
1369 for ( i = 0; i < npixels - 1; i++ )
1371 if ( pixels[i] == ( ~tkwd->
cmap0[0].pixel & 0xFF ) )
1377 tkwd->
fgcolor.pixel = pixels[i];
1378 for ( j = 0; j < npixels; j++ )
1381 XFreeColors( tkwd->
display, tkwd->
map, &pixels[j], 1, 0 );
1399 int gslevbg, gslevfg;
1414 gslevbg = (int) ( ( (
long) pls->
cmap0[0].
r +
1416 (
long) pls->
cmap0[0].
b ) / 3 );
1430 if ( gslevbg > 0x7F )
1435 fgcolor.
r = fgcolor.
g = fgcolor.
b = (
unsigned char) gslevfg;
1478 AllocCustomMap( pls );
1519 XColor xwm_colors[MAX_COLORS];
1523 unsigned long plane_masks[1], pixels[MAX_COLORS];
1530 for ( i = 0; i < MAX_COLORS; i++ )
1532 xwm_colors[i].pixel = (
long unsigned) i;
1535 XQueryColors( tkwd->
display, tkwd->
map, xwm_colors, MAX_COLORS );
1549 tkwd->
visual, AllocNone );
1554 npixels = MAX_COLORS;
1557 if ( XAllocColorCells( tkwd->
display, tkwd->
map, False,
1558 plane_masks, 0, pixels, npixels ) )
1562 plexit(
"couldn't allocate any colors" );
1567 for ( i = 0; i < XWM_COLORS; i++ )
1569 XStoreColor( tkwd->
display, tkwd->
map, &xwm_colors[i] );
1570 pixels[xwm_colors[i].pixel] = 0;
1575 for ( i = 0; i < tkwd->
ncol0; i++ )
1578 pixels[tkwd->
cmap0[i].pixel] = 0;
1587 if ( sxwm_colors_set )
1589 for ( i = 0; i < MAX_COLORS; i++ )
1591 if ( ( xwm_colors[i].red != sxwm_colors[i].red ) ||
1592 ( xwm_colors[i].green != sxwm_colors[i].green ) ||
1593 ( xwm_colors[i].blue != sxwm_colors[i].blue ) )
1595 if ( pixels[i] != 0 )
1597 XStoreColor( tkwd->
display, tkwd->
map, &xwm_colors[i] );
1606 for ( i = 0; i < npixels; i++ )
1608 if ( pixels[i] != 0 )
1609 XFreeColors( tkwd->
display, tkwd->
map, &pixels[i], 1, 0 );
1632 unsigned long plane_masks[1], pixels[MAX_COLORS];
1640 npixels = pls->
ncol0 - 1;
1643 if ( XAllocColorCells( tkwd->
display, tkwd->
map, False,
1644 plane_masks, 0, &pixels[1], npixels ) )
1648 plexit(
"couldn't allocate any colors" );
1651 tkwd->
ncol0 = npixels + 1;
1652 for ( i = 1; i < tkwd->
ncol0; i++ )
1654 tkwd->
cmap0[i].pixel = pixels[i];
1679 unsigned long plane_masks[1], pixels[MAX_COLORS];
1686 npixels =
MAX( 2,
MIN( CMAP1_COLORS, pls->
ncol1 ) );
1690 if ( XAllocColorCells( tkwd->
display, tkwd->
map, False,
1691 plane_masks, 0, pixels, npixels ) )
1702 "Warning: unable to allocate sufficient colors in cmap1\n" );
1707 tkwd->
ncol1 = npixels;
1709 fprintf( stderr,
"AllocCmap1 (xwin.c): Allocated %d colors in cmap1\n", npixels );
1715 for ( j = i = 0; i < tkwd->
ncol1; i++ )
1717 while ( pixels[j] == 0 )
1720 tkwd->
cmap1[i].pixel = pixels[j];
1724 if ( j >= tkwd->
ncol1 )
1728 tkwd->
ncol1 = npixels;
1749 for ( i = 1; i < tkwd->
ncol0; i++ )
1760 void CopyColour( XColor* from, XColor* to )
1762 to->pixel = from->pixel;
1763 to->red = from->red;
1764 to->blue = from->blue;
1765 to->green = from->green;
1766 to->flags = from->flags;
1787 for ( i = 0; i < tkwd->
ncol1; i++ )
1803 XStoreColor( tkwd->
display, tkwd->
map, col );
1808 CopyColour( xc, col );
1820 #define ToXColor( a ) ( ( ( 0xFF & ( a ) ) << 8 ) | ( a ) )
1821 #define ToPLColor( a ) ( ( (U_LONG) a ) >> 8 )
1826 xcolor->red = (
short unsigned) ToXColor( plcolor->
r );
1827 xcolor->green = (
short unsigned) ToXColor( plcolor->
g );
1828 xcolor->blue = (
short unsigned) ToXColor( plcolor->
b );
1829 xcolor->flags = DoRed | DoGreen | DoBlue;
1842 plcolor->
r = (
unsigned char) ToPLColor( xcolor->red );
1843 plcolor->
g = (
unsigned char) ToPLColor( xcolor->green );
1844 plcolor->
b = (
unsigned char) ToPLColor( xcolor->blue );
1861 color = ToPLColor( xcolor->red );
1863 if ( plcolor->
r != color )
1866 plcolor->
r = (
unsigned char) color;
1868 color = ToPLColor( xcolor->green );
1869 if ( plcolor->
g != color )
1872 plcolor->
g = (
unsigned char) color;
1874 color = ToPLColor( xcolor->blue );
1875 if ( plcolor->
b != color )
1878 plcolor->
b = (
unsigned char) color;
1894 #if defined ( __cplusplus ) || defined ( c_plusplus )
1895 #define THING c_class
1902 visual = Tk_Visual( plf->
tkwin );
1903 if ( ( visual->THING != GrayScale ) && ( visual->THING != StaticGray ) )
1909 #if !defined ( MAC_TCL ) && !defined ( __WIN32__ )
1919 CreatePixmapErrorHandler( Display *
display, XErrorEvent *
error )
1921 if ( error->error_code == BadAlloc )
1923 CreatePixmapStatus = error->error_code;
1928 XGetErrorText( display, error->error_code, buffer, 256 );
1929 fprintf( stderr,
"Error in XCreatePixmap: %s.\n", buffer );
1949 ptr = (
long *) malloc( size );
1955 for ( size = ( size /
sizeof (
long ) ) + 1, p = ptr; --size; )
1960 for ( size = ( size /
sizeof (
long ) ) + 1, p = ptr - 1; --size; )
void(* plD_line_fp)(struct PLStream_struct *, short, short, short, short)
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 PLFLT PLINT PLINT PLFLT x
for(i=0;i< nx;i++) if(n_values[i]< ny) ny
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 *)
void PLFLT PLINT PLINT PLFLT PLFLT y
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
void plP_setphy(PLINT xmin, PLINT xmax, PLINT ymin, PLINT ymax)
char PLDLLIMPEXP * plstrdup(const char *src)
void plRemakePlot(PLStream *pls)
void plwarn(const char *errormsg)
void PLColor_from_TkColor(PLColor *plcolor, XColor *xcolor)
struct PlPlotter * plPlotterPtr
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 *)
void plexit(const char *errormsg)