| 1 | #!/usr/bin/env seed |
|---|
| 2 | |
|---|
| 3 | Gtk = imports.gi.Gtk; |
|---|
| 4 | Gio = imports.gi.Gio; |
|---|
| 5 | Gdk = imports.gi.Gdk; |
|---|
| 6 | |
|---|
| 7 | const VERSION = "0.1.1"; |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | /** |
|---|
| 11 | * Creates and displays the "About" dialog. |
|---|
| 12 | */ |
|---|
| 13 | function aboutDialog(widget) |
|---|
| 14 | { |
|---|
| 15 | var dialog = new Gtk.AboutDialog(); // let |
|---|
| 16 | dialog.set_comments("This is a Seed demo."); |
|---|
| 17 | dialog.set_program_name("Seeded"); |
|---|
| 18 | dialog.set_version(VERSION); |
|---|
| 19 | dialog.set_title("Seeded " + VERSION); |
|---|
| 20 | dialog.signal.response.connect(function () { dialog.destroy(); }); |
|---|
| 21 | |
|---|
| 22 | dialog.show_all(); |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * Evaluates the current buffer from textview as JavaScript. |
|---|
| 28 | */ |
|---|
| 29 | function evaluateJavascript() |
|---|
| 30 | { |
|---|
| 31 | var buffer = textview.get_buffer(); // let |
|---|
| 32 | startiter = new Gtk.TextIter(); |
|---|
| 33 | buffer.get_start_iter(startiter); |
|---|
| 34 | enditer = new Gtk.TextIter(); |
|---|
| 35 | buffer.get_end_iter(enditer); |
|---|
| 36 | buf = buffer.get_text(startiter, enditer, false); |
|---|
| 37 | eval(buf); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | /** |
|---|
| 42 | * Creates and displays the "Find" dialog. |
|---|
| 43 | */ |
|---|
| 44 | function findDialog(widget) |
|---|
| 45 | { |
|---|
| 46 | /* callback for doing findes or closing the find dialog */ |
|---|
| 47 | function button_pressed(widget, response) { |
|---|
| 48 | if (response == Gtk.ResponseType.APPLY) { |
|---|
| 49 | |
|---|
| 50 | /* an earlier find result */ |
|---|
| 51 | var oldfound = false; // let |
|---|
| 52 | if (found) oldfound = true; |
|---|
| 53 | |
|---|
| 54 | /* init find area */ |
|---|
| 55 | var iter = new Gtk.TextIter(); // let |
|---|
| 56 | buffer.get_start_iter(iter); |
|---|
| 57 | iter.set_offset(buffer.cursor_position); |
|---|
| 58 | var match_start = new Gtk.TextIter(); // let |
|---|
| 59 | var match_end = new Gtk.TextIter(); // let |
|---|
| 60 | |
|---|
| 61 | /* perform the actual find */ |
|---|
| 62 | if (backwards) |
|---|
| 63 | found = iter.backward_search(entry.get_text(), 0, |
|---|
| 64 | match_start, match_end, null); |
|---|
| 65 | else |
|---|
| 66 | found = iter.forward_search(entry.get_text(), 0, |
|---|
| 67 | match_start, match_end, null); |
|---|
| 68 | |
|---|
| 69 | if (found) { |
|---|
| 70 | if (oldfound) tagtable.remove(tag); // remove old highlighting |
|---|
| 71 | |
|---|
| 72 | /* move the cursor and scroll the textview */ |
|---|
| 73 | if (backwards) buffer.place_cursor(match_start); |
|---|
| 74 | else buffer.place_cursor(match_end); |
|---|
| 75 | textview.scroll_to_iter(match_start, 0.0, true, 0, 0); |
|---|
| 76 | |
|---|
| 77 | /* highlight the new result */ |
|---|
| 78 | tagtable.add(tag); |
|---|
| 79 | buffer.apply_tag(tag, match_start, match_end); |
|---|
| 80 | |
|---|
| 81 | } else if (oldfound) found = true; // no new results, keep old one |
|---|
| 82 | |
|---|
| 83 | } else { // close dialog |
|---|
| 84 | dialog.destroy(); |
|---|
| 85 | if (found) tagtable.remove(tag); |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /* initialize the find dialog */ |
|---|
| 90 | var dialog = new Gtk.Dialog(); // let |
|---|
| 91 | dialog.set_default_response(Gtk.ResponseType.APPLY); |
|---|
| 92 | dialog.set_title("Find"); |
|---|
| 93 | |
|---|
| 94 | var backwards = false; // find direction // let |
|---|
| 95 | var found = false; // are there currently any results // let |
|---|
| 96 | |
|---|
| 97 | /* initialize variables related to textview */ |
|---|
| 98 | var buffer = textview.get_buffer(); // let |
|---|
| 99 | var tagtable = buffer.get_tag_table(); // let |
|---|
| 100 | var tag = new Gtk.TextTag({ name:"found", background:"green" }); // let |
|---|
| 101 | var match_start = new Gtk.TextIter(); // let |
|---|
| 102 | var match_end = new Gtk.TextIter(); // let |
|---|
| 103 | |
|---|
| 104 | /* add widgets to the dialog */ |
|---|
| 105 | var vbox = dialog.get_content_area(); // let |
|---|
| 106 | |
|---|
| 107 | var back = new Gtk.CheckButton.with_mnemonic("Search _backwards"); // let |
|---|
| 108 | back.signal.toggled.connect(function() { backwards = !backwards; }); |
|---|
| 109 | vbox.pack_start(back, false, false, 0); |
|---|
| 110 | |
|---|
| 111 | var entry = new Gtk.Entry(); // let |
|---|
| 112 | entry.signal.activate.connect(button_pressed, Gtk.ResponseType.APPLY); |
|---|
| 113 | vbox.pack_start(entry, false, false, 0); |
|---|
| 114 | |
|---|
| 115 | dialog.add_button(Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE); |
|---|
| 116 | dialog.add_button(Gtk.STOCK_FIND, Gtk.ResponseType.APPLY); |
|---|
| 117 | |
|---|
| 118 | dialog.signal.response.connect(button_pressed); |
|---|
| 119 | |
|---|
| 120 | entry.grab_focus(); |
|---|
| 121 | dialog.show_all(); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | /** |
|---|
| 126 | * Creates and displays the "Save" dialog. |
|---|
| 127 | */ |
|---|
| 128 | function saveDialog(widget) |
|---|
| 129 | { |
|---|
| 130 | var dialog = new Gtk.FileChooserDialog({ title: "Save file" }); // let |
|---|
| 131 | dialog.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL).grab_focus(); |
|---|
| 132 | dialog.add_button(Gtk.STOCK_SAVE, Gtk.ResponseType.OK); |
|---|
| 133 | dialog.set_action(Gtk.FileChooserAction.SAVE); |
|---|
| 134 | dialog.set_default_response(Gtk.ResponseType.CANCEL); |
|---|
| 135 | dialog.set_do_overwrite_confirmation(true); |
|---|
| 136 | |
|---|
| 137 | var response = dialog.run(); // run the dialog as a modal dialog // let |
|---|
| 138 | |
|---|
| 139 | if (response == Gtk.ResponseType.OK) { // save the file |
|---|
| 140 | var filename = dialog.get_filename(); // let |
|---|
| 141 | var buffer = textview.get_buffer(); // let |
|---|
| 142 | var startiter = new Gtk.TextIter(); // let |
|---|
| 143 | buffer.get_start_iter(startiter); |
|---|
| 144 | var enditer = new Gtk.TextIter(); // let |
|---|
| 145 | buffer.get_end_iter(enditer); |
|---|
| 146 | var buf = buffer.get_text(startiter, enditer, false); // let |
|---|
| 147 | // Gio.simple_write(filename, buf); |
|---|
| 148 | var file = Gio.file_new_for_path(filename); // let |
|---|
| 149 | var fstream = file.replace(); // let |
|---|
| 150 | var dstream = new Gio.DataOutputStream.c_new(fstream); // let |
|---|
| 151 | dstream.put_string(buf); |
|---|
| 152 | fstream.close(); |
|---|
| 153 | } |
|---|
| 154 | dialog.destroy(); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | /** |
|---|
| 159 | * Creates and displays the "Open" dialog. |
|---|
| 160 | */ |
|---|
| 161 | function openDialog(widget) |
|---|
| 162 | { |
|---|
| 163 | var dialog = new Gtk.FileChooserDialog({ title: "Open file" }); // let |
|---|
| 164 | dialog.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL).grab_focus(); |
|---|
| 165 | dialog.add_button(Gtk.STOCK_OPEN, Gtk.ResponseType.OK); |
|---|
| 166 | dialog.set_default_response(Gtk.ResponseType.CANCEL); |
|---|
| 167 | dialog.set_action(Gtk.FileChooserAction.OPEN); |
|---|
| 168 | |
|---|
| 169 | var response = dialog.run(); // let |
|---|
| 170 | |
|---|
| 171 | if (response == Gtk.ResponseType.OK) { // load text from file |
|---|
| 172 | var filename = dialog.get_filename(); // let |
|---|
| 173 | // var text = Gio.simple_read(filename); // let |
|---|
| 174 | var file = Gio.file_new_for_path(filename); // let |
|---|
| 175 | var fstream = file.read(); // let |
|---|
| 176 | var dstream = new Gio.DataInputStream.c_new(fstream); // let |
|---|
| 177 | var text = dstream.read_until("", 0); // let |
|---|
| 178 | fstream.close(); |
|---|
| 179 | var buffer = textview.get_buffer(); // let |
|---|
| 180 | buffer.set_text(text, -1); |
|---|
| 181 | } |
|---|
| 182 | dialog.destroy(); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | /** |
|---|
| 187 | * Clears the textview buffer. |
|---|
| 188 | */ |
|---|
| 189 | function newText() |
|---|
| 190 | { |
|---|
| 191 | var buffer = textview.get_buffer(); // let |
|---|
| 192 | buffer.set_text("", 0); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | /** |
|---|
| 197 | * Cuts to clipboard. |
|---|
| 198 | */ |
|---|
| 199 | function cut_to_clipboard() |
|---|
| 200 | { |
|---|
| 201 | var buffer = textview.get_buffer(); // let |
|---|
| 202 | buffer.cut_clipboard(clipboard, true); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | /** |
|---|
| 207 | * Copies to clipboard. |
|---|
| 208 | */ |
|---|
| 209 | function copy_to_clipboard() |
|---|
| 210 | { |
|---|
| 211 | var buffer = textview.get_buffer(); // let |
|---|
| 212 | buffer.copy_clipboard(clipboard); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | /** |
|---|
| 217 | * Pastes from clipboard. |
|---|
| 218 | */ |
|---|
| 219 | function paste_from_clipboard() |
|---|
| 220 | { |
|---|
| 221 | var buffer = textview.get_buffer(); // let |
|---|
| 222 | var iter = new Gtk.TextIter(); // let |
|---|
| 223 | buffer.get_start_iter(iter); |
|---|
| 224 | iter.set_offset(buffer.cursor_position); |
|---|
| 225 | buffer.paste_clipboard(clipboard, iter, true); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | /* "main" section - execution starts here */ |
|---|
| 230 | Gtk.disable_setlocale(); // disable localization |
|---|
| 231 | Gtk.init(null, null); |
|---|
| 232 | |
|---|
| 233 | /* main window and layout box */ |
|---|
| 234 | var window = new Gtk.Window({ title: "Seeded", resizable: true }); |
|---|
| 235 | window.resize(800, 600); |
|---|
| 236 | var mainvbox = new Gtk.VBox(); |
|---|
| 237 | window.add(mainvbox); |
|---|
| 238 | |
|---|
| 239 | /* initialize the clipboard */ |
|---|
| 240 | var atom = Gdk.atom_intern("CLIPBOARD", false); |
|---|
| 241 | var clipboard = window.get_clipboard(atom); |
|---|
| 242 | |
|---|
| 243 | /* actions that can be used as menu or toolbar items */ |
|---|
| 244 | var actions = new Gtk.ActionGroup({ name: "default" }); |
|---|
| 245 | var accels = new Gtk.AccelGroup(); // hotkeys |
|---|
| 246 | window.add_accel_group(accels); |
|---|
| 247 | |
|---|
| 248 | /* "new" */ |
|---|
| 249 | var new_action = new Gtk.Action({ name: "new", |
|---|
| 250 | label: "New", |
|---|
| 251 | tooltip: "New File", |
|---|
| 252 | stock_id: Gtk.STOCK_NEW }); |
|---|
| 253 | new_action.set_accel_group(accels); |
|---|
| 254 | actions.add_action_with_accel(new_action); |
|---|
| 255 | new_action.connect_accelerator(); |
|---|
| 256 | new_action.signal.activate.connect(newText); |
|---|
| 257 | |
|---|
| 258 | /* "open" */ |
|---|
| 259 | var open_action = new Gtk.Action({ name: "open", |
|---|
| 260 | label: "Open", |
|---|
| 261 | tooltip: "Open File", |
|---|
| 262 | stock_id: Gtk.STOCK_OPEN }); |
|---|
| 263 | open_action.set_accel_group(accels); |
|---|
| 264 | actions.add_action_with_accel(open_action); |
|---|
| 265 | open_action.connect_accelerator(); |
|---|
| 266 | open_action.signal.activate.connect(openDialog); |
|---|
| 267 | |
|---|
| 268 | /* "save" */ |
|---|
| 269 | var save_action = new Gtk.Action({ name: "save", |
|---|
| 270 | label: "Save", |
|---|
| 271 | tooltip: "Save File", |
|---|
| 272 | stock_id: Gtk.STOCK_SAVE }); |
|---|
| 273 | save_action.set_accel_group(accels); |
|---|
| 274 | actions.add_action_with_accel(save_action); |
|---|
| 275 | save_action.connect_accelerator(); |
|---|
| 276 | save_action.signal.activate.connect(saveDialog); |
|---|
| 277 | |
|---|
| 278 | /* "quit" */ |
|---|
| 279 | var quit_action = new Gtk.Action({ name: "quit", |
|---|
| 280 | label: "Quit", |
|---|
| 281 | tooltip: "Quit", |
|---|
| 282 | stock_id: Gtk.STOCK_QUIT }); |
|---|
| 283 | quit_action.set_accel_group(accels); |
|---|
| 284 | actions.add_action_with_accel(quit_action); |
|---|
| 285 | quit_action.connect_accelerator(); |
|---|
| 286 | quit_action.signal.activate.connect(Gtk.main_quit); |
|---|
| 287 | |
|---|
| 288 | /* "find" */ |
|---|
| 289 | var find_action = new Gtk.Action({ name: "find", |
|---|
| 290 | label: "Find", |
|---|
| 291 | tooltip: "Find text", |
|---|
| 292 | stock_id:Gtk.STOCK_FIND }); |
|---|
| 293 | find_action.set_accel_group(accels); |
|---|
| 294 | actions.add_action_with_accel(find_action); |
|---|
| 295 | find_action.connect_accelerator(); |
|---|
| 296 | find_action.signal.activate.connect(findDialog); |
|---|
| 297 | |
|---|
| 298 | /* "evaluate" */ |
|---|
| 299 | var eval_action = new Gtk.Action({ name: "eval", |
|---|
| 300 | label: "Evaluate", |
|---|
| 301 | tooltip: "Evaluate as JavaScript", |
|---|
| 302 | stock_id: Gtk.STOCK_EXECUTE }); |
|---|
| 303 | eval_action.set_accel_group(accels); |
|---|
| 304 | actions.add_action_with_accel(eval_action, "<Control>e"); |
|---|
| 305 | eval_action.connect_accelerator(); |
|---|
| 306 | eval_action.signal.activate.connect(evaluateJavascript); |
|---|
| 307 | |
|---|
| 308 | /* "cut" */ |
|---|
| 309 | var cut_action = new Gtk.Action({ name: "cut", |
|---|
| 310 | label: "Cut", |
|---|
| 311 | tooltip: "Cut to clipboard", |
|---|
| 312 | stock_id: Gtk.STOCK_CUT }); |
|---|
| 313 | cut_action.set_accel_group(accels); |
|---|
| 314 | actions.add_action_with_accel(cut_action); |
|---|
| 315 | cut_action.connect_accelerator(); |
|---|
| 316 | cut_action.signal.activate.connect(cut_to_clipboard); |
|---|
| 317 | |
|---|
| 318 | /* "copy" */ |
|---|
| 319 | var copy_action = new Gtk.Action({ name: "copy", |
|---|
| 320 | label: "Copy", |
|---|
| 321 | tooltip: "Copy to clipboard", |
|---|
| 322 | stock_id: Gtk.STOCK_COPY }); |
|---|
| 323 | copy_action.set_accel_group(accels); |
|---|
| 324 | actions.add_action_with_accel(copy_action); |
|---|
| 325 | copy_action.connect_accelerator(); |
|---|
| 326 | copy_action.signal.activate.connect(copy_to_clipboard); |
|---|
| 327 | |
|---|
| 328 | /* "paste" */ |
|---|
| 329 | var paste_action = new Gtk.Action({ name: "paste", |
|---|
| 330 | label: "Paste", |
|---|
| 331 | tooltip: "Paste from clipboard", |
|---|
| 332 | stock_id: Gtk.STOCK_PASTE }); |
|---|
| 333 | paste_action.set_accel_group(accels); |
|---|
| 334 | actions.add_action_with_accel(paste_action); |
|---|
| 335 | paste_action.connect_accelerator(); |
|---|
| 336 | paste_action.signal.activate.connect(paste_from_clipboard); |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | /* main menubar */ |
|---|
| 340 | var menu = new Gtk.MenuBar(); |
|---|
| 341 | mainvbox.pack_start(menu, false, false, 0); |
|---|
| 342 | |
|---|
| 343 | /* "File" */ |
|---|
| 344 | var file = new Gtk.MenuItem.with_mnemonic("_File"); |
|---|
| 345 | var file_menu = new Gtk.Menu(); |
|---|
| 346 | file.set_submenu(file_menu); |
|---|
| 347 | menu.append(file); |
|---|
| 348 | file_menu.append(new_action.create_menu_item(), -1); |
|---|
| 349 | file_menu.append(open_action.create_menu_item(), -1); |
|---|
| 350 | file_menu.append(save_action.create_menu_item(), -1); |
|---|
| 351 | file_menu.append(new Gtk.SeparatorMenuItem(), -1); |
|---|
| 352 | file_menu.append(eval_action.create_menu_item(), -1); |
|---|
| 353 | file_menu.append(new Gtk.SeparatorMenuItem(), -1); |
|---|
| 354 | file_menu.append(quit_action.create_menu_item(), -1); |
|---|
| 355 | |
|---|
| 356 | /* "Edit" */ |
|---|
| 357 | var edit = new Gtk.MenuItem.with_mnemonic("_Edit"); |
|---|
| 358 | var edit_menu = new Gtk.Menu(); |
|---|
| 359 | edit.set_submenu(edit_menu); |
|---|
| 360 | edit_menu.append(find_action.create_menu_item(), -1); |
|---|
| 361 | edit_menu.append(new Gtk.SeparatorMenuItem(), -1); |
|---|
| 362 | edit_menu.append(cut_action.create_menu_item(),-1); |
|---|
| 363 | edit_menu.append(copy_action.create_menu_item(),-1); |
|---|
| 364 | edit_menu.append(paste_action.create_menu_item(),-1); |
|---|
| 365 | menu.append(edit); |
|---|
| 366 | |
|---|
| 367 | /* "Help" */ |
|---|
| 368 | var help = new Gtk.MenuItem.with_mnemonic("_Help"); |
|---|
| 369 | var help_menu = new Gtk.Menu(); |
|---|
| 370 | help.set_submenu(help_menu); |
|---|
| 371 | var about = new Gtk.MenuItem.with_mnemonic("_About"); |
|---|
| 372 | about.signal.activate.connect(aboutDialog,window); |
|---|
| 373 | help_menu.append(about); |
|---|
| 374 | menu.append(help); |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | /* top toolbar */ |
|---|
| 378 | var toolbar = new Gtk.Toolbar(); |
|---|
| 379 | toolbar.insert(eval_action.create_tool_item()); |
|---|
| 380 | toolbar.insert(find_action.create_tool_item()); |
|---|
| 381 | toolbar.insert(new Gtk.SeparatorToolItem()); |
|---|
| 382 | toolbar.insert(paste_action.create_tool_item()); |
|---|
| 383 | toolbar.insert(copy_action.create_tool_item()); |
|---|
| 384 | toolbar.insert(cut_action.create_tool_item()); |
|---|
| 385 | toolbar.insert(new Gtk.SeparatorToolItem()); |
|---|
| 386 | toolbar.insert(quit_action.create_tool_item()); |
|---|
| 387 | toolbar.insert(save_action.create_tool_item()); |
|---|
| 388 | toolbar.insert(open_action.create_tool_item()); |
|---|
| 389 | toolbar.insert(new_action.create_tool_item()); |
|---|
| 390 | mainvbox.pack_start(toolbar, false, false, 0); |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | /* main text area */ |
|---|
| 394 | var scrollwin = new Gtk.ScrolledWindow(); |
|---|
| 395 | mainvbox.pack_start(scrollwin,true,true,0); |
|---|
| 396 | var textview = new Gtk.TextView(); |
|---|
| 397 | scrollwin.add(textview); |
|---|
| 398 | textview.grab_focus(); |
|---|
| 399 | |
|---|
| 400 | /* status bar */ |
|---|
| 401 | var statusbar = new Gtk.Statusbar(); |
|---|
| 402 | mainvbox.pack_start(statusbar,false,false,0); |
|---|
| 403 | window.signal.hide.connect(Gtk.main_quit); |
|---|
| 404 | window.show_all(); |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | /* start GTK+ event loop*/ |
|---|
| 408 | Gtk.main(); |
|---|