]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkfontchooserwidget.c
Popdown the bubble window when text view is scrolled
[~andy/gtk] / gtk / gtkfontchooserwidget.c
index 4aeabfb6506ed794b3f8fb9f5587fdb9a4fd0ad3..7454a35186a98fa2db42f83a5161f329a69c5cbe 100644 (file)
@@ -12,9 +12,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "config.h"
 #include <atk/atk.h>
 
 #include "gtkfontchooserwidget.h"
-#include "gtkfontchooser.h"
-#include "gtkfontchooserutils.h"
+
+#include "gtkadjustment.h"
+#include "gtkbuildable.h"
+#include "gtkbox.h"
 #include "gtkcellrenderertext.h"
 #include "gtkentry.h"
-#include "gtkframe.h"
-#include "gtkbbox.h"
-#include "gtkbox.h"
+#include "gtksearchentry.h"
+#include "gtkgrid.h"
+#include "gtkfontchooser.h"
+#include "gtkfontchooserutils.h"
+#include "gtkintl.h"
 #include "gtklabel.h"
 #include "gtkliststore.h"
-#include "gtkstock.h"
-#include "gtktextview.h"
-#include "gtktreeselection.h"
-#include "gtktreeview.h"
-#include "gtkscrolledwindow.h"
-#include "gtkintl.h"
-#include "gtkaccessible.h"
-#include "gtkbuildable.h"
+#include "gtknotebook.h"
 #include "gtkprivate.h"
 #include "gtkscale.h"
+#include "gtkscrolledwindow.h"
 #include "gtkspinbutton.h"
-#include "gtknotebook.h"
+#include "gtktextview.h"
+#include "gtktreeselection.h"
+#include "gtktreeview.h"
 #include "gtkwidget.h"
-#include "gtkgrid.h"
 
 /**
- * SECTION:gtkfontchooser
+ * SECTION:gtkfontchooserwidget
  * @Short_description: A widget for selecting fonts
  * @Title: GtkFontChooserWidget
  * @See_also: #GtkFontChooserDialog
@@ -78,6 +75,7 @@ struct _GtkFontChooserWidgetPrivate
 {
   GtkWidget    *search_entry;
   GtkWidget    *family_face_list;
+  GtkCellRenderer *family_face_cell;
   GtkWidget    *list_scrolled_window;
   GtkWidget    *empty_list;
   GtkWidget    *list_notebook;
@@ -245,55 +243,13 @@ gtk_font_chooser_widget_refilter_font_list (GtkFontChooserWidget *fontchooser)
 }
 
 static void
-text_changed_cb (GtkEntry       *entry,
-                 GParamSpec     *pspec,
+text_changed_cb (GtkEntry             *entry,
+                 GParamSpec           *pspec,
                  GtkFontChooserWidget *fc)
 {
-  GtkFontChooserWidgetPrivate *priv = fc->priv;
-  const gchar *text;
-
-  text = gtk_entry_get_text (entry);
-
-  if (text == NULL || text[0] == '\0')
-    {
-      GIcon *icon;
-
-      icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
-      g_object_set (G_OBJECT (priv->search_entry),
-                    "secondary-icon-gicon", icon,
-                    "secondary-icon-activatable", FALSE,
-                    "secondary-icon-sensitive", FALSE,
-                    NULL);
-      g_object_unref (icon);
-    }
-  else
-    {
-      if (!gtk_entry_get_icon_activatable (GTK_ENTRY (priv->search_entry), GTK_ENTRY_ICON_SECONDARY))
-        {
-          GIcon *icon;
-
-          icon = g_themed_icon_new_with_default_fallbacks ("edit-clear-symbolic");
-          g_object_set (G_OBJECT (priv->search_entry),
-                        "secondary-icon-gicon", icon,
-                        "secondary-icon-activatable", TRUE,
-                        "secondary-icon-sensitive", TRUE,
-                        NULL);
-          g_object_unref (icon);
-        }
-    }
-
   gtk_font_chooser_widget_refilter_font_list (fc);
 }
 
-static void
-icon_press_cb (GtkEntry             *entry,
-               GtkEntryIconPosition  pos,
-               GdkEvent             *event,
-               gpointer              user_data)
-{
-  gtk_entry_set_text (entry, "");
-}
-
 static void
 size_change_cb (GtkAdjustment *adjustment,
                 gpointer       user_data)
@@ -510,7 +466,6 @@ row_deleted_cb  (GtkTreeModel *model,
 static void
 gtk_font_chooser_widget_init (GtkFontChooserWidget *fontchooser)
 {
-  GIcon *icon;
   GtkFontChooserWidgetPrivate *priv;
   GtkWidget *scrolled_win;
   GtkWidget *grid;
@@ -529,8 +484,9 @@ gtk_font_chooser_widget_init (GtkFontChooserWidget *fontchooser)
   gtk_widget_push_composite_child ();
 
   /* Creating fundamental widgets for the private struct */
-  priv->search_entry = gtk_entry_new ();
+  priv->search_entry = gtk_search_entry_new ();
   priv->family_face_list = gtk_tree_view_new ();
+  gtk_tree_view_set_enable_search (GTK_TREE_VIEW (priv->family_face_list), FALSE);
   priv->preview = gtk_entry_new ();
   priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
                                                 0.0,
@@ -598,22 +554,11 @@ gtk_font_chooser_widget_init (GtkFontChooserWidget *fontchooser)
   gtk_entry_set_text (GTK_ENTRY (priv->preview),
                       pango_language_get_sample_string (NULL));
 
-  /* Set search icon and place holder text */
-  icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
-  g_object_set (G_OBJECT (priv->search_entry),
-                "secondary-icon-gicon", icon,
-                "secondary-icon-activatable", FALSE,
-                "secondary-icon-sensitive", FALSE,
-                NULL);
-  g_object_unref (icon);
-
   gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
 
-  /** Callback connections **/
+  /* Callback connections */
   g_signal_connect (priv->search_entry, "notify::text",
                     G_CALLBACK (text_changed_cb), fontchooser);
-  g_signal_connect (priv->search_entry,
-                    "icon-press", G_CALLBACK (icon_press_cb), NULL);
 
   g_signal_connect (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
                     "value-changed", G_CALLBACK (size_change_cb), fontchooser);
@@ -792,6 +737,58 @@ visible_func (GtkTreeModel *model,
   return result;
 }
 
+/* in pango units */
+static int
+gtk_font_chooser_widget_get_preview_text_height (GtkFontChooserWidget *fontchooser)
+{
+  GtkWidget *treeview = fontchooser->priv->family_face_list;
+  double dpi, font_size;
+
+  dpi = gdk_screen_get_resolution (gtk_widget_get_screen (treeview));
+  gtk_style_context_get (gtk_widget_get_style_context (treeview),
+                         gtk_widget_get_state_flags (treeview),
+                         "font-size", &font_size,
+                         NULL);
+
+  return (dpi < 0.0 ? 96.0 : dpi) / 72.0 * PANGO_SCALE_X_LARGE * font_size * PANGO_SCALE;
+}
+
+static PangoAttrList *
+gtk_font_chooser_widget_get_preview_attributes (GtkFontChooserWidget       *fontchooser,
+                                                const PangoFontDescription *font_desc,
+                                                gsize                       first_line_len)
+{
+  PangoAttribute *attribute;
+  PangoAttrList *attrs;
+
+  attrs = pango_attr_list_new ();
+
+  attribute = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
+  attribute->end_index = first_line_len;
+  pango_attr_list_insert (attrs, attribute);
+
+  attribute = pango_attr_scale_new (PANGO_SCALE_SMALL);
+  attribute->end_index = first_line_len;
+  pango_attr_list_insert (attrs, attribute);
+
+  if (font_desc)
+    {
+      attribute = pango_attr_font_desc_new (font_desc);
+      attribute->start_index = first_line_len;
+      pango_attr_list_insert (attrs, attribute);
+    }
+
+  attribute = pango_attr_fallback_new (FALSE);
+  attribute->start_index = first_line_len;
+  pango_attr_list_insert (attrs, attribute);
+
+  attribute = pango_attr_size_new_absolute (gtk_font_chooser_widget_get_preview_text_height (fontchooser));
+  attribute->start_index = first_line_len;
+  pango_attr_list_insert (attrs, attribute);
+
+  return attrs;
+}
+
 static void
 gtk_font_chooser_widget_cell_data_func (GtkTreeViewColumn *column,
                                         GtkCellRenderer   *cell,
@@ -802,34 +799,19 @@ gtk_font_chooser_widget_cell_data_func (GtkTreeViewColumn *column,
   GtkFontChooserWidget *fontchooser = user_data;
   PangoFontDescription *font_desc;
   PangoAttrList *attrs;
-  PangoAttribute *attribute;
   char *to_string, *text;
-  gsize to_string_len;
+  gsize first_line_len;
 
   font_desc = tree_model_get_font_description (tree_model, iter);
 
   to_string = pango_font_description_to_string (font_desc);
-  to_string_len = strlen (to_string) + 1;
 
   text = g_strconcat (to_string, "\n", fontchooser->priv->preview_text, NULL);
+  first_line_len = strlen (to_string) + 1;
   
-  attrs = pango_attr_list_new ();
-
-  attribute = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
-  attribute->end_index = to_string_len;
-  pango_attr_list_insert (attrs, attribute);
-
-  attribute = pango_attr_scale_new (PANGO_SCALE_SMALL);
-  attribute->end_index = to_string_len;
-  pango_attr_list_insert (attrs, attribute);
-
-  attribute = pango_attr_font_desc_new (font_desc);
-  attribute->start_index = to_string_len;
-  pango_attr_list_insert (attrs, attribute);
-
-  attribute = pango_attr_scale_new (PANGO_SCALE_X_LARGE);
-  attribute->start_index = to_string_len;
-  pango_attr_list_insert (attrs, attribute);
+  attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser, 
+                                                          font_desc,
+                                                          first_line_len);
 
   g_object_set (cell,
                 "attributes", attrs,
@@ -842,14 +824,47 @@ gtk_font_chooser_widget_cell_data_func (GtkTreeViewColumn *column,
   g_free (text);
 }
 
+static void
+gtk_font_chooser_widget_set_cell_size (GtkFontChooserWidget *fontchooser)
+{
+  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
+  PangoAttrList *attrs;
+  GtkRequisition size;
+
+  gtk_cell_renderer_set_fixed_size (priv->family_face_cell, -1, -1);
+
+  attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser, 
+                                                          NULL,
+                                                          1);
+  
+  g_object_set (priv->family_face_cell,
+                "attributes", attrs,
+                "text", "x\nx",
+                NULL);
+
+  pango_attr_list_unref (attrs);
+
+  gtk_cell_renderer_get_preferred_size (priv->family_face_cell,
+                                        priv->family_face_list,
+                                        &size,
+                                        NULL);
+  gtk_cell_renderer_set_fixed_size (priv->family_face_cell, size.width, size.height);
+}
+
 static void
 gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser)
 {
   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
   GtkTreeView *treeview = GTK_TREE_VIEW (priv->family_face_list);
-  GtkCellRenderer *cell;
   GtkTreeViewColumn *col;
 
+  g_signal_connect_data (priv->family_face_list,
+                         "style-updated",
+                         G_CALLBACK (gtk_font_chooser_widget_set_cell_size),
+                         fontchooser,
+                         NULL,
+                         G_CONNECT_AFTER | G_CONNECT_SWAPPED);
+
   priv->model = GTK_TREE_MODEL (gtk_list_store_new (4,
                                                     PANGO_TYPE_FONT_FAMILY,
                                                     PANGO_TYPE_FONT_FACE,
@@ -867,15 +882,17 @@ gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser)
 
   gtk_tree_view_set_rules_hint      (treeview, TRUE);
   gtk_tree_view_set_headers_visible (treeview, FALSE);
+  gtk_tree_view_set_fixed_height_mode (treeview, TRUE);
 
-  cell = gtk_cell_renderer_text_new ();
-  g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
+  priv->family_face_cell = gtk_cell_renderer_text_new ();
+  g_object_set (priv->family_face_cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
 
   col = gtk_tree_view_column_new ();
   gtk_tree_view_column_set_title (col, _("Font Family"));
-  gtk_tree_view_column_pack_start (col, cell, TRUE);
+  gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
+  gtk_tree_view_column_pack_start (col, priv->family_face_cell, TRUE);
   gtk_tree_view_column_set_cell_data_func (col,
-                                           cell,
+                                           priv->family_face_cell,
                                            gtk_font_chooser_widget_cell_data_func,
                                            fontchooser,
                                            NULL);
@@ -883,6 +900,8 @@ gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser)
   gtk_tree_view_append_column (treeview, col);
 
   gtk_font_chooser_widget_load_fonts (fontchooser);
+
+  gtk_font_chooser_widget_set_cell_size (fontchooser);
 }
 
 static void
@@ -1043,7 +1062,13 @@ gtk_font_chooser_widget_ensure_selection (GtkFontChooserWidget *fontchooser)
                                                         &filter_iter,
                                                         &priv->font_iter))
     {
+      GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter_model),
+                                                   &filter_iter);
+
       gtk_tree_selection_select_iter (selection, &filter_iter);
+      gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->family_face_list),
+                                    path, NULL, FALSE, 0.0, 0.0);
+      gtk_tree_path_free (path);
     }
   else
     {