# HG changeset patch
# User John Donoghue <john.donoghue@ieee.org>
# Date 1374372831 14400
#      Sat Jul 20 22:13:51 2013 -0400
# Node ID aa89a8b693aa8c1a86a4380135e11ab40c4bab2b
# Parent  abe91fe896a82798664422749272e3c23868a881
Display doc info pages using documentation browser when in GUI mode (Bug #39451)

* scripts/help/doc.m: call __octave_link_show_doc if in gui mode.

* libinterp/corefcn/octave-link.h, libinterp/corefcn/octave-link.cc
  (octave_link::show_doc): New function.
  (octave_link::do_show_doc): New virtual function.
  (__octave_link_show_doc__): New function.

* libgui/src/octave-qt-link.h
  (octave_qt_link::show_doc_signal): New signal.
  (octave_qt_link::do_show_doc): New function.

* libgui/src/documentation-dock-widget.cc, libgui/src/documentation-dock-widget.h
  (documentation_dock_widget::showDoc): New function.

* libgui/src/main-window.cc, libgui/src/main-window.h
  (main_window::construct_octave_qt_link): connect handle_show_doc.
  (main_window::handle_show_doc): New function.
  (main_window::show_doc_signal): New signal.

* libgui/src/qtinfo/parser.cc, libgui/src/qtinfo/parser.h
  (parser::find_ref): New function.

* libgui/src/qtinfo/webinfo.cc, libgui/src/qtinfo/webinfo.h
  (webinfo::load_ref): New function.

diff -r abe91fe896a8 -r aa89a8b693aa libgui/src/documentation-dock-widget.cc
--- a/libgui/src/documentation-dock-widget.cc	Fri Jul 19 10:35:14 2013 -0700
+++ b/libgui/src/documentation-dock-widget.cc	Sat Jul 20 22:13:51 2013 -0400
@@ -36,6 +36,9 @@
 
   _webinfo = new webinfo (this);
   setWidget (_webinfo);
+
+  connect (p, SIGNAL(show_doc_signal(const QString &)),
+   this, SLOT(showDoc(const QString &)));
 }
 
 void
@@ -48,3 +51,8 @@
 {
   _webinfo->pasteClipboard ();
 }
+void
+documentation_dock_widget::showDoc (const QString &name)
+{
+  _webinfo->load_ref (name);
+}
diff -r abe91fe896a8 -r aa89a8b693aa libgui/src/documentation-dock-widget.h
--- a/libgui/src/documentation-dock-widget.h	Fri Jul 19 10:35:14 2013 -0700
+++ b/libgui/src/documentation-dock-widget.h	Sat Jul 20 22:13:51 2013 -0400
@@ -39,6 +39,7 @@
   void copyClipboard ();
   void pasteClipboard ();
 
+  void showDoc (const QString & name);
 private:
 
   webinfo *_webinfo;
diff -r abe91fe896a8 -r aa89a8b693aa libgui/src/main-window.cc
--- a/libgui/src/main-window.cc	Fri Jul 19 10:35:14 2013 -0700
+++ b/libgui/src/main-window.cc	Sat Jul 20 22:13:51 2013 -0400
@@ -938,6 +938,10 @@
            this,
            SLOT (handle_update_breakpoint_marker_request (bool, const QString&, int)));
 
+  connect (_octave_qt_link,
+           SIGNAL (show_doc_signal (const QString &)),
+           this, SLOT (handle_show_doc (const QString &)));
+
   connect (_workspace_model,
            SIGNAL (rename_variable (const QString&, const QString&)),
            this,
@@ -1612,3 +1616,12 @@
 
   emit set_widget_shortcuts_signal (set_shortcuts);
 }
+
+void
+main_window::handle_show_doc (const QString& file)
+{
+  doc_browser_window->setVisible (true);
+  emit show_doc_signal (file);
+}
+
+
diff -r abe91fe896a8 -r aa89a8b693aa libgui/src/main-window.h
--- a/libgui/src/main-window.h	Fri Jul 19 10:35:14 2013 -0700
+++ b/libgui/src/main-window.h	Sat Jul 20 22:13:51 2013 -0400
@@ -82,6 +82,8 @@
   void new_file_signal (const QString&);
   void open_file_signal (const QString&);
 
+  void show_doc_signal (const QString&);
+
   void insert_debugger_pointer_signal (const QString& file, int line);
   void delete_debugger_pointer_signal (const QString& file, int line);
   void update_breakpoint_marker_signal (bool insert, const QString& file,
@@ -171,6 +173,8 @@
                                  const QString &dirname,
                                  const QString& multimode);
 
+  void handle_show_doc (const QString &file);
+
   // find files dialog 
   void find_files(const QString &startdir=QDir::currentPath());
   void find_files_finished(int);
diff -r abe91fe896a8 -r aa89a8b693aa libgui/src/octave-qt-link.cc
--- a/libgui/src/octave-qt-link.cc	Fri Jul 19 10:35:14 2013 -0700
+++ b/libgui/src/octave-qt-link.cc	Sat Jul 20 22:13:51 2013 -0400
@@ -498,4 +498,10 @@
   emit show_preferences_signal ();
 }
 
+void
+octave_qt_link::do_show_doc (const std::string& file)
+{
+  emit show_doc_signal (QString::fromStdString (file));
+}
 
+
diff -r abe91fe896a8 -r aa89a8b693aa libgui/src/octave-qt-link.h
--- a/libgui/src/octave-qt-link.h	Fri Jul 19 10:35:14 2013 -0700
+++ b/libgui/src/octave-qt-link.h	Sat Jul 20 22:13:51 2013 -0400
@@ -122,6 +122,8 @@
   static bool file_in_path (const std::string& file, const std::string& dir);
 
   void do_show_preferences (void);
+
+  void do_show_doc (const std::string& file);
 private:
 
   // No copying!
@@ -169,6 +171,8 @@
   void delete_debugger_pointer_signal (const QString&, int);
 
   void show_preferences_signal (void);
+
+  void show_doc_signal (const QString &file);
 };
 
 #endif
diff -r abe91fe896a8 -r aa89a8b693aa libgui/src/qtinfo/parser.cc
--- a/libgui/src/qtinfo/parser.cc	Fri Jul 19 10:35:14 2013 -0700
+++ b/libgui/src/qtinfo/parser.cc	Sat Jul 20 22:13:51 2013 -0400
@@ -592,3 +592,24 @@
   results.append ("</body></html>");
   return results;
 }
+
+QString 
+parser::find_ref (const QString &ref_name)
+{
+  QString text = "";
+
+  QHash<QString,node_position>::iterator it;
+  for (it=_ref_map.begin ();it!=_ref_map.end ();++it)
+  {
+    QString k = it.key ();
+    node_position p = it.value ();
+
+    if (k == "docX" + ref_name)
+    {
+      // found ref, so return its name
+      text = "docX" + ref_name;
+    }
+  }
+  return text;
+}
+
diff -r abe91fe896a8 -r aa89a8b693aa libgui/src/qtinfo/parser.h
--- a/libgui/src/qtinfo/parser.h	Fri Jul 19 10:35:14 2013 -0700
+++ b/libgui/src/qtinfo/parser.h	Sat Jul 20 22:13:51 2013 -0400
@@ -57,6 +57,8 @@
   QString search_node (const QString& node);
   QString global_search (const QString& text, int maxFounds);
 
+  QString find_ref (const QString &name);
+
   /** Checks if this node is reference. If node is reference, it will be returned its position
     * in text, else  it will be returned -1.
     */
diff -r abe91fe896a8 -r aa89a8b693aa libgui/src/qtinfo/webinfo.cc
--- a/libgui/src/qtinfo/webinfo.cc	Fri Jul 19 10:35:14 2013 -0700
+++ b/libgui/src/qtinfo/webinfo.cc	Sat Jul 20 22:13:51 2013 -0400
@@ -93,6 +93,7 @@
   resize (500, 300);
 
   set_info_path (QString::fromStdString (Vinfo_file));
+
 }
 
 void
@@ -190,6 +191,21 @@
 }
 
 void
+webinfo::load_ref (const QString &ref_name)
+{
+  QString text = _parser.find_ref (ref_name);
+  if(text.length () > 0)
+  {
+    load_node (text);
+  }
+  else
+  {
+    // not found
+   load_node("Top");
+  }
+}
+
+void
 webinfo::search ()
 {
   if (_search_check_box->isChecked ())
diff -r abe91fe896a8 -r aa89a8b693aa libgui/src/qtinfo/webinfo.h
--- a/libgui/src/qtinfo/webinfo.h	Fri Jul 19 10:35:14 2013 -0700
+++ b/libgui/src/qtinfo/webinfo.h	Sat Jul 20 22:13:51 2013 -0400
@@ -40,6 +40,8 @@
   webinfo (QWidget *parent = 0);
   void set_info_path (const QString& info_path);
   void load_node (const QString& node_name);
+ 
+  void load_ref (const QString &ref_name); 
 
 public slots:
   void link_clicked (const QUrl& link);
diff -r abe91fe896a8 -r aa89a8b693aa libinterp/corefcn/octave-link.cc
--- a/libinterp/corefcn/octave-link.cc	Fri Jul 19 10:35:14 2013 -0700
+++ b/libinterp/corefcn/octave-link.cc	Sat Jul 20 22:13:51 2013 -0400
@@ -416,4 +416,22 @@
   return retval;
 }
 
+DEFUN (__octave_link_show_doc__, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn {Built-in Function} {} __octave_link_show_doc__ ( @var{filename} )\n\
+Undocumented internal function.\n\
+@end deftypefn")
+{
+  octave_value retval;
+  std::string file;
 
+  if (args.length () >= 1)
+    file = args(0).string_value();
+
+  retval = octave_link::show_doc (file);
+
+  return retval;
+}
+
+
+
diff -r abe91fe896a8 -r aa89a8b693aa libinterp/corefcn/octave-link.h
--- a/libinterp/corefcn/octave-link.h	Fri Jul 19 10:35:14 2013 -0700
+++ b/libinterp/corefcn/octave-link.h	Sat Jul 20 22:13:51 2013 -0400
@@ -319,6 +319,19 @@
       return false;
   }
 
+  static bool 
+  show_doc (const std::string & file)
+  {
+    if (enabled ())
+      { 
+        instance->do_show_doc (file);
+        return true;
+      }
+    else
+      return false;
+ 
+  }
+
 private:
 
   static octave_link *instance;
@@ -439,6 +452,8 @@
                                        std::string& ps4) = 0;
 
   virtual void do_show_preferences (void) = 0;
+
+  virtual void do_show_doc (const std::string &file) = 0;
 };
 
 #endif // OCTAVELINK_H
diff -r abe91fe896a8 -r aa89a8b693aa scripts/help/doc.m
--- a/scripts/help/doc.m	Fri Jul 19 10:35:14 2013 -0700
+++ b/scripts/help/doc.m	Sat Jul 20 22:13:51 2013 -0400
@@ -52,54 +52,61 @@
     else
       fname = "";
     endif
+
+    # if GUI is running, let it display the function
+    if isguirunning ()
+      __octave_link_show_doc__ (fname);
+    else
+  
+      if (ftype == 2 || ftype == 3)
+        ffile = which (fname);
+      else
+        ffile = "";
+      endif
 
-    if (ftype == 2 || ftype == 3)
-      ffile = which (fname);
-    else
-      ffile = "";
-    endif
+      if (isempty (ffile))
+        info_dir = octave_config_info ("infodir");
+      else
+        info_dir = fileparts (ffile);
+      endif
 
-    if (isempty (ffile))
-      info_dir = octave_config_info ("infodir");
-    else
-      info_dir = fileparts (ffile);
-    endif
+      ## Determine if a file called doc.info exist in the same
+      ## directory as the function.
 
-    ## Determine if a file called doc.info exist in the same
-    ## directory as the function.
+      info_file_name = fullfile (info_dir, "doc.info");
 
-    info_file_name = fullfile (info_dir, "doc.info");
+      [stat_info, err] = stat (info_file_name);
 
-    [stat_info, err] = stat (info_file_name);
+      if (err < 0)
+        info_file_name = info_file ();
+      endif
 
-    if (err < 0)
-      info_file_name = info_file ();
-    endif
+      ## FIXME -- don't change the order of the arguments below because
+      ## the info-emacs-info script currently expects --directory DIR as
+      ## the third and fourth arguments.  Someone should fix that.
 
-    ## FIXME -- don't change the order of the arguments below because
-    ## the info-emacs-info script currently expects --directory DIR as
-    ## the third and fourth arguments.  Someone should fix that.
-
-    cmd = sprintf ("\"%s\" --file \"%s\" --directory \"%s\"",
+      cmd = sprintf ("\"%s\" --file \"%s\" --directory \"%s\"",
                    info_program (), info_file_name, info_dir);
 
-    have_fname = ! isempty (fname);
+      have_fname = ! isempty (fname);
 
-    if (have_fname)
-      status = system (sprintf ("%s --index-search \"%s\"", cmd, fname));
+      if (have_fname)
+        status = system (sprintf ("%s --index-search \"%s\"", cmd, fname));
+      endif
+   
+
+      if (! (have_fname && status == 0))
+        status = system (cmd);
+        if (status == 127)
+          warning ("unable to find info program '%s'", info_program ());
+        endif
+      endif
+
+      if (nargout > 0)
+        retval = status;
+      endif
+
     endif
-
-    if (! (have_fname && status == 0))
-      status = system (cmd);
-      if (status == 127)
-        warning ("unable to find info program '%s'", info_program ());
-      endif
-    endif
-
-    if (nargout > 0)
-      retval = status;
-    endif
-
   else
     print_usage ();
   endif
