~ian-mcintosh/luz/trunk

1 by yella
Initial import.
1
 ###############################################################################
1232 by Ian McIntosh
- make GladeXML#guard_source_from_gc call optional (as some platforms seem to be missing it)
2
 #  Copyright 2009 Ian McIntosh <ian@openanswers.org>
1 by yella
Initial import.
3
 #
4
 #  This program is free software; you can redistribute it and/or modify
5
 #  it under the terms of the GNU General Public License as published by
6
 #  the Free Software Foundation; either version 2 of the License, or
7
 #  (at your option) any later version.
8
 #
9
 #  This program is distributed in the hope that it will be useful,
10
 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 #  GNU Library General Public License for more details.
13
 #
14
 #  You should have received a copy of the GNU General Public License
15
 #  along with this program; if not, write to the Free Software
16
 #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
 ###############################################################################
18
19
require 'delegate'
20
21
class GladeWindow < DelegateClass(Gtk::Window)
2141.1.2 by scott lee davis
added new builder glade window.
22
23
	MOUSE_BUTTON_1, MOUSE_BUTTON_2, MOUSE_BUTTON_3 = (1..3).to_a
24
67 by yella
everything
25
	def initialize(root_widget_name = nil, options = {})
2145 by Ian McIntosh
- rename .glade to .ui, move them to gui/, and move icons/ to gui/
26
		file_name = sprintf("gui/%s.ui", root_widget_name) 
2141.1.2 by scott lee davis
added new builder glade window.
27
		instance_variable_names = options[:widgets] || []	
2141.1.3 by Ian McIntosh
- first working Glade => GtkBuilder conversion
28
2141.1.2 by scott lee davis
added new builder glade window.
29
		@builder = Gtk::Builder.new.add_from_file(file_name)
30
31
		# create instance variables out of created widgets
32
		instance_variable_names.each { |name|
33
			instance_variable_set('@' + name.to_s, @builder.get_object(name.to_s))
34
		}
35
36
		# hookup signal handlers
37
		@builder.connect_signals { |handler_name|
2141.1.3 by Ian McIntosh
- first working Glade => GtkBuilder conversion
38
			method(handler_name)
2141.1.2 by scott lee davis
added new builder glade window.
39
		}
2141.1.3 by Ian McIntosh
- first working Glade => GtkBuilder conversion
40
2141.1.2 by scott lee davis
added new builder glade window.
41
		# in the class, we will refer to the GtkWindow as @window when referencing variables	
42
		@window = @builder.get_object(root_widget_name)
1 by yella
Initial import.
43
		@window.realize
44
2141.1.2 by scott lee davis
added new builder glade window.
45
		#options = root_widget_name = glade = nil		# GARBAGE-HACK
67 by yella
everything
46
47
		setup_default_signal_handlers
48
532 by yella
- Add View/Fullscreen toggle to EditorWindow.
49
		@is_fullscreen = false		# TODO: remove this and instead test the state via GTK/GDK ?
50
67 by yella
everything
51
		super(@window)		# ...as required by delegation
52
	end
53
54
	def setup_default_signal_handlers
1 by yella
Initial import.
55
		@window.signal_connect('delete_event') { self.on_delete_event }
56
	end
57
63 by yella
- new directors
58
	def get_signal_handler(handler_name)
59
		# Create a new method to wrap the actual signal handler, with added exception handling
60
		# This prevents user actions from crashing the application.
61
		self.class.class_eval <<-end_class_eval
62
			def #{handler_name}_with_exception_handling(*args)
63
				begin
64
					if method(:#{handler_name}).arity == 0
65
						self.send(:#{handler_name})
66
					else
67
						self.send(:#{handler_name}, *args)
68
					end
69
				rescue Exception => e
70
					puts "Glade signal handler '#{handler_name}' caused exception:\n"
71
					puts e.report
72
				end
73
			end
74
		end_class_eval
75
76
		# return our new method
77
		method("#{handler_name}_with_exception_handling")
78
	end
79
1 by yella
Initial import.
80
	def on_delete_event
16 by yella
* Add stage preview window (not 100% working)
81
		hide 		# overrideable
1 by yella
Initial import.
82
	end
500 by yella
- GladeWindow add hack around long-standing bug "warning: GRClosure invoking callback: already destroyed" (https://bugs.launchpad.net/ubuntu/+source/ruby-gnome2/+bug/66623)
83
67 by yella
everything
84
	def on_close_button_clicked
85
		hide
86
	end
500 by yella
- GladeWindow add hack around long-standing bug "warning: GRClosure invoking callback: already destroyed" (https://bugs.launchpad.net/ubuntu/+source/ruby-gnome2/+bug/66623)
87
67 by yella
everything
88
	def on_cancel_button_clicked
89
		hide
90
	end
1 by yella
Initial import.
91
end