1 /************************************************************************** 2 Copyright: 3 (C) 2008 - 2012 Alexander Shaduri <ashaduri 'at' gmail.com> 4 License: See LICENSE_gsmartcontrol.txt 5 ***************************************************************************/ 6 /// \file 7 /// \author Alexander Shaduri 8 /// \ingroup applib 9 /// \weakgroup applib 10 /// @{ 11 12 #ifndef APP_GTKMM_UTILS_H 13 #define APP_GTKMM_UTILS_H 14 15 #include <string> 16 #include <gtkmm.h> 17 18 #include "hz/down_cast.h" 19 20 21 22 /// Get column header widget of a tree view column. 23 /// Note: This works only if the column has custom widget set. 24 Gtk::Widget* app_gtkmm_get_column_header(Gtk::TreeViewColumn& column); 25 26 27 /// Read column header text and create a label with that text. Set the label as 28 /// column's custom widget and return it. 29 Gtk::Widget* app_gtkmm_labelize_column(Gtk::TreeViewColumn& column); 30 31 32 /// A wrapper around set_tooltip_*() for portability across different gtkmm versions. 33 void app_gtkmm_set_widget_tooltip(Gtk::Widget& widget, 34 const Glib::ustring& tooltip_text, bool use_markup = false); 35 36 37 38 /// Convenience function for creating a TreeViewColumn . 39 template<typename T> 40 int app_gtkmm_create_tree_view_column(Gtk::TreeModelColumn<T>& mcol, Gtk::TreeView& treeview, 41 const Glib::ustring& title, const Glib::ustring& tooltip_text, bool sortable = false, bool cell_markup = false) 42 { 43 int num_tree_cols = treeview.append_column(title, mcol); 44 Gtk::TreeViewColumn* tcol = treeview.get_column(num_tree_cols - 1); 45 if (tcol) { 46 if (sortable) 47 tcol->set_sort_column(mcol); 48 49 app_gtkmm_labelize_column(*tcol); 50 tcol->set_reorderable(true); 51 tcol->set_resizable(true); 52 } 53 54 Gtk::Widget* header = app_gtkmm_get_column_header(*tcol); 55 if (header) 56 app_gtkmm_set_widget_tooltip(*header, tooltip_text); 57 58 if (cell_markup) { 59 Gtk::CellRendererText* cr_type = hz::down_cast<Gtk::CellRendererText*>(treeview.get_column_cell_renderer(num_tree_cols - 1)); 60 if (cr_type) { // may not be true if it's not Text (unless static_cast is used, in which case we're screwed) 61 treeview.get_column(num_tree_cols - 1)->clear_attributes(*cr_type); // clear "text" attribute. "markup" won't work without this. 62 treeview.get_column(num_tree_cols - 1)->add_attribute(cr_type->property_markup(), mcol); // render col_type as markup. 63 } 64 } 65 66 return num_tree_cols; 67 } 68 69 70 71 /// Get Glib::ustring from gchar*, freeing gchar*. 72 Glib::ustring app_ustring_from_gchar(gchar* str); 73 74 75 /// Convert a possibly invalid utf-8 string to valid utf-8. 76 /// \param str string to test and fix. 77 Glib::ustring app_utf8_make_valid(const Glib::ustring& str); 78 79 80 /// Make command output a valid utf-8 string. Essentially, this calls app_utf8_make_valid(), 81 /// supplying true for \c in_locale under Win32, and false under other systems. 82 /// The reason for this is that in Win32 we can't execute commands under C locale, 83 /// but we do execute them under C in other systems. 84 Glib::ustring app_output_make_valid(const Glib::ustring& str); 85 86 87 88 89 #endif 90 91 /// @} 92