File: uimenu.tcl

package info (click to toggle)
scilab 2.6-4
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 54,632 kB
  • ctags: 40,267
  • sloc: ansic: 267,851; fortran: 166,549; sh: 10,005; makefile: 4,119; tcl: 1,070; cpp: 233; csh: 143; asm: 135; perl: 130; java: 39
file content (630 lines) | stat: -rw-r--r-- 15,495 bytes parent folder | download | duplicates (2)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
############################################
# TKSCILAB Gui Interface facility for Scilab
# Bertrand Guiheneuf - 1998 
############################################
#
# -->   uimenu.tcl



# ************************
# important remark : in TK, menu item are indexed begining from 1
# BUT in tcl, lists indexes begin from 0 !!!!!
# Be careful.
# ************************



# define the correspondance between the scilab 'uimenu'
# object and the tk 'menu' object
set StyTrad(menu)       uimenu;

# define the correspondance between the scilab 
# menu property name and the tk menu property name
set MenuPropTrad(label) label;
set MenuPropTrad(callback)  command;

proc GetRootMenu { hparent } {
    # is is used to know which is the menu associated 
    # to a toplevel window and to create it if it does 
    # not exist
    
    # INPUT: hparent : handle of the figure
    # OUTPUT: handle of the menu

    global Win WinIdx IdxMenu StyTrad;
    
    # get the real parent, ie if the parent doesn't
    # exist or is specified as 0 get the gcf.
    set parent [GetRealParent $hparent menu];

    # we will need this to get the path of the menu
    set ParentName $Win($parent);
    global "$ParentName";
    set ParentPath [set "$ParentName\(path)"];
 
    # Compute the instance menu name
    set name "$ParentName\menu" ;
    
    # if the menubar of the figure does not 
    # exist, create it!
    if { ![winfo exists $ParentPath.menu] } { 
	
	menu $ParentPath.menu;
	  
	# Update windows list
	set Win($WinIdx) "$name" ;
	# and increment the window counter


	# Store the menu win handle in its info structure 
	global  "$name";  
	set "$name\(handle)" $WinIdx;
	incr WinIdx;
  
	set "$name\(name)" $name;
	set "$name\(style)" rootmenu;
	set "$name\(control)" rootmenu;
	set "$name\(units)" "pixels";
	set "$name\(path)" $ParentPath.menu;
	set "$name\(parent)" $parent;
	set "$name\(menuparent)" 0;
	set "$name\(childrenmenu)" {};
	set "$name\(menustyle)" command;
    }

    return [set "$name\(handle)"];

}

proc CreateUIMenu { parent } {

    global Win WinIdx IdxMenu StyTrad;

    # get the real parent, ie if the parent doesn't
    # exist or is specified as 0 get the gcf.
#    set parent [GetRealParent $hparent menu];

    # we will need this to get the path of the menu
#    set ParentName $Win($parent);
#    global "$ParentName";
#    set ParentPath [set "$ParentName\(path)"];
    

    # if no control of this kind exist, initialise the counter
    # for this control. The variable containing the control counter
    # is Idx$control ie for ex. Idxpushbutton, Idxtext ...
    if { [info exists IdxMenu] != 1 } {
    set IdxMenu 0 ;
  }

  #get the current menu index and 
  #increment it
  set UiIdx $IdxMenu;
  incr IdxMenu;

  # Compute the instance control name
  set name "Menu$UiIdx" ;
  
  # Update windows list
  set Win($WinIdx) "$name" ;
  # and increment the window counter
  #puts "Creating $name";

  # Store the menu win handle in its info structure 
  global  "$name";  
  set "$name\(handle)" $WinIdx;
  incr WinIdx;
  
  set "$name\(name)" $name;
  set "$name\(style)" $StyTrad(menu);
  set "$name\(control)" menu;
  set "$name\(units)" "pixels";
  set "$name\(menustyle)" "command";
  set "$name\(childrenmenu)" {};

  # create the control and set its parent
  SetMenuParent $name $parent;
  return [set "$name\(handle)"];

}


######################################################################################
proc deleteMenu { name } {
    # delete a menu. The menu is deleted from its parent 
    # children list. Its pathh is destroyed but the menu still
    # exists. This routine is used to move menus.
    # INPUT : name : nqme of the menu
    # OUTPUT : void
    
    global "$name" Win;
    
    if {[info exists "$name\(path)"]} {
	set path [set "$name\(path)"];
	set hmenuParent [ set "$name\(menuparent)"];
	set menuParent "$Win($hmenuParent)";
	# debug info
	#puts "\nDeleting menu $name :";
	#puts " --> menu path : $path";
	#puts " --> menu's parent handle : $hmenuParent";
	#puts " --> menu's parent name : $menuParent";
	
	global "$menuParent";
	set parentChildList  [set "$menuParent\(childrenmenu)"];
	set index [set "$name\(menuindex)"];
	set listindex [expr $index -1];
	set "$menuParent\(childrenmenu)" [lreplace $parentChildList $listindex $listindex];
	set parentPath [set "$menuParent\(path)"];
	$parentPath delete [expr $index]; 
	
	if {[llength parentChildList]==1} {
	    #puts " --> Changing parent style : $menuParent becomes 'command'";
	    menuSetCommandStyle $menuParent
	}
	if [winfo exist $path] { destroy $path }
	
    }
    
}



######################################################################################
proc SetMenuParent { name phandle } {
    # set Parent field of a menu
    # this routine is also used to create a menu
    # it is also used to change menu parent. 
    #
    # input : 'name' variable name of the menu
    #         'phandle' parent handle
    # output : void

    global "$name" Win;
    
    
    
    set phandle [GetRealParent $phandle menu];
    # delete the menu if it already exists
    #puts "\nSetting $name parent handle to $phandle";
    # to speed up menu creation:
    # if parent is already the good one
    # do nothing
    if {[info exists "$name\(parent)"]} {
	
	set oldhandle [set "$name\(parent)"];
	if { $oldhandle == $phandle } { return; }
    }
    
    deleteMenu $name; 
    
    # we have to round the handle value
    # to prevent any numerical bias introduced 
    # by scilab from killing tk
    set phandle [expr round($phandle)];
    set style [set "$name\(style)"];     # scilab's control style
    set control [set "$name\(control)"]; # tk's control style
    
    # get the parent's name
    # end create it if it does not exist
    

    set parent "$Win($phandle)";
    # get the menu_parent
    # the parent and menu_parent are different only in the case when
    # the parent is a toplevel (figure) because the menu_parent is
    # then the tpolevel menu defined with the -menu option of a 
    # toplevel (only after tk8.0)
    
    set hmenuParent [GetRealMenuParent $phandle];
    set menuParent "$Win($hmenuParent)";
    #puts "--> $name real menu parent handle: $hmenuParent";
    #puts "--> $name real menu parent name: $menuParent";

    global "$menuParent";
    set menuParentpath [set "$menuParent\(path)"];


    lappend "$menuParent\(childrenmenu)" $name;
    set index [ llength [set "$menuParent\(childrenmenu)"] ];
    
    set path "$menuParentpath.m$index" ;
    
    set "$name\(parent)" $phandle;
    set "$name\(menuparent)" $hmenuParent;
    set "$name\(path)" $path;
    
    
    # default --> to change !
    menu $path ;



    
    set "$name\(menuindex)" $index;
    

    # redraw all parent children 
    # old UpdateParents $menuParent;
    UpdateParentAdd $name
}


proc UpdateParentAdd { name } {
    # upadte the parent of a menu : sets it as cascade 
    # if necessary.
    # INPUT: name : name of the menu
    #          
    # OUTPUT: void

    global $name Win;
    
    set hparent [ set "$name\(menuparent)"]; 
    set parent $Win($hparent);
    global $parent
    set ppath [ set "$parent\(path)"];
    set path [ set "$name\(path)"];

    set parentstyle  [ set "$parent\(menustyle)"];
    if {[string compare [ set "$parent\(style)"]  uimenu] == 0 } {
	if {[string compare $parentstyle 'cascade']!=0} {
	    menuSetCascadeStyle $parent;
	}
    }
    menuAddToParent $name;
    menuSetAllProp $name;
    MenuRedrawChildren $name;
}




proc menuAddToParent { name } {
    # add a menu to its parent
    # INPUT 
    #  name : name of the menu
    # OUTPUT : void

    global $name Win;

    set hparent [ set "$name\(menuparent)"];
    set parent $Win($hparent);
    global $parent
    set ppath [ set "$parent\(path)"];
    set path [ set "$name\(path)"];
    set index  [ set "$name\(menuindex)"];
    set menustyle [ set "$name\(menustyle)"];
    #$ppath delete $index;
    switch -exact -- $menustyle {
	cascade { $ppath insert $index cascade -menu $path }
	command { $ppath insert $index command  }
    }


}

proc menuSetCascadeStyle { name } {
    # change the menuStyle of a menu as a cascade
    # INPUT :
    #  name :  name of the menu which style to change
    #  
    # OUTPUT : void

    global Win $name;
    
    set hparent [ set "$name\(menuparent)"];

    # We change the style only when the menu is not a 
    # menubar (root menu)
    if { $hparent != 0 } {
	set parent $Win($hparent);
	global $parent;
	set ppath [ set "$parent\(path)"];
	set path [ set "$name\(path)"];
	set index  [ set "$name\(menuindex)"];
	$ppath delete $index;
	$ppath insert $index cascade -menu $path;
	#puts "\nReseting $name style as 'cascade', now posting $path";
	set "$name\(menustyle)" cascade;
	menuSetAllProp $name;
	
    }
}

proc menuSetCommandStyle { name } {
    # change the menuStyle of a menu as a command
    # INPUT :
    #  name :  name of the manu
    #  
    # OUTPUT : void

    global Win $name;
    
    set hparent [ set "$name\(menuparent)"];

    # We change the style only when the menu is not a 
    # menubar (root menu)
    if { $hparent != 0 } {
	set parent $Win($hparent);
	global $parent
	
	# debug info
	#puts "\nChanging menu style of $name to 'command'";
	#puts " --> $name parent handle : $hparent";
	#puts " --> $name parent name : hparent";

	set ppath [ set "$parent\(path)"];
	#puts " --> $name parent path : hparent";
	
	
	set index  [ set "$name\(menuindex)"];
	$ppath delete $index;
	$ppath insert $index command ;
	
	set "$name\(menustyle)" command;
	menuSetAllProp $name;
	
    }
}


######################################################################################
proc resetParent { name phandle } {
    # reset the path of a menu and recreate it
    # input : 'name' variable name of the menu
    #         'phandle' parent handle
    # output : void

    global "$name";
    set rdrw 0;
    
    if {[info exists "$name\(path)"]} {  
	set path [set "$name\(path)"];
	set rdrw 1;
	if [winfo exist $path] { destroy $path }
    }

    
  
    global Win;
    
    # get the parent's name
    set phandle [GetRealParent $phandle menu];
    
    
    set parent "$Win($phandle)";
    # get the menu_parent
    # the parent and menu_parent are different only in the case when
    # the parent is a toplevel (figure) because the menu_parent is
    # then the tpolevel menu defined with the -menu option of a 
    # toplevel (only after tk8.0)
    
    set hmenuParent [GetRealMenuParent $phandle];
     
    set menuParent "$Win($hmenuParent)";
    
    global "$menuParent";
    set menuParentpath [set "$menuParent\(path)"];
    
    set path "$menuParentpath.obj$name" ;
    set "$name\(path)" $path;
    
    # default --> to change !
    menu $path;

}






proc MenuRedrawChildren { name } {
    # redraws the children of a menu

    # INPUT: name : name of the menu which sons to 
    #          redraw
    # OUTPUT:
    
    global $name Win;

    
    # get the path of the menu
    set path [ set "$name\(path)"];
    
    # get the children list
    set ChildList [set "$name\(childrenmenu)"];
    set NbChild [llength $ChildList];
    
    # delete all the children menu
    $path delete 1 $NbChild;
    set handle [set "$name\(handle)"];
    # redraw all the children
    for {set i 0} { $i < $NbChild } {incr i} {
	# Get children name
	set cname [lindex $ChildList $i];
	resetParent $cname $handle;
	MenuRedrawChildren $cname
	global "$cname";
	# Get its path
	set cpath [set "$cname\(path)"];
	# and its style 
	set cstyle [set "$cname\(menustyle)"];
	
	# Now add the child menu to the current menu
	switch -exact -- $cstyle {
	    cascade { $path add cascade -menu $cpath; } 
	    command { $path add command; }
	}
	menuSetAllProp $cname;

    }
    
}



proc menuSetField { handle property value } {
    # set a menu property. The default behaviour is the following:
    # store the property string of the menu, and if the parent exists,
    # set the property. The parent presence can't be avoided because
    # in tk, the parent holds all the property.
    
    # INPUT: 
    #  handle : menu handle
    #  property: property to set up
    #  value: property value

    # OUTPUT: void
    
    global Win MenuPropTrad;
    set name $Win($handle);
    global "$name"; 
    
   
    
    switch -exact -- $property {
	parent {
	    SetMenuParent $name $value;
	}

	callback {
	    menuSetCallback $name $value;
	}
	
	default {
	    set parent [set "$name\(menuparent)"];
	    set parentName $Win($parent);
	    
	    # determinate the entry index in the parent 
	    # children list
	    global "$parentName";
	    set index [set "$name\(menuindex)"];
	    
	    
	    # set the property in the parent
	    set path [set "$parentName\(path)"];
	    
	    set tkprop [set "MenuPropTrad\($property)"];
	    $path entryconfigure $index -$tkprop $value;
	    set "$name\($property)" $value; 
	    #puts "\nSetting $name $property to $value:";
	    #puts " --> $name parent name : $parentName";
	    #puts " --> $name parent path : $path";
	    #puts " --> $name index in parent child list : $index";
	}
	
    }
}


proc menuSetAllProp { name } {
    # set all the property of a menu 

    # INPUT 
    #  name : menu name

    # OUTPUT : void

    global "$name";
    set handle [set "$name\(handle)"];

    foreach prop { label callback} {
	if { [info exists "$name\($prop)"] } {
	    set value [set "$name\($prop)"];
	    menuSetField $handle $prop $value;
	}

    }

}

    

proc GetRealMenuParent { hparent } {
    # determine the real parent of a menu.
    # This routined is usefull to take into account
    # the case where the parent is not a menu but a figure.
    
    # INPUT: hparent : handle of the parent
    # OUTPUT: handle of the real menu_parent

    
    global Win;
    # get the parent's name
    set parent "$Win($hparent)";
    global "$parent";
    
    # initialise the return value
    set handle $hparent;

    set parentStyle [set "$parent\(style)"];
    
    # if the parent is the figure, then the real parent
    # is the associated menubar.
    if { [string compare $parentStyle "figure"]==0 } {
	# when the figure has a menu, its name is:
	set parentname "$parent\menu";
	global "$parentname";
	
	if { [info exists $parentname ]==1 } {    
	    set handle [set "$parentname\(handle)"];
	} else {    
	    set handle [GetRootMenu $hparent ];
	}
    }
  
    
    return $handle;

}





proc menuSetCallback { name callback } {
    # set the command to execute when a menu is 
    # clicked on. When the man has children, the
    # command is executed before the submenu is posted
    # 
    # INPUT : 
    #   handle : handle of the menu
    #   callback : string containing the command 
    #             to execute (in scilab language)
    # OUTPUT : void

    global Win $name;

    

    set style [set "$name\(menustyle)"];
    set command "SciCallback $name $callback";
    
    switch -exact -- $style {

	command { 
	    set parent [set "$name\(menuparent)"];
	    set parentName $Win($parent);

	    global "$parentName";
	    set index [set "$name\(menuindex)"];
	    
	    # set the property in the parent
	    set path [set "$parentName\(path)"];
	    
	    $path entryconfigure $index -command $command;
	    set "$name\(callback)" $command; 

	}
    
	cascade {
	    set path [set "$name\(path)"];
	    $path configure -postcommand $command;
	    set "$name\(callback)" $command; 
	}

    }


}