Class: ActiveWindow::Application

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/active_window/application.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Application

Returns a new instance of Application.



11
12
13
14
15
16
17
18
19
# File 'lib/active_window/application.rb', line 11

def initialize(options = {})
  @glade = GladeXML.new(find_glade, nil, options[:title] || 'application' )
  @window = widget(options[:main_window] || 'main_window')
  @window.signal_connect("destroy") { Gtk.main_quit }
  @dot_file_prefs = DotFile.read
  @database = options[:database]
  define_widget_readers
  run_callbacks :after_initialize
end

Instance Attribute Details

#controllerObject

Returns the value of attribute controller.



5
6
7
# File 'lib/active_window/application.rb', line 5

def controller
  @controller
end

#databaseObject

Returns the value of attribute database.



5
6
7
# File 'lib/active_window/application.rb', line 5

def database
  @database
end

#gladeObject

Returns the value of attribute glade.



5
6
7
# File 'lib/active_window/application.rb', line 5

def glade
  @glade
end

#windowObject

Returns the value of attribute window.



5
6
7
# File 'lib/active_window/application.rb', line 5

def window
  @window
end

Instance Method Details

#default_databaseObject



28
29
30
# File 'lib/active_window/application.rb', line 28

def default_database
  @dot_file_prefs[:db]
end

#dispatch(handler, event) ⇒ Object

dispatch a signal to the correct controller



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
# File 'lib/active_window/application.rb', line 73

def dispatch(handler, event)
  controller_name,action = handler.to_s.split('.')
  unless controller_name and action
    return(error "cannot parse handler '%s'" % handler)
  end

  name = controller_name.to_sym 
  ctrl = controller[name]
  unless ctrl
    puts controller.inspect
    return(error "no controller '%s' defined" % controller_name.camelize)
  end
  
  unless ctrl.respond_to? action
    return(error "controller '%s' does not have a method '%s'" % [ctrl.class, action])
  end
  
  method = ctrl.method(action)
  #puts "calling %s.%s" % [controller_name.camelize, action]
  if method.arity == 0
    method.call
  else
    method.call(event)
  end
end

#post_setupObject

calls post_setup on each controller



64
65
66
67
68
# File 'lib/active_window/application.rb', line 64

def post_setup
  controller.each do |name,ctrl|
    ctrl.post_setup
  end
end

#save_default_database(hash) ⇒ Object



32
33
34
35
# File 'lib/active_window/application.rb', line 32

def save_default_database(hash)
  @dot_file_prefs[:db] = hash
  @dot_file_prefs.save
end

#setupObject

creates the controllers, connects the signals calls ‘setup’ on each controller



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_window/application.rb', line 39

def setup
  @controller = {}
  
  Module.constants.grep(/.Controller$/).each do |klass|
     ctrl = Kernel.const_get(klass).instance
     ctrl.application = self
     ctrl.setup
     name = klass.to_s.sub('Controller','').underscore.to_sym # eg MyFirstController -> :my_first
     controller[name] = ctrl
  end
  
  glade.signal_autoconnect_full do |source, target, signal, handler, data|
    # for example:
    # source  : instance of Gtk::ImageMenuItem
    # target  : nil
    # signal  : activate, clicked, pressed, etc.
    # handler : window.close, which would call WindowController.close()
    # data    : nil
    #puts [source, target, signal, handler, data].inspect
    source.signal_connect(signal) { |widget,event| self.dispatch(handler, :source => source, :target => target, :signal => signal, :handler => handler, :data => data, :widget => widget, :event => event) }
    #source.signal_connect(signal) { self.(handler, data) }
  end
end

#startObject



21
22
23
24
25
26
# File 'lib/active_window/application.rb', line 21

def start
  setup
  post_setup
  window.show
  Gtk.main
end

#widget(name) ⇒ Object



7
8
9
# File 'lib/active_window/application.rb', line 7

def widget(name)
  glade[name]
end