File: gnome-about.c

package info (click to toggle)
gnome-libs 0.30.1-5
  • links: PTS
  • area: main
  • in suites: slink
  • size: 7,576 kB
  • ctags: 8,906
  • sloc: ansic: 96,177; sh: 5,055; makefile: 984; awk: 279; perl: 170; sed: 93
file content (640 lines) | stat: -rw-r--r-- 16,023 bytes parent folder | download
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/* GNOME GUI Library
 * Copyright (C) 1998 Cesar Miquel <miquel@df.uba.ar>
 * Based in gnome-about, copyright (C) 1998 Horacio J. Pea
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <config.h>
#include "gnome-about.h"
#include "libgnome/gnome-util.h"
#include "libgnome/gnome-i18nP.h"
#include "gnome-stock.h"
#include <strings.h>
#include <gtk/gtk.h>

/* FONTS */
#define HELVETICA_20_BFONT "-adobe-helvetica-bold-r-normal-*-20-*-*-*-*-*-*-*"
#define HELVETICA_14_BFONT "-adobe-helvetica-bold-r-normal-*-14-*-*-*-*-*-*-*"
#define HELVETICA_12_BFONT "-adobe-helvetica-bold-r-normal-*-12-*-*-*-*-*-*-*"
#define HELVETICA_12_FONT  "-adobe-helvetica-medium-r-normal-*-12-*-*-*-*-*-*-*"
#define HELVETICA_10_FONT  "-adobe-helvetica-medium-r-normal-*-10-*-*-*-*-*-*-*"

#define GNOME_ABOUT_DEFAULT_WIDTH               100
#define GNOME_ABOUT_MAX_WIDTH                   600
#define BASE_LINE_SKIP                          4

typedef struct
{
	gchar	*title;
	gchar *copyright;
	GList *names;
	gchar *comments;
	GdkPixmap *logo;
	GdkBitmap *mask;
	gint logo_w, logo_h;
	gint w, h;
	GdkColor light_green;
	GdkFont *font_title,
		*font_copyright,
		*font_author,
		*font_names,
		*font_comments;
} GnomeAboutInfo;

static void gnome_about_class_init (GnomeAboutClass *klass);
static void gnome_about_init       (GnomeAbout      *about);
static void gnome_about_repaint    (GtkWidget *w, 
				    GdkEventExpose *,
				    GnomeAboutInfo *data);

static void gnome_about_display_comments (GdkWindow *win, 
					  GdkFont *font,
					  GdkGC *gc, 
					  gint x, gint y, gint w, 
					  const gchar *comments);


/* ----------------------------------------------------------------------
   NAME:	gnome_about_get_type
   DESCRIPTION:	
   ---------------------------------------------------------------------- */

guint
gnome_about_get_type ()
{
	static guint about_type = 0;

	if (!about_type)
	{
		GtkTypeInfo about_info =
		{
			"GnomeAbout",
			sizeof (GnomeAbout),
			sizeof (GnomeAboutClass),
			(GtkClassInitFunc) gnome_about_class_init,
			(GtkObjectInitFunc) gnome_about_init,
			(GtkArgSetFunc) NULL,
			(GtkArgGetFunc) NULL,
		};

		about_type = gtk_type_unique (gnome_dialog_get_type (), &about_info);
	}

	return about_type;
}

/* ----------------------------------------------------------------------
   NAME:	gnome_about_class_init
   DESCRIPTION:	
   ---------------------------------------------------------------------- */

static void
gnome_about_class_init (GnomeAboutClass *klass)
{
}

/* ----------------------------------------------------------------------
   NAME:	gnome_about_init
   DESCRIPTION:	
   ---------------------------------------------------------------------- */

static void
gnome_about_init (GnomeAbout *about)
{
}

/* ----------------------------------------------------------------------
   NAME:	gnome_about_repaint
   DESCRIPTION:	
   ---------------------------------------------------------------------- */

void
gnome_about_repaint (GtkWidget *widget, 
		     GdkEventExpose *event,
		     GnomeAboutInfo *gai)
{
	GdkWindow *win = widget->window;
	GdkColor *black = &widget->style->black;
	GdkColor *white = &widget->style->white;
	GdkColor *light_green = &gai->light_green;
	GList *name;
	static GdkGC *gc = NULL;
	int h, y, x;

	if (gc == NULL)
		gc = gdk_gc_new (win);

	gdk_window_clear (win);
	
	/* Draw pixmap first */
	y = 1;
	if (gai->logo)
	{
		y += 2;
		x = (gai->w - gai->logo_w) / 2;
		if (gai->mask) {
			gdk_gc_set_clip_mask (gc, gai->mask);
			gdk_gc_set_clip_origin (gc, x, y);
		}
		gdk_draw_pixmap (win, gc, gai->logo, 
				 0, 0, x, y,
				 gai->logo_w, gai->logo_h);
		if (gai->mask) {
			gdk_gc_set_clip_mask (gc, NULL);
			gdk_gc_set_clip_origin (gc, 0, 0);
		}
		y += 2 + gai->logo_h;
	}
      
	/* Draw title */
	h = gai->font_title->descent + gai->font_title->ascent + 13;
	if (gai->title)
	{
		y += 2;
		gdk_gc_set_foreground (gc, black);
		gdk_gc_set_background (gc, black);
		gdk_draw_rectangle (win, gc, TRUE, 2, y, gai->w - 5, h);
		y += h - 7 - gai->font_title->descent;
		x = (gai->w - gdk_string_measure (gai->font_title, gai->title)) / 2;
		gdk_gc_set_foreground (gc, white);
		gdk_draw_string (win, gai->font_title, gc, 
				 x, y, 
				 gai->title);
		y += 7 + gai->font_title->descent + 2;
	}

	if (gai->copyright || gai->names || gai->comments)
	{
		gdk_gc_set_foreground (gc, light_green);
		gdk_gc_set_background (gc, light_green);
		gdk_draw_rectangle (win, gc, TRUE, 
				    2, y, 
				    gai->w-5, gai->h - y - 3);
	}

	gdk_gc_set_foreground (gc, black);

	if (gai->copyright)
	{
		y += 4 + gai->font_copyright->ascent;
		gdk_draw_string (win, gai->font_copyright, gc, 5, y, gai->copyright);
		y += gai->font_copyright->descent + 4;
	}

	if (gai->names)
	{
		y += 2 + gai->font_author->ascent;
		if (g_list_length (gai->names) == 1)
			gdk_draw_string (win, gai->font_author, gc, 5, y, _("Author:") );
		else
			gdk_draw_string (win, gai->font_author, gc, 5, y, _("Authors:") );
	}
	x = 5 + gdk_string_measure (gai->font_author, _("Authors:")) + 10;

	name = g_list_first (gai->names);
	while (name)
	{
		h = gai->font_names->descent + gai->font_names->ascent;
		gdk_draw_string (win, gai->font_names, gc, x, y, (char *)name->data);
		name = name->next;
		if (name != NULL)
			y += h;
	}

	if (gai->comments)
	{
		y += 2 + 2 * gai->font_comments->ascent;
		gnome_about_display_comments (win, gai->font_comments, gc, 
					      8, y, gai->w, 
					      gai->comments);
	}

	return;


}

void
gnome_about_display_comments (GdkWindow *win, 
			      GdkFont *font,
			      GdkGC *gc, 
			      gint x, gint y, gint w, 
			      const gchar *comments)
{
	char *tok, *p1, *p2, *p3, c;
	char *buffer;
	int  ypos, width, done;
	GList *par, *tmp;

	width = w - 16;
	if (comments == NULL)
		return;

	/* we need a buffer because strtok will modify the string! */
	/* strtok is a cool but dangerous function. */
	buffer = g_strdup(comments);

	ypos = y;

	/* Make a list with each paragraph */
	par = (GList *)NULL;

	for (tok = strtok (buffer, "\n"); tok; tok = strtok (NULL, "\n")) {
	        p1 = g_strdup(tok);
		par = g_list_append (par, g_strdup(tok));
	}

	/* Print each paragraph */
	tmp = par;
	while (tmp != NULL)
	{
		done = FALSE;
		p1 = p2 = tmp->data;
		while (!done)
		{
			c = *p1; *p1 = 0;
			while ( gdk_string_measure (font, p2) < width && c != 0)
			{
				*p1 = c; 
				p1++;
				c = *p1;
				*p1 = 0;
			}
			switch (c)
			{
			case ' ':
				p1++;
				gdk_draw_string (win, font, gc, x, ypos, p2);
				break;
			case '\0':
				done = TRUE;
				gdk_draw_string (win, font, gc, x, ypos, p2);
				break;
			default:
				p3 = p1;
				while (*p1 != ' ' && p1 > p2)
					p1--;
				if (p1 == p2) 
				{
					gdk_draw_string (win, font, gc, x, ypos, p2);
					*p3 = c;
					p1 = p3;
					break;
				}
				else
				{
					*p3 = c;
					*p1 = 0; 
					p1++;
					gdk_draw_string (win, font, gc, x, ypos, p2);
				}
				break;
			}
			ypos += font->descent + font->ascent;
			p2 = p1;
		}
		tmp = tmp->next;
		ypos += BASE_LINE_SKIP; /* Skip a bit */
	}

	/* Free list memory */
	g_list_foreach(par, (GFunc)g_free, NULL);
	g_list_free (par);
	g_free (buffer);

	return;
}


/* ----------------------------------------------------------------------
   NAME:	gnome_about_calc_size
   DESCRIPTION:	Calculates size of window WITHOUT counting pixmap. 
   ---------------------------------------------------------------------- */

void
gnome_about_calc_size (GnomeAboutInfo *gai)
{
	GList *name;
	gint num_pars, i, h, w, len[4], tmpl;
	const gchar *p;
	gfloat maxlen;

	w = GNOME_ABOUT_DEFAULT_WIDTH;
	h = 1;

	if (gai->title)
	{
		len[0] = gdk_string_measure (gai->font_title, gai->title);
		h += 4 + gai->font_title->descent + gai->font_title->ascent + 14;
	}
	else
		len[0] = 0;

	if (gai->copyright)
	{
		h += gai->font_copyright->descent + gai->font_copyright->ascent + 8;
		len[1] = gdk_string_measure (gai->font_copyright, gai->copyright);
	}
	else
		len[1] = 0;

	len[2] = 0;
	if (gai->names)
	{
		name = g_list_first (gai->names);
		while (name)
		{
			tmpl = gdk_string_measure (gai->font_names, name->data);
			if (tmpl > len[2])
				len[2] = tmpl;
			name = name->next;
		}
		tmpl = gdk_string_measure (gai->font_names, _("Authors: "));
		len[2] += tmpl + 15;
		h += g_list_length (gai->names) * 
			(gai->font_names->descent + 
			 gai->font_names->ascent + 
			 BASE_LINE_SKIP ) + 4;
	}

	maxlen = (gfloat) w;
	for (i=0; i<3; i++)
		if (len[i] > maxlen)
			maxlen = (gfloat) len[i];
    
	w = (int) (maxlen * 1.2);
	if ( w > GNOME_ABOUT_MAX_WIDTH )
		w = GNOME_ABOUT_MAX_WIDTH;

	if (gai->comments)
	{
		num_pars = 1;
		p = gai->comments;
		while (*p != '\0')
		{
			if (*p == '\n')
				num_pars++;
			p++;
		}
      
		i = gdk_string_measure (gai->font_comments, gai->comments);
		i /= w - 16;
		i += 1 + num_pars;;
		h += i * (gai->font_comments->descent + gai->font_comments->ascent);
	}

	gai->w = w+4;
	gai->h = h+3;

	return;
}  

/* ----------------------------------------------------------------------
   NAME:	gnome_fill_info
   DESCRIPTION:	
   ---------------------------------------------------------------------- */

static GnomeAboutInfo*
gnome_fill_info (GtkWidget *widget,
		 const gchar	*title,
		 const gchar	*version,
		 const gchar   *copyright,
		 const gchar   **authors,
		 const gchar   *comments,
		 const gchar   *logo)
{
	GnomeAboutInfo *gai;
	GdkColor light_green = {0, 51914, 64764, 44718};
	GtkStyle *style;

	/* alloc mem for struct */
	gai = g_new (GnomeAboutInfo, 1);

	/* Create fonts */
	/* FIXME: dirty hack, but it solves i18n problem without rewriting the
	   drawing code..  */
	style = gtk_style_ref (widget->style);

	gtk_widget_set_name (widget, "Title");
	gai->font_title = gdk_font_ref (widget->style->font);

	gtk_widget_set_name (widget, "Copyright");
	gai->font_copyright = gdk_font_ref (widget->style->font);

	gtk_widget_set_name (widget, "Author");
	gai->font_author = gdk_font_ref (widget->style->font);

	gtk_widget_set_name (widget, "Names");
	gai->font_names = gdk_font_ref (widget->style->font);

	gtk_widget_set_name (widget, "Comments");
	gai->font_comments = gdk_font_ref (widget->style->font);

	gtk_widget_set_style (widget, style);
	gtk_style_unref (style);
  
	/* Add color */
	memcpy (&gai->light_green, &light_green, sizeof (GdkColor));
	/*   gdk_color_alloc (gdk_colormap_get_system (), &gai->light_green); */

	/* FIXME: this should use a GdkColorContext for allocation.
	 * The cc structure should reside in the GnomeAboutClass so
	 * that all about boxes share it. */
	gdk_color_alloc (gtk_widget_get_default_colormap (), &gai->light_green);

	/* fill struct */
	if(title) 
	  gai->title = g_copy_strings (title, " ",
				       version ? version : "", NULL);
	else
	  gai->title = NULL;
  
	gai->copyright = g_strdup(copyright);
  
	gai->comments = g_strdup(comments);
  
	gai->names = NULL;
	if (authors && authors[0])
	{
		while( *authors ) 
		{
			gai->names = g_list_append (gai->names,
						    g_strdup(*authors));
			authors++;
		}
	}

	/* Calculate width of window */
	gnome_about_calc_size (gai);


	/* Done */
	return gai;
}

/* ----------------------------------------------------------------------
   NAME:	gnome_destroy_about
   DESCRIPTION:	
   ---------------------------------------------------------------------- */

void
gnome_destroy_about (GtkWidget *widget, gpointer *data)
{
	GnomeAboutInfo *gai;

	gai = (GnomeAboutInfo *)data;

	/* Free memory used for title, copyright and comments */
	g_free (gai->title);
	g_free (gai->copyright);
	g_free (gai->comments);

	/* Free GUI's. */
	gdk_font_unref (gai->font_title);
	gdk_font_unref (gai->font_copyright);
	gdk_font_unref (gai->font_author);
	gdk_font_unref (gai->font_names);
	gdk_font_unref (gai->font_comments);

	/* Free memory used for authors */
	g_list_foreach(gai->names, (GFunc)g_free, NULL);
	g_list_free (gai->names);

}

/* ----------------------------------------------------------------------
   NAME:	gnome_about_new
   DESCRIPTION:	Main routine that creates the widget. The arguments are:
                  title      ... Name of app.
		  version    ... version number
		  copyright  ... copyright string
		  authors    ... NULL terminated list of authors
		  comments   ... Other comments
		  logo       ... a logo bitmap file.

		a typical call would be:

   gchar *authors[] = {"author1", "author2", ..., NULL};

   GtkWdiget about = gnome_about_new ("GnoApp", "1.2b",
                           "Copyright (C) 1998",
			   authors,
			   "Comment line 1\nComment line 2",
			   "/usr/local/lib/pixmaps/app_icon.xpm");
   ---------------------------------------------------------------------- */

GtkWidget* 
gnome_about_new (const gchar	*title,
		 const gchar	*version,
		 const gchar   *copyright,
		 const gchar   **authors,
		 const gchar   *comments,
		 const gchar   *logo)
{
	GnomeAbout *about;

	about = gtk_type_new (gnome_about_get_type ());

	gnome_about_construct(about, title, version, copyright,
			      authors, comments, logo);

	return GTK_WIDGET (about);
}

void
gnome_about_construct (GnomeAbout *about,
		       const gchar	*title,
		       const gchar	*version,
		       const gchar   *copyright,
		       const gchar   **authors,
		       const gchar   *comments,
		       const gchar   *logo)
{
	GnomeAboutInfo *ai;
	GtkWidget *frame;
	GtkWidget *drawing_area;
	GtkStyle *style;

	gint w,h;
	char *filename;


	gtk_window_set_title (GTK_WINDOW (about), _("About"));
	gtk_window_set_policy (GTK_WINDOW (about), FALSE, FALSE, TRUE);

	/* x = (gdk_screen_width ()  - w) / 2; */
	/* y = (gdk_screen_height () - h) / 2;    */
	/* gtk_widget_set_uposition ( GTK_WIDGET (about), x, y); */

	frame = gtk_frame_new (NULL);
	gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
	gtk_container_border_width (GTK_CONTAINER (frame), 4);
	gtk_container_add (GTK_CONTAINER (GNOME_DIALOG(about)->vbox),
			   frame);
	gtk_widget_show (frame);

	drawing_area = gtk_drawing_area_new ();

	/* Make it have white bg color */
	gtk_widget_set_name (drawing_area, "DrawingArea");
	gtk_container_add (GTK_CONTAINER (frame), drawing_area);
	style = gtk_widget_get_style (drawing_area);

	ai = gnome_fill_info (drawing_area,
			      title, version, 
			      copyright, authors, 
			      comments, logo);
  
	w = ai->w; h = ai->h;
	/* x = (gdk_screen_width ()  - w) / 2; */
	/* y = (gdk_screen_height () - h) / 2;    */
	/* gtk_widget_set_uposition ( GTK_WIDGET (about), x, y); */

	if (logo)
	{
		filename = gnome_pixmap_file (logo);
		if (filename
		    && gdk_imlib_load_file_to_pixmap (filename, &ai->logo,
						      &ai->mask))
		{
			gdk_window_get_size ((GdkWindow *) ai->logo,
					     &ai->logo_w, &ai->logo_h);
			h += 4 + ai->logo_h;
			ai->h = h;
			ai->w = MAX (w, (ai->logo_w + 6)); 
			w = ai->w;
		}
		else
			ai->logo = NULL;
		g_free(filename);
	}
	else
		ai->logo = NULL;

	gtk_widget_set_usize ( GTK_WIDGET (drawing_area), w, h);
	gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK);

	gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
			    (GtkSignalFunc) gnome_about_repaint, (gpointer) ai);

	gtk_widget_show (drawing_area);
                                                 
	gnome_dialog_append_button ( GNOME_DIALOG(about),
				     GNOME_STOCK_BUTTON_OK);

	gnome_dialog_set_close( GNOME_DIALOG(about),
				TRUE );
}