summaryrefslogtreecommitdiffstats
path: root/src/tkbltGrLegdOp.C
blob: bdd1bfdc17e66dc71106fbc156b5898ac3f455c5 (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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
 * 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 <tk.h>
#include <tkInt.h>

#include "bltGrBind.h"
#include "bltGraph.h"
#include "bltGrLegd.h"
#include "bltGrLegdOp.h"
#include "bltGrElem.h"

using namespace Blt;

static Tk_LostSelProc LostSelectionProc;

static int LegendObjConfigure(Graph* graphPtr, Tcl_Interp* interp,
			      int objc, Tcl_Obj* const objv[])
{
  Legend* legendPtr = graphPtr->legend_;
  Tk_SavedOptions savedOptions;
  int mask =0;
  int error;
  Tcl_Obj* errorResult;

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

    if (legendPtr->configure() != TCL_OK)
      return TCL_ERROR;
    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 CgetOp(ClientData clientData, Tcl_Interp* interp, 
		  int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  if (objc != 4) {
    Tcl_WrongNumArgs(interp, 2, objv, "cget option");
    return TCL_ERROR;
  }

  Legend* legendPtr = graphPtr->legend_;
  Tcl_Obj* objPtr = Tk_GetOptionValue(interp, 
				      (char*)legendPtr->ops(), 
				      legendPtr->optionTable(),
				      objv[3], 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;
  Legend* legendPtr = graphPtr->legend_;
  if (objc <= 4) {
    Tcl_Obj* objPtr = Tk_GetOptionInfo(interp, (char*)legendPtr->ops(), 
				       legendPtr->optionTable(), 
				       (objc == 4) ? objv[3] : NULL, 
				       graphPtr->tkwin_);
    if (objPtr == NULL)
      return TCL_ERROR;
    else
      Tcl_SetObjResult(interp, objPtr);
    return TCL_OK;
  } 
  else
    return LegendObjConfigure(graphPtr, interp, objc-3, objv+3);
}

static int ActivateOp(ClientData clientData, Tcl_Interp* interp, 
		      int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  Legend* legendPtr = graphPtr->legend_;
  LegendOptions* ops = (LegendOptions*)legendPtr->ops();

  const char *string = Tcl_GetString(objv[2]);
  int active = (string[0] == 'a') ? 1 : 0;
  int redraw = 0;
  for (int ii=3; ii<objc; ii++) {
    
    const char* pattern = Tcl_GetString(objv[ii]);
    for (ChainLink* link = Chain_FirstLink(graphPtr->elements_.displayList);
	 link; link = Chain_NextLink(link)) {
      Element* elemPtr = (Element*)Chain_GetValue(link);
      if (Tcl_StringMatch(elemPtr->name_, pattern)) {
	if (active) {
	  if (!elemPtr->labelActive_) {
	    elemPtr->labelActive_ =1;
	    redraw = 1;
	  }
	}
	else {
	  if (elemPtr->labelActive_) {
	    elemPtr->labelActive_ =0;
	    redraw = 1;
	  }
	}
      }
    }
  }

  if (redraw && !ops->hide) {
    graphPtr->flags |= LAYOUT;
    graphPtr->eventuallyRedraw();
  }

  // List active elements in stacking order
  Tcl_Obj *listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **)NULL);
  for (ChainLink* link = Chain_FirstLink(graphPtr->elements_.displayList);
       link; link = Chain_NextLink(link)) {
    Element* elemPtr = (Element*)Chain_GetValue(link);
    if (elemPtr->labelActive_) {
      Tcl_Obj *objPtr = Tcl_NewStringObj(elemPtr->name_, -1);
      Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
    }
  }
  Tcl_SetObjResult(interp, listObjPtr);

  return TCL_OK;
}

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* hPtr=Tcl_FirstHashEntry(&graphPtr->elements_.tagTable, &iter); hPtr; hPtr = Tcl_NextHashEntry(&iter)) {
      char* tagName = 
	(char*)Tcl_GetHashKey(&graphPtr->elements_.tagTable, hPtr);
      Tcl_Obj *objPtr = Tcl_NewStringObj(tagName, -1);
      Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
    }

    Tcl_SetObjResult(interp, listObjPtr);
    return TCL_OK;
  }

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

static int CurselectionOp(ClientData clientData, Tcl_Interp* interp, 
			  int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  Legend* legendPtr = graphPtr->legend_;
  Tcl_Obj *listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **)NULL);
  if (legendPtr->flags & SELECT_SORTED) {
    for (ChainLink* link = Chain_FirstLink(legendPtr->selected_); link;
	 link = Chain_NextLink(link)) {
      Element* elemPtr = (Element*)Chain_GetValue(link);
      Tcl_Obj *objPtr = Tcl_NewStringObj(elemPtr->name_, -1);
      Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
    }
  }
  else {
    // List of selected entries is in stacking order
    for (ChainLink* link = Chain_FirstLink(graphPtr->elements_.displayList);
	 link; link = Chain_NextLink(link)) {
      Element* elemPtr = (Element*)Chain_GetValue(link);

      if (legendPtr->entryIsSelected(elemPtr)) {
	Tcl_Obj *objPtr = Tcl_NewStringObj(elemPtr->name_, -1);
	Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
      }
    }
  }
  Tcl_SetObjResult(interp, listObjPtr);
  return TCL_OK;
}

static int FocusOp(ClientData clientData, Tcl_Interp* interp, 
		   int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  Legend* legendPtr = graphPtr->legend_;

  legendPtr->focusPtr_ = NULL;
  if (objc == 4) {
    Element* elemPtr;
    if (legendPtr->getElementFromObj(objv[3], &elemPtr) != TCL_OK)
      return TCL_ERROR;

    if (elemPtr) {
      legendPtr->focusPtr_ = elemPtr;

      legendPtr->bindTable_->focusItem_ = (ClientData)elemPtr;
      legendPtr->bindTable_->focusContext_ = elemPtr->classId();
    }
  }

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

  if (legendPtr->focusPtr_)
    Tcl_SetStringObj(Tcl_GetObjResult(interp),legendPtr->focusPtr_->name_,-1);

  return TCL_OK;
}

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

  Legend* legendPtr = graphPtr->legend_;
  LegendOptions* ops = (LegendOptions*)legendPtr->ops();

  if (((ops->hide) == 0) && (legendPtr->nEntries_ > 0)) {
    Element* elemPtr;

    if (legendPtr->getElementFromObj(objv[3], &elemPtr) != TCL_OK)
      return TCL_ERROR;

    if (elemPtr)
      Tcl_SetStringObj(Tcl_GetObjResult(interp), elemPtr->name_, -1);
  }
  return TCL_OK;
}

const Ensemble Blt::legendEnsemble[] = {
  {"activate",     ActivateOp, 0},
  {"bind",         BindOp, 0},
  {"cget",         CgetOp, 0},
  {"configure",    ConfigureOp, 0},
  {"curselection", CurselectionOp, 0},
  {"deactivate",   ActivateOp, 0},
  {"focus",        FocusOp, 0},
  {"get",          GetOp, 0},
  {"selection",    0, selectionEnsemble},
  { 0,0,0 }
};

// Selection Ops

static int SelectionAnchorOp(ClientData clientData, Tcl_Interp* interp, 
			     int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  Legend* legendPtr = graphPtr->legend_;
  Element* elemPtr;

  if (legendPtr->getElementFromObj(objv[4], &elemPtr) != TCL_OK)
    return TCL_ERROR;

  // Set both the anchor and the mark. Indicates that a single entry
  // is selected
  legendPtr->selAnchorPtr_ = elemPtr;
  legendPtr->selMarkPtr_ = NULL;
  if (elemPtr)
    Tcl_SetStringObj(Tcl_GetObjResult(interp), elemPtr->name_, -1);

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

  return TCL_OK;
}

static int SelectionClearallOp(ClientData clientData, Tcl_Interp* interp, 
			       int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  Legend* legendPtr = graphPtr->legend_;
  legendPtr->clearSelection();

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

  return TCL_OK;
}

static int SelectionIncludesOp(ClientData clientData, Tcl_Interp* interp, 
			       int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  Legend* legendPtr = graphPtr->legend_;
  Element* elemPtr;
  if (legendPtr->getElementFromObj(objv[4], &elemPtr) != TCL_OK)
    return TCL_ERROR;

  int boo = legendPtr->entryIsSelected(elemPtr);
  Tcl_SetBooleanObj(Tcl_GetObjResult(interp), boo);
  return TCL_OK;
}

static int SelectionMarkOp(ClientData clientData, Tcl_Interp* interp, 
			   int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  Legend* legendPtr = graphPtr->legend_;
  LegendOptions* ops = (LegendOptions*)legendPtr->ops();
  Element* elemPtr;

  if (legendPtr->getElementFromObj(objv[4], &elemPtr) != TCL_OK)
    return TCL_ERROR;

  if (legendPtr->selAnchorPtr_ == NULL) {
    Tcl_AppendResult(interp, "selection anchor must be set first", NULL);
    return TCL_ERROR;
  }

  if (legendPtr->selMarkPtr_ != elemPtr) {
    // Deselect entry from the list all the way back to the anchor
    ChainLink *link, *next;
    for (link = Chain_LastLink(legendPtr->selected_); link; link = next) {
      next = Chain_PrevLink(link);
      Element *selectPtr = (Element*)Chain_GetValue(link);
      if (selectPtr == legendPtr->selAnchorPtr_)
	break;

      legendPtr->deselectElement(selectPtr);
    }

    legendPtr->flags &= ~SELECT_TOGGLE;
    legendPtr->flags |= SELECT_SET;
    legendPtr->selectRange(legendPtr->selAnchorPtr_, elemPtr);
    Tcl_SetStringObj(Tcl_GetObjResult(interp), elemPtr->name_, -1);
    legendPtr->selMarkPtr_ = elemPtr;

    if (ops->selectCmd)
      legendPtr->eventuallyInvokeSelectCmd();

    graphPtr->flags |= CACHE;
    graphPtr->eventuallyRedraw();
  }
  return TCL_OK;
}

static int SelectionPresentOp(ClientData clientData, Tcl_Interp* interp, 
			      int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  Legend* legendPtr = graphPtr->legend_;
  int boo = (Chain_GetLength(legendPtr->selected_) > 0);
  Tcl_SetBooleanObj(Tcl_GetObjResult(interp), boo);
  return TCL_OK;
}

static int SelectionSetOp(ClientData clientData, Tcl_Interp* interp, 
			  int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  Legend* legendPtr = graphPtr->legend_;
  LegendOptions* ops = (LegendOptions*)legendPtr->ops();

  legendPtr->flags &= ~SELECT_TOGGLE;
  const char* string = Tcl_GetString(objv[3]);
  switch (string[0]) {
  case 's':
    legendPtr->flags |= SELECT_SET;
    break;
  case 'c':
    legendPtr->flags |= SELECT_CLEAR;
    break;
  case 't':
    legendPtr->flags |= SELECT_TOGGLE;
    break;
  }

  Element *firstPtr;
  if (legendPtr->getElementFromObj(objv[4], &firstPtr) != TCL_OK)
    return TCL_ERROR;
  ElementOptions* eops = (ElementOptions*)firstPtr->ops();

  if ((eops->hide) && ((legendPtr->flags & SELECT_CLEAR)==0)) {
    Tcl_AppendResult(interp, "can't select hidden node \"", 
		     Tcl_GetString(objv[4]), "\"", (char *)NULL);
    return TCL_ERROR;
  }

  Element* lastPtr = firstPtr;
  if (objc > 5) {
    if (legendPtr->getElementFromObj(objv[5], &lastPtr) != TCL_OK)
      return TCL_ERROR;
    ElementOptions* eops = (ElementOptions*)firstPtr->ops();

    if (eops->hide && ((legendPtr->flags & SELECT_CLEAR) == 0)) {
      Tcl_AppendResult(interp, "can't select hidden node \"", 
		       Tcl_GetString(objv[5]), "\"", (char *)NULL);
      return TCL_ERROR;
    }
  }

  if (firstPtr == lastPtr)
    legendPtr->selectEntry(firstPtr);
  else
    legendPtr->selectRange(firstPtr, lastPtr);

  // Set both the anchor and the mark. Indicates that a single entry is
  // selected
  if (legendPtr->selAnchorPtr_ == NULL)
    legendPtr->selAnchorPtr_ = firstPtr;

  if (ops->exportSelection)
    Tk_OwnSelection(graphPtr->tkwin_, XA_PRIMARY, LostSelectionProc, legendPtr);

  if (ops->selectCmd)
    legendPtr->eventuallyInvokeSelectCmd();

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

  return TCL_OK;
}

const Ensemble Blt::selectionEnsemble[] = {
  {"anchor",   SelectionAnchorOp, 0},
  {"clear",    SelectionSetOp, 0},
  {"clearall", SelectionClearallOp, 0},
  {"includes", SelectionIncludesOp, 0},
  {"mark",     SelectionMarkOp, 0},
  {"present",  SelectionPresentOp, 0},
  {"set",      SelectionSetOp, 0},
  {"toggle",   SelectionSetOp, 0},
  { 0,0,0 }
};

// Support

static void LostSelectionProc(ClientData clientData)
{
  Legend* legendPtr = (Legend*)clientData;
  LegendOptions* ops = (LegendOptions*)legendPtr->ops();
  Graph* graphPtr = legendPtr->graphPtr_;

  if (ops->exportSelection)
    legendPtr->clearSelection();

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