Debian Codegraph @

generic/bltGrPen.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1
2 /*
3 * bltGrPen.c --
4 *
5 * This module implements pens for the BLT graph widget.
6 *
7 * Copyright 1996-1998 Lucent Technologies, Inc.
8 *
9 * Permission to use, copy, modify, and distribute this software and
10 * its documentation for any purpose and without fee is hereby
11 * granted, provided that the above copyright notice appear in all
12 * copies and that both that the copyright notice and warranty
13 * disclaimer appear in supporting documentation, and that the names
14 * of Lucent Technologies any of their entities not be used in
15 * advertising or publicity pertaining to distribution of the software
16 * without specific, written prior permission.
17 *
18 * Lucent Technologies disclaims all warranties with regard to this
19 * software, including all implied warranties of merchantability and
20 * fitness. In no event shall Lucent Technologies be liable for any
21 * special, indirect or consequential damages or any damages
22 * whatsoever resulting from loss of use, data or profits, whether in
23 * an action of contract, negligence or other tortuous action, arising
24 * out of or in connection with the use or performance of this
25 * software.
26 */
27
28 #include "bltGraph.h"
29 #include <X11/Xutil.h>
30
31 static Tk_OptionParseProc StringToColor;
32 static Tk_OptionPrintProc ColorToString;
33 static Tk_OptionParseProc StringToPen;
34 static Tk_OptionPrintProc PenToString;
35 Tk_CustomOption bltColorOption =
36 {
37 StringToColor, ColorToString, (ClientData)0
38 };
39 Tk_CustomOption bltPenOption =
40 {
41 StringToPen, PenToString, (ClientData)0
42 };
43 Tk_CustomOption bltBarPenOption =
44 {
45 StringToPen, PenToString, (ClientData)&bltBarElementUid
46 };
47 Tk_CustomOption bltLinePenOption =
48 {
49 StringToPen, PenToString, (ClientData)&bltLineElementUid
50 };
51
52 /*
53 *----------------------------------------------------------------------
54
55 * StringToColor --
56 *
57 * Convert the string representation of a color into a XColor pointer.
58 *
59 * Results:
60 * The return value is a standard Tcl result. The color pointer is
61 * written into the widget record.
62 *
63 *----------------------------------------------------------------------
64 */
65 /*ARGSUSED*/
66 static int
67 StringToColor(clientData, interp, tkwin, string, widgRec, offset)
68 ClientData clientData; /* Not used. */
69 Tcl_Interp *interp; /* Interpreter to send results back to */
70 Tk_Window tkwin; /* Not used. */
71 char *string; /* String representing color */
72 char *widgRec; /* Widget record */
73 int offset; /* Offset of color field in record */
74 {
75 XColor **colorPtrPtr = (XColor **)(widgRec + offset);
76 XColor *colorPtr;
77 unsigned int length;
78 char c;
79
80 if ((string == NULL) || (*string == '\0')) {
81 *colorPtrPtr = NULL;
82 return TCL_OK;
83 }
84 c = string[0];
85 length = strlen(string);
86
87 if ((c == 'd') && (strncmp(string, "defcolor", length) == 0)) {
88 colorPtr = COLOR_DEFAULT;
89 } else {
90 colorPtr = Tk_GetColor(interp, tkwin, Tk_GetUid(string));
91 if (colorPtr == NULL) {
92 return TCL_ERROR;
93 }
94 }
95 *colorPtrPtr = colorPtr;
96 return TCL_OK;
97 }
98
99 /*
100 *----------------------------------------------------------------------
101 *
102 * NameOfColor --
103 *
104 * Convert the color option value into a string.
105 *
106 * Results:
107 * The static string representing the color option is returned.
108 *
109 *----------------------------------------------------------------------
110 */
111 static char *
112 NameOfColor(colorPtr)
113 XColor *colorPtr;
114 {
115 if (colorPtr == NULL) {
116 return "";
117 } else if (colorPtr == COLOR_DEFAULT) {
118 return "defcolor";
119 } else {
120 return Tk_NameOfColor(colorPtr);
121 }
122 }
123
124 /*
125 *----------------------------------------------------------------------
126 *
127 * ColorToString --
128 *
129 * Convert the color value into a string.
130 *
131 * Results:
132 * The string representing the symbol color is returned.
133 *
134 *----------------------------------------------------------------------
135 */
136 /*ARGSUSED*/
137 static char *
138 ColorToString(clientData, tkwin, widgRec, offset, freeProcPtr)
139 ClientData clientData; /* Not used. */
140 Tk_Window tkwin; /* Not used. */
141 char *widgRec; /* Widget information record */
142 int offset; /* Offset of symbol type in record */
143 Tcl_FreeProc **freeProcPtr; /* Not used. */
144 {
145 XColor *colorPtr = *(XColor **)(widgRec + offset);
146
147 return NameOfColor(colorPtr);
148 }
149
150 /*
151 *----------------------------------------------------------------------
152 *
153 * StringToPen --
154 *
155 * Convert the color value into a string.
156 *
157 * Results:
158 * The string representing the symbol color is returned.
159 *
160 *----------------------------------------------------------------------
161 */
162 /*ARGSUSED*/
163 static int
164 StringToPen(clientData, interp, tkwin, string, widgRec, offset)
165 ClientData clientData; /* Not used. */
166 Tcl_Interp *interp; /* Interpreter to send results back to */
167 Tk_Window tkwin; /* Not used. */
168 char *string; /* String representing pen */
169 char *widgRec; /* Widget record */
170 int offset; /* Offset of pen field in record */
171 {
172 Blt_Uid classUid = *(Blt_Uid *)clientData; /* Element type. */
173 Pen **penPtrPtr = (Pen **)(widgRec + offset);
174 Pen *penPtr;
175 Graph *graphPtr;
176
177 penPtr = NULL;
178 graphPtr = Blt_GetGraphFromWindowData(tkwin);
179
180 if (classUid == NULL) {
181 classUid = graphPtr->classUid;
182 }
183 if ((string != NULL) && (string[0] != '\0')) {
184 if (Blt_GetPen(graphPtr, string, classUid, &penPtr) != TCL_OK) {
185 return TCL_ERROR;
186 }
187 }
188 /* Release the old pen */
189 if (*penPtrPtr != NULL) {
190 Blt_FreePen(graphPtr, *penPtrPtr);
191 }
192 *penPtrPtr = penPtr;
193 return TCL_OK;
194 }
195
196 /*
197 *----------------------------------------------------------------------
198 *
199 * PenToString --
200 *
201 * Parse the name of the name.
202 *
203 * Results:
204 * The return value is a standard Tcl result.
205 *
206 *----------------------------------------------------------------------
207 */
208 /*ARGSUSED*/
209 static char *
210 PenToString(clientData, tkwin, widgRec, offset, freeProcPtr)
211 ClientData clientData; /* Not used. */
212 Tk_Window tkwin; /* Not used. */
213 char *widgRec; /* Widget information record */
214 int offset; /* Offset of pen in record */
215 Tcl_FreeProc **freeProcPtr; /* Not used. */
216 {
217 Pen *penPtr = *(Pen **)(widgRec + offset);
218
219 return penPtr->name;
220 }
221
222 /*
223 *----------------------------------------------------------------------
224 *
225 * NameToPen --
226 *
227 * Find and return the pen style from a given name.
228 *
229 * Results:
230 * A standard TCL result.
231 *
232 *----------------------------------------------------------------------
233 */
234 static Pen *
235 NameToPen(graphPtr, name)
236 Graph *graphPtr;
237 char *name;
238 {
239 Blt_HashEntry *hPtr;
240 Pen *penPtr;
241
242 hPtr = Blt_FindHashEntry(&(graphPtr->penTable), name);
243 if (hPtr == NULL) {
244 notFound:
245 Tcl_AppendResult(graphPtr->interp, "can't find pen \"", name,
246 "\" in \"", Tk_PathName(graphPtr->tkwin), "\"", (char *)NULL);
247 return NULL;
248 }
249 penPtr = (Pen *)Blt_GetHashValue(hPtr);
250 if (penPtr->flags & PEN_DELETE_PENDING) {
251 goto notFound;
252 }
253 return penPtr;
254 }
255
256 static void
257 DestroyPen(graphPtr, penPtr)
258 Graph *graphPtr;
259 Pen *penPtr;
260 {
261 Tk_FreeOptions(penPtr->configSpecs, (char *)penPtr, graphPtr->display, 0);
262 (*penPtr->destroyProc) (graphPtr, penPtr);
263 if ((penPtr->name != NULL) && (penPtr->name[0] != '\0')) {
264 Blt_Free(penPtr->name);
265 }
266 if (penPtr->hashPtr != NULL) {
267 Blt_DeleteHashEntry(&(graphPtr->penTable), penPtr->hashPtr);
268 }
269 Blt_Free(penPtr);
270 }
271
272 void
273 Blt_FreePen(graphPtr, penPtr)
274 Graph *graphPtr;
275 Pen *penPtr;
276 {
277 penPtr->refCount--;
278 if ((penPtr->refCount == 0) && (penPtr->flags & PEN_DELETE_PENDING)) {
279 DestroyPen(graphPtr, penPtr);
280 }
281 }
282
283 Pen *
284 Blt_CreatePen(graphPtr, penName, classUid, nOpts, options)
285 Graph *graphPtr;
286 char *penName;
287 Blt_Uid classUid;
288 int nOpts;
289 char **options;
290 {
291
292 Pen *penPtr;
293 Blt_HashEntry *hPtr;
294 unsigned int length, configFlags;
295 int isNew;
296 register int i;
297
298 /*
299 * Scan the option list for a "-type" entry. This will indicate
300 * what type of pen we are creating. Otherwise we'll default to the
301 * suggested type. Last -type option wins.
302 */
303 for (i = 0; i < nOpts; i += 2) {
304 length = strlen(options[i]);
305 if ((length > 2) && (strncmp(options[i], "-type", length) == 0)) {
306 char *arg;
307
308 arg = options[i + 1];
309 if (strcmp(arg, "bar") == 0) {
310 classUid = bltBarElementUid;
311 } else if (strcmp(arg, "line") != 0) {
312 classUid = bltLineElementUid;
313 } else if (strcmp(arg, "strip") != 0) {
314 classUid = bltLineElementUid;
315 } else {
316 Tcl_AppendResult(graphPtr->interp, "unknown pen type \"",
317 arg, "\" specified", (char *)NULL);
318 return NULL;
319 }
320 }
321 }
322 if (classUid == bltStripElementUid) {
323 classUid = bltLineElementUid;
324 }
325 hPtr = Blt_CreateHashEntry(&(graphPtr->penTable), penName, &isNew);
326 if (!isNew) {
327 penPtr = (Pen *)Blt_GetHashValue(hPtr);
328 if (!(penPtr->flags & PEN_DELETE_PENDING)) {
329 Tcl_AppendResult(graphPtr->interp, "pen \"", penName,
330 "\" already exists in \"", Tk_PathName(graphPtr->tkwin), "\"",
331 (char *)NULL);
332 return NULL;
333 }
334 if (penPtr->classUid != classUid) {
335 Tcl_AppendResult(graphPtr->interp, "pen \"", penName,
336 "\" in-use: can't change pen type from \"", penPtr->classUid,
337 "\" to \"", classUid, "\"", (char *)NULL);
338 return NULL;
339 }
340 penPtr->flags &= ~PEN_DELETE_PENDING;
341 } else {
342 if (classUid == bltBarElementUid) {
343 penPtr = Blt_BarPen(penName);
344 } else {
345 penPtr = Blt_LinePen(penName);
346 }
347 penPtr->classUid = classUid;
348 penPtr->hashPtr = hPtr;
349 Blt_SetHashValue(hPtr, penPtr);
350 }
351
352 configFlags = (penPtr->flags & (ACTIVE_PEN | NORMAL_PEN));
353 if (Blt_ConfigureWidgetComponent(graphPtr->interp, graphPtr->tkwin,
354 penPtr->name, "Pen", penPtr->configSpecs, nOpts, options,
355 (char *)penPtr, configFlags) != TCL_OK) {
356 if (isNew) {
357 DestroyPen(graphPtr, penPtr);
358 }
359 return NULL;
360 }
361 (*penPtr->configProc) (graphPtr, penPtr);
362 return penPtr;
363 }
364
365 int
366 Blt_GetPen(graphPtr, name, classUid, penPtrPtr)
367 Graph *graphPtr;
368 char *name;
369 Blt_Uid classUid;
370 Pen **penPtrPtr;
371 {
372 Pen *penPtr;
373
374 penPtr = NameToPen(graphPtr, name);
375 if (penPtr == NULL) {
376 return TCL_ERROR;
377 }
378 if (classUid == bltStripElementUid) {
379 classUid = bltLineElementUid;
380 }
381 if (penPtr->classUid != classUid) {
382 Tcl_AppendResult(graphPtr->interp, "pen \"", name,
383 "\" is the wrong type (is \"", penPtr->classUid, "\"",
384 ", wanted \"", classUid, "\")", (char *)NULL);
385 return TCL_ERROR;
386 }
387 penPtr->refCount++;
388 *penPtrPtr = penPtr;
389 return TCL_OK;
390 }
391
392 /*
393 *----------------------------------------------------------------------
394 *
395 * Blt_DestroyPens --
396 *
397 * Release memory and resources allocated for the style.
398 *
399 * Results:
400 * None.
401 *
402 * Side effects:
403 * Everything associated with the pen style is freed up.
404 *
405 *----------------------------------------------------------------------
406 */
407 void
408 Blt_DestroyPens(graphPtr)
409 Graph *graphPtr;
410 {
411 Blt_HashEntry *hPtr;
412 Blt_HashSearch cursor;
413 Pen *penPtr;
414
415 for (hPtr = Blt_FirstHashEntry(&(graphPtr->penTable), &cursor);
416 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
417 penPtr = (Pen *)Blt_GetHashValue(hPtr);
418 penPtr->hashPtr = NULL;
419 DestroyPen(graphPtr, penPtr);
420 }
421 Blt_DeleteHashTable(&(graphPtr->penTable));
422 }
423
424 /*
425 * ----------------------------------------------------------------------
426 *
427 * CgetOp --
428 *
429 * Queries axis attributes (font, line width, label, etc).
430 *
431 * Results:
432 * A standard Tcl result. If querying configuration values,
433 * interp->result will contain the results.
434 *
435 * ----------------------------------------------------------------------
436 */
437 /* ARGSUSED */
438 static int
439 CgetOp(interp, graphPtr, argc, argv)
440 Tcl_Interp *interp;
441 Graph *graphPtr;
442 int argc; /* Not used. */
443 char *argv[];
444 {
445 Pen *penPtr;
446 unsigned int configFlags;
447
448 penPtr = NameToPen(graphPtr, argv[3]);
449 if (penPtr == NULL) {
450 return TCL_ERROR;
451 }
452 configFlags = (penPtr->flags & (ACTIVE_PEN | NORMAL_PEN));
453 return Tk_ConfigureValue(interp, graphPtr->tkwin, penPtr->configSpecs,
454 (char *)penPtr, argv[4], configFlags);
455 }
456
457 /*
458 * ----------------------------------------------------------------------
459 *
460 * ConfigureOp --
461 *
462 * Queries or resets pen attributes (font, line width, color, etc).
463 *
464 * Results:
465 * A standard Tcl result. If querying configuration values,
466 * interp->result will contain the results.
467 *
468 * Side Effects:
469 * Pen resources are possibly allocated (GC, font).
470 *
471 * ----------------------------------------------------------------------
472 */
473 static int
474 ConfigureOp(interp, graphPtr, argc, argv)
475 Tcl_Interp *interp;
476 Graph *graphPtr;
477 int argc;
478 char *argv[];
479 {
480 int flags;
481 Pen *penPtr;
482 int nNames, nOpts;
483 int redraw;
484 char **options;
485 register int i;
486
487 /* Figure out where the option value pairs begin */
488 argc -= 3;
489 argv += 3;
490 for (i = 0; i < argc; i++) {
491 if (argv[i][0] == '-') {
492 break;
493 }
494 if (NameToPen(graphPtr, argv[i]) == NULL) {
495 return TCL_ERROR;
496 }
497 }
498 nNames = i; /* Number of pen names specified */
499 nOpts = argc - i; /* Number of options specified */
500 options = argv + i; /* Start of options in argv */
501
502 redraw = 0;
503 for (i = 0; i < nNames; i++) {
504 penPtr = NameToPen(graphPtr, argv[i]);
505 flags = TK_CONFIG_ARGV_ONLY | (penPtr->flags & (ACTIVE_PEN|NORMAL_PEN));
506 if (nOpts == 0) {
507 return Tk_ConfigureInfo(interp, graphPtr->tkwin,
508 penPtr->configSpecs, (char *)penPtr, (char *)NULL, flags);
509 } else if (nOpts == 1) {
510 return Tk_ConfigureInfo(interp, graphPtr->tkwin,
511 penPtr->configSpecs, (char *)penPtr, options[0], flags);
512 }
513 if (Tk_ConfigureWidget(interp, graphPtr->tkwin, penPtr->configSpecs,
514 nOpts, options, (char *)penPtr, flags) != TCL_OK) {
515 break;
516 }
517 (*penPtr->configProc) (graphPtr, penPtr);
518 if (penPtr->refCount > 0) {
519 redraw++;
520 }
521 }
522 if (redraw) {
523 graphPtr->flags |= REDRAW_BACKING_STORE | DRAW_MARGINS;
524 Blt_EventuallyRedrawGraph(graphPtr);
525 }
526 if (i < nNames) {
527 return TCL_ERROR;
528 }
529 return TCL_OK;
530 }
531
532 /*
533 *----------------------------------------------------------------------
534 *
535 * CreateOp --
536 *
537 * Adds a new penstyle to the graph.
538 *
539 * Results:
540 * A standard Tcl result.
541 *
542 *----------------------------------------------------------------------
543 */
544 static int
545 CreateOp(interp, graphPtr, argc, argv)
546 Tcl_Interp *interp;
547 Graph *graphPtr;
548 int argc;
549 char **argv;
550 {
551 Pen *penPtr;
552
553 penPtr = Blt_CreatePen(graphPtr, argv[3], graphPtr->classUid, argc - 4,
554 argv + 4);
555 if (penPtr == NULL) {
556 return TCL_ERROR;
557 }
558 Tcl_SetResult(interp, penPtr->name, TCL_VOLATILE);
559 return TCL_OK;
560 }
561
562 /*
563 *--------------------------------------------------------------
564 *
565 * DeleteOp --
566 *
567 * Delete the given pen.
568 *
569 * Results:
570 * Always returns TCL_OK. The interp->result field is
571 * a list of the graph axis limits.
572 *
573 *--------------------------------------------------------------
574 */
575
576 /*ARGSUSED*/
577 static int
578 DeleteOp(interp, graphPtr, argc, argv)
579 Tcl_Interp *interp;
580 Graph *graphPtr;
581 int argc;
582 char **argv;
583 {
584 Pen *penPtr;
585 int i;
586
587 for (i = 3; i < argc; i++) {
588 penPtr = NameToPen(graphPtr, argv[i]);
589 if (penPtr == NULL) {
590 return TCL_ERROR;
591 }
592 if (penPtr->flags & PEN_DELETE_PENDING) {
593 Tcl_AppendResult(graphPtr->interp, "can't find pen \"", argv[i],
594 "\" in \"", Tk_PathName(graphPtr->tkwin), "\"", (char *)NULL);
595 return TCL_ERROR;
596 }
597 penPtr->flags |= PEN_DELETE_PENDING;
598 if (penPtr->refCount == 0) {
599 DestroyPen(graphPtr, penPtr);
600 }
601 }
602 return TCL_OK;
603 }
604
605 /*
606 * ----------------------------------------------------------------------
607 *
608 * NamesOp --
609 *
610 * Return a list of the names of all the axes.
611 *
612 * Results:
613 * Returns a standard Tcl result.
614 *
615 * ----------------------------------------------------------------------
616 */
617 /*ARGSUSED*/
618 static int
619 NamesOp(interp, graphPtr, argc, argv)
620 Tcl_Interp *interp;
621 Graph *graphPtr;
622 int argc;
623 char **argv;
624 {
625 Blt_HashSearch cursor;
626 Pen *penPtr;
627 register int i;
628 register Blt_HashEntry *hPtr;
629
630 for (hPtr = Blt_FirstHashEntry(&(graphPtr->penTable), &cursor);
631 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
632 penPtr = (Pen *)Blt_GetHashValue(hPtr);
633 if (penPtr->flags & PEN_DELETE_PENDING) {
634 continue;
635 }
636 if (argc == 3) {
637 Tcl_AppendElement(interp, penPtr->name);
638 continue;
639 }
640 for (i = 3; i < argc; i++) {
641 if (Tcl_StringMatch(penPtr->name, argv[i])) {
642 Tcl_AppendElement(interp, penPtr->name);
643 break;
644 }
645 }
646 }
647 return TCL_OK;
648 }
649
650 /*
651 * ----------------------------------------------------------------------
652 *
653 * TypeOp --
654 *
655 * Return the type of pen.
656 *
657 * Results:
658 * Returns a standard Tcl result.
659 *
660 * ----------------------------------------------------------------------
661 */
662 /*ARGSUSED*/
663 static int
664 TypeOp(interp, graphPtr, argc, argv)
665 Tcl_Interp *interp;
666 Graph *graphPtr;
667 int argc;
668 char **argv;
669 {
670 Pen *penPtr;
671
672 penPtr = NameToPen(graphPtr, argv[3]);
673 if (penPtr == NULL) {
674 return TCL_ERROR;
675 }
676 Tcl_SetResult(interp, penPtr->classUid, TCL_STATIC);
677 return TCL_OK;
678 }
679
680 static Blt_OpSpec penOps[] =
681 {
682 {"cget", 2, (Blt_Op)CgetOp, 5, 5, "penName option",},
683 {"configure", 2, (Blt_Op)ConfigureOp, 4, 0,
684 "penName ?penName?... ?option value?...",},
685 {"create", 2, (Blt_Op)CreateOp, 4, 0, "penName ?option value?...",},
686 {"delete", 2, (Blt_Op)DeleteOp, 3, 0, "?penName?...",},
687 {"names", 1, (Blt_Op)NamesOp, 3, 0, "?pattern?...",},
688 {"type", 1, (Blt_Op)TypeOp, 4, 4, "penName",},
689 };
690 static int nPenOps = sizeof(penOps) / sizeof(Blt_OpSpec);
691
692 int
693 Blt_PenOp(graphPtr, interp, argc, argv)
694 Graph *graphPtr;
695 Tcl_Interp *interp;
696 int argc;
697 char **argv;
698 {
699 Blt_Op proc;
700
701 proc = Blt_GetOp(interp, nPenOps, penOps, BLT_OP_ARG2, argc, argv, 0);
702 if (proc == NULL) {
703 return TCL_ERROR;
704 }
705 return (*proc) (interp, graphPtr, argc, argv);
706 }