~ubuntu-branches/ubuntu/karmic/photoprint/karmic

« back to all changes in this revision

Viewing changes to effects/effectselector.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Milan Zamazal
  • Date: 2007-05-01 16:32:13 UTC
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20070501163213-k0gaendx7grjlmk5
Tags: upstream-0.3.5
ImportĀ upstreamĀ versionĀ 0.3.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * effectselector.c - provides a list of available effects.
 
3
 *
 
4
 * Copyright (c) 2007 by Alastair M. Robinson
 
5
 * Distributed under the terms of the GNU General Public License -
 
6
 * see the file named "COPYING" for more details.
 
7
 *
 
8
 */
 
9
 
 
10
#include <iostream>
 
11
 
 
12
#include <string.h>
 
13
#include <stdlib.h>
 
14
 
 
15
#include <gtk/gtk.h>
 
16
#include <gtk/gtkentry.h>
 
17
#include <gtk/gtklist.h>
 
18
#include <gtk/gtkfilesel.h>
 
19
#include <gtk/gtktreeselection.h>
 
20
#include <gtk/gtkscrolledwindow.h>
 
21
#include <gdk-pixbuf/gdk-pixbuf.h>
 
22
#include <gdk-pixbuf/gdk-pixdata.h>
 
23
 
 
24
#include "../support/generaldialogs.h"
 
25
 
 
26
#include "ppeffect.h"
 
27
#include "ppeffect_desaturate.h"
 
28
#include "ppeffect_temperature.h"
 
29
 
 
30
#include "effectlist.h"
 
31
 
 
32
#include "effectselector.h"
 
33
 
 
34
#include "../config.h"
 
35
#include "../gettext.h"
 
36
#define _(x) gettext(x)
 
37
#define N_(x) gettext_noop(x)
 
38
 
 
39
using namespace std;
 
40
 
 
41
enum {
 
42
        CHANGED_SIGNAL,
 
43
        LAST_SIGNAL
 
44
};
 
45
 
 
46
static guint effectselector_signals[LAST_SIGNAL] = { 0 };
 
47
 
 
48
static void effectselector_class_init (EffectSelectorClass *klass);
 
49
static void effectselector_init (EffectSelector *sel);
 
50
 
 
51
 
 
52
static EffectListItem *findbyname(EffectSelector *il,const char *name)
 
53
{
 
54
        GList *iter=il->imagelist;
 
55
        while(iter)
 
56
        {
 
57
                EffectListItem *ii=(EffectListItem *)iter->data;
 
58
                if(ii && (ii->name==NULL) && (name==NULL))
 
59
                        return(ii);
 
60
                if(ii && ii->name && strcmp(ii->name,name)==0)
 
61
                        return(ii);
 
62
                iter=g_list_next(iter);
 
63
        }
 
64
        cerr << name << " Not found" << endl;
 
65
        return(NULL);
 
66
}
 
67
 
 
68
 
 
69
static EffectListItem *findbypixbuf(EffectSelector *il,GdkPixbuf *pb)
 
70
{
 
71
        GList *iter=il->imagelist;
 
72
        while(iter)
 
73
        {
 
74
                EffectListItem *ii=(EffectListItem *)iter->data;
 
75
                if(ii && ii->GetIcon()==pb)
 
76
                        return(ii);
 
77
                iter=g_list_next(iter);
 
78
        }
 
79
        cerr << " Not found" << endl;
 
80
        return(NULL);
 
81
}
 
82
 
 
83
 
 
84
static gint is_cmpfunc(const void *p1,const void *p2)
 
85
{
 
86
        EffectListItem *i1=(EffectListItem *)p1;
 
87
        EffectListItem *i2=(EffectListItem *)p2;
 
88
        return(strcmp(i1->name,i2->name));
 
89
}
 
90
 
 
91
 
 
92
static void clear_list(EffectSelector *il)
 
93
{
 
94
        gtk_list_store_clear(il->liststore);
 
95
 
 
96
        GList *iter=il->imagelist;
 
97
        while(iter)
 
98
        {
 
99
                EffectListItem *ii=(EffectListItem *)iter->data;
 
100
                GList *niter=g_list_next(iter);
 
101
                il->imagelist=g_list_delete_link(il->imagelist,iter);
 
102
                delete ii;
 
103
                iter=niter;
 
104
        }
 
105
        il->imagelist=NULL;
 
106
}
 
107
 
 
108
 
 
109
static void populate_list(EffectSelector *il)
 
110
{
 
111
        GtkTreeIter iter1;
 
112
 
 
113
        cerr << "Populating list..." << endl;
 
114
        for(int i=0;i<il->els->EffectCount();++i)
 
115
        {
 
116
                EffectListItem *ii=il->els->GetEffect(i);
 
117
                cerr << "Got effect..." << endl;
 
118
                il->imagelist=g_list_append(il->imagelist,ii);
 
119
                cerr << "Added..." << endl;
 
120
        }
 
121
 
 
122
//      cerr << "Sorting list..." << endl;
 
123
        
 
124
//      il->imagelist=g_list_sort(il->imagelist,is_cmpfunc);
 
125
 
 
126
 
 
127
        GList *liter=il->imagelist;
 
128
        while(liter)
 
129
        {
 
130
                EffectListItem *ii=(EffectListItem *)liter->data;
 
131
 
 
132
                gtk_list_store_append(il->liststore,&iter1);
 
133
                gtk_list_store_set(il->liststore,&iter1,0,ii->GetIcon(),-1);
 
134
 
 
135
                while(gtk_events_pending())
 
136
                        gtk_main_iteration_do(false);
 
137
 
 
138
                liter=g_list_next(liter);
 
139
        }
 
140
}
 
141
 
 
142
 
 
143
static void rebuild_liststore(EffectSelector *c)
 
144
{
 
145
        if(!c->imagelist)
 
146
                populate_list(c);
 
147
        else
 
148
        {
 
149
                // Rebuild list view from EffectSelector
 
150
                GList *iter=c->imagelist;
 
151
        
 
152
                GtkTreeIter iter1;
 
153
        
 
154
                gtk_list_store_clear(c->liststore);
 
155
        
 
156
                while(iter)
 
157
                {
 
158
                        EffectListItem *ii=(EffectListItem *)iter->data;
 
159
                        gtk_list_store_append(c->liststore,&iter1);
 
160
                        gtk_list_store_set(c->liststore,&iter1,0,ii->GetIcon(),-1);
 
161
                        iter=g_list_next(iter);
 
162
                }       
 
163
        }
 
164
}
 
165
 
 
166
 
 
167
static void selection_changed(GtkTreeSelection *select,gpointer user_data)
 
168
{
 
169
        EffectSelector *pe=EFFECTSELECTOR(user_data);
 
170
        if(const EffectListItem *ii=effectselector_get_selected(pe))
 
171
        {
 
172
                g_signal_emit(G_OBJECT (pe),effectselector_signals[CHANGED_SIGNAL], 0);
 
173
        }
 
174
}
 
175
 
 
176
 
 
177
GtkWidget*
 
178
effectselector_new (EffectListSource *els)
 
179
{
 
180
        EffectSelector *c=EFFECTSELECTOR(g_object_new (effectselector_get_type (), NULL));
 
181
 
 
182
        c->liststore=gtk_list_store_new(1,GDK_TYPE_PIXBUF);
 
183
        c->els=els;
 
184
 
 
185
        GtkWidget *sw = gtk_scrolled_window_new (NULL, NULL);
 
186
        gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),GTK_SHADOW_ETCHED_IN);
 
187
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),GTK_POLICY_NEVER,GTK_POLICY_AUTOMATIC);
 
188
        gtk_box_pack_start (GTK_BOX (c), sw, TRUE, TRUE, 0);
 
189
        gtk_widget_show(sw);
 
190
 
 
191
        c->treeview=gtk_tree_view_new_with_model(GTK_TREE_MODEL(c->liststore));
 
192
        GtkCellRenderer *renderer;
 
193
        GtkTreeViewColumn *column;
 
194
        
 
195
        renderer=gtk_cell_renderer_pixbuf_new();
 
196
        column=gtk_tree_view_column_new_with_attributes("Effect",renderer,"pixbuf",0,NULL);
 
197
        gtk_tree_view_append_column(GTK_TREE_VIEW(c->treeview),column);
 
198
 
 
199
        GtkTreeSelection *select;
 
200
        select = gtk_tree_view_get_selection (GTK_TREE_VIEW (c->treeview));
 
201
        gtk_tree_selection_set_mode (select, GTK_SELECTION_SINGLE);
 
202
        g_signal_connect (G_OBJECT (select), "changed",
 
203
                G_CALLBACK (selection_changed),c);
 
204
 
 
205
        gtk_container_add(GTK_CONTAINER(sw),c->treeview);
 
206
        gtk_widget_show(c->treeview);
 
207
        
 
208
        GtkWidget *hbox=gtk_hbox_new(FALSE,0);
 
209
        gtk_box_pack_start(GTK_BOX(c),hbox,FALSE,FALSE,0);
 
210
        gtk_widget_show(hbox);
 
211
 
 
212
        populate_list(c);
 
213
        
 
214
        return(GTK_WIDGET(c));
 
215
}
 
216
 
 
217
 
 
218
GType
 
219
effectselector_get_type (void)
 
220
{
 
221
        static GType stpuic_type = 0;
 
222
 
 
223
        if (!stpuic_type)
 
224
        {
 
225
                static const GTypeInfo effectselector_info =
 
226
                {
 
227
                        sizeof (EffectSelectorClass),
 
228
                        NULL, /* base_init */
 
229
                        NULL, /* base_finalize */
 
230
                        (GClassInitFunc) effectselector_class_init,
 
231
                        NULL, /* class_finalize */
 
232
                        NULL, /* class_data */
 
233
                        sizeof (EffectSelector),
 
234
                        0,
 
235
                        (GInstanceInitFunc) effectselector_init,
 
236
                };
 
237
                stpuic_type = g_type_register_static (GTK_TYPE_VBOX, "EffectSelector", &effectselector_info, GTypeFlags(0));
 
238
        }
 
239
        return stpuic_type;
 
240
}
 
241
 
 
242
 
 
243
static void *parent_class=NULL;
 
244
 
 
245
static void effectselector_destroy(GtkObject *object)
 
246
{
 
247
        EffectSelector *il=(EffectSelector *)object;
 
248
        if (GTK_OBJECT_CLASS (parent_class)->destroy)
 
249
                (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
 
250
 
 
251
        clear_list(il);
 
252
}
 
253
 
 
254
 
 
255
static void
 
256
effectselector_class_init (EffectSelectorClass *cls)
 
257
{
 
258
        GtkObjectClass *object_class=(GtkObjectClass *)cls;
 
259
//      GtkWidgetClass *widget_class=(GtkWidgetClass *)cls;
 
260
 
 
261
        parent_class = gtk_type_class (gtk_widget_get_type ());
 
262
 
 
263
        object_class->destroy = effectselector_destroy;
 
264
 
 
265
        effectselector_signals[CHANGED_SIGNAL] =
 
266
        g_signal_new ("changed",
 
267
                G_TYPE_FROM_CLASS (cls),
 
268
                GSignalFlags(G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION),
 
269
                G_STRUCT_OFFSET (EffectSelectorClass, changed),
 
270
                NULL, NULL,
 
271
                g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
 
272
}
 
273
 
 
274
 
 
275
static void
 
276
effectselector_init (EffectSelector *c)
 
277
{
 
278
        c->imagelist=NULL;
 
279
}
 
280
 
 
281
 
 
282
gboolean effectselector_refresh(EffectSelector *c)
 
283
{
 
284
        clear_list(c);
 
285
        rebuild_liststore(c);
 
286
        return(true);
 
287
}
 
288
 
 
289
 
 
290
EffectListItem *effectselector_get_selected(EffectSelector *pe)
 
291
{
 
292
        GtkTreeIter iter;
 
293
        GtkTreeModel *model;
 
294
        
 
295
        GtkTreeSelection *select;
 
296
        select = gtk_tree_view_get_selection (GTK_TREE_VIEW (pe->treeview));
 
297
 
 
298
        if (gtk_tree_selection_get_selected (select,&model, &iter))
 
299
        {
 
300
                GdkPixbuf *pb;
 
301
 
 
302
                gtk_tree_model_get (model, &iter, 0, &pb, -1);
 
303
                if(pb)
 
304
                {
 
305
                        EffectListItem *ii=findbypixbuf(pe,pb);
 
306
                        return(ii);
 
307
                }
 
308
        }
 
309
        return(NULL);
 
310
}
 
311
 
 
312
#if 0
 
313
 
 
314
void effectselector_set_filename(EffectSelector *c,const char *filename)
 
315
{
 
316
        ImageEntry *ii=find_filename(c,filename);
 
317
 
 
318
        if(!ii)
 
319
                ii=add_node(c,filename);
 
320
 
 
321
        if(ii)
 
322
        {
 
323
                if(c->filename)
 
324
                        free(c->filename);
 
325
                c->filename=NULL;
 
326
        
 
327
                if(filename)
 
328
                        c->filename=strdup(filename);
 
329
                
 
330
                GtkTreeIter iter;
 
331
                GtkTreePath *path;
 
332
        
 
333
                if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(c->liststore),&iter))
 
334
                {
 
335
                        do
 
336
                        {
 
337
                                GdkPixbuf *pb;
 
338
                                gtk_tree_model_get(GTK_TREE_MODEL(c->liststore),&iter,0,&pb,-1);
 
339
                                if(pb==ii->pixbuf)
 
340
                                {
 
341
                                        path=gtk_tree_model_get_path(GTK_TREE_MODEL(c->liststore),&iter);
 
342
                                        gtk_tree_view_set_cursor(GTK_TREE_VIEW(c->treeview),path,NULL,false);
 
343
                                        gtk_tree_path_free(path);
 
344
                                        break;
 
345
                                }
 
346
                        } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(c->liststore),&iter));
 
347
                }
 
348
        }
 
349
}
 
350
#endif