clean code and changed version nro.
[irreco.git] / irreco / src / core / irreco_listbox_text.c
blob8db5af0a3d20d054cd07d82e8c78acb8b7836587
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2007 Arto Karppinen (arto.karppinen@iki.fi)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "irreco_listbox_text.h"
22 /**
23 * @addtogroup IrrecoListboxText
24 * @ingroup Irreco
26 * A single column, scrollable listbox with text cels.
28 * @{
31 /**
32 * @file
33 * Source file of @ref IrrecoListboxText.
36 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
37 /* Prototypes. */
38 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
40 static void irreco_listbox_text_tree_size_request(GtkWidget *widget,
41 GtkRequisition *requisition,
42 IrrecoListboxText *self);
44 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
45 /* Datatypes */
46 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
48 enum
50 DATA_COL,
51 TEXT_COL,
52 N_COLUMNS
57 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
58 /* Construction & Destruction */
59 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
61 /**
62 * @name Construction & Destruction
63 * @{
66 G_DEFINE_TYPE(IrrecoListboxText, irreco_listbox_text, IRRECO_TYPE_LISTBOX)
68 static void irreco_listbox_text_class_init(IrrecoListboxTextClass *klass)
72 static void irreco_listbox_text_init(IrrecoListboxText *self)
74 IrrecoListbox *parent;
75 GtkTreeViewColumn *column;
76 GtkCellRenderer *renderer;
77 GtkTable *table;
78 IRRECO_ENTER
80 parent = IRRECO_LISTBOX(self);
81 parent->text_col_id = TEXT_COL;
82 parent->data_col_id = DATA_COL;
84 /* Create GtkTreeStore and GtkTreeView */
85 parent->list_store = gtk_list_store_new(
86 N_COLUMNS, G_TYPE_POINTER, G_TYPE_STRING);
87 parent->tree_view = gtk_tree_view_new_with_model(
88 GTK_TREE_MODEL(parent->list_store));
89 g_object_unref(G_OBJECT(parent->list_store));
91 /* Setup column. */
92 renderer = gtk_cell_renderer_text_new();
93 column = gtk_tree_view_column_new_with_attributes(
94 NULL, renderer, "text", TEXT_COL, NULL);
95 gtk_tree_view_append_column(GTK_TREE_VIEW(parent->tree_view),
96 column);
98 /* Set selection callback. */
99 parent->tree_selection = gtk_tree_view_get_selection(
100 GTK_TREE_VIEW(parent->tree_view));
101 gtk_tree_selection_set_mode(parent->tree_selection,
102 GTK_SELECTION_SINGLE);
104 /* Add Widgets to GtkTable. */
105 table = GTK_TABLE(gtk_table_new(2, 2, FALSE));
107 gtk_table_attach_defaults(table, parent->tree_view, 0 ,1, 0, 1);
109 parent->vscrollbar = gtk_vscrollbar_new(gtk_tree_view_get_vadjustment(
110 GTK_TREE_VIEW(parent->tree_view)));
111 gtk_table_attach(table, parent->vscrollbar, 1 ,2, 0, 1,
112 GTK_SHRINK, GTK_FILL, 0, 0);
114 parent->hscrollbar = gtk_hscrollbar_new(gtk_tree_view_get_hadjustment(
115 GTK_TREE_VIEW(parent->tree_view)));
116 gtk_table_attach(table, parent->hscrollbar, 0 ,1, 1, 2,
117 GTK_FILL, GTK_SHRINK, 0, 0);
119 gtk_box_pack_start(GTK_BOX(self), GTK_WIDGET(table), TRUE, TRUE, 0);
121 /* Connect signals. */
122 g_signal_connect(G_OBJECT(IRRECO_LISTBOX(self)->tree_view),
123 "size-request",
124 G_CALLBACK(irreco_listbox_text_tree_size_request),
125 self);
127 IRRECO_RETURN
130 GtkWidget *irreco_listbox_text_new()
132 IrrecoListboxText *self;
133 IRRECO_ENTER
135 self = IRRECO_LISTBOX_TEXT(g_object_new(IRRECO_TYPE_LISTBOX_TEXT, NULL));
136 IRRECO_RETURN_PTR(GTK_WIDGET(self));
140 * Create widgets and initialize IrrecoListboxText structure.
142 * The is no need to call destructor for IrrecoListboxText as long as you attach
143 * the returned GtkWidget to somewhere in the GTK widget tree. Gtk will then
144 * handle the destruction of the widgets when they are no longer needed. This
145 * will work fine as long as IrrecoListboxText structure is still around when the
146 * widget is destroyed.
148 * @param min_width
149 * Minimum width Requisition for the widget.
150 * @param min_height
151 * Minimum height Requisition for the widget.
153 GtkWidget *irreco_listbox_text_new_with_autosize(gint min_width,
154 gint max_width,
155 gint min_height,
156 gint max_height)
158 IrrecoListboxText *self;
159 IRRECO_ENTER
161 self = IRRECO_LISTBOX_TEXT(g_object_new(IRRECO_TYPE_LISTBOX_TEXT, NULL));
162 irreco_listbox_set_autosize(IRRECO_LISTBOX(self), min_width, max_width,
163 min_height, max_height);
164 IRRECO_RETURN_PTR(GTK_WIDGET(self));
167 /** @} */
171 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
172 /* Public Functions */
173 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
176 * @name Public Functions
177 * @{
181 * Append text to the listbox.
183 void irreco_listbox_text_append(IrrecoListboxText *self,
184 const gchar *label,
185 gpointer user_data)
187 IrrecoListbox *parent;
188 GtkTreeIter iter;
189 IRRECO_ENTER
191 parent = IRRECO_LISTBOX(self);
192 gtk_list_store_append(parent->list_store, &iter);
193 gtk_list_store_set(parent->list_store, &iter,
194 TEXT_COL, label, DATA_COL, user_data, -1);
196 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(parent->tree_view));
198 if (parent->select_new_rows == TRUE) {
199 gtk_tree_selection_select_iter(parent->tree_selection, &iter);
202 IRRECO_RETURN
205 /** @} */
207 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
208 /* Events and Callbacks */
209 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
212 * @name Events and Callbacks
213 * @{
216 static void irreco_listbox_text_tree_size_request(GtkWidget *widget,
217 GtkRequisition *requisition,
218 IrrecoListboxText *self)
220 IrrecoListbox *parent;
221 gboolean show_hscrollbar = TRUE;
222 IRRECO_ENTER
224 parent = IRRECO_LISTBOX(self);
226 if (requisition->width <= parent->max_width - 22) {
227 show_hscrollbar = FALSE;
230 if (requisition->width < parent->min_width - 22) {
231 requisition->width = parent->min_width - 22;
232 } else if (requisition->width > parent->max_width - 22) {
233 requisition->width = parent->max_width - 22;
236 if (requisition->height < parent->min_height - 22) {
237 requisition->height = parent->min_height - 22;
238 } else if (requisition->height > parent->max_height &&
239 show_hscrollbar == FALSE) {
240 requisition->height = parent->max_height;
241 } else if (requisition->height > parent->max_height - 22) {
242 requisition->height = parent->max_height - 22;
245 if (show_hscrollbar == TRUE) {
246 gtk_widget_show(parent->hscrollbar);
247 } else {
248 gtk_widget_hide(parent->hscrollbar);
251 IRRECO_RETURN
254 /** @} */
256 /** @} */