Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / editors / sced / scedwin / py / ConfigurationDialog.py
blob35ca8bd8a5ea6f2439c3c85691b50098e09eef60
1 # sced (SuperCollider mode for gedit)
3 # Copyright 2012 Jakob Leben
4 # Copyright 2009 Artem Popov and other contributors (see AUTHORS)
6 # sced is free software:
7 # you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 import gobject
21 import gtk
22 from Settings import Settings
24 def on_pref_widget_notify_sensitive(widget, spec):
25 label = widget.get_data("pref-label")
26 if label is not None:
27 label.set_sensitive(widget.props.sensitive)
29 # FIXME: implement custom widget (or custom widget sequence) as well
30 def create_pref_section(title, wlabels=[], custom=[]):
31 vbox = gtk.VBox(spacing=6)
33 label = gobject.new(gtk.Label, label="<b>%s</b>" % title,
34 use_markup=True,
35 xalign=0)
36 vbox.pack_start(label, expand=False)
37 label.show()
39 align = gobject.new(gtk.Alignment, left_padding=12)
40 vbox.pack_start(align, expand=False)
41 align.show()
43 table = gobject.new(gtk.Table,
44 n_rows=len(wlabels) + len(custom),
45 n_columns=2,
46 row_spacing=6,
47 column_spacing=12)
48 align.add(table)
49 table.show()
51 for i in range(len(wlabels)):
52 l, widget = wlabels[i]
53 label = gobject.new(gtk.Label, label=l, xalign=0)
54 widget.connect("notify::sensitive", on_pref_widget_notify_sensitive)
55 widget.set_data("pref-label", label)
57 if l is not None:
58 table.attach(label, 0, 1, i, i + 1,
59 xoptions=gtk.FILL, yoptions=gtk.FILL)
60 table.attach(widget, 1, 2, i, i + 1,
61 xoptions=gtk.EXPAND | gtk.FILL, yoptions=gtk.FILL)
62 else:
63 table.attach(widget, 0, 2, i, i + 1,
64 xoptions=gtk.EXPAND | gtk.FILL, yoptions=gtk.FILL)
66 table.show_all()
67 return vbox
69 # FIXME: fix notification
70 class ConfigurationDialog(gtk.Dialog):
71 __gsignals__ = {
72 "response": "override",
73 } # __gsignals__
74 def __init__(self, plugin):
75 gtk.Dialog.__init__(self, title=_("Sced configuration"),
76 flags=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
77 buttons = (
78 gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
79 gtk.STOCK_OK, gtk.RESPONSE_ACCEPT
81 self.set_default_response(gtk.RESPONSE_ACCEPT)
83 self.__settings = plugin.settings()
85 self.__create_page_general()
87 def __create_filesystem_entry( self, chooser, action, stock = gtk.STOCK_OPEN ):
88 entry = gtk.Entry()
89 btn = gtk.Button(stock=stock)
90 box = gtk.HBox()
91 box.add(entry)
92 box.add(btn)
94 def run_dialog(btn):
95 chooser.set_action(action)
96 chooser.set_filename(entry.get_text())
97 response = chooser.run()
98 chooser.hide()
99 if response == gtk.RESPONSE_ACCEPT:
100 entry.set_text(chooser.get_filename())
102 btn.connect("clicked", run_dialog)
104 return (box, entry)
108 def __create_page_general(self):
109 # create views
110 chooser = gtk.FileChooserDialog(
111 parent = self,
112 title = "Choose interpreter program",
113 buttons = (
114 gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
115 gtk.STOCK_OK, gtk.RESPONSE_ACCEPT
118 chooser.set_select_multiple(False)
120 sc_dir_view, sc_dir_entry = self.__create_filesystem_entry (
121 chooser,
122 gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER )
124 adv_view = gtk.CheckButton()
126 cmd_view, cmd_entry = self.__create_filesystem_entry (
127 chooser,
128 gtk.FILE_CHOOSER_ACTION_OPEN )
130 wd_view, wd_entry = self.__create_filesystem_entry (
131 chooser,
132 gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER )
134 def toggle_advanced(advanced):
135 sc_dir_view.set_sensitive(not advanced)
136 cmd_view.set_sensitive(advanced)
137 wd_view.set_sensitive(advanced)
139 adv_view.connect("toggled", lambda btn: toggle_advanced(btn.get_active()) )
141 # fill in the data
142 sets = self.__settings
143 if sets.sc_dir is not None:
144 sc_dir_entry.set_text(sets.sc_dir)
145 adv_view.set_active(sets.advanced is True)
146 if sets.sclang_cmd is not None:
147 cmd_entry.set_text(sets.sclang_cmd)
148 if sets.sclang_work_dir is not None:
149 wd_entry.set_text(sets.sclang_work_dir)
150 toggle_advanced(sets.advanced is True)
152 self.__adv_check = adv_view
153 self.__sc_dir_entry = sc_dir_entry
154 self.__cmd_entry = cmd_entry
155 self.__wd_entry = wd_entry
157 # lay out
158 section = create_pref_section("Basic", [
159 ("SuperCollider folder:", sc_dir_view),
160 ("Advanced settings:", adv_view),
162 section.props.border_width = 12
163 self.vbox.add(section)
164 section.show()
165 section = create_pref_section("Interpreter options", [
166 ("Command:", cmd_view),
167 ("Runtime folder:", wd_view)
169 section.props.border_width = 12
170 self.vbox.add(section)
171 section.show()
173 def do_response(self, response):
174 if response == gtk.RESPONSE_ACCEPT:
175 sets = self.__settings
176 sets.sc_dir = self.__sc_dir_entry.get_text()
177 sets.advanced = self.__adv_check.get_active()
178 sets.sclang_work_dir = self.__wd_entry.get_text()
179 sets.sclang_cmd = self.__cmd_entry.get_text()
180 sets.save()
182 self.destroy()