| 1 | /* Copyright (C) 2006 The gtkmm Development Team |
| 2 | * |
| 3 | * This library is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of the GNU Lesser General Public |
| 5 | * License as published by the Free Software Foundation; either |
| 6 | * version 2.1 of the License, or(at your option) any later version. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public |
| 14 | * License along with this library; if not, see <https://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include <gtkmm/listviewtext.h> |
| 18 | |
| 19 | namespace Gtk |
| 20 | { |
| 21 | |
| 22 | ListViewText::TextModelColumns::TextModelColumns(guint columns_count) |
| 23 | : m_columns_count(columns_count) |
| 24 | { |
| 25 | m_columns = new Gtk::TreeModelColumn<Glib::ustring>[m_columns_count]; |
| 26 | |
| 27 | if(m_columns) |
| 28 | { |
| 29 | for(guint i = 0; i < m_columns_count; ++i) |
| 30 | { |
| 31 | add(m_columns[i]); |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | ListViewText::TextModelColumns::~TextModelColumns() noexcept |
| 37 | { |
| 38 | if(m_columns) |
| 39 | delete[] m_columns; |
| 40 | } |
| 41 | |
| 42 | guint ListViewText::TextModelColumns::get_num_columns() const |
| 43 | { |
| 44 | return m_columns_count; |
| 45 | } |
| 46 | |
| 47 | ListViewText::ListViewText(guint columns_count, bool editable, Gtk::SelectionMode mode) |
| 48 | : m_model_columns(columns_count) |
| 49 | { |
| 50 | char column_title[20]; |
| 51 | |
| 52 | // Create model |
| 53 | m_model = Gtk::ListStore::create(m_model_columns); |
| 54 | set_model(m_model); |
| 55 | |
| 56 | // Append columns |
| 57 | for(guint i = 0; i < columns_count; ++i) |
| 58 | { |
| 59 | //Get text for the number: |
| 60 | sprintf(column_title, "%d", i); |
| 61 | |
| 62 | if(editable) |
| 63 | append_column_editable(column_title, m_model_columns.m_columns[i]); |
| 64 | else |
| 65 | append_column(column_title, m_model_columns.m_columns[i]); |
| 66 | } |
| 67 | |
| 68 | // Set multiple or simple selection |
| 69 | get_selection()->set_mode(mode); |
| 70 | } |
| 71 | |
| 72 | ListViewText::~ListViewText() noexcept |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | void ListViewText::set_column_title(guint column, const Glib::ustring& title) |
| 77 | { |
| 78 | g_return_if_fail( column < get_columns().size() ); |
| 79 | |
| 80 | get_column(column)->set_title(title); |
| 81 | } |
| 82 | |
| 83 | Glib::ustring ListViewText::get_column_title(guint column) const |
| 84 | { |
| 85 | g_return_val_if_fail( column < get_columns().size(), "" ); //Using Glib::ustring() fails sometimes: Bug #352226 |
| 86 | |
| 87 | return get_column(column)->get_title(); |
| 88 | } |
| 89 | |
| 90 | guint ListViewText::append(const Glib::ustring& column_one_value) |
| 91 | { |
| 92 | Gtk::TreeModel::Row newRow = *(m_model->append()); |
| 93 | |
| 94 | newRow[m_model_columns.m_columns[0]] = column_one_value; |
| 95 | |
| 96 | return size() - 1; |
| 97 | } |
| 98 | |
| 99 | void ListViewText::prepend(const Glib::ustring& column_one_value) |
| 100 | { |
| 101 | Gtk::TreeModel::Row newRow = *(m_model->prepend()); |
| 102 | |
| 103 | newRow[m_model_columns.m_columns[0]] = column_one_value; |
| 104 | } |
| 105 | |
| 106 | void ListViewText::insert(guint row, const Glib::ustring& column_one_value) |
| 107 | { |
| 108 | g_return_if_fail( row < size() ); |
| 109 | |
| 110 | Gtk::ListStore::const_iterator iter = m_model->children()[row]; |
| 111 | Gtk::TreeModel::Row newRow = *(m_model->insert(iter)); |
| 112 | |
| 113 | if(!column_one_value.empty()) |
| 114 | { |
| 115 | newRow[m_model_columns.m_columns[0]] = column_one_value; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void ListViewText::clear_items() |
| 120 | { |
| 121 | m_model->clear(); |
| 122 | } |
| 123 | |
| 124 | Glib::ustring ListViewText::get_text(guint row, guint column) const |
| 125 | { |
| 126 | Glib::ustring result; |
| 127 | |
| 128 | g_return_val_if_fail( row < size(), result ); |
| 129 | |
| 130 | Gtk::TreeModel::iterator iter = m_model->children()[row]; |
| 131 | iter->get_value(column, result); |
| 132 | |
| 133 | return result; |
| 134 | } |
| 135 | |
| 136 | void ListViewText::set_text(guint row, guint column, const Glib::ustring& value) |
| 137 | { |
| 138 | g_return_if_fail( row < size() ); |
| 139 | |
| 140 | Gtk::TreeModel::iterator iter = m_model->children()[row]; |
| 141 | (*iter)->set_value(column, value); |
| 142 | } |
| 143 | |
| 144 | void ListViewText::set_text(guint row, const Glib::ustring& value) |
| 145 | { |
| 146 | g_return_if_fail( row < size() ); |
| 147 | |
| 148 | Gtk::TreeModel::iterator iter = m_model->children()[ row ]; |
| 149 | (*iter)->set_value(0, value); |
| 150 | } |
| 151 | |
| 152 | guint ListViewText::size() const |
| 153 | { |
| 154 | return (guint)m_model->children().size(); |
| 155 | } |
| 156 | |
| 157 | guint ListViewText::get_num_columns() const |
| 158 | { |
| 159 | return m_model_columns.get_num_columns(); |
| 160 | } |
| 161 | |
| 162 | ListViewText::SelectionList ListViewText::get_selected() |
| 163 | { |
| 164 | Glib::RefPtr<Gtk::TreeSelection> selected = get_selection(); |
| 165 | std::vector<TreeModel::Path> selectedRows = selected->get_selected_rows(); |
| 166 | |
| 167 | // Reserve space |
| 168 | SelectionList selectionList; |
| 169 | selectionList.reserve( selected->count_selected_rows() ); |
| 170 | |
| 171 | // Save selected rows |
| 172 | |
| 173 | for (std::vector<TreeModel::Path>::const_iterator iter = selectedRows.begin(); iter != selectedRows.end(); ++iter) |
| 174 | { |
| 175 | selectionList.push_back( *((*iter).begin()) ); |
| 176 | } |
| 177 | |
| 178 | return selectionList; |
| 179 | } |
| 180 | |
| 181 | } //namespace Gtk |