Also change the startup behaviour: to get it to process stdin, pass '-' on
the command line.
From: Marius Gedminas <marius@gedmin.as>
| ... | ... |
@@ -1096,6 +1096,8 @@ class DotWindow(gtk.Window): |
| 1096 | 1096 |
ui = ''' |
| 1097 | 1097 |
<ui> |
| 1098 | 1098 |
<toolbar name="ToolBar"> |
| 1099 |
+ <toolitem action="Open"/> |
|
| 1100 |
+ <separator/> |
|
| 1099 | 1101 |
<toolitem action="ZoomIn"/> |
| 1100 | 1102 |
<toolitem action="ZoomOut"/> |
| 1101 | 1103 |
<toolitem action="ZoomFit"/> |
| ... | ... |
@@ -1131,6 +1133,7 @@ class DotWindow(gtk.Window): |
| 1131 | 1133 |
|
| 1132 | 1134 |
# Create actions |
| 1133 | 1135 |
actiongroup.add_actions(( |
| 1136 |
+ ('Open', gtk.STOCK_OPEN, None, None, None, self.on_open),
|
|
| 1134 | 1137 |
('ZoomIn', gtk.STOCK_ZOOM_IN, None, None, None, self.widget.on_zoom_in),
|
| 1135 | 1138 |
('ZoomOut', gtk.STOCK_ZOOM_OUT, None, None, None, self.widget.on_zoom_out),
|
| 1136 | 1139 |
('ZoomFit', gtk.STOCK_ZOOM_FIT, None, None, None, self.widget.on_zoom_fit),
|
| ... | ... |
@@ -1156,6 +1159,38 @@ class DotWindow(gtk.Window): |
| 1156 | 1159 |
def set_dotcode(self, dotcode): |
| 1157 | 1160 |
self.widget.set_dotcode(dotcode) |
| 1158 | 1161 |
|
| 1162 |
+ def open_file(self, filename): |
|
| 1163 |
+ try: |
|
| 1164 |
+ fp = file(filename, 'rt') |
|
| 1165 |
+ self.set_dotcode(fp.read()) |
|
| 1166 |
+ fp.close() |
|
| 1167 |
+ except IOError: |
|
| 1168 |
+ # TODO: show an error message |
|
| 1169 |
+ pass |
|
| 1170 |
+ |
|
| 1171 |
+ def on_open(self, action): |
|
| 1172 |
+ chooser = gtk.FileChooserDialog(title="Open dot File", |
|
| 1173 |
+ action=gtk.FILE_CHOOSER_ACTION_OPEN, |
|
| 1174 |
+ buttons=(gtk.STOCK_CANCEL, |
|
| 1175 |
+ gtk.RESPONSE_CANCEL, |
|
| 1176 |
+ gtk.STOCK_OPEN, |
|
| 1177 |
+ gtk.RESPONSE_OK)) |
|
| 1178 |
+ chooser.set_default_response(gtk.RESPONSE_OK) |
|
| 1179 |
+ filter = gtk.FileFilter() |
|
| 1180 |
+ filter.set_name("Graphviz dot files")
|
|
| 1181 |
+ filter.add_pattern("*.dot")
|
|
| 1182 |
+ chooser.add_filter(filter) |
|
| 1183 |
+ filter = gtk.FileFilter() |
|
| 1184 |
+ filter.set_name("All files")
|
|
| 1185 |
+ filter.add_pattern("*")
|
|
| 1186 |
+ chooser.add_filter(filter) |
|
| 1187 |
+ if chooser.run() == gtk.RESPONSE_OK: |
|
| 1188 |
+ filename = chooser.get_filename() |
|
| 1189 |
+ chooser.destroy() |
|
| 1190 |
+ self.open_file(filename) |
|
| 1191 |
+ else: |
|
| 1192 |
+ chooser.destroy() |
|
| 1193 |
+ |
|
| 1159 | 1194 |
|
| 1160 | 1195 |
def main(): |
| 1161 | 1196 |
import optparse |
| ... | ... |
@@ -1165,17 +1200,16 @@ def main(): |
| 1165 | 1200 |
version="%%prog %s" % __version__) |
| 1166 | 1201 |
|
| 1167 | 1202 |
(options, args) = parser.parse_args(sys.argv[1:]) |
| 1168 |
- |
|
| 1169 |
- if len(args) == 0: |
|
| 1170 |
- fp = sys.stdin |
|
| 1171 |
- elif len(args) == 1: |
|
| 1172 |
- fp = file(args[0], 'rt') |
|
| 1173 |
- else: |
|
| 1203 |
+ if len(args) > 1: |
|
| 1174 | 1204 |
parser.error('incorrect number of arguments')
|
| 1175 | 1205 |
|
| 1176 | 1206 |
win = DotWindow() |
| 1177 |
- win.set_dotcode(fp.read()) |
|
| 1178 | 1207 |
win.connect('destroy', gtk.main_quit)
|
| 1208 |
+ if len(args) >= 1: |
|
| 1209 |
+ if args[0] == '-': |
|
| 1210 |
+ win.set_dotcode(sys.stdin.read()) |
|
| 1211 |
+ else: |
|
| 1212 |
+ win.open_file(args[0]) |
|
| 1179 | 1213 |
gtk.main() |
| 1180 | 1214 |
|
| 1181 | 1215 |
|