~centralelyon2010/inkscape/imagelinks2

« back to all changes in this revision

Viewing changes to src/ui/dialog/icon-preview.cpp

  • Committer: Steren Giannini
  • Date: 2010-05-29 15:00:23 UTC
  • mfrom: (9012.1.442 trunk)
  • Revision ID: steren.giannini@gmail.com-20100529150023-mi9r10jkhtfs7yyj
sync with rev 9454

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 *   Other dudes from The Inkscape Organization
8
8
 *
9
9
 * Copyright (C) 2004 Bob Jamison
10
 
 * Copyright (C) 2005 Jon A. Cruz
 
10
 * Copyright (C) 2005,2010 Jon A. Cruz
11
11
 *
12
12
 * Released under GNU GPL, read the file 'COPYING' for more information
13
13
 */
18
18
#include <gtk/gtk.h>
19
19
#include <glib/gmem.h>
20
20
#include <glibmm/i18n.h>
 
21
#include <gtkmm/alignment.h>
21
22
#include <gtkmm/buttonbox.h>
22
23
#include <gtkmm/stock.h>
23
24
 
42
43
 
43
44
namespace Inkscape {
44
45
namespace UI {
45
 
namespace Dialogs {
46
 
 
47
 
 
48
 
IconPreviewPanel&
49
 
IconPreviewPanel::getInstance()
 
46
namespace Dialog {
 
47
 
 
48
 
 
49
IconPreviewPanel &IconPreviewPanel::getInstance()
50
50
{
51
 
    static IconPreviewPanel &instance = *new IconPreviewPanel();
52
 
 
53
 
    instance.refreshPreview();
54
 
 
55
 
    return instance;
 
51
    IconPreviewPanel *instance = new IconPreviewPanel();
 
52
 
 
53
    instance->refreshPreview();
 
54
 
 
55
    return *instance;
56
56
}
57
57
 
58
58
//#########################################################################
81
81
 */
82
82
IconPreviewPanel::IconPreviewPanel() :
83
83
    UI::Widget::Panel("", "/dialogs/iconpreview", SP_VERB_VIEW_ICON_PREVIEW),
 
84
    deskTrack(),
 
85
    desktop(0),
 
86
    document(0),
 
87
    timer(0),
 
88
    pending(false),
84
89
    hot(1),
85
 
    refreshButton(0),
86
 
    selectionButton(0)
 
90
    selectionButton(0),
 
91
    desktopChangeConn(),
 
92
    docReplacedConn(),
 
93
    docModConn(),
 
94
    selChangedConn()
87
95
{
88
96
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
89
97
    numEntries = 0;
90
98
 
91
99
    std::vector<Glib::ustring> pref_sizes = prefs->getAllDirs("/iconpreview/sizes/default");
92
100
    std::vector<int> rawSizes;
93
 
    
 
101
 
94
102
    for (std::vector<Glib::ustring>::iterator i = pref_sizes.begin(); i != pref_sizes.end(); ++i) {
95
103
        if (prefs->getBool(*i + "/show", true)) {
96
104
            int sizeVal = prefs->getInt(*i + "/value", -1);
139
147
 
140
148
    Gtk::VBox* magBox = new Gtk::VBox();
141
149
 
142
 
    magBox->pack_start( magnified );
 
150
    Gtk::Frame *magFrame = Gtk::manage(new Gtk::Frame(_("Magnified:")));
 
151
    magFrame->add( magnified );
 
152
 
 
153
    magBox->pack_start( *magFrame, Gtk::PACK_EXPAND_WIDGET );
143
154
    magBox->pack_start( magLabel, Gtk::PACK_SHRINK );
144
155
 
145
156
 
146
 
    Gtk::VBox * verts = new Gtk::VBox();
147
 
    for ( int i = 0; i < numEntries; i++ ) {
 
157
    Gtk::VBox *verts = new Gtk::VBox();
 
158
    Gtk::HBox *horiz = 0;
 
159
    int previous = 0;
 
160
    int avail = 0;
 
161
    for ( int i = numEntries - 1; i >= 0; --i ) {
148
162
        pixMem[i] = new guchar[4 * sizes[i] * sizes[i]];
149
163
        memset( pixMem[i], 0x00, 4 *  sizes[i] * sizes[i] );
150
164
 
154
168
        Glib::ustring label(*labels[i]);
155
169
        buttons[i] = new Gtk::ToggleToolButton(label);
156
170
        buttons[i]->set_active( i == hot );
157
 
        buttons[i]->set_icon_widget(*images[i]);
 
171
        Gtk::Frame *frame = new Gtk::Frame();
 
172
        frame->set_shadow_type(Gtk::SHADOW_ETCHED_IN);
 
173
        frame->add(*images[i]);
 
174
        buttons[i]->set_icon_widget(*Gtk::manage(frame));
158
175
 
159
176
        tips.set_tip((*buttons[i]), label);
160
177
 
161
178
        buttons[i]->signal_clicked().connect( sigc::bind<int>( sigc::mem_fun(*this, &IconPreviewPanel::on_button_clicked), i) );
162
179
 
163
180
 
164
 
        verts->add(*buttons[i]);
 
181
        Gtk::Alignment *align = Gtk::manage(new Gtk::Alignment(0.5, 0.5, 0, 0));
 
182
        align->add(*buttons[i]);
 
183
 
 
184
        int pad = 12;
 
185
        if ((avail == 0) && (previous == 0)) {
 
186
            verts->pack_end(*align, Gtk::PACK_SHRINK);
 
187
            previous = sizes[i];
 
188
            avail = sizes[i];
 
189
        } else {
 
190
            if ((avail < pad) || ((sizes[i] > avail) && (sizes[i] < previous))) {
 
191
                horiz = 0;
 
192
            }
 
193
            if ((horiz == 0) && (sizes[i] <= previous)) {
 
194
                avail = previous;
 
195
            }
 
196
            if (sizes[i] <= avail) {
 
197
                if (!horiz) {
 
198
                    horiz = Gtk::manage(new Gtk::HBox());
 
199
                    avail = previous;
 
200
                    verts->pack_end(*horiz, Gtk::PACK_SHRINK);
 
201
                }
 
202
                horiz->pack_start(*align, Gtk::PACK_EXPAND_WIDGET);
 
203
                avail -= sizes[i];
 
204
                avail -= pad; // a little extra for padding
 
205
            } else {
 
206
                horiz = 0;
 
207
                verts->pack_end(*align, Gtk::PACK_SHRINK);
 
208
            }
 
209
        }
165
210
    }
166
211
 
167
212
    iconBox.pack_start(splitter);
168
213
    splitter.pack1( *magBox, true, true );
169
 
    splitter.pack2( *verts, false, false );
170
 
 
171
 
 
172
 
    //## The Refresh button
173
 
 
174
 
 
175
 
    Gtk::HButtonBox* holder = new Gtk::HButtonBox( Gtk::BUTTONBOX_END );
176
 
    _getContents()->pack_end(*holder, false, false);
177
 
 
178
 
    selectionButton = new Gtk::ToggleButton(_("Selection")); // , GTK_RESPONSE_APPLY
179
 
    holder->pack_start( *selectionButton, false, false );
 
214
    Gtk::Frame *actuals = Gtk::manage(new Gtk::Frame(_("Actual Size:")));
 
215
    actuals->add(*verts);
 
216
    splitter.pack2( *actuals, false, false );
 
217
 
 
218
 
 
219
    selectionButton = new Gtk::CheckButton(_("Selection")); // , GTK_RESPONSE_APPLY
 
220
    magBox->pack_start( *selectionButton, Gtk::PACK_SHRINK );
180
221
    tips.set_tip((*selectionButton), _("Selection only or whole document"));
181
222
    selectionButton->signal_clicked().connect( sigc::mem_fun(*this, &IconPreviewPanel::modeToggled) );
182
223
 
183
224
    gint val = prefs->getBool("/iconpreview/selectionOnly");
184
225
    selectionButton->set_active( val != 0 );
185
226
 
186
 
    refreshButton = new Gtk::Button(Gtk::Stock::REFRESH); // , GTK_RESPONSE_APPLY
187
 
    holder->pack_end( *refreshButton, false, false );
188
 
    tips.set_tip((*refreshButton), _("Refresh the icons"));
189
 
    refreshButton->signal_clicked().connect( sigc::mem_fun(*this, &IconPreviewPanel::refreshPreview) );
190
 
 
191
 
 
192
 
    _getContents()->pack_start(iconBox, Gtk::PACK_EXPAND_WIDGET);
 
227
 
 
228
    _getContents()->pack_start(iconBox, Gtk::PACK_SHRINK);
193
229
 
194
230
    show_all_children();
 
231
 
 
232
    // Connect this up last
 
233
    desktopChangeConn = deskTrack.connectDesktopChanged( sigc::mem_fun(*this, &IconPreviewPanel::setDesktop) );
 
234
    deskTrack.connect(GTK_WIDGET(gobj()));
 
235
}
 
236
 
 
237
IconPreviewPanel::~IconPreviewPanel()
 
238
{
 
239
    setDesktop(0);
 
240
    if (timer) {
 
241
        timer->stop();
 
242
        delete timer;
 
243
        timer = 0;
 
244
    }
 
245
 
 
246
    selChangedConn.disconnect();
 
247
    docModConn.disconnect();
 
248
    docReplacedConn.disconnect();
 
249
    desktopChangeConn.disconnect();
 
250
    deskTrack.disconnect();
195
251
}
196
252
 
197
253
//#########################################################################
199
255
//#########################################################################
200
256
 
201
257
 
 
258
void IconPreviewPanel::setDesktop( SPDesktop* desktop )
 
259
{
 
260
    Panel::setDesktop(desktop);
 
261
 
 
262
    SPDocument *newDoc = (desktop) ? desktop->doc() : 0;
 
263
 
 
264
    if ( desktop != this->desktop ) {
 
265
        docReplacedConn.disconnect();
 
266
        selChangedConn.disconnect();
 
267
 
 
268
        this->desktop = Panel::getDesktop();
 
269
        if ( this->desktop ) {
 
270
            docReplacedConn = this->desktop->connectDocumentReplaced(sigc::hide<0>(sigc::mem_fun(this, &IconPreviewPanel::setDocument)));
 
271
            if (this->desktop->selection) {
 
272
                selChangedConn = desktop->selection->connectChanged(sigc::hide(sigc::mem_fun(this, &IconPreviewPanel::queueRefresh)));
 
273
            }
 
274
        }
 
275
    }
 
276
    setDocument(newDoc);
 
277
    deskTrack.setBase(desktop);
 
278
}
 
279
 
 
280
void IconPreviewPanel::setDocument( SPDocument *document )
 
281
{
 
282
    if (this->document != document) {
 
283
        docModConn.disconnect();
 
284
 
 
285
        this->document = document;
 
286
        if (this->document) {
 
287
            docModConn = this->document->connectModified(sigc::hide(sigc::mem_fun(this, &IconPreviewPanel::queueRefresh)));
 
288
            queueRefresh();
 
289
        }
 
290
    }
 
291
}
 
292
 
202
293
void IconPreviewPanel::refreshPreview()
203
294
{
204
295
    SPDesktop *desktop = getDesktop();
205
 
    if ( desktop ) {
206
 
 
 
296
    if (!timer) {
 
297
        timer = new Glib::Timer();
 
298
    }
 
299
    if (timer->elapsed() < 0.1) {
 
300
        // Do not refresh too quickly
 
301
        queueRefresh();
 
302
    } else if ( desktop ) {
207
303
        if ( selectionButton && selectionButton->get_active() )
208
304
        {
209
305
            Inkscape::Selection * sel = sp_desktop_selection(desktop);
215
311
                while ( items && !target ) {
216
312
                    SPItem* item = SP_ITEM( items->data );
217
313
                    SPObject * obj = SP_OBJECT(item);
218
 
                    gchar const *id = SP_OBJECT_ID( obj );
 
314
                    gchar const *id = obj->getId();
219
315
                    if ( id ) {
220
316
                        target = obj;
221
317
                    }
226
322
                    renderPreview(target);
227
323
                }
228
324
            }
229
 
        }
230
 
        else
231
 
        {
 
325
        } else {
232
326
            SPObject *target = desktop->currentRoot();
233
327
            if ( target ) {
234
328
                renderPreview(target);
235
329
            }
236
330
        }
 
331
        timer->reset();
 
332
    }
 
333
}
 
334
 
 
335
bool IconPreviewPanel::refreshCB()
 
336
{
 
337
    bool callAgain = true;
 
338
    if (!timer) {
 
339
        timer = new Glib::Timer();
 
340
    }
 
341
    if ( timer->elapsed() > 0.1 ) {
 
342
        callAgain = false;
 
343
        refreshPreview();
 
344
        pending = false;
 
345
    }
 
346
    return callAgain;
 
347
}
 
348
 
 
349
void IconPreviewPanel::queueRefresh()
 
350
{
 
351
    if (!pending) {
 
352
        pending = true;
 
353
        if (!timer) {
 
354
            timer = new Glib::Timer();
 
355
        }
 
356
        Glib::signal_idle().connect( sigc::mem_fun(this, &IconPreviewPanel::refreshCB), Glib::PRIORITY_DEFAULT_IDLE );
237
357
    }
238
358
}
239
359
 
248
368
void IconPreviewPanel::renderPreview( SPObject* obj )
249
369
{
250
370
    SPDocument * doc = SP_OBJECT_DOCUMENT(obj);
251
 
    gchar * id = SP_OBJECT_ID(obj);
 
371
    gchar const * id = obj->getId();
252
372
 
253
373
//    g_message(" setting up to render '%s' as the icon", id );
254
374
 
290
410
    magnified.get_parent()->queue_draw();
291
411
}
292
412
 
293
 
 
294
413
} //namespace Dialogs
295
414
} //namespace UI
296
415
} //namespace Inkscape