summaryrefslogtreecommitdiff
path: root/gtk/gtkstyle.c
diff options
context:
space:
mode:
authorOwen Taylor <otaylor@redhat.com>2002-03-03 19:59:29 +0000
committerOwen Taylor <otaylor@src.gnome.org>2002-03-03 19:59:29 +0000
commitc848fbbcfe7813b6347a00452a890aeb17e767bb (patch)
tree91b2e23d465dc215130c97d2d6d46cc8a8fe3739 /gtk/gtkstyle.c
parent5d5cfcd435d00ffa11ffce63d3b708904faf2182 (diff)
downloadgtk+-c848fbbcfe7813b6347a00452a890aeb17e767bb.tar.gz
Centralize lookup and caching of cursor GC's here.
Sun Mar 3 14:26:33 2002 Owen Taylor <otaylor@redhat.com> * gtk/gtkstyle.[ch] (_gtk_get_insertion_cursor_gc): Centralize lookup and caching of cursor GC's here. * gtk/gtkentry.[ch] gtk/gtktextview.[ch] gtk/gtktextdisplay.c gtk/gtklabel.c: Move to using _gtk_get_insertion_cursor_gc(). * gtk/gtkentry.c (gtk_entry_expose): Draw cursors under the text, not over the text. * gtk/gtkwidget.c (gtk_widget_class_init): add a secondary-cursor-color property. * gtk/gtkwidget.c (gtk_widget_class_init): Decrease the default aspect ratio to 1/25 (0.04). * tests/testgtkrc (bell_duration): Comment out cursor color setting.
Diffstat (limited to 'gtk/gtkstyle.c')
-rw-r--r--gtk/gtkstyle.c133
1 files changed, 133 insertions, 0 deletions
diff --git a/gtk/gtkstyle.c b/gtk/gtkstyle.c
index 472a20c418..5ac1002d09 100644
--- a/gtk/gtkstyle.c
+++ b/gtk/gtkstyle.c
@@ -312,6 +312,7 @@ static void hls_to_rgb (gdouble *h,
gdouble *l,
gdouble *s);
+static void style_unrealize_cursor_gcs (GtkStyle *style);
/*
* Data for default check and radio buttons
@@ -1774,6 +1775,8 @@ gtk_style_real_unrealize (GtkStyle *style)
gdk_colormap_free_colors (style->colormap, style->text, 5);
gdk_colormap_free_colors (style->colormap, style->base, 5);
gdk_colormap_free_colors (style->colormap, style->text_aa, 5);
+
+ style_unrealize_cursor_gcs (style);
}
static void
@@ -5645,6 +5648,136 @@ gtk_style_set_font (GtkStyle *style,
}
}
+typedef struct _CursorInfo CursorInfo;
+
+struct _CursorInfo
+{
+ GType for_type;
+ GdkGC *primary_gc;
+ GdkGC *secondary_gc;
+};
+
+static void
+style_unrealize_cursor_gcs (GtkStyle *style)
+{
+ CursorInfo *
+
+ cursor_info = g_object_get_data (G_OBJECT (style), "gtk-style-cursor-info");
+ if (cursor_info)
+ {
+ if (cursor_info->primary_gc)
+ gtk_gc_release (cursor_info->primary_gc);
+
+ if (cursor_info->secondary_gc)
+ gtk_gc_release (cursor_info->secondary_gc);
+
+ g_free (cursor_info);
+ g_object_set_data (G_OBJECT (style), "gtk-style-cursor-info", NULL);
+ }
+}
+
+static GdkGC *
+make_cursor_gc (GtkWidget *widget,
+ const gchar *property_name,
+ GdkColor *fallback)
+{
+ GdkGCValues gc_values;
+ GdkGCValuesMask gc_values_mask;
+ GdkColor *cursor_color;
+
+ gtk_widget_style_get (widget, property_name, &cursor_color, NULL);
+
+ gc_values_mask = GDK_GC_FOREGROUND;
+ if (cursor_color)
+ {
+ gc_values.foreground = *cursor_color;
+ gdk_color_free (cursor_color);
+ }
+ else
+ gc_values.foreground = *fallback;
+
+ gdk_rgb_find_color (widget->style->colormap, &gc_values.foreground);
+ return gtk_gc_get (widget->style->depth, widget->style->colormap, &gc_values, gc_values_mask);
+}
+
+/**
+ * _gtk_get_insertion_cursor_gc:
+ * @widget: a #GtkWidget
+ * @is_primary: if the cursor should be the primary cursor color.
+ *
+ * Get a GC suitable for drawing the primary or secondary text
+ * cursor.
+ *
+ * Note: the return value is ref'ed because calls to this function
+ * on other widgets could result in this the GC being released
+ * which would be an unexpected side effect. If made public,
+ * this function should possibly be called create_insertion_cursor_gc().
+ *
+ * Return value: an appropriate #GdkGC. Call g_object_unref() on
+ * the gc when you are done with it; this GC may be shared with
+ * other users, so you must not modify the GC except for temporarily
+ * setting the clip before drawing with the GC, and then unsetting the clip
+ * again afterwards.
+ **/
+GdkGC *
+_gtk_get_insertion_cursor_gc (GtkWidget *widget,
+ gboolean is_primary)
+{
+ CursorInfo *cursor_info;
+
+ cursor_info = g_object_get_data (G_OBJECT (widget->style), "gtk-style-cursor-info");
+ if (!cursor_info)
+ {
+ cursor_info = g_new (CursorInfo, 1);
+ g_object_set_data (G_OBJECT (widget->style), "gtk-style-cursor-info", cursor_info);
+ cursor_info->primary_gc = NULL;
+ cursor_info->secondary_gc = NULL;
+ cursor_info->for_type = G_TYPE_INVALID;
+ }
+
+ /* We have to keep track of the type because gtk_widget_style_get()
+ * can return different results when called on the same property and
+ * same style but for different widgets. :-(. That is,
+ * GtkEntry::cursor-color = "red" in a style will modify the cursor
+ * color for entries but not for text view.
+ */
+ if (cursor_info->for_type != G_OBJECT_TYPE (widget))
+ {
+ cursor_info->for_type = G_OBJECT_TYPE (widget);
+ if (cursor_info->primary_gc)
+ {
+ gtk_gc_release (cursor_info->primary_gc);
+ cursor_info->primary_gc = NULL;
+ }
+ if (cursor_info->secondary_gc)
+ {
+ gtk_gc_release (cursor_info->secondary_gc);
+ cursor_info->secondary_gc = NULL;
+ }
+ }
+
+ if (is_primary)
+ {
+ if (!cursor_info->primary_gc)
+ cursor_info->primary_gc = make_cursor_gc (widget,
+ "cursor-color",
+ &widget->style->black);
+
+ return g_object_ref (cursor_info->primary_gc);
+ }
+ else
+ {
+ static GdkColor gray = { 0, 0x8888, 0x8888, 0x8888 };
+
+ if (!cursor_info->secondary_gc)
+ cursor_info->secondary_gc = make_cursor_gc (widget,
+ "secondary-cursor-color",
+ &gray);
+
+ return g_object_ref (cursor_info->secondary_gc);
+ }
+}
+
/**
* _gtk_draw_insertion_cursor:
* @widget: a #GtkWidget