summaryrefslogtreecommitdiffstats
path: root/src/bltGrAxisOption.C
blob: 6a94e78b4492022d48f244f4c0da8331bf48496d (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
/*
 * 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 <math.h>
#include <stdlib.h>

#include "bltGraph.h"
#include "bltGrAxis.h"
#include "bltGrAxisOption.h"
#include "bltConfig.h"

using namespace Blt;

static Tk_CustomOptionSetProc AxisSetProc;
static Tk_CustomOptionGetProc AxisGetProc;
static Tk_CustomOptionFreeProc AxisFreeProc;
Tk_ObjCustomOption xAxisObjOption =
  {
    "xaxis", AxisSetProc, AxisGetProc, RestoreProc, AxisFreeProc,
    (ClientData)CID_AXIS_X
  };
Tk_ObjCustomOption yAxisObjOption =
  {
    "yaxis", AxisSetProc, AxisGetProc, RestoreProc, AxisFreeProc,
    (ClientData)CID_AXIS_Y
  };

static int AxisSetProc(ClientData clientData, Tcl_Interp* interp,
		       Tk_Window tkwin, Tcl_Obj** objPtr, char* widgRec,
		       int offset, char* savePtr, int flags)
{
  Axis** axisPtrPtr = (Axis**)(widgRec + offset);
  *(double*)savePtr = *(double*)axisPtrPtr;
  
  if (!axisPtrPtr)
    return TCL_OK;

  Graph* graphPtr = getGraphFromWindowData(tkwin);
  ClassId classId = (ClassId)(long(clientData));

  Axis *axisPtr;
  if (graphPtr->getAxis(*objPtr, &axisPtr) != TCL_OK)
    return TCL_ERROR;

  if (classId != CID_NONE) {
    // Set the axis type on the first use of it.
    if ((axisPtr->refCount_ == 0) || (axisPtr->classId_ == CID_NONE))
      axisPtr->setClass(classId);

    else if (axisPtr->classId_ != classId) {
      Tcl_AppendResult(interp, "axis \"", Tcl_GetString(*objPtr),
		       "\" is already in use on an opposite ", 
		       axisPtr->className_, "-axis", 
		       NULL);
      return TCL_ERROR;
    }
    axisPtr->refCount_++;
  }

  *axisPtrPtr = axisPtr;
  return TCL_OK;
};

static Tcl_Obj* AxisGetProc(ClientData clientData, Tk_Window tkwin, 
			    char *widgRec, int offset)
{
  Axis* axisPtr = *(Axis**)(widgRec + offset);
  if (!axisPtr)
    return Tcl_NewStringObj("", -1);

  return Tcl_NewStringObj(axisPtr->name_, -1);
};

static void AxisFreeProc(ClientData clientData, Tk_Window tkwin, char *ptr)
{
  Axis* axisPtr = *(Axis**)ptr;
  if (axisPtr) {
    axisPtr->refCount_--;
    if (axisPtr->refCount_ == 0)
      delete axisPtr;
  }
}

static Tk_CustomOptionSetProc LimitSetProc;
static Tk_CustomOptionGetProc LimitGetProc;
Tk_ObjCustomOption limitObjOption =
  {
    "limit", LimitSetProc, LimitGetProc, NULL, NULL, NULL
  };

static int LimitSetProc(ClientData clientData, Tcl_Interp* interp,
			Tk_Window tkwin, Tcl_Obj** objPtr, char* widgRec,
			int offset, char* save, int flags)
{
  double* limitPtr = (double*)(widgRec + offset);
  const char* string = Tcl_GetString(*objPtr);
  if (!string || !string[0]) {
    *limitPtr = NAN;
    return TCL_OK;
  }

  if (Tcl_GetDoubleFromObj(interp, *objPtr, limitPtr) != TCL_OK)
    return TCL_ERROR;

  return TCL_OK;
}

static Tcl_Obj* LimitGetProc(ClientData clientData, Tk_Window tkwin, 
			     char *widgRec, int offset)
{
  double limit = *(double*)(widgRec + offset);
  Tcl_Obj* objPtr;

  if (!isnan(limit))
    objPtr = Tcl_NewDoubleObj(limit);
  else
    objPtr = Tcl_NewStringObj("", -1);

  return objPtr;
}

static Tk_CustomOptionSetProc TicksSetProc;
static Tk_CustomOptionGetProc TicksGetProc;
static Tk_CustomOptionFreeProc TicksFreeProc;
Tk_ObjCustomOption ticksObjOption =
  {
    "ticks", TicksSetProc, TicksGetProc, RestoreProc, TicksFreeProc, NULL
  };

static int TicksSetProc(ClientData clientData, Tcl_Interp* interp,
			Tk_Window tkwin, Tcl_Obj** objPtr, char* widgRec,
			int offset, char* savePtr, int flags)
{
  Ticks** ticksPtrPtr = (Ticks**)(widgRec + offset);
  *(double*)savePtr = *(double*)ticksPtrPtr;

  if (!ticksPtrPtr)
    return TCL_OK;

  int objc;
  Tcl_Obj** objv;
  if (Tcl_ListObjGetElements(interp, *objPtr, &objc, &objv) != TCL_OK)
    return TCL_ERROR;

  Ticks* ticksPtr = NULL;
  if (objc > 0) {
    ticksPtr = new Ticks(objc);
    for (int ii=0; ii<objc; ii++) {
      double value;
      if (Tcl_GetDoubleFromObj(interp, objv[ii], &value) != TCL_OK) {
	delete ticksPtr;
	return TCL_ERROR;
      }
      ticksPtr->values[ii] = value;
    }
    ticksPtr->nTicks = objc;
  }

  *ticksPtrPtr = ticksPtr;

  return TCL_OK;
}

static Tcl_Obj* TicksGetProc(ClientData clientData, Tk_Window tkwin, 
			     char *widgRec, int offset)
{
  Ticks* ticksPtr = *(Ticks**)(widgRec + offset);

  if (!ticksPtr)
    return Tcl_NewListObj(0, NULL);

  int cnt = ticksPtr->nTicks;
  Tcl_Obj** ll = new Tcl_Obj*[cnt];
  for (int ii = 0; ii<cnt; ii++)
    ll[ii] = Tcl_NewDoubleObj(ticksPtr->values[ii]);

  Tcl_Obj* listObjPtr = Tcl_NewListObj(cnt, ll);
  delete [] ll;

  return listObjPtr;
}

static void TicksFreeProc(ClientData clientData, Tk_Window tkwin,
			 char *ptr)
{
  Ticks* ticksPtr = *(Ticks**)ptr;
  if (ticksPtr)
    delete ticksPtr;
}

static Tk_CustomOptionSetProc ObjectSetProc;
static Tk_CustomOptionGetProc ObjectGetProc;
static Tk_CustomOptionFreeProc ObjectFreeProc;
Tk_ObjCustomOption objectObjOption =
  {
    "object", ObjectSetProc, ObjectGetProc, RestoreProc, ObjectFreeProc, NULL,
  };

static int ObjectSetProc(ClientData clientData, Tcl_Interp* interp,
			Tk_Window tkwin, Tcl_Obj** objPtr, char* widgRec,
			int offset, char* savePtr, int flags)
{
  Tcl_Obj** objectPtrPtr = (Tcl_Obj**)(widgRec + offset);
  *(double*)savePtr = *(double*)objectPtrPtr;

  if (!objectPtrPtr)
    return TCL_OK;

  Tcl_IncrRefCount(*objPtr);
  *objectPtrPtr = *objPtr;

  return TCL_OK;
}
    
static Tcl_Obj* ObjectGetProc(ClientData clientData, Tk_Window tkwin, 
			      char *widgRec, int offset)
{
  Tcl_Obj** objectPtrPtr = (Tcl_Obj**)(widgRec + offset);

  if (!objectPtrPtr)
    return Tcl_NewObj();

  return *objectPtrPtr;
}

static void ObjectFreeProc(ClientData clientData, Tk_Window tkwin,
			   char *ptr)
{
  Tcl_Obj* objectPtr = *(Tcl_Obj**)ptr;
  if (objectPtr)
    Tcl_DecrRefCount(objectPtr);
}