/* * Copyright 2010 Joakim Sindholt * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * on the rights to use, copy, modify, merge, publish, distribute, sub * license, and/or sell copies of the Software, and to permit persons to whom * the Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * USE OR OTHER DEALINGS IN THE SOFTWARE. */ using Gtk; namespace GUI { class CSImport : Dialog { private static string xml_ui = """ True vertical True 0 none True 12 True True True <b>Name</b> True False 0 True 0 none True 12 True True automatic automatic etched-in True True meta_store Value Meta 0 True <b>DWORDs</b> True 1 True 5 3 True True 0 True end Load True True True image1 False False 0 False 1 False False 2 True gtk-convert """; private static string[] packets = { "packet0", "packet1", "packet2", "packet3" }; private Entry name_entry; private Label status_label; private ListStore meta_store; private Button ok_button; private Array dwords; private bool validcs; public CSImport() { var builder = new Builder(); try { builder.add_from_string(xml_ui, xml_ui.length); } catch (Error e) { stderr.printf("Unable to construct import dialog.\n"); return; } set_transient_for(main); title = "Import CS"; var col = builder.get_object("value_column") as TreeViewColumn; var cell = builder.get_object("value_cell") as CellRendererText; col.set_cell_data_func(cell, (coll, celll, model, iter) => { var path = meta_store.get_path(iter); assert(path != null); int *indices = path.get_indices(); assert(indices[0] < dwords.length); (celll as CellRendererText).text = dwords.index(indices[0]).to_string("0x%08X"); }); meta_store = builder.get_object("meta_store") as ListStore; add_button(STOCK_CANCEL, ResponseType.CANCEL); ok_button = add_button(STOCK_OK, ResponseType.OK) as Button; ok_button.sensitive = false; var load_button = builder.get_object("load_button") as Button; load_button.clicked.connect((source) => { var fc = new FileChooserDialog("Open File", this, FileChooserAction.OPEN, STOCK_CANCEL, ResponseType.CANCEL, STOCK_OPEN, ResponseType.OK, null); if (fc.run() == ResponseType.OK) { open_cs(fc.get_filename()); } fc.destroy(); }); validcs = false; dwords = new Array(false, false, (uint)sizeof(uint32)); name_entry = builder.get_object("name_entry") as Entry; name_entry.changed.connect(update_ok_button); Gdk.Color color = Gdk.Color(); color.red = 0xFFFF; color.green = 0x0000; color.blue = 0x0000; status_label = builder.get_object("status_label") as Label; status_label.modify_fg(StateType.NORMAL, color); vbox.pack_start(builder.get_object("mainbox") as VBox, true, true, 0); response.connect((source, id) => { if (id == ResponseType.OK) { main.add_cs(name_entry.text, new Emulation.CS(&dwords.index(0), dwords.length)); } destroy(); }); set_default_size(300, 400); } private void open_cs(string filename) { FileStream fs = FileStream.open(filename, "r"); if (fs == null) { var dlg = new MessageDialog(this, DialogFlags.MODAL, MessageType.ERROR, ButtonsType.OK, "Unable to open the selected file: " + filename); dlg.run(); dlg.destroy(); return; } meta_store.clear(); dwords.set_size(0); uint32 dword; uint dword_count = 0; for (uint id = 0; fs.scanf("%x", out dword) == 1; id++) { dwords.append_val(dword); TreeIter iter; meta_store.append(out iter); if (dword_count > 0) { dword_count--; meta_store.set(iter, 0, null, -1); } else { Emulation.PacketBase p = Emulation.make_packet(dword); dword_count = p.length; meta_store.set(iter, 0, packets[p.type_id], -1); } } validcs = (dword_count == 0); update_ok_button(); } private void update_ok_button() { if (validcs) { ok_button.sensitive = (name_entry.text_length > 0 && dwords.length > 0); status_label.label = ""; } else { if (ok_button.sensitive) { ok_button.sensitive = false; status_label.label = "This CS is invalid!"; } } } } }