GRIDPLUS2 - Inserting a Tablelist Row or Text Line
Home Reference Manpage Examples Download License Contact

Inserting a Tablelist Row or Text Line

GRIDPLUS provides the gpinsert command to insert a Tablelist row or Text line.

In both cases the value of the "null" array item set.

Inserting a Tablelist Row

A Tablelist row is inserted at the specified position. The position is a row number (where the first row is "0") -or- "end".

Syntax:-

gpinsert item position row-data

For Example:

gpinsert .mytable 0 {{Column one} {Column Two}}

...Will insert the specified row data at the begining of ".mytable".

gpinsert .mytable end {{Column one} {Column two}}

...Will insert the specified row data at the end of ".mytable".

Note: To have the Tablelist display adjust to make the inserted row visible use the -seeinsert option when creating the Tablelist.

Inserting a Text Line

A Text line is insert at the specified position. The position is a line number (where the first line is "1") -or- "end".

Syntax:-

gpinsert item position line

For Example:

gpinsert .mytext 1 "This is some text"

...Will insert the specified line at the begining of ".mytext".

gpinsert .mytext end "This is some text"

...Will insert the specified line at the end of ".mytext".

Note: To have the Text display adjust to make the inserted line visible use the -seeinsert option when creating the Text.

Example

This example has the following functionality:-

Window:

If the "Text" then "Table" buttons are pressed the following is displayed:-

...Then selecting the "Middle" radiobutton and pressing the "Text" then "Table" buttons...

...Then selecting the "End" radiobutton and pressing the "Text" then "Table" buttons...

Source Code:

#==================================================#
# Procedure invoked when "Text" button is pressed. #
#==================================================#
proc target,text {} {
   global {}

   switch -- $(.position) {
      begining {set position 1}
      middle   {set position [expr {([.mytext.text count -lines 1.0 end] / 2) + 1}]}
      end      {set position end}
   }

   gpinsert .mytext $position "This is some text $(COUNT) - $(.position)"

   incr (COUNT)
}

#====================================================#
# Proocedure invoked when "Table" button is pressed. #
#====================================================#
proc target,table {} {
   global {}

   switch -- $(.position) {
      begining {set position 0}
      middle   {set position [expr {([.mytable.tablelist size] / 2) + 1}]}
      end      {set position end}
   }

   gpinsert .mytable $position [subst {{This is a row $(COUNT)} {$(.position)}}]

   incr (COUNT)
}

#==================================================================#
# Create text, tableist, buttons, radiobuttons and display window. #
#==================================================================#

gridplus text .mytext -scroll y -title "Text"

gridplus tablelist .mytable -scroll y -title "Tablelist" {
   0 Column1
   0 Column2
}

gridplus button .target -title "Target" {
   {"Text"  .text}
   {"Table" .table}
}

gridplus radiobutton .position -title "Position" {
   {.begining "Begining" +begining}
   {.middle   "Middle"   -middle}
   {.end      "End"      -end}
}

gridplus layout .main -wtitle "Example" {
   .mytext     -
   .mytable    -
   .target:nsw .position:e
}

pack .main

gpset COUNT 0
gpset .mytext "Start of text\nEnd of text\n"
gpset .mytable {{{First row} {Begining}} {{Last row} {End}}} 

Comments:

proc target,text {} {
   global {}

   switch -- $(.position) {
      begining {set position 1}
      middle   {set position [expr {([.mytext.text count -lines 1.0 end] / 2) + 1}]}
      end      {set position end}
   }

   gpinsert .mytext $position "This is some text $(COUNT) - $(.position)"

   incr (COUNT)
}

Creates a procedure called "target,text" which is invoked by pressing the "Text" button.

The "position" variable is set according to the position selected by the ".position" radiobutton.

Note: The actual text widget created for ".mytext" is called ".mytext.text". The "middle" option gets the number of lines in the text widget and approximates a line position in the middle of the text currently in the widget.

proc target,table {} {
   global {}

   switch -- $(.position) {
      begining {set position 0}
      middle   {set position [expr {([.mytable.tablelist size] / 2) + 1}]}
      end      {set position end}
   }

   gpinsert .mytable $position [subst {{This is a row $(COUNT)} {$(.position)}}]

   incr (COUNT)
}

Creates a procedure called "target,table" which is invoked by pressing the "Table" button.

The "position" variable is set according to the position selected by the ".position" radiobutton.

Note: The actual tablelist widget created for ".mytable" is called ".mytable.tablelist". The "middle" option gets the number of rows in the tablelist widget and approximates a row position in the middle of the data currently in the widget.

gridplus text .mytext -scroll y -title "Text"

Creates a GRIDPLUS text called ".mytext".
gridplus tablelist .mytable -scroll y -title "Tablelist" {
   0 Column1
   0 Column2
}

Creates a GRIDPLUS tablelist called ".mytable".
gridplus button .target -title "Target" {
   {"Text"  .text}
   {"Table" .table}
}

Creates a GRIDPLUS button grid called ".target".
gridplus radiobutton .position -title "Position" {
   {.begining "Begining" +begining}
   {.middle   "Middle"   -middle}
   {.end      "End"      -end}
}

Creates a GRIDPLUS radiobutton grid called ".position".
gridplus layout .main -wtitle "Example" {
   .mytext     -
   .mytable    -
   .target:nsw .position:e
}

pack .main

Creates and displayes a GRIDPLUS layout called ".main" to arrange the text, tablelist, buttons and radiobuttons.
gpset COUNT 0
gpset .mytext "Start of text\nEnd of text\n"
gpset .mytable {{{First row} {Begining}} {{Last row} {End}}} 

Populates ".mytext" and ".mytable" with suitable example data. The "COUNT" data item is an incrementing number which is included in the inserted data so that the inserted data can be recognised.


Copyright © 2013 Adrian Davis.