15
15
#include " arrays.h"
16
16
17
17
#include < cairo/cairo.h>
18
- #ifdef TK
19
- #include < tk.h>
20
- #endif
21
18
#include < vector>
22
19
#include < string>
23
20
#include < limits>
@@ -190,225 +187,6 @@ namespace ecolab
190
187
}
191
188
};
192
189
193
- #ifdef TK
194
- struct PhotoImageBlock : public Tk_PhotoImageBlock
195
- {
196
- PhotoImageBlock (): transparency(true ) {}
197
- PhotoImageBlock (int x, int y, bool transparency):
198
- transparency (transparency)
199
- {
200
- pixelPtr=NULL ;
201
- width=x;
202
- height=y;
203
- cairo_format_t format = transparency?
204
- CAIRO_FORMAT_ARGB32: CAIRO_FORMAT_RGB24;
205
- pitch = cairo_format_stride_for_width (format, width);
206
- pixelSize=4 ;
207
- for (int i=0 ; i<3 ; ++i) offset[i]=2 -i;
208
- offset[3 ]=3 ;
209
- }
210
- bool transparency;
211
- };
212
-
213
- class TkPhotoSurface : public Surface
214
- {
215
- Tk_PhotoHandle photo;
216
- std::vector<unsigned char > imageData;
217
- PhotoImageBlock imageBlock;
218
- public:
219
- // / if true, then blits do not clear previous photo contents
220
- bool compositing;
221
-
222
- double width () const override {return imageBlock.width ;}
223
- double height () const override {return imageBlock.height ;}
224
- void clear () override {
225
- if (imageBlock.transparency )
226
- // make it transparent
227
- memset (imageData.data (),0 ,imageData.size ());
228
- else
229
- // make it white
230
- memset (imageData.data (),255 ,imageData.size ());
231
- }
232
- TkPhotoSurface (Tk_PhotoHandle photo, bool transparency=true ):
233
- photo (photo), compositing(false )
234
- {
235
- init (transparency);
236
- }
237
-
238
- void init (bool transparency=true )
239
- {
240
- int width, height;
241
- Tk_PhotoGetSize (photo, &width, &height);
242
- imageBlock = PhotoImageBlock (width, height, transparency);
243
- imageData.resize (size_t (imageBlock.pitch ) * imageBlock.height );
244
- imageBlock.pixelPtr = imageData.data ();
245
-
246
- surface ( cairo_image_surface_create_for_data
247
- (imageData.data (), transparency?
248
- CAIRO_FORMAT_ARGB32: CAIRO_FORMAT_RGB24,
249
- width, height, imageBlock.pitch ));
250
- }
251
-
252
- void resize (size_t width, size_t height) override
253
- {
254
- // #if TK_MAJOR_VERSION == 8 && TK_MINOR_VERSION < 5
255
- // Tk_PhotoSetSize(photo, width, height);
256
- // #else
257
- // Tk_PhotoSetSize(interp(), photo, width, height);
258
- // #endif
259
- // init(imageBlock.transparency);
260
- }
261
-
262
- // / copy the cairo output into the TkPhoto
263
- void blit (unsigned x, unsigned y, unsigned width, unsigned height) override
264
- {
265
- // imageBlock.pixelPtr=&imageData[x*imageBlock.pixelSize + y*imageBlock.pitch];
266
- // if (width != unsigned(imageBlock.width) ||
267
- // height != unsigned(imageBlock.height))
268
- // #if TK_MAJOR_VERSION == 8 && TK_MINOR_VERSION < 5
269
- // Tk_PhotoSetSize(photo, width, height);
270
- // Tk_PhotoPutBlock
271
- // (photo, &imageBlock, 0, 0, width, height,
272
- // compositing? TK_PHOTO_COMPOSITE_OVERLAY:TK_PHOTO_COMPOSITE_SET);
273
- // #else
274
- // Tk_PhotoSetSize(interp(), photo, width, height);
275
- // Tk_PhotoPutBlock(interp(), photo, &imageBlock, 0, 0, width, height,
276
- // compositing? TK_PHOTO_COMPOSITE_OVERLAY:TK_PHOTO_COMPOSITE_SET);
277
- // #endif
278
- }
279
- void blit () override {blit (0 ,0 ,imageBlock.width , imageBlock.height );}
280
- void requestRedraw () override {blit ();}
281
- };
282
-
283
- class CairoImage
284
- {
285
- CLASSDESC_ACCESS (CairoImage);
286
- protected:
287
- void resize (size_t width, size_t height);
288
-
289
- // / resize photo and surface
290
- // / Complaint draw() routines should set the transformation
291
- // / matrix to this, rather than the identity matrix, to allow
292
- // / scaling. identMatrix is automatically translated to the
293
- // / centre of the cairoSurface
294
- double xScale, yScale; // /< track scale requests
295
- double m_scale, rotation;// /< track setMatrix requests
296
-
297
- public:
298
- // / configSpecs for this type (itemconfigure options)
299
- static Tk_ConfigSpec configSpecs[];
300
-
301
- SurfacePtr cairoSurface;
302
-
303
- // / sets the transformation matrix so that user coordinates are
304
- // / centered on the bitmap, and scaled according to the current
305
- // / scale value
306
- void initMatrix ();
307
-
308
- // / implementation of -scale and -rotation item options
309
- void setMatrix (double scale, double rotation)
310
- {m_scale=scale, this ->rotation =rotation;}
311
-
312
- // apply an additional scale transformation
313
- void scale (double xScale, double yScale) {
314
- if (xScale!=1 || yScale!=1 )
315
- {this ->xScale *=xScale; this ->yScale *=yScale;}
316
- }
317
-
318
- CairoImage (): xScale(1 ), yScale(1 ) {setMatrix (1 ,0 );}
319
- CairoImage (const SurfacePtr& cairoSurface):
320
- xScale (1 ), yScale(1 ), cairoSurface(cairoSurface) {setMatrix (1 ,0 );}
321
-
322
- virtual ~CairoImage () {}
323
-
324
- void attachToImage (const std::string& imgName);
325
-
326
- // / distance x, y is from CairoItem (in device coordinates)
327
- double distanceFrom (double x, double y) const ;
328
-
329
- // / bounding box (in device coordinates) of the clip region
330
- virtual ecolab::array_ns::array<double > boundingBox ();
331
-
332
- // / return -1, 0, 1 if rectangle is outside, partially inside or
333
- // / completely inside the clip. Arguments are in device
334
- // / coordinates
335
- int inClip (double x0, double y0, double x1, double y1) const ;
336
-
337
- // / draw this item. @throw nothing.
338
- virtual void draw ()=0;
339
- void redrawIfSurfaceTooSmall ();
340
- void blit () {cairoSurface->blit ();}
341
-
342
- };
343
-
344
- // / internal structure used for Tk Canvas cairo image items
345
- struct ImageItem {
346
- Tk_Item header; /* Generic stuff that's the same for all
347
- * types. MUST BE FIRST IN STRUCTURE. */
348
- Tk_Canvas canvas; /* Canvas containing the image. */
349
- double x, y; /* Coordinates of positioning point for
350
- * image. */
351
- double scale; /* scale = 1 means cairo drawing
352
- coordinates in the range [-1,1] map
353
- to [x0,x1], [y0,y1] of the image
354
- pixmap */
355
-
356
- double rotation; /* rotation opf cairo user coordinate
357
- with respect to pixmap coordinates,
358
- in radians */
359
-
360
- Tk_Anchor anchor; /* Where to anchor image relative to (x,y). */
361
- char *imageString; /* String describing -image option
362
- * (malloc-ed). NULL means no image right
363
- * now. */
364
- char *activeImageString; /* String describing -activeimage option.
365
- * NULL means no image right now. */
366
- char *disabledImageString; /* String describing -disabledimage option.
367
- * NULL means no image right now. */
368
- Tk_Image image; /* Image to display in window, or NULL if no
369
- * image at present. */
370
- Tk_Image activeImage; /* Image to display in window, or NULL if no
371
- * image at present. */
372
- Tk_Image disabledImage; /* Image to display in window, or NULL if no
373
- * image at present. */
374
- bool redrawing; // set this flag to disable enqueing of eventually redraw
375
- CairoImage *cairoItem;
376
- };
377
-
378
- namespace TkImageCode
379
- {
380
- int CreateImage (Tcl_Interp *interp, Tk_Canvas canvas, Tk_Item *itemPtr,
381
- int objc,Tcl_Obj *CONST objv[],Tk_ConfigSpec[]);
382
- void DeleteImage (Tk_Canvas canvas, Tk_Item *itemPtr, Display *display);
383
- // / generic configure function, given a particular \a configSpecs
384
- int configureCairoItem (Tcl_Interp *interp, Tk_Canvas canvas,
385
- Tk_Item *itemPtr, int objc,
386
- Tcl_Obj *const objv[],
387
- int flags, Tk_ConfigSpec configSpecs[]);
388
- void ComputeImageBbox (Tk_Canvas canvas, ImageItem *imgPtr);
389
- }
390
-
391
- // / factory method for creating a CairoImage, to be used for
392
- // / creating Tk canvas itemTypes
393
- template <class C >
394
- int createImage (Tcl_Interp *interp, Tk_Canvas canvas, Tk_Item *itemPtr,
395
- int objc,Tcl_Obj *CONST84 objv[])
396
- {
397
- if (TkImageCode::CreateImage (interp,canvas,itemPtr,objc,objv,C::configSpecs)==TCL_OK)
398
- {
399
- ImageItem* imgPtr=(ImageItem*)(itemPtr);
400
- imgPtr->cairoItem = new C;
401
- imgPtr->cairoItem ->setMatrix (imgPtr->scale , imgPtr->rotation );
402
- TkImageCode::ComputeImageBbox (canvas, imgPtr);
403
- return TCL_OK;
404
- }
405
- TkImageCode::DeleteImage (canvas, itemPtr,
406
- Tk_Display (Tk_CanvasTkwin (canvas)));
407
- return TCL_ERROR;
408
- }
409
-
410
- const Tk_ItemType& cairoItemType ();
411
- #endif // #ifdef TK
412
190
413
191
}
414
192
}
@@ -426,4 +204,9 @@ namespace classdesc_access
426
204
427
205
#include " cairo_base.cd"
428
206
#endif
207
+
208
+ #ifdef TK
209
+ #include " tk_cairo.h"
210
+ #endif
211
+
429
212
#endif
0 commit comments