# SrcMenuBar
# Copyright 2000 Red Hat, Inc.
#
# 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.

# ----------------------------------------------------------------------
# Implements a menubar that is attached to a source window.
#
#   PUBLIC ATTRIBUTES:
#
#
#   METHODS:
#
#     configure ....... used to change public attributes
#
#   PRIVATE METHODS
#
#   X11 OPTION DATABASE ATTRIBUTES
#
#
# ----------------------------------------------------------------------

class SrcMenuBar {
  inherit GDBMenuBar

  # ------------------------------------------------------------------
  #  CONSTRUCTOR - create widget
  # ------------------------------------------------------------------
  constructor {src args} {
    set source $src

    if {! [create_menu_items]} {
      destroy $this
    } else {
      [winfo toplevel $itk_interior] configure -menu $Menu
    }

    eval itk_initialize $args
    add_hook gdb_idle_hook "$this enable_ui 1"
    add_hook gdb_busy_hook "$this enable_ui 0"
    add_hook gdb_no_inferior_hook "$this enable_ui 2"
    add_hook gdb_set_hook "$this set_hook"
    add_hook control_mode_hook "$this set_control_mode"
  }

  # ------------------------------------------------------------------
  #  DESTRUCTOR - destroy window containing widget
  # ------------------------------------------------------------------
  destructor {
    remove_hook gdb_idle_hook "$this enable_ui 1"
    remove_hook gdb_busy_hook "$this enable_ui 0"
    remove_hook gdb_no_inferior_hook "$this enable_ui 2"
    remove_hook gdb_set_hook "$this set_hook"
    remove_hook control_mode_hook "$this set_control_mode"

    #destroy $this
  }

  ####################################################################
  # The next set of functions create the common menu groupings that
  # are used in gdb menus.
  # Private.  Used at contruction time.
  # These were previously at the GDBToolBar...
  ####################################################################

  # ------------------------------------------------------------------
  #  METHOD:  create_menu_items - Add some menu items to the menubar.
  #                               Returns 1 if any items added.
  # ------------------------------------------------------------------
  private method create_menu_items {} {

    create_file_menu

    create_run_menu

    create_view_menu

    if {[pref get gdb/control_target]} {
      create_control_menu
    }

    if {[pref get gdb/mode]} {
      create_trace_menu
    }

    create_pref_menu
    
    create_help_menu

    return 1
  }

  # ------------------------------------------------------------------
  #  METHOD:  create_file_menu - Creates the standard file menu. 
  # ------------------------------------------------------------------
  
  private method create_file_menu {} {
    global enable_external_editor tcl_platform

    menubar_new_menu file "File" 0

    if {[info exists enable_external_editor] && $enable_external_editor} {
      menubar_add_menu_command None "Edit Source" \
	[code $source edit]
    }

    menubar_add_menu_command Other "Open..."  \
      "_open_file" -underline 0 -accelerator "Ctrl+O"

    menubar_add_menu_command Other "Source..." \
      "source_file" -underline 0

    set sessions [session_list]
    if {[llength $sessions]} {
      menubar_add_menu_separator
      set i 1
      foreach item $sessions {
	menubar_add_menu_command Other "$i $item" \
	  [list session_load $item] \
	  -underline 0
	incr i
      }
    }

    menubar_add_menu_separator

    if {$tcl_platform(platform) == "windows"} {
      menubar_add_menu_command None "Page Setup..." \
	[format {
	  set top %s
	  ide_winprint page_setup -parent $top
	} [winfo toplevel [namespace tail $this]]] \
	-underline 8
    }

    menubar_add_menu_command None "Print Source..." \
      [code $source print] \
      -underline 0 -accelerator "Ctrl+P"

    menubar_add_menu_separator

    menubar_add_menu_command Other "Target Settings..." "set_target_name" \
      -underline 0

    menubar_add_menu_separator

    menubar_add_menu_command None "Exit" gdbtk_quit -underline 1
  }

  # ------------------------------------------------------------------
  #  METHOD:  create_run_menu - Creates the standard run menu, 
  #  or reconfigures it if it already exists.
  # ------------------------------------------------------------------
  
  private method create_run_menu {} {

    if {![menubar_menu_exists Run]} {
      set run_menu [menubar_new_menu run "Run" 0]
    } else {
      set run_menu [menubar_clear_menu Run]
    }
    
    set is_native [TargetSelection::native_debugging]

    # If we are on a Unix target, put in the attach options.  "ps" doesn't
    # give me the Windows PID yet, and the attach also seems flakey, so 
    # I will hold off on the Windows implementation for now.

    if {$is_native} {
      if {[string compare $::tcl_platform(platform) windows] != 0} {
	menubar_add_menu_command Attach "Attach to process" \
	  [code $this do_attach $run_menu] \
	  -underline 0 -accelerator "Ctrl+A"
      }
    } else {
      menubar_add_menu_command Other "Connect to target" \
	"$this do_connect $run_menu" -underline 0
    }

    if {[pref get gdb/control_target]} {
      if {!$is_native} {
	menubar_add_menu_command Other "Download" Download::download_it \
	  -underline 0 -accelerator "Ctrl+D"
      }
      menubar_add_menu_command Other "Run" [code $source inferior run] \
        -underline 0 -accelerator R
    }

    if {$is_native} {
      if {[string compare $::tcl_platform(platform) windows] != 0} {
	menubar_add_menu_command Detach "Detach" \
          [code $this do_detach $run_menu] \
	  -underline 0 -state disabled
      }
    } else {
      menubar_add_menu_command Other "Disconnect"  \
	[code $this do_disconnect $run_menu] -underline 0 -state disabled
    }

    if {$is_native} {
      menubar_add_menu_separator
      menubar_add_menu_command Control "Kill" [code $this do_kill $run_menu] \
	-underline 0 -state disabled
    }

    if { [pref get gdb/mode] } {
      menubar_add_menu_separator 

      menubar_add_menu_command Other "Start collection" "$this do_tstop" \
	-underline 0 -accelerator "Ctrl+B"
         
      menubar_add_menu_command Other "Stop collection" "$this do_tstop" \
	-underline 0  -accelerator "Ctrl+E" -state disabled
    }
  }

  # ------------------------------------------------------------------
  #  METHOD:  create_view_menu - Creates the standard view menu
  # ------------------------------------------------------------------
  
  private method create_view_menu {} {

    menubar_new_menu view "View" 0

    menubar_add_menu_command Other "Stack" {ManagedWin::open StackWin} \
      -underline 0 -accelerator "Ctrl+S" 
      
    menubar_add_menu_command Other "Registers" {ManagedWin::open RegWin} \
      -underline 0 -accelerator "Ctrl+R" 
      
    menubar_add_menu_command Other "Memory" {ManagedWin::open MemWin} \
      -underline 0 -accelerator "Ctrl+M" 
      
    menubar_add_menu_command Other "Watch Expressions" \
      {ManagedWin::open WatchWin} \
      -underline 0 -accelerator "Ctrl+W" 
    menubar_add_menu_command Other "Local Variables" \
      {ManagedWin::open LocalsWin} \
      -underline 0 -accelerator "Ctrl+L" 

    if {[pref get gdb/control_target]} {
      menubar_add_menu_command Other "Breakpoints" \
	{ManagedWin::open BpWin -tracepoints 0} \
	-underline 0 -accelerator "Ctrl+B" 
    }

    if {[pref get gdb/mode]} {
      menubar_add_menu_command Other "Tracepoints" \
        {ManagedWin::open BpWin -tracepoints 1} \
	-underline 0 -accelerator "Ctrl+T"
      menubar_add_menu_command Other "Tdump" {ManagedWin::open TdumpWin} \
	-underline 2 -accelerator "Ctrl+U"
    }

    menubar_add_menu_command Other "Console" {ManagedWin::open Console} \
      -underline 2 -accelerator "Ctrl+N" 
      
    menubar_add_menu_command Other "Function Browser" \
      {ManagedWin::open BrowserWin} \
      -underline 1 -accelerator "Ctrl+F" 
    menubar_add_menu_command Other "Thread List" \
      {ManagedWin::open ProcessWin} \
      -underline 0 -accelerator "Ctrl+H"
    if {[info exists ::env(GDBTK_DEBUG)] && $::env(GDBTK_DEBUG)} {
      menubar_add_menu_separator
      menubar_add_menu_command Other "Debug Window" \
        {ManagedWin::open DebugWin} \
	-underline 3 -accelerator "Ctrl+U"
    }
  }

  # ------------------------------------------------------------------
  #  METHOD:  create_control_menu - Creates the standard control menu
  # ------------------------------------------------------------------
  
  private method create_control_menu {} {

    menubar_new_menu cntrl "Control" 0
    
    menubar_add_menu_command Control "Step" [code $source inferior step] \
      -underline 0 -accelerator S
    
    menubar_add_menu_command Control "Next" [code $source inferior next] \
      -underline 0 -accelerator N
    
    menubar_add_menu_command Control "Finish" [code $source inferior finish] \
      -underline 0 -accelerator F
    
    menubar_add_menu_command Control "Continue" \
      [code $source inferior continue] \
      -underline 0 -accelerator C
    
    menubar_add_menu_separator
    menubar_add_menu_command Control "Step Asm Inst" \
      [code $source inferior stepi] \
      -underline 1 -accelerator S
    
    menubar_add_menu_command Control "Next Asm Inst" \
      [code $source inferior nexti] \
      -underline 1 -accelerator N
    
    # menubar_add_menu_separator
    # menubar_add_menu_command Other "Automatic Step" auto_step
  }

  # ------------------------------------------------------------------
  #  METHOD:  create_trace_menu - Creates the standard trace menu
  # ------------------------------------------------------------------
  
  private method create_trace_menu {} {

    menubar_new_menu trace "Trace" 0
    
    menubar_add_menu_command Other "Save Trace Commands..." \
      "save_trace_commands" \
      -underline 0

    menubar_add_menu_separator

    menubar_add_menu_command Trace "Next Hit" {tfind_cmd tfind} \
      -underline 0 -accelerator N
    
    menubar_add_menu_command Trace "Previous Hit" {tfind_cmd "tfind -"} \
      -underline 0 -accelerator P
    
    menubar_add_menu_command Trace "First Hit" {tfind_cmd "tfind start"} \
      -underline 0 -accelerator F
    
    menubar_add_menu_command Trace "Next Line Hit" {tfind_cmd "tfind line"} \
      -underline 5 -accelerator L
    
    menubar_add_menu_command Trace "Next Hit Here" \
      {tfind_cmd "tfind tracepoint"} \
      -underline 9 -accelerator H
    
    menubar_add_menu_separator
    menubar_add_menu_command Trace "Tfind Line..." \
      "ManagedWin::open TfindArgs -Type LN" \
      -underline 9 -accelerator E
    
    menubar_add_menu_command Trace "Tfind PC..." \
      "ManagedWin::open TfindArgs -Type PC" \
      -underline 7 -accelerator C
    
    menubar_add_menu_command Trace "Tfind Tracepoint..." \
      "ManagedWin::open TfindArgs -Type TP" \
      -underline 6 -accelerator T

    menubar_add_menu_command Trace "Tfind Frame..." \
      "ManagedWin::open TfindArgs -Type FR" \
      -underline 6 -accelerator F
  }

  # ------------------------------------------------------------------
  #  METHOD:  create_pref_menu - Creates the standard preferences menu
  # ------------------------------------------------------------------  
  private method create_pref_menu {} {

    menubar_new_menu pref "Preferences" 0
    
    menubar_add_menu_command Other "Global..." \
      "ManagedWin::open GlobalPref -transient" -underline 0
    
    menubar_add_menu_command Other "Source..." \
      "ManagedWin::open SrcPref -transient" -underline 0
  }

  # ------------------------------------------------------------------
  #  METHOD:  create_help_menu - Creates the standard help menu
  # ------------------------------------------------------------------  
  private method create_help_menu {} {

    menubar_new_menu help "Help" 0
    menubar_add_menu_command Other "Help Topics" \
      {HtmlViewer::open_help index.html} \
      -underline 0
    menubar_add_menu_separator
    menubar_add_menu_command Other "About GDB..." \
      {ManagedWin::open About -transient} \
      -underline 0
  }

  ####################################################################
  # 
  ####################################################################

  # ------------------------------------------------------------------
  #  METHOD:  set_control_mode - sets up the srcbar for browsing 
  #  a trace experiment.
  #   mode: 1 => browse mode
  #         0 => control mode
  # ------------------------------------------------------------------
  method set_control_mode  {mode} {
    debug "set_control_mode called with mode $mode"
    if {$mode} {
      set Browsing 1
      enable_ui 1
    } else {
      set Browsing 0
      enable_ui 1
    }
  }

  # ------------------------------------------------------------------
  # METHOD:  do_attach: attach to a running target
  # ------------------------------------------------------------------
  method do_attach {menu} {
      gdbtk_attach_native
  }

  # ------------------------------------------------------------------
  # METHOD:  do_detach: detach from a running target
  # ------------------------------------------------------------------
  method do_detach {menu} {
    gdbtk_disconnect
    gdbtk_idle
  }

  # ------------------------------------------------------------------
  # METHOD:  do_kill: kill the current target
  # ------------------------------------------------------------------
  method do_kill {menu} {
    gdb_cmd "kill"
    run_hooks gdb_no_inferior_hook
  }
  
  # ------------------------------------------------------------------
  # METHOD:  do_connect: connect to a remote target 
  #                      in asynch mode if async is 1
  # ------------------------------------------------------------------
  method do_connect {menu {async 0}} {

    set successful [gdbtk_connect $async]

    if {$successful} {
      $menu entryconfigure "Connect to target" -state disabled
      $menu entryconfigure "Disconnect" -state normal
    } else {
      $menu entryconfigure "Connect to target" -state normal
      $menu entryconfigure "Disconnect" -state disabled
    }

    # Make the menu reflect this change
    ::update idletasks
  }

  # ------------------------------------------------------------------
  # METHOD:  do_disconnect: disconnect from a remote target 
  #                               in asynch mode if async is 1.   
  #   
  # ------------------------------------------------------------------
  method do_disconnect {menu {async 0}} {
    debug "$menu $async"
    #
    # For now, these are the same, but they might be different...
    # 

    gdbtk_disconnect $async

    $menu entryconfigure "Connect to target" -state normal
    $menu entryconfigure "Disconnect" -state disabled
  }

  # ------------------------------------------------------------------
  # METHOD:  do_tstop: Change the GUI state, then do the tstop or
  #                    tstart command, whichever is appropriate.   
  #   
  # ------------------------------------------------------------------
  method do_tstop {} {
    debug "do_tstop called... Collecting is $Collecting"

    # FIXME: This must be done in conjuntion with the buttons
    # or the states won't match

    if {!$Collecting} {
      #
      # Start the trace experiment
      #

      if {$Browsing} {
	set ret [tk_messageBox -title "Warning" -message \
"You are currently browsing a trace experiment. 
This command will clear the results of that experiment.
Do you want to continue?" \
		   -icon warning -type okcancel -default ok]
	if {[string compare $ret cancel] == 0} {
	  return
	}
	set_control_mode 1
      }
      if {[tstart]} {
        # FIXME: Must enable the Stop Collection menu item and
        # disable the Start Collection item
	set Collecting 1
      } else {
	tk_messageBox -title Error -message "Error downloading tracepoint info" \
	  -icon error -type ok
      }
    } else {
      #
      # Stop the trace experiment
      #

      if {[tstop]} {	
        # FIXME: Must enable the Stop Collection menu item and
        # disable the Start Collection item
	set Collecting 0
     }
    }
  }

  # ------------------------------------------------------------------
  #  METHOD:  set_hook - run when user enters a `set' command.
  #
  #  FIXME: Should not be accessing the base class internal data
  #         As the spec says, one must clear the menu and recreate it.
  # ------------------------------------------------------------------  
  method set_hook {varname value} {
    debug "Got $varname = $value"

    if {$varname == "os"} {
      # Make current_menu pointer point to the View Menu.
      # FIXME: Should not be accessing the base class internal data directly
      set view_menu [menu_find View]
      # Restore the current_menu pointer.
      set save_menu [menubar_set_current_menu $view_menu]
      set title "Kernel Objects"

      # Look for the KOD menu entry...
      if {[catch {$view_menu index $title} index]} {
	set index none
      }

      # FIXME: This assumes that the KOD menu is the last one as it does not
      # adjust the index information kept by the GDBMenuBar class.
      if {$value == ""} {
	# No OS, so remove KOD from View menu.
	if {$index != "none"} {
          # FIXME: Should not be accessing the base class internal data
	  $view_menu delete $index
	}
      } else {
	# Add KOD to View menu, but only if it isn't already there.
	if {$index == "none"} {
	  menubar_add_menu_command Other $title {ManagedWin::open KodWin} \
	    -underline 0 -accelerator "Ctrl+K"
	}
      }

      # Restore the current_menu pointer.
      menubar_set_current_menu $save_menu

      global gdb_kod_cmd
      set gdb_kod_cmd $value
    }
  }

  ####################################################################
  # The following method enables/disables both menus and buttons.
  ####################################################################

  # ------------------------------------------------------------------
  # METHOD:  enable_ui - enable/disable the appropriate buttons and menus
  # Called from the busy, idle, and no_inferior hooks.
  #
  # on must be:
  # value      Control    Other    Trace    State
  #   0          off       off      off     gdb is busy
  #   1          on        on       off     gdb has inferior, and is idle
  #   2          off       on       off     gdb has no inferior, and is idle
  # ------------------------------------------------------------------
  public method enable_ui {on} {
    global tcl_platform
    debug "$on - Browsing=$Browsing"

    # Do the enabling so that all the disabling happens first, this way if a
    # button belongs to two groups, enabling takes precedence, which is
    # probably right.

    switch $on {
      0 {
	set enable_list {Control disabled \
			   Other disabled \
			   Trace disabled \
			   Attach disabled \
			   Detach disabled}
      }
      1 {
	if {!$Browsing} {
	  set enable_list {Trace disabled \
			     Control normal \
			     Other normal \
			     Attach disabled \
			     Detach normal }
#	  # set the states of stepi and nexti correctly
#	  _set_stepi
	} else {
	  set enable_list {Control disabled Other normal Trace normal}
	}

      }
      2 {
	set enable_list {Control disabled \
			   Trace disabled \
			   Other normal \
			   Attach normal \
			   Detach disabled }
      }
      default {
	debug "Unknown type: $on in enable_ui"
	return
      }
    }

    set_class_state $enable_list
  }

  ####################################################################
  #
  #  PRIVATE DATA
  #
  ####################################################################

  # This is a handle on our parent source window.
  private variable source {}

  ####################################################################
  #
  #  PUBLIC DATA
  #
  ####################################################################

  # The next two determine the state of the application when Tracing is enabled.

  public variable Browsing   0  ;# Are we currently browsing a trace experiment?
  public variable Collecting 0  ;# Are we currently collecting a trace exp.?
}
