summaryrefslogtreecommitdiff
path: root/src/nautilus-about.c
blob: 0bab5344b327c12106d918c7c77f7e7a017ef5e6 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* 
 * Nautilus
 *
 * Copyright (C) 2000 Eazel, Inc.
 *
 * Nautilus 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.
 *
 * Nautilus 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: Andy Hertzfeld <andy@eazel.com>
 *
 * This is the implementation for the nautilus about dialog
 *
 */

#include <config.h>
#include "nautilus-about.h"

#include <libgnome/gnome-defs.h>

#include <math.h>
#include <gnome.h>
#include <gdk/gdk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkmenu.h>
#include <gtk/gtkmenuitem.h>
#include <libgnome/gnome-util.h>
#include <libgnomeui/gnome-pixmap.h>
#include <libnautilus-extensions/nautilus-gdk-extensions.h>
#include <libnautilus-extensions/nautilus-gdk-pixbuf-extensions.h>
#include <libnautilus-extensions/nautilus-gtk-macros.h>
#include <libnautilus-extensions/nautilus-gtk-extensions.h>
#include <libnautilus-extensions/nautilus-glib-extensions.h>
#include <libnautilus-extensions/nautilus-global-preferences.h>
#include <libnautilus-extensions/nautilus-icon-factory.h>
#include <libnautilus-extensions/nautilus-file-utilities.h>
#include <libnautilus-extensions/nautilus-scalable-font.h>
#include <libnautilus-extensions/nautilus-theme.h>

#define MAX_AUTHOR_COUNT 128

struct NautilusAboutDetails {
	GtkWidget *drawing_area;
	GdkPixbuf *background_pixbuf;	
	int	  last_update_time;
	int	  timer_task;
	char	  **authors;
	int	  order_array[MAX_AUTHOR_COUNT];
};

static gboolean nautilus_about_close 		(NautilusAbout *about, gpointer  *unused);

static void     nautilus_about_initialize_class	(NautilusAboutClass *klass);
static void     nautilus_about_initialize		(NautilusAbout *about);
static void	nautilus_about_destroy		(GtkObject *object);
static void 	nautilus_about_repaint    	(GtkWidget *drawing_area, 
				    		 GdkEventExpose *event,
				    		 NautilusAbout *about);
static void	nautilus_about_draw_info 	(NautilusAbout  *about,
						 const char	*title,
						 const char	*version,
						 const char	*copyright,
						 const char	**authors,
						 const char	*comments,
						 const char	*timestamp);
static int	update_authors_if_necessary 	(NautilusAbout *about);

/* author box layout definitions */
#define	AUTHOR_TOP_POS 88
#define	AUTHOR_LEFT_POS 200
#define	AUTHOR_COLUMN_WIDTH 140
#define AUTHOR_LINE_HEIGHT 15
#define ITEMS_PER_COLUMN 11

/* delay between randomizing, in seconds  */
#define UPDATE_TIME_INTERVAL 8
#define SHUFFLE_COUNT 200

/* gtk class definition boilerplate */
NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusAbout, nautilus_about, GNOME_TYPE_DIALOG)

static void
nautilus_about_initialize_class (NautilusAboutClass *about_class)
{
	GtkObjectClass *object_class = GTK_OBJECT_CLASS (about_class);
		
	object_class->destroy = nautilus_about_destroy;
}

static void 
nautilus_about_destroy (GtkObject *object)
{
	NautilusAbout *about;
	
	about = NAUTILUS_ABOUT (object);
	
	if (about->details->background_pixbuf) {
		gdk_pixbuf_unref (about->details->background_pixbuf);
	}
	
	if (about->details->authors) {
		g_strfreev (about->details->authors);
	}

	if (about->details->timer_task != -1)
		gtk_timeout_remove(about->details->timer_task);
	
	g_free (NAUTILUS_ABOUT (object)->details);
	
	NAUTILUS_CALL_PARENT_CLASS (GTK_OBJECT_CLASS, destroy, (object));
}


/* initialize the about */
static void
nautilus_about_initialize (NautilusAbout *about)
{
	char *background_path;
	GtkWidget *frame;
	int area_width, area_height;
	
	about->details = g_new0 (NautilusAboutDetails, 1);

	background_path = nautilus_theme_get_image_path ("about_background.png");
	about->details->background_pixbuf = gdk_pixbuf_new_from_file (background_path);
	g_free (background_path);	

	/* set the window title */
	gtk_window_set_title (GTK_WINDOW (about), _("About Nautilus"));
	
	/* allocate a frame to hold the drawing area */
	frame = gtk_frame_new (NULL);
	gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);

	gtk_container_add (GTK_CONTAINER (GNOME_DIALOG(about)->vbox),
			   GTK_WIDGET(frame));
	gtk_widget_show (frame);
	
	/* allocate the drawing area */
	about->details->drawing_area = gtk_drawing_area_new ();
	
	area_width  = gdk_pixbuf_get_width  (about->details->background_pixbuf);
	area_height = gdk_pixbuf_get_height (about->details->background_pixbuf);
	
	gtk_widget_set_usize ( GTK_WIDGET (about->details->drawing_area), area_width, area_height);
	gtk_widget_set_events (about->details->drawing_area, GDK_EXPOSURE_MASK);

	gtk_signal_connect (GTK_OBJECT (about->details->drawing_area), "expose_event",
			    (GtkSignalFunc) nautilus_about_repaint, (gpointer) about);

	gtk_widget_show (about->details->drawing_area);
 	gtk_container_add (GTK_CONTAINER (frame), about->details->drawing_area);

	/* set up the timer task */
	about->details->timer_task = gtk_timeout_add (2000, (GtkFunction) update_authors_if_necessary, about); 
	       
	/* configure the dialog */                                  
	gnome_dialog_append_button ( GNOME_DIALOG(about),
				     GNOME_STOCK_BUTTON_OK);
	
	gnome_dialog_set_close( GNOME_DIALOG(about), TRUE);			
	gnome_dialog_close_hides ( GNOME_DIALOG(about), TRUE);

	gtk_signal_connect
	        (GTK_OBJECT (about),
	         "close",
	         GTK_SIGNAL_FUNC (nautilus_about_close), 
	         NULL);

}

/* allocate a new about dialog */
GtkWidget*
nautilus_about_new (const char	*title,
		    const char	*version,
		    const char	*copyright,
		    const char	**authors,
		    const char	*comments,
		    const char *timestamp)
{
	NautilusAbout *about;
	
	about = gtk_type_new (nautilus_about_get_type ());
	
	/* draw the info onto the pixbuf, once and for all */
	nautilus_about_draw_info (about, title, version, copyright, authors, comments, timestamp);
	
	return GTK_WIDGET (about);
}


/* utility to simplify drawing */
static void
draw_pixbuf (GdkPixbuf *pixbuf, GdkDrawable *drawable, int x, int y)
{
	gdk_pixbuf_render_to_drawable_alpha (pixbuf, drawable, 0, 0, x, y,
					     gdk_pixbuf_get_width (pixbuf),
					     gdk_pixbuf_get_height (pixbuf),
					     GDK_PIXBUF_ALPHA_BILEVEL, 128, GDK_RGB_DITHER_MAX,
					     0, 0);
}

/* here's the routine that does all the real work, repainting the drawing area */
static void
nautilus_about_repaint (GtkWidget *widget, GdkEventExpose *event, NautilusAbout *about)
{
	/* draw the background image */
	if (about->details->background_pixbuf) {
		draw_pixbuf (about->details->background_pixbuf,
						     widget->window, 0, 0);
	}
}

/* utility routine to draw a string at a position */
static void
draw_aa_string (NautilusScalableFont *font, GdkPixbuf *pixbuf, int font_size, int x_pos, int y_pos, uint color, uint shadow_color, const char* text, int shadow_offset)
{
	if (shadow_offset) {
		nautilus_scalable_font_draw_text (font, pixbuf,
						  x_pos + shadow_offset, y_pos + shadow_offset,
						  NULL,
						  font_size, font_size,
						  text, strlen (text),
						  shadow_color, 255, FALSE);	
	}
	
	nautilus_scalable_font_draw_text (font, pixbuf, x_pos, y_pos, NULL, font_size, font_size, text, strlen (text), color, 255, FALSE);	
}

/* randomize_authors randomizes the order array so different names get displayed in different positions each time */

static void
randomize_authors (NautilusAbout *about)
{
	int author_count;
	int index, temp;
	int first_element, second_element;
	/* count the authors */	
	author_count = 0;
	while (about->details->authors[author_count] != NULL)
		author_count += 1;

	/* initialize the order array */
	for (index = 0; index < author_count; index++) {
		about->details->order_array[index] = index;
	}
	
	/* randomize the order array */
	for (index = 0; index < SHUFFLE_COUNT; index++) {
		first_element = rand() % author_count; 
		second_element = rand() % author_count; 
		temp = about->details->order_array[first_element];
		about->details->order_array[first_element] = about->details->order_array[second_element];
		about->details->order_array[second_element] = temp;
	}
}

/* draw the author list */
static void
draw_author_list (NautilusAbout *about, GdkPixbuf *pixbuf, NautilusScalableFont *plain_font)
{
	int index, column_count;
	int xpos, ypos;
	
	/* draw the authors */
	index = 0;
	column_count = 0;
	
	xpos = AUTHOR_LEFT_POS; ypos = AUTHOR_TOP_POS;
	while (about->details->authors[about->details->order_array[index]] != NULL) {
		draw_aa_string (plain_font, pixbuf, 12, xpos, ypos, NAUTILUS_RGB_COLOR_BLACK, NAUTILUS_RGB_COLOR_BLACK, about->details->authors[about->details->order_array[index]], 0);
		ypos += AUTHOR_LINE_HEIGHT;
		index++;
		column_count++;
		if (column_count >= ITEMS_PER_COLUMN) {
			/* two columns only and then stop drawing */
			if (xpos != AUTHOR_LEFT_POS)
				break;
			
			column_count = 0;
			ypos = AUTHOR_TOP_POS;
			xpos += AUTHOR_COLUMN_WIDTH;
		}
	}	
}

/* draw the information onto the pixbuf */
static void
nautilus_about_draw_info (
	NautilusAbout	*about,
	const char	*title,
	const char	*version,
	const char	*copyright,
	const char	**authors,
	const char	*comments,
	const char	*timestamp)
{
	char *display_str, *temp_str  ;
	char **comment_array;
	NautilusScalableFont *plain_font, *bold_font;
	GdkPixbuf *pixbuf;
	uint	black, white, grey;
	int xpos, ypos, total_height;
	int index;
	
	plain_font = NAUTILUS_SCALABLE_FONT (nautilus_scalable_font_new ("helvetica", "medium", NULL, NULL));
	bold_font  = NAUTILUS_SCALABLE_FONT (nautilus_scalable_font_new ("helvetica", "bold", NULL, NULL));

	pixbuf = about->details->background_pixbuf;
	total_height = gdk_pixbuf_get_height (pixbuf);
	
	black = NAUTILUS_RGBA_COLOR_PACK (0, 0, 0, 255);
	white = NAUTILUS_RGBA_COLOR_PACK (255, 255, 255, 255);
	grey = NAUTILUS_RGBA_COLOR_PACK (153, 153, 153, 255);
	
	/* draw the name and version */
	display_str = g_strdup_printf ("%s %s", title, version);
	draw_aa_string (bold_font, pixbuf, 24, 12, 10, white, black, display_str, 1);
	g_free (display_str);
	
	/* draw the copyright notice */
	draw_aa_string (bold_font, pixbuf, 11, 12, 40, black, black, copyright, 0);

	/* draw the authors title */
	draw_aa_string (bold_font, pixbuf, 20, 184, 64, black, black, "Authors", 0);
	
	/* draw the timestamp */
	draw_aa_string (plain_font, pixbuf, 11, 284, total_height - 14, grey, black, timestamp, 0);
	
	/* remember the authors */
	if (about->details->authors != NULL) {
		g_strfreev (about->details->authors);
	}

	temp_str = g_strjoinv ("\n", (char**) authors);
	about->details->authors = g_strsplit (temp_str, "\n", MAX_AUTHOR_COUNT);
	g_free (temp_str);
	
	randomize_authors (about);
	draw_author_list (about, pixbuf, plain_font);
	about->details->last_update_time = time (NULL);
	
	/* draw the comment, breaking it up into multiple lines */
	comment_array = g_strsplit (comments, "\n", 10);
	index = 0;
	xpos = 6; ypos = 118;
	while (comment_array[index] != NULL) {
		draw_aa_string (plain_font, pixbuf, 14, xpos, ypos, black, black, comment_array[index], 0);
		ypos += 18;
		index++;	
	}
	g_strfreev (comment_array);
	
	/* release the fonts */	
	gtk_object_unref (GTK_OBJECT(plain_font));
	gtk_object_unref (GTK_OBJECT(bold_font));
}

/* update authors is called to randomize the author array and redraw it */ 

void
nautilus_about_update_authors (NautilusAbout *about)
{
	GdkRectangle author_area;
	NautilusScalableFont *plain_font;
	
	/* clear the author area */
	author_area.x = AUTHOR_LEFT_POS - 24;
	author_area.y = AUTHOR_TOP_POS;
	author_area.width = 2 * AUTHOR_COLUMN_WIDTH;
	author_area.height = AUTHOR_LINE_HEIGHT * ITEMS_PER_COLUMN;
	
	nautilus_gdk_pixbuf_fill_rectangle_with_color (about->details->background_pixbuf, &author_area, NAUTILUS_RGBA_COLOR_PACK (255, 255, 255, 255));

	/* randomize the author array */
	randomize_authors (about);
	
	/* redraw the authors */
	plain_font = NAUTILUS_SCALABLE_FONT (nautilus_scalable_font_new ("helvetica", "medium", NULL, NULL));
	draw_author_list (about, about->details->background_pixbuf, plain_font);
	gtk_object_unref (GTK_OBJECT(plain_font));
	
	about->details->last_update_time = time (NULL);

	/* set up the timer task if necessary */
	if (about->details->timer_task == -1) {
		about->details->timer_task = gtk_timeout_add (2000, (GtkFunction) update_authors_if_necessary, about); 
  	}  
	
	/* schedule a redraw for the about box */
	gtk_widget_queue_draw (GTK_WIDGET (about));
}

/* handle the dialog closing by killing the timer task */

static gboolean
nautilus_about_close (NautilusAbout *about, gpointer  *unused)
{
  if (about->details->timer_task != -1) {
	gtk_timeout_remove(about->details->timer_task);
  	about->details->timer_task = -1;
  }  
  return FALSE;
}

static int
update_authors_if_necessary (NautilusAbout *about)
{
	uint current_time, next_time;
	
	current_time = time(NULL);
	next_time = about->details->last_update_time + UPDATE_TIME_INTERVAL;

	if (current_time > next_time) {
		nautilus_about_update_authors (about);
	}
	return TRUE;
	
}