class Gtk3assist::Msgbox

Constants

ARGS_ALLOWED

An array of possible arguments that the constructor accepts.

DATA

Various data like the current showing messagebox.

Attributes

dialog[R]

The Gtk::Dialog-object.

result[R]

The result of the dialog.

Public Class Methods

current() click to toggle source
# File lib/gtk3assist_msgbox.rb, line 23
def self.current
  raise "No current showing message-box." if !DATA[:current]
end
error(e, args = {}) click to toggle source
# 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
new(args) click to toggle source

Constructor.

# 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

Public Instance Methods

respond(res) click to toggle source

Responds to the dialog.

# 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_text() click to toggle source

Returns the result as a string.

# File lib/gtk3assist_msgbox.rb, line 147
def result_text
  raise "No result yet." if !@result
  return Gtk::ResponseType[@result].to_sym
end
run() click to toggle source

Runs the dialog.

# File lib/gtk3assist_msgbox.rb, line 125
def run
  @result = @dialog.run
  return @result
end
show() click to toggle source

Shows the dialog but doesnt run it.

# File lib/gtk3assist_msgbox.rb, line 131
def show
  @dialog.show
end