summaryrefslogtreecommitdiffstats
path: root/src/bltGrXAxisOp.C
blob: 4ec5d6c863a4106fd434f256b68cedbb16c130ff (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
/*
 * 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.
 */

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

#include "bltGrAxis.h"
#include "bltGrAxisOp.h"

extern int AxisCgetOp(Tcl_Interp* interp, Axis* axisPtr, 
		      int objc, Tcl_Obj* const objv[]);
extern int AxisConfigureOp(Tcl_Interp* interp, Axis* axisPtr, 
			   int objc, Tcl_Obj* const objv[]);
extern int AxisActivateOp(Tcl_Interp* interp, Axis* axisPtr, 
			  int objc, Tcl_Obj* const objv[]);
extern int AxisInvTransformOp(Tcl_Interp* interp, Axis* axisPtr, 
			      int objc, Tcl_Obj* const objv[]);
extern int AxisLimitsOp(Tcl_Interp* interp, Axis* axisPtr, 
			int objc, Tcl_Obj* const objv[]);
extern int AxisMarginOp(Tcl_Interp* interp, Axis* axisPtr, 
			int objc, Tcl_Obj* const objv[]);
extern int AxisTransformOp(Tcl_Interp* interp, Axis* axisPtr, 
			   int objc, Tcl_Obj* const objv[]);
extern int AxisTypeOp(Tcl_Interp* interp, Axis* axisPtr, 
		      int objc, Tcl_Obj* const objv[]);
extern int AxisViewOp(Tcl_Interp* interp, Axis* axisPtr, 
		      int objc, Tcl_Obj* const objv[]);

static int lastMargin;


int Blt_CreateAxes(Graph* graphPtr)
{
  for (int ii=0; ii<4; ii++) {
    int isNew;
    Tcl_HashEntry* hPtr = 
      Tcl_CreateHashEntry(&graphPtr->axes.table, axisNames[ii].name, &isNew);
    Blt_Chain chain = Blt_Chain_Create();

    Axis* axisPtr = new Axis(graphPtr, axisNames[ii].name, ii);
    if (!axisPtr)
      return TCL_ERROR;
    AxisOptions* ops = (AxisOptions*)axisPtr->ops();

    axisPtr->hashPtr_ = hPtr;
    Tcl_SetHashValue(hPtr, axisPtr);

    axisPtr->refCount_ = 1;
    axisPtr->margin_ = ii;
    axisPtr->use_ =1;
    
    axisPtr->setClass(!(ii&1) ? CID_AXIS_X : CID_AXIS_Y);

    if (Tk_InitOptions(graphPtr->interp, (char*)axisPtr->ops(), 
		       axisPtr->optionTable(), graphPtr->tkwin) != TCL_OK)
      return TCL_ERROR;

    if (axisPtr->configure() != TCL_OK)
      return TCL_ERROR;

    if ((axisPtr->margin_ == MARGIN_RIGHT) || (axisPtr->margin_ == MARGIN_TOP))
      ops->hide = 1;

    graphPtr->axisChain[ii] = chain;
    axisPtr->link = Blt_Chain_Append(chain, axisPtr);
    axisPtr->chain = chain;
  }
  return TCL_OK;
}

// Ops

static int BindOp(Tcl_Interp* interp, Axis* axisPtr, 
		  int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = axisPtr->graphPtr_;

  return Blt_ConfigureBindingsFromObj(interp, graphPtr->bindTable, Blt_MakeAxisTag(graphPtr, axisPtr->name()), objc-3, objv+3);
}
          
static int UseOp(Tcl_Interp* interp, Axis* axisPtr, 
		 int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph *)axisPtr;

  Blt_Chain chain = graphPtr->margins[lastMargin].axes;
  if (objc == 3) {
    Tcl_Obj *listObjPtr;

    listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **)NULL);
    for (Blt_ChainLink link = Blt_Chain_FirstLink(chain); link != NULL;
	 link = Blt_Chain_NextLink(link)) {
      Axis* axisPtr = (Axis*)Blt_Chain_GetValue(link);
      Tcl_ListObjAppendElement(interp, listObjPtr,
			       Tcl_NewStringObj(axisPtr->name(), -1));
    }
    Tcl_SetObjResult(interp, listObjPtr);
    return TCL_OK;
  }
  ClassId classId;
  if ((lastMargin == MARGIN_BOTTOM) || (lastMargin == MARGIN_TOP))
    classId = (graphPtr->inverted) ? CID_AXIS_Y : CID_AXIS_X;
  else
    classId = (graphPtr->inverted) ? CID_AXIS_X : CID_AXIS_Y;

  int axisObjc;  
  Tcl_Obj **axisObjv;
  if (Tcl_ListObjGetElements(interp, objv[3], &axisObjc, &axisObjv) 
      != TCL_OK) {
    return TCL_ERROR;
  }
  for (Blt_ChainLink link = Blt_Chain_FirstLink(chain); link!= NULL; 
       link = Blt_Chain_NextLink(link)) {
    Axis* axisPtr;

    axisPtr = (Axis*)Blt_Chain_GetValue(link);
    axisPtr->link = NULL;
    axisPtr->use_ =0;
    /* Clear the axis type if it's not currently used.*/
    if (axisPtr->refCount_ == 0)
      axisPtr->setClass(CID_NONE);
  }
  Blt_Chain_Reset(chain);
  for (int i=0; i<axisObjc; i++) {
    Axis* axisPtr;
    if (GetAxisFromObj(interp, graphPtr, axisObjv[i], &axisPtr) != TCL_OK)
      return TCL_ERROR;

    if (axisPtr->classId() == CID_NONE)
      axisPtr->setClass(classId);
    else if (axisPtr->classId() != classId) {
      Tcl_AppendResult(interp, "wrong type axis \"", 
		       axisPtr->name(), "\": can't use ", 
		       axisPtr->className(), " type axis.", NULL); 
      return TCL_ERROR;
    }
    if (axisPtr->link) {
      /* Move the axis from the old margin's "use" list to the new. */
      Blt_Chain_UnlinkLink(axisPtr->chain, axisPtr->link);
      Blt_Chain_AppendLink(chain, axisPtr->link);
    } else {
      axisPtr->link = Blt_Chain_Append(chain, axisPtr);
    }
    axisPtr->chain = chain;
    axisPtr->use_ =1;
  }
  graphPtr->flags |= (GET_AXIS_GEOMETRY | LAYOUT_NEEDED | RESET_AXES);
  /* When any axis changes, we need to layout the entire graph.  */
  graphPtr->flags |= (MAP_WORLD | REDRAW_WORLD);
  Blt_EventuallyRedrawGraph(graphPtr);

  return TCL_OK;
}

static Blt_OpSpec axisOps[] = {
  {"activate",     1, (void*)AxisActivateOp,     3, 3, "",},
  {"bind",         1, (void*)BindOp,         2, 5, "sequence command",},
  {"cget",         2, (void*)AxisCgetOp,         4, 4, "option",},
  {"configure",    2, (void*)AxisConfigureOp,    3, 0, "?option value?...",},
  {"deactivate",   1, (void*)AxisActivateOp,     3, 3, "",},
  {"invtransform", 1, (void*)AxisInvTransformOp, 4, 4, "value",},
  {"limits",       1, (void*)AxisLimitsOp,       3, 3, "",},
  {"transform",    1, (void*)AxisTransformOp,    4, 4, "value",},
  {"use",          1, (void*)UseOp,          3, 4, "?axisName?",},
  {"view",         1, (void*)AxisViewOp,         3, 6, "?moveto fract? ",},
};

static int nAxisOps = sizeof(axisOps) / sizeof(Blt_OpSpec);

typedef int (GraphAxisProc)(Tcl_Interp* interp, Axis* axisPtr, 
			    int objc, Tcl_Obj* const objv[]);

int Blt_XAxisOp(Tcl_Interp* interp, Graph* graphPtr, int margin, 
		  int objc, Tcl_Obj* const objv[])
{
  GraphAxisProc* proc = (GraphAxisProc*)Blt_GetOpFromObj(interp, nAxisOps, axisOps, BLT_OP_ARG2, objc, objv, 0);
  if (!proc)
    return TCL_ERROR;

  if (proc == UseOp) {
    lastMargin = margin;
    return (*proc)(interp, (Axis*)graphPtr, objc, objv);
  } 
  else {
    Axis* axisPtr = Blt_GetFirstAxis(graphPtr->margins[margin].axes);
    if (!axisPtr)
      return TCL_OK;
    return (*proc)(interp, axisPtr, objc, objv);
  }
}