From dc09e6918c7e1e9c506eea4df16d1b06d84009d2 Mon Sep 17 00:00:00 2001 From: elvez Date: Thu, 24 Aug 2006 00:28:19 +0000 Subject: [PATCH] - First experiment with pango markup on the tree widget: it looks odd but it works! git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@935 3ecf2c5c-341e-0410-92b4-d18e462d057c --- src/schedulables_tree_widget.cc | 98 +++++++++++++++++++++++-- src/schedulables_tree_widget.hh | 22 +++++- src/testsuite/test-simulation_widget.cc | 2 +- 3 files changed, 112 insertions(+), 10 deletions(-) diff --git a/src/schedulables_tree_widget.cc b/src/schedulables_tree_widget.cc index fb86fcc..1db0f12 100644 --- a/src/schedulables_tree_widget.cc +++ b/src/schedulables_tree_widget.cc @@ -31,6 +31,8 @@ #include #include +#include + #include #include #include @@ -41,6 +43,12 @@ using namespace Gtk; using namespace Glib; using Gnome::Glade::Xml; +PropertyProxy_Base +SchedulablesTreeWidget::CellRendererTextMarkup::_property_renderable() +{ + return Glib::PropertyProxy_Base(this, "markup"); +} + SchedulablesTreeWidget::SchedulablesTreeWidget() : _add_process_dialog_glade(Xml::create(GLADEDIR "/add-process-dialog.glade")), _add_thread_dialog_glade(Xml::create(GLADEDIR "/add-thread-dialog.glade")), @@ -54,10 +62,13 @@ SchedulablesTreeWidget::SchedulablesTreeWidget() : set_model(_model); - append_column("schedulables", _main_column); + //append_column("schedulables", _main_column); //invisible // append_column("handles", _types_column); // append_column("handles", _handles_column); + int idx = append_column("schedulables", _cell_renderer) - 1; + Gtk::TreeViewColumn* tvc = get_column(idx); + tvc->set_cell_data_func(_cell_renderer, sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_cell_name_data)); /** DIALOGS **/ _add_process_dialog_glade->get_widget("AddProcessDialog", _add_process_dialog); @@ -119,12 +130,19 @@ T* SchedulablesTreeWidget::get_selected() { TreeModel::iterator sel = get_selection()->get_selected(); + + return get_row_handle_as(sel); +} - if(!sel) +template +T* +SchedulablesTreeWidget::get_row_handle_as(const Gtk::TreeModel::iterator& row) +{ + if(!row) return NULL; - const void* p_handle = (*sel)[_handles_column]; - HandleType type = (*sel)[_types_column]; + const void* p_handle = (*row)[_handles_column]; + HandleType type = (*row)[_types_column]; if(!check_type(type)) return NULL; @@ -137,12 +155,19 @@ SchedulablesTreeWidget::get_selection_type() { TreeModel::iterator sel = get_selection()->get_selected(); - if(!sel) + return get_row_type(sel); +} + +SchedulablesTreeWidget::HandleType +SchedulablesTreeWidget::get_row_type(const TreeModel::iterator& row) +{ + if(!row) return htype_undefined; else - return (*sel)[_types_column]; + return (*row)[_types_column]; } + bool SchedulablesTreeWidget::on_button_press_event(GdkEventButton* event) { @@ -406,3 +431,64 @@ SchedulablesTreeWidget::_on_remove_subrequest() Simulation::get_instance().get_history().remove(owner); } +void +SchedulablesTreeWidget::_on_cell_name_data(Gtk::CellRenderer* cr, + const Gtk::TreeModel::iterator& it) +{ + CellRendererTextMarkup& crtm = reinterpret_cast(*cr); + + ustring marked_up; + + switch(get_row_type(it)) + { + case htype_process: + marked_up = markup_process(*get_row_handle_as(it)); + break; + default: + marked_up = ""; + marked_up += Markup::escape_text(it->get_value(_main_column)); + marked_up += ""; + } + + crtm.property_markup() = marked_up; +} + +ustring +SchedulablesTreeWidget::markup_process(const Process& p) +{ + using std::ostringstream; + using std::endl; + + ustring color; + + switch(p.get_state()) + { + case Process::state_running: + color = "green"; + break; + case Process::state_ready: + color = "yellow"; + break; + case Process::state_blocked: + color = "red"; + break; + case Process::state_terminated: + color = "grey"; + break; + case Process::state_future: + color = "LightSeaGreen"; + break; + } + + ostringstream oss; + + oss << "" << Markup::escape_text(p.get_name()) << + " / arrived at: " << p.get_arrival_time() << + " / base priority: " << p.get_base_priority() << endl; + oss << "elapsed: " << p.get_elapsed_time() << "" << endl; + oss << "priority: " << p.get_current_priority() << ""; + + // TODO I'm unable to set foreground color, why? + return ustring("" + oss.str() + ""; +} + diff --git a/src/schedulables_tree_widget.hh b/src/schedulables_tree_widget.hh index 51eadae..2df1d37 100644 --- a/src/schedulables_tree_widget.hh +++ b/src/schedulables_tree_widget.hh @@ -54,8 +54,7 @@ namespace sgpem virtual void update(const History& history); - bool on_button_press_event(GdkEventButton* event); - + virtual bool on_button_press_event(GdkEventButton* event); private: enum HandleType @@ -66,13 +65,24 @@ namespace sgpem htype_subrequest, htype_undefined }; - + + class CellRendererTextMarkup : public Gtk::CellRendererText + { + public: + Glib::PropertyProxy_Base _property_renderable(); + }; + template bool check_type(HandleType type); template T* get_selected(); + + template + T* get_row_handle_as(const Gtk::TreeModel::iterator& row); + HandleType get_selection_type(); + HandleType get_row_type(const Gtk::TreeModel::iterator& row); void _on_add_process(); void _on_add_thread(); @@ -83,6 +93,11 @@ namespace sgpem void _on_remove_request(); void _on_remove_subrequest(); + void _on_cell_name_data(Gtk::CellRenderer* cr, + const Gtk::TreeModel::iterator& it); + + Glib::ustring markup_process(const Process& p); + Glib::RefPtr _model; Gtk::TreeModelColumnRecord _columns; Gtk::TreeModelColumn _main_column; @@ -95,6 +110,7 @@ namespace sgpem Gtk::Dialog* _add_process_dialog; Gtk::Dialog* _add_thread_dialog; AddRequestDialog* _add_request_dialog; + CellRendererTextMarkup _cell_renderer; }; } //~ namespace sgpem diff --git a/src/testsuite/test-simulation_widget.cc b/src/testsuite/test-simulation_widget.cc index 68c2b2c..d08d9f0 100644 --- a/src/testsuite/test-simulation_widget.cc +++ b/src/testsuite/test-simulation_widget.cc @@ -488,7 +488,7 @@ void fillHistory(History &hist) Thread& p1_t1 = hist.add_thread(Glib::ustring("P1 - Thread 1"), p1, 14, 0, 2); // add a thread - name, parent, cpu time, arrival time, priority - // Thread& p1_t2 = hist.add_thread(Glib::ustring("P1 - Thread 2"), p1, 3, 0, 5); + Thread& p1_t2 = hist.add_thread(Glib::ustring("P1 - Thread 2"), p1, 8, 0, 5); // add a thread - name, parent, cpu time, arrival time, priority Thread& p2_t1 = hist.add_thread(Glib::ustring("P2 - Thread 1"), p2, 20, 0, 2);