* Bug 15433 fixed: getPreferencesValue() => xmlGetValues() in [xml]
[scilab.git] / scilab / modules / gui / macros / main_menubar_cb.sci
1 // Copyright (C) 2017 - Samuel GOUGEON
2 //
3 // Copyright (C) 2012 - 2016 - Scilab Enterprises
4 //
5 // This file is hereby licensed under the terms of the GNU GPL v2.0,
6 // pursuant to article 5.3.4 of the CeCILL v.2.1.
7 // This file was originally licensed under the terms of the CeCILL v2.1,
8 // and continues to be available under such terms.
9 // For more information, see the COPYING file which you should have received
10 // along with this program.
11
12 function main_menubar_cb(key)
13     // Private internal function
14     // * initially called by scilab.start
15     // * afterwards called by the menu(File).callback set according to
16     //   SCI/modules/gui/etc/main_menubar.xml
17
18     if key=="openFile"
19         msg = gettext("Select a file to open");
20         ext = [ "*.sce|*.sci"       _("Scilab scripts")
21                 "*.xcos|*.zcos"     _("Xcos diagrams")
22                 "*.tst|*.dia.ref"   _("Scilab Tests")
23                 "*.scg"             _("Scilab Graphics")
24                 "lib"               _("Scilab Library")
25                 "*.sc*|*.*cos|*.tst|*.dia.ref|lib" _("All Scilab files")
26                 ];
27         %fileToOpen = uigetfile(ext, pwd(), msg);
28         if %fileToOpen ~= "" then
29             ext = convstr(fileparts(%fileToOpen, "extension"));
30             if or(ext==[".xcos" ".zcos"])
31                 xcos(%fileToOpen);
32             elseif or(ext==[".sce" ".sci" ".tst" ".ref"])
33                 editor(%fileToOpen);
34             elseif ext==".scg"
35                 load(%fileToOpen);
36             elseif basename(%fileToOpen)=="lib"
37                 libname = xmlGetValues("/scilablib","name",%fileToOpen);
38                 load(%fileToOpen);
39                 mprintf(libname+" loaded.\n");
40                 execstr(libname+"=return("+libname+")");
41             else
42                 if getos()=="Windows"
43                     winopen(%fileToOpen)
44                 elseif getos()=="Linux"
45                     host("xdg-open "+%fileToOpen+" &")
46                 else
47                     host("open "+%fileToOpen+" &")
48                 end
49             end
50         end
51
52     elseif  key=="setFavoriteDirs"
53         // Setting the list of directories of interest
54         // = major directories + Scinotes favorites
55         //
56         prefFile = SCIHOME + "/scinotesConfiguration.xml";
57         try
58             tagPath = "//Profile/favoriteDirectories/Directory";
59             r = xmlGetValues(tagPath, "path", prefFile);
60         catch
61             r = []
62         end
63         try
64             tagPath = "//Profile/recentFiles/document";
65             r2 = xmlGetValues(tagPath, "path", prefFile);
66         catch
67             r2 = []
68         end
69         try
70             tagPath = "//Profile/openFiles/document";
71             r2 = [r2 ; xmlGetValues(tagPath, "path", prefFile)];
72         end
73
74         if isempty([r;r2]) then
75             return;
76         end
77
78         if ~isempty(r2) then
79             r2 = fileparts(r2, "path");
80         end
81
82         r = getlongpathname([r ; r2])
83
84         // Shortening paths with aliases when possible
85         if getos() == "Windows" then
86             r = strsubst(r, WSCI, "SCI");
87         else
88             r = strsubst(r, SCI, "SCI");
89         end
90         r = strsubst(r, SCIHOME, "SCIHOME");
91         r = strsubst(r, TMPDIR, "TMPDIR");
92         r = strsubst(r, home, "home");
93         r = unique(r)
94
95         // Removing main dirs duplicated in the Scinotes favorites
96         tmp = ["SCI" "SCIHOME" "TMPDIR" "home"];
97         tmp = strcat([tmp tmp+"\/" tmp+"\\"],"|");
98         tmp = grep(r,"_^("+tmp+")$_", "r");
99         r(tmp) = [];
100
101         // Finding the handle of the submenu "Files => Go to Bookmark"
102         // It has a sub-submenu with Label = "TMPDIR", coming from the
103         // SCI/modules/gui/etc/main_menubar.xml configuration file.
104         c = get(0);
105         old = c.showhiddenhandles;   // to be restored before leaving
106         c.showhiddenhandles = "on";
107         f = c.children($);    // The "File" menu is the first one created
108         f.callback = "main_menubar_cb(""setFavoriteDirs"")";
109         menu_found = %f;
110         for i = 1:length(f.children)
111             for j = 1:length(f.children(i).children)
112                 if f.children(i).children(j).Label=="TMPDIR"
113                     menu_found = %t;
114                     break;
115                 end
116             end
117             if menu_found
118                 break;
119             end
120         end
121         if menu_found
122             h = f.children(i);
123             // We update the list of bookmarks
124             //     Deleting the list of other bookmarks
125             //     keeping only "TMPDIR","SCIHOME", "SCI" and "home":
126             delete(h.children(1:$-4));
127             // Creating the new list
128             for d = r'
129                 uimenu(h, "Label", d, "Callback", "cd("""+d+"""),");
130             end
131         end
132         c.showhiddenhandles = old;
133     end
134 endfunction