#!/bin/sh
# the next line restarts using wish \
exec wish "$0" "$@"

set    auto_path      "[pwd] $auto_path"
set    tk_strictMotif 1
set    mdw_lib        ./mdw_lib.tk


  if {[catch "source $mdw_lib"]} {
    puts stderr "\nCant find the mdw_lib.tk - file (the mdw-library) !!"
    puts stderr "Make sure it is in the same directory as the mdw_demo ?"
    puts stderr "Please refer to the file README for help.\n"
    exit 1
  }

########################################################################
# Now we can start with the real important code
########################################################################
global mdw_demo

########################################################################
## Create the Main Window
########################################################################
  wm title        . "mdw_lib - Demo"
  wm iconname     . "mdw_lib - Demo"
  wm maxsize      . [winfo screenwidth .] [winfo screenheight .]
  wm minsize      . 640 480
  wm geometry     . 640x480

  frame .menu -relief raised   -borderwidth 1
  pack  .menu -side top -fill  x

  # Generate an info line
  frame .info -relief raised -borderwidth 1
  pack  .info -side bottom -fill x

  set mdw_demo(info) ".info.l"
  label $mdw_demo(info) -text "mdw-Demo" -relief sunken -width 24
  pack  $mdw_demo(info) -side left

  # create a Menu bar
  menubutton .menu.file    -text "File"      -menu .menu.file.m -underline 0
  menubutton .menu.windows -text "Windows"   -menu .menu.windows.m -underline 0

  pack       .menu.file .menu.windows -side left -padx 2m -pady 1m

  # after the menubuttons we can create the multi document frame
  global mdw_main ; # a mdw_lib variable to access the mdw_main_frame

  # call the mdw's mainframe constructor
  mdw_main::mdw_main mdw_demo .mdw_frame

  set mdw_main(maxchildren)      10
  set mdw_main(window_menu_path) ".menu.windows"
  set mdw_main(frameopts)        "-relief groove -width 640 \
                                  -height 480"
  set mdw_main(packopts)         "-side top -fill both -expand true"

  # now make the mdw mainframe visible
  mdw_main::create_mdw_frame

  # Pull Down Menus, the windows pulldown menu will be created by the 
  # mdw_lib itself
  menu .menu.file.m -tearoff 0
       .menu.file.m add command -label "New" \
                                -command {set no [mdw_demo::CreateNewChild]
                                          mdw_demo::ActivateChild $no} \
                                -underline 0


########################################################################
## now some procedures
########################################################################

#######################################################################
## a user defined callback procedure to determine what happens when
## a mdw-child-window becomes active
#######################################################################
proc mdw_demo::ActivateChild {no} {
global mdw_demo mdw_child

  if {![winfo exist $mdw_demo(child,$no,textwidget)]} return
  focus $mdw_demo(child,$no,textwidget)

  # connect childs title to main windows state label
  $mdw_demo(info) configure -textvariable "mdw_demo(child,$no,title)"

  # for my multi document child
  set mdw_child($mdw_demo(child,$no,mdw_child),title) $mdw_demo(child,$no,title)
  mdw_main::activate_child $mdw_demo(child,$no,mdw_child)
}

#######################################################################
## a user defined callback procedure to determine what happens when
## a mdw-child-window will be deleted
## including the call to the destructor of the mdw-child
#######################################################################
proc mdw_demo::DeleteChild {no} {
global mdw_demo mdw_child
  puts stdout "Child: $mdw_demo(child,$no,title) was deleted\n"
  mdw_main::destroy_child $mdw_demo(child,$no,mdw_child)
  unset mdw_demo(child,$no,mdw_child)
}

###################################################################
## create a new mdw child window
###################################################################
proc mdw_demo::CreateNewChild {} {
global mdw_demo

  # get a class (a number) for the new child
  set no 0
  while {[info exist mdw_demo(child,$no,mdw_child)]} {
    incr no
  }

  # call the multi document childwindow's creation procedure
  # this will run childs constructor
  set mdw_demo(child,$no,mdw_child) [mdw_main::create_child]

  # max child windows reached
  if {$mdw_demo(child,$no,mdw_child) < 0} {
    return 0
  }

  global mdw_child

  # setup for the mdw child
  #########################

  # connect a close callback to the mdw child
  set mdw_child($mdw_demo(child,$no,mdw_child),close_cmd) \
      "mdw_demo::DeleteChild $no"

  # to get back the focus on activating, add an activation callback
  set mdw_child($mdw_demo(child,$no,mdw_child),activate_cmd) \
      "mdw_demo::ActivateChild $no"

  # give the new one a temporary title
  set mdw_demo(child,$no,title) "child $no"
  set mdw_child($mdw_demo(child,$no,mdw_child),title) $mdw_demo(child,$no,title)

  # make the child window visible
  mdw_child::create_window $mdw_demo(child,$no,mdw_child)

  # to put a textwidget into the mdw child's client_path
  set mdw_demo(child,$no,textwidget) \
      $mdw_child($mdw_demo(child,$no,mdw_child),client_path).tw

  # to put a scrollbar into the mdw child's client_path
  set mdw_demo(child,$no,scroll) \
      $mdw_child($mdw_demo(child,$no,mdw_child),client_path).sy

  # Generate text window with scrollbar
  text $mdw_demo(child,$no,textwidget) -relief groove -bd 2 \
                                       -yscrollcommand \
                                        "$mdw_demo(child,$no,scroll) set" \
                                       -wrap none \
                                       -highlightthickness 0 \
                                       -setgrid 1 \
                                       -width 80 -height 25 \
                                       -font "-*-*-medium-r-*-*-14-*-*-*-*-*-*-*" \
                                       -setgrid 0

  scrollbar $mdw_demo(child,$no,scroll) -relief sunken \
                                        -orient vertical \
                                        -width 12 \
                                        -command \
                                        "$mdw_demo(child,$no,textwidget) yview"

  pack $mdw_demo(child,$no,scroll)     -side right  -fill y
  pack $mdw_demo(child,$no,textwidget) -expand true -fill both

  bind $mdw_demo(child,$no,textwidget) <ButtonPress> "mdw_demo::ActivateChild $no"

  # after all is in show the child window
  mdw_child::show $mdw_demo(child,$no,mdw_child)

  mdw_demo::ActivateChild $no

return $no
}


  ## Now start some windows
  #########################
  set no [mdw_demo::CreateNewChild]
  set fd [open "mdw_demo" r]
  $mdw_demo(child,$no,textwidget) insert 1.0 [read $fd]
  close $fd
  set mdw_demo(child,$no,title) "The mdw-Demo sourcecode !"
  mdw_demo::ActivateChild $no

  set no [mdw_demo::CreateNewChild]
  set fd [open "mdw_lib.tk" r]
  $mdw_demo(child,$no,textwidget) insert 1.0 [read $fd]
  close $fd
  set mdw_demo(child,$no,title) "The mdw-Lib sourcecode !"
  mdw_demo::ActivateChild $no

  set no [mdw_demo::CreateNewChild]
  set fd [open "README.mdw" r]
  $mdw_demo(child,$no,textwidget) insert 1.0 [read $fd]
  close $fd
  set mdw_demo(child,$no,title) "The mdw-Lib README !"
  mdw_demo::ActivateChild $no

  # cascade children
  mdw_main::cascade


