OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [start/] [insight/] [libgui/] [src/] [tkTableTag.c] - Blame information for rev 578

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/*
2
 * tkTableTag.c --
3
 *
4
 *      This module implements tags for table widgets.
5
 *
6
 * Copyright (c) 1998 Jeffrey Hobbs
7
 *
8
 * See the file "license.terms" for information on usage and redistribution
9
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10
 *
11
 */
12
 
13
#include "tkTable.h"
14
 
15
static void     CreateTagEntry _ANSI_ARGS_((Table *tablePtr, char *name,
16
                                            int argc, char **argv));
17
static void     TableImageProc _ANSI_ARGS_((ClientData clientData, int x,
18
                                            int y, int width, int height,
19
                                            int imageWidth, int imageHeight));
20
 
21
/* tag subcommands */
22
#define TAG_CELLTAG     1       /* tag a cell */
23
#define TAG_CGET        2       /* get a config value */
24
#define TAG_COLTAG      3       /* tag a column */
25
#define TAG_CONFIGURE   4       /* config/create a new tag */
26
#define TAG_DELETE      5       /* delete a tag */
27
#define TAG_EXISTS      6       /* does a tag exist? */
28
#define TAG_NAMES       7       /* print the tag names */
29
#define TAG_ROWTAG      8       /* tag a row */
30
#define TAG_INCLUDES    9       /* does an index have a particular tag */
31
 
32
static Cmd_Struct tag_cmds[] = {
33
  {"celltag",   TAG_CELLTAG},
34
  {"coltag",    TAG_COLTAG},
35
  {"configure", TAG_CONFIGURE},
36
  {"cget",      TAG_CGET},
37
  {"delete",    TAG_DELETE},
38
  {"exists",    TAG_EXISTS},
39
  {"names",     TAG_NAMES},
40
  {"rowtag",    TAG_ROWTAG},
41
  {"includes",  TAG_INCLUDES},
42
  {"", 0}
43
};
44
 
45
static Cmd_Struct tagState_vals[]= {
46
  {"unknown",    STATE_UNKNOWN},
47
  {"normal",     STATE_NORMAL},
48
  {"disabled",   STATE_DISABLED},
49
  {"",           0 }
50
};
51
 
52
static Tk_CustomOption tagStateOpt = { Cmd_OptionSet, Cmd_OptionGet,
53
                                       (ClientData)(&tagState_vals) };
54
 
55
/*
56
 * The default specification for configuring tags
57
 * Done like this to make the command line parsing easy
58
 */
59
 
60
static Tk_ConfigSpec tagConfig[] = {
61
  {TK_CONFIG_ANCHOR, "-anchor", "anchor", "Anchor", "center",
62
   Tk_Offset(TableTag, anchor), TK_CONFIG_DONT_SET_DEFAULT },
63
  {TK_CONFIG_BORDER, "-background", "background", "Background", NULL,
64
   Tk_Offset(TableTag, bg),
65
   TK_CONFIG_DONT_SET_DEFAULT|TK_CONFIG_NULL_OK },
66
  {TK_CONFIG_SYNONYM, "-bg", "background", (char *) NULL,
67
   (char *) NULL, 0, 0 },
68
  {TK_CONFIG_BORDER, "-foreground", "foreground", "Foreground", NULL,
69
   Tk_Offset(TableTag, fg),
70
   TK_CONFIG_DONT_SET_DEFAULT|TK_CONFIG_NULL_OK },
71
  {TK_CONFIG_SYNONYM, "-fg", "foreground", (char *) NULL,
72
   (char *) NULL, 0, 0 },
73
  {TK_CONFIG_FONT, "-font", "font", "Font", NULL,
74
   Tk_Offset(TableTag, tkfont),
75
   TK_CONFIG_DONT_SET_DEFAULT|TK_CONFIG_NULL_OK },
76
  {TK_CONFIG_STRING, "-image", "image", "Image", NULL,
77
   Tk_Offset(TableTag, imageStr),
78
   TK_CONFIG_DONT_SET_DEFAULT|TK_CONFIG_NULL_OK },
79
  {TK_CONFIG_JUSTIFY, "-justify", "justify", "Justify", "left",
80
   Tk_Offset(TableTag, justify), TK_CONFIG_DONT_SET_DEFAULT },
81
  {TK_CONFIG_INT, "-multiline", "multiline", "Multiline", "1",
82
   Tk_Offset(TableTag, multiline), TK_CONFIG_DONT_SET_DEFAULT },
83
  {TK_CONFIG_RELIEF, "-relief", "relief", "Relief", "flat",
84
   Tk_Offset(TableTag, relief),
85
   TK_CONFIG_DONT_SET_DEFAULT|TK_CONFIG_NULL_OK },
86
  {TK_CONFIG_INT, "-showtext", "showText", "ShowText", "0",
87
   Tk_Offset(TableTag, showtext), TK_CONFIG_DONT_SET_DEFAULT },
88
  {TK_CONFIG_CUSTOM, "-state", "state", "State", "unknown",
89
   Tk_Offset(TableTag, state), TK_CONFIG_DONT_SET_DEFAULT, &tagStateOpt },
90
  {TK_CONFIG_INT, "-wrap", "wrap", "Wrap", "0",
91
   Tk_Offset(TableTag, wrap), TK_CONFIG_DONT_SET_DEFAULT },
92
  {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
93
   (char *) NULL, 0, 0 }
94
};
95
 
96
/*
97
 *----------------------------------------------------------------------
98
 *
99
 * TableImageProc --
100
 *      Called when an image associated with a tag is changed.
101
 *
102
 * Results:
103
 *      None.
104
 *
105
 * Side effects:
106
 *      Invalidates the whole table.
107
 *      FIX - should only invalidate affected cells.
108
 *
109
 *----------------------------------------------------------------------
110
 */
111
static void
112
TableImageProc(ClientData clientData, int x, int y, int width, int height,
113
               int imageWidth, int imageHeight)
114
{
115
  TableInvalidateAll((Table *)clientData, 0);
116
}
117
 
118
/*
119
 *----------------------------------------------------------------------
120
 *
121
 * TableNewTag --
122
 *      ckallocs space for a new tag structure and inits the structure.
123
 *
124
 * Results:
125
 *      Returns a pointer to the new structure.  Must be freed later.
126
 *
127
 * Side effects:
128
 *      None.
129
 *
130
 *----------------------------------------------------------------------
131
 */
132
TableTag *
133
TableNewTag(void)
134
{
135
  TableTag *tagPtr = (TableTag *) ckalloc(sizeof(TableTag));
136
  tagPtr->anchor        = (Tk_Anchor)-1;
137
  tagPtr->bg            = NULL;
138
  tagPtr->fg            = NULL;
139
  tagPtr->tkfont        = NULL;
140
  tagPtr->image         = NULL;
141
  tagPtr->imageStr      = NULL;
142
  tagPtr->justify       = (Tk_Justify)-1;
143
  tagPtr->multiline     = -1;
144
  tagPtr->relief        = -1;
145
  tagPtr->showtext      = -1;
146
  tagPtr->state         = STATE_UNKNOWN;
147
  tagPtr->wrap          = -1;
148
  return tagPtr;
149
}
150
 
151
/*
152
 *----------------------------------------------------------------------
153
 *
154
 * TableMergeTag --
155
 *      This routine merges two tags by adding any fields from the addTag
156
 *      that are set to the baseTag.
157
 *
158
 * Results:
159
 *      baseTag will inherit all set characteristics of addTag
160
 *      (addTag thus has the priority).
161
 *
162
 * Side effects:
163
 *      None.
164
 *
165
 *----------------------------------------------------------------------
166
 */
167
void
168
TableMergeTag(TableTag *baseTag, TableTag *addTag)
169
{
170
  if (addTag->anchor != (Tk_Anchor)-1)  baseTag->anchor = addTag->anchor;
171
  if (addTag->bg != NULL)               baseTag->bg = addTag->bg;
172
  if (addTag->fg != NULL)               baseTag->fg = addTag->fg;
173
  if (addTag->tkfont != NULL)           baseTag->tkfont = addTag->tkfont;
174
  if (addTag->imageStr != NULL) {
175
    baseTag->imageStr = addTag->imageStr;
176
    baseTag->image = addTag->image;
177
  }
178
  if (addTag->multiline >= 0)            baseTag->multiline = addTag->multiline;
179
  if (addTag->relief != -1)             baseTag->relief = addTag->relief;
180
  if (addTag->showtext >= 0)             baseTag->showtext = addTag->showtext;
181
  if (addTag->state != STATE_UNKNOWN)   baseTag->state = addTag->state;
182
  if (addTag->justify != (Tk_Justify)-1) baseTag->justify = addTag->justify;
183
  if (addTag->wrap >= 0)         baseTag->wrap = addTag->wrap;
184
}
185
 
186
/*
187
 *----------------------------------------------------------------------
188
 *
189
 * TableInvertTag --
190
 *      This routine swaps background and foreground for the selected tag.
191
 *
192
 * Results:
193
 *      Inverts fg and bg of tag.
194
 *
195
 * Side effects:
196
 *      None.
197
 *
198
 *----------------------------------------------------------------------
199
 */
200
void
201
TableInvertTag(TableTag *baseTag)
202
{
203
  Tk_3DBorder tmpBg;
204
 
205
  tmpBg = baseTag->fg;
206
  baseTag->fg = baseTag->bg;
207
  baseTag->bg = tmpBg;
208
}
209
 
210
/*
211
 *----------------------------------------------------------------------
212
 *
213
 * CreateTagEntry --
214
 *      Takes a name and optional args and create a tag entry in the
215
 *      table's tag table.
216
 *
217
 * Results:
218
 *      A new tag entry will be created.
219
 *
220
 * Side effects:
221
 *      None.
222
 *
223
 *----------------------------------------------------------------------
224
 */
225
static void
226
CreateTagEntry(Table *tablePtr, char *name, int argc, char **argv)
227
{
228
  Tcl_HashEntry *entryPtr;
229
  TableTag *tagPtr = TableNewTag();
230
  int dummy;
231
  Tk_ConfigureWidget(tablePtr->interp, tablePtr->tkwin, tagConfig,
232
                     argc, argv, (char *)tagPtr, TK_CONFIG_ARGV_ONLY);
233
  entryPtr = Tcl_CreateHashEntry(tablePtr->tagTable, name, &dummy);
234
  Tcl_SetHashValue(entryPtr, (ClientData) tagPtr);
235
}
236
 
237
/*
238
 *----------------------------------------------------------------------
239
 *
240
 * TableInitTags --
241
 *      Creates the static table tags.
242
 *
243
 * Results:
244
 *      active, sel, title and flash are created as tags.
245
 *
246
 * Side effects:
247
 *      None.
248
 *
249
 *----------------------------------------------------------------------
250
 */
251
void
252
TableInitTags(Table *tablePtr)
253
{
254
  static char *activeArgs[]     = {"-bg", ACTIVE_BG, "-relief", "flat" };
255
  static char *selArgs[]        = {"-bg", SELECT_BG, "-relief", "sunken" };
256
  static char *titleArgs[]      = {"-bg", DISABLED,  "-relief", "flat",
257
                                   "-fg", "white", "-state", "disabled" };
258
  static char *flashArgs[]      = {"-bg", "red" };
259
  CreateTagEntry(tablePtr, "active", ARSIZE(activeArgs), activeArgs);
260
  CreateTagEntry(tablePtr, "sel", ARSIZE(selArgs), selArgs);
261
  CreateTagEntry(tablePtr, "title", ARSIZE(titleArgs), titleArgs);
262
  CreateTagEntry(tablePtr, "flash", ARSIZE(flashArgs), flashArgs);
263
}
264
 
265
/*
266
 *----------------------------------------------------------------------
267
 *
268
 * FindRowColTag --
269
 *      Finds a row/col tag based on the row/col styles and tagCommand.
270
 *
271
 * Results:
272
 *      Returns tag associated with row/col cell, if any.
273
 *
274
 * Side effects:
275
 *      Possible side effects from eval of tagCommand.
276
 *
277
 *----------------------------------------------------------------------
278
 */
279
TableTag *
280
FindRowColTag(Table *tablePtr, int cell, int mode)
281
{
282
  Tcl_HashTable *hash;
283
  Tcl_HashEntry *entryPtr;
284
 
285
  hash = (mode == ROW) ? tablePtr->rowStyles : tablePtr->colStyles;
286
  if ((entryPtr = Tcl_FindHashEntry(hash, (char *)cell)) == NULL) {
287
    char *cmd = (mode == ROW) ? tablePtr->rowTagCmd : tablePtr->colTagCmd;
288
    if (cmd) {
289
      register Tcl_Interp *interp = tablePtr->interp;
290
      char buf[INDEX_BUFSIZE];
291
      /* Since it does not exist, eval command with row/col appended */
292
      sprintf(buf, " %d", cell);
293
      Tcl_Preserve((ClientData) interp);
294
      if (Tcl_VarEval(interp, cmd, buf, (char *)NULL) == TCL_OK) {
295
        char *name = Tcl_GetStringResult(interp);
296
        if (name && *name) {
297
          /* If a result was returned, check to see if it is a known tag */
298
          entryPtr = Tcl_FindHashEntry(tablePtr->tagTable, name);
299
        }
300
      }
301
      Tcl_Release((ClientData) interp);
302
      Tcl_ResetResult(interp);
303
    }
304
  }
305
  return (TableTag *) (entryPtr ? Tcl_GetHashValue(entryPtr) : NULL);
306
}
307
 
308
/*
309
 *----------------------------------------------------------------------
310
 *
311
 * TableCleanupTag --
312
 *      Releases the resources used by a tag before it is freed up.
313
 *
314
 * Results:
315
 *      None.
316
 *
317
 * Side effects:
318
 *      The tag is no longer valid.
319
 *
320
 *----------------------------------------------------------------------
321
 */
322
void
323
TableCleanupTag(Table *tablePtr, TableTag *tagPtr)
324
{
325
  if (tagPtr->image)
326
    Tk_FreeImage(tagPtr->image);
327
  /* free the options in the widget */
328
  Tk_FreeOptions(tagConfig, (char *) tagPtr, tablePtr->display, 0);
329
}
330
 
331
/*
332
 *--------------------------------------------------------------
333
 *
334
 * TableTagCmd --
335
 *      This procedure is invoked to process the tag method
336
 *      that corresponds to a widget managed by this module.
337
 *      See the user documentation for details on what it does.
338
 *
339
 * Results:
340
 *      A standard Tcl result.
341
 *
342
 * Side effects:
343
 *      See the user documentation.
344
 *
345
 *--------------------------------------------------------------
346
 */
347
int
348
TableTagCmd(Table * tablePtr, register Tcl_Interp *interp,
349
            int argc, char *argv[])
350
{
351
  int result = TCL_OK, retval, i, newEntry, value;
352
  int row, col;
353
  TableTag *tagPtr;
354
  Tcl_HashEntry *entryPtr, *scanPtr, *newEntryPtr, *oldEntryPtr;
355
  Tcl_HashTable *hashTblPtr;
356
  Tcl_HashSearch search;
357
  Tk_Image image;
358
  char buf[INDEX_BUFSIZE], *keybuf, *yes = "1", *no = "0";
359
 
360
  /* parse the next argument */
361
  retval = Cmd_Parse(interp, tag_cmds, argv[2]);
362
  switch (retval) {
363
    /* failed to parse the argument, error */
364
  case 0:
365
    return TCL_ERROR;
366
 
367
  case TAG_CELLTAG:
368
    /* tag a (group of) cell(s) */
369
    if (argc < 4) {
370
      Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
371
                       " tag cell tag ?arg arg ...?\"", (char *) NULL);
372
      return TCL_ERROR;
373
    }
374
    /* are we deleting */
375
    if (!(*argv[3]))
376
      tagPtr = NULL;
377
    else {
378
      /* check to see if the tag actually exists */
379
      if ((entryPtr = Tcl_FindHashEntry(tablePtr->tagTable, argv[3]))==NULL) {
380
        /* Unknown tag, just return empty string */
381
        Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
382
        return TCL_OK;
383
      }
384
      /* get the pointer to the tag structure */
385
      tagPtr = (TableTag *) Tcl_GetHashValue (entryPtr);
386
    }
387
 
388
    /* No more args -> display only */
389
    if (argc == 4) {
390
      /* Added special handling for tags: active, flash, sel, title */
391
 
392
      if ((tablePtr->flags & HAS_ACTIVE) && strcmp(argv[3], "active") == 0) {
393
        TableMakeArrayIndex(tablePtr->activeRow+tablePtr->rowOffset,
394
                            tablePtr->activeCol+tablePtr->colOffset, buf);
395
        Tcl_AppendElement(interp, buf);
396
      } else if (tablePtr->flashMode && strcmp(argv[3], "flash") == 0) {
397
        for (scanPtr = Tcl_FirstHashEntry(tablePtr->flashCells, &search);
398
             scanPtr != NULL; scanPtr = Tcl_NextHashEntry(&search)) {
399
          Tcl_AppendElement(interp,
400
                            Tcl_GetHashKey(tablePtr->flashCells, scanPtr));
401
        }
402
      } else if (strcmp(argv[3], "sel") == 0) {
403
        for (scanPtr = Tcl_FirstHashEntry(tablePtr->selCells, &search);
404
             scanPtr != NULL; scanPtr = Tcl_NextHashEntry(&search)) {
405
          Tcl_AppendElement(interp,
406
                            Tcl_GetHashKey(tablePtr->selCells, scanPtr));
407
        }
408
      } else if (strcmp(argv[3], "title") == 0 &&
409
                 (tablePtr->titleRows || tablePtr->titleCols)) {
410
        for (row = tablePtr->rowOffset;
411
             row < tablePtr->rowOffset+tablePtr->rows; row++) {
412
          for (col = tablePtr->colOffset;
413
               col < tablePtr->colOffset+tablePtr->titleCols; col++) {
414
            TableMakeArrayIndex(row, col, buf);
415
            Tcl_AppendElement(interp, buf);
416
          }
417
        }
418
        for (row = tablePtr->rowOffset;
419
             row < tablePtr->rowOffset+tablePtr->titleRows; row++) {
420
          for (col = tablePtr->colOffset+tablePtr->titleCols;
421
               col < tablePtr->colOffset+tablePtr->cols; col++) {
422
            TableMakeArrayIndex(row, col, buf);
423
            Tcl_AppendElement(interp, buf);
424
          }
425
        }
426
      } else {
427
        for (scanPtr = Tcl_FirstHashEntry(tablePtr->cellStyles, &search);
428
             scanPtr != NULL; scanPtr = Tcl_NextHashEntry(&search)) {
429
          /* is this the tag pointer for this cell */
430
          if ((TableTag *) Tcl_GetHashValue(scanPtr) == tagPtr) {
431
            Tcl_AppendElement(interp,
432
                              Tcl_GetHashKey(tablePtr->cellStyles, scanPtr));
433
          }
434
        }
435
      }
436
      return TCL_OK;
437
    }
438
    /* Now loop through the arguments and fill in the hash table */
439
    for (i = 4; i < argc; i++) {
440
      /* can I parse this argument */
441
      if (TableGetIndex(tablePtr, argv[i], &row, &col) != TCL_OK) {
442
        return TCL_ERROR;
443
      }
444
      /* get the hash key ready */
445
      TableMakeArrayIndex(row, col, buf);
446
 
447
      /* is this a deletion */
448
      if (tagPtr == NULL) {
449
        oldEntryPtr = Tcl_FindHashEntry(tablePtr->cellStyles, buf);
450
        if (oldEntryPtr != NULL)
451
          Tcl_DeleteHashEntry(oldEntryPtr);
452
      } else {
453
        /* add a key to the hash table */
454
        newEntryPtr = Tcl_CreateHashEntry(tablePtr->cellStyles, buf,
455
                                           &newEntry);
456
 
457
        /* and set it to point to the Tag structure */
458
        Tcl_SetHashValue (newEntryPtr, (ClientData) tagPtr);
459
      }
460
      /* now invalidate the area */
461
      TableRefresh(tablePtr, row-tablePtr->rowOffset,
462
                   col-tablePtr->colOffset, CELL);
463
    }
464
    return TCL_OK;
465
 
466
  case TAG_COLTAG:
467
  case TAG_ROWTAG:
468
    /* tag a row or a column */
469
    if (argc < 4) {
470
      Tcl_AppendResult(interp, "wrong # args: should be \"",
471
                        argv[0], " tag ", (retval == TAG_ROWTAG) ? "row" :
472
                        "col", " tag ?arg arg ..?\"", (char *) NULL);
473
      return TCL_ERROR;
474
    }
475
    /* if the tag is null, we are deleting */
476
    if (!(*argv[3]))
477
      tagPtr = NULL;
478
    else {                      /* check to see if the tag actually exists */
479
      if ((entryPtr = Tcl_FindHashEntry(tablePtr->tagTable, argv[3]))==NULL) {
480
        /* Unknown tag, just return empty string */
481
        Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
482
        return TCL_OK;
483
      }
484
      /* get the pointer to the tag structure */
485
      tagPtr = (TableTag *) Tcl_GetHashValue (entryPtr);
486
    }
487
 
488
    /* and choose the correct hash table */
489
    hashTblPtr = (retval == TAG_ROWTAG) ?
490
      tablePtr->rowStyles : tablePtr->colStyles;
491
 
492
    /* No more args -> display only */
493
    if (argc == 4) {
494
      for (scanPtr = Tcl_FirstHashEntry(hashTblPtr, &search);
495
           scanPtr != NULL; scanPtr = Tcl_NextHashEntry(&search)) {
496
        /* is this the tag pointer on this row */
497
        if ((TableTag *) Tcl_GetHashValue (scanPtr) == tagPtr) {
498
          sprintf(buf, "%d", (int) Tcl_GetHashKey (hashTblPtr, scanPtr));
499
          Tcl_AppendElement(interp, buf);
500
        }
501
      }
502
      return TCL_OK;
503
    }
504
    /* Now loop through the arguments and fill in the hash table */
505
    for (i = 4; i < argc; i++) {
506
      /* can I parse this argument */
507
      if (Tcl_GetInt(interp, argv[i], &value) != TCL_OK) {
508
        return TCL_ERROR;
509
      }
510
      /* deleting or adding */
511
      if (tagPtr == NULL) {
512
        oldEntryPtr = Tcl_FindHashEntry(hashTblPtr, (char *) value);
513
        if (oldEntryPtr != NULL)
514
          Tcl_DeleteHashEntry(oldEntryPtr);
515
      } else {
516
        /* add a key to the hash table */
517
        newEntryPtr = Tcl_CreateHashEntry(hashTblPtr, (char *) value,
518
                                          &newEntry);
519
 
520
        /* and set it to point to the Tag structure */
521
        Tcl_SetHashValue (newEntryPtr, (ClientData) tagPtr);
522
      }
523
      /* and invalidate the row or column affected */
524
      if (retval == TAG_ROWTAG) {
525
        TableRefresh(tablePtr, value-tablePtr->rowOffset, 0, ROW);
526
      } else {
527
        TableRefresh(tablePtr, 0, value-tablePtr->colOffset, COL);
528
      }
529
    }
530
    return TCL_OK;      /* COLTAG && ROWTAG */
531
 
532
  case TAG_CGET:
533
    if (argc != 5) {
534
      Tcl_AppendResult(interp, "wrong # args: should be \"",
535
                       argv[0], " tag cget tagName option\"", (char *) NULL);
536
      return TCL_ERROR;
537
    }
538
    if ((entryPtr=Tcl_FindHashEntry(tablePtr->tagTable, argv[3])) == NULL) {
539
      Tcl_AppendResult(interp, "invalid tag name \"", argv[3],
540
                       "\"", (char *) NULL);
541
      return TCL_ERROR;
542
    } else {
543
      tagPtr = (TableTag *) Tcl_GetHashValue (entryPtr);
544
      result = Tk_ConfigureValue(interp, tablePtr->tkwin, tagConfig,
545
                                 (char *) tagPtr, argv[4], 0);
546
    }
547
    return result;      /* CGET */
548
 
549
  case TAG_CONFIGURE:
550
    if (argc < 4) {
551
      Tcl_AppendResult(interp, "wrong # args: should be \"",
552
                       argv[0], " tag configure tagName ?arg arg  ...?\"",
553
                       (char *) NULL);
554
      return TCL_ERROR;
555
    }
556
    /* first see if this is a reconfiguration */
557
    entryPtr = Tcl_CreateHashEntry(tablePtr->tagTable, argv[3], &newEntry);
558
    if (newEntry) {
559
      /* create the structure */
560
      tagPtr = TableNewTag();
561
 
562
      /* insert it into the table */
563
      Tcl_SetHashValue(entryPtr, (ClientData) tagPtr);
564
 
565
      /* configure the tag structure */
566
      result = Tk_ConfigureWidget(interp, tablePtr->tkwin, tagConfig,
567
                                  argc - 4, argv + 4, (char *) tagPtr, 0);
568
      if (result == TCL_ERROR)
569
        return TCL_ERROR;
570
    } else {
571
      /* pointer wasn't null, do a reconfig if we have enough arguments */
572
      /* get the tag pointer from the table */
573
      tagPtr = (TableTag *) Tcl_GetHashValue(entryPtr);
574
 
575
      /* 5 args means that there are values to replace */
576
      if (argc > 5) {
577
        /* and do a reconfigure */
578
        result = Tk_ConfigureWidget(interp, tablePtr->tkwin,
579
                                    tagConfig, argc - 4, argv + 4,
580
                                    (char *) tagPtr, TK_CONFIG_ARGV_ONLY);
581
        if (result == TCL_ERROR)
582
          return TCL_ERROR;
583
      }
584
    }
585
 
586
    /* handle change of image name */
587
    if (tagPtr->imageStr) {
588
      image = Tk_GetImage(interp, tablePtr->tkwin, tagPtr->imageStr,
589
                          TableImageProc, (ClientData)tablePtr);
590
      if (image == NULL)
591
        result = TCL_ERROR;
592
    } else {
593
      image = NULL;
594
    }
595
    if (tagPtr->image) {
596
      Tk_FreeImage(tagPtr->image);
597
    }
598
    tagPtr->image = image;
599
 
600
    /*
601
     * If there were less than 6 args, we need
602
     * to do a printout of the config, even for new tags
603
     */
604
    if (argc < 6) {
605
      result = Tk_ConfigureInfo(interp, tablePtr->tkwin, tagConfig,
606
                                (char *) tagPtr, (argc == 5)?argv[4]:0, 0);
607
    } else {
608
      /* Otherwise we reconfigured so invalidate the table for a redraw */
609
      TableInvalidateAll(tablePtr, 0);
610
    }
611
    return result;
612
 
613
  case TAG_DELETE:
614
    /* delete a tag */
615
    if (argc < 4) {
616
      Tcl_AppendResult(interp, "wrong # args: should be \"",
617
               argv[0], " tag delete tagName ?tagName ...?\"", (char *) NULL);
618
      return TCL_ERROR;
619
    }
620
    /* run through the remaining arguments */
621
    for (i = 3; i < argc; i++) {
622
      /* cannot delete the title tag */
623
      if (strcmp(argv[i], "title") == 0 || strcmp (argv[i], "sel") == 0 ||
624
          strcmp(argv[i], "flash") == 0 || strcmp (argv[i], "active") == 0) {
625
        Tcl_AppendResult(interp, "cannot delete ", argv[i],
626
                          " tag", (char *) NULL);
627
        return TCL_ERROR;
628
      }
629
      if ((entryPtr = Tcl_FindHashEntry(tablePtr->tagTable, argv[i]))!=NULL) {
630
        /* get the tag pointer */
631
        tagPtr = (TableTag *) Tcl_GetHashValue(entryPtr);
632
 
633
        /* delete all references to this tag in rows */
634
        scanPtr = Tcl_FirstHashEntry(tablePtr->rowStyles, &search);
635
        for (; scanPtr != NULL; scanPtr = Tcl_NextHashEntry(&search))
636
          if ((TableTag *)Tcl_GetHashValue(scanPtr) == tagPtr)
637
            Tcl_DeleteHashEntry(scanPtr);
638
 
639
        /* delete all references to this tag in cols */
640
        scanPtr = Tcl_FirstHashEntry(tablePtr->colStyles, &search);
641
        for (; scanPtr != NULL; scanPtr = Tcl_NextHashEntry(&search))
642
          if ((TableTag *)Tcl_GetHashValue(scanPtr) == tagPtr)
643
            Tcl_DeleteHashEntry(scanPtr);
644
 
645
        /* delete all references to this tag in cells */
646
        scanPtr = Tcl_FirstHashEntry(tablePtr->cellStyles, &search);
647
        for (; scanPtr != NULL; scanPtr = Tcl_NextHashEntry(&search))
648
          if ((TableTag *)Tcl_GetHashValue(scanPtr) == tagPtr)
649
            Tcl_DeleteHashEntry(scanPtr);
650
 
651
        /* release the structure */
652
        TableCleanupTag(tablePtr, tagPtr);
653
        ckfree((char *) tagPtr);
654
 
655
        /* and free the hash table entry */
656
        Tcl_DeleteHashEntry(entryPtr);
657
      }
658
    }
659
    /* since we deleted a tag, redraw the screen */
660
    TableInvalidateAll(tablePtr, 0);
661
    return result;
662
 
663
  case TAG_EXISTS:
664
    if (argc != 4) {
665
      Tcl_AppendResult(interp, "wrong # args: should be \"",
666
                        argv[0], " tag exists tagName\"", (char *) NULL);
667
      return TCL_ERROR;
668
    }
669
    if (Tcl_FindHashEntry(tablePtr->tagTable, argv[3]) != NULL) {
670
      Tcl_SetResult(interp, yes, TCL_VOLATILE);
671
    } else {
672
      Tcl_SetResult(interp, no, TCL_VOLATILE);
673
    }
674
    return TCL_OK;
675
 
676
  case TAG_INCLUDES:
677
    /* does a tag contain a index ? */
678
    if (argc != 5) {
679
      Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
680
                        " tag includes tag index\"", (char *) NULL);
681
      return TCL_ERROR;
682
    }
683
    /* check to see if the tag actually exists */
684
    if ((entryPtr = Tcl_FindHashEntry(tablePtr->tagTable, argv[3])) == NULL) {
685
      /* Unknown tag, just return no */
686
      Tcl_SetResult(interp, no, TCL_VOLATILE);
687
      return TCL_OK;
688
    }
689
    /* parse index */
690
    if (TableGetIndex (tablePtr, argv[4], &row, &col) != TCL_OK) {
691
      return TCL_ERROR;
692
    }
693
    /* create hash key */
694
    TableMakeArrayIndex(row, col, buf);
695
 
696
    if (strcmp(argv[3], "active") == 0) {
697
      if (tablePtr->activeRow+tablePtr->rowOffset == row &&
698
          tablePtr->activeCol+tablePtr->colOffset == col)
699
        Tcl_SetResult(interp, yes, TCL_VOLATILE);
700
      else
701
        Tcl_SetResult(interp, no, TCL_VOLATILE);
702
      return TCL_OK;
703
    } else if (strcmp(argv[3], "flash") == 0) {
704
      if (tablePtr->flashMode && Tcl_FindHashEntry(tablePtr->flashCells, buf))
705
        Tcl_SetResult(interp, yes, TCL_VOLATILE);
706
      else
707
        Tcl_SetResult(interp, no, TCL_VOLATILE);
708
      return TCL_OK;
709
    } else if (strcmp(argv[3], "sel") == 0) {
710
      if (Tcl_FindHashEntry(tablePtr->selCells, buf))
711
        Tcl_SetResult(interp, yes, TCL_VOLATILE);
712
      else
713
        Tcl_SetResult(interp, no, TCL_VOLATILE);
714
      return TCL_OK;
715
    } else if (strcmp(argv[3], "title") == 0) {
716
      if (row < tablePtr->titleRows+tablePtr->rowOffset ||
717
          col < tablePtr->titleCols+tablePtr->colOffset)
718
        Tcl_SetResult(interp, yes, TCL_VOLATILE);
719
      else
720
        Tcl_SetResult(interp, no, TCL_VOLATILE);
721
      return TCL_OK;
722
    }
723
 
724
    /* get the pointer to the tag structure */
725
    tagPtr = (TableTag *) Tcl_GetHashValue(entryPtr);
726
    scanPtr = Tcl_FindHashEntry(tablePtr->cellStyles, buf);
727
    /* look to see if there is a cell, row, or col tag for this cell */
728
    if ((scanPtr && ((TableTag *) Tcl_GetHashValue(scanPtr) == tagPtr)) ||
729
        (tagPtr == FindRowColTag(tablePtr, row, ROW)) ||
730
        (tagPtr == FindRowColTag(tablePtr, col, COL))) {
731
      /* yes there is - return true */
732
      Tcl_SetResult(interp, yes, TCL_VOLATILE);
733
      return TCL_OK;
734
    }
735
    Tcl_SetResult(interp, no, TCL_VOLATILE);
736
    return TCL_OK;
737
 
738
  case TAG_NAMES:
739
    /* just print out the tag names */
740
    if (argc != 3 && argc != 4) {
741
      Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
742
                       " tag names ?pattern?\"", (char *) NULL);
743
      return TCL_ERROR;
744
    }
745
    entryPtr = Tcl_FirstHashEntry(tablePtr->tagTable, &search);
746
    while (entryPtr != NULL) {
747
      keybuf = Tcl_GetHashKey(tablePtr->tagTable, entryPtr);
748
      if (argc == 3 || Tcl_StringMatch(keybuf, argv[3]))
749
        Tcl_AppendElement(interp, keybuf);
750
      entryPtr = Tcl_NextHashEntry(&search);
751
    }
752
    return TCL_OK;
753
  }
754
  return TCL_OK;
755
}
756
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.