| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tixPort.h" |
| 17 |
#include "tixInt.h" |
| 18 |
#include "tixGrid.h" |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
static Tcl_Interp *sortInterp = NULL; |
| 30 |
|
| 31 |
static enum {ASCII, INTEGER, REAL, COMMAND} sortMode; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#ifndef _LANG |
| 37 |
static Tcl_DString sortCmd; |
| 38 |
|
| 39 |
|
| 40 |
#endif |
| 41 |
static int sortIncreasing; |
| 42 |
|
| 43 |
static int sortCode; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 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 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 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 |
|
| 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; |
| 150 |
int argc; |
| 151 |
char **argv; |
| 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; |
| 159 |
|
| 160 |
int sortKeyIndex; |
| 161 |
int gridSize[2]; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
if (sortInterp != NULL) { |
| 168 |
interp->result = "can't invoke the tixGrid sort command recursively"; |
| 169 |
return TCL_ERROR; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 219 |
return TCL_OK; |
| 220 |
} |
| 221 |
if (end == start) { |
| 222 |
|
| 223 |
return TCL_OK; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 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]; |
| 238 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 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 |
|
| 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 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
static int |
| 366 |
SortCompareProc(first, second) |
| 367 |
CONST VOID *first, *second; |
| 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 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
return order; |
| 381 |
} |
| 382 |
if (firstString == NULL && secondString == NULL) { |
| 383 |
|
| 384 |
return order; |
| 385 |
} |
| 386 |
if (secondString == NULL) { |
| 387 |
|
| 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 |
|
| 431 |
Tcl_Panic("Need Callback Handling Added"); |
| 432 |
return 0; |
| 433 |
#else |
| 434 |
int oldLength; |
| 435 |
char *end; |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 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 |
|
| 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 |
} |