clean code and changed version nro.
[irreco.git] / irreco / src / core / irreco_listbox_image.c
blob68108a8b06a044ff8e88bacdb10e79910a791dc0
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_image.h"
22 /**
23 * @addtogroup IrrecoListboxImage
24 * @ingroup Irreco
26 * A two column, scrollable listbox with an image cell and a text cel.
28 * @{
31 /**
32 * @file
33 * Source file of @ref IrrecoListboxImage.
36 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
37 /* Prototypes. */
38 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
40 static void irreco_listbox_image_tree_size_request(GtkWidget *widget,
41 GtkRequisition *requisition,
42 IrrecoListboxImage *self);
44 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
45 /* Datatypes */
46 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
48 enum
50 DATA_COL,
51 TEXT_COL,
52 PIXBUF_COL,
53 N_COLUMNS
58 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
59 /* Construction & Destruction */
60 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
62 /**
63 * @name Construction & Destruction
64 * @{
67 G_DEFINE_TYPE(IrrecoListboxImage, irreco_listbox_image, IRRECO_TYPE_LISTBOX)
69 static void irreco_listbox_image_class_init(IrrecoListboxImageClass *klass)
73 static void irreco_listbox_image_init(IrrecoListboxImage *self)
75 IrrecoListbox *parent;
76 GtkTreeViewColumn *column;
77 GtkCellRenderer *renderer;
78 GtkTable *table;
79 IRRECO_ENTER
81 parent = IRRECO_LISTBOX(self);
82 parent->text_col_id = TEXT_COL;
83 parent->data_col_id = DATA_COL;
85 /* Create GtkTreeStore and GtkTreeView */
86 parent->list_store = gtk_list_store_new(
87 N_COLUMNS, G_TYPE_POINTER, G_TYPE_STRING, GDK_TYPE_PIXBUF);
88 parent->tree_view = gtk_tree_view_new_with_model(
89 GTK_TREE_MODEL(parent->list_store));
90 g_object_unref(G_OBJECT(parent->list_store));
92 /* Create pixbuf column. */
93 renderer = gtk_cell_renderer_pixbuf_new();
94 column = gtk_tree_view_column_new_with_attributes(
95 NULL, renderer, "pixbuf", PIXBUF_COL, NULL);
96 gtk_tree_view_append_column(GTK_TREE_VIEW(parent->tree_view),
97 column);
99 /* Create text column. */
100 renderer = gtk_cell_renderer_text_new();
101 column = gtk_tree_view_column_new_with_attributes(
102 NULL, renderer, "text", TEXT_COL, NULL);
103 gtk_tree_view_append_column(GTK_TREE_VIEW(parent->tree_view),
104 column);
106 /* Set selection callback. */
107 parent->tree_selection = gtk_tree_view_get_selection(
108 GTK_TREE_VIEW(parent->tree_view));
109 gtk_tree_selection_set_mode(parent->tree_selection,
110 GTK_SELECTION_SINGLE);
112 /* Add Widgets to GtkTable. */
113 table = GTK_TABLE(gtk_table_new(2, 2, FALSE));
115 gtk_table_attach_defaults(table, parent->tree_view, 0 ,1, 0, 1);
117 parent->vscrollbar = gtk_vscrollbar_new(gtk_tree_view_get_vadjustment(
118 GTK_TREE_VIEW(parent->tree_view)));
119 gtk_table_attach(table, parent->vscrollbar, 1 ,2, 0, 1,
120 GTK_SHRINK, GTK_FILL, 0, 0);
122 parent->hscrollbar = gtk_hscrollbar_new(gtk_tree_view_get_hadjustment(
123 GTK_TREE_VIEW(parent->tree_view)));
124 gtk_table_attach(table, parent->hscrollbar, 0 ,1, 1, 2,
125 GTK_FILL, GTK_SHRINK, 0, 0);
127 gtk_box_pack_start(GTK_BOX(self), GTK_WIDGET(table), TRUE, TRUE, 0);
129 /* Connect signals. */
130 g_signal_connect(G_OBJECT(IRRECO_LISTBOX(self)->tree_view),
131 "size-request",
132 G_CALLBACK(irreco_listbox_image_tree_size_request),
133 self);
135 IRRECO_RETURN
138 GtkWidget *irreco_listbox_image_new()
140 IrrecoListboxImage *self;
141 IRRECO_ENTER
143 self = IRRECO_LISTBOX_IMAGE(g_object_new(IRRECO_TYPE_LISTBOX_IMAGE,
144 NULL));
145 IRRECO_RETURN_PTR(GTK_WIDGET(self));
149 * Create widgets and initialize IrrecoListboxImage structure.
151 * The is no need to call destructor for IrrecoListboxImage as long as you attach
152 * the returned GtkWidget to somewhere in the GTK widget tree. Gtk will then
153 * handle the destruction of the widgets when they are no longer needed. This
154 * will work fine as long as IrrecoListboxImage structure is still around when the
155 * widget is destroyed.
157 * Args: min_width Minimum width Requisition for the widget.
158 * min_height Minimum height Requisition for the widget.
160 GtkWidget *irreco_listbox_image_new_with_autosize(gint min_width,
161 gint max_width,
162 gint min_height,
163 gint max_height)
165 IrrecoListboxImage *self;
166 IRRECO_ENTER
168 self = IRRECO_LISTBOX_IMAGE(g_object_new(IRRECO_TYPE_LISTBOX_IMAGE, NULL));
170 irreco_listbox_set_autosize(IRRECO_LISTBOX(self), min_width, max_width,
171 min_height, max_height);
173 IRRECO_RETURN_PTR(GTK_WIDGET(self));
176 /** @} */
180 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
181 /* Public Functions */
182 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
185 * @name Public Functions
186 * @{
190 * Append row to the listbox.
192 void irreco_listbox_image_append(IrrecoListboxImage *self,
193 const gchar *label,
194 gpointer user_data,
195 const gchar *image)
197 IrrecoListbox *parent = NULL;
198 GError *error = NULL;
199 GdkPixbuf *pixbuf = NULL;
200 GtkTreeIter iter;
201 IRRECO_ENTER
203 parent = IRRECO_LISTBOX(self);
204 IRRECO_DEBUG("Loading image: \"%s\"\n", image);
205 pixbuf = gdk_pixbuf_new_from_file(image, &error);
206 irreco_gerror_check_print(&error);
208 gtk_list_store_append(parent->list_store, &iter);
209 gtk_list_store_set(parent->list_store, &iter,
210 DATA_COL, user_data,
211 TEXT_COL, label,
212 PIXBUF_COL, pixbuf,
213 -1);
215 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(parent->tree_view));
217 if (parent->select_new_rows == TRUE) {
218 gtk_tree_selection_select_iter(parent->tree_selection, &iter);
221 if (pixbuf != NULL) g_object_unref(G_OBJECT(pixbuf));
222 IRRECO_RETURN
226 * Append row to the listbox with resized image.
228 void irreco_listbox_image_append_with_size(IrrecoListboxImage *self,
229 const gchar *label,
230 gpointer user_data,
231 const gchar *image,
232 gint image_width,
233 gint image_height)
235 IrrecoListbox *parent = NULL;
236 GError *error = NULL;
237 GdkPixbuf *pixbuf = NULL;
238 GtkTreeIter iter;
239 IRRECO_ENTER
241 parent = IRRECO_LISTBOX(self);
242 IRRECO_DEBUG("Loading image: \"%s\"\n", image);
243 pixbuf = gdk_pixbuf_new_from_file_at_scale(image,
244 image_width,
245 image_height,
246 GDK_INTERP_NEAREST,
247 &error);
248 irreco_gerror_check_print(&error);
250 gtk_list_store_append(parent->list_store, &iter);
251 gtk_list_store_set(parent->list_store, &iter,
252 DATA_COL, user_data,
253 TEXT_COL, label,
254 PIXBUF_COL, pixbuf,
255 -1);
257 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(parent->tree_view));
259 if (parent->select_new_rows == TRUE) {
260 gtk_tree_selection_select_iter(parent->tree_selection, &iter);
263 if (pixbuf != NULL) g_object_unref(G_OBJECT(pixbuf));
264 IRRECO_RETURN
267 /** @} */
269 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
270 /* Events and Callbacks */
271 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
274 * @name Events and Callbacks
275 * @{
278 static void irreco_listbox_image_tree_size_request(GtkWidget *widget,
279 GtkRequisition *requisition,
280 IrrecoListboxImage *self)
282 IrrecoListbox *parent;
283 gboolean show_hscrollbar = TRUE;
284 IRRECO_ENTER
286 parent = IRRECO_LISTBOX(self);
288 if (requisition->width <= parent->max_width - 22) {
289 show_hscrollbar = FALSE;
292 if (requisition->width < parent->min_width - 22) {
293 requisition->width = parent->min_width - 22;
294 } else if (requisition->width > parent->max_width - 22) {
295 requisition->width = parent->max_width - 22;
298 if (requisition->height < parent->min_height - 22) {
299 requisition->height = parent->min_height - 22;
300 } else if (requisition->height > parent->max_height &&
301 show_hscrollbar == FALSE) {
302 requisition->height = parent->max_height;
303 } else if (requisition->height > parent->max_height - 22) {
304 requisition->height = parent->max_height - 22;
307 if (show_hscrollbar == TRUE) {
308 gtk_widget_show(parent->hscrollbar);
309 } else {
310 gtk_widget_hide(parent->hscrollbar);
313 IRRECO_RETURN
316 /** @} */
318 /** @} */