Debian Codegraph @

pTk/tkProperty.c

raw symbols SCIP
1 /*
2 * tkProperty.c --
3 *
4 * This file manages properties for the Tk toolkit,
5 *
6 * Copyright (c) 1994-1999 Nick-Ing-Simmons
7 *
8 * All rights reserved.
9 *
10 * Permission is hereby granted, without written agreement and without
11 * license or royalty fees, to use, copy, modify, and distribute this
12 * software and its documentation for any purpose, provided that the
13 * above copyright notice and the following two paragraphs appear in
14 * all copies of this software.
15 *
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR
17 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
18 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR
19 * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20 *
21 * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES,
22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
24 * ON AN "AS IS" BASIS, AND THE AUTHOR HAS NO OBLIGATION TO
25 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
26 */
27
28 #ifndef lint
29 static char rcsid[] = "$Header$";
30 #endif
31
32 #include "tkPort.h"
33 #include "tkInt.h"
34
35 /* /home/nick/bin/add_protos : Added declarations with prototypes */
36 static int PropToResult _ANSI_ARGS_((Tcl_Interp *interp, Tk_Window tkwin, Atom type, unsigned char *p, int format, long unsigned int count));
37 static int ArgToProp _ANSI_ARGS_((Tcl_Interp *interp, Tk_Window tkwin, Atom type, int format, Tcl_Obj * arg, unsigned char **prop, long unsigned int *count));
38 extern int Tk_PropertyCmd _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc, Tcl_Obj * *args));
39 static int PropertyExists _ANSI_ARGS_((Tk_Window tkwin, Window xid, Atom key, long unsigned int *sizep));
40
41 static int
42 PropertyExists(tkwin, xid, key, sizep)
43 Tk_Window tkwin;
44 Window xid;
45 Atom key;
46 long unsigned int *sizep;
47 {
48 unsigned long bytes_after = 0;
49 Atom type = None;
50 unsigned long count = 0;
51 unsigned long bytesafter = 0;
52 unsigned char *prop = NULL;
53 int format = 0;
54 if (!sizep)
55 sizep = &bytes_after;
56 XGetWindowProperty(Tk_Display(tkwin), xid, key, 0L, 0L, False,
57 AnyPropertyType, &type, &format, &count, sizep, &prop);
58 if (prop)
59 XFree((char *) prop);
60 return format;
61 }
62
63 static int
64 PropToResult(interp, tkwin, type, p, format, count)
65 Tcl_Interp *interp;
66 Tk_Window tkwin;
67 Atom type;
68 unsigned char *p;
69 int format;
70 long unsigned int count;
71 {
72 if (format == 8)
73 {
74 Lang_SetBinaryResult(interp,(char *) p, count, TCL_VOLATILE);
75 }
76 else
77 {
78 while (count--)
79 {
80 unsigned long value = 0;
81 if (format == 8)
82 {
83 value = *((unsigned char *) p);
84 p += sizeof(unsigned char);
85 }
86 else if (format == 16)
87 {
88 value = *((unsigned short *) p);
89 p += sizeof(unsigned short);
90 }
91 else if (format == 32)
92 {
93 value = *((unsigned long *) p);
94 p += sizeof(unsigned long);
95 }
96 else
97 {
98 Tcl_SprintfResult(interp, "No type for format %d", format);
99 return TCL_ERROR;
100 }
101
102 if (type == XA_ATOM)
103 {
104 if ((Atom) value != None)
105 Tcl_AppendElement(interp, Tk_GetAtomName(tkwin, value));
106 }
107 else
108 {
109 Tcl_IntResults(interp, 1, 1, value);
110 }
111 }
112 }
113 return TCL_OK;
114 }
115
116 static int
117 ArgToProp(interp, tkwin, type, format, arg, prop, count)
118 Tcl_Interp *interp;
119 Tk_Window tkwin;
120 Atom type;
121 int format;
122 Tcl_Obj * arg;
123 unsigned char **prop;
124 long unsigned int *count;
125 {
126 int result = TCL_OK;
127 if (format == 8)
128 {
129 char *s = Tcl_GetString(arg);
130 int l = strlen(s);
131 *prop = (unsigned char *) ckalloc(l + 1);
132 *count = l + 1;
133 strcpy((char *)(*prop), s);
134 }
135 else
136 {
137 int valc = 0;
138 Tcl_Obj * *valv = NULL;
139 result = Tcl_ListObjGetElements(interp, arg, &valc, &valv);
140 if (result == TCL_OK)
141 {
142 unsigned char *p = (unsigned char *) ckalloc(valc * (format == 8 ? sizeof(unsigned char) : format == 16 ? sizeof(unsigned short) : sizeof(unsigned long)));
143 int i;
144 *prop = p;
145 *count = valc;
146 for (i = 0; i < valc; i++)
147 {
148 int value = 0;
149 result = Tcl_GetIntFromObj(interp, valv[i], &value);
150 if (result != TCL_OK)
151 {
152 if (type == XA_ATOM)
153 {
154 value = Tk_InternAtom(tkwin, Tcl_GetString(valv[i]));
155 result = TCL_OK;
156 }
157 else
158 break;
159 }
160 if (format == 8)
161 {
162 *((unsigned char *) p) = value;
163 p += sizeof(unsigned char);
164 }
165 else if (format == 16)
166 {
167 *((unsigned short *) p) = value;
168 p += sizeof(unsigned short);
169 }
170 else if (format == 32)
171 {
172 *((unsigned long *) p) = value;
173 p += sizeof(unsigned long);
174 }
175 else
176 {
177 Tcl_SprintfResult(interp, "No type for format %d", format);
178 result = TCL_ERROR;
179 break;
180 }
181 }
182 if (result != TCL_OK)
183 {
184 ckfree(*prop);
185 *prop = NULL;
186 *count = 0;
187 }
188 }
189 }
190 return result;
191 }
192
193 static int ErrorProc _ANSI_ARGS_((ClientData clientData,
194 XErrorEvent *errEventPtr));
195
196 static int
197 ErrorProc(clientData,errEventPtr)
198 ClientData clientData;
199 XErrorEvent *errEventPtr;
200 {
201 int *resultPtr = (int *) clientData;
202 *resultPtr = TCL_ERROR;
203 return 0;
204 }
205
206 int
207 Tk_PropertyCmd(clientData, interp, argc, args)
208 ClientData clientData; /* Main window associated with
209 * interpreter. */
210 Tcl_Interp *interp; /* Current interpreter. */
211 int argc; /* Number of arguments. */
212 Tcl_Obj * *args; /* Argument strings. */
213 {
214
215 Tk_Window tkwin = (Tk_Window) clientData;
216 Tk_Window window;
217 Atom atom;
218 Window xid;
219 int length;
220 char c;
221 int result = TCL_OK;
222 Tk_ErrorHandler errorHandler = NULL;
223
224 if (argc < 3)
225 {
226 error:
227 Tcl_SprintfResult(interp,
228 "wrong # args: should be \"%.50s option window ?arg arg ...?\"",
229 Tcl_GetString(args[0]));
230 return TCL_ERROR;
231 }
232
233 window = Tk_NameToWindow(interp, Tcl_GetString(args[2]), tkwin);
234 if (window == NULL)
235 return TCL_ERROR;
236 tkwin = window;
237 Tk_MakeWindowExist(window);
238 xid = Tk_WindowId(window);
239
240 c = Tcl_GetString(args[1])[0];
241 length = strlen(Tcl_GetString(args[1]));
242 if (!c)
243 goto error;
244
245 errorHandler = Tk_CreateErrorHandler(Tk_Display(tkwin), -1, -1, -1, ErrorProc, (ClientData) &result);
246
247 if (((c == 'g') && (strncmp(Tcl_GetString(args[1]), "get", length) == 0)) ||
248 ((c == 'e') && (strncmp(Tcl_GetString(args[1]), "exists", length) == 0)) ||
249 ((c == 'd') && (strncmp(Tcl_GetString(args[1]), "delete", length) == 0))
250 )
251 {
252 if (argc == 5)
253 {
254 if (!strcmp(Tcl_GetString(args[4]), "root"))
255 xid = RootWindowOfScreen(Tk_Screen(tkwin));
256 else
257 {
258 char *end;
259 xid = strtoul(Tcl_GetString(args[4]), &end, 10);
260 if (*end)
261 {
262 Tcl_SprintfResult(interp, "Bad number '%s'", Tcl_GetString(args[4]));
263 result = TCL_ERROR;
264 goto done;
265 }
266 }
267 argc--;
268 }
269 if (argc != 4)
270 {
271 Tcl_SprintfResult(interp, "wrong # args: should be \"%.50s %s window Atom ?xid?\"",
272 Tcl_GetString(args[0]), Tcl_GetString(args[1]));
273 result = TCL_ERROR;
274 goto done;
275 }
276 else
277 {
278 Atom atom = Tk_InternAtom(tkwin, Tcl_GetString(args[3]));
279 if (c == 'd')
280 {
281 XDeleteProperty(Tk_Display(tkwin), xid, atom);
282 if (result != TCL_OK)
283 {
284 Tcl_SprintfResult(interp, "XError in XDeleteProperty()");
285 goto done;
286 }
287 }
288 else
289 {
290 long unsigned int size = 0;
291 int format = PropertyExists(tkwin, xid, atom, &size);
292 if (result != TCL_OK)
293 Tcl_SprintfResult(interp, "XError in occured");
294 else if (c == 'e')
295 {
296 Tcl_SetIntObj(Tcl_GetObjResult(interp), format);
297 }
298 else
299 {
300 Atom type = None;
301 unsigned char *prop = NULL;
302 unsigned long count = 0;
303 XGetWindowProperty(Tk_Display(tkwin), xid, atom, 0L, size, False,
304 AnyPropertyType, &type, &format, &count, &size, &prop);
305 if (result != TCL_OK)
306 Tcl_SprintfResult(interp, "XError occured");
307 else if (format == 0 || type == None)
308 {
309 Tcl_SprintfResult(interp, "Property %s does not exist on 0x%lx",
310 Tcl_GetString(args[3]), (unsigned long) xid);
311 result = TCL_ERROR;
312 }
313 else
314 {
315 Tcl_SetResult(interp, (char *) Tk_GetAtomName(tkwin, type), TCL_STATIC);
316 result = PropToResult(interp, tkwin, type, prop, format, count);
317 }
318 if (prop)
319 XFree((char *) prop);
320 }
321 }
322 }
323 goto done;
324 }
325 else if ((c == 'l') && (strncmp(Tcl_GetString(args[1]), "list", length) == 0))
326 {
327 if (argc == 4)
328 {
329 if (!strcmp(Tcl_GetString(args[3]), "root"))
330 xid = RootWindowOfScreen(Tk_Screen(tkwin));
331 else
332 {
333 char *end;
334 xid = strtoul(Tcl_GetString(args[3]), &end, 10);
335 if (*end)
336 {
337 Tcl_SprintfResult(interp, "Bad number '%s'", Tcl_GetString(args[3]));
338 result = TCL_ERROR;
339 goto done;
340 }
341 }
342 argc--;
343 }
344 if (argc == 3)
345 {
346 #ifndef WIN32
347 int num_prop = 0;
348 Atom *list = XListProperties(Tk_Display(tkwin), xid, &num_prop);
349 int i;
350 for (i = 0; i < num_prop; i++)
351 {
352 if (list[i] != None)
353 Tcl_AppendElement(interp, Tk_GetAtomName(tkwin, list[i]));
354 }
355 if (list)
356 XFree((char *) list);
357 #endif
358 }
359 }
360 else if ((c == 's') && (strncmp(Tcl_GetString(args[1]), "set", length) == 0))
361 {
362 int result = TCL_OK;
363 if (argc == 8)
364 {
365 if (!strcmp(Tcl_GetString(args[7]), "root"))
366 xid = RootWindowOfScreen(Tk_Screen(tkwin));
367 else
368 {
369 char *end;
370 xid = strtoul(Tcl_GetString(args[7]), &end, 10);
371 if (*end)
372 {
373 Tcl_SprintfResult(interp, "Bad number '%s'", Tcl_GetString(args[7]));
374 result = TCL_ERROR;
375 goto done;
376 }
377 }
378 argc--;
379 }
380 if (argc != 7)
381 {
382 Tcl_SprintfResult(interp, "wrong # args: should be \"%.50s %s window Atom type format value ?xid?\"",
383 Tcl_GetString(args[0]), Tcl_GetString(args[1]));
384 result = TCL_ERROR;
385 goto done;
386 }
387 else
388 {
389 Atom atom = Tk_InternAtom(tkwin, Tcl_GetString(args[3]));
390 Atom type = Tk_InternAtom(tkwin, Tcl_GetString(args[4]));
391 int format = 0;
392 result = Tcl_GetIntFromObj(interp, args[5], &format);
393 if (result == TCL_OK)
394 {
395 unsigned char *prop = NULL;
396 unsigned long count = 0;
397 result = ArgToProp(interp, tkwin, type, format, args[6], &prop, &count);
398 if (result == TCL_OK)
399 {
400 XChangeProperty(Tk_Display(tkwin), xid, atom, type, format,
401 PropModeReplace, prop, count);
402 if (prop)
403 ckfree(prop);
404 }
405 }
406 }
407 }
408 else
409 {
410 Tcl_SprintfResult(interp,
411 "bad option \"%.50s\": must be get, exists, list, delete or set",
412 Tcl_GetString(args[1]));
413 result = TCL_ERROR;
414 }
415 done:
416 if (errorHandler)
417 Tk_DeleteErrorHandler(errorHandler);
418 return result;
419 }
420