drawer-widget.c 12.1 KB
Newer Older
1
/* Gnome panel: drawer widget
2 3 4 5
 * (C) 1999 the Free Software Foundation
 *
 * Authors:  Jacob Berkman
 *           George Lebl
6 7
 *
 */
8

9
#include "drawer-widget.h"
10
#include "border-widget.h"
11
#include "floating-widget.h"
12
#include "panel_config_global.h"
13

14
extern GlobalConfig global_config;
15 16 17 18 19 20 21 22 23 24 25
extern int pw_minimized_size;

static void drawer_pos_class_init (DrawerPosClass *klass);
static void drawer_pos_init (DrawerPos *pos);

static void drawer_pos_set_hidebuttons (BasePWidget *basep);
static PanelOrientType drawer_pos_get_applet_orient (BasePWidget *basep);

static PanelOrientType drawer_pos_get_hide_orient (BasePWidget *basep);
static void drawer_pos_get_hide_pos (BasePWidget *basep,
				     PanelOrientType hide_orient,
26 27
				     int *x, int *y,
				     int w, int h);
28 29
static void drawer_pos_get_hide_size (BasePWidget *basep, 
				      PanelOrientType hide_orient,
30
				      int *w, int *h);
31

32
static void drawer_pos_get_pos(BasePWidget *basep,
33 34
			       int *x, int *y,
			       int width, int height);
35

36 37 38
static void drawer_pos_get_menu_pos (BasePWidget *basep,
				     GtkWidget *widget,
				     GtkRequisition *mreq,
39 40 41
				     int *x, int *y,
				     int wx, int wy,
				     int ww, int wh);
42

43 44 45 46 47 48 49 50
static int drawer_pos_hidebutton_click (BasePWidget *basep);

static void drawer_pos_pre_convert_hook (BasePWidget *basep);

static BasePPosClass *parent_class;

GtkType
drawer_pos_get_type ()
51
{
52 53 54 55 56 57 58 59 60 61
	static GtkType drawer_pos_type = 0;

	if (!drawer_pos_type) {
		GtkTypeInfo drawer_pos_info = {
			"DrawerPos",
			sizeof (DrawerPos),
			sizeof (DrawerPosClass),
			(GtkClassInitFunc) drawer_pos_class_init,
			(GtkObjectInitFunc) drawer_pos_init,
			NULL, NULL
62 63
		};

64 65
		drawer_pos_type = gtk_type_unique (BASEP_POS_TYPE,
						   &drawer_pos_info);
66 67
	}

68
	return drawer_pos_type;
69 70 71
}

static void
72
drawer_pos_class_init (DrawerPosClass *klass)
73
{
74 75 76 77 78 79 80 81 82 83 84
	/*GtkObjectClass *object_class = GTK_OBJECT_CLASS(klass);*/
	BasePPosClass *pos_class = BASEP_POS_CLASS(klass);

	parent_class = gtk_type_class(BASEP_POS_TYPE);

	/* fill out the virtual funcs */
	pos_class->set_hidebuttons = drawer_pos_set_hidebuttons;
	pos_class->get_applet_orient = drawer_pos_get_applet_orient;
	pos_class->get_size = NULL; /* the default is ok */
	pos_class->get_hide_orient = drawer_pos_get_hide_orient;
	pos_class->get_hide_pos = drawer_pos_get_hide_pos;
85
	pos_class->get_hide_size = drawer_pos_get_hide_size;
86 87
	pos_class->get_pos = drawer_pos_get_pos;
	pos_class->get_menu_pos = drawer_pos_get_menu_pos;
88

89 90 91 92 93 94 95
	pos_class->north_clicked = 
		pos_class->west_clicked = 
		pos_class->south_clicked = 
		pos_class->east_clicked =
		drawer_pos_hidebutton_click;
	pos_class->pre_convert_hook = drawer_pos_pre_convert_hook;
}
96

97 98
static void
drawer_pos_init (DrawerPos *pos) { }
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
static void
drawer_pos_set_hidebuttons (BasePWidget *basep)
{
	switch(DRAWER_POS(basep->pos)->orient) {
	case ORIENT_UP:
		gtk_widget_show(basep->hidebutton_n);
		gtk_widget_hide(basep->hidebutton_e);
		gtk_widget_hide(basep->hidebutton_w);
		gtk_widget_hide(basep->hidebutton_s);
		break;
	case ORIENT_DOWN:
		gtk_widget_hide(basep->hidebutton_n);
		gtk_widget_hide(basep->hidebutton_e);
		gtk_widget_hide(basep->hidebutton_w);
		gtk_widget_show(basep->hidebutton_s);
		break;
	case ORIENT_LEFT:
		gtk_widget_hide(basep->hidebutton_n);
		gtk_widget_hide(basep->hidebutton_e);
		gtk_widget_show(basep->hidebutton_w);
		gtk_widget_hide(basep->hidebutton_s);
		break;
	case ORIENT_RIGHT:
		gtk_widget_hide(basep->hidebutton_n);
		gtk_widget_show(basep->hidebutton_e);
		gtk_widget_hide(basep->hidebutton_w);
		gtk_widget_hide(basep->hidebutton_s);
		break;
	}
129 130
}

131 132
static void
get_widget_pos(GtkWidget *widget, int *x, int *y)
133
{
134 135 136 137 138 139 140 141 142
       if(!widget->window) {
               *x = *y = 0;
               return;
       }
       gdk_window_get_origin (widget->window, x, y);
       if(GTK_WIDGET_NO_WINDOW(widget)) {
               *x += widget->allocation.x;
               *y += widget->allocation.y;
       }
143 144 145 146 147 148 149
}

static PanelOrientType
drawer_pos_get_applet_orient (BasePWidget *basep)
{
	PanelWidget *panel = PANEL_WIDGET (basep->panel);
	PanelOrientation porient = panel->orient;
150
	int x,y;
151

152 153 154 155 156 157 158 159 160 161
	get_widget_pos(GTK_WIDGET(basep),&x,&y);

	if(porient == PANEL_VERTICAL) {
		if(x > (gdk_screen_width()/2))
			return ORIENT_LEFT;
		else
			return ORIENT_RIGHT;
	} else {
		if(y > (gdk_screen_height()/2))
			return ORIENT_UP;
162
		else
163
			return ORIENT_DOWN;
164
	}
165 166
}

167 168
static PanelOrientType
drawer_pos_get_hide_orient (BasePWidget *basep)
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
	DrawerPos *pos = DRAWER_POS (basep->pos);
	PanelWidget *panel = PANEL_WIDGET (basep->panel);

	switch (basep->state) {
	case BASEP_AUTO_HIDDEN:
		switch (pos->orient) {
		case ORIENT_UP: return ORIENT_DOWN;
		case ORIENT_RIGHT: return ORIENT_LEFT;
		case ORIENT_DOWN: return ORIENT_UP;
		case ORIENT_LEFT: return ORIENT_RIGHT;
		}
		g_assert_not_reached ();
		break;
	case BASEP_HIDDEN_LEFT:
		return (panel->orient == PANEL_HORIZONTAL)
			? ORIENT_LEFT : ORIENT_UP;
	case BASEP_HIDDEN_RIGHT:
		return (panel->orient == PANEL_HORIZONTAL)
			? ORIENT_RIGHT : ORIENT_DOWN;
	default:
		g_assert_not_reached ();
		break;
	}
	g_assert_not_reached ();
	return -1;
}
196
	
197 198 199 200 201 202
void
drawer_widget_open_drawer (DrawerWidget *drawer, BasePWidget *parentp)
{
	parentp->drawers_open++;
	/*gtk_widget_show (GTK_WIDGET (drawer));*/
	basep_widget_explicit_show (BASEP_WIDGET (drawer));
203 204
}

205 206
void
drawer_widget_close_drawer (DrawerWidget *drawer, BasePWidget *parentp)
207
{
208
	BasePWidget *basep = BASEP_WIDGET (drawer);
209 210 211 212

	if(GTK_WIDGET(parentp)->window)
		gdk_window_raise(GTK_WIDGET(parentp)->window);

213 214 215 216 217 218 219 220 221
	switch (DRAWER_POS (basep->pos)->orient) {
	case ORIENT_UP:
	case ORIENT_LEFT:
		basep_widget_explicit_hide (basep, BASEP_HIDDEN_RIGHT);
		break;
	case ORIENT_RIGHT:
	case ORIENT_DOWN:
		basep_widget_explicit_hide (basep, BASEP_HIDDEN_LEFT);
		break;
222
	}
223 224 225
	/*gtk_widget_hide (GTK_WIDGET (drawer));*/
	parentp->drawers_open--;
}
226

227 228 229 230 231 232
static int
drawer_pos_hidebutton_click (BasePWidget *basep)
{
	Drawer *drawer = gtk_object_get_data (GTK_OBJECT (basep),
					      DRAWER_PANEL_KEY);
	PanelWidget *panel = PANEL_WIDGET (drawer->button->parent);
233
	BasePWidget *parent = BASEP_WIDGET(panel->panel_parent);
234

235 236 237 238
	drawer_widget_close_drawer (DRAWER_WIDGET (basep), parent);

	return FALSE;
}
239

240 241 242 243
static void
drawer_pos_get_menu_pos (BasePWidget *basep,
			 GtkWidget *widget,
			 GtkRequisition *mreq,
244 245 246
			 int *x, int *y,
			 int wx, int wy,
			 int ww, int wh)
247 248 249 250 251 252 253
{	
	PanelWidget *panel =
		PANEL_WIDGET(basep->panel);

	if(panel->orient==PANEL_VERTICAL) {
		*x = wx + ww;
		*y += wy;
254
	} else {
255 256
		*x += wx;
		*y = wy - mreq->height;
257
	}
258 259
}

260 261
static void
drawer_pos_get_pos(BasePWidget *basep,
262 263
		   int *x, int *y,
		   int width, int height)
264 265 266
{
	PanelWidget *panel = PANEL_WIDGET(basep->panel);
	DrawerPos *pos = DRAWER_POS (basep->pos);
267 268 269 270 271 272 273 274

	/* we are shown but we are hidden, life is full of ironies */
	if (pos->temp_hidden) {
		*x = -width - 1;
		*y = -height - 1;
		return;
	}

275
	if (panel->master_widget &&
276 277 278 279
	    GTK_WIDGET_REALIZED (panel->master_widget) &&
	    /*"allocated" data will be set on each allocation, until then,
	      don't show the actual panel*/
	    gtk_object_get_data(GTK_OBJECT(panel->master_widget),"allocated")) {
280 281 282
		int bx, by, bw, bh;
		int px, py, pw, ph;
		GtkWidget *ppanel; /*parent panel*/
283
		
284
		/*get the parent of the applet*/
285 286
		/*note we know these are not NO_WINDOW widgets, so
		  we don't need to check*/
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
287
		ppanel = panel->master_widget->parent;
288 289 290 291
		bx = panel->master_widget->allocation.x +
			ppanel->allocation.x;
		by = panel->master_widget->allocation.y +
			ppanel->allocation.y;
292
		/*go the the toplevel panel widget*/
293
		while(ppanel->parent) {
294
			ppanel = ppanel->parent;
295 296 297 298
			if(!GTK_WIDGET_NO_WINDOW(ppanel)) {
				bx += ppanel->allocation.x;
				by += ppanel->allocation.y;
			}
299
		}
300 301 302 303 304 305 306 307

		bw = panel->master_widget->allocation.width;
		bh = panel->master_widget->allocation.height;
		px = ppanel->allocation.x;
		py = ppanel->allocation.y;
		pw = ppanel->allocation.width;
		ph = ppanel->allocation.height;

308
		switch(pos->orient) {
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
		case ORIENT_UP:
			*x = bx+(bw-width)/2;
			*y = py - height;
			break;
		case ORIENT_DOWN:
			*x = bx+(bw-width)/2;
			*y = py + ph;
			break;
		case ORIENT_LEFT:
			*x = px - width;
			*y = by+(bh-height)/2;
			break;
		case ORIENT_RIGHT:
			*x = px + pw;
			*y = by+(bh-height)/2;
			break;
		}
326 327 328
	}
}

329
static void
330 331
drawer_pos_get_hide_pos (BasePWidget *basep,
			 PanelOrientType hide_orient,
332 333
			 int *x, int *y,
			 int w, int h)
334
{
335 336 337 338
	if (basep->state != BASEP_SHOWN ||
	    DRAWER_POS (basep->pos)->temp_hidden) {
		*x = -ABS(*x) - 1;
		*y = -ABS(*y) - 1;
339
	}
340 341
}

342 343 344
static void
drawer_pos_get_hide_size (BasePWidget *basep, 
			  PanelOrientType hide_orient,
345
			  int *w, int *h)
346 347 348 349 350 351 352 353 354 355 356 357 358 359
{
	switch (hide_orient) {
	case ORIENT_UP:
	case ORIENT_DOWN:
		*h = 1;
		break;
	case ORIENT_RIGHT:
	case ORIENT_LEFT:
		*w = 1;
		break;
	}
}


360
static void
361
drawer_pos_pre_convert_hook (BasePWidget *basep)
362
{
363 364
	basep->keep_in_screen = FALSE;
	PANEL_WIDGET (basep->panel)->packed = TRUE;
365 366
}

367 368 369 370
void drawer_widget_change_params (DrawerWidget *drawer,
				   PanelOrientType orient,
				  BasePMode mode,
				  BasePState state,
371
				  int sz,
372 373 374 375
				  int hidebuttons_enabled,
				  int hidebutton_pixmap_enabled,
				  PanelBackType back_type,
				  char *back_pixmap,
376 377 378
				  gboolean fit_pixmap_bg,
				  gboolean strech_pixmap_bg,
				  gboolean rotate_pixmap_bg,
379
				  GdkColor *back_color)
380
{
381 382
	PanelOrientation porient;
	DrawerPos *pos = DRAWER_POS (BASEP_WIDGET (drawer)->pos);
383

384
	switch (orient) {
385 386
	case ORIENT_UP:
	case ORIENT_DOWN:
387
		porient = PANEL_VERTICAL;
388 389 390
		break;
	case ORIENT_LEFT:
	case ORIENT_RIGHT:
391 392
	default:
		porient = PANEL_HORIZONTAL;
393
		break;
394 395
	}

396 397
	if (PANEL_WIDGET (BASEP_WIDGET (drawer)->panel)->orient != porient)
		BASEP_WIDGET (drawer)->request_cube = TRUE;
398

399 400 401 402 403 404 405 406 407 408 409 410
	if (state != BASEP_WIDGET (drawer)->state ||
	    pos->orient != orient) {
		if (state == BASEP_HIDDEN_LEFT &&
		    (orient == ORIENT_LEFT ||
		     orient == ORIENT_UP))
			state = BASEP_HIDDEN_RIGHT;
		else if (state == BASEP_HIDDEN_RIGHT &&
			 (orient == ORIENT_RIGHT ||
			  orient == ORIENT_DOWN))
			 state = BASEP_HIDDEN_LEFT;
	}

411 412 413 414 415 416 417 418
	if (pos->orient != orient) {
		pos->orient = orient;
#if 0
		gtk_signal_emit (GTK_OBJECT (drawer),
				 drawer_pos_signals[ORIENT_CHANGE_SIGNAL],
				 orient);
#endif
	}
419

420 421 422 423 424
	basep_widget_change_params (BASEP_WIDGET (drawer),
				    porient, sz, mode, state,
				    hidebuttons_enabled,
				    hidebutton_pixmap_enabled,
				    back_type, back_pixmap,
425 426 427
				    fit_pixmap_bg, strech_pixmap_bg,
				    rotate_pixmap_bg,
				    back_color);
428
				    
429 430 431
}

void
432 433
drawer_widget_change_orient (DrawerWidget *drawer,
			     PanelOrientType orient)
434
{
435 436 437 438 439 440 441 442 443 444 445 446 447
	DrawerPos *pos = DRAWER_POS (drawer->pos);
	if (pos->orient != orient) {
		BasePWidget *basep = BASEP_WIDGET (drawer);
		PanelWidget *panel = PANEL_WIDGET (basep->panel);
		drawer_widget_change_params (drawer, orient,
					     basep->mode,
					     basep->state,
					     panel->sz,
					     basep->hidebuttons_enabled,
					     basep->hidebutton_pixmaps_enabled,
					     panel->back_type,
					     panel->back_pixmap,
					     panel->fit_pixmap_bg,
448 449
					     panel->strech_pixmap_bg,
					     panel->rotate_pixmap_bg,
450
					     &panel->back_color);
451 452 453
	}
}

454
GtkWidget *
455
drawer_widget_new (PanelOrientType orient,
456 457
		   BasePMode mode,
		   BasePState state,
458
		   int sz,
459 460
		   int hidebuttons_enabled,
		   int hidebutton_pixmap_enabled,
461 462
		   PanelBackType back_type,
		   char *back_pixmap,
463 464 465
		   gboolean fit_pixmap_bg,
		   gboolean strech_pixmap_bg,
		   gboolean rotate_pixmap_bg,
466
		   GdkColor *back_color)
467 468
{
	DrawerWidget *drawer;
469
	DrawerPos *pos;
470 471
	PanelOrientation porient;

472 473 474
	drawer = gtk_type_new (DRAWER_WIDGET_TYPE);
	drawer->pos = gtk_type_new (DRAWER_POS_TYPE);
	pos = DRAWER_POS (drawer->pos);
475
	pos->orient = orient;
476

477
	switch (orient) {
478 479 480 481 482 483 484
	case ORIENT_UP:
	case ORIENT_DOWN:
		porient = PANEL_VERTICAL;
		break;
	default:
		porient = PANEL_HORIZONTAL;
		break;
485 486
	}

487 488 489 490 491 492 493 494 495
	basep_widget_construct (BASEP_WIDGET (drawer),
				TRUE, TRUE,
				porient,
				sz, mode, state,
				hidebuttons_enabled,
				hidebutton_pixmap_enabled,
				back_type,
				back_pixmap,
				fit_pixmap_bg,
496 497
				strech_pixmap_bg,
				rotate_pixmap_bg,
498 499 500
				back_color);

	return GTK_WIDGET (drawer);
501 502
}

503
#if 0
504 505 506
void
drawer_widget_restore_state(DrawerWidget *drawer)
{
507
	DRAWER_POS (BASEP_WIDGET (drawer)->pos)->temp_hidden = FALSE;
508
	gtk_widget_queue_resize(GTK_WIDGET(drawer));
509
	gtk_widget_show(GTK_WIDGET(drawer));
510
}
511
#endif