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

# this example shows some bonobo functions
load ../src/gnocl.so

if { [llength [info commands gnocl::bonobo]] == 0} {
   puts "*********************************************"
   puts "The bonobo command is disabled by default."
   puts "Please edit the Makefile and recompile gnocl."
   puts "*********************************************"
   exit -1
}

proc fillClist {} {
   set res ""
   foreach el [gnocl::bonobo listServices] {
      set run [gnocl::bonobo activateService [lindex $el 0] -type existingOnly]
      lappend res [linsert $el 3 [expr {$run?"yes":"no"}]]
   }
   $::clistWidget configure -children $res
}

proc activate { how id } {
   if { [catch {gnocl::bonobo $how $id} res] } {
      gnocl::dialog "Error in activating" %#Ok -type error -text $res
   }
   fillClist
}

proc showMenu {} {
   set sel [$::clistWidget getSelection]
   set men [gnocl::menu]
   set id [$::clistWidget getCellText $sel 0]
   $men add [gnocl::menuItem label -text "activate $id" \
         -command "activate activateService $id"]
   foreach el [$::clistWidget getCellText $sel 2] {
      $men add [gnocl::menuItem label -text "activate any $el" \
            -command "activate activateRepoID $el"]
   }
   $men popup
}


set menu [gnocl::menu]
$menu add [gnocl::menuItem label -text "%#_Quit" -command exit \
      -tooltip "Quit program"]

set file [gnocl::menuItem label -text "%__File" -submenu $menu]

set menu [gnocl::menu]
$menu add [gnocl::menuItem label -text "%#_About" \
      -tooltip "Show about dialog" \
      -command {gnocl::about -title "Bonobo Services" \
      -comments {Test application for remote activation} \
      -version [gnocl::info version] \
      -authors {{Peter G. Baum <peter@dr-baum.net>}}}]
set help [gnocl::menuItem label -text "%__Help" -submenu $menu]

set toolbar [gnocl::toolbar -style both]
$toolbar add item -text "Quit" -icon "%#Quit" -tooltip "Quit Application" \
            -command exit
$toolbar add space
$toolbar add item -text "Refresh" -icon "%#Refresh" -tooltip "Refresh List" \
            -command fillClist

set clistWidget [gnocl::clist -columns 5 \
      -titles {ID Type Interfaces Running Description} \
      -columnClicked {%w sort %c}]
      # -children [gnocl::bonobo listServices]
      # -selectionChanged {if { %s } { selected [%w getCellText %r 0] } } 
fillClist

gnocl::app testApp -title "Bonobo test" \
      -menubar [gnocl::menubar -children [list $file $help]] \
      -statusbar [gnocl::appBar] -toolbar $toolbar \
      -contents $clistWidget -width 500 -height 400

gnocl::bind $clistWidget {buttonRelease 3} showMenu

# if sourced from wish withdraw tk toplevel window, else
# enter gtk main loop
if { [info exists tk_patchLevel] } {
   wm withdraw .
} else {
   gnocl::mainLoop
}
