Class: Gtk3assist::Msgbox

Inherits:
Object
  • Object
show all
Defined in:
lib/gtk3assist_msgbox.rb

Constant Summary collapse

ARGS_ALLOWED =

An array of possible arguments that the constructor accepts.

[:msg, :parent, :run, :title, :type]
DATA =

Various data like the current showing messagebox.

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Msgbox

Constructor.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/gtk3assist_msgbox.rb', line 28

def initialize(args)
  raise "'args' wasnt a hash." if !args.is_a?(Hash)
  args.each do |key, val|
    raise "Invalid argument: '#{key}'." if !ARGS_ALLOWED.include?(key)
  end
  
  raise "No ':msg' was given." if args[:msg].to_s.strip.empty?
  args[:type] = :info if !args[:type]
  
  if !args[:title]
    case args[:type]
      when :warning
        args[:title] = Gtk3assist._("Warning")
      when :yesno
        args[:title] = Gtk3assist._("Question")
      when :info
        args[:title] = Gtk3assist._("Information")
      else
        raise "Unknown type: '#{args[:type]}'."
    end
  end
  
  @dialog = Gtk::Dialog.new
  @dialog.resize(350, 1)
  @dialog.set_title(args[:title])
  
  DATA[:current] = self
  @dialog.signal_connect("destroy") do
    DATA.delete(:current)
  end
  
  box = Gtk::Box.new(Gtk::Orientation[:horizontal], 4)
  @dialog.get_content_area.add(box)
  
  case args[:type]
    when :warning, :info
      if args[:type] == :info
        image = Gtk::Image.new_from_stock(Gtk::STOCK_DIALOG_INFO, Gtk::IconSize[:dialog])
      elsif args[:type] == :warning
        image = Gtk::Image.new_from_stock(Gtk::STOCK_DIALOG_WARNING, Gtk::IconSize[:dialog])
      else
        raise "Unknown type: '#{args[:type]}'."
      end
      
      box.pack_start(image, false, false, 4)
      image.show
      
      lab = Gtk::Label.new(args[:msg])
      lab.set_selectable(true)
      lab.set_justify(Gtk::Justification[:left])
      
      box.pack_start(lab, false, false, 4)
      lab.show
      
      but = Gtk::Button.new_from_stock(Gtk::STOCK_OK)
      but.signal_connect("clicked") do
        @result = Gtk::ResponseType[:ok]
        @dialog.response(@result)
        @dialog.destroy
      end
      
      res = Gtk::ResponseType[:ok]
      @dialog.get_action_area.add(but)
      but.show
    when :yesno
      image = Gtk::Image.new_from_stock(Gtk::STOCK_DIALOG_QUESTION, Gtk::IconSize[:dialog])
      
      box.pack_start(image, false, false, 4)
      image.show
      
      lab = Gtk::Label.new(args[:msg])
      lab.set_selectable(true)
      lab.set_justify(Gtk::Justification[:left])
      
      box.pack_start(lab, false, false, 4)
      lab.show
      
      but_yes = Gtk::Button.new_from_stock(Gtk::STOCK_YES)
      but_yes.signal_connect("clicked") do
        @result = Gtk::ResponseType[:yes]
        @dialog.response(@result)
      end
      
      but_no = Gtk::Button.new_from_stock(Gtk::STOCK_NO)
      but_no.signal_connect("clicked") do
        @result = Gtk::ResponseType[:no]
        @dialog.response(@result)
      end
    else
      raise "Unknown type: '#{args[:type]}'."
  end
  
  box.show
  @result = @dialog.run if !args.key?(:run) or args[:run]
end

Instance Attribute Details

#dialogObject (readonly)

The Gtk::Dialog-object.



9
10
11
# File 'lib/gtk3assist_msgbox.rb', line 9

def dialog
  @dialog
end

#resultObject (readonly)

The result of the dialog.



12
13
14
# File 'lib/gtk3assist_msgbox.rb', line 12

def result
  @result
end

Class Method Details

.currentObject



23
24
25
# File 'lib/gtk3assist_msgbox.rb', line 23

def self.current
  raise "No current showing message-box." if !DATA[:current]
end

.error(e, args = {}) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/gtk3assist_msgbox.rb', line 14

def self.error(e, args = {})
  msg = Gtk3assist._("An error occurred.")
  msg << "\n\n"
  msg << "<#{e.class.name}: #{e.message}>\n"
  msg << e.backtrace.join("\n")
  
  return Gtk3assist::Msgbox.new(args.merge(:type => :warning, :msg => msg))
end

Instance Method Details

#respond(res) ⇒ Object

Responds to the dialog.



136
137
138
139
140
141
142
143
144
# File 'lib/gtk3assist_msgbox.rb', line 136

def respond(res)
  case res
    when :cancel, :no, :ok, :yes
      @result = Gtk::ResponseType[res]
      @dialog.response(@result)
    else
      raise "Unknown response: '#{res}'."
  end
end

#result_textObject

Returns the result as a string.



147
148
149
150
# File 'lib/gtk3assist_msgbox.rb', line 147

def result_text
  raise "No result yet." if !@result
  return Gtk::ResponseType[@result].to_sym
end

#runObject

Runs the dialog.



125
126
127
128
# File 'lib/gtk3assist_msgbox.rb', line 125

def run
  @result = @dialog.run
  return @result
end

#showObject

Shows the dialog but doesnt run it.



131
132
133
# File 'lib/gtk3assist_msgbox.rb', line 131

def show
  @dialog.show
end