Console menu File => Working dir: add "Go to Favorites" = dynamical list 02/19002/6
Samuel GOUGEON [Mon, 19 Dec 2016 22:41:45 +0000 (23:41 +0100)]
 * Related report: http://bugzilla.scilab.org/14171

 * The 3 items of the WD section are:
   Current working directory: (clicking prints it)
       Browse for new
       Go to Bookmark => TMPDIR
                         SCIHOME
                         SCI
                         User's home ~
                         <column of Scinotes favorite dirs>

 * getPreferencesValue() must be priorly upgraded:
   Please review and merge https://codereview.scilab.org/18946: DONE

 * All directories of current and recent files opened in Scinotes,
   and Scinotes favorite dirs are sorted, deduplicated, shortened with
   TMPDIR SCI SCIHOME home prefixes, and listed.
   This dynamical list is more efficient than items set by hand.
   It sticks to Scinotes (most recent) activity. Scinotes already
   allows to set favorites dirs by hand, that will appear here as well.
   Synchronisation will Scinotes needs is ensured.

Change-Id: Iaf3a9c74dfbc88d191b9c7194a6776f68f7bfc66

scilab/etc/scilab.start
scilab/modules/gui/etc/main_menubar.xml
scilab/modules/gui/macros/main_menubar_cb.sci [new file with mode: 0644]

index 78199ef..7d9ce0b 100644 (file)
@@ -155,6 +155,7 @@ if getscilabmode() == "STD" then
   setmenu(gettext("&?"));
   setmenu(gettext("&Toolboxes"));
   toolbar(-1, "on");
+  main_menubar_cb("setFavoriteDirs");
 end
 // ====================================================================
 clear ans
index fffcce6..3efce3c 100644 (file)
@@ -2,29 +2,44 @@
 <menubar>
     <!-- All "main" menus are disabled when created and then enabled in scilab.start file after all macros are loaded -->
     <!-- The accelerator can be "control shift A" or "control A" or "meta A" or "OSSCKEY A" where oskey means "OS ShortCut key"
-       and is replaced by the good key (control for Unix/Windows OS and meta for Mac) -->
+    and is replaced by the good key (control for Unix/Windows OS and meta for Mac) -->
     <!-- File menu -->
     <menu label="&amp;File" enabled="true">
         <submenu label="&amp;Execute..." accelerator="OSSCKEY E">
-            <callback instruction="               %fileToExec = uigetfile(&quot;*.sc*&quot;, &quot;&quot;, gettext(&quot;Select a file to execute&quot;));               if ~(%fileToExec == &quot;&quot;) then                 exec(%fileToExec);                 disp(msprintf(gettext(&quot;Execution done.\n&quot;)));               end;               clear(&quot;%fileToExec&quot;);" type="0"/>
+            <callback instruction="%fileToExec = uigetfile(&quot;*.sc*&quot;, &quot;&quot;, gettext(&quot;Select a file to execute&quot;)); if ~(%fileToExec == &quot;&quot;) then exec(%fileToExec); disp(msprintf(gettext(&quot;Execution done.\n&quot;))); end; clear(&quot;%fileToExec&quot;);" type="0"/>
         </submenu>
         <separator/>
         <submenu label="&amp;Open a file..." accelerator="OSSCKEY O" icon="document-open-sci">
-            <callback instruction="               %fileToOpen = uigetfile(&quot;*.sc*&quot;, &quot;&quot;, gettext(&quot;Select a file to open with SciNotes&quot;));               if ~(%fileToOpen == &quot;&quot;) then                 editor(%fileToOpen);               end;               clear(&quot;%fileToOpen&quot;);" type="0"/>
+            <callback instruction="%fileToOpen = uigetfile(&quot;*.sc*&quot;, &quot;&quot;, gettext(&quot;Select a file to open with SciNotes&quot;)); if ~(%fileToOpen == &quot;&quot;) then editor(%fileToOpen); end; clear(&quot;%fileToOpen&quot;);" type="0"/>
         </submenu>
         <submenu label="&amp;Load environment..." accelerator="OSSCKEY L" icon="document-open">
-            <callback instruction="               %fileToLoad = uigetfile([&quot;*.sav&quot;;&quot;*.sod&quot;], &quot;&quot;, gettext(&quot;Select a file to load&quot;));               if ~(%fileToLoad == &quot;&quot;) then                 load(%fileToLoad);                 updatebrowsevar();                 disp(msprintf(gettext(&quot;Environment loaded.\n&quot;)));               end;               clear(&quot;%fileToLoad&quot;);" type="0"/>
+            <callback instruction="%fileToLoad = uigetfile([&quot;*.sav&quot;;&quot;*.sod&quot;], &quot;&quot;, gettext(&quot;Select a file to load&quot;)); if ~(%fileToLoad == &quot;&quot;) then load(%fileToLoad); updatebrowsevar(); disp(msprintf(gettext(&quot;Environment loaded.\n&quot;))); end; clear(&quot;%fileToLoad&quot;);" type="0"/>
         </submenu>
         <submenu label="&amp;Save environment..." accelerator="OSSCKEY S" icon="document-save">
-            <callback instruction="               %fileToSave = uiputfile([&quot;*.sav&quot;;&quot;*.sod&quot;], &quot;&quot;, gettext(&quot;Select a file to write&quot;));               if ~(%fileToSave == &quot;&quot;) then                 if (fileparts(%fileToSave, &quot;extension&quot;) == &quot;&quot;) then                   %fileToSave = %fileToSave + &quot;.sav&quot;;                 end;                 save(%fileToSave);                 disp(msprintf(gettext(&quot;Environment saved.\n&quot;)));               end;               clear(&quot;%fileToSave&quot;);" type="0"/>
+            <callback instruction="%fileToSave = uiputfile([&quot;*.sav&quot;;&quot;*.sod&quot;], &quot;&quot;, gettext(&quot;Select a file to write&quot;)); if ~(%fileToSave == &quot;&quot;) then if (fileparts(%fileToSave, &quot;extension&quot;) == &quot;&quot;) then %fileToSave = %fileToSave + &quot;.sav&quot;; end; save(%fileToSave); disp(msprintf(gettext(&quot;Environment saved.\n&quot;))); end; clear(&quot;%fileToSave&quot;);" type="0"/>
         </submenu>
         <separator/>
-        <submenu label="&amp;Change current directory..." icon="scilab-folder-open">
-            <callback instruction="               %newDir = uigetdir(pwd(), gettext(&quot;Select a directory&quot;));               if ~(%newDir == &quot;&quot;) then                 cd(%newDir);               end;               clear(&quot;%newDir&quot;);" type="0"/>
-        </submenu>
-        <submenu label="&amp;Display current directory">
+        <submenu label="&amp;Current Working Directory (CWD):" icon="scilab-folder-open">
             <callback instruction="if iswaitingforinput() then printf(&quot;\n%s\n\n%s\n&quot;,prompt(),pwd()); else printf(&quot;\n\n%s\n\n&quot;,pwd());end" type="0"/>
         </submenu>
+        <submenu label="    &amp;Browse for new">
+            <callback instruction="%newDir = uigetdir(pwd(), gettext(&quot;Select a directory&quot;)); if ~(%newDir == &quot;&quot;) then cd(%newDir); end; clear(&quot;%newDir&quot;);" type="0"/>
+        </submenu>
+        <submenu label="    Go to &amp;Favorite" accelerator="OSSCKEY B" >
+            <submenu label="TMPDIR">
+                <callback instruction="cd(TMPDIR)," type="0"/>
+            </submenu>
+            <submenu label="SCIHOME">
+                <callback instruction="cd(SCIHOME)," type="0"/>
+            </submenu>
+            <submenu label="SCI">
+                <callback instruction="cd(SCI)," type="0"/>
+            </submenu>
+            <submenu label="User's home ~">
+                <callback instruction="cd(home)," type="0"/>
+            </submenu>
+            <separator/>
+        </submenu>
         <separator/>
         <submenu label="P&amp;age setup...">
             <callback instruction="org.scilab.modules.gui.bridge.CallScilabBridge.pageSetup" type="3"/>
@@ -58,7 +73,7 @@
         </submenu>
         <separator/>
         <submenu label="Show/Hide &amp;Toolbar">
-            <callback instruction="                 if toolbar(-1)==&quot;on&quot; then                   toolbar(-1, &quot;off&quot;);                 else                   toolbar(-1, &quot;on&quot;);                 end" type="0"/>
+            <callback instruction="if toolbar(-1)==&quot;on&quot; then toolbar(-1, &quot;off&quot;); else toolbar(-1, &quot;on&quot;); end" type="0"/>
         </submenu>
         <separator/>
         <submenu label="Clear &amp;History">
             </submenu>
         </submenu>
         <separator/>
+        <submenu label="&amp;News feed">
+            <callback instruction="org.scilab.modules.ui_data.newsfeed.NewsFeedTab.displayTab" type="3"/>
+        </submenu>
+        <separator/>
         <submenu label="&amp;Scilab Enterprises">
             <callback instruction="org.scilab.modules.gui.bridge.CallScilabBridge.openSE" type="3"/>
         </submenu>
diff --git a/scilab/modules/gui/macros/main_menubar_cb.sci b/scilab/modules/gui/macros/main_menubar_cb.sci
new file mode 100644 (file)
index 0000000..59dc6b8
--- /dev/null
@@ -0,0 +1,99 @@
+// Copyright (C) 2017 - Samuel GOUGEON
+//
+// Copyright (C) 2012 - 2016 - Scilab Enterprises
+//
+// This file is hereby licensed under the terms of the GNU GPL v2.0,
+// pursuant to article 5.3.4 of the CeCILL v.2.1.
+// This file was originally licensed under the terms of the CeCILL v2.1,
+// and continues to be available under such terms.
+// For more information, see the COPYING file which you should have received
+// along with this program.
+
+function main_menubar_cb(key)
+    // Private internal function
+    // * initially called by scilab.start
+    // * afterwards called by the menu(File).callback
+
+    if  key=="setFavoriteDirs"
+        // Setting the list of directories of interest
+        // = major directories + Scinotes favorites
+        //
+        prefFile = SCIHOME + "/scinotesConfiguration.xml";
+        try
+            tagPath = "//Profile/favoriteDirectories/Directory";
+            r = getPreferencesValue(tagPath, "path", prefFile);
+        catch
+            r = []
+        end
+        try
+            tagPath = "//Profile/recentFiles/document";
+            r2 = getPreferencesValue(tagPath, "path", prefFile);
+        catch
+            r2 = []
+        end
+        try
+            tagPath = "//Profile/openFiles/document";
+            r2 = [r2 ; getPreferencesValue(tagPath, "path", prefFile)];
+        end
+
+        if isempty([r;r2]) then
+            return;
+        end
+
+        if ~isempty(r2) then
+            r2 = fileparts(r2, "path");
+        end
+
+        r = getlongpathname([r ; r2])
+
+        // Shortening paths with aliases when possible
+        if getos() == "Windows" then
+            r = strsubst(r, WSCI, "SCI");
+        else
+            r = strsubst(r, SCI, "SCI");
+        end
+        r = strsubst(r, SCIHOME, "SCIHOME");
+        r = strsubst(r, TMPDIR, "TMPDIR");
+        r = strsubst(r, home, "home");
+        r = unique(r)
+
+        // Removing main dirs duplicated in the Scinotes favorites
+        tmp = ["SCI" "SCIHOME" "TMPDIR" "home"];
+        tmp = strcat([tmp tmp+"\/" tmp+"\\"],"|");
+        tmp = grep(r,"_^("+tmp+")$_", "r");
+        r(tmp) = [];
+
+        // Finding the handle of the submenu "Files => Go to Bookmark"
+        // It has a sub-submenu with Label = "TMPDIR", coming from the
+        // SCI/modules/gui/etc/main_menubar.xml configuration file.
+        c = get(0);
+        old = c.showhiddenhandles;   // to be restored before leaving
+        c.showhiddenhandles = "on";
+        f = c.children($);    // The "File" menu is the first one created
+        f.callback = "main_menubar_cb(""setFavoriteDirs"")";
+        menu_found = %f;
+        for i = 1:length(f.children)
+            for j = 1:length(f.children(i).children)
+                if f.children(i).children(j).Label=="TMPDIR"
+                    menu_found = %t;
+                    break;
+                end
+            end
+            if menu_found
+                break;
+            end
+        end
+        if menu_found
+            h = f.children(i);
+            // We update the list of bookmarks
+            //     Deleting the list of other bookmarks
+            //     keeping only "TMPDIR","SCIHOME", "SCI" and "home":
+            delete(h.children(1:$-4));
+            // Creating the new list
+            for d = r'
+                uimenu(h, "Label", d, "Callback", "cd("""+d+"""),");
+            end
+        end
+        c.showhiddenhandles = old;
+    end
+endfunction