pd.tk 304 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/wish
# Copyright (c) 1997-1999 Miller Puckette.
# For information on usage and redistribution, and for a DISCLAIMER OF ALL
# WARRANTIES, see the file, "LICENSE.txt," in this distribution.

# changed by Thomas Musil 09.2001
# between "pdtk_graph_dialog -- dialog window for graphs"
# and "pdtk_array_dialog -- dialog window for arrays"
# a new dialogbox was inserted, named:
# "pdtk_iemgui_dialog -- dialog window for iem guis"
#
12
# all these changes are labeled with #######iemlib########
Miller Puckette's avatar
Miller Puckette committed
13

Miller Puckette's avatar
Miller Puckette committed
14
# set pd_nt (bad name) 0 for unix, 1 for microsoft, and 2 for Mac OSX.
15
16
17
18
19

#May 22 2009
#GUI improvements and dynamic runtime 8.5 widget upgrade for Linux by Ivica Ico Bukvic
#http://ico.bukvic.net <ico@vt.edu>

20
21
22
23
24
25
# Mathieu's way of listing all defined procs
#rename proc proque
#proque proc {name args body} {
#  puts "proc $name"
#  uplevel 1 [list proque $name $args $body]
#}
26

27
#puts stderr [info tclversion]
28
29
30
31
32
##--moo: force default system and stdio encoding to UTF-8
encoding system utf-8
fconfigure stderr -encoding utf-8
fconfigure stdout -encoding utf-8
##--/moo
33

34
35
36
37
set appname [tk appname]
tk appname $appname-startup
#puts stderr "APPNAME [tk appname]"

38
39
40
41
42
if { [info tclversion] >= 8.5 } {

	# EDIT THIS PART TO CUSTOMIZE LINUX THEME
	# match gnome colors
	# Ubuntu Human
43
	# set linux_wm_bgcolor "#3e3d39"
44
45
	set linux_wm_bgcolor "#e5e5e5"
	# set linux_wm_hlcolor "#fdbf69"
46
47
48
	# set linux_wm_hlcolor "#f9ba81"
	# set linux_wm_hlcolor "#ffaa16"
	set linux_wm_hlcolor "#eb5f28"
49
50
51
52
53
54
55
56
57
58

	# ttk colors
	# -frame, -lighter, and -selectbg are automatically
	#	replaced by theme colors specified above
	variable colors
	array set colors {
		-frame 		"#e5e5e5"
		-window		"#ffffff"
		-darkest 	"#999999"
		-darker 	"#709970"
59
60
		#-dark		"#a8e6a8"
		-dark		"#bcbcbc"
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
		-light		"#d2ffd2"
		-lighter 	"#e5e5e5"
		-disabledfg	"#a3a3a3"
		-selectbg	"#fdbf69"
		-selectfg	"#000000"
	}
	# STOP EDITING BEYOND THIS POINT

	#sync ttk with theme choice above it
	set colors(-frame) $linux_wm_bgcolor
	set colors(-lighter) $linux_wm_bgcolor
	set colors(-selectbg) $linux_wm_hlcolor

	ttk::style theme create purty_linux -parent clam -settings {

		ttk::style configure "." \
			-background 	$colors(-frame) \
			-foreground 	black \
			-bordercolor	$colors(-darkest) \
			-selectbackground 	$colors(-selectbg) \
			-selectforeground 	$colors(-selectfg) \
			-troughcolor	$colors(-dark) \
			-arrowcolor 	$colors(-darkest) \
			-font 		TkDefaultFont \
			;

		ttk::style map "." -background \
			[list disabled $colors(-frame)  active $colors(-lighter)] ;
		ttk::style map "." -foreground [list disabled $colors(-disabledfg)] ;
		    ttk::style map "." -embossed [list disabled 0] ;

		ttk::style configure TButton \
			-anchor center -width -11 -padding "1 1" \
			-relief raised -shiftrelief 1 \
			-highlightthickness 1 -highlightcolor $colors(-selectbg)

		ttk::style map TButton -relief {
			{pressed !disabled} 	sunken
			{active !disabled} 	raised
		} -highlightcolor {alternate black}

		ttk::style configure TCheckbutton \
			-indicatorcolor $colors(-window) -padding 2
		ttk::style configure TRadiobutton \
			-indicatorcolor $colors(-window) -padding 2
		ttk::style map TCheckbutton -indicatorcolor \
			[list  disabled $colors(-frame)  pressed $colors(-frame)]
		ttk::style map TRadiobutton -indicatorcolor \
			[list  disabled $colors(-frame)  pressed $colors(-frame)]

		ttk::style configure TMenubutton \
			-width -11 -padding "3 3" -relief raised

		ttk::style configure TEntry \
			-padding 1 -fieldbackground $colors(-window) ;
		ttk::style map TEntry -fieldbackground \
			[list readonly $colors(-frame) disabled $colors(-frame)]
		ttk::style configure TCombobox \
			-padding 1 -fieldbackground $colors(-window) ;
		ttk::style map TCombobox -fieldbackground \
			[list readonly $colors(-frame) disabled $colors(-frame)]

		ttk::style configure Toolbutton -relief flat -padding 2
		ttk::style map Toolbutton -relief \
			{disabled flat selected sunken pressed sunken active raised}
		ttk::style map Toolbutton -background \
			[list pressed $colors(-dark)  active $colors(-light)]

		ttk::style configure TScrollbar -relief raised
		ttk::style configure TScale \
			-groovewidth 4 -troughrelief sunken \
			-sliderwidth raised -borderwidth 2
		ttk::style configure TProgressbar \
			-background $colors(-selectbg) -borderwidth 0

		ttk::style configure TLabelframe -relief groove -borderwidth 2

		ttk::style configure TNotebook -tabmargins {2 2 1 0}
		ttk::style configure TNotebook.Tab \
			-padding {4 2} -background $colors(-dark)
		ttk::style map TNotebook.Tab \
			-background [list selected $colors(-frame)] \
			-expand [list selected {2 2 1 0}] \
			;

		ttk::style configure Treeview -fieldbackground $colors(-window)
		ttk::style configure Heading -font TkHeadingFont -relief raised
		ttk::style configure Row -background $colors(-window)
		ttk::style map Row \
			-background [list selected $colors(-selectbg)]
		ttk::style map Item \
			-foreground [list selected $colors(-selectfg)]
		ttk::style map Cell \
			-foreground [list selected $colors(-selectfg)]
	}
}

# Automate matching of the linux wm and use ttk where possible
# As of right now it is only enabled if system is Linux/Unix using tcl >= 8.5
proc match_linux_wm {list} {
	global pd_nt linux_wm_bgcolor linux_wm_hlcolor
	if { [info tclversion] >= 8.5 && $pd_nt == 0 } {
		if {[lsearch -regexp $list ::] == -1} {
164
			#puts stderr [lindex $list 0]
165
166
167
168
			if {[lindex $list 0] != "button" \
				&& [lindex $list 0] != "checkbutton" \
				&& [lindex $list 0] != "radiobutton" \
				&& [lindex $list 0] != "entry" \
169
170
				&& [lindex $list 0] != "scrollbar" \
				&& [lindex $list 1] != "add"} { 
171
172
173
174
175
176
177
					lappend list -bg $linux_wm_bgcolor
			}
			if {[lindex $list 0] == "listbox" \
				|| [lindex $list 0] == "text"} {

				lappend list -bg white -highlightcolor $linux_wm_hlcolor
			}
178
179
180
181
182
			if {[lindex $list 1] == "add"} {
	
				#lappend list -background "#000000"
				set newlist [list [lindex $list 0] add command -label {} -state disabled]
			}
183
184
			if {[lindex $list 0] == "menu"} {
	
185
				lappend list -activebackground $linux_wm_hlcolor -bg "#3c3b37" -fg "#dddddd" -bd 0 -activeborderwidth 0 -relief flat -disabledforeground "#777777"
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
			}
			#convert non-ttk objects to ttk objects
			if {[lindex $list 0] == "button" \
				|| [lindex $list 0] == "checkbutton" \
				|| [lindex $list 0] == "radiobutton" \
				|| [lindex $list 0] == "entry" \
				|| [lindex $list 0] == "scrollbar"} { 
				set newlist [lreplace $list 0 0 ttk::[lindex $list 0]]
			}
		}
	}
	if {[info exists newlist]} {
		eval $newlist
	} else {
		eval $list
	}
}

Miller Puckette's avatar
Miller Puckette committed
204
205
if { $tcl_platform(platform) == "windows" }  {
    set pd_nt 1
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
206
    set ctrl_key "Control"
207
    # fontsc
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
208
    set defaultFontFamily {DejaVu Sans Mono}
209
    font create menuFont -family Tahoma -size 10
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
210
211
212
213
214
215
216
217
    # mouse cursors
    set cursor_runmode_nothing "arrow"
    set cursor_runmode_clickme "right_ptr"
    set cursor_runmode_thicken "sb_v_double_arrow"
    set cursor_runmode_addpoint "plus"
    set cursor_editmode_nothing "hand2"
    set cursor_editmode_connect "circle"
    set cursor_editmode_disconnect "X_cursor"
218
219
	set cursor_editmode_resize "sb_h_double_arrow"
	set cursor_editmode_resize_bottom_right "bottom_right_corner"
220
	set cursor_scroll "fleur"
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
221
222
223
224
225
226
227
228
229
230
231
232
233
	# set file types that open/save recognize
	set filetypes {
		{{Pd Files}         {.pd}  }
		{{Max Patch Files}  {.pat} }
		{{Max Text Files}   {.mxt} }
		{{Max Binary Files} {.mxb} }
		{{Max Help Files}   {.help} }
	}
	# use CommonProgramFiles by default instead because its a lot easier to
	# find. Microsoft seems to have carefully hidden the AppData folder from
	# all but the most determined users.
	#set externalsdir "$::env(AppData)/Pd"
	set externalsdir "$::env(CommonProgramFiles)/Pd"
Miller Puckette's avatar
Miller Puckette committed
234
235
} elseif { $tcl_platform(os) == "Darwin" } {  
    set pd_nt 2
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
236
237
	set ctrl_key "Mod1"
    # fonts
Miller Puckette's avatar
Miller Puckette committed
238
    set defaultFontFamily Monaco
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
239
240
241
242
243
244
245
246
    # mouse cursors
    set cursor_runmode_nothing "arrow"
    set cursor_runmode_clickme "center_ptr"
    set cursor_runmode_thicken "sb_v_double_arrow"
    set cursor_runmode_addpoint "plus"
    set cursor_editmode_nothing "hand2"
    set cursor_editmode_connect "circle"
    set cursor_editmode_disconnect "X_cursor"
247
248
	set cursor_editmode_resize "sb_h_double_arrow"
	set cursor_editmode_resize_bottom_right "bottom_right_corner"
249
	set cursor_scroll "fleur"
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
250
251
252
253
254
255
256
257
258
	# set file types that open/save recognize
	set filetypes {
		{{Pd Files}                {.pd}  }
		{{Max Patch Files (.pat)}  {.pat} }
		{{Max Text Files (.mxt)}   {.mxt} }
		{{Max Binary Files (.mxb)} {.mxb} }
		{{Max Help Files (.help)}  {.help} }
	}
	set externalsdir "$::env(HOME)/Library/Pd"
Miller Puckette's avatar
Miller Puckette committed
259
} else { 
260
261
	# set nicer theme
	ttk::style theme use purty_linux
262
	clipboard clear
263
	set window_prefs {}
Miller Puckette's avatar
Miller Puckette committed
264
    set pd_nt 0
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
265
    set ctrl_key "Control"
266
267
268
	# Shift modifier
	set shift_l_down 0
	set shift_r_down 0
269
270
271
	# Ctrl modifier
	set ctrl_l_down 0
	set ctrl_r_down 0
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
272
273
274
275
276
277
278
279
    # fonts
    set defaultFontFamily {DejaVu Sans Mono}
    # mouse cursors
    set cursor_runmode_nothing "left_ptr"
    set cursor_runmode_clickme "right_ptr"
    set cursor_runmode_thicken "sb_v_double_arrow"
    set cursor_runmode_addpoint "plus"
    set cursor_editmode_nothing "hand2"
280
    set cursor_editmode_connect "target"
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
281
    set cursor_editmode_disconnect "X_cursor"
282
283
	set cursor_editmode_resize "sb_h_double_arrow"
	set cursor_editmode_resize_bottom_right "bottom_right_corner"
284
	set cursor_scroll "fleur"
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
285
286
287
288
289
290
291
292
	# set file types that open/save recognize
	set filetypes {
		{{pd files}         {.pd}  }
		{{max patch files}  {.pat} }
		{{max text files}   {.mxt} }
		{{max binary files} {.mxb} }
		{{max help files}   {.help} }
	}
293
	set externalsdir "$::env(HOME)/pd-l2ork-externals"
Ivica Bukvic's avatar
Ivica Bukvic committed
294
295
}   

Ivica Bukvic's avatar
Ivica Bukvic committed
296
297
proc bgerror {message} {
	pdtk_post "\ntcl error: $message"
298
	#puts "\033\[0;1;33mtcl error: $::errorInfo\033\[0m"
Ivica Bukvic's avatar
Ivica Bukvic committed
299
}     
Miller Puckette's avatar
Miller Puckette committed
300

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
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
# make sure the user folder for installing externals exists
catch {
	if { ! [file exists $externalsdir] && [file exists [file dirname $externalsdir]] } {
		puts stderr "Creating folder for user-installed externals: \n\t$externalsdir"
		file mkdir $externalsdir
	}
}


# namespace for general-purpose functions
proc pdtk_encode { listdata } {
    set outlist {}
    foreach this_path $listdata {
        if {0==[string match "" $this_path]} {
            lappend outlist [pdtk_encodedialog $this_path]
        }
    }
    return $outlist
}


# args is a list of length 1 or 2,
# specifying optional additional x, y offsets for the window
proc center_window { w args } {
    set offx 0
    set offy 0

    if { [llength $args] >= 2 } {
        set offx [lindex $args 0]
        set offy [lindex $args 1]
    }

    set x [expr [winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
        - [winfo vrootx [winfo parent $w]] + $offx]
    set y [expr [winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
        - [winfo vrooty [winfo parent $w]] + $offy]
    wm geom $w +$x+$y
}

340
341
342
343
344
345
346
347
348
# backported workaround for pdsend
#proc pdsend { args } {
#	#puts stderr "===============pdsend pdtk $args"
#	pd [concat pd $args \;]
#}
proc ::pdsend {args} {
	#puts stderr "===============[join $args { }]"
	pd "[join $args { }] ;"
}
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
349

Miller Puckette's avatar
Miller Puckette committed
350
351
352
# start Pd-extended font hacks -----------------------------

# Pd-0.39.2-extended hacks to make font/box sizes the same across platform
353
#puts stderr "tk scaling is [tk scaling]"
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
354
tk scaling 1
Miller Puckette's avatar
Miller Puckette committed
355
356

# this font is for the Pd Window console text
357
font create console_font -family $defaultFontFamily -size 9 -weight normal
Miller Puckette's avatar
Miller Puckette committed
358
# this font is for text in Pd windows
359
font create text_font -family $defaultFontFamily -size 9 -weight normal
Miller Puckette's avatar
Miller Puckette committed
360
# for text in Properties Panels and other panes
361
font create highlight_font -family $defaultFontFamily -size 9 -weight bold
362
363
364
if { $pd_nt == 0 } {
	font create menufont -family $defaultFontFamily -size 9 -weight normal
}
Miller Puckette's avatar
Miller Puckette committed
365
366
367

# end Pd-extended font hacks -----------------------------

368
369
# K-12 version global variable (enabled through -k12, disabled by default)
set k12_mode 0
370
set k12_saveas_on_new 0
371
372
373
374

# Only can be enabled at startup (cannot be toggled afterwards)
proc pdtk_enable_k12_mode {extra_dir} {
	global k12_mode
375
#	global signal_cord_width
376
	global autotips
377
#	global signal_cord_highlight
378
#	global signal_cord
379
	global highlight_width
380
381

	set k12_mode 1
382
383
#	set signal_cord_width 5
	set ::pd_colors(signal_cord_width) 5
384
	set autotips 1
385
386
387
#	set signal_cord_highlight "#474"
	set ::pd_colors(signal_cord) "#2ca7d4"
	set ::pd_colors(signal_nlet) "#2ca7d4"
388
#	set signal_cord "#2ca7d4"
389
	set highlight_width 7
390

391
392
	image create photo i.wii_connect -file $extra_dir/K12/icons/wii_connect_on.png
	image create photo i.wii_buttons -file $extra_dir/K12/icons/wii_buttons.png
393
	image create photo i.wii_speed_xry -file $extra_dir/K12/icons/wii_speed_xry.png
394
395
	image create photo i.wii_speed -file $extra_dir/K12/icons/wii_speed.png
	image create photo i.wii_hit -file $extra_dir/K12/icons/wii_hit.png
396

397
398
399
400
401
402
	image create photo i.wii_accelerometer -file $extra_dir/K12/icons/wii_accelerometer.png
	image create photo i.wii_nunchuk_buttons -file $extra_dir/K12/icons/wii_nunchuk_buttons.png
	image create photo i.wii_nunchuk_hit -file $extra_dir/K12/icons/wii_nunchuk_hit.png
	image create photo i.wii_nunchuk_accelerometer -file $extra_dir/K12/icons/wii_nunchuk_accelerometer.png
	image create photo i.wii_nunchuk_stick -file $extra_dir/K12/icons/wii_nunchuk_stick.png

403
	image create photo i.sarcduino -file $extra_dir/K12/icons/sarcduino_on.png
404
405
406
	image create photo i.sarcduino_digital -file $extra_dir/K12/icons/sarcduino_digital.png
	image create photo i.sarcduino_analog -file $extra_dir/K12/icons/sarcduino_analog.png
	image create photo i.sarcduino_hit -file $extra_dir/K12/icons/sarcduino_hit.png
407
	image create photo i.sarcduino_piezo -file $extra_dir/K12/icons/sarcduino_piezo.png
408
409
	#image create photo i.sarcduino_net -file $extra_dir/K12/icons/sarcduino_net.png

410
411
412
	image create photo i.raspberry_digital -file $extra_dir/K12/icons/raspberrypi_gpio_digital_on.png
	image create photo i.raspberry_analog_out -file $extra_dir/K12/icons/raspberrypi_gpio_analog_out_on.png
	image create photo i.raspberry_analog_in -file $extra_dir/K12/icons/raspberrypi_3008_analog_in_on.png
413
	image create photo i.math_number -file $extra_dir/K12/icons/math_number.png
414
	image create photo i.math_netsend -file $extra_dir/K12/icons/math_netsend.png
415

416
417
418
	image create photo i.math_netreceive -file $extra_dir/K12/icons/math_netreceive.png
	image create photo i.math_tag -file $extra_dir/K12/icons/math_tag.png
	image create photo i.math_routebytag -file $extra_dir/K12/icons/math_routebytag.png
419
	image create photo i.math_average -file $extra_dir/K12/icons/math_average.png
420
	image create photo i.math_random -file $extra_dir/K12/icons/math_random.png
421

422
423
424
	image create photo i.math_add -file $extra_dir/K12/icons/math_add.png
	image create photo i.math_subtract -file $extra_dir/K12/icons/math_subtract.png
	image create photo i.math_multiply -file $extra_dir/K12/icons/math_multiply.png
425
	image create photo i.math_divide -file $extra_dir/K12/icons/math_divide.png
426
	image create photo i.math_scale -file $extra_dir/K12/icons/math_scale.png
427

428
	image create photo i.logic_compare -file $extra_dir/K12/icons/logic_compare.png
429
	image create photo i.logic_mapper -file $extra_dir/K12/icons/logic_mapper.png
430
431
432
433
	image create photo i.logic_metronome -file $extra_dir/K12/icons/logic_metronome.png
	image create photo i.logic_counter -file $extra_dir/K12/icons/logic_counter.png
	image create photo i.logic_sequencer -file $extra_dir/K12/icons/logic_sequencer.png

434
435
436
	image create photo i.preset -file $extra_dir/K12/icons/preset.png
	image create photo i.comment -file $extra_dir/K12/icons/comment.png

437
	image create photo i.signal_microphone -file $extra_dir/K12/icons/signal_microphone.png
438
439
	image create photo i.signal_netsend -file $extra_dir/K12/icons/signal_netsend.png
	image create photo i.signal_netreceive -file $extra_dir/K12/icons/signal_netreceive.png	
440
	image create photo i.signal_sampler -file $extra_dir/K12/icons/signal_sampler.png
441
	image create photo i.signal_player -file $extra_dir/K12/icons/signal_player.png
442
443
444
445
446

	image create photo i.signal_sine -file $extra_dir/K12/icons/signal_sine.png
	image create photo i.signal_saw -file $extra_dir/K12/icons/signal_saw.png
	image create photo i.signal_square -file $extra_dir/K12/icons/signal_square.png
	image create photo i.signal_triangle -file $extra_dir/K12/icons/signal_triangle.png
447
	image create photo i.signal_envelope -file $extra_dir/K12/icons/signal_envelope.png
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464

	image create photo i.signal_noise -file $extra_dir/K12/icons/signal_noise.png
	image create photo i.signal_pink -file $extra_dir/K12/icons/signal_pink.png
	image create photo i.signal_add -file $extra_dir/K12/icons/signal_add.png
	image create photo i.signal_multiply -file $extra_dir/K12/icons/signal_multiply.png

	image create photo i.instr_short1 -file $extra_dir/K12/icons/instr_short1.png
	image create photo i.instr_short2 -file $extra_dir/K12/icons/instr_short2.png
	image create photo i.instr_sustained1 -file $extra_dir/K12/icons/instr_sustained1.png
	image create photo i.instr_sustained2 -file $extra_dir/K12/icons/instr_sustained2.png

	image create photo i.fx_filter -file $extra_dir/K12/icons/fx_filter.png
	image create photo i.fx_multitap -file $extra_dir/K12/icons/fx_multitap.png
	image create photo i.fx_reverb -file $extra_dir/K12/icons/fx_reverb.png

	image create photo i.output -file $extra_dir/K12/icons/audio_on.png

465
466
	image create photo i.edit -file $extra_dir/K12/icons/edit.png
	image create photo i.perform -file $extra_dir/K12/icons/perform.png
467

468
469
470
471
472
	image create photo i.data -file $extra_dir/K12/icons/data.png
	image create photo i.sound -file $extra_dir/K12/icons/sound.png
	image create photo i.data_on -file $extra_dir/K12/icons/data_on.png
	image create photo i.sound_on -file $extra_dir/K12/icons/sound_on.png

473
474
	.mbar.file insert 2 command -label {K12 Demos} -command {menu_k12_open_demos}

475
	#pack forget .controls.switches.audiobutton
476
	#pack forget .controls.inout
477
	#.mbar.audio delete 0 2
478
479
}

480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# calculate offset on a movable canvas
set tmp_xpix 0
set tmp_ypix 0

set popup_xpix 0
set popup_ypix 0

# monitor pointer position
set pointer_x_local 0
set pointer_y_local 0

set pointer_x_virtual 0
set pointer_y_virtual 0

set pointer_x_global 0
set pointer_y_global 0

# text pasting global
set copytexttocanvas 0

500
set global_clipboard 0
501
502
# used for comparison to avoid redundant copyfromexternalbuffer
set last_clipboard 0
503
504
set global_selection 0

505
506
507
508
509
510
511
#TOOLTIPS PATCH
#TODO: make a separate tcl file for tooltips and put
#afterid in its scope    
variable afterid 0
variable duplicate_tags -1
variable current_window 0
variable nlet_color 0
512
variable autotips 0
513
#variable magicglass 0
514
variable tooltip_visible 0
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
# x:y location tooltip during dragging
set tooltip [toplevel .tooltip -bd 1 -bg black]
wm attributes $tooltip -topmost 1
wm overrideredirect $tooltip 1
pack [label $tooltip.label -bg lightyellow -fg black -text aaa -justify left]
wm withdraw $tooltip

# timeout for the tooltip
proc pdtk_toggle_xy_tooltip {name num} {
	global tooltip
	if {$num == 0} {
		wm withdraw $tooltip
	} else {
		wm deiconify $tooltip
	}
}

proc pdtk_update_xy_tooltip {name x y} {
	global tooltip
	global pointer_x_virtual pointer_y_virtual pointer_x_global pointer_y_global
	wm geometry $tooltip +[expr $pointer_x_global+15]+[expr $pointer_y_global-2]
	$tooltip.label configure -text [concat x: $x\ny: $y]
	set pointer_x_virtual $x
	set pointer_y_virtual $y
}

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
542
543
544
545
546
547
548
549
550
551
552
553
# begin hack to hide hidden files/folder in tk browser on unix ----------
if {$pd_nt == 0} {
    # load the dialog once, otherwise setting the vars will not work
    catch {tk_getOpenFile -with-invalid-argument} 

    # change the environment variables
    namespace eval ::tk::dialog::file {
    variable showHiddenBtn 1
    variable showHiddenVar 0
    }
}
# end hidden files/folder hack ------------------
Miller Puckette's avatar
Miller Puckette committed
554

555
# Tearoff is set to false by default:
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
556
set pd_tearoff 0
557
558
# Put menu tearoff is set to true by default:
set put_tearoff 1
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
559
560
# turn off tearoff menus globally
#option add *tearOff 0
Miller Puckette's avatar
Miller Puckette committed
561
562
563
564
565
566
567
568

# jsarlo
set pd_array_listview_pagesize 1000
set pd_array_listview_id(0) 0
set pd_array_listview_entry(0) 0
set pd_array_listview_page(0) 0
# end jsarlo

569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# Prefences Dialog
# dialog_prefs.tcl must come first because dialog_gui.tcl depends on it
# This is an ugly hack-- it should just be loaded as tcl packages like
# in 0.43...
source [file join [file dirname [info script]] dialog_prefs.tcl]
source [file join [file dirname [info script]] dialog_gui.tcl]
source [file join [file dirname [info script]] dialog_midi.tcl]
source [file join [file dirname [info script]] dialog_audio.tcl]

# Add a trace for the color array. This way any gui-plugin can make
# color changes simply by changing one of the elements of the pd_colors
# array, and all the preference dialog stuff and open patches will
# magically update
trace add variable pd_colors write ::dialog_prefs::set_color

# ... and a trace for the ::gui_preset variable.  There's probably
# a way to do this inside the dropdown code, which would be better...
trace add variable gui_preset write "::dialog_gui::set_gui_preset"

# Now that we've got a trace for the colors, we can set a
# color scheme (defined in dialog_gui.tcl)
590
set gui_preset L2Ork
591
592
593
594
595

# todo: find a home for these:
set ::pd_colors(signal_cord_width) 3
set ::pd_colors(control_cord_width) 1

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
596
# color scheme
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# set canvas_fill "white"
# set text_color "#000"
# set select_color "#e87216"
# set dash_outline "#f00"
# set dash_fill "#f7f7f7"
# set box_outline "#ccc"
# set graph_outline "#777"
# set atom_box_fill "#eee"
# set msg_box_fill "#f8f8f6"
# set obj_box_fill "#f6f8f8"
# Doesn't look like signal_cord_highlight is used
# set signal_cord_highlight "#58a"
# set signal_cord "#808095"
# set signal_nlet $signal_cord
# Todo: make sure these work in k12 mode
# set signal_cord_width 3
# set msg_cord_highlight "#474"
# set msg_cord "#565"
# set msg_nlet "white"
# set mixed_nlet "#88aaff"
# set msg_cord_width 1
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
618

619
#nlet highlighting stuff
620
set select_nlet_color $pd_colors(selection)
621
set highlight_width 5
622

Miller Puckette's avatar
Miller Puckette committed
623
624
625
626
if {$pd_nt == 1} {
    global pd_guidir
    set pd_gui2 [string range $argv0 0 [expr [string last \\ $argv0 ] - 1]]
    regsub -all \\\\ $pd_gui2 / pd_gui3
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
627
    set pd_guidir [file normalize $pd_gui3/..]
Miller Puckette's avatar
Miller Puckette committed
628
629
630
631
    load $pd_guidir/bin/pdtcl.dll
}

if {$pd_nt == 2} {
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
632
    # turn on James Tittle II's fast drawing
Miller Puckette's avatar
Miller Puckette committed
633
    set tk::mac::useCGDrawing 1
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
634
635
    # anti-alias all lines that need it
    set tk::mac::CGAntialiasLimit 0
Miller Puckette's avatar
Miller Puckette committed
636
637
    global pd_guidir
    set pd_gui2 [string range $argv0 0 [expr [string last / $argv0 ] - 1]]
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
638
    set pd_guidir [file normalize $pd_gui2/..]
Miller Puckette's avatar
Miller Puckette committed
639
640
641
642
643
644
645
646
647
648
649
650
651
    load $pd_guidir/bin/libPdTcl.dylib
    global pd_macready
    set pd_macready 0
    global pd_macdropped
    set pd_macdropped ""
    # tk::mac::OpenDocument is called with the filenames put into the 
    # var args whenever docs are either dropped on the Pd.app icon or 
    # opened from the Finder.
    # It uses menu_doc_open so it can handles numerous file types.
    proc tk::mac::OpenDocument {args} {
        global pd_macready pd_macdropped
        foreach file $args {
            if {$pd_macready != 0} {
652
653
                pd [concat pd open [pdtk_enquote [file tail $file]] \
                        [pdtk_enquote  [file dirname $file]] \;]
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
654
                menu_doc_open [file dirname $file] [file tail $file]
Miller Puckette's avatar
Miller Puckette committed
655
656
657
658
659
            } else {
                set pd_macdropped $args
            }
        }
    }
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
660
661
662
663
664
	# intercept kAEQuitApplication from Cmd-Q
	proc ::tk::mac::Quit {} {
		puts stderr "::tk::mac::Quit"
		menu_quit
	}
Miller Puckette's avatar
Miller Puckette committed
665
666
667
668
669
670
671
672
673
674
675
676
677
}

# hack so you can easily test-run this script in linux... define pd_guidir
# (which is normally defined at startup in pd under linux...)

if {$pd_nt == 0} {
    if {! [info exists pd_guidir]} {
        global pd_guidir
        puts stderr {setting pd_guidir to '.'}
        set pd_guidir .
    }
}

678
679
#set pd_deffont {courier 10}
set pd_deffont console_font
Miller Puckette's avatar
Miller Puckette committed
680
681
682

set help_top_directory $pd_guidir/doc

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
683
684
685
#==============================================================================#
# pd-gui-rewrite-0.43 help browser backport

686
687
688
689
690
691
switch -- $pd_nt {
    0  { set ::windowingsystem "x11" }
    1  { set ::windowingsystem "win32" }
    2  { set ::windowingsystem "aqua" }
}

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
692
693
694
695
696
# Pd's packages are stored in the same directory as the main script (pd-gui.tcl)
set auto_path [linsert $auto_path 0 [file dirname [info script]]]
# the pd-gui-rewrite-0.43 help browser is backported to Pd-extended 0.42
package require helpbrowser
package require msgcat
697
package require tkpng
698
package require tkdnd
699
700
701
702
703
704
705
706
707
package require pd_guiprefs
package require pd_menus

namespace import ::pd_guiprefs::init
namespace import ::pd_guiprefs::update_recentfiles
namespace import ::pd_guiprefs::write_recentfiles

set total_recentfiles 5
::pd_guiprefs::init
Miller Puckette's avatar
Miller Puckette committed
708

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
709
710
711
712
713
714
715
716
717
718
719
720
721
# official GNU gettext msgcat shortcut
proc _ {s} {return [::msgcat::mc $s]}

namespace eval ::pdwindow:: {
    proc verbose {level postme} {puts stderr "$level: $postme"}
    proc error {postme} {puts stderr $postme; pdtk_post $postme}
    proc warn {postme} {::pdwindow::error $postme}
}

set ::dialog_menubar .mbar
set ::modifier $ctrl_key
set ::sys_libdir $pd_guidir
switch $pd_nt {
722
    0 {set ::pd_path [list ~/pd-l2ork-externals /usr/local/lib/pd-l2ork-externals]}
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
723
724
725
726
727
    1 {set ::pd_path [list [file normalize $::env(AppData)/Pd] \
                          [file normalize $::env(CommonProgramFiles)/Pd]]}
    2 {set ::pd_path [list ~/Library/Pd /Library/Pd]}
}
#==============================================================================#
Miller Puckette's avatar
Miller Puckette committed
728
729
730
731
732

################## set up main window #########################
# the menus are instantiated here for the main window
# for the patch windows, they are created by pdtk_canvas_new

733
734
735
736
737
738
739
740
#ttk error button customizations
ttk::style configure IOErrorOn.TButton -background "#dd2222"
ttk::style map IOErrorOn.TButton -background [list active "#ff2222"] \
	-foreground [list disabled black]
ttk::style configure IOErrorOff.TButton -background lightgrey
ttk::style map IOErrorOff.TButton -background [list active $colors(-frame)]

match_linux_wm [list menu .mbar -relief flat]
741
match_linux_wm [list menu .mbar.file -tearoff $pd_tearoff -postcommand [concat pdtk_fixfilemenu .mbar.file]]
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
742
.mbar add cascade -label File -menu .mbar.file
743
match_linux_wm [list menu .mbar.edit -tearoff $pd_tearoff]
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
744
.mbar add cascade -label Edit -menu .mbar.edit
745
match_linux_wm [list menu .mbar.put -tearoff $pd_tearoff]
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
746
.mbar add cascade -label Put -menu .mbar.put
747
748
749
750
751
#match_linux_wm [list menu .mbar.find -tearoff $put_tearoff]
#.mbar add cascade -label Find -menu .mbar.find
match_linux_wm [list menu .mbar.windows -postcommand [concat pdtk_fixwindowmenu] \
	-tearoff $pd_tearoff]
match_linux_wm [list menu .mbar.audio -tearoff $pd_tearoff]
Miller Puckette's avatar
Miller Puckette committed
752
753
754
if {$pd_nt != 2} {
    .mbar add cascade -label "Windows" -menu .mbar.windows
    .mbar add cascade -label "Media" -menu .mbar.audio
755
    match_linux_wm [list menu .mbar.help -tearoff $pd_tearoff]
Miller Puckette's avatar
Miller Puckette committed
756
757
758
759
    .mbar add cascade -label "Help" -menu .mbar.help
} else {
    menu .mbar.apple -tearoff 0
    .mbar add cascade -label "Apple" -menu .mbar.apple 
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
760
    # arrange menus according to Apple HIG
Miller Puckette's avatar
Miller Puckette committed
761
762
    .mbar add cascade -label "Media" -menu .mbar.audio
    .mbar add cascade -label "Window" -menu .mbar.windows
763
    match_linux_wm [list menu .mbar.help -tearoff $pd_tearoff]
Miller Puckette's avatar
Miller Puckette committed
764
765
766
    .mbar add cascade -label "Help" -menu .mbar.help
}

Miller Puckette's avatar
Miller Puckette committed
767
768
769
# fix menu font size on Windows with tk scaling = 1
if {$pd_nt == 1} {
    .mbar.file configure -font menuFont
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
770
    .mbar.edit configure -font menuFont
Miller Puckette's avatar
Miller Puckette committed
771
772
773
774
775
776
    .mbar.find configure -font menuFont
    .mbar.windows configure -font menuFont
    .mbar.audio configure -font menuFont
    .mbar.help configure -font menuFont
}

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
777
778
proc showhide_printouttext {state} {
    if {$state} {
779
780
        #pack .printout -side bottom -fill both -expand 1
		#pack .controls.clr_console -pady 30
781
782
783
		wm deiconify .printout
		#wm resizable . 1 1
		#wm minsize . 460 194
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
784
    } else {
785
        #pack forget .printout
786
787
788
789
790
		wm withdraw .printout
		#pack forget .controls.clr_console
		#wm minsize . 0 0
		#wm geometry . 355x75
		#wm resizable . 0 0
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
791
792
793
    }
}

Miller Puckette's avatar
Miller Puckette committed
794
795
796
797
set ctrls_audio_on 0
set ctrls_meter_on 0
set ctrls_inlevel 0
set ctrls_outlevel 0
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
798
set show_text_window 0
Miller Puckette's avatar
Miller Puckette committed
799

800
match_linux_wm [list frame .controls]
801
802
803
#if { [info tclversion] >= 8.5 && $pd_nt == 0 } {
#	ttk::separator .eyecandy
#	pack .eyecandy .controls -fill x -expand 0
804
#} else {checkbutton
805
806
pack .controls -fill x -expand 1
#}
807
808
match_linux_wm [list frame .controls.switches]
match_linux_wm [list checkbutton .controls.switches.audiobutton -text {compute audio  } \
Miller Puckette's avatar
Miller Puckette committed
809
    -variable ctrls_audio_on \
810
    -command {pd [concat pd dsp $ctrls_audio_on \;]}]
Miller Puckette's avatar
Miller Puckette committed
811

812
match_linux_wm [list checkbutton .controls.switches.meterbutton -text {peak meters  } \
Miller Puckette's avatar
Miller Puckette committed
813
    -variable ctrls_meter_on \
814
    -command {pd [concat pd meters $ctrls_meter_on \;]}]
Miller Puckette's avatar
Miller Puckette committed
815

816
match_linux_wm [list checkbutton .controls.switches.console -text "console" \
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
817
    -variable show_console \
818
    -command {showhide_printouttext $show_console}]
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
819

Miller Puckette's avatar
Miller Puckette committed
820
pack .controls.switches.audiobutton .controls.switches.meterbutton \
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
821
    .controls.switches.console -side top -anchor w
Miller Puckette's avatar
Miller Puckette committed
822

823
824
825
826
827
828
829
830
831
832
833
834
835
836
match_linux_wm [list frame .controls.inout]
match_linux_wm [list frame .controls.inout.in]
match_linux_wm [list label .controls.inout.in.label -text IN]
entry .controls.inout.in.level -textvariable ctrls_inlevel -width 3 \
	-highlightthickness 0 -takefocus 0 -state readonly -readonlybackground white
if { [info tclversion] >= 8.5 && $pd_nt == 0 } {
	match_linux_wm [list button .controls.inout.in.clip \
		-text {CLIP} -state disabled -style IOErrorOff.TButton -width 4]
} else {
	button .controls.inout.in.clip -text {CLIP} -state disabled
}

match_linux_wm [list frame .controls.inout.in.spacer]

Miller Puckette's avatar
Miller Puckette committed
837
pack .controls.inout.in.label .controls.inout.in.level \
838
839
840
841
842
843
844
845
846
847
848
849
850
851
    .controls.inout.in.clip .controls.inout.in.spacer -side top -pady 1

match_linux_wm [list frame .controls.inout.out]
match_linux_wm [list label .controls.inout.out.label -text OUT]
entry .controls.inout.out.level -textvariable ctrls_outlevel -width 3 \
	 -highlightthickness 0 -takefocus 0 -state readonly -readonlybackground white
if { [info tclversion] >= 8.5 && $pd_nt == 0 } {
	match_linux_wm [list button .controls.inout.out.clip \
		-text {CLIP} -state disabled -style IOErrorOff.TButton -width 4]
} else {
	button .controls.inout.out.clip -text {CLIP} -state disabled
}

match_linux_wm [list frame .controls.inout.out.spacer]
Miller Puckette's avatar
Miller Puckette committed
852
853

pack .controls.inout.out.label .controls.inout.out.level \
854
855
856
857
858
859
860
861
862
863
864
865
866
867
    .controls.inout.out.clip .controls.inout.out.spacer -side top -pady 1

if { [info tclversion] >= 8.5 && $pd_nt == 0 } {
	match_linux_wm [list ttk::button .controls.dio -text "\nIO Errors\n" \
		-command { if {![info exists show_console] || $show_console == 0} \
		{ menu_toggle_console; }; \
		pd [concat pd audiostatus \;]} \
		-width 8 -style IOErrorOff.TButton]
} else {
	button .controls.dio -text "DIO\nerrors" \
		-command { if {![info exists show_console] || $show_console == 0} \ 
		{ .controls.switches.console invoke; }; \
		pd [concat pd audiostatus \;]}
}
Miller Puckette's avatar
Miller Puckette committed
868

869
870
#match_linux_wm [list button .controls.clr_console -text "Clear Console" \
#	-command [concat menu_clear_console]]
Miller Puckette's avatar
Miller Puckette committed
871
872
873

pack .controls.inout.in .controls.inout.out -side left -padx 6
pack .controls.inout -side left -padx 14
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
874
875
876
pack .controls.switches -side right
pack .controls.dio -side right -padx 20

877
878
879
#if {[info exists show_console] && $show_console == 1} {
#	pack .controls.clr_console -pady 30
#}
Miller Puckette's avatar
Miller Puckette committed
880

881
toplevel .printout -class [winfo class .]
882
wm title .printout "Console"
883
# initial location of the console window (+x+y)
884
wm geometry .printout +10+170
885
# wm minsize .printout 460 194
886
wm protocol .printout WM_DELETE_WINDOW { .controls.switches.console invoke }
887
888
match_linux_wm [list frame .printout.frame]
text .printout.frame.text -relief sunken -bd 1 -font console_font \
889
    -yscrollcommand ".printout.frame.scroll set"  -width 70 -height 24 \
890
	-highlightthickness 0 -takefocus 0 -fg gray20 -state disabled
891
892
893
894
895
896
897
898
899
900
#.printout.frame.text tag configure sel -background $linux_wm_hlcolor

# .printout.frame.text insert end "\n\n\n\n\n\n\n\n\n\n"
match_linux_wm [list scrollbar .printout.frame.scroll -command ".printout.frame.text yview"]
match_linux_wm [list frame .printout.bar]
match_linux_wm [list button .printout.bar.clear -text "CLEAR" \
	-width 7 -command [concat menu_clear_console]]
match_linux_wm [list entry .printout.bar.entry \
	-textvariable send_textvariable]
bind .printout.bar.entry <KeyPress-Return> {
901
	pdtk_post "\nmessage-to-pd: $send_textvariable"
902
903
904
905
    pd [concat $send_textvariable \;]
}
#match_linux_wm [list button .printout.bar.x -text "x" \
#	-width 2 -command [concat .printout.bar.entry delete 0 end]]
906
match_linux_wm [list label .printout.bar.label -text "Send Message to Pd:"]
907
908
909

pack .printout.frame.scroll -side right -fill y
pack .printout.frame.text -side left -fill both -expand 1
910
pack .printout.bar -side bottom -fill x -expand 0
911
912
913
pack .printout.frame -fill both -expand 1
pack .printout.bar.clear -side left -padx 3 -pady 3
#pack .printout.bar.x -side right -padx 3 -pady 3
Ivica Bukvic's avatar
Ivica Bukvic committed
914
pack .printout.bar.entry -side right -fill both -expand 1 -padx 3 -pady 3
915
pack .printout.bar.label -side right -padx 3 -pady 3
Miller Puckette's avatar
Miller Puckette committed
916

917
918
919
# the console is open by default
#.controls.switches.console select
# in the case console should be turned off by default
920
921
922
923
wm withdraw .printout
#wm geometry .printout 1x1+0+0
#wm overrideredirect .printout 1
#wm transient .printout
924

925
926
bind .printout.frame.text <Button> {pdtk_update_root_edit_menu 0}
bind .printout.frame.text <<Selection>> {pdtk_update_root_edit_menu 1}
927
928
bind .printout <Control-Next> {menu_raisenextwindow}
bind .printout <Control-Prior> {menu_raisepreviouswindow} ;# needs Tcl/Tk 8.5
929
bind .printout <Control-w> { .controls.switches.console invoke }
930
931

proc pdtk_update_root_edit_menu {value} {
932
	#pdtk_post "update_root_edit_menu $value\n"
933
	if {$value} {
934
		set range [.printout.frame.text tag ranges sel]
935
936
		#pdtk_post "[lindex $range 0] -- [lindex $range 1]\n"
		if {[lindex $range 0] != [lindex $range 1]} {
937
			#.mbar.edit entryconfigure "Cut" -state normal
938
939
940
941
			.mbar.edit entryconfigure "Copy" -state normal
		}
	} else {
		#pdtk_post off
942
		#.mbar.edit entryconfigure "Cut" -state disabled
943
944
945
946
		.mbar.edit entryconfigure "Copy" -state disabled
	}
}

Miller Puckette's avatar
Miller Puckette committed
947
proc pdtk_post {stuff} {
948
949
	.printout.frame.text configure -state normal
    .printout.frame.text insert end $stuff
950
951
    after cancel .printout.frame.text yview end-2char
    after idle .printout.frame.text yview end-2char
952
	.printout.frame.text configure -state disabled
Miller Puckette's avatar
Miller Puckette committed
953
954
}

955
956
957
958
959
960
961
962
963
964
965
966
proc pderror_select_by_id {objectid} {
    if [llength $objectid] {
        pdsend "pd findinstance $objectid"
    }
}

# stopgap for ::pdwindow::logpost
proc pdtk_posterror {objectid loglevel errormsg} {
    set t .printout.frame.text
    $t configure -state normal
    # if we have an object id, make a friendly link...
    if {$objectid ne ""} {
967
        $t insert end "error" [list obj$objectid errorlink]
968
969
970
        $t tag bind obj$objectid <1> "pderror_select_by_id $objectid; break"
        $t tag bind obj$objectid <Enter> "$t configure -cursor hand2"
        $t tag bind obj$objectid <Leave> "$t configure -cursor xterm"
971
972
        $t tag configure errorlink -foreground "$::pd_colors(dash_outline)" \
            -underline on -background "$::pd_colors(dash_fill)"
973
974
975
976
977
978
979
980
981
982
983
    } else {
        $t insert end "error"
    }
    $t insert end ": "
    $t insert end $errormsg

    after cancel .printout.frame.text yview end-2char
    after idle .printout.frame.text yview end-2char
	.printout.frame.text configure -state disabled
}

984
985
986
987
988
989
990
991
proc pdtk_k12panel_standardkeybindings {id} {
    global pd_nt ctrl_key
    set names [split $id .]
    set name [concat .[lindex $names 1].c]
    bind $id <$ctrl_key-Key> "pdtk_canvas_ctrlkey $name %K 0"
	bind $id <$ctrl_key-Shift-Key> "pdtk_canvas_ctrlkey $name %K 1"
}

Miller Puckette's avatar
Miller Puckette committed
992
proc pdtk_standardkeybindings {id} {
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
993
994
995
996
997
998
999
1000
1001
1002
1003
    global pd_nt ctrl_key
    bind $id <$ctrl_key-Key> {pdtk_canvas_ctrlkey %W %K 0}
    bind $id <$ctrl_key-Shift-Key> {pdtk_canvas_ctrlkey %W %K 1}
}

proc pdtk_panelkeybindings {id panelname} {
    global pd_nt ctrl_key
    pdtk_standardkeybindings $id
    bind $id <KeyPress-Escape> [format "%s_cancel %s" $panelname $id]
    bind $id <KeyPress-Return> [format "%s_ok %s" $panelname $id]
    bind $id <$ctrl_key-Key-w> [format "%s_cancel %s" $panelname $id]
Miller Puckette's avatar
Miller Puckette committed
1004
1005
1006
}

pdtk_standardkeybindings .
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1007
# hacks to add standard key bindings to the Pd window
1008
1009
bind . <$ctrl_key-Key-a> {.printout.frame.text tag add sel 1.0 end}
#bind . <$ctrl_key-Key-x> {tk_textCut .printout.frame.text; \
1010
#	.mbar.edit entryconfigure "Paste" -state normal}
1011
bind . <$ctrl_key-Key-c> {tk_textCopy .printout.frame.text}
1012
bind .printout <$ctrl_key-Key-c> {tk_textCopy .printout.frame.text}
1013
bind .printout <$ctrl_key-Key-a> {.printout.frame.text tag add sel 1.0 end}
1014
#	.mbar.edit entryconfigure "Paste" -state normal}
1015
#bind . <$ctrl_key-Key-v> {tk_textPaste .printout.frame.text}
1016
bind . <$ctrl_key-Key-w> {menu_quit}
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1017
1018
1019
1020
1021
# kludge to add window control bindings to the Pd Window
if {$pd_nt == 2} {
	bind . <Mod1-quoteleft>  {menu_raisenextwindow}
} else {
	bind . <Control-Next>    {menu_raisenextwindow}
Ivica Bukvic's avatar
Ivica Bukvic committed
1022
	bind . <Control-Prior>   {menu_raisepreviouswindow} ;# needs Tcl/Tk 8.5
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1023
}
Miller Puckette's avatar
Miller Puckette committed
1024

1025
# pass key presses inside main window to patch windows
1026
proc pdtk_capture_root_window_keys {state key iso shift serial} {
1027
1028
1029
1030
1031
1032
	global menu_windowlist

	set first_patch_window_name [lindex $menu_windowlist 0 1]
	# pdtk_post "$first_patch_window_name\n"

	if { [string length $first_patch_window_name] > 0 } {
1033
		pdtk_canvas_sendkey $first_patch_window_name.c $state $key $iso $shift 0 $serial
1034
1035
1036
	}
}

1037
1038
1039
bind . <Key> {pdtk_capture_root_window_keys 1 %K %A 0 %t}
bind . <Shift-Key> {pdtk_capture_root_window_keys 1 %K %A 1 %t}
bind . <KeyRelease> {pdtk_capture_root_window_keys 0 %K %A 0 %t}
1040
1041
1042
bind . <Control-Key> {pdtk_canvas_ctrlkey %W %K 0}
bind . <Control-Shift-Key> {pdtk_canvas_ctrlkey %W %K 1}

1043
1044
1045
bind .printout <Key> {pdtk_capture_root_window_keys 1 %K %A 0 %t}
bind .printout <Shift-Key> {pdtk_capture_root_window_keys 1 %K %A 1 %t}
bind .printout <KeyRelease> {pdtk_capture_root_window_keys 0 %K %A 0 %t}
1046
1047
bind .printout <Control-Key> {pdtk_canvas_ctrlkey %W %K 0}
bind .printout <Control-Shift-Key> {pdtk_canvas_ctrlkey %W %K 1}
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1048

1049
wm title . "Pure-Data L2Ork"
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1050
# initial location of Pd window (+x+y)
1051
wm geometry . +10+35
1052
catch {
Ivica Bukvic's avatar
Ivica Bukvic committed
1053
	set appicon [image create photo -format gif -file "$pd_guidir/pd-l2ork.gif"]
1054
1055
	wm iconphoto . -default $appicon
}
1056
1057
. configure -menu .mbar -width 200 -height 150
if { [info tclversion] >= 8.5 && $pd_nt == 0 } { wm resizable . 0 0 }
Miller Puckette's avatar
Miller Puckette committed
1058
1059
# Intercept closing the main pd window: MP 20060413:
wm protocol . WM_DELETE_WINDOW menu_quit
1060
focus .
1061
1062
1063
1064
1065
after idle {
	dnd bindtarget . text/uri-list <Drop> { foreach file %D {open_file $file} }
	dnd bindtarget .printout.frame text/uri-list <Drop> { foreach file %D {open_file $file} }
	dnd bindtarget .printout.bar text/uri-list <Drop> { foreach file %D {open_file $file} }
}
Miller Puckette's avatar
Miller Puckette committed
1066

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1067

Miller Puckette's avatar
Miller Puckette committed
1068
1069
1070
############### set up global variables ################################

set untitled_number 1
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1071
1072
1073
1074
1075
if {$pd_nt == 2} {
    set untitled_directory $::env(HOME)
} else {
    set untitled_directory [pwd]
}
Miller Puckette's avatar
Miller Puckette committed
1076
1077
set pd_opendir $untitled_directory
set pd_savedir $untitled_directory
1078
1079
1080
#set pd_undoaction no
#set pd_redoaction no
#set pd_undocanvas no
Miller Puckette's avatar
Miller Puckette committed
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117

################ utility functions #########################

# enquote a string to send it to a tcl function
proc pdtk_enquote {x} {
    set foo [string map {"," "" ";" "" \" ""} $x]
    set foo2 [string map {" " "\\ "} $foo]
    concat $foo2
}

#enquote a string to send it to Pd.  Blow off semi and comma; alias spaces
#we also blow off "{", "}", "\" because they'll just cause bad trouble later.
proc pdtk_unspace {x} {
    set y [string map {" " "_" ";" "" "," "" "{" "" "}" "" "\\" ""} $x]
    if {$y == ""} {set y "empty"}
    concat $y
}

#enquote a string for preferences (command strings etc.)
proc pdtk_encodedialog {x} {
    concat +[string map {" " "+_" "$" "+d" ";" "+s" "," "+c" "+" "++"} $x]
}

proc pdtk_debug {x} {
    tk_messageBox -message $x -type ok
}

proc pdtk_watchdog {} {
    pd [concat pd watchdog \;]
    after 2000 {pdtk_watchdog}
}

proc pdtk_ping {} {
    pd [concat pd ping \;]
}

##### routine to ask user if OK and, if so, send a message on to Pd ######
Miller Puckette's avatar
Miller Puckette committed
1118
proc pdtk_check {canvas x message default} {
Miller Puckette's avatar
Miller Puckette committed
1119
    global pd_nt
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1120
	raise $canvas
Miller Puckette's avatar
Miller Puckette committed
1121
1122
1123
1124
1125
1126
    if {$pd_nt == 1} {
        set answer [tk_messageBox -message $x -type yesno -default $default \
            -icon question]
    } else {
        set answer [tk_messageBox -message $x -type yesno -default $default \
            -parent $canvas -icon question]
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1127
    }
1128
1129
1130
    if {! [string compare $answer yes]}  {
		pd $message
		if {$canvas eq "."} {
1131
			focus $canvas
1132
1133
1134
1135
		} else {
			menu_close $canvas
		}
	}
Miller Puckette's avatar
Miller Puckette committed
1136
1137
}

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1138
1139
1140
1141
1142
##### ask user Save? Discard? Cancel?, and if so, send a message on to Pd ######
proc pdtk_canvas_menuclose {window reply} {
	global pd_nt
	raise $window
	set filename [wm title $window]
1143
	set message [format {Do you wish to save the changes you made in "%s"?} $filename]
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1144
1145
1146
	set answer [tk_messageBox -message $message -type yesnocancel -default "yes" \
					-parent $window -icon question]
	switch -- $answer {
1147
		yes {
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1148
			pd [concat $window menusave \;]
1149
1150
1151
1152
1153
1154
			#when quitting the application sometimes the save menu fails to show up
			#after 250 pd $reply
			#pd $reply
		}
		no {
			pd [concat $window dirty 0 \;]
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1155
1156
1157
1158
1159
1160
			pd $reply
		}
		cancel {}
	}
}

1161
1162
1163
1164
proc pdtk_fixfilemenu {name} {
	::pd_menus::update_recentfiles_menu $name false
}

Miller Puckette's avatar
Miller Puckette committed
1165
1166
1167
set menu_windowlist {} 

proc pdtk_fixwindowmenu {} {
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
    global menu_windowlist pd_nt k12_mode
	if { $k12_mode == 0 } {
		.mbar.windows delete 0 end
		if {$pd_nt == 2} {
			.mbar.windows add command -label {Minimize} -command {menu_minimize .} \
				-accelerator [accel_munge "Ctrl+m"]
			.mbar.windows add command -label {Zoom} -command {menu_zoom .}
		} else {
			.mbar.windows add command -label "Next Window" -command {menu_raisenextwindow} \
				-accelerator "Ctrl+PageDown"
Ivica Bukvic's avatar
Ivica Bukvic committed
1178
1179
			.mbar.windows add command -label "Previous Window" -command {menu_raisepreviouswindow} \
				-accelerator "Ctrl+PageUp"
1180
		}
1181
		match_linux_wm [list .mbar.windows add separator]
1182
1183
1184
		.mbar.windows add command -label {parent window} -state disabled
		.mbar.windows add command -label {Pd & Console} -command menu_raise_console \
			-accelerator [accel_munge "Ctrl+;"] -state disabled
1185
		match_linux_wm [list .mbar.windows add separator]
1186
1187
1188
1189
1190
		foreach i $menu_windowlist {
		    .mbar.windows add command -label [lindex $i 0] \
		        -command [concat menu_domenuwindow [lindex $i 1]]
		    menu_fixwindowmenu [lindex $i 1]
		}
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1191
	}
Miller Puckette's avatar
Miller Puckette committed
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
}

####### Odd little function to make better Mac accelerators #####

proc accel_munge {acc} {
    global pd_nt

    if {$pd_nt == 2} {
        if [string is upper [string index $acc end]] {
            return [format "%s%s" "Shift+" \
                        [string toupper [string map {Ctrl Meta} $acc] end]]
        } else {
            return [string toupper [string map {Ctrl Meta} $acc] end]
        }
    } else {
        return $acc
    }
}



###############  the "New" menu command  ########################
proc menu_new {} {
    global untitled_number
    global untitled_directory
1217
	global k12_mode
1218
	global k12_saveas_on_new
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1219
1220
1221
    if { ! [file isdirectory $untitled_directory]} {set untitled_directory $::env(HOME)}
    pd [concat pd filename Untitled-$untitled_number \
			[pdtk_enquote $untitled_directory] \;]
1222
	if { $k12_mode == 1 } {
1223
		set k12_saveas_on_new 1
1224
1225
		pd {
		    #N canvas;
1226
			#X obj -30 -30 preset_hub k12 1 %hidden%;
1227
1228
1229
1230
1231
1232
1233
1234
		    #X pop 1;
		}
	} else {
		pd {
		    #N canvas;
		    #X pop 1;
		}
	}
Miller Puckette's avatar
Miller Puckette committed
1235
1236
1237
    set untitled_number [expr $untitled_number + 1]
}

1238
1239
1240
1241
1242
1243
############## the helper command for buggy popups #################
# this is an ugly workaround that should go away

proc reset_ctrl_on_popup_window {} {
	global menu_windowlist
	set first_patch_window_name [lindex $menu_windowlist 0 1]
1244
1245
1246
1247
	if { $first_patch_window_name != "" } {
		pdtk_canvas_sendkey $first_patch_window_name.c 0 Control_L null 0 1 -1
		pdtk_canvas_sendkey $first_patch_window_name.c 0 Control_R null 0 1 -1
	}
1248
1249
}

Miller Puckette's avatar
Miller Puckette committed
1250
1251
################## the "Open" menu command #########################

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1252
1253
1254
1255
1256
1257
proc menu_open {} {
    global pd_opendir filetypes
    if { ! [file isdirectory $pd_opendir]} {set pd_opendir $::env(HOME)}
    set files [tk_getOpenFile -defaultextension .pd \
					  -multiple true \
                      -filetypes $filetypes -initialdir $pd_opendir]
1258
    reset_ctrl_on_popup_window
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1259
1260
1261
1262
1263
    if {$files != ""} {
		foreach filename $files {
			open_file $filename
		}
	}
Miller Puckette's avatar
Miller Puckette committed
1264
1265
}

1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
proc menu_k12_open_demos {} {
	global pd_guidir filetypes
	if { ! [file isdirectory $pd_guidir/extra/K12/demos]} {
		set files [tk_getOpenFile -defaultextension .pd \
						  -multiple true \
		                  -filetypes $filetypes -initialdir $pd_guidir/extra/]
	} else {
		set files [tk_getOpenFile -defaultextension .pd \
						  -multiple true \
		                  -filetypes $filetypes -initialdir $pd_guidir/extra/K12/demos]
	}
    if {$files != ""} {
		foreach filename $files {
			open_file $filename
		}
	}	
}

1284
1285
1286
1287
1288
1289
1290
proc pdtk_set_current_dir {path_and_filename} {
	global pd_opendir untitled_directory
	set directory [file dirname $path_and_filename]
	set pd_opendir $directory
	set untitled_directory $directory
}

Miller Puckette's avatar
Miller Puckette committed
1291
proc open_file {filename} {
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1292
    global pd_opendir pd_guidir pd_nt
1293
	#puts stderr open_file
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
	set directory [file dirname $filename]