Unnamed Fossil Project

Check-in [422deb7b94]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:fixes and new code for treeview insert
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:422deb7b947ef2abba980340712330694aac32bc
User & Date: arnulf 2013-09-18 19:40:44
Context
2013-09-20
19:45
fixes for treeview insert and draw check-in: 9156f55936 user: arnulf tags: trunk
2013-09-18
19:40
fixes and new code for treeview insert check-in: 422deb7b94 user: arnulf tags: trunk
2013-09-17
19:42
new code for treeview insert check-in: 9640fda380 user: arnulf tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to library/ntkTTreeview.tcl.

15
16
17
18
19
20
21

22
23
24
25
26
27
28
..
73
74
75
76
77
78
79



80
81
82
83
84
85
86
...
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243


244
245
246
247
248
249
250
...
295
296
297
298
299
300
301

302
303
304
305
306
307
308
309
....
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
....
1034
1035
1036
1037
1038
1039
1040

1041
1042
1043
1044
1045
1046
1047
1048
....
1124
1125
1126
1127
1128
1129
1130
1131























































































1132
1133
1134
1135
1136
1137
1138
1139
1140

1141

1142
1143
1144
1145
1146
1147
1148
1149
....
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
....
1205
1206
1207
1208
1209
1210
1211
1212
1213

1214



1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
....
1230
1231
1232
1233
1234
1235
1236
1237
1238

1239
1240


1241


1242




1243

1244

1245
1246
1247
1248



1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334

1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
#--------------------------------------------------------------------------

namespace eval ::ntk {

::itcl::extendedclass treeview {
    inherit classes::widget classes::utils classes::scroll classes::tagset


    protected variable tree

    public option [list -columns columns Columns] -default ""
    public option [list -displaycolumns displayColumns DisplayColumns] -default "#all"
    public option [list -show show Show] -default "tree headings"
    public option [list -selectmode selectMode SelectMode] -default "extended"
    public option [list -height height Height] -default 10
................................................................................
    public method TreeviewDetachCommand {args} 
    public method TreeviewDragCommand {args} 
    public method TreeviewExistsCommand {args} 
    public method TreeviewFocusCommand {args} 
    public method TreeviewHeadingCommand {args} 
    public method TreeviewIdentifyCommand {args} 
    public method TreeviewIndexCommand {args} 



    private method ConfigureItem {item args}
    private method InsertPosition {parent index}
    private method EndPosition {parent}
    private method FindItem {itemName}
    public method TreeviewInsertCommand {args} 
    public method TreeviewItemCommand {args} 
    public method TreeviewMoveCommand {args} 
................................................................................
# NewItem --
#     Allocate a new, uninitialized, unlinked item
#=========================================================================

::itcl::body treeview::NewItem {} {
#puts stderr "NewItem!"
    set item [dict create]
    dict set item entryPtr 0
    dict set item parent ""
    dict set item children ""
    dict set item next ""
    dict set item prev ""

    dict set item state  0
    dict set item textObj ""
    dict set item imageObj ""
    dict set item valuesObj ""
    dict set item openObj ""
    dict set item tagsObj ""

    dict set item tagset ""
    dict set item imagespec ""



    return $item
}

#================================ TreeviewInitialize ============================
#     Initialization hooks.
#=========================================================================
................................................................................
    dict set tree endPtr ""

    # Create root item "":

    dict set tree root [NewItem]
#    Tk_InitOptions(interp, (ClientData)tv->tree.root, tv->tree.itemOptionTable, tv->core.tkwin);
    dict set tree root tagset [list]

    dict set tree root entryPtr [dict get $tree root]

    # Scroll handles:

    dict set tree xscrollHandle [CreateScrollHandle $core xscroll]
    dict set tree xscrollHandle xscroll $xscroll
    dict set tree yscrollHandle [CreateScrollHandle $core yscroll]
    dict set tree yscrollHandle yscroll $yscroll
................................................................................
#     Draw a sequence of items and their visible descendants.

# Returns:
#     Row number of the last item drawn.
#=========================================================================

::itcl::body treeview::DrawForest {item depth row} {
puts stderr "DrawForest!"
    while {$item ne "" && $row <= [dict get $tree yscroll last]} {
        set row [DrawSubtree $item $depth $row]
        set item [dict get $item next]
    }
    return $row;
}

................................................................................

::itcl::body treeview::TreeviewDisplay {} {
#puts stderr "TreeviewDisplay!"
    ::ntk::classes::layout::DrawLayout [dict get $core layout] [dict get $core state]
    if {[lsearch [dict get $tree showFlags] headings] >= 0 } {
        DrawHeadings
    }

#    DrawForest [dict get $tree root children] 0 0
}

#================================ TreeviewBBoxCommand ============================
#     
#=========================================================================

::itcl::body treeview::TreeviewBBoxCommand {args} {
................................................................................
#================================ TreeviewIndexCommand ============================
#     
#=========================================================================

::itcl::body treeview::TreeviewIndexCommand {args} {
puts stderr "TreeviewIndexCommand!$args!"
}
























































































#================================ ConfigureItem ============================
# ConfigureItem --
#     Set item options.
#=========================================================================

::itcl::body treeview::ConfigureItem {item args} {
puts stderr "ConfigureItem!$args!"
    set newImageSpec ""
    set newTagSet ""



    SetOptions $item $args savedOptions mask]

    # Make sure that -values is a valid list:
 
    if {[dict get $item valuesObj] ne ""} {
#        if (Tcl_ListObjLength(interp, item->valuesObj, &unused) != TCL_OK)
#            goto error;
    }
................................................................................
#    Tk_FreeSavedOptions(&savedOptions);
    if {[lsearch $mask itemOptionTagsChanged] >= 0} {
        dict set item tagset $newTagSet
    }
    if {[lsearch $mask itemOptionImageChanged] >= 0} {
        dict set item imagespec $newImageSpec
    }
    RedisplayWidget

#error:
#    Tk_RestoreSavedOptions(&savedOptions)
#    if (newTagSet) { Ttk_FreeTagSet(newTagSet); }
#    if (newImageSpec) { TtkFreeImageSpec(newImageSpec); }
#    return TCL_ERROR;
}
................................................................................
#     Locate the previous sibling for [$tree insert].
#
#     Returns a pointer to the item just before the specified index,
#     or 0 if the item is to be inserted at the beginning.
#=========================================================================

::itcl::body treeview::InsertPosition {parent index} {
puts stderr "InsertPosition!$parent!$index!"
    set prev ""

    set next [dict get $parent children]




    while {$next ne "" && $index > 0} {
        incr index -1
        set prev $next
        set next [dict get $prev next]
    }

    return $prev
}

#================================ EndPosition ============================
# EndPosition --
................................................................................
#     where the treeview is populated in breadth-first or
#     depth-first order using [$tv insert $parent end ...],
#     we cache the result from the last call to EndPosition()
#     and start the search from there on a cache hit.
#=========================================================================

::itcl::body treeview::EndPosition {parent} {
puts stderr "EndPosition!$parent!"
    set endPtr [dict get $tree endPtr]


    while {$endPtr ne "" && [dict get $endPtr parent] ne $parent} {


        set endPtr [dict get $endPtr parent]


    }




    if {$endPtr eq ""} {

        set endPtr [dict get $parent children]

    }

    if {$endPtr ne ""} {
        while {[dict get $endPtr next]} {



            set endPtr [dict get $endPtr next]
        }
        dict set tree endPtr $endPtr
    }

    return $endPtr
}

#================================ FindItem ============================
#     
#=========================================================================

::itcl::body treeview::FindItem {itemName} {
puts stderr "FindItem!$itemName!"
    if {$itemName eq ""} {
        return $itemName
    }
    if {![dict exists $tree items itemName]} {
        return -code error "Item $itemName not found"
    }
    return $itemName
}

#================================ TreeviewInsertCommand ============================
#     
#=========================================================================

::itcl::body treeview::TreeviewInsertCommand {args} {
puts stderr "TreeviewInsertCommand!$args!"
    # Get parent node:

    set parent [FindItem [lindex $args 0]]
#    if {$parent eq ""} {
#        return -code error "error in FindItem"
#    }

    # Locate previous sibling based on $index:

    if {[lindex $args 1] eq "end"} {
        set sibling [EndPosition $parent]
    } else {
        set index [lindex $args 2]
        if {![string is integer $index]} {
            return -code error "index is not integer"
        }
        set sibling [InsertPosition $parent $index]
    }

puts stderr "sibling!$sibling!"
    # Get node name:
    #     If -id supplied and does not already exist, use that;
    #     Otherwise autogenerate new one.
    set args [lrange $args 3 end]
    if {[llength $args] == 2 && [lindex $args 0] eq "-id"} {
        set itemName [lindex $args 1]

        if {[dict exists $tree items $itemName]} {
            return -code error "Item $itemName already exists"
        }
        dict set tree items $itemName [dict create]
        set args [lrange $args 2 end]
    } else {
        while {1} {
            dict set tree serial [expr {[dict get $tree serial] + 1}]
            set idbuf [format "I%03X" [dict get $tree serial]
            if {![dict exists $tree items $idbuf]} {
                dict set tree items $idbuf [dict create]
                set itemName $idbuf
                break
            }
        }
    }
puts stderr "itemName!$itemName!"

    # Create and configure new item:
 
    set newItem [NewItem]
#    Tk_InitOptions(newItem, tv->tree.itemOptionTable, tv->core.tkwin);
    dict set newItem tagset [GetTagSetFromObj [dict get $tv tagTable]]
    ConfigureItem $newItem $args

    # Store in hash table, link into tree:
 
    dict set tree items $itemName $newItem
    dict set newItem entryPtr itemName
    InsertItem $parent $sibling $newItem

    RedisplayWidget

    return [ItemID $newItem]
}

#================================ TreeviewItemCommand ============================
#     
#=========================================================================

::itcl::body treeview::TreeviewItemCommand {args} {







>







 







>
>
>







 







|





|








>
>







 







>
|







 







|







 







>
|







 








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>






|


>

>
|







 







|







 







|

>
|
>
>
>



|
|







 







|

>

|
>
>
|
>
>
|
>
>
>
>

>
|
>



|
>
>
>
|












|

|

|


|











<
<
<


<



|






|



|
|










|







|





|





|
|
>
|

|







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
..
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
...
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
...
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
....
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
....
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
....
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
....
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
....
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
....
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395



1396
1397

1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
#--------------------------------------------------------------------------

namespace eval ::ntk {

::itcl::extendedclass treeview {
    inherit classes::widget classes::utils classes::scroll classes::tagset

    protected variable item_id 0
    protected variable tree

    public option [list -columns columns Columns] -default ""
    public option [list -displaycolumns displayColumns DisplayColumns] -default "#all"
    public option [list -show show Show] -default "tree headings"
    public option [list -selectmode selectMode SelectMode] -default "extended"
    public option [list -height height Height] -default 10
................................................................................
    public method TreeviewDetachCommand {args} 
    public method TreeviewDragCommand {args} 
    public method TreeviewExistsCommand {args} 
    public method TreeviewFocusCommand {args} 
    public method TreeviewHeadingCommand {args} 
    public method TreeviewIdentifyCommand {args} 
    public method TreeviewIndexCommand {args} 
    private method ItemName {item}
    private method SetOptions {itemVar options savedOptionsVar maskVar}
    private method InsertItem {args}
    private method ConfigureItem {item args}
    private method InsertPosition {parent index}
    private method EndPosition {parent}
    private method FindItem {itemName}
    public method TreeviewInsertCommand {args} 
    public method TreeviewItemCommand {args} 
    public method TreeviewMoveCommand {args} 
................................................................................
# NewItem --
#     Allocate a new, uninitialized, unlinked item
#=========================================================================

::itcl::body treeview::NewItem {} {
#puts stderr "NewItem!"
    set item [dict create]
    dict set item entryPtr ""
    dict set item parent ""
    dict set item children ""
    dict set item next ""
    dict set item prev ""

    dict set item state  ""
    dict set item textObj ""
    dict set item imageObj ""
    dict set item valuesObj ""
    dict set item openObj ""
    dict set item tagsObj ""

    dict set item tagset ""
    dict set item imagespec ""
    dict set item id $item_id
    incr item_id

    return $item
}

#================================ TreeviewInitialize ============================
#     Initialization hooks.
#=========================================================================
................................................................................
    dict set tree endPtr ""

    # Create root item "":

    dict set tree root [NewItem]
#    Tk_InitOptions(interp, (ClientData)tv->tree.root, tv->tree.itemOptionTable, tv->core.tkwin);
    dict set tree root tagset [list]
    dict set tree items "" [dict get $tree root]
    dict set tree root entryPtr ""

    # Scroll handles:

    dict set tree xscrollHandle [CreateScrollHandle $core xscroll]
    dict set tree xscrollHandle xscroll $xscroll
    dict set tree yscrollHandle [CreateScrollHandle $core yscroll]
    dict set tree yscrollHandle yscroll $yscroll
................................................................................
#     Draw a sequence of items and their visible descendants.

# Returns:
#     Row number of the last item drawn.
#=========================================================================

::itcl::body treeview::DrawForest {item depth row} {
puts stderr "DrawForest!$item!$depth!$row!"
    while {$item ne "" && $row <= [dict get $tree yscroll last]} {
        set row [DrawSubtree $item $depth $row]
        set item [dict get $item next]
    }
    return $row;
}

................................................................................

::itcl::body treeview::TreeviewDisplay {} {
#puts stderr "TreeviewDisplay!"
    ::ntk::classes::layout::DrawLayout [dict get $core layout] [dict get $core state]
    if {[lsearch [dict get $tree showFlags] headings] >= 0 } {
        DrawHeadings
    }
::ntk::classes::layout::printDict [dict get $tree items ""]
    DrawForest [dict get $tree root children] 0 0
}

#================================ TreeviewBBoxCommand ============================
#     
#=========================================================================

::itcl::body treeview::TreeviewBBoxCommand {args} {
................................................................................
#================================ TreeviewIndexCommand ============================
#     
#=========================================================================

::itcl::body treeview::TreeviewIndexCommand {args} {
puts stderr "TreeviewIndexCommand!$args!"
}

#================================ ItemName ============================
#     
#=========================================================================

::itcl::body treeview::ItemName {item} {
#puts stderr "ItemName!$item!"
    return [dict get $tree items [dict get $item entryPtr]]
}

#================================ SetOptions ============================
#     
#=========================================================================

::itcl::body treeview::SetOptions {itemVar options savedOptionsVar maskVar} {
    upvar $savedOptionsVar savedOptions
    upvar $maskVar mask
    upvar $itemVar item

    set options [lindex $options 0]
#puts stderr "SetOptions!$item!\n$options!"
    dict set savedOptions recordPtr $item
    dict set savedOptions numItems 0
    dict set savedOptions nextPtr ""
    set lastSavePtr $savedOptions

    set mask ""
    while {[llength $options] > 0} {
        set option [lindex $options 0]
        if {[llength $options] < 2} {
            return -code error "value for \"$option\" missing"
        }
        set value [lindex $options 1]
        dict set item optionsTable $option $value
#        lappend mask typeMask
        set options [lrange $options 2 end]
    }
}

#================================ InsertItem ============================
# InsertItem --
#       Insert an item into the tree after the specified item.
#
# Preconditions:
#      item is currently detached
#      prev != NULL ==> prev->parent == parent.
#=========================================================================

::itcl::body treeview::InsertItem {parentVar prevVar itemVar} {
    upvar $parentVar parent
    upvar $prevVar prev
    upvar $itemVar item

#puts stderr "InsertItem!parent!$parent!\nprev!$prev!\nitem!$item!"
    if {$parent ne "" && [dict exists $parent entryPtr]} {
        dict set item parent [dict get $parent entryPtr]
    } else {
        dict set item parent ""
    }
    if {$prev ne ""} {
        if {$prev ne "" [dict exists $prev entryPtr]} {
            dict set item prev [dict get $prev entryPtr]
        } else {
            dict set item prev ""
        }
    } else {
        dict set item prev $prev
    }
    if {$prev ne ""} {
        if {[dict exists $prev next entryPtr]} {
            dict set item next [dict get $prev next entryPtr]
        } else {
            dict set item next ""
        }
        dict set prev next [dict get $item entryPtr]
    } else {
        if {[dict exists $parent children entryPtr]} {
            dict set item next [dict get $parent children entryPtr]
        } else {
            dict set item next ""
        }
        dict set parent children [dict get $item entryPtr]
    }
    if {[dict get $item next] ne ""} {
        dict set item next prev [dict get $item entryPtr]
    }
}

#================================ ConfigureItem ============================
# ConfigureItem --
#     Set item options.
#=========================================================================

::itcl::body treeview::ConfigureItem {item args} {
#puts stderr "ConfigureItem!$item!\n$args!"
    set newImageSpec ""
    set newTagSet ""
    set mask ""

    set savedOptions [dict create]
    SetOptions item $args savedOptions mask

    # Make sure that -values is a valid list:
 
    if {[dict get $item valuesObj] ne ""} {
#        if (Tcl_ListObjLength(interp, item->valuesObj, &unused) != TCL_OK)
#            goto error;
    }
................................................................................
#    Tk_FreeSavedOptions(&savedOptions);
    if {[lsearch $mask itemOptionTagsChanged] >= 0} {
        dict set item tagset $newTagSet
    }
    if {[lsearch $mask itemOptionImageChanged] >= 0} {
        dict set item imagespec $newImageSpec
    }
    RedisplayWidget ""

#error:
#    Tk_RestoreSavedOptions(&savedOptions)
#    if (newTagSet) { Ttk_FreeTagSet(newTagSet); }
#    if (newImageSpec) { TtkFreeImageSpec(newImageSpec); }
#    return TCL_ERROR;
}
................................................................................
#     Locate the previous sibling for [$tree insert].
#
#     Returns a pointer to the item just before the specified index,
#     or 0 if the item is to be inserted at the beginning.
#=========================================================================

::itcl::body treeview::InsertPosition {parent index} {
#puts stderr "InsertPosition!$parent!$index!"
    set prev ""
    if {$parent ne "" && [dict exists $parent children entryPtr]} {
        set next [dict get $parent children entryPtr]
    } else {
        set next ""
    }

    while {$next ne "" && $index > 0} {
        incr index -1
        set prev [dict get $next entryPtr]
        set next [dict get $prev next entryPtr]
    }

    return $prev
}

#================================ EndPosition ============================
# EndPosition --
................................................................................
#     where the treeview is populated in breadth-first or
#     depth-first order using [$tv insert $parent end ...],
#     we cache the result from the last call to EndPosition()
#     and start the search from there on a cache hit.
#=========================================================================

::itcl::body treeview::EndPosition {parent} {
#puts stderr "EndPosition!$parent!"
    set endPtr [dict get $tree endPtr]
#puts stderr "endPtr!$endPtr!"

    while {$endPtr ne ""} {
        if {[dict exists $endPtr parent entryPtr] && [dict exists $parent entryPtr]} {
            if {[dict get $endPtr parent entryPtr] ne [dict get $parent entryPtr]} {
                set endPtr [dict get $endPtr parent entryPtr]
            } else {
                break
            }
        } else {
            break
        }
    }
    if {$endPtr eq ""} {
        if {[dict exists $endPtr parent entryPtr]} {
            set endPtr [dict get $parent children entryPtr]
        }
    }

    if {$endPtr ne ""} {
        while {1} {
            if {![dict exists $endPtr next entryPtr]} {
                break
            }
            set endPtr [dict get $endPtr next entryPtr]
        }
        dict set tree endPtr $endPtr
    }

    return $endPtr
}

#================================ FindItem ============================
#     
#=========================================================================

::itcl::body treeview::FindItem {itemName} {
#puts stderr "FindItem!$itemName!"
    if {$itemName eq ""} {
        return [dict get $tree items $itemName]
    }
    if {![dict exists $tree items $itemName]} {
        return -code error "Item $itemName not found"
    }
    return [dict get $tree items $itemName]
}

#================================ TreeviewInsertCommand ============================
#     
#=========================================================================

::itcl::body treeview::TreeviewInsertCommand {args} {
puts stderr "TreeviewInsertCommand!$args!"
    # Get parent node:

    set parent [FindItem [lindex $args 0]]




    # Locate previous sibling based on $index:

    if {[lindex $args 1] eq "end"} {
        set sibling [EndPosition $parent]
    } else {
        set index [lindex $args 1]
        if {![string is integer $index]} {
            return -code error "index is not integer"
        }
        set sibling [InsertPosition $parent $index]
    }

#puts stderr "sibling!$sibling!"
    # Get node name:
    #     If -id supplied and does not already exist, use that;
    #     Otherwise autogenerate new one.
    set args [lrange $args 2 end]
    if {[llength $args] >= 2 && [lindex $args 0] eq "-id"} {
        set itemName [lindex $args 1]

        if {[dict exists $tree items $itemName]} {
            return -code error "Item $itemName already exists"
        }
        dict set tree items $itemName [dict create]
        set args [lrange $args 2 end]
    } else {
        while {1} {
            dict set tree serial [expr {[dict get $tree serial] + 1}]
            set idbuf [format "I%03X" [dict get $tree serial]]
            if {![dict exists $tree items $idbuf]} {
                dict set tree items $idbuf [dict create]
                set itemName $idbuf
                break
            }
        }
    }
#puts stderr "itemName!$itemName!"

    # Create and configure new item:
 
    set newItem [NewItem]
#    Tk_InitOptions(newItem, tv->tree.itemOptionTable, tv->core.tkwin);
#    dict set newItem tagset [GetTagSetFromObj [dict get $tree tagTable]]
    ConfigureItem $newItem $args

    # Store in hash table, link into tree:
 
    dict set tree items $itemName $newItem
    dict set newItem entryPtr $itemName
    InsertItem parent sibling newItem
    dict set tree items $itemName $newItem
    RedisplayWidget ""

    return [ItemName $newItem]
}

#================================ TreeviewItemCommand ============================
#     
#=========================================================================

::itcl::body treeview::TreeviewItemCommand {args} {