summaryrefslogtreecommitdiffstats
path: root/src/tkbltGrMarkerOption.C
blob: b6eab579a6dc2927aca1baf212dddad9b0346020 (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
/*
 * 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 <stdlib.h>
#include <string.h>
#include <float.h>

#include "tkbltGrMarker.h"
#include "tkbltGrMarkerOption.h"
#include "tkbltConfig.h"

using namespace Blt;

static Tcl_Obj* PrintCoordinate(double x);
static int GetCoordinate(Tcl_Interp* interp, Tcl_Obj *objPtr, double *valuePtr);

static Tk_CustomOptionSetProc CoordsSetProc;
static Tk_CustomOptionGetProc CoordsGetProc;
static Tk_CustomOptionFreeProc CoordsFreeProc;
Tk_ObjCustomOption coordsObjOption =
  {
    "coords", CoordsSetProc, CoordsGetProc, RestoreProc, CoordsFreeProc, NULL
  };

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

  if (!coordsPtrPtr)
    return TCL_OK;

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

  if (objc == 0) {
    *coordsPtrPtr = NULL;
    return TCL_OK;
  }

  if (objc & 1) {
    Tcl_AppendResult(interp, "odd number of marker coordinates specified",NULL);
    return TCL_ERROR;
  }

  Coords* coordsPtr = new Coords;
  coordsPtr->num = objc/2;
  coordsPtr->points = new Point2d[coordsPtr->num];

  Point2d* pp = coordsPtr->points;
  for (int ii=0; ii<objc; ii+=2) {
    double x, y;
    if ((GetCoordinate(interp, objv[ii], &x) != TCL_OK) ||
	(GetCoordinate(interp, objv[ii+1], &y) != TCL_OK))
      return TCL_ERROR;
    pp->x = x;
    pp->y = y;
    pp++;
  }

  *coordsPtrPtr = coordsPtr;
  return TCL_OK;
}

static Tcl_Obj* CoordsGetProc(ClientData clientData, Tk_Window tkwin, 
			      char *widgRec, int offset)
{
  Coords* coordsPtr = *(Coords**)(widgRec + offset);

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

  int cnt = coordsPtr->num*2;
  Tcl_Obj** ll = new Tcl_Obj*[cnt];

  Point2d* pp = coordsPtr->points;
  for (int ii=0; ii<cnt; pp++) {
    ll[ii++] = PrintCoordinate(pp->x);
    ll[ii++] = PrintCoordinate(pp->y);
  }

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

static void CoordsFreeProc(ClientData clientData, Tk_Window tkwin,
			   char *ptr)
{
  Coords* coordsPtr = *(Coords**)ptr;
  if (coordsPtr) {
    if (coordsPtr->points)
      delete [] coordsPtr->points;
    delete coordsPtr;
  }
}

static Tk_CustomOptionSetProc CapStyleSetProc;
static Tk_CustomOptionGetProc CapStyleGetProc;
Tk_ObjCustomOption capStyleObjOption =
  {
    "capStyle", CapStyleSetProc, CapStyleGetProc, NULL, NULL, NULL
  };

static int CapStyleSetProc(ClientData clientData, Tcl_Interp* interp,
			   Tk_Window tkwin, Tcl_Obj** objPtr, char* widgRec,
			   int offset, char* save, int flags)
{
  int* ptr = (int*)(widgRec + offset);

  Tk_Uid uid = Tk_GetUid(Tcl_GetString(*objPtr));
  int cap;
  if (Tk_GetCapStyle(interp, uid, &cap) != TCL_OK)
    return TCL_ERROR;
  *ptr = cap;

  return TCL_OK;
}

static Tcl_Obj* CapStyleGetProc(ClientData clientData, Tk_Window tkwin, 
			      char *widgRec, int offset)
{
  int* ptr = (int*)(widgRec + offset);
  return Tcl_NewStringObj(Tk_NameOfCapStyle(*ptr), -1);
}

static Tk_CustomOptionSetProc JoinStyleSetProc;
static Tk_CustomOptionGetProc JoinStyleGetProc;
Tk_ObjCustomOption joinStyleObjOption =
  {
    "joinStyle", JoinStyleSetProc, JoinStyleGetProc, NULL, NULL, NULL
  };

static int JoinStyleSetProc(ClientData clientData, Tcl_Interp* interp,
			   Tk_Window tkwin, Tcl_Obj** objPtr, char* widgRec,
			   int offset, char* save, int flags)
{
  int* ptr = (int*)(widgRec + offset);

  Tk_Uid uid = Tk_GetUid(Tcl_GetString(*objPtr));
  int join;
  if (Tk_GetJoinStyle(interp, uid, &join) != TCL_OK)
    return TCL_ERROR;
  *ptr = join;

  return TCL_OK;
}

static Tcl_Obj* JoinStyleGetProc(ClientData clientData, Tk_Window tkwin, 
			      char *widgRec, int offset)
{
  int* ptr = (int*)(widgRec + offset);
  return Tcl_NewStringObj(Tk_NameOfJoinStyle(*ptr), -1);
}

static Tcl_Obj* PrintCoordinate(double x)
{
  if (x == DBL_MAX)
    return Tcl_NewStringObj("+Inf", -1);
  else if (x == -DBL_MAX)
    return Tcl_NewStringObj("-Inf", -1);
  else
    return Tcl_NewDoubleObj(x);
}

static int GetCoordinate(Tcl_Interp* interp, Tcl_Obj *objPtr, double *valuePtr)
{
  const char* expr = Tcl_GetString(objPtr);
  char c = expr[0];
  if ((c == 'I') && (strcmp(expr, "Inf") == 0))
    *valuePtr = DBL_MAX;		/* Elastic upper bound */
  else if ((c == '-') && (expr[1] == 'I') && (strcmp(expr, "-Inf") == 0))
    *valuePtr = -DBL_MAX;		/* Elastic lower bound */
  else if ((c == '+') && (expr[1] == 'I') && (strcmp(expr, "+Inf") == 0))
    *valuePtr = DBL_MAX;		/* Elastic upper bound */
  else if (Tcl_GetDoubleFromObj(interp, objPtr, valuePtr) != TCL_OK)
    return TCL_ERROR;

  return TCL_OK;
}