Debian Codegraph @

pTk/mTk/tixGeneric/tixGrData.c

raw symbols SCIP
1
2 /* $Id: tixGrData.c,v 1.1.1.1 2000/05/17 11:08:42 idiscovery Exp $ */
3
4 /*
5 * tixGrData.c --
6 *
7 * This module manipulates the data structure for a Grid widget.
8 *
9 * Copyright (c) 1996, Expert Interface Technologies
10 *
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 */
15
16 #include "tixPort.h"
17 #include "tixInt.h"
18 #include "tixGrid.h"
19
20 /* need "(unsigned int)" to prevent sign-extension on 64-bit machines, and
21 * "(unsigned long)" to avoid an egcs warning
22 */
23 #define FIX(X) ((char*)(unsigned long)(unsigned int)(X))
24
25 static int FindRowCol _ANSI_ARGS_((TixGridDataSet * dataSet,
26 int x, int y, TixGridRowCol * rowcol[2],
27 Tcl_HashEntry * hashPtrs[2]));
28 static TixGridRowCol * InitRowCol _ANSI_ARGS_((int index));
29 static int RowColMaxSize _ANSI_ARGS_((WidgetPtr wPtr,
30 int which, TixGridRowCol *rowCol,
31 TixGridSize * defSize));
32
33 static TixGridRowCol *
34 InitRowCol(index)
35 int index;
36 {
37 TixGridRowCol * rowCol = (TixGridRowCol *)ckalloc(sizeof(TixGridRowCol));
38
39 rowCol->dispIndex = index;
40 rowCol->size.sizeType = TIX_GR_DEFAULT;
41 rowCol->size.sizeValue = 0;
42 rowCol->size.charValue = 0;
43 rowCol->size.pad0 = 2;
44 rowCol->size.pad1 = 2;
45 rowCol->size.pixels = 0;
46
47 Tcl_InitHashTable(&rowCol->table, TCL_ONE_WORD_KEYS);
48
49 return rowCol;
50 }
51
52 /*----------------------------------------------------------------------
53 * TixGridDataSetInit --
54 *
55 * Create an instance of the TixGridDataSet data structure.
56 *
57 *----------------------------------------------------------------------
58 */
59 TixGridDataSet *
60 TixGridDataSetInit()
61 {
62 TixGridDataSet * dataSet =(TixGridDataSet*)ckalloc(sizeof(TixGridDataSet));
63
64 Tcl_InitHashTable(&dataSet->index[0], TCL_ONE_WORD_KEYS);
65 Tcl_InitHashTable(&dataSet->index[1], TCL_ONE_WORD_KEYS);
66
67 dataSet->maxIdx[0] = -1;
68 dataSet->maxIdx[1] = -1;
69
70 return dataSet;
71 }
72
73 /*----------------------------------------------------------------------
74 * TixGridDataSetFree --
75 *
76 * Frees an instance of the TixGridDataSet data structure.
77 *
78 *----------------------------------------------------------------------
79 */
80
81 void
82 TixGridDataSetFree(dataSet)
83 TixGridDataSet* dataSet;
84 {
85 Tcl_HashSearch hashSearch;
86 Tcl_HashEntry *hashPtr;
87 TixGridRowCol *rcPtr;
88 int i;
89
90 for (i=0; i<2; i++) {
91 for (hashPtr = Tcl_FirstHashEntry(&dataSet->index[i], &hashSearch);
92 hashPtr;
93 hashPtr = Tcl_NextHashEntry(&hashSearch)) {
94 rcPtr = (TixGridRowCol *)Tcl_GetHashValue(hashPtr);
95 if (rcPtr->table.numEntries > 0) {
96 #if 0
97 fprintf(stderr, "Grid hash entry leaked: %d : %d\n", i,
98 rcPtr->dispIndex);
99 #endif
100 }
101
102 Tcl_DeleteHashTable(&rcPtr->table);
103 ckfree((char*)rcPtr);
104 }
105 }
106
107 Tcl_DeleteHashTable(&dataSet->index[0]);
108 Tcl_DeleteHashTable(&dataSet->index[1]);
109 ckfree((char*)dataSet);
110 }
111
112 /*----------------------------------------------------------------------
113 * TixGridDataFindEntry --
114 *
115 * Results:
116 * Return the element if it exists. Otherwise returns NULL.
117 *
118 * Side effects:
119 * None.
120 *----------------------------------------------------------------------
121 */
122
123 char *
124 TixGridDataFindEntry(dataSet, x, y)
125 TixGridDataSet * dataSet;
126 int x;
127 int y;
128 {
129 TixGridRowCol *col, *row;
130 Tcl_HashEntry *hashPtr;
131
132 /* (1) Find the row and column */
133 if (!(hashPtr = Tcl_FindHashEntry(&dataSet->index[0], FIX(x)))) {
134 return NULL;
135 }
136 col = (TixGridRowCol *)Tcl_GetHashValue(hashPtr);
137
138 if (!(hashPtr = Tcl_FindHashEntry(&dataSet->index[1], FIX(y)))) {
139 return NULL;
140 }
141 row = (TixGridRowCol *)Tcl_GetHashValue(hashPtr);
142
143 /* (2) Find the entry */
144 if (row->table.numEntries < col->table.numEntries) {
145 if (!(hashPtr = Tcl_FindHashEntry(&row->table, (char*)col))) {
146 return NULL;
147 }
148 }
149 else {
150 if (!(hashPtr = Tcl_FindHashEntry(&col->table, (char*)row))) {
151 return NULL;
152 }
153 }
154
155 return (char *)Tcl_GetHashValue(hashPtr);
156 }
157
158 /*----------------------------------------------------------------------
159 * FindRowCol --
160 *
161 * Internal function: finds row and column info an entry.
162 *
163 * Results:
164 * Returns true if BOTH row and column exist. If so, the row and
165 * column info is returned in the rowcol.
166 *
167 * Side effects:
168 * None.
169 *----------------------------------------------------------------------
170 */
171
172 static int
173 FindRowCol(dataSet, x, y, rowcol, hashPtrs)
174 TixGridDataSet * dataSet; /* The Grid dataset. */
175 int x, y; /* Location of the cell. */
176 TixGridRowCol * rowcol[2]; /* Returns information about the row/col. */
177 Tcl_HashEntry * hashPtrs[2];/* Returns hash table info about the row/col.*/
178 {
179 hashPtrs[0] = Tcl_FindHashEntry(&dataSet->index[0], FIX(x));
180 if (hashPtrs[0] != NULL) {
181 rowcol[0] = (TixGridRowCol *)Tcl_GetHashValue(hashPtrs[0]);
182 } else {
183 return 0;
184 }
185
186 hashPtrs[1] = Tcl_FindHashEntry(&dataSet->index[1], FIX(y));
187 if (hashPtrs[1] != NULL) {
188 rowcol[1] = (TixGridRowCol *)Tcl_GetHashValue(hashPtrs[1]);
189 } else {
190 return 0;
191 }
192
193 return 1;
194 }
195
196 /*----------------------------------------------------------------------
197 * TixGridDataCreateEntry --
198 *
199 * Find or create the entry at the specified index.
200 *
201 * Results:
202 * A handle to the entry.
203 *
204 * Side effects:
205 * A new entry is created if it is not already in the dataset.
206 *----------------------------------------------------------------------
207 */
208
209 char *
210 TixGridDataCreateEntry(dataSet, x, y, defaultEntry)
211 TixGridDataSet * dataSet;
212 int x;
213 int y;
214 char * defaultEntry;
215 {
216 TixGridRowCol *rowcol[2];
217 Tcl_HashEntry *hashPtr;
218 int isNew, i, dispIndex[2];
219
220 dispIndex[0] = x;
221 dispIndex[1] = y;
222
223 for (i=0; i<2; i++) {
224 hashPtr = Tcl_CreateHashEntry(&dataSet->index[i],
225 FIX(dispIndex[i]), &isNew);
226
227 if (!isNew) {
228 rowcol[i] = (TixGridRowCol *)Tcl_GetHashValue(hashPtr);
229 } else {
230 rowcol[i] = InitRowCol(dispIndex[i]);
231 Tcl_SetHashValue(hashPtr, (char*)rowcol[i]);
232
233 if (dataSet->maxIdx[i] < dispIndex[i]) {
234 dataSet->maxIdx[i] = dispIndex[i];
235 }
236 }
237 }
238
239 hashPtr = Tcl_CreateHashEntry(&rowcol[0]->table,
240 (char*)rowcol[1], &isNew);
241
242 if (!isNew) {
243 return (char *) Tcl_GetHashValue(hashPtr);
244 }
245 else {
246 TixGrEntry *chPtr = (TixGrEntry *)defaultEntry;
247
248 Tcl_SetHashValue(hashPtr, (char*)chPtr);
249 chPtr->entryPtr[0] = hashPtr;
250
251 hashPtr = Tcl_CreateHashEntry(&rowcol[1]->table,
252 (char*)rowcol[0], &isNew);
253 Tcl_SetHashValue(hashPtr, (char*)defaultEntry);
254 chPtr->entryPtr[1] = hashPtr;
255
256 return defaultEntry;
257 }
258 }
259
260 /*----------------------------------------------------------------------
261 * TixGridDataDeleteEntry --
262 *
263 * Deletes the entry at the specified index.
264 *
265 * Results:
266 * True iff the entry existed and was deleted.
267 *
268 * Side effects:
269 * If there is an entry at the index, it is deleted.
270 *----------------------------------------------------------------------
271 */
272
273 int
274 TixGridDataDeleteEntry(dataSet, x, y)
275 TixGridDataSet * dataSet; /* The Grid dataset. */
276 int x; /* Column number of the entry. */
277 int y; /* Row number of the entry. */
278 {
279 TixGridRowCol *rowcol[2];
280 Tcl_HashEntry *hashPtrs[2]; /* Hash entries of the row/col. */
281 Tcl_HashEntry *cx, *cy; /* Hash entries of the cell in the row/col. */
282
283 if (!FindRowCol(dataSet, x, y, rowcol, hashPtrs)) {
284 /*
285 * The row and/or the column do not exist.
286 */
287 return 0;
288 }
289
290 cx = Tcl_FindHashEntry(&rowcol[0]->table, (char*)rowcol[1]);
291 cy = Tcl_FindHashEntry(&rowcol[1]->table, (char*)rowcol[0]);
292
293 if (cx == NULL && cy == NULL) {
294 return 0;
295 }
296 else if (cx != NULL && cy != NULL) {
297 Tcl_DeleteHashEntry(cx);
298 Tcl_DeleteHashEntry(cy);
299 }
300 else {
301 panic("Inconsistent grid dataset: (%d,%d) : %x %x", x, y, cx, cy);
302 }
303
304 return 1;
305
306 /*
307 * ToDo: trim down the hash table.
308 */
309 }
310
311 /* return value: has the size of the grid changed as a result of sorting */
312 int
313 TixGridDataUpdateSort(dataSet, axis, start, end, items)
314 TixGridDataSet * dataSet;
315 int axis;
316 int start;
317 int end;
318 Tix_GrSortItem *items;
319 {
320 TixGridRowCol **ptr;
321 Tcl_HashEntry *hashPtr;
322 int numItems = end - start + 1;
323 int i, k, max = 0;
324
325 if (numItems <= 0) {
326 return 0;
327 }
328
329 ptr = (TixGridRowCol **)ckalloc(numItems * sizeof(TixGridRowCol *));
330
331 for (k=0,i=start; i<=end; i++,k++) {
332 if (!(hashPtr = Tcl_FindHashEntry(&dataSet->index[axis], FIX(i)))) {
333 /*
334 * This row/col doesn't exist
335 */
336 ptr[k] = NULL;
337 } else {
338 ptr[k] = (TixGridRowCol *)Tcl_GetHashValue(hashPtr);
339 Tcl_DeleteHashEntry(hashPtr);
340 }
341 }
342
343 for (k=0,i=start; i<=end; i++,k++) {
344 int pos, isNew;
345 pos = items[k].index - start;
346
347 if (ptr[pos] != NULL) {
348 hashPtr = Tcl_CreateHashEntry(&dataSet->index[axis], FIX(i),
349 &isNew);
350 Tcl_SetHashValue(hashPtr, (char*)ptr[pos]);
351 ptr[pos]->dispIndex = i;
352 max = i;
353 }
354 }
355
356 ckfree((char*)ptr);
357
358 if (end+1 >= dataSet->maxIdx[axis]) {
359 if (dataSet->maxIdx[axis] != max+1) {
360 dataSet->maxIdx[axis] = max+1;
361 return 1; /* size changed */
362 }
363 }
364 return 0; /* size not changed */
365 }
366
367
368 static int
369 RowColMaxSize(wPtr, which, rowCol, defSize)
370 WidgetPtr wPtr;
371 int which; /* 0=cols, 1=rows */
372 TixGridRowCol *rowCol;
373 TixGridSize * defSize;
374 {
375 Tcl_HashSearch hashSearch;
376 Tcl_HashEntry *hashPtr;
377 TixGrEntry * chPtr;
378 int maxSize = 1;
379
380 if (rowCol->table.numEntries == 0) {
381 return defSize->pixels;
382 }
383
384 for (hashPtr = Tcl_FirstHashEntry(&rowCol->table, &hashSearch);
385 hashPtr;
386 hashPtr = Tcl_NextHashEntry(&hashSearch)) {
387
388 chPtr = (TixGrEntry *)Tcl_GetHashValue(hashPtr);
389 if (maxSize < chPtr->iPtr->base.size[which]) {
390 maxSize = chPtr->iPtr->base.size[which];
391 }
392 }
393
394 return maxSize;
395 }
396
397 /*
398 *----------------------------------------------------------------------
399 * TixGridDataGetRowColSize --
400 *
401 * Returns width of a column or height of a row.
402 *
403 * Results:
404 * The width or height.
405 *
406 * Side effects:
407 * None.
408 *----------------------------------------------------------------------
409 */
410
411 int
412 TixGridDataGetRowColSize(wPtr, dataSet, which, index, defSize, pad0, pad1)
413 WidgetPtr wPtr; /* Info about Grid widget */
414 TixGridDataSet * dataSet; /* Dataset of the Grid */
415 int which; /* 0=cols, 1=rows */
416 int index; /* Column or row number */
417 TixGridSize * defSize; /* The default size for the grid cells */
418 int *pad0; /* Holds return value of horizontal padding. */
419 int *pad1; /* Holds return value of vertical padding. */
420 {
421 TixGridRowCol *rowCol;
422 Tcl_HashEntry *hashPtr;
423 int size;
424
425 if (!(hashPtr = Tcl_FindHashEntry(&dataSet->index[which], FIX(index)))) {
426 size = defSize->pixels;
427 *pad0 = defSize->pad0;
428 *pad1 = defSize->pad1;
429 }
430 else {
431 rowCol = (TixGridRowCol *)Tcl_GetHashValue(hashPtr);
432
433 switch (rowCol->size.sizeType) {
434 case TIX_GR_AUTO:
435 size = RowColMaxSize(wPtr, which, rowCol, defSize);
436 *pad0 = rowCol->size.pad0;
437 *pad1 = rowCol->size.pad1;
438 break;
439
440 case TIX_GR_DEFINED_PIXEL:
441 size = rowCol->size.sizeValue;
442 *pad0 = rowCol->size.pad0;
443 *pad1 = rowCol->size.pad1;
444 break;
445
446 case TIX_GR_DEFINED_CHAR:
447 size = (int)(rowCol->size.charValue * wPtr->fontSize[which]);
448 *pad0 = rowCol->size.pad0;
449 *pad1 = rowCol->size.pad1;
450 break;
451
452 case TIX_GR_DEFAULT:
453 default: /* some error ?? */
454 if (defSize->sizeType == TIX_GR_AUTO) {
455 size = RowColMaxSize(wPtr, which, rowCol, defSize);
456 } else {
457 size = defSize->pixels;
458 }
459 *pad0 = defSize->pad0;
460 *pad1 = defSize->pad1;
461 }
462 }
463
464 return size;
465 }
466
467 int
468 TixGridDataGetIndex(interp, wPtr, xStr, yStr, xPtr, yPtr)
469 Tcl_Interp * interp;
470 WidgetPtr wPtr;
471 Tcl_Obj * xStr;
472 Tcl_Obj * yStr;
473 int * xPtr;
474 int * yPtr;
475 {
476 Tcl_Obj * str[2];
477 int * ptr[2];
478 int i;
479
480 str[0] = xStr;
481 str[1] = yStr;
482 ptr[0] = xPtr;
483 ptr[1] = yPtr;
484
485 for (i=0; i<2; i++) {
486 if (str[i] == NULL) { /* ignore this index */
487 continue;
488 }
489
490 if (strcmp(Tcl_GetString(str[i]), "max") == 0) {
491 *ptr[i] = wPtr->dataSet->maxIdx[i];
492 if (*ptr[i] < wPtr->hdrSize[i]) {
493 *ptr[i] = wPtr->hdrSize[i];
494 }
495 }
496 else if (strcmp(Tcl_GetString(str[i]), "end") == 0) {
497 *ptr[i] = wPtr->dataSet->maxIdx[i] + 1;
498 if (*ptr[i] < wPtr->hdrSize[i]) {
499 *ptr[i] = wPtr->hdrSize[i];
500 }
501 } else {
502 if (Tcl_GetIntFromObj(interp, str[i], ptr[i]) != TCL_OK) {
503 return TCL_ERROR;
504 }
505 }
506 if (*ptr[i] < 0) {
507 *ptr[i] = 0;
508 }
509 }
510
511 return TCL_OK;
512 }
513
514 /*
515 *----------------------------------------------------------------------
516 * TixGridDataConfigRowColSize --
517 *
518 * Configure width of column or height of rows.
519 *
520 * Results:
521 * Standard Tcl result.
522 *
523 * Side effects:
524 * The column/rows size will be changed in an idle event.
525 *----------------------------------------------------------------------
526 */
527
528 int
529 TixGridDataConfigRowColSize(interp, wPtr, dataSet, which, index, argc, argv,
530 argcErrorMsg, changed_ret)
531 Tcl_Interp * interp;
532 WidgetPtr wPtr;
533 TixGridDataSet * dataSet;
534 int which; /* 0=cols, 1=rows */
535 int index;
536 int argc;
537 char ** argv;
538 char * argcErrorMsg;
539 int *changed_ret; /* Returns whether size has been changed. */
540 {
541 TixGridRowCol *rowCol;
542 Tcl_HashEntry *hashPtr;
543 int isNew, code;
544
545 hashPtr = Tcl_CreateHashEntry(&dataSet->index[which], FIX(index), &isNew);
546
547 if (!isNew) {
548 rowCol = (TixGridRowCol *)Tcl_GetHashValue(hashPtr);
549 } else {
550 rowCol = InitRowCol(index);
551 Tcl_SetHashValue(hashPtr, (char*)rowCol);
552
553 if (dataSet->maxIdx[which] < index) {
554 dataSet->maxIdx[which] = index;
555 }
556 }
557
558 code = Tix_GrConfigSize(interp, wPtr, argc, argv, &rowCol->size,
559 argcErrorMsg, changed_ret);
560
561 if (changed_ret) {
562 *changed_ret |= isNew;
563 }
564
565 return code;
566 }
567
568 /*
569 *----------------------------------------------------------------------
570 * TixGridDataGetGridSize --
571 *
572 * Returns the number of rows and columns of the grid.
573 *
574 * Results:
575 * None.
576 *
577 * Side effects:
578 * None.
579 *----------------------------------------------------------------------
580 */
581
582 /*
583 * ToDo: maintain numCol and numRow info while adding entries.
584 */
585
586 void
587 TixGridDataGetGridSize(dataSet, numCol_ret, numRow_ret)
588 TixGridDataSet * dataSet;
589 int *numCol_ret;
590 int *numRow_ret;
591 {
592 int maxSize[2], i;
593 Tcl_HashEntry *hashPtr;
594 Tcl_HashSearch hashSearch;
595 TixGridRowCol * rowCol;
596
597 maxSize[0] = 1;
598 maxSize[1] = 1;
599
600 if (dataSet->index[0].numEntries == 0 || dataSet->index[1].numEntries==0) {
601 goto done;
602 }
603
604 for (i=0; i<2; i++) {
605
606 for (hashPtr = Tcl_FirstHashEntry(&dataSet->index[i], &hashSearch);
607 hashPtr;
608 hashPtr = Tcl_NextHashEntry(&hashSearch)) {
609
610 rowCol = (TixGridRowCol *)Tcl_GetHashValue(hashPtr);
611 if (maxSize[i] < rowCol->dispIndex+1) {
612 maxSize[i] = rowCol->dispIndex+1;
613 }
614 }
615 }
616
617 done:
618 if (numCol_ret) {
619 *numCol_ret = maxSize[0];
620 }
621 if (numRow_ret) {
622 *numRow_ret = maxSize[1];
623 }
624 }
625
626
627 /*
628 * the following four functions return true if done -- no more rows or cells
629 * are left to traverse
630 */
631
632 int
633 TixGrDataFirstRow(dataSet, rowSearchPtr)
634 TixGridDataSet* dataSet;
635 Tix_GrDataRowSearch * rowSearchPtr;
636 {
637 rowSearchPtr->hashPtr = Tcl_FirstHashEntry(&dataSet->index[0],
638 &rowSearchPtr->hashSearch);
639
640 if (rowSearchPtr->hashPtr != NULL) {
641 rowSearchPtr->row = (TixGridRowCol *)Tcl_GetHashValue(
642 rowSearchPtr->hashPtr);
643 return 0;
644 } else {
645 rowSearchPtr->row = NULL;
646 return 1;
647 }
648 }
649
650 int
651 TixGrDataNextRow(rowSearchPtr)
652 Tix_GrDataRowSearch * rowSearchPtr;
653 {
654 rowSearchPtr->hashPtr = Tcl_NextHashEntry(&rowSearchPtr->hashSearch);
655
656 if (rowSearchPtr->hashPtr != NULL) {
657 rowSearchPtr->row = (TixGridRowCol *)Tcl_GetHashValue(
658 rowSearchPtr->hashPtr);
659 return 0;
660 } else {
661 rowSearchPtr->row = NULL;
662 return 1;
663 }
664 }
665
666 int
667 TixGrDataFirstCell(rowSearchPtr, cellSearchPtr)
668 Tix_GrDataRowSearch * rowSearchPtr;
669 Tix_GrDataCellSearch * cellSearchPtr;
670 {
671 cellSearchPtr->hashPtr = Tcl_FirstHashEntry(&rowSearchPtr->row->table,
672 &cellSearchPtr->hashSearch);
673
674 if (cellSearchPtr->hashPtr != NULL) {
675 cellSearchPtr->data = (char *)Tcl_GetHashValue(
676 cellSearchPtr->hashPtr);
677 return 0;
678 } else {
679 cellSearchPtr->data = NULL;
680 return 1;
681 }
682 }
683
684 int
685 TixGrDataNextCell(cellSearchPtr)
686 Tix_GrDataCellSearch * cellSearchPtr;
687 {
688 cellSearchPtr->hashPtr = Tcl_NextHashEntry(&cellSearchPtr->hashSearch);
689
690 if (cellSearchPtr->hashPtr != NULL) {
691 cellSearchPtr->data = (char *)Tcl_GetHashValue(
692 cellSearchPtr->hashPtr);
693 return 0;
694 } else {
695 cellSearchPtr->data = NULL;
696 return 1;
697 }
698 }
699
700 /*----------------------------------------------------------------------
701 * TixGridDataDeleteSearchedEntry --
702 *
703 * Deletes an entry returned by one of the search functions.
704 *
705 * Results:
706 * None.
707 *
708 * Side effects:
709 * If there is an entry at the index, it is deleted.
710 *----------------------------------------------------------------------
711 */
712
713 void
714 TixGridDataDeleteSearchedEntry(cellSearchPtr)
715 Tix_GrDataCellSearch * cellSearchPtr;
716 {
717 TixGrEntry * chPtr = (TixGrEntry *)cellSearchPtr->data;
718
719 Tcl_DeleteHashEntry(chPtr->entryPtr[0]);
720 Tcl_DeleteHashEntry(chPtr->entryPtr[1]);
721 }
722
723 /*
724 *----------------------------------------------------------------------
725 * TixGridDataDeleteRange --
726 *
727 * Deletes the rows (columns) at the given range.
728 *
729 * Results:
730 * None.
731 *
732 * Side effects:
733 * The given rows (columns) are deleted.
734 *----------------------------------------------------------------------
735 */
736
737 void
738 TixGridDataDeleteRange(wPtr, dataSet, which, from, to)
739 WidgetPtr wPtr; /* Info about the grid widget. */
740 TixGridDataSet * dataSet; /* Dataset of the Grid */
741 int which; /* 0=cols, 1=rows */
742 int from; /* Starting column/row. */
743 int to; /* Ending column/row (inclusive). */
744 {
745 int tmp, i, other, deleted = 0;
746
747 if (from < 0 ) {
748 from = 0;
749 }
750 if (to < 0 ) {
751 to = 0;
752 }
753 if (from > to) {
754 tmp = to;
755 to = from;
756 from = tmp;
757 }
758 if (which == 0) {
759 other = 1;
760 } else {
761 other = 0;
762 }
763
764 for (i=from; i<=to; i++) {
765 Tcl_HashEntry *hashPtr, *hp, *toDel;
766 TixGridRowCol *rcPtr, *rcp;
767 Tcl_HashSearch hashSearch;
768
769 hashPtr = Tcl_FindHashEntry(&dataSet->index[which], FIX(i));
770 if (hashPtr != NULL) {
771 rcPtr = (TixGridRowCol *)Tcl_GetHashValue(hashPtr);
772
773 for (hp = Tcl_FirstHashEntry(&dataSet->index[other], &hashSearch);
774 hp;
775 hp = Tcl_NextHashEntry(&hashSearch)) {
776
777 rcp = (TixGridRowCol *)Tcl_GetHashValue(hp);
778 toDel = Tcl_FindHashEntry(&rcp->table, (char*)rcPtr);
779 if (toDel != NULL) {
780 TixGrEntry * chPtr;
781
782 chPtr = (TixGrEntry *)Tcl_GetHashValue(toDel);
783 if (chPtr) {
784 deleted = 1;
785 Tix_GrFreeElem(chPtr);
786 }
787
788 Tcl_DeleteHashEntry(toDel);
789 }
790 }
791
792 Tcl_DeleteHashEntry(hashPtr);
793 Tcl_DeleteHashTable(&rcPtr->table);
794 ckfree((char*)rcPtr);
795 }
796 }
797
798 if (deleted) {
799 Tix_GrDoWhenIdle(wPtr, TIX_GR_RESIZE);
800 }
801 }
802
803 /*
804 *----------------------------------------------------------------------
805 * TixGridDataMoveRange --
806 *
807 * Moves a range of row (columns) by a given offset. E.g. move 2-4 by 2
808 * changes the rows 2,3,4 to 4,5,6.
809 *
810 * Results:
811 * None.
812 *
813 * Side effects:
814 * Rows (columns) at locations where the given rows will be moved
815 * to are deleted.
816 *----------------------------------------------------------------------
817 */
818
819 void
820 TixGridDataMoveRange(wPtr, dataSet, which, from, to, by)
821 WidgetPtr wPtr; /* Info about the grid widget. */
822 TixGridDataSet * dataSet; /* Dataset of the Grid */
823 int which; /* 0=cols, 1=rows */
824 int from; /* Starting column/row. */
825 int to; /* Ending column/row (inclusive). */
826 int by; /* The distance of the move. */
827 {
828 int tmp, i, s, e, incr;
829 int df, dt; /* Rows inside this range will be deleted
830 * before the given rows are moved. */
831
832 if (by == 0) {
833 return;
834 }
835 if (from < 0 ) {
836 from = 0;
837 }
838 if (to < 0 ) {
839 to = 0;
840 }
841 if (from > to) {
842 tmp = to;
843 to = from;
844 from = tmp;
845 }
846
847 if ((from + by) < 0) {
848 /*
849 * Delete the leading rows that will be moved beyond the top of grid.
850 */
851 int n; /* Number of rows to delete. */
852
853 n = - (from + by);
854 if (n > (to - from + 1)) {
855 n = to - from + 1;
856 }
857 TixGridDataDeleteRange(wPtr, dataSet, which, from, (from+n-1));
858 from = from + n;
859
860 if (from > to) {
861 /*
862 * All the rows have been deleted.
863 */
864 return;
865 }
866 }
867
868 /*
869 * Delete rows at locations where the given rows will be moved to.
870 */
871 df = from + by;
872 dt = to + by;
873
874 if (by > 0) {
875 if (df <= to) {
876 df = to + 1;
877 }
878 } else {
879 if (dt >= from) {
880 dt = from - 1;
881 }
882 }
883 TixGridDataDeleteRange(wPtr, dataSet, which, df, dt);
884
885 /*
886 * Rename the rows.
887 */
888 if (by > 0) {
889 s = to;
890 e = from-1;
891 incr = -1;
892 } else {
893 s = from;
894 e = to+1;
895 incr = 1;
896 }
897
898 for (i=s; i!=e; i+=incr) {
899 Tcl_HashEntry *hashPtr;
900 TixGridRowCol *rcPtr;
901 int isNew;
902
903 hashPtr = Tcl_FindHashEntry(&dataSet->index[which], FIX(i));
904 if (hashPtr != NULL) {
905 rcPtr = (TixGridRowCol *)Tcl_GetHashValue(hashPtr);
906 rcPtr->dispIndex = i+by;
907 Tcl_DeleteHashEntry(hashPtr);
908 hashPtr = Tcl_CreateHashEntry(&dataSet->index[which], FIX(i+by),
909 &isNew);
910 Tcl_SetHashValue(hashPtr, (char*)rcPtr);
911 }
912 }
913 }