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>
17 #include <gtkmm/sizegroup.h>
18 #include <gtkmm/scrollbar.h>
20 #define COLUMNS_FOR_SMALL 16
21 #define COLUMNS_FOR_LARGE 8
22 //#define COLUMNS_FOR_SMALL 48
23 //#define COLUMNS_FOR_LARGE 32
26 namespace Inkscape {
27 namespace UI {
30 PreviewHolder::PreviewHolder() :
31 VBox(),
32 PreviewFillable(),
33 _scroller(0),
34 _anchor(Gtk::ANCHOR_CENTER),
35 _baseSize(Gtk::ICON_SIZE_MENU),
36 _view(VIEW_TYPE_LIST)
37 {
38 _scroller = manage(new Gtk::ScrolledWindow());
39 _insides = manage(new Gtk::Table( 1, 2 ));
40 _insides->set_col_spacings( 8 );
42 // Add a container with the scroller and a spacer
43 Gtk::Table* spaceHolder = manage( new Gtk::Table(1, 2) );
44 _scroller->add( *_insides );
45 spaceHolder->attach( *_scroller, 0, 1, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
47 pack_start(*spaceHolder, Gtk::PACK_EXPAND_WIDGET);
48 }
50 PreviewHolder::~PreviewHolder()
51 {
52 }
56 void PreviewHolder::clear()
57 {
58 items.clear();
59 _prefCols = 0;
60 rebuildUI();
61 }
63 void PreviewHolder::addPreview( Previewable* preview )
64 {
65 items.push_back(preview);
66 int i = items.size() - 1;
68 if ( _view == VIEW_TYPE_LIST ) {
69 Gtk::Widget* label = manage(preview->getPreview(PREVIEW_STYLE_BLURB, VIEW_TYPE_LIST, _baseSize));
70 Gtk::Widget* thing = manage(preview->getPreview(PREVIEW_STYLE_PREVIEW, VIEW_TYPE_LIST, _baseSize));
72 _insides->attach( *thing, 0, 1, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
73 _insides->attach( *label, 1, 2, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::SHRINK );
74 } else {
75 Gtk::Widget* thing = manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, VIEW_TYPE_GRID, _baseSize));
77 int width = 1;
78 int height = 1;
79 calcGridSize( thing, items.size(), width, height );
80 int col = i % width;
81 int row = i / width;
83 if ( i < 10 ) {
84 g_message( "i:%d width:%d height:%d prop cols:%d", i, width, height, (int)_insides->property_n_columns() );
85 }
87 if ( col == 0 ) {
88 // we just started a new row
89 _insides->resize( row + 1, width );
90 } else if ( _insides && width > (int)_insides->property_n_columns() ) {
91 _insides->resize( height, width );
92 }
93 _insides->attach( *thing, col, col+1, row, row+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
94 }
96 _scroller->show_all_children();
97 _scroller->queue_draw();
98 }
100 void PreviewHolder::setStyle(Gtk::BuiltinIconSize size, ViewType view)
101 {
102 if ( size != _baseSize || view != _view ) {
103 _baseSize = size;
104 _view = view;
105 rebuildUI();
106 }
107 }
109 void PreviewHolder::setOrientation( Gtk::AnchorType how )
110 {
111 if ( _anchor != how )
112 {
113 _anchor = how;
114 switch ( _anchor )
115 {
116 case Gtk::ANCHOR_NORTH:
117 case Gtk::ANCHOR_SOUTH:
118 {
119 dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_ALWAYS, Gtk::POLICY_AUTOMATIC );
120 }
121 break;
123 case Gtk::ANCHOR_EAST:
124 case Gtk::ANCHOR_WEST:
125 {
126 dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC );
127 }
128 break;
130 default:
131 {
132 dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC );
133 }
134 }
135 rebuildUI();
136 }
137 }
139 void PreviewHolder::setColumnPref( int cols )
140 {
141 _prefCols = cols;
142 }
144 void PreviewHolder::on_size_allocate( Gtk::Allocation& allocation )
145 {
146 // g_message( "on_size_allocate(%d, %d) (%d, %d)", allocation.get_x(), allocation.get_y(), allocation.get_width(), allocation.get_height() );
147 // g_message(" anchor:%d", _anchor);
148 Gtk::VBox::on_size_allocate( allocation );
149 }
151 void PreviewHolder::on_size_request( Gtk::Requisition* requisition )
152 {
153 // g_message( "on_size_request(%d, %d)", requisition->width, requisition->height );
154 Gtk::VBox::on_size_request( requisition );
155 // g_message( " super (%d, %d)", requisition->width, requisition->height );
156 // g_message(" anchor:%d", _anchor);
157 // g_message(" items:%d", (int)items.size());
158 }
160 void PreviewHolder::calcGridSize( const Gtk::Widget* thing, int itemCount, int& width, int& height )
161 {
162 width = itemCount;
163 height = 1;
165 if ( _anchor == Gtk::ANCHOR_SOUTH || _anchor == Gtk::ANCHOR_NORTH ) {
166 Gtk::Requisition req = _scroller->size_request();
167 int currW = _scroller->get_width();
168 if ( currW > req.width ) {
169 req.width = currW;
170 }
172 Gtk::HScrollbar* hs = dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->get_hscrollbar();
173 if ( hs ) {
174 Gtk::Requisition scrollReq = hs->size_request();
176 // the +8 is a temporary hack
177 req.height -= scrollReq.height + 8;
178 }
180 Gtk::Requisition req2 = thing->size_request();
182 int h2 = req.height / req2.height;
183 int w2 = req.width / req2.width;
184 width = (itemCount + (h2 - 1)) / h2;
185 if ( width < w2 ) {
186 width = w2;
187 }
188 } else {
189 width = _baseSize == Gtk::ICON_SIZE_MENU ? COLUMNS_FOR_SMALL : COLUMNS_FOR_LARGE;
190 if ( _prefCols > 0 ) {
191 width = _prefCols;
192 }
193 height = (itemCount + (width - 1)) / width;
194 if ( height < 1 ) {
195 height = 1;
196 }
197 }
198 }
200 void PreviewHolder::rebuildUI()
201 {
202 _scroller->remove();
203 _insides = 0; // remove() call should have deleted the Gtk::Table.
205 if ( _view == VIEW_TYPE_LIST ) {
206 _insides = manage(new Gtk::Table( 1, 2 ));
207 _insides->set_col_spacings( 8 );
209 for ( unsigned int i = 0; i < items.size(); i++ ) {
210 Gtk::Widget* label = manage(items[i]->getPreview(PREVIEW_STYLE_BLURB, _view, _baseSize));
211 //label->set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
213 Gtk::Widget* thing = manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, _view, _baseSize));
215 _insides->attach( *thing, 0, 1, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
216 _insides->attach( *label, 1, 2, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::SHRINK );
217 }
218 _scroller->add( *_insides );
219 } else {
220 int col = 0;
221 int row = 0;
222 int width = 2;
223 int height = 1;
225 for ( unsigned int i = 0; i < items.size(); i++ ) {
226 Gtk::Widget* thing = manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, _view, _baseSize));
228 if ( !_insides ) {
229 calcGridSize( thing, items.size(), width, height );
230 _insides = manage(new Gtk::Table( height, width ));
231 }
233 _insides->attach( *thing, col, col+1, row, row+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
234 if ( ++col >= width ) {
235 col = 0;
236 row++;
237 }
238 }
239 if ( !_insides ) {
240 _insides = manage(new Gtk::Table( 1, 2 ));
241 }
243 _scroller->add( *_insides );
244 }
246 _scroller->show_all_children();
247 _scroller->queue_draw();
248 }
254 } //namespace UI
255 } //namespace Inkscape
258 /*
259 Local Variables:
260 mode:c++
261 c-file-style:"stroustrup"
262 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
263 indent-tabs-mode:nil
264 fill-column:99
265 End:
266 */
267 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :