Class: Interphase::Dialog

Inherits:
Widget
  • Object
show all
Defined in:
lib/interphase/widgets/dialog.rb

Overview

A base class representing a simple, blank dialog box.

Direct Known Subclasses

MessageDialog

Instance Attribute Summary

Attributes inherited from Widget

#gtk_instance, #name, #parent

Instance Method Summary collapse

Methods inherited from Widget

#destroy, #method_missing, #on, #respond_to_missing?, #show, #size

Constructor Details

#initialize(instance = nil, **options, &block) ⇒ Dialog

Construct a new blank dialog box.


9
10
11
12
13
# File 'lib/interphase/widgets/dialog.rb', line 9

def initialize(instance = nil, **options, &block)
  super(instance || Gtk::Dialog.new, **options, &block)

  options['buttons']&.map { |btn| add_button(*btn) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Interphase::Widget

Instance Method Details

#action_to_integer(action) ⇒ Object

Converts an #add_button action symbol to the GTK RESPONSE integer which it represents.

action

The action symbol to convert. Should be one of the valid values of action to #add_button.


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/interphase/widgets/dialog.rb', line 19

def action_to_integer(action)
  valid_actions =
    %i[none reject accept delete ok cancel close yes no apply help]

  throw ArgumentError, 'Invalid button action' \
    unless valid_actions.include? action

  # Map :delete to its GTK equivalent
  action = :delete_event if action == :delete

  Gtk::Dialog.const_get("RESPONSE_#{action.to_s.upcase}")
end

#add_button(text, action) ⇒ Object

Adds a button to the dialog box.

text

The text to display on the button.

action

The action which occurs when this button is clicked. Must be one of: :none, :reject, :accept, :delete, :ok, :cancel, :close, :yes, :no, :apply, :help.


37
38
39
40
# File 'lib/interphase/widgets/dialog.rb', line 37

def add_button(text, action)
  gtk_instance.add_button(text, action_to_integer(action))
  nil
end

#runObject

Run this dialog box. This call will block execution until the dialog is closed. Consider using Window#in_background if blocking is not desired. You should usually call destroy after this method, otherwise the dialog will remain even after selecting an option.


46
47
48
# File 'lib/interphase/widgets/dialog.rb', line 46

def run
  gtk_instance.run
end