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 _insides(0),
35 _prefCols(0),
36 _updatesFrozen(false),
37 _anchor(Gtk::ANCHOR_CENTER),
38 _baseSize(PREVIEW_SIZE_SMALL),
39 _ratio(100),
40 _view(VIEW_TYPE_LIST),
41 _wrap(false)
42 {
43 _scroller = manage(new Gtk::ScrolledWindow());
44 _insides = manage(new Gtk::Table( 1, 2 ));
45 _insides->set_col_spacings( 8 );
47 // Add a container with the scroller and a spacer
48 Gtk::Table* spaceHolder = manage( new Gtk::Table(1, 2) );
49 _scroller->add( *_insides );
50 spaceHolder->attach( *_scroller, 0, 1, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
52 pack_start(*spaceHolder, Gtk::PACK_EXPAND_WIDGET);
53 }
55 PreviewHolder::~PreviewHolder()
56 {
57 }
61 void PreviewHolder::clear()
62 {
63 items.clear();
64 _prefCols = 0;
65 // Kludge to restore scrollbars
66 if ( !_wrap && (_view != VIEW_TYPE_LIST) && (_anchor == Gtk::ANCHOR_NORTH || _anchor == Gtk::ANCHOR_SOUTH) ) {
67 dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_NEVER );
68 }
69 rebuildUI();
70 }
72 void PreviewHolder::addPreview( Previewable* preview )
73 {
74 items.push_back(preview);
75 if ( !_updatesFrozen )
76 {
77 int i = items.size() - 1;
79 if ( _view == VIEW_TYPE_LIST ) {
80 Gtk::Widget* label = manage(preview->getPreview(PREVIEW_STYLE_BLURB, VIEW_TYPE_LIST, _baseSize, _ratio));
81 Gtk::Widget* thing = manage(preview->getPreview(PREVIEW_STYLE_PREVIEW, VIEW_TYPE_LIST, _baseSize, _ratio));
83 _insides->attach( *thing, 0, 1, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
84 _insides->attach( *label, 1, 2, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::SHRINK );
85 } else {
86 Gtk::Widget* thing = manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, VIEW_TYPE_GRID, _baseSize, _ratio));
88 int width = 1;
89 int height = 1;
90 calcGridSize( thing, items.size(), width, height );
91 int col = i % width;
92 int row = i / width;
94 if ( _insides && width > (int)_insides->property_n_columns() ) {
95 std::vector<Gtk::Widget*>kids = _insides->get_children();
96 int oldWidth = (int)_insides->property_n_columns();
97 int childCount = (int)kids.size();
98 // g_message(" %3d resize from %d to %d (r:%d, c:%d) with %d children", i, oldWidth, width, row, col, childCount );
99 _insides->resize( height, width );
101 for ( int j = oldWidth; j < childCount; j++ ) {
102 Gtk::Widget* target = kids[childCount - (j + 1)];
103 int col2 = j % width;
104 int row2 = j / width;
105 Glib::RefPtr<Gtk::Widget> handle(target);
106 _insides->remove( *target );
107 _insides->attach( *target, col2, col2+1, row2, row2+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
108 }
109 } else if ( col == 0 ) {
110 // we just started a new row
111 _insides->resize( row + 1, width );
112 }
113 _insides->attach( *thing, col, col+1, row, row+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
114 }
116 _scroller->show_all_children();
117 _scroller->queue_draw();
118 }
119 }
121 void PreviewHolder::freezeUpdates()
122 {
123 _updatesFrozen = true;
124 }
126 void PreviewHolder::thawUpdates()
127 {
128 _updatesFrozen = false;
129 rebuildUI();
130 }
132 void PreviewHolder::setStyle( ::PreviewSize size, ViewType view, guint ratio )
133 {
134 if ( size != _baseSize || view != _view || ratio != _ratio ) {
135 _baseSize = size;
136 _view = view;
137 _ratio = ratio;
138 // Kludge to restore scrollbars
139 if ( !_wrap && (_view != VIEW_TYPE_LIST) && (_anchor == Gtk::ANCHOR_NORTH || _anchor == Gtk::ANCHOR_SOUTH) ) {
140 dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_NEVER );
141 }
142 rebuildUI();
143 }
144 }
146 void PreviewHolder::setOrientation( Gtk::AnchorType how )
147 {
148 if ( _anchor != how )
149 {
150 _anchor = how;
151 switch ( _anchor )
152 {
153 case Gtk::ANCHOR_NORTH:
154 case Gtk::ANCHOR_SOUTH:
155 {
156 dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_AUTOMATIC, _wrap ? Gtk::POLICY_AUTOMATIC : Gtk::POLICY_NEVER );
157 }
158 break;
160 case Gtk::ANCHOR_EAST:
161 case Gtk::ANCHOR_WEST:
162 {
163 dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC );
164 }
165 break;
167 default:
168 {
169 dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC );
170 }
171 }
172 rebuildUI();
173 }
174 }
176 void PreviewHolder::setWrap( bool b )
177 {
178 if ( b != _wrap ) {
179 _wrap = b;
180 switch ( _anchor )
181 {
182 case Gtk::ANCHOR_NORTH:
183 case Gtk::ANCHOR_SOUTH:
184 {
185 dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_AUTOMATIC, _wrap ? Gtk::POLICY_AUTOMATIC : Gtk::POLICY_NEVER );
186 }
187 break;
188 default:
189 {
190 (void)0;
191 // do nothing;
192 }
193 }
194 rebuildUI();
195 }
196 }
198 void PreviewHolder::setColumnPref( int cols )
199 {
200 _prefCols = cols;
201 }
203 void PreviewHolder::on_size_allocate( Gtk::Allocation& allocation )
204 {
205 // g_message( "on_size_allocate(%d, %d) (%d, %d)", allocation.get_x(), allocation.get_y(), allocation.get_width(), allocation.get_height() );
206 // g_message(" anchor:%d", _anchor);
207 Gtk::VBox::on_size_allocate( allocation );
209 if ( _insides && !_wrap && (_view != VIEW_TYPE_LIST) && (_anchor == Gtk::ANCHOR_NORTH || _anchor == Gtk::ANCHOR_SOUTH) ) {
210 Gtk::Requisition req;
211 _insides->size_request(req);
212 gint delta = allocation.get_width() - req.width;
214 if ( (delta > 4) && req.height < allocation.get_height() ) {
215 dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_NEVER, Gtk::POLICY_NEVER );
216 } else {
217 dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_NEVER );
218 }
219 }
220 }
222 void PreviewHolder::on_size_request( Gtk::Requisition* requisition )
223 {
224 // g_message( "on_size_request(%d, %d)", requisition->width, requisition->height );
225 Gtk::VBox::on_size_request( requisition );
226 // g_message( " super (%d, %d)", requisition->width, requisition->height );
227 // g_message(" anchor:%d", _anchor);
228 // g_message(" items:%d", (int)items.size());
229 }
231 void PreviewHolder::calcGridSize( const Gtk::Widget* thing, int itemCount, int& width, int& height )
232 {
233 width = itemCount;
234 height = 1;
236 if ( _anchor == Gtk::ANCHOR_SOUTH || _anchor == Gtk::ANCHOR_NORTH ) {
237 Gtk::Requisition req;
238 _scroller->size_request(req);
239 int currW = _scroller->get_width();
240 if ( currW > req.width ) {
241 req.width = currW;
242 }
244 Gtk::HScrollbar* hs = dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->get_hscrollbar();
245 if ( hs ) {
246 Gtk::Requisition scrollReq;
247 hs->size_request(scrollReq);
249 // the +8 is a temporary hack
250 req.height -= scrollReq.height + 8;
251 }
253 Gtk::Requisition req2;
254 const_cast<Gtk::Widget*>(thing)->size_request(req2);
256 int h2 = ((req2.height > 0) && (req.height > req2.height)) ? (req.height / req2.height) : 1;
257 int w2 = ((req2.width > 0) && (req.width > req2.width)) ? (req.width / req2.width) : 1;
258 width = (itemCount + (h2 - 1)) / h2;
259 if ( width < w2 ) {
260 width = w2;
261 }
262 } else {
263 width = (_baseSize == PREVIEW_SIZE_SMALL || _baseSize == PREVIEW_SIZE_TINY) ? COLUMNS_FOR_SMALL : COLUMNS_FOR_LARGE;
264 if ( _prefCols > 0 ) {
265 width = _prefCols;
266 }
267 height = (itemCount + (width - 1)) / width;
268 if ( height < 1 ) {
269 height = 1;
270 }
271 }
272 }
274 void PreviewHolder::rebuildUI()
275 {
276 _scroller->remove();
277 _insides = 0; // remove() call should have deleted the Gtk::Table.
279 if ( _view == VIEW_TYPE_LIST ) {
280 _insides = manage(new Gtk::Table( 1, 2 ));
281 _insides->set_col_spacings( 8 );
283 for ( unsigned int i = 0; i < items.size(); i++ ) {
284 Gtk::Widget* label = manage(items[i]->getPreview(PREVIEW_STYLE_BLURB, _view, _baseSize, _ratio));
285 //label->set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
287 Gtk::Widget* thing = manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, _view, _baseSize, _ratio));
289 _insides->attach( *thing, 0, 1, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
290 _insides->attach( *label, 1, 2, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::SHRINK );
291 }
292 _scroller->add( *_insides );
293 } else {
294 int col = 0;
295 int row = 0;
296 int width = 2;
297 int height = 1;
299 for ( unsigned int i = 0; i < items.size(); i++ ) {
300 Gtk::Widget* thing = manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, _view, _baseSize, _ratio));
302 if ( !_insides ) {
303 calcGridSize( thing, items.size(), width, height );
304 _insides = manage(new Gtk::Table( height, width ));
305 }
307 _insides->attach( *thing, col, col+1, row, row+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
308 if ( ++col >= width ) {
309 col = 0;
310 row++;
311 }
312 }
313 if ( !_insides ) {
314 _insides = manage(new Gtk::Table( 1, 2 ));
315 }
317 _scroller->add( *_insides );
318 }
320 _scroller->show_all_children();
321 _scroller->queue_draw();
322 }
328 } //namespace UI
329 } //namespace Inkscape
332 /*
333 Local Variables:
334 mode:c++
335 c-file-style:"stroustrup"
336 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
337 indent-tabs-mode:nil
338 fill-column:99
339 End:
340 */
341 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :