zerenity 1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/CHANGELOG +21 -0
  2. data/README +63 -0
  3. data/examples/example_calendar.rb +6 -0
  4. data/examples/example_colorselection.rb +5 -0
  5. data/examples/example_entry.rb +16 -0
  6. data/examples/example_error.rb +3 -0
  7. data/examples/example_fileselection.rb +26 -0
  8. data/examples/example_info.rb +3 -0
  9. data/examples/example_list.rb +10 -0
  10. data/examples/example_progress.rb +40 -0
  11. data/examples/example_question.rb +4 -0
  12. data/examples/example_textinfo.rb +11 -0
  13. data/examples/example_warning.rb +3 -0
  14. data/lib/zerenity.rb +14 -0
  15. data/lib/zerenity/base.rb +50 -0
  16. data/lib/zerenity/calendar.rb +26 -0
  17. data/lib/zerenity/colorselection.rb +30 -0
  18. data/lib/zerenity/entry.rb +36 -0
  19. data/lib/zerenity/error.rb +17 -0
  20. data/lib/zerenity/fileselection.rb +68 -0
  21. data/lib/zerenity/info.rb +18 -0
  22. data/lib/zerenity/list.rb +110 -0
  23. data/lib/zerenity/messagedialog.rb +34 -0
  24. data/lib/zerenity/progress.rb +136 -0
  25. data/lib/zerenity/progress.rb~ +136 -0
  26. data/lib/zerenity/question.rb +26 -0
  27. data/lib/zerenity/scale.rb +41 -0
  28. data/lib/zerenity/textinfo.rb +49 -0
  29. data/lib/zerenity/warning.rb +18 -0
  30. data/test/all.rb +2 -0
  31. data/test/tc_base.rb +35 -0
  32. data/test/tc_calendar.rb +28 -0
  33. data/test/tc_entry.rb +49 -0
  34. data/test/tc_fileselection.rb +37 -0
  35. data/test/tc_info.rb +16 -0
  36. data/test/tc_list.rb +138 -0
  37. data/test/tc_messagedialog.rb +16 -0
  38. data/test/tc_progress.rb +76 -0
  39. data/test/tc_question.rb +23 -0
  40. data/test/tc_scale.rb +33 -0
  41. data/test/tc_textinfo.rb +33 -0
  42. data/test/tc_warning.rb +16 -0
  43. data/test/ts_zerenity.rb +33 -0
  44. metadata +97 -0
@@ -0,0 +1,21 @@
1
+ = Zerenity Changelog
2
+ == 1.3
3
+ * Added a color selection dialog
4
+ == 1.2
5
+ * Added the scale dialog, allowing the user to use a slider to choose a numerical value.
6
+ == 1.1
7
+ * From this version the OK button is set as the default widget. Pressing Enter
8
+ in a dialog will cause it to be fired. If you do not want your dialog to have
9
+ this behavior set the :activatesDefault option to false. Note that certain widgets
10
+ (List, FileSelection and TextInfo when :editable is set) do not allow this as Enter
11
+ could be used for other actions. Originally suggested by Justin Collins.
12
+ * Instead of passing a Gtk::VBox to the build and retrieve_selection methods, now
13
+ pass in the parent Gtk::Dialog.
14
+ == 1.0
15
+ * Added significant unit testing and code coverage testing.
16
+ == 0.9
17
+ * Added a :password option to the Entry dialog. When set to true it will
18
+ treat the text entry like a password entry and replaces the characters
19
+ entered with a '*'.
20
+ == 0.8
21
+ * Initial release.
data/README ADDED
@@ -0,0 +1,63 @@
1
+ = Zerenity
2
+
3
+ == Version
4
+ 1.3
5
+
6
+ == Author
7
+ Farrel Lifson http://www.lifson.info http://www.aimred.com
8
+
9
+ == Contributors
10
+ Daniel Lucraft http://www.daniellucraft.com
11
+
12
+ == Synopsis
13
+ Zerenity is an almost clone of Zenity(http://freshmeat.net/projects/zenity) for Ruby. It allows for the easy creation of simple graphical dialogs from a Ruby script.
14
+
15
+ == Features
16
+ Zerenity allows for the creation of the following dialogs
17
+ * Date selection
18
+ * Text entry
19
+ * Error display
20
+ * File/directory selection
21
+ * Information display
22
+ * List display and selection
23
+ * Progress display
24
+ * Question display
25
+ * Text information display and editing
26
+ * Warning display
27
+ * Sliding scales
28
+ * Color selection
29
+
30
+ == Dependencies
31
+ Zerenity requires GTK2(http://www.gtk.org) and Ruby/GTK2(http://ruby-gnome2.sourceforge.jp/hiki.cgi).
32
+
33
+ == Installation
34
+ Zerenity is avaiable as a Ruby gem. Links to the latest release can be found at http://www.aimred.com/files/projects/zerenity.
35
+
36
+ The code is also available via it's Github repository at http://github.com/farrel/zerenity/tree/master
37
+
38
+ == Example Usage
39
+ === User Input and Display
40
+ require 'zerenity'
41
+
42
+ if (name = Zerenity::Entry(:text=>"What is your name?"))
43
+ Zerenity::Info(:text=>"Hello #{name}")
44
+ else
45
+ puts "No name entered"
46
+ end
47
+
48
+ === Progress
49
+ require 'zerenity'
50
+
51
+ fileList = filesToProcess(folder)
52
+ Zerenity::Progress(:text=>'Processing files',:autoClose=>true) do |progress|
53
+ fileList.each_index do |file,index|
54
+ processFile(file)
55
+ progress.update(index/fileList.length,"#{100*index/fileList.length}% processed...")
56
+ end
57
+ end
58
+
59
+ == License
60
+ Zerenity is released under the BSD license.
61
+
62
+ == Copyright
63
+ (c) 2006 - 2009 Farrel Lifson
@@ -0,0 +1,6 @@
1
+ require('zerenity/calendar')
2
+
3
+ date = Zerenity::Calendar(:text=>"What day is your birthday?",:title=>"Please select a date")
4
+
5
+ puts "You selected #{date.to_s}" if date
6
+ puts "You didn't select a date" unless date
@@ -0,0 +1,5 @@
1
+ require( 'zerenity/colorselection' )
2
+
3
+ color = Zerenity::ColorSelection( :title => "Choose a color" )
4
+
5
+ puts "You chose #{ color || 'nothing' }"
@@ -0,0 +1,16 @@
1
+ require('zerenity/entry')
2
+
3
+ name = Zerenity::Entry(:text=>"Please enter your name", :ok_button => "Go!", :cancel_button => "Forget it")
4
+ puts "Hello #{name}" if name
5
+ puts "You did not enter your name" unless name
6
+
7
+ text = Zerenity::Entry(:text=>"Please enter your password",:password=>true)
8
+ puts "Your password is: #{text}" if text
9
+ puts "You did not enter your password" unless text
10
+
11
+ if (text = Zerenity::Entry(:text=>"Please enter your name. Click OK to close dialog.",:activatesDefault=>false, :string => name))
12
+ puts "Hello #{text}"
13
+ else
14
+ puts "You did not enter your name"
15
+ end
16
+
@@ -0,0 +1,3 @@
1
+ require 'zerenity/error'
2
+
3
+ Zerenity::Error(:text=>"An error has occured.",:title=>"Oops!")
@@ -0,0 +1,26 @@
1
+ require('zerenity/fileselection')
2
+
3
+ fileName = Zerenity::FileSelection(:title=>"Which file do you want?",:filename=>File.expand_path(__FILE__),:action=>:open)
4
+
5
+ puts "You chose #{fileName}" if fileName
6
+ puts "You did not select a file" unless fileName
7
+
8
+ fileNames = Zerenity::FileSelection(:title=>"Please choose the required files",:multiple=>true)
9
+
10
+ if fileNames
11
+ puts "You chose the following files"
12
+ fileNames.each do |fileName|
13
+ puts "\t#{fileName}"
14
+ end
15
+ else
16
+ puts "You did not choose any files"
17
+ end
18
+
19
+ filename = Zerenity::FileSelection(:title=>"Save file as",:action=>:save)
20
+ filename ? puts("File saved as #{filename}") : puts("No file saved")
21
+
22
+ folder = Zerenity::FileSelection(:title=>"Select a folder",:action=>:select_folder,:multiple=>true)
23
+ folder ? puts("You selected folder #{folder}") : puts("You did not choose a folder")
24
+
25
+ folder = Zerenity::FileSelection(:title=>"Create folder",:action=>:create_folder)
26
+ folder ? puts("Created folder #{folder}") : puts("You did not create a folder")
@@ -0,0 +1,3 @@
1
+ require("zerenity/info")
2
+
3
+ Zerenity::Info(:text=>"Hello, world",:title=>"Hello!")
@@ -0,0 +1,10 @@
1
+ require('zerenity/list')
2
+
3
+ choice = Zerenity::List(:columns=>["Food","Energy"],:data=>[["Chips","200KJ"],["Chocolate","300KJ"]])
4
+ choice ? puts("You chose #{choice[0]} which has #{choice[1]} of energy.") : puts("You didn't choose an item.")
5
+
6
+ choice = Zerenity::List(:columns=>["Selected","Food","Energy"],:data=>[[true,"Chips","200KJ"],[false,"Chocolate","300KJ"],[false,"Pretzels","250KJ"]],:radiolist=>true)
7
+ choice ? puts("You chose #{choice[0]} which has #{choice[1]} of energy.") : puts("You didn't choose an item.")
8
+
9
+ choices = Zerenity::List(:columns=>["Selected","Food","Energy"],:data=>[[true,"Chips","200KJ"],[false,"Chocolate","300KJ"],[false,"Pretzels","250KJ"]],:checklist=>true,:title=>"Please select snacks")
10
+ choices ? choices.each {|row| puts("You chose #{row[0]} which has #{row[1]} of energy.")} : puts("You did not choose any items")
@@ -0,0 +1,40 @@
1
+ require('zerenity/progress')
2
+
3
+ Zerenity::Progress(:title=>"Normal",:text=>"Building index") do |progress|
4
+ 0.step(1,0.01) do |number|
5
+ progress.update(number,"#{(number*100).to_i}%")
6
+ sleep(0.02)
7
+ end
8
+ end
9
+
10
+ Zerenity::Progress(:title=>"Normal pulse",:text=>"Querying Database") do |progress|
11
+ 0.step(1,0.01) do
12
+ progress.pulse
13
+ sleep(0.02)
14
+ end
15
+ progress.update(1,"Finished")
16
+ end
17
+
18
+ cancelled = Zerenity::Progress(:cancellable=>true,:text=>"Press Cancel To End Rendering",:title=>"Cancellabe") do |progress|
19
+ while !progress.cancelled?
20
+ progress.pulse("Processing")
21
+ sleep(0.02)
22
+ end
23
+ 0.step(1,0.02) do |number|
24
+ progress.update(1.0-number,"Cleaning up")
25
+ sleep(0.01)
26
+ end
27
+ progress.update(0,"Finished")
28
+ end
29
+
30
+ if cancelled
31
+ puts "Updating has been cancelled."
32
+ end
33
+
34
+ Zerenity::Progress(:autoClose=>true,:text=>"Copying kernel...",:title=>"Auto close") do |progress|
35
+ 0.step(1,0.02) do |number|
36
+ progress.update(number)
37
+ sleep(0.03)
38
+ end
39
+ end
40
+
@@ -0,0 +1,4 @@
1
+ require('zerenity/question')
2
+
3
+ choice = Zerenity::Question(:text=>"Process images?")
4
+ choice ? (puts "OK") : (puts "CANCEL")
@@ -0,0 +1,11 @@
1
+ require('zerenity/textinfo')
2
+
3
+ Zerenity::TextInfo(:title=>"Source file: #{$0}",:text=>File.new($0).read)
4
+
5
+ text = Zerenity::TextInfo(:title=>"Editable Text",:editable=>true)
6
+ if text
7
+ puts "You entered:"
8
+ puts text
9
+ else
10
+ puts "You clicked cancel"
11
+ end
@@ -0,0 +1,3 @@
1
+ require 'zerenity/warning'
2
+
3
+ Zerenity::Warning(:text=>"An error could occur",:title=>"Danger!")
@@ -0,0 +1,14 @@
1
+ require('zerenity/base')
2
+ require('zerenity/calendar')
3
+ require('zerenity/entry')
4
+ require('zerenity/error')
5
+ require('zerenity/fileselection')
6
+ require('zerenity/info')
7
+ require('zerenity/list')
8
+ require('zerenity/messagedialog')
9
+ require('zerenity/progress')
10
+ require('zerenity/question')
11
+ require('zerenity/scale')
12
+ require('zerenity/textinfo')
13
+ require('zerenity/warning')
14
+ require('zerenity/colorselection')
@@ -0,0 +1,50 @@
1
+ require 'gtk2'
2
+
3
+ # Zerenity provides a number of simple graphical dialogs.
4
+ #
5
+ # ==== Global Options
6
+ # [:title] The text displayed in the title bar.
7
+ # [:text] The text that will be displayed in the dialog (if needed).
8
+ # [:activatesDefault] If set to false disables the firing of the OK
9
+ # button when the Enter key is pressed.
10
+ module Zerenity
11
+ CLICKED = "clicked"
12
+ class Base # :nodoc:
13
+ def self.check(options)
14
+ options[:activatesDefault] = (options[:activatesDefault].nil? || options[:activatesDefault])
15
+ options[:title] ||= ""
16
+ options[:text] ||= ""
17
+ end
18
+
19
+ def self.build(dialog, options)
20
+ options[:ok_button] = dialog.add_button(options[:ok_button]||Gtk::Stock::OK,
21
+ Gtk::Dialog::RESPONSE_OK)
22
+ options[:cancel_button] = dialog.add_button(options[:cancel_button]||Gtk::Stock::CANCEL,
23
+ Gtk::Dialog::RESPONSE_CANCEL)
24
+ dialog.set_default_response(Gtk::Dialog::RESPONSE_OK) if options[:activatesDefault]
25
+ end
26
+
27
+ def self.run(options)
28
+ Gtk.init
29
+ self.check(options)
30
+ dialog = Gtk::Dialog.new(options[:title])
31
+ self.build(dialog,options)
32
+ result = nil
33
+ options[:ok_button].signal_connect(CLICKED) do
34
+ result = self.retrieve_selection(dialog,options)
35
+ dialog.destroy
36
+ Gtk.main_quit
37
+ end
38
+ options[:cancel_button].signal_connect(CLICKED) do
39
+ dialog.destroy
40
+ Gtk.main_quit
41
+ end
42
+ dialog.show_all
43
+ Gtk.main
44
+ return result
45
+ end
46
+
47
+ def self.retrieve_selection(dialog,options)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,26 @@
1
+ require 'zerenity/base'
2
+
3
+ module Zerenity
4
+ # Creates a calendar dialog allowing the user to select a date. Returns
5
+ # a Time object representing the selected date or nil if Cancel is
6
+ # clicked.
7
+ #
8
+ # ==== Example Usage
9
+ # date = Zerenity::Calendar(:text=>"Please select a date")
10
+ def self.Calendar(options={})
11
+ Calendar.run(options)
12
+ end
13
+
14
+ class Calendar < Zerenity::Base # :nodoc:
15
+ def self.build(dialog,options)
16
+ super(dialog,options)
17
+ dialog.vbox.add(Gtk::Label.new(options[:text],false))
18
+ dialog.vbox.add(Gtk::Calendar.new)
19
+ end
20
+
21
+ def self.retrieve_selection(dialog,options)
22
+ super(dialog,options)
23
+ Time.local(*dialog.vbox.children[1].date)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,30 @@
1
+ require 'zerenity/base'
2
+
3
+ module Zerenity
4
+ # Displays a color selection dialog. Returns the value of the color selected
5
+ # as #rrrrggggbbbb, where r, g and b are hex digits representing the red,
6
+ # green and blue components respectively
7
+ #
8
+ # ====Example Usage:
9
+ # Zereity::ColorSelection( :title => "Select a color" )
10
+ def self.ColorSelection( options = {} )
11
+ ColorSelection.run( options )
12
+ end
13
+
14
+ class ColorSelection < Base # :nodoc:
15
+ def self.run( options )
16
+ self.check( options )
17
+ Gtk.init
18
+ color_selection = Gtk::ColorSelectionDialog.new( options[ :title ])
19
+ result = nil
20
+ color_selection.run do |response|
21
+ if ( response == Gtk::Dialog::RESPONSE_OK )
22
+ # I don't have the latest Ruby/GNOME2 on my system so this mimics current_color.to_s
23
+ result = "##{ color_selection.colorsel.current_color.to_a.map{|channel| channel.to_s(16).rjust(4,'0').upcase }.join }"
24
+ end
25
+ end
26
+ color_selection.destroy
27
+ return result
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,36 @@
1
+ require('zerenity/base')
2
+
3
+ module Zerenity
4
+ # Displays a text entry box. Returns the text entered or nil if
5
+ # Cancel is pressed.
6
+ #
7
+ # ====Options
8
+ # [:password] When set to true all all characters in the text
9
+ # entry area will be masked by the '*' character.
10
+ # ====Example Usage
11
+ # name = Zerenity::Entry(:text=>"Please enter your name.")
12
+ def self.Entry(options={})
13
+ Entry.run(options)
14
+ end
15
+
16
+ class Entry < Base # :nodoc:
17
+ def self.check(options)
18
+ super(options)
19
+ end
20
+
21
+ def self.build(dialog,options)
22
+ super(dialog,options)
23
+ dialog.vbox.add(Gtk::Label.new(options[:text],false))
24
+ entry = Gtk::Entry.new
25
+ entry.text = options[:string] if options[:string]
26
+ entry.set_visibility(!options[:password])
27
+ entry.set_activates_default(options[:activatesDefault])
28
+ dialog.vbox.add(entry)
29
+ end
30
+
31
+ def self.retrieve_selection(dialog,options)
32
+ super(dialog,options)
33
+ dialog.vbox.children[1].text
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ require 'zerenity/messagedialog'
2
+
3
+ module Zerenity
4
+ # Displays an error dialog.
5
+ # ====Example Usage
6
+ # Zerenity::Error(:text=>"An error has occured. Please check the system log.")
7
+ def self.Error(options={})
8
+ Error.run(options)
9
+ end
10
+
11
+ class Error < MessageDialog # :nodoc:
12
+ def self.check(options)
13
+ super(options)
14
+ options[:type] = Gtk::MessageDialog::ERROR
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,68 @@
1
+ require 'zerenity/base'
2
+
3
+ module Zerenity
4
+ # Displays a file/directory selection dialog. Returns the name(s)
5
+ # of the files/directories chosen or nil if Cancel is pressed.
6
+ #
7
+ # ====Options
8
+ # [:filename] The file to selected initially
9
+ # [:multiple] If set to true multiple files/directories can be selected
10
+ # and an array of file/directory names will be returned.
11
+ # [:action] The default behavour is to select a file but this can be
12
+ # overridden by setting the action key to one of the following values:
13
+ # [:save] Save a file
14
+ # [:select_folder] Select a folder
15
+ # [:create_folder] Create a folder - note this will actually create
16
+ # the directory.
17
+ #
18
+ # ====Example Usage:
19
+ # Zerenity::FileSelection(:title=>"Select a directory", :action=>:select_folder)
20
+ def self.FileSelection(options={})
21
+ FileSelection.run(options)
22
+ end
23
+
24
+ class FileSelection < Base # :nodoc:
25
+ def self.check(options)
26
+ super(options)
27
+ options[:action] = (case options[:action]
28
+ when :save:
29
+ Gtk::FileChooser::ACTION_SAVE
30
+ when :select_folder:
31
+ Gtk::FileChooser::ACTION_SELECT_FOLDER
32
+ when :create_folder:
33
+ Gtk::FileChooser::ACTION_CREATE_FOLDER
34
+ else
35
+ Gtk::FileChooser::ACTION_OPEN
36
+ end)
37
+ options[:title] ||= ""
38
+ options[:filename] ||= nil
39
+ options[:multiple] ||= false
40
+ end
41
+
42
+ def self.run(options)
43
+ self.check(options)
44
+ Gtk.init
45
+ fileSelection = Gtk::FileChooserDialog.new(
46
+ options[:title],
47
+ nil,
48
+ options[:action],
49
+ nil,
50
+ [Gtk::Stock::CANCEL,Gtk::Dialog::RESPONSE_CANCEL],
51
+ [Gtk::Stock::OK,Gtk::Dialog::RESPONSE_OK])
52
+ fileSelection.set_filename(options[:filename]) if options[:filename]
53
+ fileSelection.set_select_multiple(true) if options[:multiple]
54
+ result = nil
55
+ fileSelection.run do |response|
56
+ if (response == Gtk::Dialog::RESPONSE_OK)
57
+ if options[:multiple]
58
+ result = fileSelection.filenames
59
+ else
60
+ result = fileSelection.filename
61
+ end
62
+ end
63
+ end
64
+ fileSelection.destroy
65
+ return result
66
+ end
67
+ end
68
+ end