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



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

def default_database
  @dot_file_prefs[:db]
end

#dispatch(handler, event) ⇒ Object

dispatch a signal to the correct controller



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

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



68
69
70
71
72
# File 'lib/active_window/application.rb', line 68

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

#runObject



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

def run
  Gtk.main
end

#save_default_database(hash) ⇒ Object



35
36
37
38
# File 'lib/active_window/application.rb', line 35

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



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

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
  post_setup
end

#startObject



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

def start
  setup
  window.show
  run
end

#widget(name) ⇒ Object



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

def widget(name)
  glade[name]
end