summaryrefslogtreecommitdiff
path: root/components/sample/nautilus-sample-content-view.c
blob: 8cbe527541785f877e93ec9b57786bb9982385e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* 
 * Copyright (C) 2000 Eazel, 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 Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Maciej Stachowiak <mjs@eazel.com>
 */

/* nautilus-sample-content-view.c - sample content view
   component. This component displays a simple label of the URI
   and demonstrates merging menu items & toolbar buttons. 
   It should be a good basis for writing out-of-proc content views.
 */

#include <config.h>
#include "nautilus-sample-content-view.h"

#include <bonobo/bonobo-control.h>
#include <gtk/gtksignal.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomeui/gnome-stock.h>
#include <libnautilus/nautilus-bonobo-ui.h>
#include <libnautilus-extensions/nautilus-gtk-macros.h>

/* A NautilusContentView's private information. */
struct NautilusSampleContentViewDetails {
	char *uri;
	NautilusView *nautilus_view;
};

static void nautilus_sample_content_view_initialize_class (NautilusSampleContentViewClass *klass);
static void nautilus_sample_content_view_initialize       (NautilusSampleContentView      *view);
static void nautilus_sample_content_view_destroy          (GtkObject                      *object);
static void sample_load_location_callback                 (NautilusView                   *nautilus_view,
							   const char                     *location,
							   NautilusSampleContentView      *view);
static void sample_merge_bonobo_items_callback            (BonoboObject                   *control,
							   gboolean                        state,
							   gpointer                        user_data);

NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusSampleContentView, nautilus_sample_content_view, GTK_TYPE_LABEL)
     
static void
nautilus_sample_content_view_initialize_class (NautilusSampleContentViewClass *klass)
{
	GtkObjectClass *object_class;
	
	object_class = GTK_OBJECT_CLASS (klass);
	
	object_class->destroy = nautilus_sample_content_view_destroy;
}

static void
nautilus_sample_content_view_initialize (NautilusSampleContentView *view)
{
	view->details = g_new0 (NautilusSampleContentViewDetails, 1);
	
	gtk_label_set_text (GTK_LABEL (view), g_strdup ("(none)"));
	
	view->details->nautilus_view = nautilus_view_new (GTK_WIDGET (view));
	
	gtk_signal_connect (GTK_OBJECT (view->details->nautilus_view), 
			    "load_location",
			    GTK_SIGNAL_FUNC (sample_load_location_callback), 
			    view);

	/* 
	 * Get notified when our bonobo control is activated so we
	 * can merge menu & toolbar items into Nautilus's UI.
	 */
        gtk_signal_connect (GTK_OBJECT (nautilus_view_get_bonobo_control
					(view->details->nautilus_view)),
                            "activate",
                            sample_merge_bonobo_items_callback,
                            view);
	
	gtk_widget_show (GTK_WIDGET (view));
}

static void
nautilus_sample_content_view_destroy (GtkObject *object)
{
	NautilusSampleContentView *view;
	
	view = NAUTILUS_SAMPLE_CONTENT_VIEW (object);
	
	g_free (view->details->uri);
	g_free (view->details);
	
	NAUTILUS_CALL_PARENT_CLASS (GTK_OBJECT_CLASS, destroy, (object));
}

/**
 * nautilus_sample_content_view_get_nautilus_view:
 *
 * Return the NautilusView object associated with this view; this
 * is needed to export the view via CORBA/Bonobo.
 * @view: NautilusSampleContentView to get the nautilus_view from..
 * 
 **/
NautilusView *
nautilus_sample_content_view_get_nautilus_view (NautilusSampleContentView *view)
{
	return view->details->nautilus_view;
}

/**
 * nautilus_sample_content_view_load_uri:
 *
 * Load the resource pointed to by the specified URI.
 * 
 **/
void
nautilus_sample_content_view_load_uri (NautilusSampleContentView *view,
				       const char               *uri)
{
	char *label_text;
	
	g_free (view->details->uri);
	view->details->uri = g_strdup (uri);

	label_text = g_strdup_printf (_("%s\n\nThis is a sample Nautilus content view component."), uri);
	gtk_label_set_text (GTK_LABEL (view), label_text);
	g_free (label_text);
}

static void
sample_load_location_callback (NautilusView *nautilus_view, 
			       const char *location,
			       NautilusSampleContentView *view)
{
	g_assert (nautilus_view == view->details->nautilus_view);
	
	/* It's mandatory to send an underway message once the
	 * component starts loading, otherwise nautilus will assume it
	 * failed. In a real component, this will probably happen in
	 * some sort of callback from whatever loading mechanism it is
	 * using to load the data; this component loads no data, so it
	 * gives the progress update here.
	 */
	nautilus_view_report_load_underway (nautilus_view);
	
	/* Do the actual load. */
	nautilus_sample_content_view_load_uri (view, location);
	
	/* It's mandatory to call report_load_complete once the
	 * component is done loading successfully, or
	 * report_load_failed if it completes unsuccessfully. In a
	 * real component, this will probably happen in some sort of
	 * callback from whatever loading mechanism it is using to
	 * load the data; this component loads no data, so it gives
	 * the progress update here.
	 */
	nautilus_view_report_load_complete (nautilus_view);
}

static void
bonobo_sample_callback (BonoboUIHandler *ui_handler, gpointer user_data, const char *path)
{
 	NautilusSampleContentView *view;
	char *label_text;

        g_assert (NAUTILUS_IS_SAMPLE_CONTENT_VIEW (user_data));

	view = NAUTILUS_SAMPLE_CONTENT_VIEW (user_data);

	if (strcmp (path, "/File/Sample") == 0) {
		label_text = g_strdup_printf ("%s\n\nYou selected the Sample menu item.", view->details->uri);
	} else {
		g_assert (strcmp (path, "/Main/Sample") == 0);
		label_text = g_strdup_printf (_("%s\n\nYou clicked the Sample toolbar button."), view->details->uri);
	}
	
	gtk_label_set_text (GTK_LABEL (view), label_text);
	g_free (label_text);
}

static void
sample_merge_bonobo_items_callback (BonoboObject *control, gboolean state, gpointer user_data)
{
 	NautilusSampleContentView *view;
	BonoboUIHandler *local_ui_handler;
	GdkPixbuf		*pixbuf;
	BonoboUIHandlerPixmapType pixmap_type;
	char *path;
	
	g_assert (NAUTILUS_IS_SAMPLE_CONTENT_VIEW (user_data));

	view = NAUTILUS_SAMPLE_CONTENT_VIEW (user_data);
	local_ui_handler = bonobo_control_get_ui_handler (BONOBO_CONTROL (control));

	if (state) {
		/* Tell the Nautilus window to merge our bonobo_ui_handler items with its ones */
		bonobo_ui_handler_set_container (local_ui_handler, 
                                                 bonobo_control_get_remote_ui_handler (BONOBO_CONTROL (control)));

		/* Load test pixbuf */
		pixbuf = gdk_pixbuf_new_from_file ("/gnome/share/pixmaps/nautilus/i-directory-24.png");		
		if (pixbuf != NULL)
			pixmap_type = BONOBO_UI_HANDLER_PIXMAP_PIXBUF_DATA;
		else
			pixmap_type = BONOBO_UI_HANDLER_PIXMAP_NONE;

	       /* Create our sample menu item. */ 
		path = bonobo_ui_handler_build_path (NULL,
						     "File", 
						     "Sample",
						     NULL);
		bonobo_ui_handler_menu_new_item 
			(local_ui_handler,					/* BonoboUIHandler */
	        	 path,							/* menu item path, must start with /some-existing-menu-path and be otherwise unique */
	                 _("_Sample"),						/* menu item user-displayed label */
	                 _("This is a sample merged menu item"),		/* hint that appears in status bar */
	                 bonobo_ui_handler_menu_get_pos 
	                 	(local_ui_handler, 
	                         NAUTILUS_MENU_PATH_NEW_WINDOW_ITEM) + 1, 	/* position within menu; -1 means last */
	                 pixmap_type,						/* pixmap type */
	                 pixbuf,						/* pixmap data */
	                 'M',							/* accelerator key, couldn't bear the thought of using Control-S for anything except Save */
	                 GDK_CONTROL_MASK,					/* accelerator key modifiers */
	                 bonobo_sample_callback,				/* callback function */
	                 view);                					/* callback function data */
		g_free (path);

                /* Create our sample toolbar button. */ 
                path = bonobo_ui_handler_build_path (NAUTILUS_TOOLBAR_PATH_MAIN_TOOLBAR, 
                				     "Sample",
                				     NULL);
	        bonobo_ui_handler_toolbar_new_item 
	        	(local_ui_handler,				/* BonoboUIHandler */
	        	 path,						/* button path, must start with /some-existing-toolbar-path and be otherwise unique */
	        	 _("Sample"),					/* button user-displayed label */
	        	 _("This is a sample merged toolbar button"),	/* hint that appears in status bar */
	        	 -1,						/* position, -1 means last */
	        	 pixmap_type,					/* pixmap type */
	        	 pixbuf,					/* pixmap data */
	        	 0,						/* accelerator key */
	        	 0,						/* accelerator key modifiers */
	        	 bonobo_sample_callback,			/* callback function */
	        	 view);						/* callback function's data */
	        g_free (path);
	} else {
		/* Do nothing. */
	}

        /* 
         * Note that we do nothing if state is FALSE. Nautilus content views are activated
         * when installed, but never explicitly deactivated. When the view changes to another,
         * the content view object is destroyed, which ends up calling bonobo_ui_handler_unset_container,
         * which removes its merged menu & toolbar items.
         */
}