Version 4 of A simple slideshow

Updated 2002-05-02 10:26:26

Arjen Markus The script below is an experiment in a more sophisticated use of the text widget than what I usually do. It is also a joke - with two Tcl events this year, 2002, why not use Tcl/Tk for a presentation?

Note that the current version is far from completed. But you will get an idea anyway.


   # present.tcl --
   #
   #    Script to show slides, using a Wiki-like format for entering the text
   #
   # Version information:
   #    version 0.1: initial implementation, april 2002

   # buildSlide --
   #    Build up a new slide
   #
   # Arguments:
   #    contents  List containing the text to be shown
   #
   # Result:
   #    None
   #
   # Side effects:
   #    Text window is filled with the new text
   #
   proc buildSlide { contents } {
      global textwindow
      global buttonNo

      $textwindow delete 0.0 end

      foreach {tag_text} $contents {
         foreach {tag text} $tag_text {break}
         switch -- $tag {
         "title" -
         "text"  -
         "code"  {
            $textwindow insert end "$text" $tag
         }
         "bullet" { $textwindow insert end "\t*\t$text" $tag }
         "image"  {
            $textwindow insert end "(Image not yet implemented)" $tag
         }
         "button" {
            incr buttonNo
            button $textwindow.button$buttonNo -command $text -text "Run"
            $textwindow window create end -window $textwindow.button$buttonNo
         }
         } ;# end switch
      }
   }

   # displayNewSlide --
   #    Move to a new slide (depending on the direction: next, previous, ...)
   #
   # Arguments:
   #    dir       Direction to take
   #
   # Result:
   #    None
   #
   # Side effects:
   #    Text window shows the new slide
   #
   proc displayNewSlide { dir } {
      global current_slide
      global number_slides
      global slide_contents

      switch -- $dir {
      "1"     { incr current_slide    }
      "-1"    { incr current_slide -1 }
      "begin" { set  current_slide  0 }
      "end"   { set  current_slide  $number_slides }
      default { return }
      }

      if { $current_slide < 0 } {
         set current_slide 0
      }
      if { $current_slide >= $number_slides } {
         set current_slide [expr {$number_slides-1}]
      }

      buildSlide $slide_contents($current_slide)
   }

   # mainWindow --
   #    Set up the main (text) window and bindings
   #
   # Arguments:
   #    None
   #
   # Result:
   #    None
   #
   # Side effect:
   #    Main window, tags and bindings defined
   #
   proc mainWindow { } {
      global textwindow

      set textwindow ".textwindow"

      text $textwindow -width 70 -height 25
      pack $textwindow -fill both

      font create Title -family Helvetica -size 24 -weight bold
      font create Text  -family Helvetica -size 16 -weight normal
      font create Code  -family Courier   -size 16 -weight bold

      $textwindow tag configure title  -justify center -font Title -wrap word
      $textwindow tag configure text   -justify left   -font Text  -wrap word
      $textwindow tag configure code   -justify left   -font Code  -wrap word
      $textwindow tag configure bullet -justify left   -font Text  -wrap word \
         -tabs ".5c center 1c left" -lmargin1 0 -lmargin2 1c

      bind $textwindow <KeyPress-space> {displayNewSlide 1}
      bind $textwindow <KeyPress-Down>  {displayNewSlide 1}
      bind $textwindow <KeyPress-Up>    {displayNewSlide -1}
      bind $textwindow <KeyPress-Home>  {displayNewSlide begin}
      bind $textwindow <KeyPress-End>   {displayNewSlide end}
   }

   # readSlides --
   #    Read a file containing the slides (in Wiki-like format)
   #
   # Arguments:
   #    filename
   #
   # Result:
   #    None
   #
   # Side effect:
   #    Main window, tags and bindings defined
   #
   # Note:
   #    No provision for bold or italic text yet, nor images
   #
   proc readSlides { filename } {
      global number_slides
      global slide_contents

      set number_slides  0
      set current       -1

      set infile [open $filename "r"]

      # Force the first slide
      set line "----"
      while 1 {

         switch -regexp -- $line {
         {----} {
            if { $current > -1} { puts $slide_contents($current) }
            incr number_slides
            incr current
            if { [gets $infile line] < 0 } {
               break
            }
            set slide_contents($current) [list [list "title" $line\n]]
            }
         {^ +[*] } {
            regexp {^ +[*] *(.*)} $line => text
            lappend slide_contents($current) [list "bullet" $text\n]
            }
         {^ +} {
            lappend slide_contents($current) [list "code" $line\n]
            }
         {^\[button:} {
            regexp {^\[button:(.*)\]} $line => command
            lappend slide_contents($current) [list "button" $command]
            }
         default {
            lappend slide_contents($current) [list "text" $line\n]
            }
         }

         # Get the next line
         #
         if { [gets $infile line] < 0 } {
            puts $line
            break
         }
      }
      if { $current > -1} { puts $slide_contents($current) }

      close $infile
   }

   #
   # Main code
   #

   readSlides [lindex $argv 0]

   mainWindow

   global buttonNo
   global slide_contents
   global current_slide
   global number_slides

   set buttonNo 0
   displayNewSlide begin

   #
   # Take the focus - in this order!
   #
   focus $::textwindow
   focus -force .

The following is an example of the possible input (all the text between **** and **** - just ignore the Wiki formatting, it is after all almost the Wiki format) **** This is the first slide Some text

  • Bullet 1
  • Bullet 2
  • Bullet 3

Yet another line


This is the second slide Some text, followed by code:

  proc aha {
     puts "hm"
  }

****


This is the last slide - a demo! button:tk_messageBox -type ok -message "Wow!" -icon info


A script that also exploits the possibilities of the simple Wiki format: htext. See also Canvas presentation graphics


Arts and crafts of Tcl-Tk programming