summaryrefslogtreecommitdiffstats
path: root/src/ui/previewholder.cpp
blob: 12a71c8e89c0919595d648493b917f198355a559 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * A simple interface for previewing representations.
 *
 * Authors:
 *   Jon A. Cruz
 *
 * Copyright (C) 2005 Jon A. Cruz
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */


#include "previewable.h"
#include "previewholder.h"

#include <gtkmm/scrolledwindow.h>
#include <gtkmm/sizegroup.h>
#include <gtkmm/scrollbar.h>
#include <gtkmm/adjustment.h>
#include <gtkmm/grid.h>

#define COLUMNS_FOR_SMALL 16
#define COLUMNS_FOR_LARGE 8
//#define COLUMNS_FOR_SMALL 48
//#define COLUMNS_FOR_LARGE 32


namespace Inkscape {
namespace UI {


PreviewHolder::PreviewHolder() :
    Bin(),
    _scroller(nullptr),
    _insides(nullptr),
    _prefCols(0),
    _updatesFrozen(false),
    _anchor(SP_ANCHOR_CENTER),
    _baseSize(UI::Widget::PREVIEW_SIZE_SMALL),
    _ratio(100),
    _view(UI::Widget::VIEW_TYPE_LIST),
    _wrap(false),
    _border(UI::Widget::BORDER_NONE)
{
    set_name( "PreviewHolder" );
    _scroller = Gtk::manage(new Gtk::ScrolledWindow());
    _scroller->set_name( "PreviewHolderScroller" );
    _scroller->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);

    _insides = Gtk::manage(new Gtk::Grid());
    _insides->set_name( "PreviewHolderGrid" );
    _insides->set_column_spacing(8);

    _scroller->set_hexpand();
    _scroller->set_vexpand();
    _scroller->add( *_insides );

    // Disable overlay scrolling as the scrollbar covers up swatches.
    // For some reason this also makes the height 55px.
    _scroller->set_overlay_scrolling(false);

    add(*_scroller);
}

PreviewHolder::~PreviewHolder()
= default;

/**
 * Translates vertical scrolling into horizontal
 */
bool PreviewHolder::on_scroll_event(GdkEventScroll *event)
{
    if (_wrap) {
        return FALSE;
    }

    // Scroll horizontally by page on mouse wheel
    auto adj = _scroller->get_hadjustment();

    if (!adj) {
        return FALSE;
    }

    double move;
    switch (event->direction) {
        case GDK_SCROLL_UP:
        case GDK_SCROLL_LEFT:
            move = -adj->get_page_size();
            break;
        case GDK_SCROLL_DOWN:
        case GDK_SCROLL_RIGHT:
            move =  adj->get_page_size();
            break;
        case GDK_SCROLL_SMOOTH:
            if (fabs(event->delta_y) <= fabs(event->delta_x)) {
                return FALSE;
            }
#ifdef GDK_WINDOWING_QUARTZ
            move = event->delta_y;
#else
            move = event->delta_y * adj->get_page_size();
#endif
            break;
        default:
            return FALSE;
    }

    double value = adj->get_value() + move;

    adj->set_value(value);

    return TRUE;
}

void PreviewHolder::clear()
{
    items.clear();
    _prefCols = 0;
    // Kludge to restore scrollbars
    if ( !_wrap && (_view != UI::Widget::VIEW_TYPE_LIST) && (_anchor == SP_ANCHOR_NORTH || _anchor == SP_ANCHOR_SOUTH) ) {
        _scroller->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_NEVER );
    }
    rebuildUI();
}

/**
 * Add a Previewable item to the PreviewHolder
 *
 * \param[in] preview The Previewable item to add
 */
void PreviewHolder::addPreview( Previewable* preview )
{
    items.push_back(preview);
    if ( !_updatesFrozen )
    {
        int i = items.size() - 1;

        switch(_view) {
            case UI::Widget::VIEW_TYPE_LIST:
                {
                    Gtk::Widget* label = Gtk::manage(preview->getPreview(UI::Widget::PREVIEW_STYLE_BLURB,
                                                                         UI::Widget::VIEW_TYPE_LIST,
                                                                         _baseSize, _ratio, _border));
                    Gtk::Widget* item = Gtk::manage(preview->getPreview(UI::Widget::PREVIEW_STYLE_PREVIEW,
                                                                        UI::Widget::VIEW_TYPE_LIST,
                                                                        _baseSize, _ratio, _border));

                    item->set_hexpand();
                    item->set_vexpand();
                    _insides->attach(*item, 0, i, 1, 1);

                    label->set_hexpand();
                    label->set_valign(Gtk::ALIGN_CENTER);
                    _insides->attach(*label, 1, i, 1, 1);
                }

                break;
            case UI::Widget::VIEW_TYPE_GRID:
                {
                    Gtk::Widget* item = Gtk::manage(items[i]->getPreview(UI::Widget::PREVIEW_STYLE_PREVIEW,
                                                                         UI::Widget::VIEW_TYPE_GRID,
                                                                         _baseSize, _ratio, _border));

                    int ncols = 1;
                    int nrows = 1;
                    int col = 0;
                    int row = 0;

                    // To get size
		    auto kids = _insides->get_children();
		    int childCount = (int)kids.size();
                    if (childCount > 0 ) {

                        // Need already shown widget
                        calcGridSize( kids[0], items.size()+1, ncols, nrows );

                        // Column and row for the new widget
                        col = i % ncols;
                        row = i / ncols;

                    }

		    // Loop through the existing widgets and move them to new location
		    for ( int j = 1; j < childCount; j++ ) {
			    auto target = kids[childCount - (j + 1)];
			    int col2 = j % ncols;
			    int row2 = j / ncols;
			    _insides->remove( *target );

			    target->set_hexpand();
			    target->set_vexpand();
			    _insides->attach( *target, col2, row2, 1, 1);
		    }
		    item->set_hexpand();
		    item->set_vexpand();
		    _insides->attach(*item, col, row, 1, 1);
                }
        }

        _scroller->show_all_children();
    }
}

void PreviewHolder::freezeUpdates()
{
    _updatesFrozen = true;
}

void PreviewHolder::thawUpdates()
{
    _updatesFrozen = false;
    rebuildUI();
}

void
PreviewHolder::setStyle(UI::Widget::PreviewSize size,
                        UI::Widget::ViewType    view,
                        guint                   ratio,
                        UI::Widget::BorderStyle border )
{
    if ( size != _baseSize || view != _view || ratio != _ratio || border != _border ) {
        _baseSize = size;
        _view = view;
        _ratio = ratio;
        _border = border;
        // Kludge to restore scrollbars
        if ( !_wrap && (_view != UI::Widget::VIEW_TYPE_LIST) && (_anchor == SP_ANCHOR_NORTH || _anchor == SP_ANCHOR_SOUTH) ) {
            _scroller->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_NEVER );
        }
        rebuildUI();
    }
}

void PreviewHolder::setOrientation(SPAnchorType anchor)
{
    if ( _anchor != anchor )
    {
        _anchor = anchor;
        switch ( _anchor )
        {
            case SP_ANCHOR_NORTH:
            case SP_ANCHOR_SOUTH:
            {
                _scroller->set_policy( Gtk::POLICY_AUTOMATIC, _wrap ? Gtk::POLICY_AUTOMATIC : Gtk::POLICY_NEVER );
            }
            break;

            case SP_ANCHOR_EAST:
            case SP_ANCHOR_WEST:
            {
                _scroller->set_policy( Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC );
            }
            break;

            default:
            {
                _scroller->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC );
            }
        }
        rebuildUI();
    }
}

void PreviewHolder::setWrap( bool wrap )
{
    if (_wrap != wrap) {
        _wrap = wrap;
        switch ( _anchor )
        {
            case SP_ANCHOR_NORTH:
            case SP_ANCHOR_SOUTH:
            {
                _scroller->set_policy( Gtk::POLICY_AUTOMATIC, _wrap ? Gtk::POLICY_AUTOMATIC : Gtk::POLICY_NEVER );
            }
            break;
            default:
            {
                (void)0;
                // do nothing;
            }
        }
        rebuildUI();
    }
}

void PreviewHolder::setColumnPref( int cols )
{
    _prefCols = cols;
}


/**
 * Calculate the grid side of a preview holder
 *
 * \param[in]  item       A sample preview widget.
 * \param[in]  itemCount  The number of items to pack into the grid.
 * \param[out] ncols      The number of columns in grid.
 * \param[out] nrows      The number of rows in grid.
 */
void PreviewHolder::calcGridSize( const Gtk::Widget* item, int itemCount, int& ncols, int& nrows )
{
    // Initially set all items in a horizontal row
    ncols = itemCount;
    nrows = 1;

    if ( _anchor == SP_ANCHOR_SOUTH || _anchor == SP_ANCHOR_NORTH ) {
        Gtk::Requisition req;
        Gtk::Requisition req_natural;
        _scroller->get_preferred_size(req, req_natural);
        int currW = _scroller->get_width();
        if ( currW > req.width ) {
            req.width = currW;
        }

        if (_wrap && item != nullptr) {

            // Get width of bar.
            int width_scroller = _scroller->get_width();

            // Get width of one item (must be visible).
            int minimum_width_item = 0;
            int natural_width_item = 0;
            item->get_preferred_width(minimum_width_item, natural_width_item);

            // Calculate columns and rows.
            if (natural_width_item < 1) {
                natural_width_item = 1;
            }
            ncols = width_scroller / natural_width_item - 1;

            // On first run, scroller width is not set correct... so we need to fudge it:
            if (ncols < 2) {
                ncols = itemCount/2;
                nrows = 2;
            } else {
                nrows = itemCount / ncols;
            }
        }
    } else {
        ncols = (_baseSize == UI::Widget::PREVIEW_SIZE_SMALL || _baseSize == UI::Widget::PREVIEW_SIZE_TINY) ?
            COLUMNS_FOR_SMALL : COLUMNS_FOR_LARGE;
        if ( _prefCols > 0 ) {
            ncols = _prefCols;
        }
        nrows = (itemCount + (ncols - 1)) / ncols;
        if ( nrows < 1 ) {
            nrows = 1;
        }
    }
}

void PreviewHolder::rebuildUI()
{
    auto children = _insides->get_children();
    for (auto child : children) {
        _insides->remove(*child);
        delete child;
    }

    _insides->set_column_spacing(0);
    _insides->set_row_spacing(0);
    if (_border == UI::Widget::BORDER_WIDE) {
        _insides->set_column_spacing(1);
        _insides->set_row_spacing(1);
    }

    switch (_view) {
        case UI::Widget::VIEW_TYPE_LIST:
        {
            _insides->set_column_spacing(8);

            for ( unsigned int i = 0; i < items.size(); i++ ) {
                Gtk::Widget* label = Gtk::manage(items[i]->getPreview(UI::Widget::PREVIEW_STYLE_BLURB, _view, _baseSize, _ratio, _border));
                //label->set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);

                Gtk::Widget* item = Gtk::manage(items[i]->getPreview(UI::Widget::PREVIEW_STYLE_PREVIEW, _view, _baseSize, _ratio, _border));

                item->set_hexpand();
                item->set_vexpand();
                _insides->attach(*item, 0, i, 1, 1);

                label->set_hexpand();
                label->set_valign(Gtk::ALIGN_CENTER);
                _insides->attach(*label, 1, i, 1, 1);
            }
        }
        break;

        case UI::Widget::VIEW_TYPE_GRID:
        {
            int col = 0;
            int row = 0;
            int ncols = 2;
            int nrows = 1;

            for ( unsigned int i = 0; i < items.size(); i++ ) {

                // If this is the last row, flag so the previews can draw a bottom
                UI::Widget::BorderStyle border = ((row == nrows -1) && (_border == UI::Widget::BORDER_SOLID)) ?
                    UI::Widget::BORDER_SOLID_LAST_ROW : _border;

                Gtk::Widget* item = Gtk::manage(items[i]->getPreview(UI::Widget::PREVIEW_STYLE_PREVIEW, _view, _baseSize, _ratio, border));
                item->set_hexpand();
                item->set_vexpand();

                if (i == 0) {
                    // We need one item shown before we can call calcGridSize()...
                    _insides->attach( *item, 0, 0, 1, 1);
                    _scroller->show_all_children();
                    calcGridSize( item, items.size(), ncols, nrows );
                } else {
                    // We've already calculated grid size.
                    _insides->attach( *item, col, row, 1, 1);
                }

                if ( ++col >= ncols ) {
                    col = 0;
                    row++;
                }
            }
        }
    }

    _scroller->show_all_children();
}





} //namespace UI
} //namespace Inkscape


/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :