da991e1dfc85756e7abf220be1c5ba08325ca41c
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 rebuildUI();
66 }
68 void PreviewHolder::addPreview( Previewable* preview )
69 {
70 items.push_back(preview);
71 if ( !_updatesFrozen )
72 {
73 int i = items.size() - 1;
75 if ( _view == VIEW_TYPE_LIST ) {
76 Gtk::Widget* label = manage(preview->getPreview(PREVIEW_STYLE_BLURB, VIEW_TYPE_LIST, _baseSize, _ratio));
77 Gtk::Widget* thing = manage(preview->getPreview(PREVIEW_STYLE_PREVIEW, VIEW_TYPE_LIST, _baseSize, _ratio));
79 _insides->attach( *thing, 0, 1, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
80 _insides->attach( *label, 1, 2, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::SHRINK );
81 } else {
82 Gtk::Widget* thing = manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, VIEW_TYPE_GRID, _baseSize, _ratio));
84 int width = 1;
85 int height = 1;
86 calcGridSize( thing, items.size(), width, height );
87 int col = i % width;
88 int row = i / width;
90 if ( _insides && width > (int)_insides->property_n_columns() ) {
91 std::vector<Gtk::Widget*>kids = _insides->get_children();
92 int oldWidth = (int)_insides->property_n_columns();
93 int childCount = (int)kids.size();
94 // g_message(" %3d resize from %d to %d (r:%d, c:%d) with %d children", i, oldWidth, width, row, col, childCount );
95 _insides->resize( height, width );
97 for ( int j = oldWidth; j < childCount; j++ ) {
98 Gtk::Widget* target = kids[childCount - (j + 1)];
99 int col2 = j % width;
100 int row2 = j / width;
101 Glib::RefPtr<Gtk::Widget> handle(target);
102 _insides->remove( *target );
103 _insides->attach( *target, col2, col2+1, row2, row2+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
104 }
105 } else if ( col == 0 ) {
106 // we just started a new row
107 _insides->resize( row + 1, width );
108 }
109 _insides->attach( *thing, col, col+1, row, row+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
110 }
112 _scroller->show_all_children();
113 _scroller->queue_draw();
114 }
115 }
117 void PreviewHolder::freezeUpdates()
118 {
119 _updatesFrozen = true;
120 }
122 void PreviewHolder::thawUpdates()
123 {
124 _updatesFrozen = false;
125 rebuildUI();
126 }
128 void PreviewHolder::setStyle( ::PreviewSize size, ViewType view, guint ratio )
129 {
130 if ( size != _baseSize || view != _view || ratio != _ratio ) {
131 _baseSize = size;
132 _view = view;
133 _ratio = ratio;
134 rebuildUI();
135 }
136 }
138 void PreviewHolder::setOrientation( Gtk::AnchorType how )
139 {
140 if ( _anchor != how )
141 {
142 _anchor = how;
143 switch ( _anchor )
144 {
145 case Gtk::ANCHOR_NORTH:
146 case Gtk::ANCHOR_SOUTH:
147 {
148 dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_AUTOMATIC, _wrap ? Gtk::POLICY_AUTOMATIC : Gtk::POLICY_NEVER );
149 }
150 break;
152 case Gtk::ANCHOR_EAST:
153 case Gtk::ANCHOR_WEST:
154 {
155 dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC );
156 }
157 break;
159 default:
160 {
161 dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC );
162 }
163 }
164 rebuildUI();
165 }
166 }
168 void PreviewHolder::setWrap( bool b )
169 {
170 if ( b != _wrap ) {
171 _wrap = b;
172 switch ( _anchor )
173 {
174 case Gtk::ANCHOR_NORTH:
175 case Gtk::ANCHOR_SOUTH:
176 {
177 dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_AUTOMATIC, _wrap ? Gtk::POLICY_AUTOMATIC : Gtk::POLICY_NEVER );
178 }
179 break;
180 default:
181 {
182 (void)0;
183 // do nothing;
184 }
185 }
186 rebuildUI();
187 }
188 }
190 void PreviewHolder::setColumnPref( int cols )
191 {
192 _prefCols = cols;
193 }
195 void PreviewHolder::on_size_allocate( Gtk::Allocation& allocation )
196 {
197 // g_message( "on_size_allocate(%d, %d) (%d, %d)", allocation.get_x(), allocation.get_y(), allocation.get_width(), allocation.get_height() );
198 // g_message(" anchor:%d", _anchor);
199 Gtk::VBox::on_size_allocate( allocation );
200 }
202 void PreviewHolder::on_size_request( Gtk::Requisition* requisition )
203 {
204 // g_message( "on_size_request(%d, %d)", requisition->width, requisition->height );
205 Gtk::VBox::on_size_request( requisition );
206 // g_message( " super (%d, %d)", requisition->width, requisition->height );
207 // g_message(" anchor:%d", _anchor);
208 // g_message(" items:%d", (int)items.size());
209 }
211 void PreviewHolder::calcGridSize( const Gtk::Widget* thing, int itemCount, int& width, int& height )
212 {
213 width = itemCount;
214 height = 1;
216 if ( _anchor == Gtk::ANCHOR_SOUTH || _anchor == Gtk::ANCHOR_NORTH ) {
217 Gtk::Requisition req;
218 _scroller->size_request(req);
219 int currW = _scroller->get_width();
220 if ( currW > req.width ) {
221 req.width = currW;
222 }
224 Gtk::HScrollbar* hs = dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->get_hscrollbar();
225 if ( hs ) {
226 Gtk::Requisition scrollReq;
227 hs->size_request(scrollReq);
229 // the +8 is a temporary hack
230 req.height -= scrollReq.height + 8;
231 }
233 Gtk::Requisition req2;
234 const_cast<Gtk::Widget*>(thing)->size_request(req2);
236 int h2 = ((req2.height > 0) && (req.height > req2.height)) ? (req.height / req2.height) : 1;
237 int w2 = ((req2.width > 0) && (req.width > req2.width)) ? (req.width / req2.width) : 1;
238 width = (itemCount + (h2 - 1)) / h2;
239 if ( width < w2 ) {
240 width = w2;
241 }
242 } else {
243 width = (_baseSize == PREVIEW_SIZE_SMALL || _baseSize == PREVIEW_SIZE_TINY) ? COLUMNS_FOR_SMALL : COLUMNS_FOR_LARGE;
244 if ( _prefCols > 0 ) {
245 width = _prefCols;
246 }
247 height = (itemCount + (width - 1)) / width;
248 if ( height < 1 ) {
249 height = 1;
250 }
251 }
252 }
254 void PreviewHolder::rebuildUI()
255 {
256 _scroller->remove();
257 _insides = 0; // remove() call should have deleted the Gtk::Table.
259 if ( _view == VIEW_TYPE_LIST ) {
260 _insides = manage(new Gtk::Table( 1, 2 ));
261 _insides->set_col_spacings( 8 );
263 for ( unsigned int i = 0; i < items.size(); i++ ) {
264 Gtk::Widget* label = manage(items[i]->getPreview(PREVIEW_STYLE_BLURB, _view, _baseSize, _ratio));
265 //label->set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
267 Gtk::Widget* thing = manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, _view, _baseSize, _ratio));
269 _insides->attach( *thing, 0, 1, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
270 _insides->attach( *label, 1, 2, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::SHRINK );
271 }
272 _scroller->add( *_insides );
273 } else {
274 int col = 0;
275 int row = 0;
276 int width = 2;
277 int height = 1;
279 for ( unsigned int i = 0; i < items.size(); i++ ) {
280 Gtk::Widget* thing = manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, _view, _baseSize, _ratio));
282 if ( !_insides ) {
283 calcGridSize( thing, items.size(), width, height );
284 _insides = manage(new Gtk::Table( height, width ));
285 }
287 _insides->attach( *thing, col, col+1, row, row+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
288 if ( ++col >= width ) {
289 col = 0;
290 row++;
291 }
292 }
293 if ( !_insides ) {
294 _insides = manage(new Gtk::Table( 1, 2 ));
295 }
297 _scroller->add( *_insides );
298 }
300 _scroller->show_all_children();
301 _scroller->queue_draw();
302 }
308 } //namespace UI
309 } //namespace Inkscape
312 /*
313 Local Variables:
314 mode:c++
315 c-file-style:"stroustrup"
316 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
317 indent-tabs-mode:nil
318 fill-column:99
319 End:
320 */
321 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :