summaryrefslogtreecommitdiffstats
path: root/generic/tkbltGrMarkerOp.C
blob: 20933abc637969f061302a1ad1077f71095e229b (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
 * 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 <string.h>

#include "tkbltGrBind.h"
#include "tkbltGraph.h"
#include "tkbltGrElem.h"
#include "tkbltGrMarkerOp.h"
#include "tkbltGrMarker.h"
#include "tkbltGrMarkerLine.h"
#include "tkbltGrMarkerPolygon.h"
#include "tkbltGrMarkerText.h"

using namespace Blt;

static int GetMarkerFromObj(Tcl_Interp* interp, Graph* graphPtr, 
			    Tcl_Obj* objPtr, Marker** markerPtrPtr);

#define FIND_ENCLOSED	 (1<<0)
#define FIND_OVERLAPPING (1<<1)

static int MarkerObjConfigure( Graph* graphPtr,Marker* markerPtr,
			       Tcl_Interp* interp, 
			       int objc, Tcl_Obj* const objv[])
{
  Tk_SavedOptions savedOptions;
  int mask =0;
  int error;
  Tcl_Obj* errorResult;

  for (error=0; error<=1; error++) {
    if (!error) {
      if (Tk_SetOptions(interp, (char*)markerPtr->ops(), 
			markerPtr->optionTable(), 
			objc, objv, graphPtr->tkwin_, &savedOptions, &mask)
	  != TCL_OK)
	continue;
    }
    else {
      errorResult = Tcl_GetObjResult(interp);
      Tcl_IncrRefCount(errorResult);
      Tk_RestoreSavedOptions(&savedOptions);
    }

    markerPtr->flags |= MAP_ITEM;
    if (markerPtr->configure() != TCL_OK)
      return TCL_ERROR;

    MarkerOptions* ops = (MarkerOptions*)markerPtr->ops();
    if (ops->drawUnder)
      graphPtr->flags |= CACHE;
    graphPtr->flags |= mask;
    graphPtr->eventuallyRedraw();

    break; 
  }

  if (!error) {
    Tk_FreeSavedOptions(&savedOptions);
    return TCL_OK;
  }
  else {
    Tcl_SetObjResult(interp, errorResult);
    Tcl_DecrRefCount(errorResult);
    return TCL_ERROR;
  }
}

static int CreateMarker(Graph* graphPtr, Tcl_Interp* interp, 
			int objc, Tcl_Obj* const objv[])
{
  int offset = 5;
  const char* name =NULL;
  ostringstream str;
  if (objc == 4) {
    offset = 4;
    str << "marker" << graphPtr->nextMarkerId_++ << ends;
    name = dupstr(str.str().c_str());
  }
  else {
    name = dupstr(Tcl_GetString(objv[4]));
    if (name[0] == '-') {
      delete [] name;
      offset = 4;
      str << "marker" << graphPtr->nextMarkerId_++ << ends;
      name = dupstr(str.str().c_str());
    }
  }

  int isNew;
  Tcl_HashEntry* hPtr =
    Tcl_CreateHashEntry(&graphPtr->markers_.table, name, &isNew);
  if (!isNew) {
    Tcl_AppendResult(graphPtr->interp_, "marker \"", name,
		     "\" already exists in \"", Tcl_GetString(objv[0]),
		     "\"", NULL);
    return TCL_ERROR;
  }

  const char* type = Tcl_GetString(objv[3]);
  Marker* markerPtr;
  if (!strcmp(type, "line"))
    markerPtr = new LineMarker(graphPtr, name, hPtr);
  else if (!strcmp(type, "polygon"))
    markerPtr = new PolygonMarker(graphPtr, name, hPtr);
  else if (!strcmp(type, "text"))
    markerPtr = new TextMarker(graphPtr, name, hPtr);
  else {
    Tcl_AppendResult(interp, "unknown marker type ", type, NULL);
    return TCL_ERROR;
  }

  Tcl_SetHashValue(hPtr, markerPtr);

  if ((Tk_InitOptions(graphPtr->interp_, (char*)markerPtr->ops(), markerPtr->optionTable(), graphPtr->tkwin_) != TCL_OK) || (MarkerObjConfigure(graphPtr, markerPtr, interp, objc-offset, objv+offset) != TCL_OK)) {
    delete markerPtr;
    return TCL_ERROR;
  }

  // Unlike elements, new markers are drawn on top of old markers
  markerPtr->link = graphPtr->markers_.displayList->prepend(markerPtr);

  Tcl_SetStringObj(Tcl_GetObjResult(interp), name, -1);

  delete [] name;
  return TCL_OK;
}

static int CgetOp(ClientData clientData, Tcl_Interp* interp, 
		  int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  if (objc<4)
    return TCL_ERROR;

  Marker* markerPtr;
  if (GetMarkerFromObj(interp, graphPtr, objv[3], &markerPtr) != TCL_OK)
    return TCL_ERROR;

  Tcl_Obj* objPtr = Tk_GetOptionValue(interp, 
				      (char*)markerPtr->ops(), 
				      markerPtr->optionTable(),
				      objv[4], graphPtr->tkwin_);
  if (objPtr == NULL)
    return TCL_ERROR;
  else
    Tcl_SetObjResult(interp, objPtr);
  return TCL_OK;
}

static int ConfigureOp(ClientData clientData, Tcl_Interp* interp, 
		       int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  if (objc<4)
    return TCL_ERROR;

  Marker* markerPtr;
  if (GetMarkerFromObj(interp, graphPtr, objv[3], &markerPtr) != TCL_OK)
    return TCL_ERROR;

  if (objc <= 5) {
    Tcl_Obj* objPtr = Tk_GetOptionInfo(interp, (char*)markerPtr->ops(), 
				       markerPtr->optionTable(), 
				       (objc == 5) ? objv[4] : NULL, 
				       graphPtr->tkwin_);
    if (objPtr == NULL)
      return TCL_ERROR;
    else
      Tcl_SetObjResult(interp, objPtr);
    return TCL_OK;
  } 
  else
    return MarkerObjConfigure(graphPtr, markerPtr, interp, objc-4, objv+4);
}

static int BindOp(ClientData clientData, Tcl_Interp* interp, 
		  int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  if (objc == 3) {
    Tcl_Obj *listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **)NULL);
    Tcl_HashSearch iter;
    for (Tcl_HashEntry* hp = 
	   Tcl_FirstHashEntry(&graphPtr->markers_.tagTable, &iter); 
	 hp; hp = Tcl_NextHashEntry(&iter)) {

      const char* tag = 
	(const char*)Tcl_GetHashKey(&graphPtr->markers_.tagTable, hp);
      Tcl_Obj* objPtr = Tcl_NewStringObj(tag, -1);
      Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
    }
    Tcl_SetObjResult(interp, listObjPtr);
    return TCL_OK;
  }

  return graphPtr->bindTable_->configure(graphPtr->markerTag(Tcl_GetString(objv[3])), objc - 4, objv + 4);
}

static int CreateOp(ClientData clientData, Tcl_Interp* interp,
		    int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  if (CreateMarker(graphPtr, interp, objc, objv) != TCL_OK)
    return TCL_ERROR;
  // set in CreateMarker
  // Tcl_SetObjResult(interp, objv[3]);

  graphPtr->flags |= CACHE;
  graphPtr->eventuallyRedraw();

  return TCL_OK;
}

static int DeleteOp(ClientData clientData, Tcl_Interp* interp, 
		    int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  for (int ii=3; ii<objc; ii++) {
    Marker* markerPtr;
    if (GetMarkerFromObj(NULL, graphPtr, objv[ii], &markerPtr) != TCL_OK) {
      Tcl_AppendResult(interp, "can't find marker \"", 
		       Tcl_GetString(objv[ii]), "\" in \"", 
		       Tk_PathName(graphPtr->tkwin_), "\"", NULL);
      return TCL_ERROR;
    }
    delete markerPtr;
  }

  graphPtr->flags |= CACHE;
  graphPtr->eventuallyRedraw();

  return TCL_OK;
}

static int ExistsOp(ClientData clientData, Tcl_Interp* interp, 
		    int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  if (objc<4)
    return TCL_ERROR;

  Tcl_HashEntry* hPtr =
    Tcl_FindHashEntry(&graphPtr->markers_.table, Tcl_GetString(objv[3]));
  Tcl_SetBooleanObj(Tcl_GetObjResult(interp), (hPtr != NULL));

  return TCL_OK;
}

static int FindOp(ClientData clientData, Tcl_Interp* interp, 
		  int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  if (objc<4)
    return TCL_ERROR;

  const char* string = Tcl_GetString(objv[3]);
  int mode;
  if (strcmp(string, "enclosed") == 0)
    mode = FIND_ENCLOSED;
  else if (strcmp(string, "overlapping") == 0)
    mode = FIND_OVERLAPPING;
  else {
    Tcl_AppendResult(interp, "bad search type \"", string, 
		     ": should be \"enclosed\", or \"overlapping\"",
		     NULL);
    return TCL_ERROR;
  }

  int left, right, top, bottom;
  if ((Tcl_GetIntFromObj(interp, objv[4], &left) != TCL_OK) ||
      (Tcl_GetIntFromObj(interp, objv[5], &top) != TCL_OK) ||
      (Tcl_GetIntFromObj(interp, objv[6], &right) != TCL_OK) ||
      (Tcl_GetIntFromObj(interp, objv[7], &bottom) != TCL_OK)) {
    return TCL_ERROR;
  }

  Region2d extents;
  if (left < right) {
    extents.left = (double)left;
    extents.right = (double)right;
  }
  else {
    extents.left = (double)right;
    extents.right = (double)left;
  }
  if (top < bottom) {
    extents.top = (double)top;
    extents.bottom = (double)bottom;
  }
  else {
    extents.top = (double)bottom;
    extents.bottom = (double)top;
  }

  int enclosed = (mode == FIND_ENCLOSED);
  for (ChainLink* link = Chain_FirstLink(graphPtr->markers_.displayList);
       link; link = Chain_NextLink(link)) {
    Marker* markerPtr = (Marker*)Chain_GetValue(link);
    MarkerOptions* ops = (MarkerOptions*)markerPtr->ops();
    if (ops->hide)
      continue;

    if (graphPtr->isElementHidden(markerPtr))
      continue;

    if (markerPtr->regionIn(&extents, enclosed)) {
      Tcl_Obj* objPtr = Tcl_GetObjResult(interp);
      Tcl_SetStringObj(objPtr, markerPtr->name_, -1);
      return TCL_OK;
    }
  }

  Tcl_SetStringObj(Tcl_GetObjResult(interp), "", -1);
  return TCL_OK;
}

static int NamesOp(ClientData clientData, Tcl_Interp* interp, 
		   int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  Tcl_Obj* listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **)NULL);
  if (objc == 3) {
    for (ChainLink* link=Chain_FirstLink(graphPtr->markers_.displayList); 
	 link; link = Chain_NextLink(link)) {
      Marker* markerPtr = (Marker*)Chain_GetValue(link);
      Tcl_ListObjAppendElement(interp, listObjPtr,
			       Tcl_NewStringObj(markerPtr->name_, -1));
    }
  } 
  else {
    for (ChainLink* link=Chain_FirstLink(graphPtr->markers_.displayList); 
	 link; link = Chain_NextLink(link)) {
      Marker* markerPtr = (Marker*)Chain_GetValue(link);
      for (int ii = 3; ii<objc; ii++) {
	const char* pattern = (const char*)Tcl_GetString(objv[ii]);
	if (Tcl_StringMatch(markerPtr->name_, pattern)) {
	  Tcl_ListObjAppendElement(interp, listObjPtr,
				   Tcl_NewStringObj(markerPtr->name_, -1));
	  break;
	}
      }
    }
  }

  Tcl_SetObjResult(interp, listObjPtr);
  return TCL_OK;
}

static int RelinkOp(ClientData clientData, Tcl_Interp* interp, 
		    int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  if (objc<4)
    return TCL_ERROR;

  Marker* markerPtr;
  if (GetMarkerFromObj(interp, graphPtr, objv[3], &markerPtr) != TCL_OK)
    return TCL_ERROR;

  Marker* placePtr =NULL;
  if (objc == 5)
    if (GetMarkerFromObj(interp, graphPtr, objv[4], &placePtr) != TCL_OK)
      return TCL_ERROR;

  ChainLink* link = markerPtr->link;
  graphPtr->markers_.displayList->unlinkLink(markerPtr->link);

  ChainLink* place = placePtr ? placePtr->link : NULL;

  const char* string = Tcl_GetString(objv[2]);
  if (string[0] == 'l')
    graphPtr->markers_.displayList->linkAfter(link, place);
  else
    graphPtr->markers_.displayList->linkBefore(link, place);

  graphPtr->flags |= CACHE;
  graphPtr->eventuallyRedraw();

  return TCL_OK;
}

static int TypeOp(ClientData clientData, Tcl_Interp* interp, 
		  int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  if (objc<4)
    return TCL_ERROR;

  Marker* markerPtr;
  if (GetMarkerFromObj(interp, graphPtr, objv[3], &markerPtr) != TCL_OK)
    return TCL_ERROR;

  Tcl_SetStringObj(Tcl_GetObjResult(interp), markerPtr->typeName(), -1);
  return TCL_OK;
}

const Ensemble Blt::markerEnsemble[] = {
  {"bind",      BindOp, 0},
  {"cget",      CgetOp, 0},
  {"configure", ConfigureOp, 0},
  {"create",    CreateOp, 0},
  {"delete",    DeleteOp, 0},
  {"exists",    ExistsOp, 0},
  {"find",      FindOp, 0},
  {"lower",     RelinkOp, 0},
  {"names",     NamesOp, 0},
  {"raise",     RelinkOp, 0},
  {"type",      TypeOp, 0},
  { 0,0,0 }
};

// Support

static int GetMarkerFromObj(Tcl_Interp* interp, Graph* graphPtr, 
			    Tcl_Obj *objPtr, Marker** markerPtrPtr)
{
  const char* string = Tcl_GetString(objPtr);
  Tcl_HashEntry* hPtr = Tcl_FindHashEntry(&graphPtr->markers_.table, string);
  if (hPtr) {
    *markerPtrPtr = (Marker*)Tcl_GetHashValue(hPtr);
    return TCL_OK;
  }
  if (interp) {
    Tcl_AppendResult(interp, "can't find marker \"", string, 
		     "\" in \"", Tk_PathName(graphPtr->tkwin_), NULL);
  }

  return TCL_ERROR;
}