Commit 32f07575 authored by Darin Adler's avatar Darin Adler
Browse files

Made the index panel use NautilusBackground.

parent 8c92ecec
2000-01-11 Darin Adler <darin@eazel.com>
NautilusBackground is now used for the index panel background.
* libnautilus/nautilus-background.c:
(nautilus_background_attach_to_canvas):
(nautilus_get_widget_background):
(nautilus_gtk_style_get_default_class):
(nautilus_gdk_window_update_sizes):
(nautilus_background_draw_flat_box):
(nautilus_background_get_gtk_style_class):
(nautilus_background_set_widget_style):
(nautilus_background_set_up_canvas):
(nautilus_widget_background_changed):
(nautilus_get_widget_background):
Added new code that changes the background of a widget by
modifying the GtkStyle. For solid colors and tiled images we
can just modify the GtkStyle. For gradients we need to make
a GtkStyle subclass.
* libnautilus/nautilus-background.h:
(nautilus_background_attach_to_canvas):
(nautilus_get_widget_background):
Changed the old interface that was canvas-specific, to a new one
that allows attacking a background to any widget.
* src/ntl-index-panel.h:
Now that the background is attached to the widget, we don't need
a background field in NautilusIndexPanel.
* src/ntl-index-panel.c:
(nautilus_index_panel_set_up_background)
(nautilus_index_panel_finalize):
(nautilus_index_panel_drag_data_received):
(nautilus_index_panel_set_up_info):
Changed the index panel to use NautilusBackground.
* libnautilus/nautilus-background-canvas-group.h:
libnautilus/nautilus-background-canvas-group.c:
(nautilus_background_canvas_group_get_background):
(nautilus_background_canvas_group_set_background):
Moved the code that manages the background object out of the
canvas group code, since we can now attach a background to
any widget.
* libnautilus/gdk-extensions.c:
(nautilus_gdk_color_parse_with_white_default): Changed this helper
to accept NULLs and return the default instead of prohibiting
NULL. This is consistent with the gradient's use of NULL to
represent the default color.
2000-01-11 John Sullivan <sullivan@eazel.com>
* libnautilus/nautilus-gtk-extensions.h:
......
......@@ -391,7 +391,7 @@ void
nautilus_gdk_color_parse_with_white_default (const char *color_spec,
GdkColor *color)
{
if (!gdk_color_parse (color_spec, color)) {
if (color_spec == NULL || !gdk_color_parse (color_spec, color)) {
color->red = 0xFFFF;
color->green = 0xFFFF;
color->blue = 0xFFFF;
......
......@@ -84,8 +84,7 @@ nautilus_background_canvas_group_draw (GnomeCanvasItem *item, GdkDrawable *drawa
NautilusBackground *background;
/* Draw the background. */
background = nautilus_background_canvas_group_get_background
(NAUTILUS_BACKGROUND_CANVAS_GROUP (item));
background = nautilus_get_widget_background(GTK_WIDGET (item->canvas));
if (background != NULL) {
GdkGC *gc;
GdkColormap *colormap;
......@@ -124,31 +123,3 @@ nautilus_background_canvas_group_draw (GnomeCanvasItem *item, GdkDrawable *drawa
drawable_corner_x, drawable_corner_y,
drawable_width, drawable_height));
}
NautilusBackground *
nautilus_background_canvas_group_get_background (NautilusBackgroundCanvasGroup *canvas_group)
{
gpointer data;
data = gtk_object_get_data (GTK_OBJECT (canvas_group), "nautilus_background");
g_assert (data == NULL || NAUTILUS_IS_BACKGROUND (data));
return data;
}
void
nautilus_background_canvas_group_set_background (NautilusBackgroundCanvasGroup *canvas_group,
NautilusBackground *background)
{
NautilusBackground *old_background;
g_return_if_fail (NAUTILUS_IS_BACKGROUND_CANVAS_GROUP (canvas_group));
g_return_if_fail (background == NULL || NAUTILUS_IS_BACKGROUND (background));
old_background = nautilus_background_canvas_group_get_background (canvas_group);
gtk_object_set_data (GTK_OBJECT (canvas_group), "nautilus_background", background);
if (background != NULL)
gtk_object_ref (GTK_OBJECT (background));
if (old_background != NULL)
gtk_object_unref (GTK_OBJECT (old_background));
}
......@@ -27,6 +27,7 @@
#define NAUTILUS_BACKGROUND_CANVAS_GROUP_H
#include "nautilus-background.h"
#include <libgnomeui/gnome-canvas.h>
/* A NautilusBackgroundCanvasGroup is used internally by NautilusBackground to change
the color of a canvas. The reason we have to change the class of a canvas group is
......@@ -55,8 +56,5 @@ typedef GnomeCanvasGroupClass NautilusBackgroundCanvasGroupClass;
(GTK_CHECK_CLASS_TYPE ((klass), NAUTILUS_TYPE_BACKGROUND_CANVAS_GROUP))
GtkType nautilus_background_canvas_group_get_type (void);
NautilusBackground *nautilus_background_canvas_group_get_background (NautilusBackgroundCanvasGroup *root);
void nautilus_background_canvas_group_set_background (NautilusBackgroundCanvasGroup *root,
NautilusBackground *background);
#endif /* NAUTILUS_BACKGROUND_CANVAS_GROUP_H */
......@@ -33,11 +33,25 @@
#include "nautilus-background-canvas-group.h"
#include "nautilus-lib-self-check-functions.h"
#include "nautilus-gtk-macros.h"
static void nautilus_background_initialize_class (gpointer klass);
static void nautilus_background_initialize (gpointer object, gpointer klass);
static void nautilus_background_destroy (GtkObject *object);
static void nautilus_background_finalize (GtkObject *object);
#include "nautilus-string.h"
static void nautilus_background_initialize_class (gpointer klass);
static void nautilus_background_initialize (gpointer object,
gpointer klass);
static void nautilus_background_destroy (GtkObject *object);
static void nautilus_background_finalize (GtkObject *object);
static void nautilus_background_draw_flat_box (GtkStyle *style,
GdkWindow *window,
GtkStateType state_type,
GtkShadowType shadow_type,
GdkRectangle *area,
GtkWidget *widget,
gchar *detail,
gint x,
gint y,
gint width,
gint height);
NAUTILUS_DEFINE_GET_TYPE_FUNCTION (NautilusBackground, nautilus_background, GTK_TYPE_OBJECT)
......@@ -155,11 +169,124 @@ nautilus_background_set_color (NautilusBackground *background,
gtk_signal_emit (GTK_OBJECT (background), signals[CHANGED]);
}
void
nautilus_background_attach_to_canvas (NautilusBackground *background,
GnomeCanvas *canvas)
static GtkStyleClass *
nautilus_gtk_style_get_default_class (void)
{
static GtkStyleClass *default_class;
if (default_class == NULL) {
GtkStyle *style;
style = gtk_style_new ();
default_class = style->klass;
gtk_style_unref (style);
}
return default_class;
}
static void nautilus_gdk_window_update_sizes (GdkWindow *window, int *width, int *height)
{
g_return_if_fail (window != NULL);
g_return_if_fail (width != NULL);
g_return_if_fail (height != NULL);
if (*width == -1 && *height == -1)
gdk_window_get_size (window, width, height);
else if (*width == -1)
gdk_window_get_size (window, width, NULL);
else if (*height == -1)
gdk_window_get_size (window, NULL, height);
}
static void nautilus_background_draw_flat_box (GtkStyle *style,
GdkWindow *window,
GtkStateType state_type,
GtkShadowType shadow_type,
GdkRectangle *area,
GtkWidget *widget,
gchar *detail,
gint x,
gint y,
gint width,
gint height)
{
gboolean call_parent;
NautilusBackground *background;
call_parent = TRUE;
background = nautilus_get_widget_background (widget);
if (background != NULL) {
if (nautilus_gradient_is_gradient (background->details->color))
call_parent = FALSE;
}
if (!call_parent)
g_warning ("gradient fills not yet hooked up in nautilus_background");
(* nautilus_gtk_style_get_default_class()->draw_flat_box)
(style, window, state_type, shadow_type, area, widget,
detail, x, y, width, height);
}
static GtkStyleClass *
nautilus_background_get_gtk_style_class (void)
{
static GtkStyleClass *klass;
if (klass == NULL) {
static GtkStyleClass klass_storage;
klass = &klass_storage;
*klass = *nautilus_gtk_style_get_default_class ();
klass->draw_flat_box = nautilus_background_draw_flat_box;
}
return klass;
}
static void
nautilus_background_set_widget_style (NautilusBackground *background,
GtkWidget *widget)
{
/* Since there's no signal to override in GnomeCanvas to control
GtkStyle *style;
char *start_color_spec;
g_return_if_fail (NAUTILUS_IS_BACKGROUND (background));
g_return_if_fail (GTK_IS_WIDGET (widget));
style = gtk_widget_get_style (widget);
g_return_if_fail(style->klass == nautilus_gtk_style_get_default_class ()
|| style->klass == nautilus_background_get_gtk_style_class ());
/* Make a copy of the style. */
style = gtk_style_copy (style);
/* Give it the special class that allows it to draw gradients. */
style->klass = nautilus_background_get_gtk_style_class ();
/* Set up the colors in the style. */
start_color_spec = nautilus_gradient_get_start_color_spec (background->details->color);
nautilus_gdk_color_parse_with_white_default
(start_color_spec, &style->bg[GTK_STATE_NORMAL]);
g_free (start_color_spec);
/* Put the style in the widget. */
gtk_widget_set_style (widget, style);
gtk_style_unref (style);
}
static void
nautilus_background_set_up_canvas (GtkWidget *widget)
{
g_return_if_fail (GTK_IS_WIDGET (widget));
/* Attach ourselves to a canvas in a way that will work.
Changing the style is not sufficient.
Since there's no signal to override in GnomeCanvas to control
drawing the background, we change the class of the canvas root.
This gives us a chance to draw the background before any of the
objects draw themselves, and has no effect on the bounds or
......@@ -169,16 +296,57 @@ nautilus_background_attach_to_canvas (NautilusBackground *background,
canvas item as the background. The canvas item contributed to
the bounds of the canvas and had to constantly be resized.
*/
if (GNOME_IS_CANVAS (widget)) {
g_assert (GTK_OBJECT (GNOME_CANVAS (widget)->root)->klass
== gtk_type_class (GNOME_TYPE_CANVAS_GROUP)
|| GTK_OBJECT (GNOME_CANVAS (widget)->root)->klass
== gtk_type_class (NAUTILUS_TYPE_BACKGROUND_CANVAS_GROUP));
GTK_OBJECT (GNOME_CANVAS (widget)->root)->klass =
gtk_type_class (NAUTILUS_TYPE_BACKGROUND_CANVAS_GROUP);
}
}
g_return_if_fail (NAUTILUS_IS_BACKGROUND (background));
g_return_if_fail (GNOME_IS_CANVAS (canvas));
static void
nautilus_widget_background_changed (GtkWidget *widget, NautilusBackground *background)
{
nautilus_background_set_widget_style (background, widget);
nautilus_background_set_up_canvas (widget);
g_assert (GTK_OBJECT (canvas->root)->klass == gtk_type_class (GNOME_TYPE_CANVAS_GROUP)
|| GTK_OBJECT (canvas->root)->klass == gtk_type_class (NAUTILUS_TYPE_BACKGROUND_CANVAS_GROUP));
gtk_widget_queue_clear (widget);
}
GTK_OBJECT (canvas->root)->klass = gtk_type_class (NAUTILUS_TYPE_BACKGROUND_CANVAS_GROUP);
NautilusBackground *
nautilus_get_widget_background (GtkWidget *widget)
{
gpointer data;
NautilusBackground *background;
GtkStyle *old_style;
GtkStyle *new_style;
g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
nautilus_background_canvas_group_set_background (NAUTILUS_BACKGROUND_CANVAS_GROUP (canvas->root), background);
/* Check for an existing background. */
data = gtk_object_get_data (GTK_OBJECT (widget), "nautilus_background");
if (data != NULL) {
g_assert (NAUTILUS_IS_BACKGROUND (data));
return data;
}
/* Store the background in the widget's data. */
background = nautilus_background_new ();
gtk_object_set_data_full (GTK_OBJECT (widget), "nautilus_background",
background, (GtkDestroyNotify) gtk_object_unref);
gtk_object_ref (GTK_OBJECT (background));
gtk_object_sink (GTK_OBJECT (background));
/* Arrange to get the signal whenever the background changes. */
gtk_signal_connect_object_while_alive (GTK_OBJECT (background), "changed",
nautilus_widget_background_changed,
GTK_OBJECT (widget));
nautilus_widget_background_changed (widget, background);
return background;
}
/* self check code */
......@@ -197,6 +365,8 @@ nautilus_self_check_background (void)
nautilus_background_set_color (background, "red");
nautilus_background_set_color (background, "red-blue");
nautilus_background_set_color (background, "red-blue:h");
gtk_object_unref (GTK_OBJECT (background));
}
#endif /* !NAUTILUS_OMIT_SELF_CHECK */
......@@ -33,10 +33,15 @@
The color or gradient is always present, even if there's a tiled image
on top of it. This is used when the tiled image can't be loaded for
some reason (or just has not been loaded yet).
The NautilusBackground object is easier to modify than a GtkStyle.
You can just call nautilus_get_window_background and modify the
returned background directly, unlike a style, which must be copied,
modified and then set.
*/
#include <gdk/gdktypes.h>
#include <libgnomeui/gnome-canvas.h>
#include <gtk/gtkwidget.h>
typedef struct _NautilusBackground NautilusBackground;
typedef struct _NautilusBackgroundClass NautilusBackgroundClass;
......@@ -69,9 +74,17 @@ void nautilus_background_draw (NautilusBackground *
GdkColormap *colormap,
const GdkRectangle *rectangle);
void nautilus_background_attach_to_canvas (NautilusBackground *background,
GnomeCanvas *canvas);
/* Gets the background attached to a widget.
If the widget doesn't already have a NautilusBackground object,
this will create one. To change the widget's background, you can
just call nautilus_background methods on the widget.
Later, we might want a call to find out if we already have a background,
or a way to share the same background among multiple widgets; both would
be straightforward.
*/
NautilusBackground *nautilus_get_widget_background (GtkWidget *widget);
typedef struct _NautilusBackgroundDetails NautilusBackgroundDetails;
......
......@@ -391,7 +391,7 @@ void
nautilus_gdk_color_parse_with_white_default (const char *color_spec,
GdkColor *color)
{
if (!gdk_color_parse (color_spec, color)) {
if (color_spec == NULL || !gdk_color_parse (color_spec, color)) {
color->red = 0xFFFF;
color->green = 0xFFFF;
color->blue = 0xFFFF;
......
......@@ -84,8 +84,7 @@ nautilus_background_canvas_group_draw (GnomeCanvasItem *item, GdkDrawable *drawa
NautilusBackground *background;
/* Draw the background. */
background = nautilus_background_canvas_group_get_background
(NAUTILUS_BACKGROUND_CANVAS_GROUP (item));
background = nautilus_get_widget_background(GTK_WIDGET (item->canvas));
if (background != NULL) {
GdkGC *gc;
GdkColormap *colormap;
......@@ -124,31 +123,3 @@ nautilus_background_canvas_group_draw (GnomeCanvasItem *item, GdkDrawable *drawa
drawable_corner_x, drawable_corner_y,
drawable_width, drawable_height));
}
NautilusBackground *
nautilus_background_canvas_group_get_background (NautilusBackgroundCanvasGroup *canvas_group)
{
gpointer data;
data = gtk_object_get_data (GTK_OBJECT (canvas_group), "nautilus_background");
g_assert (data == NULL || NAUTILUS_IS_BACKGROUND (data));
return data;
}
void
nautilus_background_canvas_group_set_background (NautilusBackgroundCanvasGroup *canvas_group,
NautilusBackground *background)
{
NautilusBackground *old_background;
g_return_if_fail (NAUTILUS_IS_BACKGROUND_CANVAS_GROUP (canvas_group));
g_return_if_fail (background == NULL || NAUTILUS_IS_BACKGROUND (background));
old_background = nautilus_background_canvas_group_get_background (canvas_group);
gtk_object_set_data (GTK_OBJECT (canvas_group), "nautilus_background", background);
if (background != NULL)
gtk_object_ref (GTK_OBJECT (background));
if (old_background != NULL)
gtk_object_unref (GTK_OBJECT (old_background));
}
......@@ -27,6 +27,7 @@
#define NAUTILUS_BACKGROUND_CANVAS_GROUP_H
#include "nautilus-background.h"
#include <libgnomeui/gnome-canvas.h>
/* A NautilusBackgroundCanvasGroup is used internally by NautilusBackground to change
the color of a canvas. The reason we have to change the class of a canvas group is
......@@ -55,8 +56,5 @@ typedef GnomeCanvasGroupClass NautilusBackgroundCanvasGroupClass;
(GTK_CHECK_CLASS_TYPE ((klass), NAUTILUS_TYPE_BACKGROUND_CANVAS_GROUP))
GtkType nautilus_background_canvas_group_get_type (void);
NautilusBackground *nautilus_background_canvas_group_get_background (NautilusBackgroundCanvasGroup *root);
void nautilus_background_canvas_group_set_background (NautilusBackgroundCanvasGroup *root,
NautilusBackground *background);
#endif /* NAUTILUS_BACKGROUND_CANVAS_GROUP_H */
......@@ -33,11 +33,25 @@
#include "nautilus-background-canvas-group.h"
#include "nautilus-lib-self-check-functions.h"
#include "nautilus-gtk-macros.h"
static void nautilus_background_initialize_class (gpointer klass);
static void nautilus_background_initialize (gpointer object, gpointer klass);
static void nautilus_background_destroy (GtkObject *object);
static void nautilus_background_finalize (GtkObject *object);
#include "nautilus-string.h"
static void nautilus_background_initialize_class (gpointer klass);
static void nautilus_background_initialize (gpointer object,
gpointer klass);
static void nautilus_background_destroy (GtkObject *object);
static void nautilus_background_finalize (GtkObject *object);
static void nautilus_background_draw_flat_box (GtkStyle *style,
GdkWindow *window,
GtkStateType state_type,
GtkShadowType shadow_type,
GdkRectangle *area,
GtkWidget *widget,
gchar *detail,
gint x,
gint y,
gint width,
gint height);
NAUTILUS_DEFINE_GET_TYPE_FUNCTION (NautilusBackground, nautilus_background, GTK_TYPE_OBJECT)
......@@ -155,11 +169,124 @@ nautilus_background_set_color (NautilusBackground *background,
gtk_signal_emit (GTK_OBJECT (background), signals[CHANGED]);
}
void
nautilus_background_attach_to_canvas (NautilusBackground *background,
GnomeCanvas *canvas)
static GtkStyleClass *
nautilus_gtk_style_get_default_class (void)
{
static GtkStyleClass *default_class;
if (default_class == NULL) {
GtkStyle *style;
style = gtk_style_new ();
default_class = style->klass;
gtk_style_unref (style);
}
return default_class;
}
static void nautilus_gdk_window_update_sizes (GdkWindow *window, int *width, int *height)
{
g_return_if_fail (window != NULL);
g_return_if_fail (width != NULL);
g_return_if_fail (height != NULL);
if (*width == -1 && *height == -1)
gdk_window_get_size (window, width, height);
else if (*width == -1)
gdk_window_get_size (window, width, NULL);
else if (*height == -1)
gdk_window_get_size (window, NULL, height);
}
static void nautilus_background_draw_flat_box (GtkStyle *style,
GdkWindow *window,
GtkStateType state_type,
GtkShadowType shadow_type,
GdkRectangle *area,
GtkWidget *widget,
gchar *detail,
gint x,
gint y,
gint width,
gint height)
{
gboolean call_parent;
NautilusBackground *background;
call_parent = TRUE;
background = nautilus_get_widget_background (widget);
if (background != NULL) {
if (nautilus_gradient_is_gradient (background->details->color))
call_parent = FALSE;
}
if (!call_parent)
g_warning ("gradient fills not yet hooked up in nautilus_background");
(* nautilus_gtk_style_get_default_class()->draw_flat_box)
(style, window, state_type, shadow_type, area, widget,
detail, x, y, width, height);
}
static GtkStyleClass *
nautilus_background_get_gtk_style_class (void)
{
static GtkStyleClass *klass;
if (klass == NULL) {
static GtkStyleClass klass_storage;
klass = &klass_storage;
*klass = *nautilus_gtk_style_get_default_class ();
klass->draw_flat_box = nautilus_background_draw_flat_box;
}
return klass;
}
static void
nautilus_background_set_widget_style (NautilusBackground *background,
GtkWidget *widget)
{
/* Since there's no signal to override in GnomeCanvas to control
GtkStyle *style;
char *start_color_spec;
g_return_if_fail (NAUTILUS_IS_BACKGROUND (background));
g_return_if_fail (GTK_IS_WIDGET (widget));
style = gtk_widget_get_style (widget);
g_return_if_fail(style->klass == nautilus_gtk_style_get_default_class ()
|| style->klass == nautilus_background_get_gtk_style_class ());
/* Make a copy of the style. */
style = gtk_style_copy (style);
/* Give it the special class that allows it to draw gradients. */
style->klass = nautilus_background_get_gtk_style_class ();
/* Set up the colors in the style. */
start_color_spec = nautilus_gradient_get_start_color_spec (background->details->color);
nautilus_gdk_color_parse_with_white_default
(start_color_spec, &style->bg[GTK_STATE_NORMAL]);
g_free (start_color_spec);