summaryrefslogtreecommitdiff
path: root/src/hal/utils/halgui/app.py
blob: 3ce7326663405f9431874f5685d9dda53d0859f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os, sys, gtk, gobject

from design import Design
from load import file_load, file_new
from save import file_save

def appquit(*args):
	gtk.main_quit()

def open_file(action):
	app.openfile()

def save_file(action):
	if app.design.file_name:
		file_save(app.design, app.design.file_name)
	else:
		app.savefile()

def save_file_as(action):
	app.savefile()

def new_file(action):
	file_new(app.design)

def refresh(action):
	app.design.update()

menu_entries = (
	("FileMenu", None, "_File"),
	("New", gtk.STOCK_NEW, None, "<control>N",
		"Create a new file", new_file),
	("Open", gtk.STOCK_OPEN, None, "<control>O",
		"Open an existing file", open_file),
	("Save", gtk.STOCK_SAVE, None, "<control>S",
		"Save file", save_file),
	("Save as", gtk.STOCK_SAVE_AS, None, "<shift><control>S",
		"Save file with new name", save_file_as),
	("Quit", gtk.STOCK_QUIT, None, "<control>Q",
		"Quit program", appquit),
	("ViewMenu", None, "_View"),
	("Refresh", gtk.STOCK_REFRESH, None, "<control>R",
		"Refresh", refresh),
)

class Application(gtk.Window):
	def __init__(self, parent=None):
		global app
		app = self
		gtk.Window.__init__(self)
		try:
			self.set_screen(parent.get_screen())
		except AttributeError:
			self.connect('destroy', lambda *w: gtk.main_quit())

		self.design = Design(self)

		actions = gtk.ActionGroup("Actions")
		actions.add_actions(menu_entries)
		
		ui = gtk.UIManager()
		ui.insert_action_group(actions, 0)
		self.add_accel_group(ui.get_accel_group())

		# better path to 'ui.xml' needed
		uifile = os.path.join(os.path.dirname(sys.argv[0]), "ui.xml")

		try:
			mergeid = ui.add_ui_from_file(uifile)
		except gobject.GError, msg:
			print "error building menus: %s" % (msg)
		
		box1 = gtk.VBox(False, 0)

		self.add(box1)

		box1.pack_start(ui.get_widget("/MenuBar"), False, False)
		box1.pack_start(ui.get_widget("/ToolBar"), False, False)

		box1.pack_start(self.design, True, True)

		statusbar = gtk.Statusbar()
		box1.pack_start(statusbar, False, False)

		self.set_default_size(1024, 768)
		self.settitle()
		self.set_border_width(0)

	def show_app(self):
		self.show_all()

	def settitle(self):
		if self.design.file_name:
			self.set_title("Crapahalic - " + self.design.file_name)
		else:
			self.set_title("Crapahalic")

	def openfile(self):
		dialog = gtk.FileChooserDialog(title="Open...", action=gtk.FILE_CHOOSER_ACTION_OPEN,
			buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
		dialog.set_default_response(gtk.RESPONSE_OK)
		hal_filter = gtk.FileFilter()
		hal_filter.set_name("craphal files")
		hal_filter.add_pattern("*.hal")
		dialog.add_filter(hal_filter)
		response = dialog.run()
		if response == gtk.RESPONSE_OK:
			new_file(None)
			file_load(app.design, dialog.get_filename())
		dialog.destroy()

	def savefile(self):
		dialog = gtk.FileChooserDialog(title="Save...", action=gtk.FILE_CHOOSER_ACTION_SAVE,
			buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_SAVE,gtk.RESPONSE_OK))
		dialog.set_default_response(gtk.RESPONSE_OK)
		hal_filter = gtk.FileFilter()
		hal_filter.set_name("craphal files")
		hal_filter.add_pattern("*.hal")
		dialog.add_filter(hal_filter)
		response = dialog.run()
		if response == gtk.RESPONSE_OK:
			file_save(app.design, dialog.get_filename())
		dialog.destroy()