Debian Codegraph @

pTk/mTk/tixGeneric/tixDItem.c

raw symbols SCIP
1
2 /* $Id: tixDItem.c,v 1.1.1.1 2000/05/17 11:08:38 idiscovery Exp $ */
3
4 /*
5 * tixDItem.c --
6 *
7 * This file implements the "Display Items" in the Tix library.
8 *
9 * Since many Tix widgets use the same type of display items, for
10 * example, text items, image items, or text-image items (used in
11 * HList, TList and Table), it makes sense to provide a set of
12 * common routines to support these display items. Code re-use is
13 * the major issue: we don't want to re-define almost the same
14 * configSpecs again and again in different widgets. Therefore,
15 * all display items provide common methods to configure,
16 * display, calculate geometry, etc.
17 *
18 *
19 * Copyright (c) 1996, Expert Interface Technologies
20 *
21 * See the file "license.terms" for information on usage and redistribution
22 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
23 *
24 */
25
26 #include "tixPort.h"
27 #include "tix.h"
28 #include "tixInt.h"
29
30 #ifdef _LANG
31 #define FORWARD extern
32 #define LINKAGE
33 #define DItemParseProc TixDItemParseProc
34 #define DItemPrintProc TixDItemPrintProc
35 #else
36 #define FORWARD static
37 #define LINKAGE static
38 #endif
39
40 FORWARD int DItemParseProc _ANSI_ARGS_((ClientData clientData,
41 Tcl_Interp *interp, Tk_Window tkwin, Tcl_Obj * value,
42 char *widRec, int offset));
43
44 FORWARD Tcl_Obj * DItemPrintProc _ANSI_ARGS_((
45 ClientData clientData, Tk_Window tkwin, char *widRec,
46 int offset, Tcl_FreeProc **freeProcPtr));
47
48 /*----------------------------------------------------------------------
49 *
50 *
51 * PUBLIC INTERFACE
52 *
53 *
54 * The following functions are called by widget implementors
55 *
56 *----------------------------------------------------------------------
57 */
58 /* Tix_AddDItemType, Tix_GetDItemType --
59 *
60 *
61 * Maintain a list of item types, each identifies by a unique string
62 * name;
63 */
64 static Tix_DItemInfo * diTypes = NULL;
65
66 void Tix_AddDItemType(diTypePtr)
67 Tix_DItemInfo * diTypePtr;
68 {
69 diTypePtr->next = diTypes;
70 diTypes = diTypePtr;
71 }
72
73 Tix_DItemInfo * Tix_GetDItemType(interp, type)
74 Tcl_Interp * interp;
75 char * type;
76 {
77 Tix_DItemInfo * diTypePtr;
78
79 for (diTypePtr = diTypes; diTypePtr; diTypePtr = diTypePtr->next) {
80 if (strcmp(type,diTypePtr->name)==0) {
81 return diTypePtr;
82 }
83 }
84
85 if (interp) {
86 Tcl_AppendResult(interp, "unknown display type \"", type, "\"", NULL);
87 }
88 return NULL;
89 }
90
91
92 /*----------------------------------------------------------------------
93 * Tix_DItemCreate --
94 *
95 * Create a display item according to the "type" string.
96 *
97 *----------------------------------------------------------------------
98 */
99 Tix_DItem * Tix_DItemCreate(ddPtr, type)
100 Tix_DispData * ddPtr;
101 char * type;
102 {
103 Tix_DItemInfo * diTypePtr;
104
105 if ((diTypePtr = Tix_GetDItemType(ddPtr->interp, type)) == NULL) {
106 return NULL;
107 }
108
109 return diTypePtr->createProc(ddPtr, diTypePtr);
110 }
111
112 /*----------------------------------------------------------------------
113 * Tix_DItemConfigure --
114 *
115 * Configures a display item.
116 *
117 *----------------------------------------------------------------------
118 */
119 int Tix_DItemConfigure(iPtr, argc, argv, flags)
120 Tix_DItem * iPtr;
121 int argc;
122 char ** argv;
123 int flags;
124 {
125 return iPtr->base.diTypePtr->configureProc(iPtr, argc, argv, flags);
126 }
127
128
129 void Tix_DItemDisplay(pixmap, gc, iPtr, x, y, width, height, flags)
130 Pixmap pixmap;
131 GC gc;
132 Tix_DItem * iPtr;
133 int x;
134 int y;
135 int width;
136 int height;
137 int flags;
138 {
139 iPtr->base.diTypePtr->displayProc(pixmap, gc, iPtr, x, y,
140 width, height, flags);
141 }
142
143 void Tix_DItemFree(iPtr)
144 Tix_DItem * iPtr;
145 {
146 iPtr->base.diTypePtr->freeProc(iPtr);
147
148 /*
149 * When it comes to here, iPtr is no longer a valid pointer!
150 */
151 }
152
153 void Tix_DItemCalculateSize(iPtr)
154 Tix_DItem * iPtr;
155 {
156 iPtr->base.diTypePtr->calculateSizeProc(iPtr);
157 }
158
159 char * Tix_DItemComponent(iPtr, x, y)
160 Tix_DItem * iPtr;
161 int x;
162 int y;
163 {
164 return (iPtr->base.diTypePtr->componentProc(iPtr, x, y));
165 }
166
167
168 /*----------------------------------------------------------------------
169 * Tix_FreeArgumentList --
170 *
171 * Free the argument lists allocated by Tix_SplitConfig;
172 *----------------------------------------------------------------------
173 */
174 void
175 Tix_FreeArgumentList(argListPtr)
176 Tix_ArgumentList *argListPtr;
177 {
178 int i;
179
180 for (i=0; i<argListPtr->numLists; i++) {
181 ckfree((char*)argListPtr->arg[i].argv);
182 }
183 if (argListPtr->arg != argListPtr->preAlloc) {
184 ckfree((char*)argListPtr->arg);
185 }
186 }
187
188 /*----------------------------------------------------------------------
189 * Tix_SplitConfig --
190 *
191 * Split the command line arguments according for several configurable
192 * objects.
193 *----------------------------------------------------------------------
194 */
195 int
196 Tix_SplitConfig(interp, tkwin, specsList, numLists, argc, argv, argListPtr)
197 Tcl_Interp * interp;
198 Tk_Window tkwin;
199 Tk_ConfigSpec ** specsList; /* a list of two or more config spec
200 * arrays */
201 int numLists;
202 int argc;
203 char ** argv;
204 Tix_ArgumentList * argListPtr;
205 {
206 Tix_Argument *arg;
207 int i, n, code = TCL_OK;
208 Tk_ConfigSpec *specPtr;
209 size_t len;
210 int found;
211
212 if (argc % 2) {
213 Tcl_AppendResult(interp, "value for \"", argv[argc-1],
214 "\" missing", (char *) NULL);
215 return TCL_ERROR;
216 }
217
218 if (numLists > FIXED_SIZE) {
219 arg = (Tix_Argument*)ckalloc(numLists * sizeof(Tix_Argument));
220 } else {
221 arg = argListPtr->preAlloc;
222 }
223 argListPtr->arg = arg;
224 argListPtr->numLists = numLists;
225 for (i=0; i<numLists; i++) {
226 arg[i].argc = 0;
227 arg[i].objv = (Tcl_Obj **)ckalloc(argc * sizeof(Tcl_Obj *));
228 }
229
230 /* Split the arguments for the appropriate objects */
231 for (n=0; n<argc; n+=2) {
232 len = strlen(argv[n]);
233 found = 0;
234
235 for (i=0; i<numLists; i++) {
236 for (specPtr=specsList[i];
237 specPtr->type != TK_CONFIG_END;
238 specPtr++) {
239
240 if (specPtr->argvName == NULL) {
241 continue;
242 }
243
244 if (strncmp(argv[n], specPtr->argvName, len) == 0) {
245 arg[i].objv[arg[i].argc++] = objv[n ];
246 arg[i].objv[arg[i].argc++] = objv[n+1];
247 found = 1;
248 break;
249 }
250 }
251 }
252 if (found == 0) {
253 Tcl_AppendResult(interp, "unknown option \"", argv[n],
254 "\"", (char *) NULL);
255 code = TCL_ERROR;
256 goto done;
257 }
258 }
259
260 done:
261 if (code == TCL_ERROR) {
262 Tix_FreeArgumentList(argListPtr);
263 }
264 return code;
265 }
266
267 int
268 Tix_MultiConfigureInfo(interp, tkwin, specsList, numLists, widgRecList,
269 argvName, flags, request)
270 Tcl_Interp *interp; /* Interpreter for error reporting. */
271 Tk_Window tkwin; /* Window corresponding to widgRec. */
272 Tk_ConfigSpec **specsList; /* Describes legal options. */
273 int numLists;
274 char **widgRecList; /* Record whose fields contain current
275 * values for options. */
276 char *argvName; /* If non-NULL, indicates a single option
277 * whose info is to be returned. Otherwise
278 * info is returned for all options. */
279 int flags; /* Used to specify additional flags
280 * that must be present in config specs
281 * for them to be considered. */
282 int request;
283 {
284 int i, found;
285 Tk_ConfigSpec *specPtr;
286 size_t len;
287
288 if (argvName != NULL) {
289 len = strlen(argvName);
290 for (found=0,i=0; i<numLists; i++) {
291 for (specPtr=specsList[i];
292 specPtr->type != TK_CONFIG_END;
293 specPtr++) {
294
295 if (specPtr->argvName == NULL) {
296 continue;
297 }
298
299 if (strncmp(argvName, specPtr->argvName, len) == 0) {
300 found = 1;
301 goto done;
302 }
303 }
304 }
305 done:
306 if (!found) {
307 Tcl_AppendResult(interp, "unknown option \"", argvName,
308 "\"", (char *) NULL);
309 return TCL_ERROR;
310 }
311 if (request == TIX_CONFIG_INFO) {
312 if (widgRecList[i] != NULL) {
313 return Tk_ConfigureInfo(interp, tkwin, specsList[i],
314 widgRecList[i], argvName, flags);
315 } else {
316 return TCL_OK;
317 }
318 } else {
319 if (widgRecList[i] != NULL) {
320 return Tk_ConfigureValue(interp, tkwin, specsList[i],
321 widgRecList[i], argvName, flags);
322 } else {
323 return TCL_OK;
324 }
325 }
326 }
327
328 Tcl_ResetResult(interp);
329 for (i=0; i<numLists; i++) {
330 if (widgRecList[i] != NULL) {
331 Tk_ConfigureInfo(interp, tkwin, specsList[i], widgRecList[i],
332 NULL, flags);
333 /* FIXME - check error code ?? */
334 }
335 }
336 return TCL_OK;
337 }
338
339 /*----------------------------------------------------------------------
340 * Tix_ConfigureValue2 --
341 *
342 *
343 * Returns the config information of a entry element (of an HList,
344 * for example) and its display item.
345 *----------------------------------------------------------------------
346 */
347 int
348 Tix_ConfigureValue2(interp, tkwin, entRec, entConfigSpecs, iPtr,
349 argvName, flags)
350 Tcl_Interp *interp; /* Interpreter for error reporting. */
351 Tk_Window tkwin; /* Window corresponding to widgRec. */
352 char * entRec;
353 Tk_ConfigSpec *entConfigSpecs; /* Describes legal options of the entry */
354 Tix_DItem * iPtr; /* points to the entry's data record */
355 char *argvName; /* If non-NULL, indicates a single option
356 * whose info is to be returned. Otherwise
357 * info is returned for all options. */
358 int flags; /* Used to specify additional flags
359 * that must be present in config specs
360 * for them to be considered. */
361 {
362 Tk_ConfigSpec *specsList[2];
363 char *widgRecList[2];
364
365 specsList[0] = entConfigSpecs;
366 specsList[1] = Tix_DItemConfigSpecs(iPtr);
367 widgRecList[1] = (char*)iPtr;
368 widgRecList[0] = (char*)entRec;
369
370 return Tix_MultiConfigureInfo(interp, tkwin, specsList, 2, widgRecList,
371 argvName, flags, TIX_CONFIG_VALUE);
372 }
373
374 /*----------------------------------------------------------------------
375 * Tix_ConfigureInfo2 --
376 *
377 *
378 * Returns the config information of a entry element (of an HList,
379 * for example) and its display item.
380 *----------------------------------------------------------------------
381 */
382 int
383 Tix_ConfigureInfo2(interp, tkwin, entRec, entConfigSpecs, iPtr,
384 argvName, flags)
385 Tcl_Interp *interp; /* Interpreter for error reporting. */
386 Tk_Window tkwin; /* Window corresponding to widgRec. */
387 char * entRec;
388 Tk_ConfigSpec *entConfigSpecs; /* Describes legal options of the entry */
389 Tix_DItem * iPtr; /* points to the entry's data record */
390 char *argvName; /* If non-NULL, indicates a single option
391 * whose info is to be returned. Otherwise
392 * info is returned for all options. */
393 int flags; /* Used to specify additional flags
394 * that must be present in config specs
395 * for them to be considered. */
396 {
397 Tk_ConfigSpec *specsList[2];
398 char *widgRecList[2];
399
400 specsList[0] = entConfigSpecs;
401 specsList[1] = Tix_DItemConfigSpecs(iPtr);
402 widgRecList[1] = (char*)iPtr;
403 widgRecList[0] = (char*)entRec;
404
405 return Tix_MultiConfigureInfo(interp, tkwin, specsList, 2, widgRecList,
406 argvName, flags, TIX_CONFIG_INFO);
407 }
408
409 int
410 Tix_WidgetConfigure2(interp, tkwin, entRec, entConfigSpecs, iPtr,
411 argc, argv, flags, forced, sizeChanged_ret)
412 Tcl_Interp *interp; /* Interpreter for error reporting. */
413 Tk_Window tkwin; /* Window corresponding to widgRec. */
414 char * entRec;
415 Tk_ConfigSpec *entConfigSpecs; /* Describes legal options of the entry */
416 Tix_DItem * iPtr; /* points to the entry's data record */
417 int argc;
418 char ** argv;
419 int flags;
420 int forced; /* forced configure of DItem? */
421 int * sizeChanged_ret;
422 {
423 Tix_ArgumentList argList;
424 Tk_ConfigSpec *specsList[2];
425 char *widgRecList[2];
426 int code = TCL_OK;
427 int dummy;
428
429 if (sizeChanged_ret == NULL) {
430 sizeChanged_ret = &dummy;
431 }
432
433 specsList[0] = entConfigSpecs;
434 specsList[1] = Tix_DItemConfigSpecs(iPtr);
435 widgRecList[0] = (char*)entRec;
436 widgRecList[1] = (char*)iPtr;
437
438 if (Tix_SplitConfig(interp, tkwin, specsList,
439 2, argc, argv, &argList) != TCL_OK) {
440 return TCL_ERROR;
441 }
442
443 /* Handle the info specific to the entry */
444 if (argList.arg[0].argc > 0) {
445 if (Tk_ConfigureWidget(interp, tkwin,
446 entConfigSpecs, argList.arg[0].argc, argList.arg[0].argv,
447 (char*)entRec, flags) != TCL_OK) {
448
449 code = TCL_ERROR;
450 goto done;
451 }
452 }
453
454 if (iPtr == NULL) {
455 goto done;
456 }
457 if (argList.arg[1].argc > 0 || forced) {
458 int oldSize[2];
459 oldSize[0] = iPtr->base.size[0];
460 oldSize[1] = iPtr->base.size[1];
461 if (Tix_DItemConfigure(iPtr, argList.arg[1].argc,
462 argList.arg[1].argv, flags) != TCL_OK) {
463 code = TCL_ERROR;
464 goto done;
465 }
466
467 if (oldSize[0] != iPtr->base.size[0] ||
468 oldSize[1] != iPtr->base.size[1]) {
469 * sizeChanged_ret = 1;
470 } else {
471 * sizeChanged_ret = 0;
472 }
473 }
474
475 done:
476
477 Tix_FreeArgumentList(&argList);
478 return code;
479 }
480
481 /*----------------------------------------------------------------------
482 *
483 * The Tix Customed Config Options
484 *
485 *----------------------------------------------------------------------
486 */
487 /*
488 * The global data structures to use in widget configSpecs arrays
489 *
490 * These are declared in <tixConfig.h>
491 */
492
493 #ifndef _LANG
494 Tk_CustomOption tixConfigItemType = {
495 DItemParseProc, DItemPrintProc, 0,
496 };
497 #endif
498
499 /*----------------------------------------------------------------------
500 * DItemParseProc --
501 *
502 * Parse the text string and store the Tix_DItemType information
503 * inside the widget record.
504 *----------------------------------------------------------------------
505 */
506 LINKAGE int
507 DItemParseProc(clientData, interp, tkwin, value, widRec,offset)
508 ClientData clientData;
509 Tcl_Interp *interp;
510 Tk_Window tkwin;
511 Tcl_Obj * value;
512 char *widRec;
513 int offset;
514 {
515 Tix_DItemInfo *newPtr;
516 Tix_DItemInfo **ptr = (Tix_DItemInfo **)(widRec + offset);
517
518 if (value == NULL) {
519 newPtr = NULL;
520 } else {
521 newPtr = Tix_GetDItemType(interp, Tcl_GetString(value));
522 if (newPtr == NULL) {
523 return TCL_ERROR;
524 }
525 }
526 *ptr = newPtr;
527
528 return TCL_OK;
529 }
530
531 LINKAGE Tcl_Obj *
532 DItemPrintProc(clientData, tkwin, widRec,offset, freeProcPtr)
533 ClientData clientData;
534 Tk_Window tkwin;
535 char *widRec;
536 int offset;
537 Tcl_FreeProc **freeProcPtr;
538 {
539 Tix_DItemInfo *diTypePtr = *((Tix_DItemInfo**)(widRec+offset));
540
541 if (diTypePtr != NULL) {
542 return LangStringArg(diTypePtr->name);
543 } else {
544 return 0;
545 }
546 }
547 /*----------------------------------------------------------------------
548 *
549 *
550 * PRIVATE INTERFACE
551 *
552 *
553 * The following functions are called by display type implementors
554 *
555 *----------------------------------------------------------------------
556 */
557
558 /* The priority is selected > disabled > active > normal */
559
560 void TixGetColorDItemGC(iPtr, backGC_ret, foreGC_ret, flags)
561 Tix_DItem * iPtr;
562 GC * backGC_ret;
563 GC * foreGC_ret;
564 int flags;
565 {
566 TixColorStyle * stylePtr = (TixColorStyle *) iPtr->base.stylePtr;
567
568 if (flags & TIX_DITEM_SELECTED_FG) {
569 *foreGC_ret = stylePtr->colors[TIX_DITEM_SELECTED].foreGC;
570 }
571 else if (flags & TIX_DITEM_DISABLED_FG) {
572 *foreGC_ret = stylePtr->colors[TIX_DITEM_DISABLED].foreGC;
573 }
574 else if (flags & TIX_DITEM_ACTIVE_FG) {
575 *foreGC_ret = stylePtr->colors[TIX_DITEM_ACTIVE].foreGC;
576 }
577 else if (flags & TIX_DITEM_NORMAL_FG) {
578 *foreGC_ret = stylePtr->colors[TIX_DITEM_NORMAL].foreGC;
579 }
580 else {
581 *foreGC_ret = None;
582 }
583
584 if (flags & TIX_DITEM_SELECTED_BG) {
585 *backGC_ret = stylePtr->colors[TIX_DITEM_SELECTED].backGC;
586 }
587 else if (flags & TIX_DITEM_DISABLED_BG) {
588 *backGC_ret = stylePtr->colors[TIX_DITEM_DISABLED].backGC;
589 }
590 else if (flags & TIX_DITEM_ACTIVE_BG) {
591 *backGC_ret = stylePtr->colors[TIX_DITEM_ACTIVE].backGC;
592 }
593 else if (flags & TIX_DITEM_NORMAL_BG) {
594 *backGC_ret = stylePtr->colors[TIX_DITEM_NORMAL].backGC;
595 }
596 else {
597 *backGC_ret = None;
598 }
599 }
600
601 /*----------------------------------------------------------------------
602 * TixDItemGetAnchor --
603 *
604 * Calculate the position of the element according to its anchor
605 *----------------------------------------------------------------------
606 */
607 void
608 TixDItemGetAnchor(anchor, x, y, cav_w, cav_h, width, height, x_ret, y_ret)
609 Tk_Anchor anchor;
610 int x;
611 int y;
612 int cav_w;
613 int cav_h;
614 int width;
615 int height;
616 int * x_ret;
617 int * y_ret;
618 {
619 if (width > cav_w) {
620 * x_ret = x;
621 } else {
622 int rem = cav_w - width;
623
624 switch (anchor) {
625 case TK_ANCHOR_NW: case TK_ANCHOR_W: case TK_ANCHOR_SW:
626 * x_ret = x;
627 break;
628 case TK_ANCHOR_N: case TK_ANCHOR_CENTER: case TK_ANCHOR_S:
629 * x_ret = x + rem/2;
630 break;
631 default:
632 * x_ret = x + rem;
633 }
634 }
635 if (height > cav_h) {
636 * y_ret = y;
637 }
638 else {
639 int rem = cav_h - height;
640 switch (anchor) {
641 case TK_ANCHOR_NW: case TK_ANCHOR_N: case TK_ANCHOR_NE:
642 * y_ret = y;
643 break;
644 case TK_ANCHOR_W: case TK_ANCHOR_CENTER: case TK_ANCHOR_E:
645 * y_ret = y + rem/2;
646 if ((rem % 2) == 1) {
647 /* Usually it looks better if we shift down one pixel
648 * if the hight of the region is an odd number of pixels
649 */
650 * y_ret += 1;
651 }
652 break;
653 default:
654 * y_ret = y + rem;
655 }
656 }
657 }
658
659 void Tix_DItemDrawBackground(pixmap, gc, iPtr, x, y, width, height, flags)
660 Pixmap pixmap;
661 GC gc;
662 Tix_DItem * iPtr;
663 int x;
664 int y;
665 int width;
666 int height;
667 int flags;
668 {
669 GC foreGC, backGC;
670
671 switch Tix_DItemType(iPtr) {
672 case TIX_DITEM_WINDOW:
673 case TIX_DITEM_NONE:
674 /* not a colored item */
675 return;
676 }
677
678 TixGetColorDItemGC(iPtr, &backGC, &foreGC, flags);
679
680 if (backGC != None) {
681 /* Draw the background */
682 XFillRectangle(iPtr->base.ddPtr->display, pixmap,
683 backGC,
684 x, y, width, height);
685 }
686 }