summaryrefslogtreecommitdiffstats
path: root/bltGrMarkerBitmap.C
blob: 210b8a0944aca50c116a671311d986ae1c793922 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Smithsonian Astrophysical Observatory, Cambridge, MA, USA
 * This code has been modified under the terms listed below and is made
 * available under the same terms.
 */

/*
 *	Copyright 1993-2004 George A Howlett.
 *
 *	Permission is hereby granted, free of charge, to any person obtaining
 *	a copy of this software and associated documentation files (the
 *	"Software"), to deal in the Software without restriction, including
 *	without limitation the rights to use, copy, modify, merge, publish,
 *	distribute, sublicense, and/or sell copies of the Software, and to
 *	permit persons to whom the Software is furnished to do so, subject to
 *	the following conditions:
 *
 *	The above copyright notice and this permission notice shall be
 *	included in all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *	LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *	OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "bltC.h"

extern "C" {
#include "bltGraph.h"
};

#include "bltGrMarkerBitmap.h"

using namespace Blt;

// OptionSpecs

static Tk_OptionSpec optionSpecs[] = {
  {TK_OPTION_ANCHOR, "-anchor", "anchor", "Anchor", 
   "center", -1, Tk_Offset(BitmapMarkerOptions, anchor), 0, NULL, 0},
  {TK_OPTION_COLOR, "-background", "background", "Background",
   NULL, -1, Tk_Offset(BitmapMarkerOptions, fillColor),
   TK_OPTION_NULL_OK, NULL, 0},
  {TK_OPTION_SYNONYM, "-bg", NULL, NULL, NULL, -1, 0, 0, "-background", 0},
  {TK_OPTION_CUSTOM, "-bindtags", "bindTags", "BindTags", 
   "Bitmap all", -1, Tk_Offset(BitmapMarkerOptions, tags), 
   TK_OPTION_NULL_OK, &listObjOption, 0},
  {TK_OPTION_BITMAP, "-bitmap", "bitmap", "Bitmap", 
   NULL, -1, Tk_Offset(BitmapMarkerOptions, bitmap),
   TK_OPTION_NULL_OK, NULL, 0},
  {TK_OPTION_CUSTOM, "-coords", "coords", "Coords",
   NULL, -1, Tk_Offset(BitmapMarkerOptions, worldPts), 
   TK_OPTION_NULL_OK, &coordsObjOption, 0},
  {TK_OPTION_STRING, "-element", "element", "Element", 
   NULL, -1, Tk_Offset(BitmapMarkerOptions, elemName),
   TK_OPTION_NULL_OK, NULL, 0},
  {TK_OPTION_SYNONYM, "-fg", NULL, NULL, NULL, -1, 0, 0, "-foreground", 0},
  {TK_OPTION_SYNONYM, "-fill", NULL, NULL, NULL, -1, 0, 0, "-background", 0},
  {TK_OPTION_COLOR, "-foreground", "foreground", "Foreground",
   STD_NORMAL_FOREGROUND, -1, Tk_Offset(BitmapMarkerOptions, outlineColor), 
   TK_OPTION_NULL_OK, NULL, 0},
  {TK_OPTION_BOOLEAN, "-hide", "hide", "Hide", 
   "no", -1, Tk_Offset(BitmapMarkerOptions, hide), 0, NULL, 0},
  {TK_OPTION_CUSTOM, "-mapx", "mapX", "MapX",
   "x", -1, Tk_Offset(BitmapMarkerOptions, axes.x), 0, &xAxisObjOption, 0},
  {TK_OPTION_CUSTOM, "-mapy", "mapY", "MapY", 
   "y", -1, Tk_Offset(BitmapMarkerOptions, axes.y), 0, &yAxisObjOption, 0},
  {TK_OPTION_SYNONYM, "-outline", NULL, NULL, NULL, -1, 0, 0, "-foreground", 0},
  {TK_OPTION_STRING_TABLE, "-state", "state", "State", 
   "normal", -1, Tk_Offset(BitmapMarkerOptions, state), 0, &stateObjOption, 0},
  {TK_OPTION_BOOLEAN, "-under", "under", "Under",
   "no", -1, Tk_Offset(BitmapMarkerOptions, drawUnder), 0, NULL, 0},
  {TK_OPTION_PIXELS, "-xoffset", "xOffset", "XOffset",
   "0", -1, Tk_Offset(BitmapMarkerOptions, xOffset), 0, NULL, 0},
  {TK_OPTION_PIXELS, "-yoffset", "yOffset", "YOffset",
   "0", -1, Tk_Offset(BitmapMarkerOptions, yOffset), 0, NULL, 0},
  {TK_OPTION_END, NULL, NULL, NULL, NULL, -1, 0, 0, NULL, 0}
};

static MarkerClass bitmapMarkerClass = {
  optionSpecs,
};

BitmapMarker::BitmapMarker(Graph* graphPtr, const char* name)
  : Marker(graphPtr, name)
{
  obj.classId = CID_MARKER_BITMAP;
  obj.className = dupstr("BitmapMarker");
  classPtr = &bitmapMarkerClass;
  ops = (BitmapMarkerOptions*)calloc(1, sizeof(BitmapMarkerOptions));
  optionTable = Tk_CreateOptionTable(graphPtr->interp, optionSpecs);

  anchorPt.x =0;
  anchorPt.y =0;
  gc =NULL;
  fillGC =NULL;
  nOutlinePts =0;
  width =0;
  height =0;
}

BitmapMarker::~BitmapMarker()
{
  Graph* graphPtr = obj.graphPtr;

  if (gc)
    Tk_FreeGC(graphPtr->display, gc);
  if (fillGC)
    Tk_FreeGC(graphPtr->display, fillGC);
}

int BitmapMarker::configure()
{
  Graph* graphPtr = obj.graphPtr;
  BitmapMarkerOptions* opp = (BitmapMarkerOptions*)ops;

  if (opp->bitmap == None)
    return TCL_OK;

  XGCValues gcValues;
  unsigned long gcMask = 0;
  if (opp->outlineColor) {
    gcMask |= GCForeground;
    gcValues.foreground = opp->outlineColor->pixel;
  }

  if (opp->fillColor) {
    // Opaque bitmap: both foreground and background (fill) colors are used
    gcValues.background = opp->fillColor->pixel;
    gcMask |= GCBackground;
  }
  else {
    // Transparent bitmap: set the clip mask to the current bitmap
    gcValues.clip_mask = opp->bitmap;
    gcMask |= GCClipMask;
  }

  // This is technically a shared GC, but we're going to set/change the clip
  // origin anyways before we draw the bitmap.  This relies on the fact that
  // no other client will be allocated this GC with the GCClipMask set to
  // this particular bitmap.
  GC newGC = Tk_GetGC(graphPtr->tkwin, gcMask, &gcValues);
  if (gc)
    Tk_FreeGC(graphPtr->display, gc);
  gc = newGC;

  // Create the background GC containing the fill color
  if (opp->fillColor) {
    gcValues.foreground = opp->fillColor->pixel;
    newGC = Tk_GetGC(graphPtr->tkwin, gcMask, &gcValues);
    if (fillGC)
      Tk_FreeGC(graphPtr->display, fillGC);
    fillGC = newGC;
  }

  return TCL_OK;
}

void BitmapMarker::draw(Drawable drawable)
{
  Graph* graphPtr = obj.graphPtr;
  BitmapMarkerOptions* opp = (BitmapMarkerOptions*)ops;

  if ((opp->bitmap == None) || (width < 1) || (height < 1))
    return;

  if (opp->fillColor == NULL) {
    XSetClipMask(graphPtr->display, gc, opp->bitmap);
    XSetClipOrigin(graphPtr->display, gc, anchorPt.x, anchorPt.y);
  }
  else {
    XSetClipMask(graphPtr->display, gc, None);
    XSetClipOrigin(graphPtr->display, gc, 0, 0);
  }
  XCopyPlane(graphPtr->display, opp->bitmap, drawable, gc, 0, 0,
	     width, height, anchorPt.x, anchorPt.y, 1);
}

void BitmapMarker::map()
{
  Graph* graphPtr = obj.graphPtr;
  BitmapMarkerOptions* opp = (BitmapMarkerOptions*)ops;

  if (opp->bitmap == None)
    return;
 
  if (!opp->worldPts || (opp->worldPts->num < 1))
    return;

  int lwidth;
  int lheight;
  Tk_SizeOfBitmap(graphPtr->display, opp->bitmap, &lwidth, &lheight);

  Point2d lanchorPt = mapPoint(opp->worldPts->points, &opp->axes);
  lanchorPt = 
    Blt_AnchorPoint(lanchorPt.x, lanchorPt.y, lwidth, lheight, opp->anchor);
  lanchorPt.x += opp->xOffset;
  lanchorPt.y += opp->yOffset;

  Region2d extents;
  extents.left = lanchorPt.x;
  extents.top = lanchorPt.y;
  extents.right = lanchorPt.x + lwidth - 1;
  extents.bottom = lanchorPt.y + lheight - 1;
  clipped = boxesDontOverlap(graphPtr, &extents);

  if (clipped)
    return;

  width = lwidth;
  height = lheight;
  anchorPt = lanchorPt;

  // Compute a polygon to represent the background area of the bitmap.
  // This is needed for print a background in PostScript.
  double rotWidth, rotHeight;
  Point2d polygon[5];
  Blt_GetBoundingBox(width, height, 0, &rotWidth, &rotHeight, polygon);
	
  // Adjust each point of the polygon. Both scale it to the new size and
  // translate it to the actual screen position of the bitmap.
  double tx = extents.left + width * 0.5;
  double ty = extents.top + height * 0.5;
  for (int ii=0; ii<4; ii++) {
    polygon[ii].x = (polygon[ii].x) + tx;
    polygon[ii].y = (polygon[ii].y) + ty;
  }
  Blt_GraphExtents(graphPtr, &extents);
  int nn = Blt_PolyRectClip(&extents, polygon, 4, outline); 
  if (nn < 3) { 
    memcpy(&outline, polygon, sizeof(Point2d) * 4);
    nOutlinePts = 4;
  }
  else
    nOutlinePts = nn;
}

int BitmapMarker::pointIn(Point2d *samplePtr)
{
  BitmapMarkerOptions* opp = (BitmapMarkerOptions*)ops;

  if (opp->bitmap == None)
    return 0;

  return ((samplePtr->x >= anchorPt.x) && 
	  (samplePtr->x < (anchorPt.x + width)) &&
	  (samplePtr->y >= anchorPt.y) && 
	  (samplePtr->y < (anchorPt.y + height)));
}

int BitmapMarker::regionIn(Region2d *extsPtr, int enclosed)
{
  if (enclosed) {
    return ((anchorPt.x >= extsPtr->left) &&
	    (anchorPt.y >= extsPtr->top) && 
	    ((anchorPt.x + width) <= extsPtr->right) &&
	    ((anchorPt.y + height) <= extsPtr->bottom));
  }

  return !((anchorPt.x >= extsPtr->right) ||
	   (anchorPt.y >= extsPtr->bottom) ||
	   ((anchorPt.x + width) <= extsPtr->left) ||
	   ((anchorPt.y + height) <= extsPtr->top));
}

void BitmapMarker::postscript(Blt_Ps ps)
{
  Graph* graphPtr = obj.graphPtr;
  BitmapMarkerOptions* opp = (BitmapMarkerOptions*)ops;

  if ((opp->bitmap == None) || (width < 1) || (height < 1))
    return;

  if (opp->fillColor) {
    Blt_Ps_XSetBackground(ps, opp->fillColor);
    Blt_Ps_XFillPolygon(ps, outline, 4);
  }
  Blt_Ps_XSetForeground(ps, opp->outlineColor);

  Blt_Ps_Format(ps, "  gsave\n    %g %g translate\n    %d %d scale\n", 
		anchorPt.x, anchorPt.y + height, width, -height);
  Blt_Ps_Format(ps, "    %d %d true [%d 0 0 %d 0 %d] {",
		width, height, width, -height, height);
  Blt_Ps_XSetBitmapData(ps, graphPtr->display, opp->bitmap, width, height);
  Blt_Ps_VarAppend(ps, "    } imagemask\n", "grestore\n", (char*)NULL);
}