| 1 |
/* AbiWord |
| 2 |
* Copyright (C) 2009,2012 Hubert Figuiere |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU General Public License |
| 6 |
* as published by the Free Software Foundation; either version 2 |
| 7 |
* of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 |
* 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "xap_GtkComboBoxHelpers.h" |
| 21 |
|
| 22 |
void XAP_makeGtkComboBoxText(GtkComboBox * combo, GType secondary) |
| 23 |
{ |
| 24 |
GtkListStore * store; |
| 25 |
if (secondary != G_TYPE_NONE) { |
| 26 |
store = gtk_list_store_new(2, G_TYPE_STRING, secondary); |
| 27 |
} |
| 28 |
else { |
| 29 |
store = gtk_list_store_new(1, G_TYPE_STRING); |
| 30 |
} |
| 31 |
gtk_combo_box_set_model(combo, GTK_TREE_MODEL(store)); |
| 32 |
|
| 33 |
gtk_cell_layout_clear(GTK_CELL_LAYOUT(combo)); |
| 34 |
GtkCellRenderer *cell = GTK_CELL_RENDERER(gtk_cell_renderer_text_new()); |
| 35 |
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), cell, TRUE); |
| 36 |
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), cell, |
| 37 |
"text", 0, NULL); |
| 38 |
} |
| 39 |
|
| 40 |
void XAP_makeGtkComboBoxText2(GtkComboBox * combo, GType secondary, |
| 41 |
GType tertiary) |
| 42 |
{ |
| 43 |
GtkListStore * store; |
| 44 |
store = gtk_list_store_new(3, G_TYPE_STRING, secondary, tertiary); |
| 45 |
gtk_combo_box_set_model(combo, GTK_TREE_MODEL(store)); |
| 46 |
|
| 47 |
gtk_cell_layout_clear(GTK_CELL_LAYOUT(combo)); |
| 48 |
GtkCellRenderer *cell = GTK_CELL_RENDERER(gtk_cell_renderer_text_new()); |
| 49 |
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), cell, TRUE); |
| 50 |
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), cell, |
| 51 |
"text", 0, NULL); |
| 52 |
} |
| 53 |
|
| 54 |
void XAP_populateComboBoxWithIndex(GtkComboBox * combo, |
| 55 |
const UT_GenericVector<const char*> & vec) |
| 56 |
{ |
| 57 |
GtkListStore *store = GTK_LIST_STORE(gtk_combo_box_get_model(combo)); |
| 58 |
GtkTreeIter iter; |
| 59 |
|
| 60 |
for(UT_sint32 i = 0; i < vec.getItemCount(); i++) { |
| 61 |
gtk_list_store_append(store, &iter); |
| 62 |
gtk_list_store_set(store, &iter, 0, vec[i], 1, i, -1); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
void XAP_appendComboBoxText(GtkComboBox* combo, const char* text) |
| 67 |
{ |
| 68 |
GtkTreeIter iter; |
| 69 |
GtkListStore *store = GTK_LIST_STORE(gtk_combo_box_get_model(combo)); |
| 70 |
gtk_list_store_append(store, &iter); |
| 71 |
gtk_list_store_set(store, &iter, 0, text, -1); |
| 72 |
} |
| 73 |
|
| 74 |
void XAP_appendComboBoxTextAndInt(GtkComboBox * combo, const char * text, |
| 75 |
int value) |
| 76 |
{ |
| 77 |
GtkTreeIter iter; |
| 78 |
GtkListStore *store = GTK_LIST_STORE(gtk_combo_box_get_model(combo)); |
| 79 |
gtk_list_store_append(store, &iter); |
| 80 |
gtk_list_store_set(store, &iter, 0, text, 1, value, -1); |
| 81 |
} |
| 82 |
|
| 83 |
void XAP_appendComboBoxTextAndString(GtkComboBox * combo, const char * text, |
| 84 |
const char * value) |
| 85 |
{ |
| 86 |
GtkTreeIter iter; |
| 87 |
GtkListStore *store = GTK_LIST_STORE(gtk_combo_box_get_model(combo)); |
| 88 |
gtk_list_store_append(store, &iter); |
| 89 |
gtk_list_store_set(store, &iter, 0, text, 1, value, -1); |
| 90 |
} |
| 91 |
|
| 92 |
void XAP_appendComboBoxTextAndStringString(GtkComboBox * combo, |
| 93 |
const char * text, |
| 94 |
const char * value, |
| 95 |
const char *value2) |
| 96 |
{ |
| 97 |
GtkTreeIter iter; |
| 98 |
GtkListStore *store = GTK_LIST_STORE(gtk_combo_box_get_model(combo)); |
| 99 |
gtk_list_store_append(store, &iter); |
| 100 |
gtk_list_store_set(store, &iter, 0, text, 1, value, 2, value2, -1); |
| 101 |
} |
| 102 |
|
| 103 |
void XAP_appendComboBoxTextAndIntString(GtkComboBox * combo, |
| 104 |
const char * text, |
| 105 |
const int value, |
| 106 |
const char *value2) |
| 107 |
{ |
| 108 |
GtkTreeIter iter; |
| 109 |
GtkListStore *store = GTK_LIST_STORE(gtk_combo_box_get_model(combo)); |
| 110 |
gtk_list_store_append(store, &iter); |
| 111 |
gtk_list_store_set(store, &iter, 0, text, 1, value, 2, value2, -1); |
| 112 |
} |
| 113 |
|
| 114 |
int XAP_comboBoxGetActiveInt(GtkComboBox * combo) |
| 115 |
{ |
| 116 |
int value = 0; |
| 117 |
GtkTreeIter iter; |
| 118 |
gtk_combo_box_get_active_iter(combo, &iter); |
| 119 |
GtkTreeModel *store = gtk_combo_box_get_model(combo); |
| 120 |
gtk_tree_model_get(store, &iter, 1, &value, -1); |
| 121 |
return value; |
| 122 |
} |
| 123 |
|
| 124 |
std::string XAP_comboBoxGetActiveText(GtkComboBox * combo) |
| 125 |
{ |
| 126 |
char* value = 0; |
| 127 |
GtkTreeIter iter; |
| 128 |
gtk_combo_box_get_active_iter(combo, &iter); |
| 129 |
GtkTreeModel *store = gtk_combo_box_get_model(combo); |
| 130 |
gtk_tree_model_get(store, &iter, 0, &value, -1); |
| 131 |
return value; |
| 132 |
} |
| 133 |
|
| 134 |
bool XAP_comboBoxSetActiveFromIntCol(GtkComboBox * combo, |
| 135 |
int col, int value) |
| 136 |
{ |
| 137 |
GtkTreeIter iter; |
| 138 |
GtkTreeModel *store = gtk_combo_box_get_model(combo); |
| 139 |
if(gtk_tree_model_get_iter_first(store, &iter)) { |
| 140 |
do { |
| 141 |
int v; |
| 142 |
gtk_tree_model_get(store, &iter, col, &v, -1); |
| 143 |
if(v == value) { |
| 144 |
gtk_combo_box_set_active_iter(combo, &iter); |
| 145 |
return true; |
| 146 |
} |
| 147 |
} while(gtk_tree_model_iter_next(store, &iter)); |
| 148 |
} |
| 149 |
return false; |
| 150 |
} |