Debian Codegraph @

pTk/mTk/tixGeneric/tixGrSort.c

raw symbols SCIP
1
2 /* $Id: tixGrSort.c,v 1.1.1.1 2000/05/17 11:08:38 idiscovery Exp $ */
3
4 /*
5 * tixGrSel.c --
6 *
7 * This module handles the sorting of the 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 /*
21 * The variables below are used to implement the "lsort" command.
22 * Unfortunately, this use of static variables prevents "lsort"
23 * from being thread-safe, but there's no alternative given the
24 * current implementation of qsort. In a threaded environment
25 * these variables should be made thread-local if possible, or else
26 * "lsort" needs internal mutual exclusion.
27 */
28
29 static Tcl_Interp *sortInterp = NULL; /* Interpreter for "lsort" command.
30 * NULL means no lsort is active. */
31 static enum {ASCII, INTEGER, REAL, COMMAND} sortMode;
32 /* Mode for sorting:compare as strings,
33 * compare as numbers, or call
34 * user-defined command for
35 * comparison. */
36 #ifndef _LANG
37 static Tcl_DString sortCmd; /* Holds command if mode is COMMAND.
38 * pre-initialized to hold base of
39 * command. */
40 #endif
41 static int sortIncreasing; /* 0 means sort in decreasing order,
42 * 1 means increasing order. */
43 static int sortCode; /* Anything other than TCL_OK means a
44 * problem occurred while sorting; this
45 * executing a comparison command, so
46 * the sort was aborted. */
47
48 /*
49 * Forward declarations for procedures defined in this file:
50 */
51
52 EXTERN TIX_DECLARE_SUBCMD(Tix_GrSort);
53
54 static int SortCompareProc _ANSI_ARGS_((CONST VOID *first,
55 CONST VOID *second));
56 Tcl_Obj * Tix_GrGetCellText _ANSI_ARGS_((WidgetPtr wPtr,
57 int x, int y));
58 Tix_GrSortItem * Tix_GrGetSortItems _ANSI_ARGS_((WidgetPtr wPtr,
59 int axis, int start, int end, int sortKeyIndex));
60 void Tix_GrFreeSortItems _ANSI_ARGS_((WidgetPtr wPtr,
61 Tix_GrSortItem * items, int numItems));
62
63
64 /*
65 *----------------------------------------------------------------------
66 *
67 * Tcl_LsortCmd --
68 *
69 * This procedure is invoked to process the "lsort" Tcl command.
70 * See the user documentation for details on what it does.
71 *
72 * Results:
73 * A standard Tcl result.
74 *
75 * Side effects:
76 * See the user documentation.
77 *
78 *----------------------------------------------------------------------
79 */
80
81 /* ARGSUSED */
82
83 Tcl_Obj *
84 Tix_GrGetCellText(wPtr, x, y)
85 WidgetPtr wPtr;
86 int x;
87 int y;
88 {
89 TixGrEntry* chPtr;
90
91 if ((chPtr = (TixGrEntry*) TixGridDataFindEntry(wPtr->dataSet, x, y))) {
92 switch (Tix_DItemType(chPtr->iPtr)) {
93 case TIX_DITEM_TEXT:
94 return chPtr->iPtr->text.text;
95 case TIX_DITEM_IMAGETEXT:
96 return chPtr->iPtr->imagetext.text;
97 default:
98 return NULL;
99 }
100 } else {
101 return NULL;
102 }
103 }
104
105 Tix_GrSortItem *
106 Tix_GrGetSortItems(wPtr, axis, start, end, sortKeyIndex)
107 WidgetPtr wPtr;
108 int axis;
109 int start;
110 int end;
111 int sortKeyIndex;
112 {
113 int i, k, numItems;
114 Tix_GrSortItem *items;
115
116 if (end <= start) {
117 /* sanity check: no need to sort */
118 return (Tix_GrSortItem *) NULL;
119 }
120
121 numItems = end-start+1;
122 items = (Tix_GrSortItem *)ckalloc(sizeof(Tix_GrSortItem) * numItems);
123
124 for (k=0,i=start; i<=end; i++, k++) {
125 items[k].index = i;
126 if (axis == 0) {
127 items[k].data = Tix_GrGetCellText(wPtr, i, sortKeyIndex);
128 } else {
129 items[k].data = Tix_GrGetCellText(wPtr, sortKeyIndex, i);
130 }
131 }
132
133 return items;
134 }
135
136
137 void
138 Tix_GrFreeSortItems(wPtr, items, numItems)
139 WidgetPtr wPtr;
140 Tix_GrSortItem * items;
141 int numItems;
142 {
143 ckfree((char*)items);
144 }
145
146 int
147 Tix_GrSort(clientData, interp, argc, argv)
148 ClientData clientData;
149 Tcl_Interp *interp; /* Current interpreter. */
150 int argc; /* Number of arguments. */
151 char **argv; /* Argument strings. */
152 {
153 WidgetPtr wPtr = (WidgetPtr) clientData;
154 int i, axis, otherAxis, start, end;
155 size_t len;
156 Tix_GrSortItem *items = NULL;
157 int numItems;
158 LangCallback *command = NULL; /* Initialization needed only to
159 * prevent compiler warning. */
160 int sortKeyIndex;
161 int gridSize[2];
162
163 /*-------------------------------------------------------------------
164 * Argument parsing
165 *-------------------------------------------------------------------
166 */
167 if (sortInterp != NULL) {
168 interp->result = "can't invoke the tixGrid sort command recursively";
169 return TCL_ERROR;
170 }
171
172 /* Figure out the sorting dimension
173 */
174 len = strlen(argv[0]);
175 if (strncmp(argv[0], "rows", len)==0) {
176 axis = 1;
177 otherAxis = 0;
178 } else if (strncmp(argv[0], "column", len)==0) {
179 axis = 0;
180 otherAxis = 1;
181 } else {
182 Tcl_AppendResult(interp, "wrong dimension \"", argv[0],
183 "\", should be row or column", (char *) NULL);
184 return TCL_ERROR;
185 }
186
187 /* get the start and end index
188 */
189 if (axis == 0) {
190 if (TixGridDataGetIndex(interp, wPtr, objv[1], NULL, &start, NULL)
191 !=TCL_OK) {
192 return TCL_ERROR;
193 }
194 if (TixGridDataGetIndex(interp, wPtr, objv[2], NULL, &end, NULL)
195 !=TCL_OK) {
196 return TCL_ERROR;
197 }
198 } else {
199 if (TixGridDataGetIndex(interp, wPtr, NULL, objv[1], NULL, &start)
200 !=TCL_OK) {
201 return TCL_ERROR;
202 }
203 if (TixGridDataGetIndex(interp, wPtr, NULL, objv[2], NULL, &end)
204 !=TCL_OK) {
205 return TCL_ERROR;
206 }
207 }
208
209 /* Check the range
210 */
211 TixGridDataGetGridSize(wPtr->dataSet, &gridSize[0], &gridSize[1]);
212 if (start > end) {
213 int tmp = start;
214 start = end;
215 end = tmp;
216 }
217 if (start >= gridSize[axis]) {
218 /* no need to sort */
219 return TCL_OK;
220 }
221 if (end == start) {
222 /* no need to sort */
223 return TCL_OK;
224 }
225
226 /* get the options
227 */
228 if ((argc-3) %2 != 0) {
229 Tcl_AppendResult(interp, "value for \"", argv[argc-1],
230 "\" missing", NULL);
231 return TCL_ERROR;
232 }
233 sortInterp = interp;
234 sortMode = ASCII;
235 sortIncreasing = 1;
236 sortCode = TCL_OK;
237 sortKeyIndex = wPtr->hdrSize[otherAxis]; /* by default, use the first
238 * scrollable item as the key
239 */
240 for (i=3; i<argc; i+=2) {
241 len = strlen(argv[i]);
242 if (strncmp(argv[i], "-type", len) == 0) {
243 if (strcmp(argv[i+1], "ascii") == 0) {
244 sortMode = ASCII;
245 } else if (strcmp(argv[i+1], "integer") == 0) {
246 sortMode = INTEGER;
247 } else if (strcmp(argv[i+1], "real") == 0) {
248 sortMode = REAL;
249 } else {
250 Tcl_AppendResult(interp, "wrong type \"", argv[i+1],
251 "\": must be ascii, integer or real", (char *) NULL);
252 sortCode = TCL_ERROR;
253 goto done;
254 }
255 }
256 else if (strncmp(argv[i], "-order", len) == 0) {
257 if (strcmp(argv[i+1], "increasing") == 0) {
258 sortIncreasing = 1;
259 } else if (strcmp(argv[i+1], "decreasing") == 0) {
260 sortIncreasing = 0;
261 } else {
262 Tcl_AppendResult(interp, "wrong order \"", argv[i+1],
263 "\": must be increasing or decreasing", (char *) NULL);
264 sortCode = TCL_ERROR;
265 goto done;
266 }
267 }
268 else if (strncmp(argv[i], "-key", len) == 0) {
269 if (axis == 0) {
270 /* sort columns: the key is a column index (1) */
271 if (TixGridDataGetIndex(interp, wPtr, NULL, objv[i+1], NULL,
272 &sortKeyIndex) !=TCL_OK) {
273 sortCode = TCL_ERROR;
274 goto done;
275 }
276 } else {
277 /* sort rows: the key is a row index (0)*/
278 if (TixGridDataGetIndex(interp, wPtr, objv[i+1], NULL,
279 &sortKeyIndex, NULL) !=TCL_OK) {
280 sortCode = TCL_ERROR;
281 goto done;
282 }
283 }
284 }
285 else if (strncmp(argv[i], "-command", len) == 0) {
286 sortMode = COMMAND;
287 command = LangMakeCallback(objv[i+1]);
288 }
289 else {
290 Tcl_AppendResult(interp, "wrong option \"", argv[i],
291 "\": must be -command, -key, -order or -type", (char *) NULL);
292 sortCode = TCL_ERROR;
293 goto done;
294 }
295 }
296 if (sortMode == COMMAND) {
297 #ifndef _LANG
298 Tcl_DStringInit(&sortCmd);
299 Tcl_DStringAppend(&sortCmd, command, -1);
300 #endif
301 }
302
303 /*------------------------------------------------------------------
304 * SORTING
305 *------------------------------------------------------------------
306 */
307 /* prepare the array to be sorted */
308 numItems = end - start + 1;
309 items = Tix_GrGetSortItems(wPtr, axis, start, end, sortKeyIndex);
310
311 if (items != NULL) {
312 int sizeChanged;
313
314 qsort((VOID *)items, (size_t)numItems, sizeof(Tix_GrSortItem),
315 SortCompareProc);
316 sizeChanged = TixGridDataUpdateSort(wPtr->dataSet, axis, start, end,
317 items);
318 if (sizeChanged) {
319 Tix_GrDoWhenIdle(wPtr, TIX_GR_RESIZE);
320 } else {
321 wPtr->toResetRB = 1;
322 Tix_GrDoWhenIdle(wPtr, TIX_GR_REDRAW);
323 }
324
325 Tix_GrFreeSortItems(wPtr, items, numItems);
326 }
327
328 /* Done */
329 if (sortCode == TCL_OK) {
330 Tcl_ResetResult(interp);
331 }
332 if (sortMode == COMMAND) {
333 #ifdef _LANG
334 LangFreeCallback(command);
335 #else
336 Tcl_DStringFree(&sortCmd);
337 #endif
338 }
339
340 done:
341 sortInterp = NULL;
342 return sortCode;
343 }
344
345 /*
346 *----------------------------------------------------------------------
347 *
348 * SortCompareProc --
349 *
350 * This procedure is invoked by qsort to determine the proper
351 * ordering between two elements.
352 *
353 * Results:
354 * < 0 means first is "smaller" than "second", > 0 means "first"
355 * is larger than "second", and 0 means they should be treated
356 * as equal.
357 *
358 * Side effects:
359 * None, unless a user-defined comparison command does something
360 * weird.
361 *
362 *----------------------------------------------------------------------
363 */
364
365 static int
366 SortCompareProc(first, second)
367 CONST VOID *first, *second; /* Elements to be compared. */
368 {
369 int order;
370 Tcl_Obj * firstString = ((Tix_GrSortItem*)first )->data;
371 Tcl_Obj * secondString = ((Tix_GrSortItem*)second)->data;
372
373 order = 0;
374 if (sortCode != TCL_OK) {
375 /*
376 * Once an error has occurred, skip any future comparisons
377 * so as to preserve the error message in sortInterp->result.
378 */
379
380 return order;
381 }
382 if (firstString == NULL && secondString == NULL) {
383 /* equal */
384 return order;
385 }
386 if (secondString == NULL) {
387 /* first larger than second */
388 order = 1;
389 goto done;
390 }
391 if (firstString == NULL) {
392 order = -1;
393 goto done;
394 }
395
396 if (sortMode == ASCII) {
397 order = strcmp(Tcl_GetString(firstString), Tcl_GetString(secondString));
398 } else if (sortMode == INTEGER) {
399 int a, b;
400
401 if ((Tcl_GetIntFromObj(sortInterp, firstString, &a) != TCL_OK)
402 || (Tcl_GetIntFromObj(sortInterp, secondString, &b) != TCL_OK)) {
403 Tcl_AddErrorInfo(sortInterp,
404 "\n (converting list element from string to integer)");
405 sortCode = TCL_ERROR;
406 return order;
407 }
408 if (a > b) {
409 order = 1;
410 } else if (b > a) {
411 order = -1;
412 }
413 } else if (sortMode == REAL) {
414 double a, b;
415
416 if ((Tcl_GetDoubleFromObj(sortInterp, firstString, &a) != TCL_OK)
417 || (Tcl_GetDoubleFromObj(sortInterp, secondString, &b) != TCL_OK)) {
418 Tcl_AddErrorInfo(sortInterp,
419 "\n (converting list element from string to real)");
420 sortCode = TCL_ERROR;
421 return order;
422 }
423 if (a > b) {
424 order = 1;
425 } else if (b > a) {
426 order = -1;
427 }
428 } else {
429 #ifdef _LANG
430 /* FIXME */
431 Tcl_Panic("Need Callback Handling Added");
432 return 0;
433 #else
434 int oldLength;
435 char *end;
436
437 /*
438 * Generate and evaluate a command to determine which string comes
439 * first.
440 */
441
442 oldLength = Tcl_DStringLength(&sortCmd);
443 Tcl_DStringAppendElement(&sortCmd, firstString);
444 Tcl_DStringAppendElement(&sortCmd, secondString);
445 sortCode = Tcl_Eval(sortInterp, Tcl_DStringValue(&sortCmd));
446 Tcl_DStringTrunc(&sortCmd, oldLength);
447 if (sortCode != TCL_OK) {
448 Tcl_AddErrorInfo(sortInterp,
449 "\n (user-defined comparison command)");
450 return order;
451 }
452
453 /*
454 * Parse the result of the command.
455 */
456
457 order = strtol(sortInterp->result, &end, 0);
458 if ((end == sortInterp->result) || (*end != 0)) {
459 Tcl_ResetResult(sortInterp);
460 Tcl_AppendResult(sortInterp,
461 "comparison command returned non-numeric result",
462 (char *) NULL);
463 sortCode = TCL_ERROR;
464 return order;
465 }
466 #endif
467 }
468
469 done:
470 if (!sortIncreasing) {
471 order = -order;
472 }
473 return order;
474 }