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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
/*
* tkProperty.c --
*
* This file manages properties for the Tk toolkit,
*
* Copyright (c) 1994-1998 Nick-Ing-Simmons
*
* All rights reserved.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR
* HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE AUTHOR HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#ifndef lint
static char rcsid[] = "$Header$";
#endif
#include "tkPort.h"
#include "tkInt.h"
/* /home/nick/bin/add_protos : Added declarations with prototypes */
static int PropToResult _ANSI_ARGS_((Tcl_Interp *interp, Tk_Window tkwin, Atom type, unsigned char *p, int format, long unsigned int count));
static int ArgToProp _ANSI_ARGS_((Tcl_Interp *interp, Tk_Window tkwin, Atom type, int format, Arg arg, unsigned char **prop, long unsigned int *count));
extern int Tk_PropertyCmd _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc, Arg *args));
static int PropertyExists _ANSI_ARGS_((Tk_Window tkwin, Window xid, Atom key, long unsigned int *sizep));
static int
PropertyExists(tkwin, xid, key, sizep)
Tk_Window tkwin;
Window xid;
Atom key;
long unsigned int *sizep;
{
unsigned long bytes_after = 0;
Atom type = None;
unsigned long count = 0;
unsigned long bytesafter = 0;
unsigned char *prop = NULL;
int format = 0;
if (!sizep)
sizep = &bytes_after;
XGetWindowProperty(Tk_Display(tkwin), xid, key, 0L, 0L, False,
AnyPropertyType, &type, &format, &count, sizep, &prop);
if (prop)
XFree((char *) prop);
return format;
}
static int
PropToResult(interp, tkwin, type, p, format, count)
Tcl_Interp *interp;
Tk_Window tkwin;
Atom type;
unsigned char *p;
int format;
long unsigned int count;
{
if (format == 8)
{
Lang_SetBinaryResult(interp,(char *) p, count, TCL_VOLATILE);
}
else
{
while (count--)
{
unsigned long value = 0;
if (8 * sizeof(unsigned char) == format)
{
value = *((unsigned char *) p);
}
else if (8 * sizeof(unsigned short) == format)
{
value = *((unsigned short *) p);
}
else if (8 * sizeof(unsigned int) == format)
{
value = *((unsigned int *) p);
}
else if (8 * sizeof(unsigned long) == format)
{
value = *((unsigned long *) p);
}
else
{
Tcl_SprintfResult(interp, "No type for format %d", format);
return TCL_ERROR;
}
p += (format / 8);
if (type == XA_ATOM)
{
if ((Atom) value != None)
Tcl_AppendElement(interp, Tk_GetAtomName(tkwin, value));
}
else
{
Tcl_IntResults(interp, 1, 1, value);
}
}
}
return TCL_OK;
}
static int
ArgToProp(interp, tkwin, type, format, arg, prop, count)
Tcl_Interp *interp;
Tk_Window tkwin;
Atom type;
int format;
Arg arg;
unsigned char **prop;
long unsigned int *count;
{
int result = TCL_OK;
if (format == 8)
{
char *s = LangString(arg);
int l = strlen(s);
*prop = (unsigned char *) ckalloc(l + 1);
*count = l + 1;
strcpy((char *)(*prop), s);
}
else
{
LangFreeProc *freeProc = NULL;
int valc = 0;
Arg *valv = NULL;
result = Lang_SplitList(interp, arg, &valc, &valv, &freeProc);
if (result == TCL_OK)
{
unsigned char *p = (unsigned char *) ckalloc(valc * format / 8);
int i;
*prop = p;
*count = valc;
for (i = 0; i < valc; i++)
{
int value = 0;
if (type == XA_ATOM)
{
value = Tk_InternAtom(tkwin, LangString(valv[i]));
}
else
{
result = Tcl_GetInt(interp, valv[i], &value);
if (result != TCL_OK)
break;
}
if (8 * sizeof(unsigned char) == format)
{
*((unsigned char *) p) = value;
}
else if (8 * sizeof(unsigned short) == format)
{
*((unsigned short *) p) = value;
}
else if (8 * sizeof(unsigned int) == format)
{
*((unsigned int *) p) = value;
}
else if (8 * sizeof(unsigned long) == format)
{
*((unsigned long *) p) = value;
}
else
{
Tcl_SprintfResult(interp, "No type for format %d", format);
result = TCL_ERROR;
break;
}
p += (format / 8);
}
if (freeProc)
(*freeProc) (valc, valv);
if (result != TCL_OK)
{
ckfree(*prop);
*prop = NULL;
*count = 0;
}
}
}
return result;
}
int
Tk_PropertyCmd(clientData, interp, argc, args)
ClientData clientData; /* Main window associated with
* interpreter. */
Tcl_Interp *interp; /* Current interpreter. */
int argc; /* Number of arguments. */
Arg *args; /* Argument strings. */
{
Tk_Window tkwin = (Tk_Window) clientData;
Tk_Window window;
Atom atom;
Window xid;
int length;
char c;
if (argc < 3)
{
error:
Tcl_SprintfResult(interp,
"wrong # args: should be \"%.50s option window ?arg arg ...?\"",
LangString(args[0]));
return TCL_ERROR;
}
window = Tk_NameToWindow(interp, LangString(args[2]), tkwin);
if (window == NULL)
return TCL_ERROR;
tkwin = window;
Tk_MakeWindowExist(window);
xid = Tk_WindowId(window);
c = LangString(args[1])[0];
length = strlen(LangString(args[1]));
if (!c)
goto error;
if (((c == 'g') && (strncmp(LangString(args[1]), "get", length) == 0)) ||
((c == 'e') && (strncmp(LangString(args[1]), "exists", length) == 0)) ||
((c == 'd') && (strncmp(LangString(args[1]), "delete", length) == 0))
)
{
int result = TCL_OK;
if (argc == 5)
{
if (!strcmp(LangString(args[4]), "root"))
xid = RootWindowOfScreen(Tk_Screen(tkwin));
else
{
char *end;
xid = strtoul(LangString(args[4]), &end, 10);
if (*end)
{
Tcl_SprintfResult(interp, "Bad number '%s'", LangString(args[4]));
return TCL_ERROR;
}
}
argc--;
}
if (argc != 4)
{
Tcl_SprintfResult(interp, "wrong # args: should be \"%.50s %s window Atom ?xid?\"",
LangString(args[0]), LangString(args[1]));
return TCL_ERROR;
}
else
{
Atom atom = Tk_InternAtom(tkwin, LangString(args[3]));
if (c == 'd')
{
XDeleteProperty(Tk_Display(tkwin), xid, atom);
}
else
{
long unsigned int size = 0;
int format = PropertyExists(tkwin, xid, atom, &size);
if (c == 'e')
{
Tcl_IntResults(interp, 1, 0, format);
}
else
{
Atom type = None;
unsigned char *prop = NULL;
unsigned long count = 0;
XGetWindowProperty(Tk_Display(tkwin), xid, atom, 0L, size, False,
AnyPropertyType, &type, &format, &count, &size, &prop);
if (format == 0 || type == None)
{
Tcl_SprintfResult(interp, "Property %s does not exist on 0x%lx",
LangString(args[3]), (unsigned long) xid);
result = TCL_ERROR;
}
else
{
Tcl_SetResult(interp, Tk_GetAtomName(tkwin, type), TCL_STATIC);
result = PropToResult(interp, tkwin, type, prop, format, count);
}
if (prop)
XFree((char *) prop);
}
}
}
return result;
}
else if ((c == 'l') && (strncmp(LangString(args[1]), "list", length) == 0))
{
if (argc == 4)
{
if (!strcmp(LangString(args[3]), "root"))
xid = RootWindowOfScreen(Tk_Screen(tkwin));
else
{
char *end;
xid = strtoul(LangString(args[3]), &end, 10);
if (*end)
{
Tcl_SprintfResult(interp, "Bad number '%s'", LangString(args[3]));
return TCL_ERROR;
}
}
argc--;
}
if (argc == 3)
{
#ifndef WIN32
int num_prop = 0;
Atom *list = XListProperties(Tk_Display(tkwin), xid, &num_prop);
int i;
for (i = 0; i < num_prop; i++)
{
if (list[i] != None)
Tcl_AppendElement(interp, Tk_GetAtomName(tkwin, list[i]));
}
if (list)
XFree((char *) list);
#endif
}
return TCL_OK;
}
else if ((c == 's') && (strncmp(LangString(args[1]), "set", length) == 0))
{
int result = TCL_OK;
if (argc == 8)
{
if (!strcmp(LangString(args[7]), "root"))
xid = RootWindowOfScreen(Tk_Screen(tkwin));
else
{
char *end;
xid = strtoul(LangString(args[7]), &end, 10);
if (*end)
{
Tcl_SprintfResult(interp, "Bad number '%s'", LangString(args[7]));
return TCL_ERROR;
}
}
argc--;
}
if (argc != 7)
{
Tcl_SprintfResult(interp, "wrong # args: should be \"%.50s %s window Atom type format value ?xid?\"",
LangString(args[0]), LangString(args[1]));
return TCL_ERROR;
}
else
{
Atom atom = Tk_InternAtom(tkwin, LangString(args[3]));
Atom type = Tk_InternAtom(tkwin, LangString(args[4]));
int format = 0;
result = Tcl_GetInt(interp, args[5], &format);
if (result == TCL_OK)
{
unsigned char *prop = NULL;
unsigned long count = 0;
result = ArgToProp(interp, tkwin, type, format, args[6], &prop, &count);
if (result == TCL_OK)
{
XChangeProperty(Tk_Display(tkwin), xid, atom, type, format,
PropModeReplace, prop, count);
if (prop)
ckfree(prop);
}
}
}
return result;
}
else
{
Tcl_SprintfResult(interp,
"bad option \"%.50s\": must be get, exists, list, delete or set",
LangString(args[1]));
return TCL_ERROR;
}
}
|