Code

fix compile
[inkscape.git] / src / ui / previewholder.cpp
index cc2339c9cf3f7dba860d7867a9125d413a34b7bc..1717e5053c0cc2b5f1d5fe2e6254a7abc24b7433 100644 (file)
@@ -31,9 +31,13 @@ PreviewHolder::PreviewHolder() :
     VBox(),
     PreviewFillable(),
     _scroller(0),
+    _insides(0),
+    _prefCols(0),
+    _updatesFrozen(false),
     _anchor(Gtk::ANCHOR_CENTER),
-    _baseSize(Gtk::ICON_SIZE_MENU),
-    _view(VIEW_TYPE_LIST)
+    _baseSize(Inkscape::ICON_SIZE_MENU),
+    _view(VIEW_TYPE_LIST),
+    _wrap(false)
 {
     _scroller = manage(new Gtk::ScrolledWindow());
     _insides = manage(new Gtk::Table( 1, 2 ));
@@ -63,41 +67,64 @@ void PreviewHolder::clear()
 void PreviewHolder::addPreview( Previewable* preview )
 {
     items.push_back(preview);
-    int i = items.size() - 1;
+    if ( !_updatesFrozen )
+    {
+        int i = items.size() - 1;
 
-    if ( _view == VIEW_TYPE_LIST ) {
-        Gtk::Widget* label = manage(preview->getPreview(PREVIEW_STYLE_BLURB, VIEW_TYPE_LIST, _baseSize));
-        Gtk::Widget* thing = manage(preview->getPreview(PREVIEW_STYLE_PREVIEW, VIEW_TYPE_LIST, _baseSize));
+        if ( _view == VIEW_TYPE_LIST ) {
+            Gtk::Widget* label = manage(preview->getPreview(PREVIEW_STYLE_BLURB, VIEW_TYPE_LIST, _baseSize));
+            Gtk::Widget* thing = manage(preview->getPreview(PREVIEW_STYLE_PREVIEW, VIEW_TYPE_LIST, _baseSize));
 
-        _insides->attach( *thing, 0, 1, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
-        _insides->attach( *label, 1, 2, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::SHRINK );
-    } else {
-        Gtk::Widget* thing = manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, VIEW_TYPE_GRID, _baseSize));
-
-        int width = 1;
-        int height = 1;
-        calcGridSize( thing, items.size(), width, height );
-        int col = i % width;
-        int row = i / width;
-
-        if ( i < 10 ) {
-            g_message( "i:%d  width:%d  height:%d   prop cols:%d", i, width, height, (int)_insides->property_n_columns() );
+            _insides->attach( *thing, 0, 1, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
+            _insides->attach( *label, 1, 2, i, i+1, Gtk::FILL|Gtk::EXPAND, Gtk::SHRINK );
+        } else {
+            Gtk::Widget* thing = manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, VIEW_TYPE_GRID, _baseSize));
+
+            int width = 1;
+            int height = 1;
+            calcGridSize( thing, items.size(), width, height );
+            int col = i % width;
+            int row = i / width;
+
+            if ( _insides && width > (int)_insides->property_n_columns() ) {
+                std::vector<Gtk::Widget*>kids = _insides->get_children();
+                int oldWidth = (int)_insides->property_n_columns();
+                int childCount = (int)kids.size();
+//             g_message("  %3d  resize from %d to %d  (r:%d, c:%d)  with %d children", i, oldWidth, width, row, col, childCount );
+                _insides->resize( height, width );
+
+                for ( int j = oldWidth; j < childCount; j++ ) {
+                    Gtk::Widget* target = kids[childCount - (j + 1)];
+                    int col2 = j % width;
+                    int row2 = j / width;
+                    Glib::RefPtr<Gtk::Widget> handle(target);
+                    _insides->remove( *target );
+                    _insides->attach( *target, col2, col2+1, row2, row2+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
+                }
+            } else if ( col == 0 ) {
+                // we just started a new row
+                _insides->resize( row + 1, width );
+            }
+            _insides->attach( *thing, col, col+1, row, row+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
         }
 
-        if ( col == 0 ) {
-            // we just started a new row
-            _insides->resize( row + 1, width );
-        } else if ( _insides && width > (int)_insides->property_n_columns() ) {
-            _insides->resize( height, width );
-        }
-        _insides->attach( *thing, col, col+1, row, row+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND );
+        _scroller->show_all_children();
+        _scroller->queue_draw();
     }
+}
 
-    _scroller->show_all_children();
-    _scroller->queue_draw();
+void PreviewHolder::freezeUpdates()
+{
+    _updatesFrozen = true;
 }
 
-void PreviewHolder::setStyle(Gtk::BuiltinIconSize size, ViewType view)
+void PreviewHolder::thawUpdates()
+{
+    _updatesFrozen = false;
+    rebuildUI();
+}
+
+void PreviewHolder::setStyle( Inkscape::IconSize size, ViewType view )
 {
     if ( size != _baseSize || view != _view ) {
         _baseSize = size;
@@ -116,7 +143,7 @@ void PreviewHolder::setOrientation( Gtk::AnchorType how )
             case Gtk::ANCHOR_NORTH:
             case Gtk::ANCHOR_SOUTH:
             {
-                dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_ALWAYS, Gtk::POLICY_AUTOMATIC );
+                dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_AUTOMATIC, _wrap ? Gtk::POLICY_AUTOMATIC : Gtk::POLICY_NEVER );
             }
             break;
 
@@ -136,6 +163,28 @@ void PreviewHolder::setOrientation( Gtk::AnchorType how )
     }
 }
 
+void PreviewHolder::setWrap( bool b )
+{
+    if ( b != _wrap ) {
+        _wrap = b;
+        switch ( _anchor )
+        {
+            case Gtk::ANCHOR_NORTH:
+            case Gtk::ANCHOR_SOUTH:
+            {
+                dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->set_policy( Gtk::POLICY_AUTOMATIC, _wrap ? Gtk::POLICY_AUTOMATIC : Gtk::POLICY_NEVER );
+            }
+            break;
+            default:
+            {
+                (void)0;
+                // do nothing;
+            }
+        }
+        rebuildUI();
+    }
+}
+
 void PreviewHolder::setColumnPref( int cols )
 {
     _prefCols = cols;
@@ -163,7 +212,8 @@ void PreviewHolder::calcGridSize( const Gtk::Widget* thing, int itemCount, int&
     height = 1;
 
     if ( _anchor == Gtk::ANCHOR_SOUTH || _anchor == Gtk::ANCHOR_NORTH ) {
-        Gtk::Requisition req = _scroller->size_request();
+        Gtk::Requisition req;
+        _scroller->size_request(req);
         int currW = _scroller->get_width();
         if ( currW > req.width ) {
             req.width = currW;
@@ -171,22 +221,24 @@ void PreviewHolder::calcGridSize( const Gtk::Widget* thing, int itemCount, int&
 
         Gtk::HScrollbar* hs = dynamic_cast<Gtk::ScrolledWindow*>(_scroller)->get_hscrollbar();
         if ( hs ) {
-            Gtk::Requisition scrollReq = hs->size_request();
+            Gtk::Requisition scrollReq;
+            hs->size_request(scrollReq);
 
             // the +8 is a temporary hack
             req.height -= scrollReq.height + 8;
         }
 
-        Gtk::Requisition req2 = thing->size_request();
+        Gtk::Requisition req2;
+        const_cast<Gtk::Widget*>(thing)->size_request(req2);
 
-        int h2 = req.height / req2.height;
-        int w2 = req.width / req2.width;
+        int h2 = ((req2.height > 0) && (req.height > req2.height)) ? (req.height / req2.height) : 1;
+        int w2 = ((req2.width > 0) && (req.width > req2.width)) ? (req.width / req2.width) : 1;
         width = (itemCount + (h2 - 1)) / h2;
         if ( width < w2 ) {
             width = w2;
         }
     } else {
-        width = _baseSize == Gtk::ICON_SIZE_MENU ? COLUMNS_FOR_SMALL : COLUMNS_FOR_LARGE;
+        width = (_baseSize == Inkscape::ICON_SIZE_MENU || _baseSize == Inkscape::ICON_SIZE_DECORATION) ? COLUMNS_FOR_SMALL : COLUMNS_FOR_LARGE;
         if ( _prefCols > 0 ) {
             width = _prefCols;
         }