Code

Disable the page selector when there's only one page
[inkscape.git] / src / widgets / font-selector.cpp
index 7aff7ee3d643f6278d26e3fa445195a2d9863391..bb155fc7c6470e1f22b756dda398006e9dc99372 100644 (file)
@@ -6,10 +6,12 @@
  * Authors:
  *   Chris Lahey <clahey@ximian.com>
  *   Lauris Kaplinski <lauris@kaplinski.com>
- *   bulia byak <buliabyak@users.sf.net>
+ *   bulia byak <buliabyak@users.sf.net> 
+ *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
  *
  * Copyright (C) 1999-2001 Ximian, Inc.
  * Copyright (C) 2002 Lauris Kaplinski
+ * Copyright (C) -2007 Authors
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
@@ -307,7 +309,16 @@ static void sp_font_selector_style_select_row (GtkTreeSelection *selection,
 
 static void sp_font_selector_size_changed (GtkComboBox *cbox, SPFontSelector *fsel)
 {
+#if GTK_CHECK_VERSION(2,6,0)
     char *sstr = gtk_combo_box_get_active_text (GTK_COMBO_BOX (fsel->size));
+#else // GTK_CHECK_VERSION(2,6,0)
+    GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (fsel->size));
+    GtkTreeIter iter;
+    char *sstr = NULL;
+
+    if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (fsel->size), &iter) && model)
+        gtk_tree_model_get (model, &iter, 0, &sstr, -1);
+#endif // GTK_CHECK_VERSION(2,6,0)
     gfloat old_size = fsel->fontsize;
     fsel->fontsize = MAX(atof(sstr), 0.1);
     if ( fabs(fsel->fontsize-old_size) > 0.001)
@@ -383,42 +394,48 @@ GtkWidget *sp_font_selector_new()
 
 void sp_font_selector_set_font (SPFontSelector *fsel, font_instance *font, double size)
 {
-       
-    if (font && (fsel->font != font || size != fsel->fontsize))
+    if (font)
     {
             gchar family[256];
             font->Family (family, 256);
+            
+            Gtk::TreePath path;
 
-            Inkscape::FontLister *fontlister = Inkscape::FontLister::get_instance();
-            Gtk::TreePath path = fontlister->get_row_for_font (family);
+            try {
+                path = Inkscape::FontLister::get_instance()->get_row_for_font (family);
+            } catch (...) {
+                return;
+            }
 
             fsel->block_emit = TRUE;
             gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview)), path.gobj());
             gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->family_treeview), path.gobj(), NULL, TRUE, 0.5, 0.5);
             fsel->block_emit = FALSE;
 
-            unsigned int i = path[0];
-
-            gchar descr[256];
-            font->Name(descr, 256);
-
             GList *list = 0;
             GtkTreeIter iter;
             GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(fsel->family_treeview));
             gtk_tree_model_get_iter (model, &iter, path.gobj());
             gtk_tree_model_get (model, &iter, 1, &list, -1);
 
-            std::string descr_best (descr);
+            gchar descr[256];
+            font->Name(descr, 256);
+            std::string descr_best (family);
+            descr_best += " ";
             descr_best += ((char*)list->data);
 
             PangoFontDescription *descr_ = pango_font_description_from_string(descr);
             PangoFontDescription *best_ = pango_font_description_from_string(descr_best.c_str());
 
+            unsigned int i = 0;
             unsigned int best_i = 0;
-
-            for ( ; list ; list = list->next)
+            
+            // try to find best match with style description (i.e. bold, italic ?)                         
+            for (list = list->next ; list ; list = list->next)
             {
-                std::string descr_try (descr);
+                i++;
+                std::string descr_try (family);
+                descr_try += " ";
                 descr_try += ((char*)list->data);
                 PangoFontDescription *try_ = pango_font_description_from_string(descr_try.c_str());
                 if (pango_font_description_better_match (descr_, best_, try_))
@@ -428,9 +445,10 @@ void sp_font_selector_set_font (SPFontSelector *fsel, font_instance *font, doubl
                     best_i = i;
                 }
                 pango_font_description_free(try_);
-                ++i;
             }
+            pango_font_description_free(descr_);
+            pango_font_description_free(best_);
+
             GtkTreePath *path_c = gtk_tree_path_new ();
             gtk_tree_path_append_index (path_c, best_i);
             gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview)), path_c);
@@ -608,11 +626,13 @@ static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event)
                         hpos[len] = base_pt[0];
                         len++;
                         if ( curF ) {
-                            NR::Rect nbbox = curF->BBox(str_text->glyph_text[i].gl);
-                            bbox.x0 = MIN(bbox.x0, base_pt[NR::X] + theSize * (nbbox.min())[0]);
-                            bbox.y0 = MIN(bbox.y0, base_pt[NR::Y] - theSize * (nbbox.max())[1]);
-                            bbox.x1 = MAX(bbox.x1, base_pt[NR::X] + theSize * (nbbox.max())[0]);
-                            bbox.y1 = MAX(bbox.y1, base_pt[NR::Y] - theSize * (nbbox.min())[1]);
+                            NR::Maybe<NR::Rect> nbbox = curF->BBox(str_text->glyph_text[i].gl);
+                            if (nbbox) {
+                                bbox.x0 = MIN(bbox.x0, base_pt[NR::X] + theSize * (nbbox->min())[0]);
+                                bbox.y0 = MIN(bbox.y0, base_pt[NR::Y] - theSize * (nbbox->max())[1]);
+                                bbox.x1 = MAX(bbox.x1, base_pt[NR::X] + theSize * (nbbox->max())[0]);
+                                bbox.y1 = MAX(bbox.y1, base_pt[NR::Y] - theSize * (nbbox->min())[1]);
+                            }
                         }
                     }
                     if ( curF ) {