managedwin.itb   [plain text]


# Managed window for GDBtk.
# Copyright 1998, 1999 Cygnus Solutions
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License (GPL) as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.


body ManagedWin::reconfig {} {}


body ManagedWin::window_name {wname {iname ""}} {
  set top [winfo toplevel [namespace tail $this]]
  wm title $top $wname
  if {$iname != ""} {
    wm iconname $top $iname
  } else {
    wm iconname $top $wname
  }
}

# ------------------------------------------------------------
# pickle - This is the base class pickle method.  It returns a
#  a command that can be used to recreate this particular window.  
# ------------------------------------------------------------
body ManagedWin::pickle {} {
  return [list ManagedWin::open [namespace tail [info class]]]
}

body ManagedWin::reveal {} {
  # Do this update to flush all changes before deiconifying the window.
  update idletasks
  
  set top [winfo toplevel [namespace tail $this]]
  raise $top
  wm deiconify $top
  
  # I don't understand this next line and no one commented it, so it's gone.
  #focus -force [focus -lastfor $top]
  
  focus $top
}

body ManagedWin::restart {} {
  # This is needed in case we've called "gdbtk_busy" before the restart.
  # This will configure the stop/run button as necessary
  after idle gdbtk_idle
  
  # call the reconfig method for each object
  foreach obj $manage_active {
    if {[catch {$obj reconfig} msg]} {
      dbug W "reconfig failed for $obj - $msg"
    } 
  }
}

# ------------------------------------------------------------------
#  shutdown - This writes all the active windows to the preferences file,
#  so they can be restored at startup.
#  FIXME: Currently assumes only ONE window per type...
# ------------------------------------------------------------------

body ManagedWin::shutdown {} {
  set activeWins {}
  foreach win $manage_active {
    if {[$win isa ManagedWin]} {
      lappend activeWins [$win pickle]
    }
  }
  pref set gdb/window/active $activeWins
}

# ------------------------------------------------------------------
#  startup - This restores all the windows that were opened at shutdown.
#  FIXME: Currently assumes only ONE window per type...
# ------------------------------------------------------------------

body ManagedWin::startup {} {
  debug "Got active list [pref get gdb/window/active]"

  foreach cmd [pref get gdb/window/active] {
    eval $cmd
  }
  ManagedWin::open SrcWin
}

body ManagedWin::open_dlg {class args} {
  
  set newwin [eval _open $class $args]
  if {$newwin != ""} {
    $newwin reveal
    $newwin post
  }
}


body ManagedWin::open {class args} {
  
  set newwin [eval _open $class $args]
  if {$newwin != ""} {
    if {[$newwin isa ModalDialog]} {
      after idle "$newwin reveal; $newwin post"
    } else {
      after idle "$newwin reveal"
    }
  }
  
  return $newwin
}

body ManagedWin::_open { class args } {
  debug "$class $args"
  
  parse_args force
  
  if {!$force} {
    # check all windows for one of this type
    foreach obj $manage_active {
      if {[$obj isa $class]} {
	$obj reveal
	return $obj
      }
    }
    
  }
  # need to create a new window
  return [eval _create $class $args]
}

body ManagedWin::_create { class args } {
  
  set win [string tolower $class]
  debug "win=$win args=$args"
  
  parse_args {center transient {over ""}} 
  
  # increment window numbers until we get an unused one
  set i 0
  while {[winfo exists .$win$i]} { incr i }
  
  while { 1 } {
    set top [toplevel .$win$i]
    wm withdraw $top
    wm protocol $top WM_DELETE_WINDOW "destroy $top"
    wm group $top .
    set newwin $top.$win
    if {[catch {uplevel \#0 eval $class $newwin $args} msg]} {
      dbug E "object creation of $class failed: $msg"
      dbug E $::errorInfo
      if {[string first "object already exists" $msg] != -1} {
	# sometimes an object is still really around even though
	# [winfo exists] said it didn't exist.  Check for this case
	# and increment the window number again.
	catch {destroy $top}
	incr i
      } else {
	return ""
      }
    } else {
      break
    }
  }
  
  if {[catch {pack $newwin -expand yes -fill both}]} {
    dbug W "packing of $newwin failed: $::errorInfo"
    return ""
  }
  
  wm maxsize $top $screenwidth $screenheight
  wm minsize $top 20 20
  
  if {$over != ""} {
    # center new window over widget
    set t [winfo toplevel [namespace tail $over]]
    set cx [expr {[winfo rootx $t] + [winfo width $t] / 2}]
    set cy [expr {[winfo rooty $t] + [winfo height $t] / 2}]
    set x [expr {$cx - [winfo reqwidth $top] / 2}]
    set y [expr {$cy - [winfo reqheight $top] / 2}]
    wm geometry $top +$x+$y    
  } elseif {$center} {
    # center the window on the screen
    set x [expr {[winfo screenwidth $top] / 2 - [winfo reqwidth $top] / 2}]
    set y [expr {[winfo screenheight $top] / 2 - [winfo reqheight $top] / 2}]
    wm geometry $top +$x+$y
  }
  
  if {$transient} {
    wm resizable $top 0 0
    wm transient $top .
  } elseif {$::tcl_platform(platform) == "unix"} {
    # Modal dialogs DONT get Icons...
    if {[pref get gdb/use_icons] && ![$newwin isa ModalDialog]} {
      set icon [make_icon_window ${top}_icon]
      wm iconwindow $top $icon
      bind $icon <Double-1> "$newwin reveal"
    }
  }
  
  if {[info exists ::env(GDBTK_TEST_RUNNING)] && $::env(GDBTK_TEST_RUNNING)} {
    set g "+100+100"
    wm geometry $top $g
    wm positionfrom $top user
  } else {
    set g [pref getd gdb/geometry/$newwin]
    if {$g == "1x1+0+0"} { 
      dbug E "bad geometry"
      set g ""
    }
    if {$g != ""} {
      # OK. We have a requested geometry. We know that it fits on the screen
      # because we set the maxsize.  Now we have to make sure it will not be
      # displayed off the screen.
      set w 0; set h 0; set x 0; set y 0
      if {![catch {scan $g  "%dx%d%d%d" w h x y} res]} {
	if {$x < 0} {
	  set x [expr $screenwidth + $x]
	}
	if {$y < 0} {
	  set y [expr $screenheight + $y]
	}
	
	# If the window is transient, then don't reset its size, since
	# the user didn't set this anyway, and in some cases where the
	# size can change dynamically, like the Global Preferences
	# dialog, this can hide parts of the dialog with no recourse...
	
	# if dont_remember_size is true, don't set size, just like
	# transients
	
	if {$transient || [dont_remember_size]} {
	  set g "+${x}+${y}"
	} else {
	  set g "${w}x${h}+${x}+${y}"
	}
	if {[expr $x+50] < $screenwidth && [expr $y+20] < $screenheight} {
	  wm geometry $top $g
	  wm positionfrom $top user
	}
      }
    }
  }

  bind $top <Alt-F4> [list delete object $newwin]

  return $newwin
}

body ManagedWin::find { win } {
  debug "$win"
  set res ""
  foreach obj $manage_active {
    if {[$obj isa $win]} {
      lappend res $obj
    }
  }
  return $res
}

body ManagedWin::enable { on } {
}


body ManagedWin::init {} {
  debug
  wm withdraw .
  set screenheight [winfo screenheight .]
  set screenwidth [winfo screenwidth .]
}

body ManagedWin::destroy_toplevel {} {
  after idle "update idletasks;destroy $Top"
}

body ManagedWin::freeze_me {} {
  $Top configure -cursor watch
  ::update idletasks
}

body ManagedWin::thaw_me {} {

  $Top configure -cursor {}
  ::update idletasks
}

# ------------------------------------------------------------------
#  make_icon_window - create a small window with an icon in
#  it for use by certain Unix window managers.
# ------------------------------------------------------------------

body ManagedWin::make_icon_window {name {file "gdbtk_icon"}} {
  if {![winfo exists $name]} {
    toplevel $name
    label $name.im -image \
      [image create photo icon_photo -file [file join $::gdb_ImageDir $file.gif]]
  }
  pack $name.im
  return $name
}