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