# Browswer window for Insight.
# Copyright (C) 1998, 1999, 2001, 2002, 2003 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 Browser window for Insight
#
# ----------------------------------------------------------------------

option add *BrowserWin.textBackground  $::Colors(textbg)

# ------------------------------------------------------------------
#  CONSTRUCTOR - create new browser window
# ------------------------------------------------------------------
itcl::body BrowserWin::constructor {args} {
  debug
  #eval itk_initialize $args 
  window_name "Function Browser"

  set _layout [pref get gdb/browser/layout]
  set Current(filename) {}
  set Current(function) {}
  _build_win

  eval itk_initialize $args
  add_hook file_changed_hook [code $this _fill_file_box]
}

# ------------------------------------------------------------------
#  DESTRUCTOR - destroy window containing widget
# ------------------------------------------------------------------
itcl::body BrowserWin::destructor {} {
  debug
  if {$filter_trace_after != ""} {
    after cancel $filter_trace_after
  }

  remove_hook file_changed_hook [code $this _fill_file_box]
  trace vdelete [pref varname gdb/search/last_symbol] \
    w [code $this _filter_trace_proc]
}

# ------------------------------------------------------------------
#  METHOD:  _build_win - build the main browser window
# ------------------------------------------------------------------
itcl::body BrowserWin::_build_win {} {
  debug

  # popup menu
  itk_component add popup {
    menu $itk_interior.pop -tearoff 0
  } {}
  set pop $itk_component(popup)
  $pop add command -label "Toggle Layout" -command [code $this _switch_layout]
  $pop add command -label "Help" -command "open_help browser.html"
  $pop add separator
  $pop add command -label "Close" -command "destroy [winfo toplevel $itk_interior]"
  bind [winfo toplevel $itk_interior] <3> "tk_popup $itk_component(popup) %X %Y"

  # Four Main Frames: filter, files, functions, and view (source)
  # Their layout depends on _layout

  if {$_layout == 1} {
    set p [cyg::panedwindow $itk_interior.p -orient vertical -height 5i \
	     -width 5i]
    $p add left
    set p2 [cyg::panedwindow [$p childsite left].p]
   
    $p2 add file
    _build_file_frame [$p2 childsite file]

    $p2 add filter
    set f [frame [$p2 childsite filter].f]
    _build_filter_frame $f
    _build_function_frame $f
    pack $f -fill both -expand yes
    pack $p2 -fill both -expand yes

    $p add view
    _build_view_frame [$p childsite view]

  } else {
    set p [cyg::panedwindow $itk_interior.p -height 5i -width 5i]
    $p add top
    set f [frame [$p childsite top].f]
    _build_filter_frame $f

    set browser [cyg::panedwindow $f.p -orient vertical]
    $browser add file
    _build_file_frame [$browser childsite file]
    $browser add function
    _build_function_frame [$browser childsite function]

    pack $browser -fill both -expand yes
    pack $f -fill both -expand yes

    $p add view
    _build_view_frame [$p childsite view]
  }
  
  # Fill file box
  _fill_file_box

  pack $p -fill both -expand yes
}

# ------------------------------------------------------------------
#  METHOD:  _filter_trace_proc
#           This is called when something is entered in the filter
#           box.  The actual filtering is done in an after to avoid
#           flashing too much if the user is typing quickly.
# ------------------------------------------------------------------
itcl::body BrowserWin::_filter_trace_proc {v1 v2 mode} {
  if {$filter_trace_after != ""} {
    after cancel $filter_trace_after
  }
  set filter_trace_after [after 100 [code $this _filter_trace_after]]
}

# ------------------------------------------------------------------
#  METHOD:  _filter_trace_after
#           This is a wrapper around search, needed to pass to trace
# ------------------------------------------------------------------
itcl::body BrowserWin::_filter_trace_after {} {
  set filter_trace_after ""
  search
}

# ------------------------------------------------------------------
#  METHOD:  _search_src
#           Search for text or jump to a specific line
#           in source window, going in the specified DIRECTION.
# ------------------------------------------------------------------
itcl::body BrowserWin::_search_src {direction} {
  set exp [$itk_component(view_search) get]
  $itk_component(view_src) search $exp $direction
}

# ------------------------------------------------------------------
#  METHOD:  search
#           Search for functions matching regexp/pattern
#           in specified files
# ------------------------------------------------------------------
itcl::body BrowserWin::search {} {

  set files [$itk_component(file_box) getcurselection]

  if {[llength $files] == 0} {
    return
  }

  _freeze_me

  set filt_pat [format $filter_regexp($cur_filter_mode) \
		  [pref get gdb/search/last_symbol]]

  if {[llength $files] == [$itk_component(file_box) size]} {
    set err [catch {gdb_search functions $filt_pat \
		    -filename 1} matches]
  } else {
    set err [catch {gdb_search functions $filt_pat \
		      -files $files -filename 1} matches]
  }

  if {$err} {
    debug "ERROR searching for [pref get gdb/search/last_symbol]: $matches"
    _thaw_me
    return
  }

  $itk_component(func_box) delete 0 end

  set i -1
  catch {unset index_to_file}

  foreach func [lsort -command "list_element_strcmp 0" $matches] {
    $itk_component(func_box) insert end [lindex $func 0]
    set index_to_file([incr i]) [lindex $func 1]
  }
  _thaw_me
}

# ------------------------------------------------------------------
#  METHOD:  _process_file_selection
#            This fills the func combo, and the more window if it
#            is currently open with the hit in the File combobox.
# ------------------------------------------------------------------
itcl::body BrowserWin::_process_file_selection {y} {

  set curIndex [$itk_component(file_box) nearest $y]
  set curSelection [$itk_component(file_box) curselection]

  # We got a button-release - First make sure the click selected the item...

  if {[lsearch $curIndex $curSelection] >= 0} {
    _fill_source [$itk_component(file_box) get $curIndex] 0
  } else {
    # If the item was deselected, go back to the first one in the list...
    # It would be better to keep a stack of the clicked items, and go to the
    # last one on the stack.  But in extended mode, this is tricky.  FIXME

    if {[llength $curSelection] > 0} {
      _fill_source [$itk_component(file_box) get [lindex $curSelection 0]] 0
    } else {
      _fill_source ""
    }
  }

  search
}

# ------------------------------------------------------------------
#  METHOD:  _process_func_selection
#            This points the  more window to the hit in the Func combobox
#            if it is currently open.
# ------------------------------------------------------------------
itcl::body BrowserWin::_process_func_selection {y} {

  set curIndex [$itk_component(func_box) nearest $y]
  set curSelection [$itk_component(func_box) curselection]

  # We got a button-release - First make sure the click selected the item...

  if {[lsearch $curIndex $curSelection] >= 0} {
    set funcName [$itk_component(func_box) get $curIndex]
    set fileName $index_to_file($curIndex)
    _fill_source $funcName 1 $fileName
  }
}

# ------------------------------------------------------------------
#  METHOD:  do_all_bp
#           Toggle a bp at every selected function in FuncLB
# ------------------------------------------------------------------
itcl::body BrowserWin::do_all_bp {onp} {

  set funcs [$itk_component(func_box) getcurselection]
  _freeze_me

  foreach f $funcs {
    if {[catch {gdb_loc $f} linespec]} {
      dbug W "Could not gdb_loc \"$f\""
      return
    }
    set bpnum [bp_exists $linespec]
    if {$bpnum == -1 && $onp} {

      # FIXME: gdb_set_bp is the preferred method, but it requires
      # a file and line number. This doesn't work very well for
      # templates...
      gdb_cmd "break $f"
    } elseif {!$onp} {
      catch {gdb_cmd "delete $bpnum"}
    }
  }
  _thaw_me
}

# ------------------------------------------------------------------
#  METHOD:  _toggle_bp
#           Toggle bp at function specified by the given Y
#           coordinate in the listbox
# ------------------------------------------------------------------
itcl::body BrowserWin::_toggle_bp {y} {

  set f [$itk_component(func_box) get [$itk_component(func_box) nearest $y]]
  if {$f != ""} {
    if {[catch {gdb_loc $f} linespec]} {
      return
    }
    set bpnum [bp_exists $linespec]
    if {$bpnum == -1} {	
      # FIXME: gdb_set_bp is the preferred method, but it requires
      # a file and line number. This doesn't work very well for
      # templates...
      gdb_cmd "break $f"
    } else {
      catch {gdb_cmd "delete $bpnum"}
    }
  }
}

# ------------------------------------------------------------------  
#  METHOD:  _select
#           Un/Highlight all files in the files list
# ------------------------------------------------------------------  
itcl::body BrowserWin::_select {highlight} {
  if {$highlight} {
    $itk_component(file_box) selection set 0 end
  } else {
    $itk_component(file_box) selection clear 0 end
  }
  search
}

# ------------------------------------------------------------------  
#  METHOD:  _set_filter_mode
#           React to changes in the filter mode
# ------------------------------------------------------------------  
itcl::body BrowserWin::_set_filter_mode {w mode} {
  if {[string compare $mode $cur_filter_mode] != 0} {
    set cur_filter_mode $mode
    pref set gdb/search/filter_mode $mode
    search
  }
}

# ------------------------------------------------------------------
#  METHOD:  _file_hide_h
#           Run when the "Hide .h files" preference is chosen.
# ------------------------------------------------------------------
itcl::body BrowserWin::_file_hide_h {} {
  _fill_file_box
  search
}

# ------------------------------------------------------------------
#  METHOD:  _fill_source
#           Helper function to fill the srctextwin
#           when needed.
# ------------------------------------------------------------------
itcl::body BrowserWin::_fill_source {f {funcp 1} {filename ""}} {
  if {($funcp && [string compare $f $Current(function)]) \
	|| [string compare $f $Current(filename)]} {
    if {!$funcp} {
      if {$filename == ""} {
	set f $f:1
      } else {
	set f $f:$filename
      }
    }

    if {[catch {gdb_loc $f} linespec]} {
      return
    }

    lassign $linespec foo funcname name line addr pc_addr lib
    set file_changed [string compare $Current(filename) $name]

    if {$file_changed} {
      # Set the file name label:
      $itk_component(view_name) configure -text $name:
      _freeze_me
    }

    # fill srctextwin
    $itk_component(view_src) location BROWSE_TAG $name $funcname \
      $line $addr $pc_addr lib

    if {$file_changed} {
      _thaw_me
    }

    set Current(function) $funcname
    # fill func combo
    if {$file_changed} {
      set Current(filename) $name
      _fill_funcs_combo $name
    }
    # Set current function in combo box
    $itk_component(view_func) entryset $f
  }
}

# ------------------------------------------------------------------
#  METHOD:  mode
#           Function called by srctextwin when the display
#           mode changes
# ------------------------------------------------------------------
itcl::body BrowserWin::mode {w {mode ""} {go 1}} {
  if {$mode != ""} {
    $itk_component(view_src) mode_set $mode $go
    $itk_component(view_mode) entryset $mode
  }
}

# ------------------------------------------------------------------
# METHOD:  _goto_func
#          Callback for the function combo box which
#          sets the srctextwin looking at the given function (VAL)
# ------------------------------------------------------------------
itcl::body BrowserWin::_goto_func {w {val ""}} {
  if {$val != ""} {
    set mang 0
    if {[info exists _mangled_func($val)]} {
      set mang $_mangled_func($val)
    }
    if {$mang} {
      set loc $val
    } else {
      set fn [lindex [::file split $Current(filename)] end]
      set loc $fn:$val
    }
    debug "GOTO \"$loc\""
    if {![catch {gdb_loc $loc} result]} {
      lassign $result foo funcname name line addr pc_addr lib
      $itk_component(view_src) location BROWSE_TAG $name $funcname \
	$line $addr $pc_addr lib
    } else {
      dbug W "gdb_loc returned \"$result\""
    }
  }
}

# ------------------------------------------------------------------
#  METHOD:  _fill_file_box
#           This private method fills the file listbox
# ------------------------------------------------------------------
itcl::body BrowserWin::_fill_file_box {} {
  # It would be cool if gdb_listfiles took a regexp to match,
  # but it doesn't...

  $itk_component(file_box) clear
  set allFiles [gdb_listfiles]

  if {[pref get gdb/browser/hide_h]} {
    foreach file $allFiles {
      if {[string compare [file extension $file] ".h"]} {
	$itk_component(file_box) insert end $file
      }
    }
  } else {
    foreach file $allFiles {
      $itk_component(file_box) insert end $file
    } 
  }
  search
}

# ------------------------------------------------------------------
#  METHOD:  _fill_funcs_combo
#           This private method fills the functions combo box
#           with all the functions in NAME.
# ------------------------------------------------------------------
itcl::body BrowserWin::_fill_funcs_combo {name} {

  $itk_component(view_func) list delete 0 end
  if {$name != ""} {
    set maxlen 10
    if {[catch {gdb_listfuncs $name} listfuncs]} {
      tk_messageBox -icon error -default ok \
	-title "GDB" -type ok \
	-message "This file can not be found or does not contain\ndebugging information."
      return
    }
    foreach f [lsort $listfuncs] {
      lassign $f func mang
      if {$func == "global constructors keyed to main"} {continue}
      set _mangled_func($func) $mang
      $itk_component(view_func) list insert end $func
      if {[string length $func] > $maxlen} {
	set maxlen [string length $func]
      }
    }
    # limit size to 40 chars because if we don't set a reasonable limit
    # then the combobox can be wider than the screen
    if {$maxlen > 40} {set maxlen 40}

    $itk_component(view_func) configure -width [expr {$maxlen + 1}]
  }
}

# ------------------------------------------------------------------
#  METHOD:  _build_filter_frame
#	This private method builds the filter frame
# ------------------------------------------------------------------
itcl::body BrowserWin::_build_filter_frame {parent} {
  
  itk_component add filter {
    iwidgets::labeledframe $parent.filter -labeltext "Function Filter" \
      -relief groove -borderwidth 2 -ipadx 6 -ipady 4
  }

  # Set up the contents of the Filter frame
  if {$_layout == 2} {
    itk_component add filt_label {
      label [$itk_component(filter) childsite].lbl -text "Show if function " \
	-font global/fixed
    }
  }

  itk_component add filt_type {
    combobox::combobox [$itk_component(filter) childsite].type -height 4 \
      -width 15 -editable 0 \
      -command [code $this _set_filter_mode] \
      -font global/fixed
  } { 
    rename -background -textbackground textBackground Background
  }

  # Fill the filter mode combo-box
  foreach elem $filter_modes {
    $itk_component(filt_type) list insert end $elem
  }

  set cur_filter_mode [pref get gdb/search/filter_mode]
  if {[lsearch $filter_modes $cur_filter_mode] < 0} {
    set cur_filter_mode [lindex $filter_modes 0]
  }
  $itk_component(filt_type) entryset $cur_filter_mode

  itk_component add filt_entry {
    entry [$itk_component(filter) childsite].ent -font global/fixed \
      -textvariable [pref varname gdb/search/last_symbol] 
  } {}
  bind_plain_key $itk_component(filt_entry) Return [list $this search]

  # Watch keystrokes into the entry box and filter on them...
  trace variable [pref varname gdb/search/last_symbol] w \
    [code $this _filter_trace_proc]

  if {$_layout == 2} {
    pack $itk_component(filt_label) -side left 
  }
  pack $itk_component(filt_type) -side left -padx 4 -fill y -pady 5
  pack $itk_component(filt_entry) -side right -fill both -expand 1 \
    -padx 6 -pady 5

  pack $itk_component(filter) -fill x -anchor n -pady 3
}

# ------------------------------------------------------------------
#  METHOD:  _build_file_frame
#	This private method builds the files frame
# ------------------------------------------------------------------
itcl::body BrowserWin::_build_file_frame {parent} {

  # Labeled Frame
  itk_component add file_frame {
    iwidgets::labeledframe $parent.file -labeltext "Files" \
      -relief groove -borderwidth 2 -ipadx 6 -ipady 4
  }

  # Listbox with files
  itk_component add file_box {
    iwidgets::scrolledlistbox  [$itk_component(file_frame) childsite].listbox \
      -selectmode extended -exportselection false \
      -hscrollmode dynamic -vscrollmode dynamic -foreground $::Colors(textfg) \
      -textbackground $::Colors(textbg)
  } {}

  bind [$itk_component(file_box) component listbox] <ButtonRelease-1> \
    [code $this _process_file_selection %y]

  itk_component add file_sep {
    frame [$itk_component(file_frame) childsite].sep -relief raised -height 2 \
      -borderwidth 1
  }

  itk_component add file_hide {
    checkbutton [$itk_component(file_frame) childsite].hide \
      -text "Hide .h files" \
      -variable [pref varname gdb/browser/hide_h] \
      -command [code $this _file_hide_h]
  }

  itk_component add file_all {
    button [$itk_component(file_frame) childsite].sel \
      -text "Select All" \
      -command [code $this _select 1]
  }

  # pack all the pieces
  grid $itk_component(file_box) -column 0 -row 0 -columnspan 2 \
    -sticky news
  grid $itk_component(file_sep) -column 0 -row 1 -columnspan 2 \
    -sticky ew -pady 8
  grid $itk_component(file_hide) -column 0 -row 2 -padx 5 -sticky w
  grid $itk_component(file_all) -column 1 -row 2 -padx 5 -sticky e

  grid columnconfigure [$itk_component(file_frame) childsite] 0 -weight 1
  grid rowconfigure [$itk_component(file_frame) childsite] 0 -weight 1

  # finally pack the main frame
  pack $itk_component(file_frame) -side left -fill both -expand yes
}

# ------------------------------------------------------------------
#  METHOD:  _build_function_frame
#	This private method builds the functions frame
# ------------------------------------------------------------------
itcl::body BrowserWin::_build_function_frame {parent} {

  # Labeled Frame
  itk_component add func_frame {
    iwidgets::labeledframe $parent.file -labeltext "Function" \
      -relief groove -borderwidth 2 -ipadx 6 -ipady 4
  }

  # Functions Listbox
  itk_component add func_box {
    iwidgets::scrolledlistbox [$itk_component(func_frame) childsite].listbox \
      -selectmode extended -hscrollmode dynamic -vscrollmode dynamic \
      -exportselection false -foreground $::Colors(textfg) \
      -textbackground $::Colors(textbg)
  } {}
	    
  bind [$itk_component(func_box) component listbox] <ButtonRelease-1> \
    [code $this _process_func_selection %y]
  bind $itk_component(func_box) <3> [code $this _toggle_bp %y]

  itk_component add func_sep {
    frame [$itk_component(func_frame) childsite].sep -relief raised \
      -height 2 -borderwidth 1
  }
	
  itk_component add func_add_bp {   
    button [$itk_component(func_frame) childsite].abp -text "Set BP" \
      -command [code $this do_all_bp 1]
  }

  itk_component add func_remove_bp {
    button [$itk_component(func_frame) childsite].rbp -text "Delete BP" \
      -command [code $this do_all_bp 0]
  }

  # pack all the pieces
  grid $itk_component(func_box) -column 0 -row 0 -columnspan 2 -sticky news
  grid $itk_component(func_sep) -row 1 -column 0 -columnspan 2 \
    -sticky ew -pady 8
  grid $itk_component(func_remove_bp) -row 2 -column 0 -padx 5 -sticky w
  grid $itk_component(func_add_bp) -row 2 -column 1  -padx 5 -sticky e

  grid columnconfigure [$itk_component(func_frame) childsite] 0 -weight 1
  grid rowconfigure [$itk_component(func_frame) childsite] 0 -weight 1

  # finally pack the main frame
  pack $itk_component(func_frame) -fill both -expand yes
}

# ------------------------------------------------------------------
#  METHOD:  _build_view_frame
#	This private method builds the view frame
# ------------------------------------------------------------------
itcl::body BrowserWin::_build_view_frame {parent} {
  itk_component add view {
    frame $parent.view
  }

  itk_component add view_src {
    SrcTextWin $itk_component(view).src -Tracing 0 \
      -parent $this -ignore_var_balloons 0
  } {
    rename -background -textbackground textBackground Background
  }

  itk_component add view_bottom {
    frame $itk_component(view).bottom
  }

  itk_component add view_name {
    label $itk_component(view).name -font global/fixed
  }

  itk_component add view_func {
    combobox::combobox $itk_component(view_bottom).combo -maxheight 15\
      -font global/fixed -command [code $this _goto_func]
  } {
    rename -background -textbackground textBackground Background
  }
  
  itk_component add view_mode {
    combobox::combobox $itk_component(view_bottom).mode -width 9 \
      -font global/fixed -command [code $this mode]
  } {
    rename -background -textbackground textBackground Background
  }

  itk_component add view_search {
    entry $itk_component(view_bottom).search -borderwidth 2 \
      -font global/fixed -width 10 -background $::Colors(textbg)
  } {}

  # Pack all the components of view into the frame:
  pack $itk_component(view_func) -side left -anchor w
  pack $itk_component(view_mode) -side left -padx 5
  pack $itk_component(view_search) -side right -padx 5 -expand 1 -anchor e
  pack $itk_component(view_name) -side top -fill x -padx 5 -pady 3
  pack $itk_component(view_bottom) -side bottom -fill x -pady 3 -padx 5
  pack $itk_component(view_src) -fill both -expand 1 -padx 5 -pady 3
  
  # Fill combo boxes
  $itk_component(view_mode) list insert end SOURCE
  $itk_component(view_mode) list insert end ASSEMBLY
  $itk_component(view_mode) list insert end MIXED
  # don't allow SRC+ASM mode... $itk_component(view_mode) insert end SRC+ASM
  $itk_component(view_mode) entryset [$itk_component(view_src) mode_get]

  # Key bindings
  bind_plain_key $itk_component(view_search) Return \
    [code $this _search_src forwards]
  bind_plain_key $itk_component(view_search) Shift-Return \
    [code $this _search_src backwards]

  pack  $itk_component(view) -fill both -expand yes
}

# ------------------------------------------------------------------
#  METHOD:  _switch_layout
#           Switch between different layouts
#
# ------------------------------------------------------------------
itcl::body BrowserWin::_switch_layout {} {
  # only 2 right now, so toggle
  if {$_layout == 1} {
    set _layout 2
  } else {
    set _layout 1
  }
  pref set gdb/browser/layout $_layout
  destroy $itk_interior.p
  destroy $itk_component(popup)
  set Current(filename) {}
  set Current(function) {}
  if {$filter_trace_after != ""} {
    after cancel $filter_trace_after
  }
  trace vdelete [pref varname gdb/search/last_symbol] \
    w [code $this _filter_trace_proc]
  _build_win
}
