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

#include <config.h>

Mark McLoughlin's avatar
Mark McLoughlin committed
11 12 13 14
#include "menu-util.h"

#include "basep-widget.h"
#include "foobar-widget.h"
15
#include "multiscreen-stuff.h"
Mark McLoughlin's avatar
Mark McLoughlin committed
16
#include "distribution.h"
17
#include "panel-config-global.h"
Mark McLoughlin's avatar
Mark McLoughlin committed
18
#include "menu.h"
19

20
#undef MENU_UTIL_DEBUG
21

22 23
extern char *kde_menudir;

24 25
extern GlobalConfig global_config;

26
GtkWidget *
27 28 29 30
add_menu_separator (GtkWidget *menu)
{
	GtkWidget *menuitem;
	
31
	menuitem = gtk_separator_menu_item_new ();
32 33
	gtk_widget_set_sensitive (menuitem, FALSE);
	gtk_widget_show (menuitem);
Martin Baulig's avatar
Martin Baulig committed
34
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
35 36

	return menuitem;
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
static void
panel_standard_menu_pos (GtkMenu *menu, gint *x, gint *y, gpointer data)
{
	gint screen_width;
	gint screen_height;
	int screen_basex, screen_basey;
	int screen;
	GtkRequisition requisition;

	gtk_widget_get_child_requisition (GTK_WIDGET (menu),
					  &requisition);

	screen = multiscreen_screen_from_pos (*x, *y);

	if (screen < 0) {
		screen_width = gdk_screen_width ();
		screen_height = gdk_screen_height ();
		screen_basex = 0;
		screen_basey = 0;
	} else {
		screen_width = multiscreen_width (screen);
		screen_height = multiscreen_height (screen);
		screen_basex = multiscreen_x (screen);
		screen_basey = multiscreen_y (screen);
	}

	*x -= screen_basex;
	*y -= screen_basey;

	*x -= 2;
	*y -= 2;

	if ((*x + requisition.width) > screen_width)
		*x -= ((*x + requisition.width) - screen_width);
	if (*x < 0)
		*x = 0;
	if ((*y + requisition.height) > screen_height)
		*y -= ((*y + requisition.height) - screen_height);
	if (*y < 0)
		*y = 0;

	*x += screen_basex;
	*y += screen_basey;
}

84
void
85 86 87 88 89
panel_menu_position (GtkMenu  *menu,
		     gint     *x,
		     gint     *y,
		     gboolean *push_in,
		     gpointer  data)
90 91 92 93
{
	GtkWidget *w = data;
	gint wx, wy;

94 95
	g_return_if_fail (w != NULL);

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
	gdk_window_get_origin (w->window, &wx, &wy);
	/*
	 * Make sure that the popup position is in the panel
	 * as the menu may be popped up by a keystroke
	 */
	if (*x < wx)
		*x = wx;
	else if (*x > wx + w->allocation.width)
		*x = wx + w->allocation.width;

	if (*y < wy)
		*y = wy;
	else if (*y > wy + w->allocation.height)
		*y = wy + w->allocation.height;

111
	*push_in = TRUE;
112 113
	
	panel_standard_menu_pos (menu, x, y, data);
114 115 116
}

void
117 118 119 120 121
applet_menu_position (GtkMenu  *menu,
		      gint     *x,
		      gint     *y,
		      gboolean *push_in,
		      gpointer  data)
122 123 124 125 126 127 128 129 130 131 132 133
{
	AppletInfo *info = data;
	int wx, wy;
	PanelWidget *panel;
	GtkWidget *w; /*the panel window widget*/

	g_return_if_fail(info != NULL);
	g_return_if_fail(info->widget != NULL);

	panel = PANEL_WIDGET(info->widget->parent);
	g_return_if_fail(panel != NULL);
	
134
	w = panel->panel_parent;
135 136 137 138 139 140

	gdk_window_get_origin (info->widget->window, &wx, &wy);
	if(GTK_WIDGET_NO_WINDOW(info->widget)) {
		wx += info->widget->allocation.x;
		wy += info->widget->allocation.y;
	}
141 142 143 144 145

#ifdef MENU_UTIL_DEBUG
	g_print ("applet_menu_position: origin x = %d, y = %d\n", wx, wy);
#endif

146
	if (BASEP_IS_WIDGET (w)) {
147
		*x = *y = 0;
148 149 150 151 152
		basep_widget_get_menu_pos(BASEP_WIDGET(w),
					  GTK_WIDGET(menu),
					  x,y,wx,wy,
					  info->widget->allocation.width,
					  info->widget->allocation.height);
153
       	} else if (FOOBAR_IS_WIDGET (w)) {
154
		GtkRequisition req;
155
		FoobarWidget *foo = FOOBAR_WIDGET (w);
156
		gtk_widget_get_child_requisition (GTK_WIDGET (menu), &req);
157 158 159 160
		*x = MIN (*x,
			  multiscreen_width (foo->screen) +
			  multiscreen_x (foo->screen) -  req.width);
		*y = w->allocation.height + multiscreen_y (foo->screen);
161
	}
162 163

	*push_in = TRUE;
164 165 166 167 168

#ifdef MENU_UTIL_DEBUG
	g_print ("applet_menu_position: x = %d, y = %d, push_in = %s\n",
		 *x, *y, *push_in ? "(true)" : "(false)");
#endif
169 170
}

171
int
172
get_default_menu_flags (void)
173
{
174 175
	DistributionType distribution = get_distribution_type();

Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
176
	int flags = MAIN_MENU_SYSTEM_SUB |
177
		MAIN_MENU_PANEL_SUB | MAIN_MENU_DESKTOP;
178
	
179
	/*guess distribution menus*/
180
	if (distribution != DISTRIBUTION_UNKNOWN)
181
		flags |= MAIN_MENU_DISTRIBUTION_SUB;
182 183

	/*guess KDE menus*/
184
	if (g_file_test (kde_menudir, G_FILE_TEST_IS_DIR))
185 186 187 188
		flags |= MAIN_MENU_KDE_SUB;

	return flags;
}
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

gboolean
got_kde_menus (void)
{
	/*guess KDE menus*/
	if (g_file_test (kde_menudir, G_FILE_TEST_IS_DIR))
		return TRUE;
	else
		return FALSE;
}

gboolean
got_distro_menus (void)
{
	/*guess distribution menus*/
	if (get_distribution_type () != DISTRIBUTION_UNKNOWN)
		return TRUE;
	else
		return FALSE;
}