#include #include #include #include ////////////////////////////////////////////////////////////// class GUI_output : public Gtk::ScrolledWindow { public: GUI_output (); void update (); protected: Gtk::TextView view; std::ostringstream outs; Glib::RefPtr buffer; }; GUI_output::GUI_output () { std::cout.rdbuf (outs.rdbuf()); std::cerr.rdbuf (outs.rdbuf()); buffer = Gtk::TextBuffer::create(); view.set_buffer (buffer); view.set_editable (false); add (view); set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); set_shadow_type (Gtk::SHADOW_IN); } void GUI_output::update () { // Update text output //const int prev_line_count = buffer->get_line_count (); buffer->set_text (outs.str ()); //const int new_line_count = buffer->get_line_count (); // Update vertical scroll bar /* Gtk::Adjustment *sb = get_vadjustment (); const int new_val = sb->get_upper () + (new_line_count - prev_line_count); sb->set_upper (new_val); sb->set_value (new_val); set_vadjustment (*sb); */ } ////////////////////////////////////////////////////////////// class var_and_function_list : public Gtk::ScrolledWindow { public: var_and_function_list (GUI_output &_output); virtual ~var_and_function_list (); void update (); protected: //Signal handlers: virtual void on_treeview_row_activated(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column); //Tree model columns: class ModelColumns : public Gtk::TreeModel::ColumnRecord { public: ModelColumns() { add(m_col_name); } Gtk::TreeModelColumn m_col_name; }; ModelColumns m_Columns; //Child widgets: Gtk::TreeView m_TreeView; Glib::RefPtr m_refTreeModel; Gtk::TreeModel::Row user_functions_row; Gtk::TreeModel::Row variable_row; GUI_output &output; }; var_and_function_list::var_and_function_list (GUI_output &_output) : output (_output) { //Add the TreeView, inside a ScrolledWindow, with the button underneath: add(m_TreeView); //Only show the scrollbars when they are necessary: set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); //Create the Tree model: m_refTreeModel = Gtk::TreeStore::create(m_Columns); m_TreeView.set_model(m_refTreeModel); //All the items to be reordered with drag-and-drop: //m_TreeView.set_reorderable(); // Build the TreeView's model user_functions_row = *(m_refTreeModel->append()); variable_row = *(m_refTreeModel->append()); user_functions_row[m_Columns.m_col_name] = "User Functions"; variable_row[m_Columns.m_col_name] = "User Variables"; //Add the TreeView's view columns: m_TreeView.append_column("Available Stuff", m_Columns.m_col_name); // Add data update (); //Connect signal: m_TreeView.signal_row_activated().connect(sigc::mem_fun(*this, &var_and_function_list::on_treeview_row_activated) ); show_all_children(); } void var_and_function_list::update () { string_vector names; octave_idx_type numel; for(Gtk::TreeModel::iterator child = user_functions_row.children().begin(); child ; child = m_refTreeModel->erase(*child)); for(Gtk::TreeModel::iterator child = variable_row.children().begin(); child ; child = m_refTreeModel->erase(*child)); names = fbi_sym_tab->user_function_name_list (); numel = names.numel (); for (octave_idx_type n = 0; n < numel; n++) { Gtk::TreeModel::Row childrow = *(m_refTreeModel->append(user_functions_row.children())); childrow[m_Columns.m_col_name] = names (n); } names = fbi_sym_tab->global_variable_name_list (); numel = names.numel (); for (octave_idx_type n = 0; n < numel; n++) { Gtk::TreeModel::Row childrow = *(m_refTreeModel->append(variable_row.children())); childrow[m_Columns.m_col_name] = names (n); } } var_and_function_list::~var_and_function_list() { } void var_and_function_list::on_treeview_row_activated(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* /* column */) { Gtk::TreeModel::iterator iter = m_refTreeModel->get_iter(path); Gtk::TreeModel::Row row = *iter; octave_value_list arg; const std::string name = row[m_Columns.m_col_name]; arg.append (octave_value(name)); feval("help", arg); output.update (); } ///////////////////Gtk::Adjustment::Adjustment/////////////////////////////////////////// class GUI_input : public Gtk::Entry { public: GUI_input (GUI_output &_output, var_and_function_list &_vaf_list); protected: //Signal handlers: virtual void input_eval (); //virtual bool on_key_press_event (GdkEventKey *event); GUI_output &output; var_and_function_list &vaf_list; }; GUI_input::GUI_input (GUI_output &_output, var_and_function_list &_vaf_list) : output (_output), vaf_list (_vaf_list) { // Connect signals signal_activate ().connect( sigc::mem_fun (*this, &GUI_input::input_eval) ); /* signal_key_press_event().connect(sigc::mem_fun(*this, &GUI_input::on_key_press_event) ); */ } /* bool GUI_input::on_key_press_event(GdkEventKey* event) { if (event->type != GDK_KEY_PRESS) return true; const std::string cmd = get_text (); bool handled = false; switch (event->keyval) { case GDK_Tab: std::cout << "TAB: " << cmd << std::endl; handled = true; break; case GDK_Up: std::cout << "UP: " << cmd << std::endl; handled = true; break; case GDK_Down: std::cout << "Down: " << cmd << std::endl; handled = true; break; } std::cout << handled << std::endl; if (handled) output.update (); return handled; } */ void GUI_input::input_eval () { const std::string cmd = get_text (); std::cout << ">> " << cmd << std::endl; output.update (); //feval("eval", octave_value (cmd)); int parse_status; try { //curr_sym_tab = top_level_sym_tab; reset_error_handler (); eval_string(cmd, false, parse_status); } catch (octave_interrupt_exception) { recover_from_exception (); std::cout << std::endl; error_state = -2; } catch (std::bad_alloc) { recover_from_exception (); std::cout << std::endl; error_state = -3; } set_text (""); output.update (); vaf_list.update (); } ////////////////////////////////////////////////////////////// class GUI : public Gtk::Window { public: GUI(); virtual ~GUI(); protected: //Child widgets: Gtk::HPaned h_box1; Gtk::VBox v_box1, right_vbox; GUI_output output; var_and_function_list vaf_list; GUI_input input; }; GUI::GUI() : vaf_list (output), input (output, vaf_list) { set_title("Simple GUI Test"); set_border_width(10); set_default_size(700, 500); // Add boxes add(h_box1); h_box1.add1 (vaf_list); h_box1.add2 (v_box1); v_box1.pack_start (right_vbox); // Add widgets right_vbox.pack_start (output); right_vbox.pack_start (input, false, true); h_box1.set_position (200); // Show it all show_all_children(); } GUI::~GUI() { } ////////////////////////////////////////////////////////////// DEFUN_DLD (simple_gui, args, , "") { octave_value_list retval; // Initialise GTK Gtk::Main kit(0, NULL); // Create GUI GUI window; // Start GUI Gtk::Main::run(window); return retval; }