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

#include <string.h>

#include "bltGraph.h"
#include "bltGrBind.h"
#include "bltGrXAxisOp.h"
#include "bltGrAxis.h"
#include "bltGrAxisOp.h"

using namespace Blt;

static Axis* GetAxisFromCmd(ClientData clientData, Tcl_Obj* obj)
{
  Graph* graphPtr = (Graph*)clientData;
  GraphOptions* ops = (GraphOptions*)graphPtr->ops_;

  int margin;
  const char* name = Tcl_GetString(obj);
  if (!strcmp(name,"xaxis"))
    margin = (ops->inverted) ? MARGIN_LEFT : MARGIN_BOTTOM;
  else if (!strcmp(name,"yaxis"))
    margin = (ops->inverted) ? MARGIN_BOTTOM : MARGIN_LEFT;
  else if (!strcmp(name,"x2axis"))
    margin = (ops->inverted) ? MARGIN_RIGHT : MARGIN_TOP;
  else if (!strcmp(name,"y2axis"))
    margin = (ops->inverted) ? MARGIN_TOP : MARGIN_RIGHT;
  else
    return NULL;

  ChainLink link = Chain_FirstLink(ops->margins[margin].axes);
  return (Axis*)Chain_GetValue(link);
}

static int CgetOp(ClientData clientData, Tcl_Interp* interp,
		  int objc, Tcl_Obj* const objv[])
{
  Axis* axisPtr = GetAxisFromCmd(clientData, objv[1]);
  return AxisCgetOp(axisPtr, interp, objc, objv);
}

static int ConfigureOp(ClientData clientData, Tcl_Interp* interp,
		       int objc, Tcl_Obj* const objv[])
{
  Axis* axisPtr = GetAxisFromCmd(clientData, objv[1]);
  return AxisConfigureOp(axisPtr, interp, objc, objv);
}

static int ActivateOp(ClientData clientData, Tcl_Interp* interp, 
		      int objc, Tcl_Obj* const objv[])
{
  Axis* axisPtr = GetAxisFromCmd(clientData, objv[1]);
  return AxisActivateOp(axisPtr, interp, objc, objv);
}

static int BindOp(ClientData clientData, Tcl_Interp* interp, 
		  int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  Axis* axisPtr = GetAxisFromCmd(clientData, objv[1]);
  return graphPtr->bindTable_->configure(graphPtr->axisTag(axisPtr->name_), objc-3, objv+3);
}

static int InvTransformOp(ClientData clientData, Tcl_Interp* interp, 
			  int objc, Tcl_Obj* const objv[])
{
  Axis* axisPtr = GetAxisFromCmd(clientData, objv[1]);
  return AxisInvTransformOp(axisPtr, interp, objc, objv);
}

static int LimitsOp(ClientData clientData, Tcl_Interp* interp, 
		    int objc, Tcl_Obj* const objv[])
{
  Axis* axisPtr = GetAxisFromCmd(clientData, objv[1]);
  return AxisLimitsOp(axisPtr, interp, objc, objv);
}

static int TransformOp(ClientData clientData, Tcl_Interp* interp, 
		       int objc, Tcl_Obj* const objv[])
{
  Axis* axisPtr = GetAxisFromCmd(clientData, objv[1]);
  return AxisTransformOp(axisPtr, interp, objc, objv);
}

static int UseOp(ClientData clientData, Tcl_Interp* interp, 
		 int objc, Tcl_Obj* const objv[])
{
  Graph* graphPtr = (Graph*)clientData;
  GraphOptions* ops = (GraphOptions*)graphPtr->ops_;

  int margin;
  ClassId classId;
  const char* name = Tcl_GetString(objv[1]);
  if (!strcmp(name,"xaxis")) {
    classId = CID_AXIS_X;
    margin = (ops->inverted) ? MARGIN_LEFT : MARGIN_BOTTOM;
  }
  else if (!strcmp(name,"yaxis")) {
    classId = CID_AXIS_Y;
    margin = (ops->inverted) ? MARGIN_BOTTOM : MARGIN_LEFT;
  }
  else if (!strcmp(name,"x2axis")) {
    classId = CID_AXIS_X;
    margin = (ops->inverted) ? MARGIN_RIGHT : MARGIN_TOP;
  }
  else if (!strcmp(name,"y2axis")) {
    classId = CID_AXIS_Y;
    margin = (ops->inverted) ? MARGIN_TOP : MARGIN_RIGHT;
  }
  else
    return TCL_ERROR;

  Chain chain = ops->margins[margin].axes;

  if (objc == 3) {
    Tcl_Obj* listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **)NULL);
    for (ChainLink link = Chain_FirstLink(chain); link;
	 link = Chain_NextLink(link)) {
      Axis* axisPtr = (Axis*)Chain_GetValue(link);
      Tcl_ListObjAppendElement(interp, listObjPtr,
			       Tcl_NewStringObj(axisPtr->name_, -1));
    }
    Tcl_SetObjResult(interp, listObjPtr);
    return TCL_OK;
  }

  int axisObjc;  
  Tcl_Obj **axisObjv;
  if (Tcl_ListObjGetElements(interp, objv[3], &axisObjc, &axisObjv) != TCL_OK)
    return TCL_ERROR;

  for (ChainLink link=Chain_FirstLink(chain); link; link=Chain_NextLink(link)) {
    Axis* axisPtr = (Axis*)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);
  }

  Chain_Reset(chain);
  for (int ii=0; ii<axisObjc; ii++) {
    Axis* axisPtr;
    if (graphPtr->getAxis(axisObjv[ii], &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. */
      Chain_UnlinkLink(axisPtr->chain, axisPtr->link);
      Chain_AppendLink(chain, axisPtr->link);
    }
    else
      axisPtr->link = Chain_Append(chain, axisPtr);

    axisPtr->chain = chain;
    axisPtr->use_ =1;
  }

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

  return TCL_OK;
}

static int ViewOp(ClientData clientData, Tcl_Interp* interp, 
		  int objc, Tcl_Obj* const objv[])
{
  Axis* axisPtr = GetAxisFromCmd(clientData, objv[1]);
  return AxisViewOp(axisPtr, interp, objc, objv);
}

const Ensemble Blt::xaxisEnsemble[] = {
  {"activate",     ActivateOp, 0},
  {"bind",         BindOp, 0},
  {"cget",         CgetOp, 0},
  {"configure",    ConfigureOp, 0},
  {"deactivate",   ActivateOp, 0},
  {"invtransform", InvTransformOp, 0},
  {"limits",       LimitsOp, 0},
  {"transform",    TransformOp, 0},
  {"use",          UseOp, 0},
  {"view",         ViewOp, 0},
  { 0,0,0 }
};