Top Level Namespace

Defined Under Namespace

Modules: DialogBindSystemSounds

Instance Method Summary collapse

Instance Method Details

#guidirectoryselect(title = 'DialogBind') ⇒ String

Shows system-native directory selection dialog.

Parameters:

  • title (String) (defaults to: 'DialogBind')

    an optional parameter specifying the title of the dialog box.

Returns:

  • (String)

    either an empty string (if the user cancels the dialog) or the native path to the file.



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/dialogbind.rb', line 544

def guidirectoryselect(title='DialogBind')
	if $dialogbind_dialog_backend == 'macos' then
		return macopen(title, [], true)
	elsif $dialogbind_dialog_backend == 'kdialog' then
		if kdialog({ 'title' => title, 'getexistingdirectory' => [] }, true) == false then
			return ''
		end
		return File.read('/tmp/kdialog.sock').gsub("\n", "")
	elsif $dialogbind_dialog_backend == 'zenity' then
		zenity({ 'title' => title, 'file-selection' => nil, 'directory' => nil, ' > /tmp/zenity.sock 2>/dev/null' => nil })
		return File.read('/tmp/zenity.sock').gsub("\n", "")
	elsif $dialogbind_dialog_backend == 'win32' then
		return win32_vbbrowseforfolder(title).gsub("\\", '/')
	else
		raise 'The selected backend does not support directory selection dialog boxes.'
	end
	return ''
end

#guierror(text, title = 'DialogBind') ⇒ Boolean

Shows an error message box with only single OK button.

Parameters:

  • text (String)

    the text that should be displayed in a message box

  • title (String) (defaults to: 'DialogBind')

    an optional parameter specifying the title of the message box. Ignored on macOS.

Returns:

  • (Boolean)

    true on success, false if something went wrong



296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/dialogbind.rb', line 296

def guierror(text, title='DialogBind')
	if $dialogbind_dialog_backend == 'zenity' then
		return zenity('error' => nil, 'title' => title, 'text' => text)
	elsif $dialogbind_dialog_backend == 'macos' then
		return macdialog(text, [ 'OK' ], 'dialog', true)
	elsif $dialogbind_dialog_backend == 'kdialog' then
		return kdialog({ 'title' => title, 'error' => text })
	elsif $dialogbind_dialog_backend == 'win32' then
		return win32_msgbox(text, title, 16)
	else
		raise 'The selected backend does not support question message boxes.'
	end
	return false
end

#guifileselect(filter = [], title = 'DialogBind') ⇒ String

Shows system-native file selection dialog. Currently does not work on Windows.

Parameters:

  • filter (Array) (defaults to: [])

    an array of file patterns. Example: [ ‘*.rb’, ‘markdown-doc-toprocess*.md’ ]

  • title (String) (defaults to: 'DialogBind')

    an optional parameter specifying the title of the dialog box.

Returns:

  • (String)

    either an empty string (if the user cancels the dialog) or the native path to the file.



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/dialogbind.rb', line 520

def guifileselect(filter=[], title='DialogBind')
	if $dialogbind_dialog_backend == 'macos' then
		return macopen(title, filter, false)
	elsif $dialogbind_dialog_backend == 'kdialog' then
		if kdialog({ 'title' => title, 'getopenfilename' => [] }, true) == false then
			return ''
		end
		return File.read('/tmp/kdialog.sock').gsub("\n", "")
	elsif $dialogbind_dialog_backend == 'zenity' then
		zenity({ 'title' => title, 'file-selection' => nil, '%' => zenityfilter(filter), ' > /tmp/zenity.sock 2>/dev/null' => nil })
		return File.read('/tmp/zenity.sock').gsub("\n", "")
	elsif $dialogbind_dialog_backend == 'win32' then
		return win32_activexopen(title)
	else
		raise 'The selected backend does not support file selection dialog boxes.'
		return ''
	end
	return ''
end

#guigets(text = 'Type something:', title = 'DialogBind') ⇒ String

Shows an input box with the specified text.

Parameters:

  • text (String) (defaults to: 'Type something:')

    the text that should be displayed in an input box

  • title (String) (defaults to: 'DialogBind')

    an optional parameter specifying the title of the input box. Ignored on macOS and Windows.

Returns:

  • (String)

    the string that the user has typed in



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/dialogbind.rb', line 568

def guigets(text='Type something:', title='DialogBind')
	if $dialogbind_dialog_backend == 'macos' then
		return macentry(text)
	elsif $dialogbind_dialog_backend == 'kdialog' then
		kdialog({ 'title' => title, 'inputbox' => [ text, '' ] }, true)
		return File.read('/tmp/kdialog.sock').gsub("\n", "")
	elsif $dialogbind_dialog_backend == 'zenity' then
		zenity({ 'title' => title, 'entry' => nil, 'text' => text, ' > /tmp/zenity.sock 2>/dev/null' => nil })
		return File.read('/tmp/zenity.sock').gsub("\n", "")
	elsif $dialogbind_dialog_backend == 'win32' then
		return win32_vbinputbox(text)
	else
		raise 'The selected backend does not support input boxes.'
		return ''
	end
	return ''
end

#guilicense(file, title = 'DialogBind') ⇒ Boolean

Shows a message box containing the license agreement that is stored in the specified file.

Parameters:

  • file (String)

    the file that contains the licensing terms

  • title (String) (defaults to: 'DialogBind')

    an optional parameter specifying the title of the message box. Ignored on macOS.

Returns:

  • (Boolean)

    true if the user accepts the terms of the license agreement or false if not



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
# File 'lib/dialogbind.rb', line 316

def guilicense(file, title='DialogBind')
	if File.exists?(file) == false then
		guierror('File "' + file + '" does not exist.', title)
		return false
	end
	if $dialogbind_dialog_backend == 'zenity' then
		return zenity({ 'text-info' => nil, 'title' => title, 'filename' => file, 'checkbox' => 'I have read and accepted the terms of the license agreement.' })
	elsif $dialogbind_dialog_backend == 'macos' then
		macdialog('Right now, the license agreement will be shown in TextEdit. Close TextEdit using Command-Q to continue,', ['OK'])
		system('open -e "' + file.gsub('"', "\\\"") + '"')
		return guiyesno('Do you accept the terms of the license agreement?', title)
	elsif $dialogbind_dialog_backend == 'win32' then
		retv_msgbox = win32_msgbox("Do you accept the terms of the license agreement below?\n\n" + File.read(file), title, 36)
		return (retv_msgbox == 6)
	elsif $dialogbind_dialog_backend == 'kdialog' then
		kdialog({ 'textbox' => file, 'title' => title })
		if kdialog({ 'yesno' => 'Do you accept the terms of the license agreement?', 'title' => title }) then
			return true
		end
		return false
	else
		raise 'The selected backend does not support license message boxes.'
		return false
	end
	return false
end

#guinotify(text, title = 'DialogBind', sound = DialogBindSystemSounds::Success) ⇒ Boolean

Shows a notification in system tray or a message box, depending on operating system notifications support.

Parameters:

  • text (String)

    the text that should be displayed in a notification.

  • title (String) (defaults to: 'DialogBind')

    an optional parameter specifying the title of the notification.

Returns:

  • (Boolean)

    true on success, false on fail.



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/dialogbind.rb', line 591

def guinotify(text, title='DialogBind', sound=DialogBindSystemSounds::Success)
	if $dialogbind_dialog_backend != 'win32' && $dialogbind_dialog_backend != 'kdialog' then
		guisound(sound)
	end
	if $dialogbind_dialog_backend == 'macos' then
		return macdialog(text, [], 'notification', false, false, title)
	elsif $dialogbind_dialog_backend == 'kdialog' then
		return kdialog({ 'title' => title, 'passivepopup' => [ text, 10 ] })
	elsif $dialogbind_dialog_backend == 'zenity' then
		return zenity({ 'title' => title, 'notification' => nil, 'text' => text })
	else
		return guiputs(text, title)
	end
	return false
end

#guiputs(text, title = 'DialogBind') ⇒ Boolean

Shows a simple message box (or information message box when using Zenity backend).

Parameters:

  • text (String)

    the text that should be displayed in a message box

  • title (String) (defaults to: 'DialogBind')

    an optional parameter specifying the title of the message box. Ignored on macOS.

Returns:

  • (Boolean)

    true on success, false if something went wrong



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/dialogbind.rb', line 246

def guiputs(text, title='DialogBind')
	if $dialogbind_dialog_backend == 'zenity' then
		return zenity({ 'info' => nil, 'title' => title, 'text' => text })
	elsif $dialogbind_dialog_backend == 'macos' then
		return macdialog(text)
	elsif $dialogbind_dialog_backend == 'kdialog' then
		return kdialog({ 'title' => title, 'msgbox' => text })
	elsif $dialogbind_dialog_backend == 'win32' then
		win32_msgbox(text, title, 0)
		return true
	else
		puts title + ': ' + text
		return true
	end
	return false
end

#guiselect(entries, text = 'Choose one of the items below:', title = 'DialogBind') ⇒ String

Shows either a message box with buttons matching the items specified in the array “entries“ or a list message box.

Parameters:

  • entries (Array)

    an array of strings that should be displayed as list in a message box.

  • text (String) (defaults to: 'Choose one of the items below:')

    the text that should be displayed in a message box

  • title (String) (defaults to: 'DialogBind')

    an optional parameter specifying the title of the message box. Ignored on macOS.

Returns:

  • (String)

    the selected string or nil on cancel



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
# File 'lib/dialogbind.rb', line 360

def guiselect(entries, text='Choose one of the items below:', title='DialogBind')
	if $dialogbind_dialog_backend == 'zenity' then
		array_of_items = []
		item_count_zenity = 0
		entries.each do |item|
			array_of_items.push(item_count_zenity)
			array_of_items.push(item)
			item_count_zenity += 1
		end
		if zenity({'list' => nil, 'radiolist' => nil, 'text' => text, 'print-column' => 'ALL', 'column' => '#', 'column%' => 'Items', '' => array_of_items, ' > /tmp/zenity.sock 2>/dev/null' => nil }) then
			return File.read('/tmp/zenity.sock').gsub("\n", "")
		else
			return nil
		end
	elsif $dialogbind_dialog_backend == 'kdialog' then
		list_args = [ text ]
		item_count = 0
		entries.each do |list_item|
			list_args.push(item_count)
			list_args.push(list_item)
			list_args.push('-')
			item_count += 1
		end
		if kdialog({ 'title' => title, 'radiolist' => list_args,}, true) == false then
			return nil
		end
		item_index = File.read('/tmp/kdialog.sock').gsub("\n", "").to_i
		if item_index > entries.length then
			return ''
		end
		return entries[item_index].clone
	elsif $dialogbind_dialog_backend == 'macos' then
		if entries.include? 'false' then
			raise 'The list of items to present to the user cannot contain the words "true" or "false" without additional punctuation due to limitations of AppleScript that is called from Ruby on macOS to display dialogs.'
		end
		return macselect(entries, text)
	elsif $dialogbind_dialog_backend == 'win32' then
		combined_msg = text.clone
		count = 0
		entries.each do |entry_item|
			combined_msg += "\r\n" + count.to_s + '. ' + entry_item.to_s
			count += 1
		end
		combined_msg += "\r\n" + " (To select one of the items above, enter the matching number before the dot)"
		entered_id = win32_vbinputbox(combined_msg).to_i
		if entered_id > entries.length then
			return ''
		end
		return entries[entered_id].clone
	else
		raise 'The selected backend does not support license message boxes.'
		return false
	end
	return nil
end

#guisound(sound_v) ⇒ Object

Plays the default system sounds.

DialogBindSystemSounds::Error and DialogBindSystemSounds::Attention. Specifying DialogBindSystemSounds::None will do nothing.

Parameters:

  • sound_v (DialogBindSystemSounds)

    the sound to play. Available values are DialogBindSystemSounds::Success,

Returns:

  • (Object)

    nothing



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
# File 'lib/dialogbind.rb', line 472

def guisound(sound_v)
	if sound_v == DialogBindSystemSounds::None then
		return
	end
	if $dialogbind_dialog_backend != 'macos' && $dialogbind_dialog_backend != 'win32' then
		linuxsound(sound_v)
		return
	end
	constant_sound_success = '/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/burn complete.aif'
	constant_sound_error = '/System/Library/Sounds/Funk.aiff'
	constant_sound_attention = '/System/Library/PrivateFrameworks/FindMyDevic.framework/Versions/A/Resources/fmd_sound.aiff'
	if File.exists?(constant_sound_attention) == false then
		constant_sound_attention = constant_sound_success
	end
	if File.exists?(constant_sound_success) == false then
		constant_sound_success = constant_sound_error
	end
	if $dialogbind_dialog_backend == 'win32' then
		constant_sound_success = 'c:/Windows/Media/tada.wav'
		constant_sound_error = 'c:/Windows/Media/Windows Error.wav'
		if File.exists?(constant_sound_error) == false then
			constant_sound_error = 'c:/Windows/Media/chord.wav'
		end
		constant_sound_attention = 'c:/Windows/Media/chimes.wav'
	end
	if sound_v == DialogBindSystemSounds::Success then
		nativesoundplay(constant_sound_success)
	elsif sound_v == DialogBindSystemSounds::Error then
		nativesoundplay(constant_sound_error)
	else
		nativesoundplay(constant_sound_attention)
	end
end

#guiyesno(text, title = 'DialogBind') ⇒ Boolean

Shows a question message box with “Yes” and “No” buttons.

Parameters:

  • text (String)

    the text that should be displayed in a message box

  • title (String) (defaults to: 'DialogBind')

    an optional parameter specifying the title of the message box. Ignored on macOS.

Returns:

  • (Boolean)

    true if the user presses yes, false if the user pressed no



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/dialogbind.rb', line 268

def guiyesno(text, title='DialogBind')
	if $dialogbind_dialog_backend == 'zenity' then
		return zenity('question' => nil, 'title' => title, 'text' => text)
	elsif $dialogbind_dialog_backend == 'macos' then
		macdialog(text, [ 'Yes', 'No' ], 'dialog', false, true)
		output = `#{$dialogbind_macos_script_cmd}`.gsub("\n", "")
		if output == nil || output.include?(':') == false then
			return false
		end
		if output.split(':')[1].downcase == 'yes' then
			return true
		end
	elsif $dialogbind_dialog_backend == 'kdialog' then
		return kdialog({ 'title' => title, 'yesno' => text })
	elsif $dialogbind_dialog_backend == 'win32' then
		retv_msgbox = win32_msgbox(text, title, 36)
		return (retv_msgbox == 6)
	else
		raise 'The selected backend does not support question message boxes.'
	end
	return false
end