summaryrefslogtreecommitdiff
path: root/modules/gui/macros/addmenu.sci
blob: a8c3ae37ce37483fa6dbcf2e4971ab38b644115f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2005 - INRIA - Allan CORNET
// Copyright (C) 2007 - INRIA - Vincent COUVERT Java version
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt

function addmenu(varargin)
    // Add menus in Scilab Windows
    // See comments in the code for different implementations

    if getscilabmode() == "NWNI" then
        error(msprintf(_("%s: can not add a menu in this mode: %s.\n"), "addmenu", getscilabmode()));
    end

    rhs=argn(2);

    select rhs
    case 1
        if type(varargin(1))==10
            // addmenu(menu_label)
            // Create a menu in Scilab main Window
            // The callback is the variable named menu_label

            // Error message in not in standard mode (we need a figure index)
            if getscilabmode() <> "STD" then
                error(msprintf(_("%s: can not add a menu in console in this mode: %s.\n"), "addmenu", getscilabmode()));
            end

            if size(varargin(1),"*") <> 1
                error(msprintf(_("%s: Wrong type for input argument #%d: A string expected.\n"),"addmenu", 1));
            end

            if getos()=="Darwin" then
                // MACOSX version add a submenu with same name because parent menu can not have a callback
                addMenuSubMenus(0, varargin(1), varargin(1));
            else
                addSingleMenu(0, varargin(1));
            end

        else
            error(msprintf(_("%s: Wrong type for input argument #%d: A string expected.\n"),"addmenu", 1));
        end
    case 2
        if type(varargin(1))==10 & type(varargin(2))==10
            // addmenu(menu_label, submenus_labels)
            // Create a menu in Scilab main Window with submenus
            // The callbacks are the items of variable named menu_label

            // Error message in not in standard mode
            if getscilabmode() <> "STD" then
                error(msprintf(_("%s: can not add a menu in console in this mode: %s.\n"), "addmenu", getscilabmode()));
            end

            if size(varargin(1),"*") <> 1
                error(msprintf(_("%s: Wrong type for input argument #%d: A string expected.\n"),"addmenu", 1));
            end

            // No verification for submenus labels

            addMenuSubMenus(0, varargin(1), varargin(2));

        elseif type(varargin(1))==10 & type(varargin(2))==15
            // addmenu(menu_label, action_in_a_list)
            // Create a menu in Scilab main Window with submenus
            // The callbacks are the items of variable named menu_label

            // Error message in not in standard mode
            if getscilabmode() <> "STD" then
                error(msprintf(_("%s: can not add a menu in console in this mode: %s.\n"), "addmenu", getscilabmode()));
            end

            if size(varargin(1),"*") <> 1
                error(msprintf(_("%s: Wrong type for input argument #%d: A string expected.\n"),"addmenu",1));
            end

            if size(varargin(2)) <> 2
                error(msprintf(_("%s: Wrong type for input argument #%d: A two-item list expected.\n"),"addmenu",2));
            end

            if getos()=="Darwin" then
                // MACOSX version add a submenu with same name because parent menu can not have a callback
                addMenuSubMenusCallback(0, varargin(1), varargin(1), varargin(2));
            else
                addSingleMenuCallback(0, varargin(1), varargin(2));
            end

        elseif type(varargin(1))==1 & type(varargin(2))==10
            // addmenu(graphic_window_id,menu_label)
            // Create a menu in Scilab main Window
            // The callback is the variable named menu_label

            if size(varargin(1),"*") <> 1
                error(msprintf(_("%s: Wrong type for input argument #%d: A real expected.\n"),"addmenu", 1));
            end

            if size(varargin(2),"*") <> 1
                error(msprintf(_("%s: Wrong type for input argument #%d: A string expected.\n"),"addmenu", 2));
            end

            if getos()=="Darwin" then
                // MACOSX version add a submenu with same name because parent menu can not have a callback
                addMenuSubMenus(scf(varargin(1)), varargin(2), varargin(2));
            else
                addSingleMenu(scf(varargin(1)), varargin(2))
            end

        else
            error(msprintf(_("%s: Wrong input arguments: %s or %s expected.\n"),"addmenu","(menu_label, action_in_a_list)","(graphic_window_id,menu_label)"));
        end
    case 3
        if type(varargin(1))==10 & type(varargin(2))==10 & type(varargin(3))==15
            // addmenu(menu_label, submenus_labels, actions_in_a_list)
            // Create a menu in Scilab main Window with submenus
            // The callbacks are given in the third input argument

            // Error message in not in standard mode
            if getscilabmode() <> "STD" then
                error(msprintf(_("%s: can not add a menu in console in this mode: %s.\n"), "addmenu", getscilabmode()));
            end

            if size(varargin(1),"*") <> 1
                error(msprintf(_("%s: Wrong type for input argument #%d: A real expected.\n"),"addmenu",1));
            end

            // No verification for submenus labels size

            if size(varargin(3)) <> 2
                error(msprintf(_("%s: Wrong type for input argument #%d: A two-item list expected.\n"),"addmenu",3));
            end

            addMenuSubMenusCallback(0, varargin(1), varargin(2), varargin(3));

        elseif type(varargin(1))==1 & type(varargin(2))==10 & type(varargin(3))==10
            // addmenu(graphic_window_id, menu_label, submenus_labels)
            // Create a menu in Scilab main Window with submenus
            // The callbacks are the items of variable named menu_label

            if size(varargin(1),"*") <> 1
                error(msprintf(_("%s: Wrong type for input argument #%d: A real expected.\n"),"addmenu",1));
            end

            if size(varargin(2),"*") <> 1
                error(msprintf(_("%s: Wrong type for input argument #%d: A string expected.\n"),"addmenu",2));
            end

            // No verification for submenus labels

            addMenuSubMenus(scf(varargin(1)), varargin(2), varargin(3));

        elseif type(varargin(1))==1 & type(varargin(2))==10 & type(varargin(3))==15
            // addmenu(graphic_window_id, menu_label, action_in_a_list)
            // Create a menu in Scilab main Window with submenus
            // The callbacks are the items of variable named menu_label

            if size(varargin(1),"*") <> 1
                error(msprintf(_("%s: Wrong type for input argument #%d: A real expected.\n"),"addmenu", 1));
            end

            if size(varargin(2),"*") <> 1
                error(msprintf(_("%s: Wrong type for input argument #%d: A string expected.\n"),"addmenu",2));
            end

            if size(varargin(3)) <> 2
                error(msprintf(_("%s: Wrong type for input argument #%d: A two-item list expected.\n"),"addmenu",3));
            end

            if getos()=="Darwin" then
                addMenuSubMenusCallback(scf(varargin(1)), varargin(2), varargin(2), varargin(3));
            else
                addSingleMenuCallback(scf(varargin(1)), varargin(2), varargin(3));
            end

        else
            error(msprintf(_("%s: Wrong input arguments: %s, %s or %s expected.\n"),"addmenu","(menu_label, submenus_labels, actions_in_a_list)","(graphic_window_id, menu_label, submenus_labels)","(graphic_window_id, menu_label, action_in_a_list)"));
        end
    case 4
        // addmenu(graphic_window_id, menu_label, submenus_labels, action_in_a_list)
        // Create a menu in a Scilab graphic Window with submenus
        // The callbacks are given in a list

        if size(varargin(1),"*") <> 1
            error(msprintf(_("%s: Wrong type for input argument #%d: A real expected.\n"),"addmenu",1));
        end

        if size(varargin(2),"*") <> 1
            error(msprintf(_("%s: Wrong type for input argument #%d: A string expected.\n"),"addmenu",2));
        end

        // No verification for submenus labels

        if size(varargin(4)) <> 2
            error(msprintf(_("%s: Wrong type for input argument #%d: A two-item list expected.\n"),"addmenu",4));
        end

        addMenuSubMenusCallback(scf(varargin(1)), varargin(2), varargin(3), varargin(4));

    else
        error(msprintf(_("%s: Wrong number of input arguments: %d to %d expected.\n"),"addmenu", 1,4));
    end
endfunction
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// Add a menu with no callback given by the user
//------------------------------------------------------------------------------
function addSingleMenu(fig, menulabel)
    h = uimenu("parent", fig, "label", menulabel);
    if type(fig)==1
        set(h, "callback", list(4, "execstr("+menulabel+"(1))"));
    else
        set(h, "callback", list(4, "execstr("+menulabel+"_"+string(get(fig,"figure_id"))+"(1))"));
    end
endfunction
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
// Add a menu with callback given by the user
//------------------------------------------------------------------------------
function addSingleMenuCallback(fig, menulabel, callback)

    [callbackStr, callbackType] = getCallbackProperties(callback);

    h = uimenu("parent", fig, "label", menulabel);

    if callbackType == 0 then
        if type(fig)==1
            set(h, "callback", list(4, "execstr("+callbackStr+"(1))"));
        else
            set(h, "callback", list(4, "execstr("+callbackStr+"_"+string(get(fig,"figure_id"))+"(1))"));
        end
    elseif callbackType == 1 then
        if type(fig)==1
            cb = "call(""" + callbackStr + """,""" + ..
            menulabel + """,1,""c""," + ..
            "-1,2,""i""," + ..
            "1,3,""i"",""out"",[1,1],2,""i"")"; // Window number is returned
        else
            cb = "call(""" + callbackStr + """,""" + ..
            menulabel + """,1,""c""," + ..
            string(get(fig,"figure_id"))+ ",2,""i""," +..
            "1,3,""i""," + ..
            """out"",[1,1],2,""i"")"; // Window number is returned
        end
        set(h, "callback", list(4, "execstr("+sci2exp(cb)+")"));
    elseif callbackType == 2 then
        if type(fig)==1
            set(h, "callback", list(4, callbackStr+"(1)"));
        else
            set(h, "callback", list(4, callbackStr+"(1,"+string(get(fig,"figure_id"))+")"));
        end
    end

endfunction
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
// Add a menu and its submenus with no callback given by the user
//------------------------------------------------------------------------------
function addMenuSubMenus(fig, menulabel, submenuslabels)

    h0 = uimenu("parent", fig, "label", menulabel);

    for K=1:size(submenuslabels,"*")
        if type(fig)==1
            uimenu("parent", h0, "label", submenuslabels(K), "callback", list(4, "execstr("+menulabel+"("+string(K)+"))"));
        else
            uimenu("parent", h0, "label", submenuslabels(K), "callback", list(4, "execstr("+menulabel+"_"+string(get(fig,"figure_id"))+"("+string(K)+"))"));
        end
    end

endfunction
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
// Add a menu and its submenus with callback given by the user
//------------------------------------------------------------------------------
function addMenuSubMenusCallback(fig, menulabel, submenuslabels, callback)

    [callbackStr, callbackType] = getCallbackProperties(callback);

    h0 = uimenu("parent",fig,"label",menulabel);

    for k=1:size(submenuslabels,"*")
        h = uimenu("parent", h0, "label", submenuslabels(k));

        if callbackType == 0 then
            if type(fig)==1
                set(h, "callback", list(4, "execstr("+callbackStr+"("+string(k)+"))"));
            else
                set(h, "callback", list(4, "execstr("+callbackStr+"_"+string(get(fig,"figure_id"))+"("+string(k)+"))"));
            end
        elseif callbackType == 1 then
            if type(fig)==1
                cb = "call(""" + callbackStr + """,""" + ..
                menulabel + """,1,""c""," + ..
                "-1,2,""i""," + ..
                string(k-1) +",3,""i"",""out"",[1,1],2,""i"")"; // Window number is returned
            else
                cb = "call(""" + callbackStr + """,""" + ..
                menulabel + """,1,""c""," + ..
                string(get(fig,"figure_id")) + ",2,""i""," + ..
                string(k-1)+ ",3,""i""," + ..
                """out"",[1,1],2,""i"")"; // Window number is returned
            end
            set(h, "callback", list(4, "execstr("+sci2exp(cb)+")"));
        elseif callbackType == 2 then
            if type(fig)==1
                set(h, "callback", list(4, callbackStr+"("+string(k)+")"));
            else
                set(h, "callback", list(4, callbackStr+"("+string(k)+","+string(get(fig,"figure_id"))+")"));
            end
        end
    end
endfunction
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
// Manages callback given as list and check parameters
//------------------------------------------------------------------------------
function [callbackStr, callbackType] = getCallbackProperties(callback)
    callbackType = callback(1);
    if type(callbackType)<>1 | size(callbackType,"*")<>1
        error(msprintf(_("%s: Wrong value for ''%s'' property: %d, %d or %d expected.\n"), "addmenu", "CallbackType", 0, 1, 2));
    end

    callbackStr = callback(2);
    if size(callbackStr,"*")<>1
        error(msprintf(_("%s: Wrong type for ''%s'' property: A string expected.\n"), "addmenu", "Callback"));
    end
endfunction
//------------------------------------------------------------------------------