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