menu.c 47.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * GNOME panel menu module.
 * (C) 1997 The Free Software Foundation
 *
 * Authors: Miguel de Icaza
 *          Federico Mena
 */

#include <config.h>
#include <stdio.h>
#include <sys/stat.h>
#include <dirent.h>
13
#include <unistd.h>
14
#include <string.h>
15
#include <gnome.h>
16 17

#include "panel-include.h"
18

19 20 21 22
#define SMALL_ICON_SIZE 20
#define BIG_ICON_SIZE   48


23 24
#define MENU_PATH "menu_path"

25 26
#define MENU_PROPERTIES "menu_properties"

27
static char *gnome_folder = NULL;
28

29
GList *small_icons = NULL;
30 31
int show_small_icons = TRUE;

32 33 34
extern GArray *applets;
extern int applet_count;

35 36
extern GlobalConfig global_config;

37 38 39 40 41 42
extern int config_sync_timeout;
extern int panels_to_sync;
extern GList *applets_to_sync;
extern int globals_to_sync;
extern int need_complete_save;

43 44
typedef struct _FileInfo FileInfo;
struct _FileInfo {
George Lebl's avatar
George Lebl committed
45
	char *name;
46
	time_t mtime;
47 48
};

49 50 51 52 53
typedef struct _MenuFinfo MenuFinfo;
struct _MenuFinfo {
	char *menudir;
	int applets;
	char *dir_name;
54
	char *pixmap_name;	
55 56 57 58 59 60 61
	int fake_menu;
	GList *finfo;
};

/*???? this might be ugly, but I guess we can safely assume that we can only
  have one menu open and that nothing weird will happen to the panel that
  opened that menu whilethe user is looking over the choices*/
62
PanelWidget *current_panel = NULL;
63

Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
64 65 66 67 68
/*the most important dialog in the whole application*/
void
about_cb (GtkWidget *widget, gpointer data)
{
	GtkWidget *about;
George Lebl's avatar
George Lebl committed
69
	char *authors[] = {
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
70 71 72 73 74 75
	  "George Lebl (jirka@5z.com)",
	  "Miguel de Icaza (miguel@kernel.org)",
	  "Federico Mena (quartic@gimp.org)",
	  "Tom Tromey (tromey@cygnus.com)",
	  "Ian Main (slow@intergate.bc.ca)",
	  "and finally, The Knights Who Say ... NI!",
76
	  NULL
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
77 78
	  };

79
	about = gnome_about_new ( _("The GNOME Panel"), VERSION,
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
80
			"(C) 1998 the Free Software Foundation",
81
			(const gchar **)authors,
82
			_("This program is responsible for launching "
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
83
			"other applications, embedding small applets "
84
			"within itself, world peace, and random X crashes."),
85
			"gnome-gegl.png");
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
86 87 88 89 90
	gtk_widget_show (about);

	return;
}

91 92 93 94 95 96 97 98 99 100 101 102
void
about_gnome_cb(GtkObject *object, char *program_path)
{
  if(!fork()) {
    int i;
    for(i = 0; i < 255; i++)
      close(i);
    execlp(program_path, program_path, NULL);
    _exit(1);
  }
}

103 104 105 106 107 108 109 110
void
activate_app_def (GtkWidget *widget, void *data)
{
	GnomeDesktopEntry *item = data;

	gnome_desktop_entry_launch (item);
}

111 112 113
static void
kill_small_icon(GtkWidget *widget, gpointer data)
{
114 115
	if(small_icons)
		small_icons = g_list_remove(small_icons,widget);
116 117
}

118 119 120 121 122
static void
add_app_to_panel (GtkWidget *widget, void *data)
{
	GnomeDesktopEntry *ii = data;
	
123
	load_launcher_applet(ii->location,current_panel,0);
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
}

/*reads in the order file and makes a list*/
static GList *
get_presorted_from(char *dir)
{
	char buf[PATH_MAX+1];
	GList *list = NULL;
	char *fname = g_concat_dir_and_file(dir,".order");
	FILE *fp = fopen(fname,"r");
	
	if(!fp) {
		g_free(fname);
		return NULL;
	}
	while(fgets(buf,PATH_MAX+1,fp)!=NULL) {
		char *p = strchr(buf,'\n');
		if(p) *p = '\0';
		list = g_list_append(list,g_strdup(buf));
	}
	fclose(fp);
	g_free(fname);
	return list;
}

static GList *
get_files_from_menudir(char *menudir)
{
	struct dirent *dent;
	DIR *dir;
	GList *out = NULL;
	
	dir = opendir (menudir);
	if (dir == NULL)
		return NULL;
	
	out = get_presorted_from(menudir);
	
	while((dent = readdir (dir)) != NULL) {
		/* Skip over dot files */
		if (dent->d_name [0] == '.')
			continue;
		if(!string_is_in_list(out,dent->d_name))
			out = g_list_append(out,g_strdup(dent->d_name));
	}
	return out;
}

static void
add_drawers_from_dir(char *dirname, char *name, int pos, PanelWidget *panel)
{
	AppletInfo *info;
	Drawer *drawer;
	PanelWidget *newpanel;
	GnomeDesktopEntry *item_info;
	char *dentry_name;
	char *subdir_name;
	char *pixmap_name;
	GList *list;

	if(!g_file_exists(dirname))
		return;

	dentry_name = g_concat_dir_and_file (dirname,
					     ".directory");
	item_info = gnome_desktop_entry_load (dentry_name);
	g_free (dentry_name);

	if(!name)
		subdir_name = item_info?item_info->name:NULL;
	else
		subdir_name = name;
	pixmap_name = item_info?item_info->icon:NULL;

	load_drawer_applet(NULL,pixmap_name,subdir_name,
199
			   panel,pos);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
	
	info = get_applet_info(applet_count -1);
	g_return_if_fail(info);
	
	drawer = info->data;
	g_return_if_fail(drawer);
	newpanel = PANEL_WIDGET(DRAWER_WIDGET(drawer->drawer)->panel);
	
	list = get_files_from_menudir(dirname);
	while(list) {
		char *filename = g_concat_dir_and_file(dirname,
						       list->data);
		struct stat s;
		if (stat (filename, &s) == 0) {
			if (S_ISDIR (s.st_mode)) {
				add_drawers_from_dir(filename,NULL,INT_MAX/2,
						     newpanel);
			} else {
				char *p = strrchr(filename,'.');
				if(p && strcmp(p,".desktop")==0)
					/*we load the applet at the right
					  side, that is end of the drawer*/
					load_launcher_applet(filename,
223 224
							     newpanel,
							     INT_MAX/2);
225 226 227 228
			}
		}
		g_free(filename);
		g_free(list->data);
229
		list = my_g_list_pop_first(list);
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	}
}

/*add a drawer with the contents of a menu to the panel*/
static void
add_menudrawer_to_panel(GtkWidget *w, gpointer data)
{
	MenuFinfo *mf = data;
	g_return_if_fail(mf);
	
	add_drawers_from_dir(mf->menudir,mf->dir_name,0,
			     current_panel);
}

static void
add_menu_to_panel (GtkWidget *widget, void *data)
{
	MenuFinfo *mf = data;
248
	if(mf)
249
		load_menu_applet(mf->menudir,0, current_panel, 0);
250
	else
251
		load_menu_applet(NULL,0, current_panel, 0);
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

/*most of this function stolen from the real gtk_menu_popup*/
static void
restore_grabs(GtkWidget *w, gpointer data)
{
	GtkWidget *menu_item = data;
	GtkMenu *menu = GTK_MENU(menu_item->parent); 
	GtkWidget *xgrab_shell;
	GtkWidget *parent;
	/* Find the last viewable ancestor, and make an X grab on it
	 */
	parent = GTK_WIDGET (menu);
	xgrab_shell = NULL;
	while (parent) {
		gboolean viewable = TRUE;
		GtkWidget *tmp = parent;

		while (tmp) {
			if (!GTK_WIDGET_MAPPED (tmp)) {
				viewable = FALSE;
				break;
			}
			tmp = tmp->parent;
		}

		if (viewable)
			xgrab_shell = parent;

		parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
	}

	/*only grab if this HAD a grab before*/
	if (xgrab_shell && (GTK_MENU_SHELL (xgrab_shell)->have_xgrab)) {
		GdkCursor *cursor = gdk_cursor_new (GDK_ARROW);

		GTK_MENU_SHELL (xgrab_shell)->have_xgrab = 
			(gdk_pointer_grab (xgrab_shell->window, TRUE,
					   GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
					   GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK,
					   NULL, cursor, 0) == 0);
		gdk_cursor_destroy (cursor);
	}

	gtk_grab_add (GTK_WIDGET (menu));
}
	


302 303
static int
show_item_menu(GtkWidget *w, GdkEvent *event, gpointer data)
304
{
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
	GtkWidget *menu, *menuitem, *prop_item;
	GdkEventButton *bevent = (GdkEventButton *)event;
	GnomeDesktopEntry *dentry;
	MenuFinfo *mf;
	int type = GPOINTER_TO_INT(data);

	if(event->type!=GDK_BUTTON_PRESS)
		return FALSE;
	
	dentry = gtk_object_get_data(GTK_OBJECT(w),"dentry");
	mf = gtk_object_get_data(GTK_OBJECT(w),"mf");
	menu = gtk_object_get_data(GTK_OBJECT(w),"menu");
	prop_item = gtk_object_get_data(GTK_OBJECT(w),"prop_item");
	
	if(!menu) {
		menu = gtk_menu_new ();
		gtk_object_set_data(GTK_OBJECT(w),"menu",menu);
322 323 324 325
		gtk_signal_connect(GTK_OBJECT(menu),"deactivate",
				   GTK_SIGNAL_FUNC(restore_grabs),
				   w->parent->parent);

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

		if(type == 1) {
			menuitem = gtk_menu_item_new ();
			setup_menuitem (menuitem, 0,
					_("Add this launcher to panel"));
			gtk_menu_append (GTK_MENU (menu), menuitem);
			gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
					   GTK_SIGNAL_FUNC(add_app_to_panel),
					   dentry);
		} else {
			menuitem = gtk_menu_item_new ();
			setup_menuitem (menuitem, 0,
					_("Add this as drawer to panel"));
			gtk_menu_append (GTK_MENU (menu), menuitem);
			gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
				   GTK_SIGNAL_FUNC(add_menudrawer_to_panel),
				   mf);

			menuitem = gtk_menu_item_new ();
			setup_menuitem (menuitem, 0,
					_("Add this as menu to panel"));
			gtk_menu_append (GTK_MENU (menu), menuitem);
			gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
					   GTK_SIGNAL_FUNC(add_menu_to_panel),
					   mf);
		}

		prop_item = gtk_menu_item_new ();
		gtk_object_set_data(GTK_OBJECT(w),"prop_item",prop_item);
		setup_menuitem (prop_item, 0, _("Properties ..."));
		gtk_menu_append (GTK_MENU (menu), prop_item);
	}
	
	/*FIXME: do properties*/
	gtk_widget_set_sensitive(prop_item,FALSE);

	gtk_menu_popup (GTK_MENU (menu),
			NULL,
			NULL,
			NULL,
			NULL,
			bevent->button,
			0);
	
	return TRUE;
}

static int
destroy_item_menu(GtkWidget *w, gpointer data)
{
	GtkWidget *menu;

	menu = gtk_object_get_data(GTK_OBJECT(w),"menu");
	
	if(menu)
		gtk_widget_destroy(menu);
382
	return FALSE;
383 384
}

385 386 387 388

static void
setup_title_menuitem (GtkWidget *menuitem, GtkWidget *pixmap, char *title,
		      MenuFinfo *mf)
389 390 391 392 393 394 395 396
{
	GtkWidget *label, *hbox, *align;

	label = gtk_label_new (title);
	gtk_misc_set_alignment (GTK_MISC(label), 0.0, 0.5);
	gtk_widget_show (label);
	
	hbox = gtk_hbox_new (FALSE, 0);
397
	gtk_widget_show (hbox);
398 399
	
	align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
400
	gtk_widget_show (align);
401 402
	gtk_container_border_width (GTK_CONTAINER (align), 1);

403 404 405 406 407 408 409 410 411 412 413
	if (pixmap) {
		gtk_container_add (GTK_CONTAINER (align), pixmap);
		gtk_widget_set_usize (align, 22, 16);
		gtk_widget_show (pixmap);
	} else
		gtk_widget_set_usize (align, 22, 16);

	small_icons = g_list_prepend (small_icons, align);
	gtk_signal_connect(GTK_OBJECT(align),"destroy",
			   GTK_SIGNAL_FUNC(kill_small_icon),NULL);

414 415 416
	gtk_box_pack_start (GTK_BOX (hbox), align, FALSE, FALSE, 0);

	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 4);
417
	if(mf) {
418 419 420 421 422 423 424 425
		GtkWidget *w = gtk_button_new_with_label(_("..."));
		gtk_object_set_data(GTK_OBJECT(w),"mf",mf);
		gtk_signal_connect(GTK_OBJECT(w),"event",
				   GTK_SIGNAL_FUNC(show_item_menu),
				   GINT_TO_POINTER(0));
		gtk_signal_connect(GTK_OBJECT(w),"destroy",
				   GTK_SIGNAL_FUNC(destroy_item_menu),
				   NULL);
426 427
		gtk_widget_show(w);
		gtk_box_pack_end (GTK_BOX (hbox), w, FALSE, FALSE, 0);
428 429
		/*this is not really a problem for large fonts but it
		  makes the button smaller*/
430
		gtk_widget_set_usize(w,0,16);
431

432
	}
433 434
	gtk_container_add (GTK_CONTAINER (menuitem), hbox);

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

	gtk_widget_show (menuitem);

	gtk_signal_connect_object(GTK_OBJECT(menuitem),"select",
				  GTK_SIGNAL_FUNC(gtk_item_deselect),
				  GTK_OBJECT(menuitem));
}

static void
setup_full_menuitem (GtkWidget *menuitem, GtkWidget *pixmap, char *title,
		     GnomeDesktopEntry *dentry)
{
	GtkWidget *label, *hbox, *align;

	label = gtk_label_new (title);
	gtk_misc_set_alignment (GTK_MISC(label), 0.0, 0.5);
	gtk_widget_show (label);
	
	hbox = gtk_hbox_new (FALSE, 0);
	gtk_widget_show (hbox);
	
	align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
	gtk_widget_show (align);
	gtk_container_border_width (GTK_CONTAINER (align), 1);

460 461 462
	if (pixmap) {
		gtk_container_add (GTK_CONTAINER (align), pixmap);
		gtk_widget_set_usize (align, 22, 16);
463
		gtk_widget_show (pixmap);
464 465 466
	} else
		gtk_widget_set_usize (align, 22, 16);

467 468 469
	small_icons = g_list_prepend (small_icons, align);
	gtk_signal_connect(GTK_OBJECT(align),"destroy",
			   GTK_SIGNAL_FUNC(kill_small_icon),NULL);
470

471 472 473 474 475

	gtk_box_pack_start (GTK_BOX (hbox), align, FALSE, FALSE, 0);

	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 4);
	if(dentry) {
476 477 478 479 480 481 482 483
		GtkWidget *w = gtk_button_new_with_label(_("..."));
		gtk_object_set_data(GTK_OBJECT(w),"dentry",dentry);
		gtk_signal_connect(GTK_OBJECT(w),"event",
				   GTK_SIGNAL_FUNC(show_item_menu),
				   GINT_TO_POINTER(1));
		gtk_signal_connect(GTK_OBJECT(w),"destroy",
				   GTK_SIGNAL_FUNC(destroy_item_menu),
				   NULL);
484 485 486 487 488 489 490 491
		gtk_widget_show(w);
		gtk_box_pack_end (GTK_BOX (hbox), w, FALSE, FALSE, 0);
		/*this is not really a problem for large fonts but it
		  makes the button smaller*/
		gtk_widget_set_usize(w,0,16);
	}
	gtk_container_add (GTK_CONTAINER (menuitem), hbox);

492 493 494
	gtk_widget_show (menuitem);
}

495 496 497 498 499 500
void
setup_menuitem (GtkWidget *menuitem, GtkWidget *pixmap, char *title)
{
	setup_full_menuitem(menuitem,pixmap,title,NULL);
}

501 502 503 504 505
void
free_app_def (GtkWidget *widget, void *data)
{
	GnomeDesktopEntry *item = data;

506 507
	if(item)
		gnome_desktop_entry_free (item);
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
}

void
add_menu_separator (GtkWidget *menu)
{
	GtkWidget *menuitem;
	
	menuitem = gtk_menu_item_new ();
	gtk_widget_show (menuitem);
	gtk_container_add (GTK_CONTAINER (menu), menuitem);
}

void
free_string (GtkWidget *widget, void *data)
{
	g_free (data);
}

526 527 528 529
static int
add_drawer_to_panel (GtkWidget *widget, void *data)
{
	load_drawer_applet(NULL,NULL,NULL,
530
			   current_panel, 0);
531 532 533 534 535 536
	return TRUE;
}

static int
add_logout_to_panel (GtkWidget *widget, void *data)
{
537
	load_logout_applet(current_panel, 0);
538
	return TRUE;
539 540
}

George Lebl's avatar
George Lebl committed
541
static int
542 543 544
add_applet (GtkWidget *w, gpointer data)
{
	GnomeDesktopEntry *ii = data;
545 546 547
	char *path;
	char *param;

548
	path = ii->exec[0];
549 550 551 552 553 554 555

	g_return_val_if_fail(path!=NULL,FALSE);
	
	if((ii->exec[1])!=NULL)
		param = gnome_string_joinv (" ", ii->exec + 1);
	else
		param = NULL;
556

557
	load_extern_applet(path,param,NULL,current_panel,0);
558

559
	if(param) g_free(param);
560
	return TRUE;
561 562
}

George Lebl's avatar
George Lebl committed
563
static int
564 565 566 567 568 569 570 571 572
check_finfo_list(GList *finfo)
{
	struct stat s;
	FileInfo *fi;

	for(;finfo!=NULL;finfo=g_list_next(finfo)) {
		fi = finfo->data;
		if (stat (fi->name, &s) == -1)
			continue;
573
		if(fi->mtime != s.st_mtime)
574 575 576 577 578 579
			return FALSE;
	}
	return TRUE;
}

static FileInfo *
George Lebl's avatar
George Lebl committed
580
make_finfo(char *name)
581 582 583 584 585 586 587 588 589
{
	struct stat s;
	FileInfo *fi;

	if (stat (name, &s) == -1)
		return NULL;

	fi = g_new(FileInfo,1);
	fi->name = g_strdup(name);
590
	fi->mtime = s.st_mtime;
591 592 593 594
	return fi;
}

static FileInfo *
George Lebl's avatar
George Lebl committed
595
make_finfo_s(char *name, struct stat *s)
596 597 598 599 600
{
	FileInfo *fi;

	fi = g_new(FileInfo,1);
	fi->name = g_strdup(name);
601
	fi->mtime = s->st_mtime;
602 603 604
	return fi;
}

605 606
static void
destroy_mf(MenuFinfo *mf)
607
{
608 609 610 611
	while(mf->finfo) {
		FileInfo *fi = mf->finfo->data;
		if(fi->name) g_free(fi->name);
		g_free(fi);
612
		mf->finfo = my_g_list_pop_first(mf->finfo);
613
	}
614 615 616 617
	if(mf->menudir) g_free(mf->menudir);
	if(mf->dir_name) g_free(mf->dir_name);
	if(mf->pixmap_name) g_free(mf->pixmap_name);
	g_free(mf);
618 619
}

620

621 622 623
static int
menu_destroy(GtkWidget *menu, gpointer data)
{
624 625 626
	GList *mfl = gtk_object_get_data(GTK_OBJECT(menu),"mf");
	while(mfl) {
		MenuFinfo *mf = mfl->data;
627
		destroy_mf(mf);
628
		mfl = my_g_list_pop_first(mfl);
629
	}
630
	gtk_object_set_data(GTK_OBJECT(menu),"mf",NULL);
631 632 633
	return FALSE;
}

634 635
static void add_menu_widget (Menu *menu, GList *menudirl,
			     int main_menu, int fake_subs);
636
static GtkWidget * create_menu_at (GtkWidget *menu,
637
				   char *menudir, 
638
				   int applets, char *dir_name,
639
				   char *pixmap_name, int fake_submenus);
640

641 642 643 644 645
/*if menu is NULL that means just reread the menu don't do anything with the
  applet*/
static GtkWidget *
check_and_reread(GtkWidget *menuw,Menu *menu,int main_menu)
{
646
	/*we are creating a whole new menuf or an applet if menu isn't NULL*/
647 648 649 650
	if(menu) {
		GList *mfl = gtk_object_get_data(GTK_OBJECT(menuw), "mf");
		GList *list;
		int need_reread = FALSE;
651
		
652 653 654 655 656
		/*we shouldn't warn, this is more for debugging anyway,
		  and nowdays we do have menus that don't have one, this
		  however might be needed for further debugging*/
		/*if(!mfl)
			g_warning("Weird menu doesn't have mf entry");*/
657 658 659 660 661 662 663 664 665 666 667

		/*check if we need to reread this*/
		for(list = mfl; list != NULL; list = g_list_next(list)) {
			MenuFinfo *mf = list->data;
			if(mf->fake_menu || !check_finfo_list(mf->finfo)) {
				need_reread = TRUE;
				break;
			}
		}

		if(need_reread) {
668
			puts("debug: rereading menu");
669
			if(main_menu)
670 671 672 673 674 675 676 677 678 679 680 681
				add_menu_widget(menu,NULL,main_menu,TRUE);
			else {
				GList *dirlist = NULL;
				for(list = mfl; list != NULL;
				    list = g_list_next(list)) {
					MenuFinfo *mf = list->data;
					dirlist = g_list_append(dirlist,
								mf->menudir);
				}
				add_menu_widget(menu,dirlist, main_menu,TRUE);
				g_list_free(dirlist);
			}
682 683 684 685 686 687 688 689

			gtk_widget_unref(menuw);
		}
	} else {
		GList *mfl = gtk_object_get_data(GTK_OBJECT(menuw), "mf");
		GList *list;
		int need_reread = FALSE;
		int all_fake = TRUE;
690
		
691 692
		/*if(!mfl)
			g_warning("Weird menu doesn't have mf entry");*/
693 694 695 696 697 698 699 700 701 702 703 704

		/*check if we need to reread this*/
		for(list = mfl; list != NULL; list = g_list_next(list)) {
			MenuFinfo *mf = list->data;
			if(!mf->fake_menu)
				all_fake = FALSE;
			if(!need_reread &&
			   (mf->fake_menu || !check_finfo_list(mf->finfo)))
				need_reread = TRUE;
		}
		if(need_reread) {
			GtkWidget *old_menu = NULL;
705
			GList *free_list = NULL;
706 707 708 709 710 711
			/*we are all fake so we want to use the previous
			  menu widget*/
			if(all_fake) {
				old_menu = menuw;
				gtk_object_set_data(GTK_OBJECT(old_menu), "mf",
						    NULL);
712 713 714 715 716
				/*set this so that we free it after we use it,
				  since it won't be freed during the
				  destruction of the menu since there will
				  be none*/
				free_list = mfl;
717 718 719 720 721 722 723
			}
			for(list = mfl; list != NULL;
			    list = g_list_next(list)) {
				MenuFinfo *mf = list->data;
				puts("debug: rereading menu");
				menuw = create_menu_at(old_menu,
						       mf->menudir,
724 725
						       mf->applets,
						       mf->dir_name,
726
						       mf->pixmap_name,
727
						       TRUE);
728
				old_menu = menuw;
729
			}
730 731 732
			/*free up stuff that won't be freed somewhere else*/
			while(free_list) {
				MenuFinfo *mf = free_list->data;
733
				destroy_mf(mf);
734
				free_list = my_g_list_pop_first(free_list);
735
			}
736 737 738 739
		}
	}
	return menuw;
}
740

741 742 743 744 745 746 747 748 749 750
static void
submenu_to_display(GtkMenuItem *menuitem, gpointer data)
{
	GtkWidget *menu = check_and_reread(menuitem->submenu,
					   NULL,FALSE);
	g_return_if_fail(menu!=NULL);
	if(menu!=menuitem->submenu)
		gtk_menu_item_set_submenu (GTK_MENU_ITEM(menuitem), menu);
}

751

752 753 754
static GtkWidget *
create_fake_menu_at (char *menudir,
		     int applets,
755 756
		     char *dir_name,
		     char *pixmap_name)
757 758 759
{	
	MenuFinfo *mf;
	GtkWidget *menu;
760
	GList *list;
761 762 763 764 765 766 767
	
	menu = gtk_menu_new ();
	
	mf = g_new(MenuFinfo,1);
	mf->menudir = g_strdup(menudir);
	mf->applets = applets;
	mf->dir_name = dir_name?g_strdup(dir_name):NULL;
768
	mf->pixmap_name = pixmap_name?g_strdup(pixmap_name):NULL;
769 770 771
	mf->fake_menu = TRUE;
	mf->finfo = NULL;
	
772 773
	list = g_list_prepend(NULL,mf);
	gtk_object_set_data(GTK_OBJECT(menu),"mf",list);
774 775 776 777 778 779
	
	gtk_signal_connect(GTK_OBJECT(menu),"destroy",
			   GTK_SIGNAL_FUNC(menu_destroy),NULL);
	
	return menu;
}
780

781
static GtkWidget *
782 783
create_menu_at (GtkWidget *menu,
		char *menudir,
784
		int applets,
785
		char *dir_name,
786
		char *pixmap_name,
787
		int fake_submenus)
788
{	
789 790
	GnomeDesktopEntry *item_info=NULL;
	GnomeDesktopEntry *dir_info=NULL;
791 792 793
	struct stat s;
	char *filename;
	int items = 0;
794
	FileInfo *fi;
795
	GList *finfo = NULL;
796
	GList *flist = NULL;
797
	GList *mfl = NULL;
798
	char *thisfile;
799
	int add_separator = FALSE;
800 801
	int first_item = 0;
	GtkWidget *menuitem;
802
	
803
	MenuFinfo *mf = NULL;
804
	
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
	/*try getting the dir_name and pixmap if we haven't gotten them*/
	if(!dir_name || !pixmap_name) {
		char *dentry_name;

		dentry_name = g_concat_dir_and_file (menudir,
						     ".directory");
		dir_info = gnome_desktop_entry_load (dentry_name);

		/*add the .directory file to the checked files list,
		  but only if we can stat it (if we can't it probably
		  doesn't exist)*/
		fi = make_finfo(dentry_name);
		if(fi)
			finfo = g_list_prepend(finfo,fi);
		g_free (dentry_name);

		if(!dir_name)
			dir_name = dir_info?dir_info->name:"Menu";
		if(!pixmap_name)
			pixmap_name = dir_info?dir_info->icon:NULL;
	}

827 828 829 830
	
	/*add dir to the checked files list*/
	fi = make_finfo(menudir);
	if(!fi)
831
		g_warning("Something is wrong, directory %s can't be stated",
832 833
			  menudir);
	else
834
		finfo = g_list_prepend(finfo,fi);
835

836 837 838 839 840 841 842 843 844
	/*add the order file to the checked files list,
	  but only if we can stat it (if we can't it probably doesn't
	  exist)*/
	filename = g_concat_dir_and_file(menudir,".order");
	fi = make_finfo(filename);
	if(fi)
		finfo = g_list_prepend(finfo,fi);
	g_free(filename);

845 846 847 848
	if(!menu) {
		menu = gtk_menu_new ();
		gtk_signal_connect(GTK_OBJECT(menu),"destroy",
				   GTK_SIGNAL_FUNC(menu_destroy),NULL);
849
	} else {
850
		first_item = g_list_length(GTK_MENU_SHELL(menu)->children);
851
		mfl = gtk_object_get_data(GTK_OBJECT(menu), "mf");
852 853 854
		if(GTK_MENU_SHELL(menu)->children)
			add_separator = TRUE;
	}
855 856

	flist = get_files_from_menudir(menudir);
857
	
858
	while (flist) {
859
		GtkWidget     *sub, *pixmap;
860
		char          *pixmap_name;
861
		char          *menuitem_name;
862
		
863 864
		thisfile = flist->data;
		filename = g_concat_dir_and_file(menudir,thisfile);
865
		flist = my_g_list_pop_first(flist);
866 867
		
		if (stat (filename, &s) == -1) {
868
			g_free (filename);
869
			g_free(thisfile);
870 871 872
			continue;
		}

873 874
		sub = NULL;
		item_info = NULL;
875 876
		if (S_ISDIR (s.st_mode)) {
			char *dentry_name;
877
			
878 879
			dentry_name = g_concat_dir_and_file (filename,
							     ".directory");
880
			item_info = gnome_desktop_entry_load (dentry_name);
881 882 883 884 885 886 887

			/*add the .directory file to the checked files list,
			  but only if we can stat it (if we can't it probably
			  doesn't exist)*/
			fi = make_finfo(dentry_name);
			if(fi)
				finfo = g_list_prepend(finfo,fi);
888 889
			g_free (dentry_name);

890
			menuitem_name = item_info?item_info->name:thisfile;
891
			pixmap_name = item_info?item_info->icon:NULL;
892

893 894 895
			if(fake_submenus)
				sub = create_fake_menu_at (filename,
							   applets,
896 897
							   menuitem_name,
							   pixmap_name);
898
			else
899 900 901 902
				sub = create_menu_at (NULL, filename, 
						      applets,
						      menuitem_name,
						      pixmap_name,
903
						      fake_submenus);
904

905 906 907 908 909 910 911
			if (!sub) {
				g_free(filename);
				g_free(thisfile);
				if(item_info)
					gnome_desktop_entry_free(item_info);
				continue;
			}
912
		} else {
913 914
			char *p = strrchr(filename,'.');
			if (!p || strcmp(p, ".desktop") != 0) {
915
				g_free (filename);
916
				g_free(thisfile);
917 918 919 920 921
				continue;
			}
			item_info = gnome_desktop_entry_load (filename);
			if (!item_info) {
				g_free (filename);
922
				g_free(thisfile);
923 924 925
				continue;
			}
			menuitem_name = item_info->name;
926
			pixmap_name = item_info->icon;
927 928 929 930
			
			/*add file to the checked files list*/
			fi = make_finfo_s(filename,&s);
			if(!fi)
931
				g_warning("Something is wrong, "
932 933 934
					  "file %s can't be stated",
					  filename);
			else
935
				finfo = g_list_prepend(finfo,fi);
936 937 938 939 940
		}
		
		items++;
		
		menuitem = gtk_menu_item_new ();
941
		if (sub) {
942
			gtk_menu_item_set_submenu (GTK_MENU_ITEM(menuitem), sub);
943 944 945 946
			gtk_signal_connect(GTK_OBJECT(menuitem),"select",
					   GTK_SIGNAL_FUNC(submenu_to_display),
					   NULL);
		}
947 948 949

		pixmap = NULL;
		if (pixmap_name && g_file_exists (pixmap_name)) {
950 951 952
			pixmap = gnome_pixmap_new_from_file_at_size (pixmap_name,
								     SMALL_ICON_SIZE,
								     SMALL_ICON_SIZE);
953 954 955 956
			if (pixmap)
				gtk_widget_show (pixmap);
		}

957 958 959 960
		/*setup the menuitem, pass item_info if this is not
		  a submenu or an applet, so that the item can be added*/
 		setup_full_menuitem (menuitem, pixmap, menuitem_name,
				     (sub||applets)?NULL:item_info);
961 962
		if(add_separator) {
			add_menu_separator(menu);
963
			first_item++;
964 965
			add_separator = FALSE;
		}
966 967 968 969 970
		gtk_menu_append (GTK_MENU (menu), menuitem);

		gtk_signal_connect (GTK_OBJECT (menuitem), "destroy",
				    (GtkSignalFunc) free_app_def, item_info);

971 972
		if(item_info && item_info->exec) {
			gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
973 974 975 976
					    applets?
					     GTK_SIGNAL_FUNC(add_applet):
					     GTK_SIGNAL_FUNC(activate_app_def),
					    item_info);
977
		}
978

979
		g_free(filename);
980
		g_free(thisfile);
981
	}
982 983 984 985 986
	
	mf = g_new(MenuFinfo,1);
	mf->menudir = g_strdup(menudir);
	mf->applets = applets;
	mf->dir_name = dir_name?g_strdup(dir_name):NULL;
987
	mf->pixmap_name = pixmap_name?g_strdup(pixmap_name):NULL;
988 989
	mf->fake_menu = FALSE;
	mf->finfo = finfo;
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
	
	if(!applets && items>0) {
		GtkWidget *pixmap;
		menuitem = gtk_menu_item_new();
		gtk_widget_show(menuitem);
		gtk_menu_insert(GTK_MENU(menu),menuitem,first_item);

		pixmap = NULL;
		if (pixmap_name && g_file_exists (pixmap_name)) {
			pixmap = gnome_pixmap_new_from_file_at_size (pixmap_name,
								     SMALL_ICON_SIZE,
								     SMALL_ICON_SIZE);
			if (pixmap)
				gtk_widget_show (pixmap);
		} else if (gnome_folder && g_file_exists (gnome_folder)) {
			pixmap = gnome_pixmap_new_from_file_at_size (gnome_folder,
								     SMALL_ICON_SIZE,
								     SMALL_ICON_SIZE);
			if (pixmap)
				gtk_widget_show (pixmap);
		}
		menuitem = gtk_menu_item_new();
		setup_title_menuitem(menuitem,pixmap,
				     dir_name?dir_name:"Menu",mf);
		gtk_menu_insert(GTK_MENU(menu),menuitem,first_item);
	}

	if(dir_info)
		gnome_desktop_entry_free(dir_info);
1019 1020 1021 1022

	mfl = g_list_append(mfl,mf);

	gtk_object_set_data(GTK_OBJECT(menu),"mf",mfl);
1023
	
1024 1025 1026
	return menu;
}

1027
static void
1028 1029 1030
destroy_menu (GtkWidget *widget, gpointer data)
{
	Menu *menu = data;
1031 1032 1033
	GtkWidget *prop_dialog = gtk_object_get_data(GTK_OBJECT(menu->button),
						     MENU_PROPERTIES);
	if(prop_dialog)
1034
		gtk_widget_destroy(prop_dialog);
1035
	gtk_widget_unref(menu->menu);
1036
	g_free(menu->path);
1037 1038 1039
	g_free(menu);
}

1040 1041 1042 1043
static void
menu_deactivate(GtkWidget *w, gpointer data)
{
	Menu *menu = data;
1044
	GtkWidget *panel = get_panel_parent(menu->button);
1045
	/* allow the panel to hide again */
1046 1047
	if(IS_SNAPPED_WIDGET(panel))
		SNAPPED_WIDGET(panel)->autohide_inhibit = FALSE;
1048 1049
	BUTTON_WIDGET(menu->button)->in_button = FALSE;
	BUTTON_WIDGET(menu->button)->ignore_leave = FALSE;
1050
	button_widget_up(BUTTON_WIDGET(menu->button));
1051 1052
}

George Lebl's avatar
George Lebl committed
1053 1054
static char *
get_real_menu_path(char *arguments, char *menu_base)
1055 1056
{
	char *this_menu;
1057

1058
	/*if null, let's put the main menu up*/
1059
	if (!arguments || !*arguments)
1060
		arguments = ".";
1061

1062 1063 1064 1065
	if (*arguments == '/')
		this_menu = g_strdup (arguments);
	else
		this_menu = g_concat_dir_and_file (menu_base, arguments);
1066

1067 1068 1069 1070 1071 1072
	if (!g_file_exists (this_menu)) {
		g_warning("menu %s does not exist "
			  "(base is %s, arguments are %s)",
			  this_menu, menu_base, arguments);
		g_free (this_menu);
		return NULL;
1073
	}
1074
	return this_menu;
1075 1076
}

1077 1078


1079 1080 1081
void
panel_configure (GtkWidget *widget, void *data)
{
1082
	panel_config_global();
1083 1084 1085
}

static GtkWidget *
1086
create_applets_menu(int fake_submenus)
1087
{
1088 1089 1090
	GtkWidget *applet_menu;
	char *menu_base = gnome_unconditional_datadir_file ("applets");
	char *menudir;
1091

1092 1093 1094 1095 1096 1097
	menudir = g_concat_dir_and_file (menu_base, ".");
	g_free (menu_base);
	if (!g_file_exists (menudir)) {
		g_free (menudir);
		return NULL;
	}
1098

1099 1100
	applet_menu = create_menu_at(NULL,menudir,TRUE,_("Applets"),NULL,
				     fake_submenus);
1101 1102
	g_free (menudir);
	return applet_menu;
1103 1104
}

1105 1106 1107
static void
create_new_panel(GtkWidget *w,gpointer data)
{
1108
	PanelType type = GPOINTER_TO_INT(data);
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
	GdkColor bcolor = {0,0,0,1};
	GtkWidget *panel;
	
	switch(type) {
	case DRAWER_PANEL:
		break; /*we don't handle this one here*/
	case SNAPPED_PANEL: 
		panel = snapped_widget_new(SNAPPED_BOTTOM,
					   SNAPPED_EXPLICIT_HIDE,
					   SNAPPED_SHOWN,
1119
					   TRUE,
1120 1121 1122 1123 1124 1125 1126
					   PANEL_BACK_NONE,
					   NULL,
					   TRUE,
					   &bcolor);
		panel_setup(panel);
		gtk_widget_show(panel);
		break;
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
	case CORNER_PANEL: 
		panel = corner_widget_new(CORNER_NE,
					  PANEL_HORIZONTAL,
					  CORNER_SHOWN,
					  PANEL_BACK_NONE,
					  NULL,
					  TRUE,
					  &bcolor);
		panel_setup(panel);
		gtk_widget_show(panel);
		break;
1138
	default: break;
1139
	}
1140
	panels_to_sync = TRUE;
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
}


static GtkWidget *
create_add_panel_submenu (void)
{
	GtkWidget *menu, *menuitem;

	menu = gtk_menu_new ();
	
	menuitem = gtk_menu_item_new ();
	setup_menuitem (menuitem, 0, _("Drawer"));
	gtk_menu_append (GTK_MENU (menu), menuitem);
	gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1155 1156
			   (GtkSignalFunc) add_drawer_to_panel,
			   NULL);
1157 1158 1159 1160 1161 1162 1163 1164

	menuitem = gtk_menu_item_new ();
	setup_menuitem (menuitem, 0, _("Edge Panel"));
	gtk_menu_append (GTK_MENU (menu), menuitem);
	gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
			   (GtkSignalFunc) create_new_panel,
			   (gpointer)SNAPPED_PANEL);

1165 1166 1167 1168 1169 1170 1171
	menuitem = gtk_menu_item_new ();
	setup_menuitem (menuitem, 0, _("Corner Panel"));
	gtk_menu_append (GTK_MENU (menu), menuitem);
	gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
			   (GtkSignalFunc) create_new_panel,
			   (gpointer)CORNER_PANEL);

1172 1173 1174
	return menu;
}

1175
static GtkWidget *
1176
create_system_menu(GtkWidget *menu, int fake_submenus)
1177
{
1178 1179 1180 1181 1182
	char *menu_base = gnome_unconditional_datadir_file ("apps");
	char *menudir;
	
	menudir = g_concat_dir_and_file (menu_base, ".");
	g_free (menu_base);
1183 1184 1185 1186
	if (g_file_exists (menudir)) {
		menu = create_menu_at (menu,menudir,FALSE,_("System Menus"),
				       gnome_folder, fake_submenus);
		g_return_val_if_fail(menu,NULL);
1187
		g_free (menudir);
1188 1189
	} else
		g_warning("No system menus found!");
1190
	
1191
	return menu;
1192 1193 1194
}

static GtkWidget *
1195
create_user_menu(GtkWidget *menu, int fake_submenus)
1196 1197 1198
{
	char *menu_base = gnome_util_home_file ("apps");
	char *menudir = g_concat_dir_and_file (menu_base, ".");
1199
	
1200 1201 1202 1203
	g_free (menu_base);
	if (!g_file_exists (menudir))
		mkdir (menudir, 0755);
	if (g_file_exists (menudir)) {
1204 1205 1206
		menu = create_menu_at (menu,menudir, FALSE,
				       _("User Menus"),
				       gnome_folder,fake_submenus);
1207
	}
1208 1209
	g_free (menudir); 
	return menu;
1210 1211
}

1212 1213
GtkWidget *
create_panel_root_menu(GtkWidget *panel)
1214 1215
{
	GtkWidget *menuitem;
1216 1217 1218 1219
	GtkWidget *panel_menu;
	GtkWidget *menu;

	panel_menu = gtk_menu_new();
1220

1221
	menuitem = gtk_menu_item_new ();
1222 1223 1224 1225 1226
	setup_menuitem (menuitem, 0, _("System Menus"));
	gtk_menu_append (GTK_MENU (panel_menu), menuitem);
	menu = gtk_menu_new();
	create_system_menu(menu,TRUE);
	gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem),menu);
1227 1228 1229 1230 1231
	gtk_signal_connect(GTK_OBJECT(menuitem),"select",
			   GTK_SIGNAL_FUNC(submenu_to_display),
			   NULL);

	menuitem = gtk_menu_item_new ();
1232 1233 1234 1235 1236
	setup_menuitem (menuitem, 0, _("User Menus"));
	gtk_menu_append (GTK_MENU (panel_menu), menuitem);
	menu = gtk_menu_new();
	create_user_menu(menu,TRUE);
	gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem),menu);
1237 1238 1239
	gtk_signal_connect(GTK_OBJECT(menuitem),"select",
			   GTK_SIGNAL_FUNC(submenu_to_display),
			   NULL);
1240

1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
	menuitem = gtk_menu_item_new();
	gtk_menu_append(GTK_MENU(panel_menu), menuitem);
	gtk_widget_show(menuitem);

	make_panel_submenu(panel_menu,TRUE);

	menuitem = gtk_menu_item_new ();
	setup_menuitem (menuitem, NULL, _("Remove this panel"));
	gtk_menu_append (GTK_MENU (panel_menu), menuitem);
	gtk_signal_connect_object (GTK_OBJECT (menuitem), "activate",
				   GTK_SIGNAL_FUNC(gtk_widget_destroy),
				   GTK_OBJECT(panel));
	gtk_object_set_data(GTK_OBJECT(panel),"remove_item",menuitem);

	menuitem = gtk_menu_item_new();
	gtk_menu_append(GTK_MENU(panel_menu), menuitem);
	gtk_widget_show(menuitem);

	menuitem = gtk_menu_item_new ();
	setup_menuitem (menuitem,
			gnome_stock_pixmap_widget(panel_menu,
						  GNOME_STOCK_PIXMAP_QUIT),
			_("Log out"));
	gtk_menu_append (GTK_MENU (panel_menu), menuitem);
	gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
			    GTK_SIGNAL_FUNC(panel_quit),
			    NULL);

	return panel_menu;
}


static void
current_panel_config(GtkWidget *w, gpointer data)
{
	GtkWidget *parent = gtk_object_get_data(GTK_OBJECT(current_panel),
						PANEL_PARENT);
	panel_config(parent);
}

void
make_panel_submenu (GtkWidget *menu, int fake_submenus)
{
	GtkWidget *menuitem;

1286
	menuitem = gtk_menu_item_new ();
1287 1288
	setup_menuitem (menuitem, 0, _("Add applet"));
	gtk_menu_append (GTK_MENU (menu), menuitem);
1289 1290
	gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem),
				   create_applets_menu(fake_submenus));
1291 1292 1293
	gtk_signal_connect(GTK_OBJECT(menuitem),"select",
			   GTK_SIGNAL_FUNC(submenu_to_display),
			   NULL);
1294

1295 1296 1297 1298 1299 1300
	menuitem = gtk_menu_item_new ();
	setup_menuitem (menuitem, 0, _("Add new panel"));
	gtk_menu_append (GTK_MENU (menu), menuitem);
	gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem),
				   create_add_panel_submenu());

1301 1302 1303 1304 1305 1306
	add_menu_separator(menu);

	menuitem = gtk_menu_item_new ();
	setup_menuitem (menuitem, 0, _("Add main menu"));
	gtk_menu_append (GTK_MENU (menu), menuitem);
	gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1307
			   GTK_SIGNAL_FUNC(add_menu_to_panel),
1308
			   NULL);
1309

1310 1311 1312 1313
	menuitem = gtk_menu_item_new ();
	setup_menuitem (menuitem, 0, _("Add log out button"));
	gtk_menu_append (GTK_MENU (menu), menuitem);
	gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1314
			   GTK_SIGNAL_FUNC(add_logout_to_panel),
1315
			   NULL);
1316

1317

1318 1319 1320 1321
	menuitem = gtk_menu_item_new ();
	setup_menuitem (menuitem, 0, _("Add swallowed app"));
	gtk_menu_append (GTK_MENU (menu), menuitem);
	gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1322
			   GTK_SIGNAL_FUNC(ask_about_swallowing),NULL);
1323

1324 1325 1326 1327 1328 1329
	menuitem = gtk_menu_item_new ();
	setup_menuitem (menuitem, 0, _("Add new launcher"));
	gtk_menu_append (GTK_MENU (menu), menuitem);
	gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
			   GTK_SIGNAL_FUNC(ask_about_launcher),NULL);

1330
	add_menu_separator(menu);
1331

1332
	menuitem = gtk_menu_item_new ();
1333 1334
	setup_menuitem (menuitem,
			gnome_stock_pixmap_widget(menu,
1335
						  GNOME_STOCK_PIXMAP_PREFERENCES),
1336
			_("This panel properties..."));
1337
	gtk_menu_append (GTK_MENU (menu), menuitem);
1338
	gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
1339 1340
			    GTK_SIGNAL_FUNC(current_panel_config), 
			    NULL);
1341

Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
1342
	menuitem = gtk_menu_item_new ();
1343 1344
	setup_menuitem (menuitem,
			gnome_stock_pixmap_widget(menu,
1345 1346
						  GNOME_STOCK_PIXMAP_PREFERENCES),
			_("Global properties..."));
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
1347 1348
	gtk_menu_append (GTK_MENU (menu), menuitem);
	gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
1349 1350
			    GTK_SIGNAL_FUNC(panel_configure), 
			    NULL);
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
1351

1352 1353 1354 1355 1356
}

static void
panel_lock (GtkWidget *widget, void *data)
{
1357
	system ("(xlock&)");
1358 1359 1360
}

static void
1361
add_special_entries (GtkWidget *menu, int fake_submenus)
1362 1363
{
	GtkWidget *menuitem;
1364
	GtkWidget *panel_menu;
1365
	char *char_tmp;
1366

1367 1368 1369 1370
	/* Panel entry */

	add_menu_separator (menu);

1371 1372
	panel_menu = gtk_menu_new();
	make_panel_submenu(panel_menu,fake_submenus);
1373 1374 1375
	menuitem = gtk_menu_item_new ();
	setup_menuitem (menuitem, 0, _("Panel"));
	gtk_menu_append (GTK_MENU (menu), menuitem);
1376
	gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem),panel_menu);
1377 1378

	add_menu_separator (menu);
1379

1380 1381 1382
	menuitem = gtk_menu_item_new ();
	setup_menuitem (menuitem, 0, _("Lock screen"));
	gtk_menu_append (GTK_MENU (menu), menuitem);
1383 1384
	gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
			    GTK_SIGNAL_FUNC(panel_lock), 0);
1385

1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
	menuitem = gtk_menu_item_new ();
	setup_menuitem (menuitem,
			gnome_stock_pixmap_widget(menu,
						  GNOME_STOCK_PIXMAP_ABOUT),
			_("About..."));
	gtk_menu_append (GTK_MENU (menu), menuitem);
	gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
			    GTK_SIGNAL_FUNC(about_cb),
			    NULL);

1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
	char_tmp = gnome_is_program_in_path("guname");
	if(char_tmp) {
	  menuitem = gtk_menu_item_new ();
	  setup_menuitem (menuitem,
			  gnome_stock_pixmap_widget(menu,
						    GNOME_STOCK_PIXMAP_ABOUT),
			  _("About GNOME..."));
	  gtk_menu_append (GTK_MENU (menu), menuitem);
	  gtk_signal_connect_interp(GTK_OBJECT (menuitem), "activate",
				    (GtkCallbackMarshal)about_gnome_cb,
				    char_tmp, (GtkDestroyNotify)g_free,
				    TRUE);
	}

1410
	menuitem = gtk_menu_item_new ();
1411 1412 1413 1414
	setup_menuitem (menuitem,
			gnome_stock_pixmap_widget(menu,
						  GNOME_STOCK_PIXMAP_QUIT),
			_("Log out"));
1415
	gtk_menu_append (GTK_MENU (menu), menuitem);
1416
	gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
1417
			    GTK_SIGNAL_FUNC(panel_quit), 0);
1418 1419

}
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
1420

1421
static GtkWidget *
1422
create_root_menu(int fake_submenus, MainMenuType type)
1423 1424
{
	GtkWidget *root_menu;
1425
	GtkWidget *uroot_menu;
1426

1427
	root_menu = create_system_menu(NULL,fake_submenus);
1428

1429 1430 1431 1432
	if(type == MAIN_MENU_BOTH)
		root_menu = create_user_menu(root_menu, fake_submenus);
	else
		uroot_menu = create_user_menu(NULL, fake_submenus);
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
	
	if(type == MAIN_MENU_USER) {
		GtkWidget *menuitem;
		menuitem = gtk_menu_item_new ();
		setup_menuitem (menuitem, 0, _("System Menus"));
		gtk_menu_append (GTK_MENU (uroot_menu), menuitem);
		gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem),
					   root_menu);
		gtk_signal_connect(GTK_OBJECT(menuitem),"select",
				   GTK_SIGNAL_FUNC(submenu_to_display),
				   NULL);
		root_menu = uroot_menu;
	} else if(type == MAIN_MENU_SYSTEM) {
		GtkWidget *menuitem;
		menuitem = gtk_menu_item_new ();
		setup_menuitem (menuitem, 0, _("User Menus"));
		gtk_menu_append (GTK_MENU (root_menu), menuitem);
		gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem),
					   uroot_menu);
		gtk_signal_connect(GTK_OBJECT(menuitem),"select",
				   GTK_SIGNAL_FUNC(submenu_to_display),
				   NULL);
	}
1456

1457
	add_special_entries (root_menu, fake_submenus);
1458 1459 1460 1461
	
	return root_menu;
}

1462
static void
1463
add_menu_widget (Menu *menu, GList *menudirl, int main_menu, int fake_subs)
1464
{
1465 1466 1467
	GList *li;

	if (main_menu)
1468
		menu->menu = create_root_menu(fake_subs, menu->main_menu_type);
1469 1470
	else {
		menu->menu = NULL;
1471 1472
		for(li=menudirl;li!=NULL;li=g_list_next(li))
			menu->menu = create_menu_at (menu->menu,li->data,
1473
						     FALSE, NULL, NULL,
1474
						     fake_subs);
1475
	}
1476 1477 1478 1479
	gtk_signal_connect (GTK_OBJECT (menu->menu), "deactivate",
			    GTK_SIGNAL_FUNC (menu_deactivate), menu);
}

1480
static void
1481
menu_button_pressed(GtkWidget *widget, gpointer data)
1482 1483
{
	Menu *menu = data;
1484 1485
	GdkEventButton *bevent = (GdkEventButton*)gtk_get_current_event();
	GtkWidget *wpanel = get_panel_parent(menu->button);
1486 1487 1488
	int applet_id =
		GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menu->button),
						    "applet_id"));
1489
	int main_menu = (strcmp (menu->path, ".") == 0);
1490
	
1491
	check_and_reread(menu->menu,menu,main_menu);
1492

1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
	/*so that the panel doesn't pop down until we're
	  done with the menu */
	if(IS_SNAPPED_WIDGET(wpanel)) {
		SNAPPED_WIDGET(wpanel)->autohide_inhibit = TRUE;
		snapped_widget_queue_pop_down(SNAPPED_WIDGET(wpanel));
	}

	/*this HAS to be set everytime we popup the menu*/
	current_panel =
		gtk_object_get_data(GTK_OBJECT(menu->button),
				    PANEL_APPLET_PARENT_KEY);