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(Gtk::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(Gtk::BuiltinIconSize 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 = _scroller->size_request();
193 int currW = _scroller->get_width();
194 if ( currW > req.width ) {
195 req.width = currW;
196 }
198 Gtk::HScrollbar* hs = dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->get_hscrollbar();
199 if ( hs ) {
200 Gtk::Requisition scrollReq = hs->size_request();
202 // the +8 is a temporary hack
203 req.height -= scrollReq.height + 8;
204 }
206 Gtk::Requisition req2 = thing->size_request();
208 int h2 = req.height / req2.height;
209 int w2 = req.width / req2.width;
210 width = (itemCount + (h2 - 1)) / h2;
211 if ( width < w2 ) {
212 width = w2;
213 }
214 } else {
215 width = _baseSize == Gtk::ICON_SIZE_MENU ? COLUMNS_FOR_SMALL : COLUMNS_FOR_LARGE;
216 if ( _prefCols > 0 ) {
217 width = _prefCols;
218 }
219 height = (itemCount + (width - 1)) / width;
220 if ( height < 1 ) {
221 height = 1;
222 }
223 }
224 }
226 void PreviewHolder::rebuildUI()
227 {
228 _scroller->remove();
229 _insides = 0; // remove() call should have deleted the Gtk::Table.
231 if ( _view == VIEW_TYPE_LIST ) {
232 _insides = manage(new Gtk::Table( 1, 2 ));
233 _insides->set_col_spacings( 8 );
235 for ( unsigned int i = 0; i < items.size(); i++ ) {
236 Gtk::Widget* label = manage(items[i]->getPreview(PREVIEW_STYLE_BLURB, _view, _baseSize));
237 //label->set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
239 Gtk::Widget* thing = manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, _view, _baseSize));
241 _insides->attach( *thing, 0, 1, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
242 _insides->attach( *label, 1, 2, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::SHRINK );
243 }
244 _scroller->add( *_insides );
245 } else {
246 int col = 0;
247 int row = 0;
248 int width = 2;
249 int height = 1;
251 for ( unsigned int i = 0; i < items.size(); i++ ) {
252 Gtk::Widget* thing = manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, _view, _baseSize));
254 if ( !_insides ) {
255 calcGridSize( thing, items.size(), width, height );
256 _insides = manage(new Gtk::Table( height, width ));
257 }
259 _insides->attach( *thing, col, col+1, row, row+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
260 if ( ++col >= width ) {
261 col = 0;
262 row++;
263 }
264 }
265 if ( !_insides ) {
266 _insides = manage(new Gtk::Table( 1, 2 ));
267 }
269 _scroller->add( *_insides );
270 }
272 _scroller->show_all_children();
273 _scroller->queue_draw();
274 }
280 } //namespace UI
281 } //namespace Inkscape
284 /*
285 Local Variables:
286 mode:c++
287 c-file-style:"stroustrup"
288 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
289 indent-tabs-mode:nil
290 fill-column:99
291 End:
292 */
293 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :