2 /*
3 * A simple interface for previewing representations.
4 *
5 * Authors:
6 * Jon A. Cruz
7 *
8 * Copyright (C) 2005 Jon A. Cruz
9 *
10 * Released under GNU GPL, read the file 'COPYING' for more information
11 */
14 #include "previewholder.h"
16 #include <gtkmm/scrolledwindow.h>
19 namespace Inkscape {
20 namespace UI {
23 PreviewHolder::PreviewHolder() :
24 VBox(),
25 PreviewFillable(),
26 _scroller(0),
27 _baseSize(Gtk::ICON_SIZE_MENU),
28 _view(VIEW_TYPE_LIST)
29 {
30 _scroller = manage(new Gtk::ScrolledWindow());
31 Gtk::Table* stuff = manage(new Gtk::Table( 1, 2 ));
32 stuff->set_col_spacings( 8 );
33 _insides = stuff;
34 _scroller->add(*stuff);
36 pack_start(*_scroller, Gtk::PACK_EXPAND_WIDGET);
37 }
39 PreviewHolder::~PreviewHolder()
40 {
41 }
45 void PreviewHolder::clear()
46 {
47 items.clear();
48 rebuildUI();
49 }
51 void PreviewHolder::addPreview( Previewable* preview )
52 {
53 items.push_back(preview);
55 int i = items.size() - 1;
56 if ( _view == VIEW_TYPE_LIST ) {
57 Gtk::Widget* label = manage(preview->getPreview(PREVIEW_STYLE_BLURB, VIEW_TYPE_LIST, _baseSize));
58 Gtk::Widget* thing = manage(preview->getPreview(PREVIEW_STYLE_PREVIEW, VIEW_TYPE_LIST, _baseSize));
60 _insides->attach( *thing, 0, 1, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
61 _insides->attach( *label, 1, 2, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::SHRINK );
62 } else {
63 Gtk::Widget* thing = manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, VIEW_TYPE_GRID, _baseSize));
64 int width = _baseSize == Gtk::ICON_SIZE_MENU ? 16 : 8;
65 int col = i % width;
66 int row = i / width;
67 if ( col == 0 ) {
68 // we just started a new row
69 _insides->resize( row + 1, width );
70 }
71 _insides->attach( *thing, col, col+1, row, row+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
72 }
74 _scroller->show_all_children();
75 _scroller->queue_draw();
76 }
78 void PreviewHolder::setStyle(Gtk::BuiltinIconSize size, ViewType view)
79 {
80 if ( size != _baseSize || view != _view ) {
81 _baseSize = size;
82 _view = view;
83 rebuildUI();
84 }
85 }
88 void PreviewHolder::rebuildUI()
89 {
90 _scroller->remove();
92 if ( _view == VIEW_TYPE_LIST ) {
93 Gtk::Table* stuff = manage(new Gtk::Table( 1, 2 ));
94 _insides = stuff;
95 stuff->set_col_spacings( 8 );
97 for ( unsigned int i = 0; i < items.size(); i++ ) {
98 Gtk::Widget* label = manage(items[i]->getPreview(PREVIEW_STYLE_BLURB, _view, _baseSize));
99 //label->set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
101 Gtk::Widget* thing = manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, _view, _baseSize));
103 stuff->attach( *thing, 0, 1, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
104 stuff->attach( *label, 1, 2, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::SHRINK );
105 }
106 _scroller->add(*stuff);
107 } else {
108 int width = _baseSize == Gtk::ICON_SIZE_MENU ? 16 : 8;
109 int height = (items.size() + (width - 1)) / width;
110 if ( height < 1 ) {
111 height = 1;
112 }
114 Gtk::Table* stuff = manage(new Gtk::Table( height, width ));
115 _insides = stuff;
116 int col = 0;
117 int row = 0;
119 for ( unsigned int i = 0; i < items.size(); i++ ) {
120 Gtk::Widget* thing = manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, _view, _baseSize));
122 stuff->attach( *thing, col, col+1, row, row+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
123 col++;
124 if ( col >= width ) {
125 col = 0;
126 row++;
127 }
128 }
129 _scroller->add(*stuff);
130 }
132 _scroller->show_all_children();
133 _scroller->queue_draw();
134 }
140 } //namespace UI
141 } //namespace Inkscape
144 /*
145 Local Variables:
146 mode:c++
147 c-file-style:"stroustrup"
148 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
149 indent-tabs-mode:nil
150 fill-column:99
151 End:
152 */
153 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :