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"
23 * @addtogroup IrrecoListboxImage
26 * A two column, scrollable listbox with an image cell and a text cel.
33 * Source file of @ref IrrecoListboxImage.
36 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
38 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
40 static void irreco_listbox_image_tree_size_request(GtkWidget
*widget
,
41 GtkRequisition
*requisition
,
42 IrrecoListboxImage
*self
);
44 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
46 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
58 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
59 /* Construction & Destruction */
60 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
63 * @name Construction & Destruction
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
;
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
),
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
),
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
),
132 G_CALLBACK(irreco_listbox_image_tree_size_request
),
138 GtkWidget
*irreco_listbox_image_new()
140 IrrecoListboxImage
*self
;
143 self
= IRRECO_LISTBOX_IMAGE(g_object_new(IRRECO_TYPE_LISTBOX_IMAGE
,
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
,
165 IrrecoListboxImage
*self
;
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
));
180 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
181 /* Public Functions */
182 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
185 * @name Public Functions
190 * Append row to the listbox.
192 void irreco_listbox_image_append(IrrecoListboxImage
*self
,
197 IrrecoListbox
*parent
= NULL
;
198 GError
*error
= NULL
;
199 GdkPixbuf
*pixbuf
= NULL
;
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
,
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
));
226 * Append row to the listbox with resized image.
228 void irreco_listbox_image_append_with_size(IrrecoListboxImage
*self
,
235 IrrecoListbox
*parent
= NULL
;
236 GError
*error
= NULL
;
237 GdkPixbuf
*pixbuf
= NULL
;
241 parent
= IRRECO_LISTBOX(self
);
242 IRRECO_DEBUG("Loading image: \"%s\"\n", image
);
243 pixbuf
= gdk_pixbuf_new_from_file_at_scale(image
,
248 irreco_gerror_check_print(&error
);
250 gtk_list_store_append(parent
->list_store
, &iter
);
251 gtk_list_store_set(parent
->list_store
, &iter
,
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
));
269 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
270 /* Events and Callbacks */
271 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
274 * @name Events and Callbacks
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
;
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
);
310 gtk_widget_hide(parent
->hscrollbar
);