summaryrefslogtreecommitdiff
path: root/components/sample/nautilus-sample-content-view.c
blob: d00bb31c498253e8ee3d2f0d8fb3ad70d492e498 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* -*- 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.
 */

/* WHAT YOU NEED TO CHANGE: You need to rename everything. Then look
 * for the individual CHANGE comments to see some things you could
 * change to make your view do what you want.  
 */

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

/* CHANGE: #ifdef this back in if this component comes as part of
 * Nautilus. If not, you can't rely on the nautilus-gtk-macros.h header.
 */
#if 0
#include <eel/eel-gtk-macros.h>
#endif

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

/* CHANGE: You probably want some different widget than a label to be
 * your main view contents.  
 */
struct NautilusSampleContentViewDetails {
	char      *location;
	GtkWidget *label;
};

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


/* CHANGE: #ifdef this back in and remove the else clause if this
 * component comes as part of Nautilus. If not, you can't rely on the
 * nautilus-gtk-macros.h header, so remove the #if 0 part, and use the
 * #else portion.
 */

#if 0

EEL_DEFINE_CLASS_BOILERPLATE (NautilusSampleContentView,
				   nautilus_sample_content_view,
				   NAUTILUS_TYPE_VIEW)
#else

static gpointer parent_class;
                                                                                                        
GtkType
nautilus_sample_content_view_get_type (void)                                                         
{                                                                                                       
	GtkType parent_type;                                                                            
	static GtkType type;                                                                            
                                                                                                        
	if (type == 0) {                                                                                
		static GtkTypeInfo info = {
		        "NautilusSampleContentView",
			sizeof (NautilusSampleContentView),
			sizeof (NautilusSampleContentViewClass),
			(GtkClassInitFunc)nautilus_sample_content_view_class_init,
			(GtkObjectInitFunc)nautilus_sample_content_view_init,
			NULL,
			NULL,
			NULL
		};

		parent_type = (NAUTILUS_TYPE_VIEW);
		type = gtk_type_unique (parent_type, &info);
		parent_class = gtk_type_class (parent_type);
	}

	return type;
}

#endif


     
static void
nautilus_sample_content_view_class_init (NautilusSampleContentViewClass *klass)
{
	GtkObjectClass *object_class;
	
	g_assert (NAUTILUS_IS_SAMPLE_CONTENT_VIEW_CLASS (klass));

	object_class = GTK_OBJECT_CLASS (klass);
	
	object_class->destroy = nautilus_sample_content_view_destroy;
}

static void
nautilus_sample_content_view_init (NautilusSampleContentView *view)
{
	g_assert (NAUTILUS_IS_SAMPLE_CONTENT_VIEW (view));

	view->details = g_new0 (NautilusSampleContentViewDetails, 1);
	
	view->details->label = gtk_label_new (_("(none)"));
	gtk_widget_show (view->details->label);
	
	nautilus_view_construct (NAUTILUS_VIEW (view), 
				 view->details->label);
	
	gtk_signal_connect (GTK_OBJECT (view), 
			    "load_location",
			    sample_load_location_callback, 
			    NULL);

	/* Get notified when our bonobo control is activated so we can
	 * merge menu & toolbar items into the shell's UI.
	 */
        gtk_signal_connect (GTK_OBJECT (nautilus_view_get_bonobo_control (NAUTILUS_VIEW (view))),
                            "activate",
                            sample_merge_bonobo_items_callback,
                            view);
	
}

static void
nautilus_sample_content_view_destroy (GtkObject *object)
{
	NautilusSampleContentView *view;
	
	view = NAUTILUS_SAMPLE_CONTENT_VIEW (object);
	
	g_free (view->details->location);
	g_free (view->details);

	/* CHANGE: Pick one. */
#if 0 /* nautilus-specific */	
	EEL_CALL_PARENT (GTK_OBJECT_CLASS, destroy, (object));
#else /* non-nautilus-specific */
	(* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
#endif
}



static void
load_location (NautilusSampleContentView *view,
	       const char *location)
{
	char *label_text;

	g_assert (NAUTILUS_IS_SAMPLE_CONTENT_VIEW (view));
	g_assert (location != NULL);
	
	g_free (view->details->location);
	view->details->location = g_strdup (location);

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

/* CHANGE: Do your own loading here. If loading can be a long-running
 * operation, you should consider doing it async, in which case you
 * should only call load_complete when the load is actually done.
 */

static void
sample_load_location_callback (NautilusView *nautilus_view, 
			       const char *location,
			       gpointer user_data)
{
	NautilusSampleContentView *view;
	
	g_assert (NAUTILUS_IS_VIEW (nautilus_view));
	g_assert (location != NULL);
	
	view = NAUTILUS_SAMPLE_CONTENT_VIEW (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. */
	load_location (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 (BonoboUIComponent *ui, 
			gpointer           user_data, 
			const char        *verb)
{
 	NautilusSampleContentView *view;
	char *label_text;

	g_assert (BONOBO_IS_UI_COMPONENT (ui));
        g_assert (verb != NULL);

	view = NAUTILUS_SAMPLE_CONTENT_VIEW (user_data);

	if (strcmp (verb, "Sample Menu Item") == 0) {
		label_text = g_strdup_printf ("%s\n\nYou selected the Sample menu item.",
					      view->details->location);
	} else {
		g_assert (strcmp (verb, "Sample Dock Item") == 0);
		label_text = g_strdup_printf (_("%s\n\nYou clicked the Sample toolbar button."),
					      view->details->location);
	}
	
	gtk_label_set_text (GTK_LABEL (view->details->label), label_text);
	g_free (label_text);
}

/* CHANGE: Do your own menu/toolbar merging here. */
static void
sample_merge_bonobo_items_callback (BonoboControl *control, 
				    gboolean       state, 
				    gpointer       user_data)
{
 	NautilusSampleContentView *view;
	BonoboUIComponent *ui_component;
	BonoboUIVerb verbs [] = {
		BONOBO_UI_VERB ("Sample Menu Item", bonobo_sample_callback),
		BONOBO_UI_VERB ("Sample Dock Item", bonobo_sample_callback),
		BONOBO_UI_VERB_END
	};

	g_assert (BONOBO_IS_CONTROL (control));
	
	view = NAUTILUS_SAMPLE_CONTENT_VIEW (user_data);

	if (state) {
		ui_component = nautilus_view_set_up_ui (NAUTILUS_VIEW (view),
							DATADIR,
							"nautilus-sample-content-view-ui.xml",
							"nautilus-sample-content-view");
									
		bonobo_ui_component_add_verb_list_with_data (ui_component, verbs, view);
	}

        /* 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.
	 */
}