glade-cursor.c 5.12 KB
Newer Older
Jose Maria Celorio's avatar
Jose Maria Celorio committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2001 Ximian, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Jose Maria Celorio's avatar
Jose Maria Celorio committed
18
19
20
21
22
 *
 * Authors:
 *   Chema Celorio <chema@celorio.com>
 */

Christian Persch's avatar
Christian Persch committed
23
#include "config.h"
Tristan Van Berkom's avatar
Tristan Van Berkom committed
24

25
26
#include "glade-app.h"
#include "glade-palette.h"
Jose Maria Celorio's avatar
Jose Maria Celorio committed
27
#include "glade-cursor.h"
28
#include "glade-widget-adaptor.h"
Jose Maria Celorio's avatar
Jose Maria Celorio committed
29

Tristan Van Berkom's avatar
Tristan Van Berkom committed
30
31
32
33
34
#include <glib.h>
#include <glib/gi18n.h>

#define ADD_PIXBUF_FILENAME "plus.png"

35
36
static GladeCursor *cursor = NULL;

37
38
39
40
41
42
static void
set_cursor_recurse (GtkWidget *widget, 
		    GdkCursor *gdk_cursor)
{
	GList *children, *list;

43
	if (!gtk_widget_get_visible (widget) ||
44
45
46
	    !GTK_WIDGET_REALIZED (widget))
		return;

47
	gdk_window_set_cursor (gtk_widget_get_window (widget), gdk_cursor);
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

	if (GTK_IS_CONTAINER (widget) &&
	    (children = 
	     glade_util_container_get_all_children (GTK_CONTAINER (widget))) != NULL)
	{
		for (list = children; list; list = list->next)
		{
			set_cursor_recurse (GTK_WIDGET (list->data), gdk_cursor);
		}
		g_list_free (children);
	}
}


static void
set_cursor (GdkCursor *gdk_cursor)
{
	GladeProject *project;
	GList        *list, *projects;

	for (projects = glade_app_get_projects ();
	     projects; projects = projects->next)
	{
		project = projects->data;

73
		for (list = (GList *) glade_project_get_objects (project); 
74
75
76
77
78
79
80
81
82
83
84
85
		     list; list = list->next)
		{
			GObject *object = list->data;

			if (GTK_IS_WINDOW (object))
			{
				set_cursor_recurse (GTK_WIDGET (object), gdk_cursor);
			}
		}
	}
}

86
87
88
89
90
91
/**
 * glade_cursor_set:
 * @window: a #GdkWindow
 * @type: a #GladeCursorType
 *
 * Sets the cursor for @window to something appropriate based on @type.
92
 * (also sets the cursor on all visible project widgets)
93
 */
Jose Maria Celorio's avatar
Jose Maria Celorio committed
94
95
96
void
glade_cursor_set (GdkWindow *window, GladeCursorType type)
{
97
98
	GladeWidgetAdaptor *adaptor;
	GdkCursor          *the_cursor = NULL;
Tristan Van Berkom's avatar
Tristan Van Berkom committed
99
100
	g_return_if_fail (cursor != NULL);

Jose Maria Celorio's avatar
Jose Maria Celorio committed
101
102
	switch (type) {
	case GLADE_CURSOR_SELECTOR:
103
		the_cursor = cursor->selector;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
104
105
		break;
	case GLADE_CURSOR_ADD_WIDGET:
106
107
		if ((adaptor = 
		     glade_palette_get_current_item (glade_app_get_palette ())) != NULL)
108
		{
109
110
111
			g_object_get (adaptor, "cursor", &the_cursor, NULL);
			
			if (the_cursor == NULL)
112
				the_cursor = cursor->add_widget;
113
			
114
115
		}
		else
116
			the_cursor = cursor->add_widget;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
117
		break;
118
	case GLADE_CURSOR_RESIZE_TOP_LEFT:
119
		the_cursor = cursor->resize_top_left;
120
121
		break;
	case GLADE_CURSOR_RESIZE_TOP_RIGHT:
122
		the_cursor = cursor->resize_top_right;
123
124
		break;
	case GLADE_CURSOR_RESIZE_BOTTOM_LEFT:
125
		the_cursor = cursor->resize_bottom_left;
126
127
		break;
	case GLADE_CURSOR_RESIZE_BOTTOM_RIGHT:
128
		the_cursor = cursor->resize_bottom_right;
129
130
		break;
	case GLADE_CURSOR_RESIZE_LEFT:
131
		the_cursor = cursor->resize_left;
132
133
		break;
	case GLADE_CURSOR_RESIZE_RIGHT:
134
		the_cursor = cursor->resize_right;
135
136
		break;
	case GLADE_CURSOR_RESIZE_TOP:
137
		the_cursor = cursor->resize_top;
138
139
		break;
	case GLADE_CURSOR_RESIZE_BOTTOM:
140
		the_cursor = cursor->resize_bottom;
141
142
		break;
	case GLADE_CURSOR_DRAG:
143
		the_cursor = cursor->drag;
144
		break;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
145
146
147
148
	default:
		break;
	}

149
	set_cursor (cursor->selector);
150
	gdk_window_set_cursor (window, the_cursor);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
151
152
}

153
154
155
156
157
/**
 * glade_cursor_init:
 *
 * Initializes cursors for use with glade_cursor_set().
 */
Jose Maria Celorio's avatar
Jose Maria Celorio committed
158
159
160
void
glade_cursor_init (void)
{
Tristan Van Berkom's avatar
Tristan Van Berkom committed
161
162
	gchar *path;
	GError *error = NULL;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
163

Tristan Van Berkom's avatar
Tristan Van Berkom committed
164
	cursor = g_new0 (GladeCursor, 1);
165

Tristan Van Berkom's avatar
Tristan Van Berkom committed
166
167
	cursor->selector            = gdk_cursor_new (GDK_TOP_LEFT_ARROW);
	cursor->add_widget          = gdk_cursor_new (GDK_PLUS);
168
169
170
171
172
173
174
175
176
	cursor->resize_top_left     = gdk_cursor_new (GDK_TOP_LEFT_CORNER);
	cursor->resize_top_right    = gdk_cursor_new (GDK_TOP_RIGHT_CORNER);
	cursor->resize_bottom_left  = gdk_cursor_new (GDK_BOTTOM_LEFT_CORNER);
	cursor->resize_bottom_right = gdk_cursor_new (GDK_BOTTOM_RIGHT_CORNER);
	cursor->resize_left         = gdk_cursor_new (GDK_LEFT_SIDE);
	cursor->resize_right        = gdk_cursor_new (GDK_RIGHT_SIDE);
	cursor->resize_top          = gdk_cursor_new (GDK_TOP_SIDE);
	cursor->resize_bottom       = gdk_cursor_new (GDK_BOTTOM_SIDE);
	cursor->drag                = gdk_cursor_new (GDK_FLEUR);
Tristan Van Berkom's avatar
Tristan Van Berkom committed
177
178
179
	cursor->add_widget_pixbuf   = NULL;

	/* load "add" cursor pixbuf */
180
	path = g_build_filename (glade_app_get_pixmaps_dir (), ADD_PIXBUF_FILENAME, NULL);
Tristan Van Berkom's avatar
Tristan Van Berkom committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

	cursor->add_widget_pixbuf = gdk_pixbuf_new_from_file (path, &error);

	if (cursor->add_widget_pixbuf == NULL)
	{
		g_critical (_("Unable to load image (%s)"), error->message);

		g_error_free (error);
		error = NULL;
	}
	g_free (path);
}

const GdkPixbuf*
glade_cursor_get_add_widget_pixbuf (void)
{
	g_return_val_if_fail (cursor != NULL, NULL);

	return cursor->add_widget_pixbuf;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
200
}