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,18 @@
1
+ require 'zerenity/messagedialog'
2
+
3
+ module Zerenity
4
+ # Displays an informational popup dialog on the screen.
5
+ #
6
+ # ====Examle Usage
7
+ # Zerenity::Info(:text=>"Processing has completed.")
8
+ def Zerenity::Info(options={})
9
+ Info.run(options)
10
+ end
11
+
12
+ class Info < MessageDialog # :nodoc:
13
+ def self.check(options)
14
+ super(options)
15
+ options[:type] = Gtk::MessageDialog::INFO
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,110 @@
1
+ require('zerenity/base')
2
+
3
+ module Zerenity
4
+ # Displays a list dialog on the screen. Items in the list
5
+ # can be selected. Returns the rows which were selected
6
+ # or nil if Cancel is clicked.
7
+ #
8
+ # ====Options
9
+ # [:columns] The names which will be displayed at the top og
10
+ # each column. This option is mandatory.
11
+ # [:data] The data for the list, with each row
12
+ # being another array.
13
+ # [:radiolist] If this is set true the first column of the
14
+ # list will be rendered as a radiobutton. The first element
15
+ # of every data array should either be a true or false.
16
+ # [:checklist] If this is set true the first column of the
17
+ # list will be rendered as a checkbox. The first element
18
+ # of every data array should either be a true or false.
19
+ # When this is set mutiple rows can be selected and an
20
+ # array of selected rows are returned.
21
+ # *Note* :radiolist and :checklist are mutually exclusive. If
22
+ # both are set true an exception is thrown. An exception
23
+ # is also thrown if the data rows are not equal length or
24
+ # they differ in length with the number of columns.
25
+ #
26
+ # ====Example Usage
27
+ # choice = Zerenity::List(:columns=>["Food","Energy"],:data=>[["Chips","200KJ"],["Chocolate","300KJ"]])
28
+ def self.List(options={})
29
+ List.run(options)
30
+ end
31
+
32
+ class List < Base # :nodoc:
33
+ def self.check(options)
34
+ super(options)
35
+ raise(ArgumentError.new("No column data was given")) unless options[:data]
36
+ raise(ArgumentError.new("No column headers were given")) unless options[:columns]
37
+ raise(ArgumentError.new("Column headers and column data differ in length")) unless options[:columns].length.eql?(options[:data][0].length)
38
+ raise(ArgumentError.new("Column data lengths are not equal")) unless options[:data].map{|element|element.length}.uniq.length.eql?(1)
39
+ raise(ArgumentError.new("Radiolist and checklist options can not both be set")) if (options[:radiolist] && options[:checklist])
40
+ end
41
+
42
+ def self.build(dialog,options)
43
+ super(dialog,options)
44
+ columnTypes = Array.new(options[:columns].length){String}
45
+ (options[:radiolist] || options[:checklist]) ? columnTypes[0] = TrueClass : nil
46
+ listStore = Gtk::ListStore.new(*columnTypes)
47
+ options[:data].each do |datarow|
48
+ row = listStore.append
49
+ datarow.each_with_index do |value,index|
50
+ row[index] = value
51
+ end
52
+ end
53
+ treeView = Gtk::TreeView.new(listStore)
54
+ treeView.selection.mode = (options[:checklist] ? Gtk::SELECTION_MULTIPLE : Gtk::SELECTION_SINGLE)
55
+ toggleProc = (if (treeView.selection.mode.eql?(Gtk::SELECTION_SINGLE))
56
+ lambda do |cell,path|
57
+ selectedRow = treeView.model.get_iter(path)
58
+ if !selectedRow[0]
59
+ selectedRow[0] = true
60
+ currentRow = treeView.model.iter_first
61
+ begin
62
+ currentRow[0] = false unless selectedRow == currentRow
63
+ end while (currentRow.next!)
64
+ end
65
+ end
66
+ else
67
+ lambda do |cell,path|
68
+ selectedRow = treeView.model.get_iter(path)
69
+ selectedRow[0] = !selectedRow[0]
70
+ end
71
+ end)
72
+ options[:columns].each_with_index do |columnName,index|
73
+ renderer,column = columnTypes[index].eql?(TrueClass) ? [Gtk::CellRendererToggle.new,:active] : [Gtk::CellRendererText.new,:text]
74
+ if index.eql?(0)
75
+ renderer.signal_connect('toggled',&toggleProc) if renderer.class.eql?(Gtk::CellRendererToggle)
76
+ renderer.set_radio(true) if options[:radiolist]
77
+ end
78
+ treeView.append_column(Gtk::TreeViewColumn.new(columnName,renderer,column=>index))
79
+ end
80
+ dialog.vbox.add(treeView)
81
+ end
82
+
83
+ def self.retrieve_selection(dialog,options)
84
+ super(dialog,options)
85
+ treeView = dialog.vbox.children[0]
86
+ results = []
87
+ if (options[:checklist])
88
+ treeView.model.each do |model,path,selectedRow|
89
+ if selectedRow[0]
90
+ resultRow = []
91
+ (model.n_columns-1).times {|index| resultRow << selectedRow[index+1]}
92
+ results << resultRow
93
+ end
94
+ end
95
+ return nil if results.size.zero?
96
+ elsif(options[:radiolist])
97
+ treeView.model.each do |model,path,selectedRow|
98
+ if selectedRow[0]
99
+ (model.n_columns-1).times{|index| results << selectedRow[index+1]}
100
+ break
101
+ end
102
+ end
103
+ else
104
+ selectedRow = treeView.selection.selected
105
+ treeView.model.n_columns.times{|index| results << selectedRow[index]}
106
+ end
107
+ return results
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,34 @@
1
+ require 'zerenity/base'
2
+
3
+ module Zerenity
4
+ class MessageDialog < Base # :nodoc:
5
+ def self.build(dialog,options)
6
+ options[:ok_button] = dialog.add_button(Gtk::Stock::OK,Gtk::Dialog::RESPONSE_OK)
7
+ dialog.set_default_response(Gtk::Dialog::RESPONSE_OK)
8
+ end
9
+
10
+ def self.run(options={})
11
+ Gtk.init
12
+ self.check(options)
13
+ dialog = Gtk::MessageDialog.new(nil,Gtk::Dialog::MODAL,options[:type],Gtk::MessageDialog::BUTTONS_NONE,options[:text])
14
+ self.build(dialog,options)
15
+ dialog.set_title(options[:title]) if options[:title]
16
+ result = nil
17
+ if options[:cancel_button]
18
+ options[:cancel_button].signal_connect(CLICKED) do
19
+ dialog.destroy
20
+ Gtk.main_quit
21
+ end
22
+ end
23
+
24
+ options[:ok_button].signal_connect(CLICKED) do
25
+ result = true
26
+ dialog.destroy
27
+ Gtk.main_quit
28
+ end
29
+ dialog.show_all
30
+ Gtk.main
31
+ return result
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,136 @@
1
+ require('zerenity/base')
2
+ module Zerenity
3
+ # Displays a progress bar which can be updated via a processing
4
+ # block which is passed a ProgressProxy object.
5
+ #
6
+ # ====Options
7
+ # [:autoClose] The dialog will automatically close once the
8
+ # progressing block is complete.
9
+ # [:cancellable] If set to true the Cancel button is enabled
10
+ # and the ProgressProxy#cancelled? will be set to true if it
11
+ # is clicked. It is the responsibility of the processing block
12
+ # to monitor this attribute and perfom the necessary actions
13
+ # when it is ckicked.
14
+ #
15
+ # ====Example Usage
16
+ # Zerenity::Progress(:text=>"Processing...",:cancellable=>true,:title=>"Processing Files") do |progress|
17
+ # files.each_with_index do |file,index|
18
+ # progress.cancelled ? break : nil
19
+ # process_file(file)
20
+ # progress.update(index/files.size,"#{((index/files.size)*100).round}% Complete")
21
+ # end
22
+ # if progress.cancelled?
23
+ # progress.text = "Cleaning up..."
24
+ # cleanup_files(files)
25
+ # end
26
+ # end
27
+ def self.Progress(options={},&block)
28
+ Progress.run(options,&block)
29
+ end
30
+
31
+ class Progress < Base # :nodoc:
32
+
33
+ def self.check(options)
34
+ super(options)
35
+ options[:autoClose] ||= false
36
+ options[:cancellable] ||= false
37
+ end
38
+
39
+ def self.build(dialog,options,progressProxy)
40
+ super(dialog,options)
41
+ label = Gtk::Label.new(options[:text])
42
+ progressBar = Gtk::ProgressBar.new
43
+ progressProxy.progressBar = progressBar
44
+ progressBar.pulse_step = 0.02
45
+ !options[:cancellable] ? options[:cancel_button].sensitive = false : nil
46
+ options[:ok_button].sensitive = false
47
+ dialog.vbox.add(label)
48
+ dialog.vbox.add(progressBar)
49
+ end
50
+
51
+ def self.run(options={},&block)
52
+ self.check(options)
53
+ Gtk.init
54
+ dialog = Gtk::Dialog.new(options[:title])
55
+ result = nil
56
+ progressProxy = Zerenity::ProgressProxy.new
57
+ self.build(dialog,options,progressProxy)
58
+ options[:cancel_button].signal_connect(CLICKED) do
59
+ progressProxy.cancel!
60
+ end
61
+ options[:ok_button].signal_connect(CLICKED) do
62
+ result = true
63
+ dialog.destroy
64
+ Gtk.main_quit
65
+ end
66
+ dialog.show_all
67
+ gtkThread = Thread.new do
68
+ Gtk.main
69
+ end
70
+ block.call(progressProxy)
71
+ if options[:autoClose]
72
+ dialog.destroy
73
+ Gtk.main_quit
74
+ return true
75
+ end
76
+ options[:ok_button].sensitive = true
77
+ options[:cancel_button].sensitive = false
78
+ gtkThread.join
79
+ return result
80
+ end
81
+ end
82
+
83
+ # ProgressProxy allows you to update the progress of the
84
+ # Zerenity::Progress dialog. It also can alert the processing
85
+ # block whether or not the Cancel button has been clicked.
86
+ class ProgressProxy
87
+ # An attribute indicating whether the Cancel button has been clicked.
88
+ attr_reader :cancelled
89
+
90
+ def initialize() # :nodoc:
91
+ @progressBar = nil
92
+ @cancelled = false
93
+ end
94
+
95
+ def progressBar=(progressBar) # :nodoc:
96
+ @progressBar = progressBar
97
+ end
98
+
99
+ # Updates the progress bar.
100
+ #
101
+ # ====Parameters
102
+ # [percentage] The percentage completed. Values range from 0 to 1.
103
+ # [text] Optional text that will be displayed in the progress area.
104
+ def update(percentage,text=nil)
105
+ @progressBar.text = text.to_s if text
106
+ @progressBar.fraction = percentage.to_f
107
+ end
108
+
109
+ # When called will put the progress bar into 'pulse' mode. A
110
+ # small bar will move forwards and backwards across the progress
111
+ # area.
112
+ def pulse(text=nil)
113
+ @progressBar.text = text.to_s if text
114
+ @progressBar.pulse
115
+ end
116
+
117
+ # Sets the text in the progress area.
118
+ def text=(text)
119
+ @progressBar.text = text.to_s
120
+ end
121
+
122
+ # Sets the percentage complete of the progress bar. Must be in the range 0-1.
123
+ def percentage=(percentage)
124
+ @progressBar.fraction = percentage.to_f
125
+ end
126
+
127
+ # Alias for cancelled.
128
+ def cancelled?
129
+ @cancelled
130
+ end
131
+
132
+ def cancel! #:nodoc:
133
+ @cancelled = true
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,136 @@
1
+ require('zerenity/base')
2
+ module Zerenity
3
+ # Displays a progress bar which can be updated via a processing
4
+ # block which is passed a ProgressProxy object.
5
+ #
6
+ # ====Options
7
+ # [:autoClose] The dialog will automatically close once the
8
+ # progressing block is complete.
9
+ # [:cancellable] If set to true the Cancel button is enabled
10
+ # and the ProgressProxy#cancelled? will be set to true if it
11
+ # is clicked. It is the responsibility of the processing block
12
+ # to monitor this attribute and perfom the necessary actions
13
+ # when it is ckicked.
14
+ #
15
+ # ====Example Usage
16
+ # Zerenity::Progress(:text=>"Processing...",:cancellable=>true,:title=>"Processing Files") do |progress|
17
+ # files.each_with_index do |file,index|
18
+ # progress.cancelled ? break : nil
19
+ # process_file(file)
20
+ # progress.update(index/files.size,"#{((index/files.size)*100).round}% Complete")
21
+ # end
22
+ # if progress.cancelled?
23
+ # progress.text = "Cleaning up..."
24
+ # cleanup_files(files)
25
+ # end
26
+ # end
27
+ def self.Progress(options={},&block)
28
+ Progress.run(options,&block)
29
+ end
30
+
31
+ class Progress < Base # :nodoc:
32
+
33
+ def self.check(options)
34
+ super(options)
35
+ options[:autoClose] ||= false
36
+ options[:cancellable] ||= false
37
+ end
38
+
39
+ def self.build(dialog,options,progressProxy)
40
+ super(dialog,options)
41
+ label = Gtk::Label.new(options[:text])
42
+ progressBar = Gtk::ProgressBar.new
43
+ progressProxy.progressBar = progressBar
44
+ progressBar.pulse_step = 0.02
45
+ !options[:cancellable] ? options[:cancel_button].sensitive = false : nil
46
+ options[:ok_button].sensitive = false
47
+ dialog.vbox.add(label)
48
+ dialog.vbox.add(progressBar)
49
+ end
50
+
51
+ def self.run(options={},&block)
52
+ self.check(options)
53
+ Gtk.init
54
+ dialog = Gtk::Dialog.new(options[:title])
55
+ result = nil
56
+ progressProxy = Zerenity::ProgressProxy.new
57
+ self.build(dialog,options,progressProxy)
58
+ options[:cancel_button].signal_connect(CLICKED) do
59
+ progressProxy.cancel!
60
+ end
61
+ options[:ok_button].signal_connect(CLICKED) do
62
+ result = true
63
+ dialog.destroy
64
+ Gtk.main_quit
65
+ end
66
+ dialog.show_all
67
+ gtkThread = Thread.new do
68
+ Gtk.main
69
+ end
70
+ block.call(progressProxy)
71
+ if options[:autoClose]
72
+ dialog.destroy
73
+ Gtk.main_quit
74
+ return true
75
+ end
76
+ options[:ok_button].sensitive = true
77
+ options[:cancel_button].sensitive = false
78
+ gtkThread.join
79
+ return result
80
+ end
81
+ end
82
+
83
+ # ProgressProxy allows you to update the progress of the
84
+ # Zerenity::Progress dialog. It also can alert the processing
85
+ # block whether or not the Cancel button has been clicked.
86
+ class ProgressProxy
87
+ # An attribute indicating whether the Cancel button has been clicked.
88
+ attr_reader :cancelled
89
+
90
+ def initialize() # :nodoc:
91
+ @progressBar = nil
92
+ @cancelled = false
93
+ end
94
+
95
+ def progressBar=(progressBar) # :nodoc:
96
+ @progressBar = progressBar
97
+ end
98
+
99
+ # Updates the progress bar.
100
+ #
101
+ # ====Parameters
102
+ # [percentage] The percentage completed. Values range from 0 to 1.
103
+ # [text] Optional text that will be displayed in the progress area.
104
+ def update(percentage,text=nil)
105
+ text ? @progressBar.text = text.to_s : nil
106
+ @progressBar.fraction = percentage.to_f
107
+ end
108
+
109
+ # When called will put the progress bar into 'pulse' mode. A
110
+ # small bar will move forwards and backwards across the progress
111
+ # area.
112
+ def pulse(text=nil)
113
+ text ? @progressBar.text = text.to_s : nil
114
+ @progressBar.pulse
115
+ end
116
+
117
+ # Sets the text in the progress area.
118
+ def text=(text)
119
+ @progressBar.text = text.to_s
120
+ end
121
+
122
+ # Sets the percentage complete of the progress bar. Must be in the range 0-1.
123
+ def percentage=(percentage)
124
+ @progressBar.fraction = percentage.to_f
125
+ end
126
+
127
+ # Alias for cancelled.
128
+ def cancelled?
129
+ @cancelled
130
+ end
131
+
132
+ def cancel! #:nodoc:
133
+ @cancelled = true
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,26 @@
1
+ require('zerenity/messagedialog')
2
+
3
+ module Zerenity
4
+ # Displays a question dialog. Returns true if OK is clicked, false if
5
+ # Cancel is clicked.
6
+ #
7
+ # ====Example Useage
8
+ # if Zerenity::Question(:text=>"Continue processing?")
9
+ # post_process_files()
10
+ # end
11
+ def self.Question(options={})
12
+ Question.run(options)
13
+ end
14
+
15
+ class Question < MessageDialog # :nodoc:
16
+ def self.check(options)
17
+ super(options)
18
+ options[:type] = Gtk::MessageDialog::QUESTION
19
+ end
20
+
21
+ def self.build(dialog,options)
22
+ super(dialog,options)
23
+ options[:cancel_button] = dialog.add_button(Gtk::Stock::CANCEL,Gtk::Dialog::RESPONSE_CANCEL)
24
+ end
25
+ end
26
+ end