panel-widget.c 49.2 KB
Newer Older
1 2 3 4 5 6
/* Gnome panel: panel widget
 * (C) 1997 the Free Software Foundation
 *
 * Authors:  George Lebl
 */
#include <limits.h>
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
7
#include <math.h>
8
#include <config.h>
9
#include <gtk/gtk.h>
10
#include <gnome.h>
11
#include "panel-widget.h"
12
#include "button-widget.h"
13
#include "panel-util.h"
14
#include "gdkextra.h"
15

16 17
GList *panels=NULL; /*other panels we might want to move the applet to*/

18 19
#define DEBUG 1

20 21
/*there  can universally be only one applet being dragged since we assume
we only have one mouse :) */
22
int panel_applet_in_drag = FALSE;
23

24 25
static void panel_widget_class_init	(PanelWidgetClass *klass);
static void panel_widget_init		(PanelWidget      *panel_widget);
26 27 28 29 30 31 32 33 34 35 36 37 38
static int  panel_try_to_set_pixmap     (PanelWidget      *panel,
					 char             *pixmap);
static void panel_resize_pixmap         (PanelWidget      *panel);
static void panel_try_to_set_back_color (PanelWidget      *panel,
					 GdkColor         *color);
static void panel_widget_size_request   (GtkWidget        *widget,
					 GtkRequisition   *requisition);
static void panel_widget_size_allocate  (GtkWidget        *widget,
					 GtkAllocation    *allocation);
static void panel_widget_cadd		(GtkContainer     *container,
					 GtkWidget        *widget);
static void panel_widget_cremove	(GtkContainer     *container,
					 GtkWidget        *widget);
39 40 41 42
static int  panel_widget_expose		(GtkWidget        *widget,
					 GdkEventExpose   *event);
static void panel_widget_draw		(GtkWidget        *widget,
					 GdkRectangle     *area);
43

44

45
GdkCursor *fleur_cursor;
46

47
/*global settings*/
48 49 50 51 52 53 54
int pw_explicit_step = 50;
int pw_drawer_step = 20;
int pw_auto_step = 10;
int pw_minimized_size = 6;
int pw_minimize_delay = 300;
int pw_disable_animations = FALSE;
PanelMovementType pw_movement_type = PANEL_SWITCH_MOVE;
55
int pw_applet_padding = 3;
56

57
static char *image_drop_types[] = {"url:ALL", "application/x-color"};
58

59 60 61 62 63
#define APPLET_EVENT_MASK (GDK_BUTTON_PRESS_MASK |		\
			   GDK_BUTTON_RELEASE_MASK |		\
			   GDK_POINTER_MOTION_MASK |		\
			   GDK_POINTER_MOTION_HINT_MASK)

64 65 66
typedef void (*OrientSignal) (GtkObject * object,
			      PanelOrientation orient,
			      gpointer data);
67

68 69 70 71 72
typedef void (*BackSignal) (GtkObject * object,
			    PanelBackType type,
			    char *pixmap,
			    GdkColor *color,
			    gpointer data);
73

74 75 76
typedef void (*AppletSignal) (GtkObject * object,
			      GtkWidget * applet,
			      gpointer data);
77

78 79 80
/************************
 debugging
 ************************/
81
/*static void
82 83 84 85 86 87 88 89 90
debug_dump_panel_list(PanelWidget *panel)
{
	GList *list;
	puts("\nDUMP START\n");
	for(list = panel->applet_list;list!=NULL;list=g_list_next(list)) {
		AppletData *ad = list->data;
		printf("pos: %d cells: %d\n",ad->pos,ad->cells);
	}
	puts("\nDUMP END\n");
91
}*/
92

93 94 95
/************************
 convenience functions
 ************************/
96
static int
97 98 99 100 101
applet_data_compare(AppletData *ad1, AppletData *ad2)
{
	return ad1->pos - ad2->pos;
}

102 103 104
/************************
 widget core
 ************************/
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
guint
panel_widget_get_type ()
{
	static guint panel_widget_type = 0;

	if (!panel_widget_type) {
		GtkTypeInfo panel_widget_info = {
			"PanelWidget",
			sizeof (PanelWidget),
			sizeof (PanelWidgetClass),
			(GtkClassInitFunc) panel_widget_class_init,
			(GtkObjectInitFunc) panel_widget_init,
			(GtkArgSetFunc) NULL,
			(GtkArgGetFunc) NULL,
		};

122 123 124
		panel_widget_type =
			gtk_type_unique (gtk_fixed_get_type (),
					 &panel_widget_info);
125 126 127 128 129
	}

	return panel_widget_type;
}

130 131
enum {
	ORIENT_CHANGE_SIGNAL,
132
	APPLET_MOVE_SIGNAL,
133 134
	APPLET_ADDED_SIGNAL,
	APPLET_REMOVED_SIGNAL,
135
	BACK_CHANGE_SIGNAL,
136 137 138
	LAST_SIGNAL
};

139
static int panel_widget_signals[LAST_SIGNAL] = {0,0,0,0,0};
140 141
static GtkFixedClass *parent_class = NULL;

142 143

static void
144 145 146 147
marshal_signal_orient (GtkObject * object,
		       GtkSignalFunc func,
		       gpointer func_data,
		       GtkArg * args)
148
{
149
	OrientSignal rfunc;
150

151
	rfunc = (OrientSignal) func;
152

153 154
	(*rfunc) (object, GTK_VALUE_ENUM (args[0]),
		  func_data);
155 156
}

157
static void
158 159 160 161
marshal_signal_applet (GtkObject * object,
		       GtkSignalFunc func,
		       gpointer func_data,
		       GtkArg * args)
162
{
163
	AppletSignal rfunc;
164

165
	rfunc = (AppletSignal) func;
166

167 168
	(*rfunc) (object, GTK_VALUE_POINTER (args[0]),
		  func_data);
169 170
}

171
static void
172 173 174 175
marshal_signal_back (GtkObject * object,
		     GtkSignalFunc func,
		     gpointer func_data,
		     GtkArg * args)
176
{
177
	BackSignal rfunc;
178

179
	rfunc = (BackSignal) func;
180 181 182 183 184 185 186

	(*rfunc) (object, GTK_VALUE_ENUM (args[0]),
		  GTK_VALUE_POINTER (args[1]),
		  GTK_VALUE_POINTER (args[2]),
		  func_data);
}

187 188 189
static void
panel_widget_class_init (PanelWidgetClass *class)
{
190 191
	GtkObjectClass *object_class = (GtkObjectClass*) class;
	GtkWidgetClass *widget_class = (GtkWidgetClass*) class;
192 193 194
	GtkContainerClass *container_class = (GtkContainerClass*) class;
	
	parent_class = gtk_type_class (gtk_fixed_get_type ());
195 196 197

	panel_widget_signals[ORIENT_CHANGE_SIGNAL] =
		gtk_signal_new("orient_change",
198
			       GTK_RUN_LAST,
199 200 201
			       object_class->type,
			       GTK_SIGNAL_OFFSET(PanelWidgetClass,
			       			 orient_change),
202
			       marshal_signal_orient,
203 204 205
			       GTK_TYPE_NONE,
			       1,
			       GTK_TYPE_ENUM);
206 207
	panel_widget_signals[APPLET_MOVE_SIGNAL] =
		gtk_signal_new("applet_move",
208
			       GTK_RUN_LAST,
209 210
			       object_class->type,
			       GTK_SIGNAL_OFFSET(PanelWidgetClass,
211
			       			 applet_move),
212
			       marshal_signal_applet,
213 214
			       GTK_TYPE_NONE,
			       1,
215
			       GTK_TYPE_POINTER);
216 217 218 219 220 221
	panel_widget_signals[APPLET_ADDED_SIGNAL] =
		gtk_signal_new("applet_added",
			       GTK_RUN_LAST,
			       object_class->type,
			       GTK_SIGNAL_OFFSET(PanelWidgetClass,
			       			 applet_added),
222
			       marshal_signal_applet,
223 224 225 226 227 228 229 230 231
			       GTK_TYPE_NONE,
			       1,
			       GTK_TYPE_POINTER);
	panel_widget_signals[APPLET_REMOVED_SIGNAL] =
		gtk_signal_new("applet_removed",
			       GTK_RUN_LAST,
			       object_class->type,
			       GTK_SIGNAL_OFFSET(PanelWidgetClass,
			       			 applet_removed),
232
			       marshal_signal_applet,
233
			       GTK_TYPE_NONE,
234 235
			       1,
			       GTK_TYPE_POINTER);
236 237 238 239 240 241
	panel_widget_signals[BACK_CHANGE_SIGNAL] =
		gtk_signal_new("back_change",
			       GTK_RUN_LAST,
			       object_class->type,
			       GTK_SIGNAL_OFFSET(PanelWidgetClass,
			       			 back_change),
242
			       marshal_signal_back,
243 244 245 246 247
			       GTK_TYPE_NONE,
			       3,
			       GTK_TYPE_ENUM,
			       GTK_TYPE_POINTER,
			       GTK_TYPE_POINTER);
248 249 250 251
	gtk_object_class_add_signals(object_class,panel_widget_signals,
				     LAST_SIGNAL);

	class->orient_change = NULL;
252
	class->applet_move = NULL;
253 254
	class->applet_added = NULL;
	class->applet_removed = NULL;
255
	class->back_change = NULL;
256 257 258
	
	widget_class->size_request = panel_widget_size_request;
	widget_class->size_allocate = panel_widget_size_allocate;
259 260
	widget_class->expose_event = panel_widget_expose;
	widget_class->draw = panel_widget_draw;
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

	container_class->add = panel_widget_cadd;
	container_class->remove = panel_widget_cremove;
}

static void
remove_panel_from_forbidden(PanelWidget *panel, PanelWidget *r)
{
	GList *list;
	if(!panel->master_widget)
		return;

	list = gtk_object_get_data(GTK_OBJECT(panel->master_widget),
				   PANEL_APPLET_FORBIDDEN_PANELS);
	if(list) {
		list = g_list_remove(list,r);
		gtk_object_set_data(GTK_OBJECT(panel->master_widget),
				    PANEL_APPLET_FORBIDDEN_PANELS,
				    list);
	}
281
	panel = PANEL_WIDGET(panel->master_widget->parent);
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	if(panel)
		remove_panel_from_forbidden(panel, r);
}

static void
add_panel_to_forbidden(PanelWidget *panel, PanelWidget *r)
{
	GList *list;
	if(!panel->master_widget)
		return;

	list = gtk_object_get_data(GTK_OBJECT(panel->master_widget),
				   PANEL_APPLET_FORBIDDEN_PANELS);
	if(g_list_find(list,r)==NULL) {
		list = g_list_prepend(list,r);

		gtk_object_set_data(GTK_OBJECT(panel->master_widget),
				    PANEL_APPLET_FORBIDDEN_PANELS,
				    list);
	}

303
	panel = PANEL_WIDGET(panel->master_widget->parent);
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
	if(panel)
		add_panel_to_forbidden(panel, r);
}

static void
run_up_forbidden(PanelWidget *panel,
		 void (*runfunc)(PanelWidget *,PanelWidget *))
{
	GList *list;
	for(list = panel->applet_list;list!=NULL;list = g_list_next(list)) {
		AppletData *ad = list->data;
		PanelWidget *p =
			gtk_object_get_data(GTK_OBJECT(ad->applet),
					    PANEL_APPLET_ASSOC_PANEL_KEY);
		if(p)
			run_up_forbidden(p,runfunc);
	}
	(*runfunc)(panel,panel);
}


static void
panel_widget_cadd(GtkContainer *container, GtkWidget *widget)
{
	PanelWidget *p;

	g_return_if_fail (container != NULL);
	g_return_if_fail (IS_PANEL_WIDGET (container));
	g_return_if_fail (widget != NULL);

	panel_widget_add(PANEL_WIDGET(container),widget,0);
	p = gtk_object_get_data(GTK_OBJECT(widget),
				PANEL_APPLET_ASSOC_PANEL_KEY);
	if(p)
		run_up_forbidden(p,add_panel_to_forbidden);
}

static void
panel_widget_cremove(GtkContainer *container, GtkWidget *widget)
{
	AppletData *ad;
	PanelWidget *p;
	PanelWidget *panel;

	g_return_if_fail (container != NULL);
	g_return_if_fail (IS_PANEL_WIDGET (container));
	g_return_if_fail (widget != NULL);
351
	
352 353 354
	panel = PANEL_WIDGET (container);
	
	ad = gtk_object_get_data(GTK_OBJECT(widget), PANEL_APPLET_DATA);
355 356
	p = gtk_object_get_data(GTK_OBJECT(widget),
				PANEL_APPLET_ASSOC_PANEL_KEY);
357

358
	gtk_widget_ref(widget);
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
	if (GTK_CONTAINER_CLASS (parent_class)->remove)
		(* GTK_CONTAINER_CLASS (parent_class)->remove) (container,
								widget);
	if(ad) {
		PanelWidget *panel = PANEL_WIDGET (container);

		panel->applet_list = g_list_remove (panel->applet_list, ad);
		panel->no_window_applet_list =
			g_list_remove (panel->no_window_applet_list, ad);
	}

	if(p)
		run_up_forbidden(p,remove_panel_from_forbidden);

	gtk_signal_emit(GTK_OBJECT(container),
			panel_widget_signals[APPLET_REMOVED_SIGNAL],
			widget);
376
	gtk_widget_unref(widget);
377 378
}

379

380
/*get the number of applets*/
381
int
382 383
panel_widget_get_applet_count(PanelWidget *panel)
{
384
	return g_list_length(GTK_FIXED(panel)->children);
385 386
}

387 388 389
/*get the list item of the data on the position pos*/
static GList *
get_applet_list_pos(PanelWidget *panel, int pos)
390 391
{
	GList *list;
392
	
393 394
	for(list=panel->applet_list;list!=NULL;list=g_list_next(list)) {
		AppletData *ad = list->data;
395 396 397 398 399
		if(ad->pos <= pos) {
		       if(ad->pos+ad->cells > pos)
			       return list;
		} else
			return NULL;
400 401 402 403
	}
	return NULL;
}

404 405 406 407 408 409 410 411 412
AppletData *
get_applet_data_pos(PanelWidget *panel, int pos)
{
	GList *list = get_applet_list_pos(panel,pos);
	if(list)
		return list->data;
	return NULL;
}

413
/*tells us if an applet is "stuck" on the right side*/
414
int
415 416 417 418
panel_widget_is_applet_stuck(PanelWidget *panel, GtkWidget *applet)
{
	AppletData *ad;
	GList *list;
419
	int i;
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

	g_return_val_if_fail(panel!=NULL,FALSE);
	g_return_val_if_fail(applet!=NULL,FALSE);

	ad = gtk_object_get_data(GTK_OBJECT(applet), PANEL_APPLET_DATA);

	g_return_val_if_fail(ad!=NULL,FALSE);

	list = g_list_find(panel->applet_list,ad);

	g_return_val_if_fail(list!=NULL,FALSE);
	
	do {
		i=ad->pos+ad->cells;
		if(i==panel->size)
			return TRUE;
		list = g_list_next(list);
		if(!list)
			break;
		ad = list->data;
	} while(ad->pos==i);
	return FALSE;
}

444
static void
445
panel_widget_switch_applet_right(PanelWidget *panel, GList *list)
446
{
447 448 449
	AppletData *ad = list->data;
	AppletData *nad;
	GList *nlist;
450

451 452 453 454 455
	nlist = g_list_next(list);
	if(nlist)
		nad = nlist->data;
	if(!nlist || nad->pos > ad->pos+ad->cells) {
		ad->pos++;
456
		gtk_widget_queue_resize(GTK_WIDGET(panel));
457
		return;
458 459 460 461
	}
	nad->pos = ad->pos;
	ad->pos = nad->pos+nad->cells;
	panel->applet_list = my_g_list_swap_prev(panel->applet_list,nlist);
462

463 464 465 466 467 468 469
	gtk_signal_emit(GTK_OBJECT(panel),
			panel_widget_signals[APPLET_MOVE_SIGNAL],
			ad->applet);
	gtk_signal_emit(GTK_OBJECT(panel),
			panel_widget_signals[APPLET_MOVE_SIGNAL],
			nad->applet);
	gtk_widget_queue_resize(GTK_WIDGET(panel));
470
}
471

472 473 474 475 476 477
static void
panel_widget_switch_applet_left(PanelWidget *panel, GList *list)
{
	AppletData *ad = list->data;
	AppletData *pad;
	GList *nlist;
478

479 480 481 482 483
	nlist = g_list_previous(list);
	if(nlist)
		pad = nlist->data;
	if(!nlist || pad->pos+pad->cells < ad->pos) {
		ad->pos--;
484
		gtk_widget_queue_resize(GTK_WIDGET(panel));
485 486 487 488 489
		return;
	}
	ad->pos = pad->pos;
	pad->pos = ad->pos+ad->cells;
	panel->applet_list = my_g_list_swap_next(panel->applet_list,nlist);
490

491 492 493 494 495 496 497
	gtk_signal_emit(GTK_OBJECT(panel),
			panel_widget_signals[APPLET_MOVE_SIGNAL],
			ad->applet);
	gtk_signal_emit(GTK_OBJECT(panel),
			panel_widget_signals[APPLET_MOVE_SIGNAL],
			pad->applet);
	gtk_widget_queue_resize(GTK_WIDGET(panel));
498
}
499

500 501 502 503 504 505 506 507 508 509 510
static int
panel_widget_get_right_switch_pos(PanelWidget *panel, GList *list)
{
	AppletData *ad = list->data;
	AppletData *nad;
	list = g_list_next(list);
	if(list)
		nad = list->data;
	if(!list || nad->pos > ad->pos+ad->cells)
		return ad->pos+1;
	return nad->pos+nad->cells-ad->cells;
511 512
}

513 514
static int
panel_widget_get_left_switch_pos(PanelWidget *panel, GList *list)
515
{
516 517 518 519 520 521 522 523 524
	AppletData *ad = list->data;
	AppletData *pad;
	list = g_list_previous(list);
	if(list)
		pad = list->data;
	if(!list || pad->pos+pad->cells < ad->pos)
		return ad->pos-1;
	return pad->pos;
}
525

526

527 528 529 530 531 532
static void
panel_widget_switch_move(PanelWidget *panel, AppletData *ad, int moveby)
{
	int finalpos;
	int pos;
	GList *list;
533

534 535
	g_return_if_fail(ad!=NULL);
	g_return_if_fail(panel!=NULL);
536

537 538
	if(moveby==0)
		return;
539

540 541
	list = g_list_find(panel->applet_list,ad);
	g_return_if_fail(list!=NULL);
542

543 544 545 546
	finalpos = ad->pos+moveby;

	if(finalpos >= panel->size)
		finalpos = panel->size-1;
547 548
	else if(finalpos < pw_applet_padding)
		finalpos = pw_applet_padding;
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

	while((ad->pos+ad->cells-1)<finalpos) {
		pos = panel_widget_get_right_switch_pos(panel,list);
		if(pos > finalpos || pos+ad->cells-1 >= panel->size)
			return;
		panel_widget_switch_applet_right(panel,list);
	}
	while(ad->pos>finalpos) {
		if((panel_widget_get_left_switch_pos(panel,list)+ad->cells-1) < 
		   finalpos)
			return;
		panel_widget_switch_applet_left(panel,list);
	}

	return;
}

/*this is a special function and may fail if called improperly, it works
only under special circumstance when we know there is nothing from
old_size to panel->size*/
static void
panel_widget_right_stick(PanelWidget *panel,int old_size)
{
	int i,pos;
	GList *list,*prev;
	AppletData *ad;
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
	if(old_size>=panel->size ||
	   panel->packed)
	   	return;
	
	list = get_applet_list_pos(panel,old_size-1);

	if(!list)
		return;
	
	pos = panel->size-1;

	ad = list->data;
	do { 
		i = ad->pos;
		ad->pos = pos--;
		ad->cells = 1;
		prev = list;
		list = g_list_previous(list);
		if(!list)
			break;
		ad = list->data;
	} while(ad->pos+ad->cells == i);

	for(list = prev;list!=NULL;list=g_list_next(list))
600 601 602
		gtk_signal_emit(GTK_OBJECT(panel),
				panel_widget_signals[APPLET_MOVE_SIGNAL],
				((AppletData *)list->data)->applet);
603 604
}

Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
605
static void
606
panel_widget_size_request(GtkWidget *widget, GtkRequisition *requisition)
607
{
608 609 610 611
	PanelWidget *panel = PANEL_WIDGET(widget);
	GList *list;
	requisition->height = 0;
	requisition->width = 0;
612 613 614 615 616
	if(panel->orient == PANEL_HORIZONTAL)
		requisition->width = pw_applet_padding;
	else
		requisition->height = pw_applet_padding;

617 618 619 620 621 622 623 624
	for(list = panel->applet_list; list!=NULL; list = g_list_next(list)) {
		AppletData *ad = list->data;
		gtk_widget_size_request (ad->applet, &ad->applet->requisition);
		if(panel->orient == PANEL_HORIZONTAL) {
			if(requisition->height <
			   ad->applet->requisition.height) {
				requisition->height =
					ad->applet->requisition.height;
625
			}
626 627
			if(panel->packed)
				requisition->width +=
628 629
					ad->applet->requisition.width +
					pw_applet_padding;
630 631 632 633 634 635 636 637
		} else {
			if(requisition->width <
			   ad->applet->requisition.width) {
				requisition->width =
					ad->applet->requisition.width;
			}
			if(panel->packed)
				requisition->height +=
638 639
					ad->applet->requisition.height +
					pw_applet_padding;
640
		}
641
	}
642 643 644 645 646
	if(!panel->packed) {
		if(panel->orient == PANEL_HORIZONTAL) {
			requisition->width = panel->size;
		} else {
			requisition->height = panel->size;
647
		}
648 649 650
	}
}

651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
void
panel_widget_draw_all(PanelWidget *panel)
{
	GList *li;
	GtkWidget *widget;
	GdkGC *gc;
	
	g_return_if_fail(panel != NULL);
	if(!panel->pixmap) /*it will get drawn soon anyhow*/
		return;

	widget = GTK_WIDGET(panel);
	
	gc = widget->style->bg_gc[GTK_WIDGET_STATE(widget)];

	if(widget->style->bg_pixmap[GTK_WIDGET_STATE(widget)]) {
		gdk_gc_set_fill(gc, GDK_TILED);
		gdk_gc_set_tile(gc, widget->style->bg_pixmap[GTK_WIDGET_STATE(widget)]);
	}
				
	gdk_draw_rectangle(panel->pixmap,gc, TRUE, 0,0,-1,-1);

	gdk_gc_set_fill(gc, GDK_SOLID);
	gdk_gc_set_tile(gc, NULL);
	
676
	for(li = panel->applet_list; li != NULL;
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
	    li = g_list_next(li)) {
		AppletData *ad = li->data;
		if(IS_BUTTON_WIDGET(ad->applet))
			button_widget_draw(BUTTON_WIDGET(ad->applet),
					   panel->pixmap);
	}
	if(GTK_WIDGET_DRAWABLE(widget))
		gdk_draw_pixmap(widget->window,
				widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
				panel->pixmap,
				0,0,0,0,-1,-1);
}

void
panel_widget_draw_icon(PanelWidget *panel, ButtonWidget *button)
{
	GtkWidget *widget;
	GtkWidget *applet;
	GdkGC *gc;
	
	g_return_if_fail(panel != NULL);
	g_return_if_fail(button != NULL);
	if(!panel->pixmap) /*it will get drawn soon anyhow*/
		return;

	widget = GTK_WIDGET(panel);
	applet = GTK_WIDGET(button);
	
	gc = widget->style->bg_gc[GTK_WIDGET_STATE(widget)];

	if(widget->style->bg_pixmap[GTK_WIDGET_STATE(widget)]) {
		gdk_gc_set_fill(gc, GDK_TILED);
		gdk_gc_set_tile(gc, widget->style->bg_pixmap[GTK_WIDGET_STATE(widget)]);
	}
				
	gdk_draw_rectangle(panel->pixmap,gc, TRUE,
			   applet->allocation.x,applet->allocation.y,
			   applet->allocation.width,applet->allocation.height);

	gdk_gc_set_fill(gc, GDK_SOLID);
	gdk_gc_set_tile(gc, NULL);

	button_widget_draw(button, panel->pixmap);

	if(GTK_WIDGET_DRAWABLE(widget))
		gdk_draw_pixmap(widget->window,
				widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
				panel->pixmap,
				applet->allocation.x,applet->allocation.y,
				applet->allocation.x,applet->allocation.y,
				applet->allocation.width,applet->allocation.height);

}

static void
panel_widget_draw(GtkWidget *widget, GdkRectangle *area)
{
	GList *li;
	PanelWidget *panel = PANEL_WIDGET(widget);
	if(!GTK_WIDGET_DRAWABLE(widget))
		return;
	if(panel->pixmap) {
		gdk_draw_pixmap(widget->window,
				widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
				panel->pixmap,
				area->x, area->y,
				area->x, area->y,
				area->width, area->height);
	}
	for(li = panel->applet_list;
	    li != NULL;
	    li = g_list_next(li)) {
		AppletData *ad = li->data;
		GdkRectangle ch_area;
		if(!IS_BUTTON_WIDGET(ad->applet) &&
		   gtk_widget_intersect(ad->applet, area, &ch_area))
			gtk_widget_draw(ad->applet, &ch_area);
	}
}

static int
panel_widget_expose(GtkWidget *widget, GdkEventExpose *event)
{
	GList *li;
	PanelWidget *panel = PANEL_WIDGET(widget);
	if(!GTK_WIDGET_DRAWABLE(widget))
763
		return FALSE;
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
	if(panel->pixmap) {
		gdk_draw_pixmap(widget->window,
				widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
				panel->pixmap,
				event->area.x, event->area.y,
				event->area.x, event->area.y,
				event->area.width, event->area.height);
	}
	for(li = panel->applet_list;
	    li != NULL;
	    li = g_list_next(li)) {
		AppletData *ad = li->data;
		GdkEventExpose ch_event = *event;

		if(!IS_BUTTON_WIDGET(ad->applet) &&
		   GTK_WIDGET_NO_WINDOW (ad->applet) &&
		   gtk_widget_intersect (ad->applet, &event->area, 
					 &ch_event.area))
			gtk_widget_event (ad->applet, (GdkEvent*) &ch_event);
	}

	return FALSE;
}

788
static void
789
panel_widget_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
790
{
791
	PanelWidget *panel = PANEL_WIDGET(widget);
792
	GList *list;
793 794 795
	GList *send_move = NULL;
	int i;
	int old_size = panel->size;
796
	int old_thick = panel->thick;
797
	
798 799 800 801 802 803 804
	widget->allocation = *allocation;
	if (GTK_WIDGET_REALIZED (widget))
		gdk_window_move_resize (widget->window,
					allocation->x, 
					allocation->y,
					allocation->width, 
					allocation->height);
805

806 807 808 809 810
	if(panel->orient == PANEL_HORIZONTAL)
		panel->size = allocation->width;
	else
		panel->size = allocation->height;
	if(old_size<panel->size)
811
		panel_widget_right_stick(panel,old_size);
812 813

	if(panel->packed) {
814
		i = pw_applet_padding;
815
		for(list = panel->applet_list;
816
		    list!=NULL;
817
		    list = g_list_next(list)) {
818
			AppletData *ad = list->data;
819 820 821 822 823 824 825
			GtkAllocation challoc;
			
			if(i != ad->pos) {
				ad->pos = i;
				send_move = g_list_prepend(send_move,ad);
			}
			if(panel->orient == PANEL_HORIZONTAL) {
826 827
				ad->cells = ad->applet->requisition.width +
					pw_applet_padding;
828 829 830
				challoc.x = ad->pos;
				challoc.y = (allocation->height - ad->applet->requisition.height) / 2;
			} else {
831 832
				ad->cells = ad->applet->requisition.height +
					pw_applet_padding;
833 834 835 836 837 838 839 840 841
				challoc.x = (allocation->width - ad->applet->requisition.width) / 2;
				challoc.y = ad->pos;
			}
			challoc.width = ad->applet->requisition.width;
			challoc.height = ad->applet->requisition.height;
			gtk_widget_size_allocate(ad->applet,&challoc);
			i += ad->cells;
		}
	} else { /*not packed*/
842
		i = panel->size;
843 844 845 846 847 848
		for(list = g_list_last(panel->applet_list);
		    list!=NULL;
		    list = g_list_previous(list)) {
			AppletData *ad = list->data;
			
			if(panel->orient == PANEL_HORIZONTAL)
849 850
				ad->cells = ad->applet->requisition.width +
					pw_applet_padding;
851
			else
852 853
				ad->cells = ad->applet->requisition.height +
					pw_applet_padding;
854 855 856 857 858 859
			if(ad->pos+ad->cells > i) {
				ad->pos = i - ad->cells;
				send_move = g_list_prepend(send_move,ad);
			}
			i = ad->pos;
		}
860 861
		if(i<pw_applet_padding) {
			i = pw_applet_padding;
862 863 864 865 866 867 868 869 870 871 872 873 874
			for(list = panel->applet_list;
			    list!=NULL;
			    list = g_list_next(list)) {
				AppletData *ad = list->data;
				
				if(ad->pos < i) {
					ad->pos = i;
					if(!g_list_find(send_move,ad))
						send_move = g_list_prepend(send_move,ad);
				}
				
				i = ad->pos + ad->cells;
			}
875
		}
876

877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
		for(list = panel->applet_list;
		    list!=NULL;
		    list = g_list_next(list)) {
			AppletData *ad = list->data;
			GtkAllocation challoc;
			if(panel->orient == PANEL_HORIZONTAL) {
				challoc.x = ad->pos;
				challoc.y = (allocation->height - ad->applet->requisition.height) / 2;
			} else {
				challoc.x = (allocation->width - ad->applet->requisition.width) / 2;
				challoc.y = ad->pos;
			}
			challoc.width = ad->applet->requisition.width;
			challoc.height = ad->applet->requisition.height;
			gtk_widget_size_allocate(ad->applet,&challoc);
		}
893
	}
894 895 896 897 898
	if(panel->orient == PANEL_HORIZONTAL)
		panel->thick = allocation->height;
	else
		panel->thick = allocation->width;

899 900 901
	if(old_thick != panel->thick &&
	   panel->fit_pixmap_bg &&
	   panel->back_type == PANEL_BACK_PIXMAP)
902
		panel_resize_pixmap(panel);
903

904
	
905 906
	while(send_move) {
		AppletData *ad = send_move->data;
907 908 909
		gtk_signal_emit(GTK_OBJECT(panel),
				panel_widget_signals[APPLET_MOVE_SIGNAL],
				ad->applet);
910
		send_move = my_g_list_pop_first(send_move);
911
	}
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
	
	if (GTK_WIDGET_REALIZED (widget)) {
		if(!panel->pixmap ||
		   allocation->width != panel->pixw ||
		   allocation->height != panel->pixh) {
			if(panel->pixmap)
				gdk_pixmap_unref(panel->pixmap);
			panel->pixw = allocation->width;
			panel->pixh = allocation->height;
			panel->pixmap = gdk_pixmap_new(widget->window,
						       panel->pixw,
						       panel->pixh,
						       gtk_widget_get_visual(widget)->depth);
		}
		panel_widget_draw_all(panel);
	}
928
}
929

930 931
int
panel_widget_is_cursor(PanelWidget *panel, int overlap)
932
{
933 934 935 936 937
	int x,y;
	int w,h;
	GtkWidget *widget = panel->drop_widget;
	
	if(!w || !GTK_WIDGET_VISIBLE(widget))
938
		return FALSE;
939 940 941 942 943 944 945 946 947

	gtk_widget_get_pointer(widget, &x, &y);
	gdk_window_get_size(widget->window, &w, &h);

	if((x+overlap)>=0 &&
	   (x-overlap)<=w &&
	   (y+overlap)>=0 &&
	   (y-overlap)<=h)
		return TRUE;
948
	return FALSE;
949
}
950

951
static void
952 953 954
panel_widget_dnd_dropped_filename (GtkWidget *widget,
				   GdkEventDropDataAvailable *event,
				   PanelWidget *panel)
955
{
956
	if (panel_try_to_set_pixmap (panel, event->data)) {
957 958 959 960
		if (panel->back_pixmap)
			g_free (panel->back_pixmap);
		panel->back_pixmap = g_strdup (event->data);
		gtk_widget_queue_draw (widget);
961
		panel->back_type = PANEL_BACK_PIXMAP;
962 963 964 965 966
		gtk_signal_emit(GTK_OBJECT(panel),
				panel_widget_signals[BACK_CHANGE_SIGNAL],
				panel->back_type,
				panel->back_pixmap,
				&panel->back_color);
967 968 969
	}
}

970 971 972 973 974 975 976 977
static void
panel_try_to_set_default_back(PanelWidget *panel)
{
	GtkStyle *ns;

	ns = gtk_style_new();

	gtk_style_ref(ns);
978
	gtk_widget_set_style(GTK_WIDGET(panel), ns);
979 980 981 982 983
	gtk_style_unref(ns);

	gtk_widget_queue_draw(GTK_WIDGET(panel));
}

984 985 986
static void
panel_try_to_set_back_color(PanelWidget *panel, GdkColor *color)
{
987
	GtkStyle *ns;
988

989
	ns = gtk_style_copy(GTK_WIDGET(panel)->style);
990 991 992
	gtk_style_ref(ns);

	ns->bg[GTK_STATE_NORMAL] = panel->back_color = *color;
993 994
	ns->bg[GTK_STATE_NORMAL].pixel =
		panel->back_color.pixel = 1; /* bogus */
995

996 997 998 999
	if(ns->bg_pixmap[GTK_STATE_NORMAL]) {
		gdk_imlib_free_pixmap(ns->bg_pixmap[GTK_STATE_NORMAL]);
		ns->bg_pixmap[GTK_STATE_NORMAL] = NULL;
	}
1000

1001
	gtk_widget_set_style(GTK_WIDGET(panel), ns);
1002

1003
	gtk_style_unref(ns);
1004

1005
	gtk_widget_queue_draw(GTK_WIDGET(panel));
1006 1007 1008
}

static void
1009 1010 1011
panel_widget_dnd_dropped_color (GtkWidget *widget,
				GdkEventDropDataAvailable *event,
				PanelWidget *panel)
1012
{
1013 1014
	gdouble *dropped = (gdouble *)event->data;
	GdkColor c;
1015

1016 1017 1018 1019
	c.red = (dropped[1]*65535);
	c.green = (dropped[2]*65535);
	c.blue = (dropped[3]*65535);
	c.pixel = 0;
1020

1021
	panel_try_to_set_back_color(panel, &c);
1022 1023
}

1024
static void
1025 1026 1027
panel_widget_dnd_drop_internal(GtkWidget *widget,
			       GdkEventDropDataAvailable *event,
			       gpointer data)
1028
{
1029
	PanelWidget *panel = data;
1030
	/* Test for the type that was dropped */
1031 1032 1033 1034 1035
	if (strcmp (event->data_type, "url:ALL") == 0) {
		panel_widget_dnd_dropped_filename (widget, event, panel);
	} else if(!strcmp(event->data_type, "application/x-color")) {
		panel_widget_dnd_dropped_color(widget, event, panel);
		panel->back_type = PANEL_BACK_COLOR;
1036 1037 1038 1039 1040
		gtk_signal_emit(GTK_OBJECT(panel),
				panel_widget_signals[BACK_CHANGE_SIGNAL],
				panel->back_type,
				panel->back_pixmap,
				&panel->back_color);
1041
	}
1042 1043 1044
	return;
}

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
static void
panel_resize_pixmap(PanelWidget *panel)
{
	GdkImlibImage *im;
	GdkPixmap *p;
	GtkStyle *ns;
	int w, h;

	im = gtk_object_get_data(GTK_OBJECT(panel),"gdk_image");
	if(!im) return;

	w = im->rgb_width;
	h = im->rgb_height;

	switch (panel->orient) {
	case PANEL_HORIZONTAL:
		gdk_imlib_render (im, w * panel->thick / h, panel->thick);
		break;

	case PANEL_VERTICAL:
		gdk_imlib_render (im, panel->thick, h * panel->thick / w);
		break;

	default:
		g_assert_not_reached ();
	}

	p = gdk_imlib_move_image (im);

1074
	ns = gtk_style_copy(GTK_WIDGET(panel)->style);
1075 1076
	gtk_style_ref(ns);

1077 1078
	if(ns->bg_pixmap[GTK_STATE_NORMAL])
		gdk_imlib_free_pixmap (ns->bg_pixmap[GTK_STATE_NORMAL]);
1079 1080
	ns->bg_pixmap[GTK_STATE_NORMAL] = p;

1081
	gtk_widget_set_style(GTK_WIDGET(panel), ns);
1082 1083 1084 1085
	
	gtk_style_unref(ns);
}

1086 1087 1088 1089
static int
panel_try_to_set_pixmap (PanelWidget *panel, char *pixmap)
{
	GdkImlibImage *im;
1090
	GdkImlibImage *im2;
1091
	GdkPixmap *p;
1092
	GtkStyle *ns;
1093

1094
	if(!pixmap || strcmp(pixmap,"")==0) {
1095
		ns = gtk_style_copy(GTK_WIDGET(panel)->style);
1096 1097 1098
		gtk_style_ref(ns);

		p = ns->bg_pixmap[GTK_STATE_NORMAL];
1099 1100
		if(p)
			gdk_imlib_free_pixmap (p);
1101 1102
		ns->bg_pixmap[GTK_STATE_NORMAL] = NULL;

1103
		gtk_widget_set_style(GTK_WIDGET(panel), ns);
1104 1105
	
		gtk_style_unref(ns);
1106 1107
		return 1;
	}
1108

1109 1110 1111 1112 1113 1114
	if (!g_file_exists (pixmap))
		return 0;
	
	im = gdk_imlib_load_image (pixmap);
	if (!im)
		return 0;
1115 1116 1117

	im2 = gtk_object_get_data(GTK_OBJECT(panel),"gdk_image");
	if(im2) gdk_imlib_destroy_image (im2);
1118
	
1119 1120 1121 1122 1123 1124 1125 1126
	if (panel->fit_pixmap_bg) {
		int w, h;

		w = im->rgb_width;
		h = im->rgb_height;

		switch (panel->orient) {
		case PANEL_HORIZONTAL:
1127
			gdk_imlib_render (im, w * panel->thick / h, panel->thick);
1128 1129 1130
			break;

		case PANEL_VERTICAL:
1131
			gdk_imlib_render (im, panel->thick, h * panel->thick / w);
1132 1133 1134 1135 1136 1137 1138 1139
			break;

		default:
			g_assert_not_reached ();
		}
	} else
		gdk_imlib_render (im, im->rgb_width, im->rgb_height);

1140
	p = gdk_imlib_move_image (im);
1141

1142
	ns = gtk_style_copy(GTK_WIDGET(panel)->style);
1143 1144
	gtk_style_ref(ns);

1145 1146
	if(ns->bg_pixmap[GTK_STATE_NORMAL])
		gdk_imlib_free_pixmap (ns->bg_pixmap[GTK_STATE_NORMAL]);
1147 1148
	ns->bg_pixmap[GTK_STATE_NORMAL] = p;

1149
	gtk_widget_set_style(GTK_WIDGET(panel), ns);
1150 1151
	
	gtk_style_unref(ns);
1152
	gtk_object_set_data(GTK_OBJECT(panel),"gdk_image",im);
1153 1154
	return 1;
}
1155

1156
static void
1157 1158 1159 1160
panel_widget_realize(GtkWidget *w, gpointer data)
{
	PanelWidget *panel = PANEL_WIDGET(w);

1161 1162 1163 1164 1165
	if(panel->back_type == PANEL_BACK_PIXMAP) {
		if (!panel_try_to_set_pixmap (panel, panel->back_pixmap))
			panel->back_type = PANEL_BACK_NONE;
	} else if(panel->back_type == PANEL_BACK_COLOR) {
		panel_try_to_set_back_color(panel, &panel->back_color);
1166
	}
1167

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
	if(panel->pixmap)
		gdk_pixmap_unref(panel->pixmap);
	panel->pixw = w->allocation.width;
	panel->pixh = w->allocation.height;
	panel->pixmap = gdk_pixmap_new(w->window,
				       panel->pixw,
				       panel->pixh,
				       gtk_widget_get_visual(w)->depth);

	panel_widget_draw_all(panel);

1179
	gtk_widget_dnd_drop_set (GTK_WIDGET(panel), TRUE,
1180 1181 1182
				 image_drop_types,
				 sizeof(image_drop_types)/sizeof(char *),
				 FALSE);
1183 1184
}

1185
static int
1186 1187 1188 1189 1190 1191 1192 1193
panel_widget_destroy(GtkWidget *w, gpointer data)
{
	GdkImlibImage *im;

	im = gtk_object_get_data(GTK_OBJECT(w),"gdk_image");
	if(im) gdk_imlib_destroy_image (im);
	gtk_object_set_data(GTK_OBJECT(w),"gdk_image",NULL);
	
1194 1195 1196
	/*remove from panels list*/
	panels = g_list_remove(panels,w);
	
1197 1198 1199
	return FALSE;
}

1200 1201
static int panel_widget_applet_event(GtkWidget *widget, GdkEvent *event, gpointer data);

1202 1203
static int
is_in_widget(GdkEventButton *bevent, GtkWidget *widget)
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
{
	if(bevent->x >= widget->allocation.x &&
	   bevent->x < (widget->allocation.x + widget->allocation.width) &&
	   bevent->y >= widget->allocation.y &&
	   bevent->y < (widget->allocation.y + widget->allocation.height))
		return TRUE;
	return FALSE;
}

static int
panel_widget_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
	PanelWidget *panel = PANEL_WIDGET(widget);
	GList *list;
	GdkEventButton *bevent = (GdkEventButton *) event;
	
1220 1221 1222
	if((event->type != GDK_BUTTON_PRESS &&
	    event->type != GDK_BUTTON_RELEASE) ||
	   bevent->window != widget->window)
1223 1224 1225 1226 1227
		return FALSE;

	for(list = panel->no_window_applet_list; list!=NULL;
	    list = g_list_next(list)) {
		AppletData *ad = list->data;
1228 1229 1230 1231
		if(is_in_widget(bevent,ad->applet) &&
		   bevent->button != 1)
			return panel_widget_applet_event(ad->applet,
							 event,data);
1232 1233 1234 1235
	}
	return FALSE;
}

1236 1237
static void
panel_widget_init (PanelWidget *panel)
1238
{
1239 1240 1241 1242
	/*this makes the popup "pop down" once the button is released*/
	gtk_widget_set_events(GTK_WIDGET(panel),
			      gtk_widget_get_events(GTK_WIDGET(panel)) |
			      GDK_BUTTON_RELEASE_MASK);
1243 1244 1245
	
	panel->pixmap = NULL;
	panel->pixw = panel->pixh = -1;
1246

1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
	panel->back_type =PANEL_BACK_NONE;
	panel->fit_pixmap_bg = FALSE;
	panel->back_pixmap = NULL;
	panel->back_color.red = 0;
	panel->back_color.green = 0;
	panel->back_color.blue = 0;
	panel->back_color.pixel = 1;
	panel->packed = FALSE;
	panel->orient = PANEL_HORIZONTAL;
	panel->thick = PANEL_MINIMUM_WIDTH;
	panel->size = INT_MAX;
	panel->applet_list = NULL;
1259
	panel->no_window_applet_list = NULL;
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
	panel->master_widget = NULL;
	panel->drop_widget = GTK_WIDGET(panel);

	if(!fleur_cursor)
		fleur_cursor = gdk_cursor_new(GDK_FLEUR);

	gtk_signal_connect(GTK_OBJECT(panel),
			   "destroy",
			   GTK_SIGNAL_FUNC(panel_widget_destroy),
			   NULL);
1270 1271 1272 1273
	gtk_signal_connect(GTK_OBJECT(panel),
			   "event",
			   GTK_SIGNAL_FUNC(panel_widget_event),
			   NULL);
1274 1275

	/* Ok, cool hack begins: drop image files on the panel */
1276
	gtk_signal_connect (GTK_OBJECT (panel),
1277 1278 1279
			    "drop_data_available_event",
			    GTK_SIGNAL_FUNC (panel_widget_dnd_drop_internal),
			    panel);
1280
	
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
	panels = g_list_append(panels,panel);
}

GtkWidget *
panel_widget_new (int packed,
		  PanelOrientation orient,
		  PanelBackType back_type,
		  char *back_pixmap,
		  int fit_pixmap_bg,
		  GdkColor *back_color)
{
	PanelWidget *panel;

1294 1295 1296
	gtk_widget_push_visual (gdk_imlib_get_visual ());
	gtk_widget_push_colormap (gdk_imlib_get_colormap ());

1297 1298
	panel = gtk_type_new(panel_widget_get_type());

1299 1300 1301
	gtk_widget_pop_colormap ();
	gtk_widget_pop_visual ();

1302
	panel->back_type = back_type;
1303

1304 1305 1306 1307 1308
	panel->fit_pixmap_bg = fit_pixmap_bg;
	if(back_pixmap)
		panel->back_pixmap = g_strdup(back_pixmap);
	else
		panel->back_pixmap = NULL;
1309
	
1310 1311 1312 1313 1314 1315
	if(back_color)
		panel->back_color = *back_color;
	else {
		panel->back_color.red = 0;
		panel->back_color.green = 0;
		panel->back_color.blue = 0;
1316 1317
		panel->back_color.pixel = 1;
	}	
1318

1319
	panel->orient = orient;
1320

1321 1322
	panel->packed = packed;
	if(packed)
1323
		panel->size = 0;
1324 1325 1326
	else
		panel->size = INT_MAX;
	
1327 1328 1329 1330
	gtk_signal_connect_after(GTK_OBJECT(panel),
				 "realize",
				 GTK_SIGNAL_FUNC(panel_widget_realize),
				 panel);
1331
	
1332
	return GTK_WIDGET(panel);
1333
}
1334

1335 1336
static void
_panel_widget_applet_drag_start_no_grab(PanelWidget *panel, GtkWidget *applet)
1337
{
1338 1339 1340 1341
#ifdef DEBUG
  g_message("Starting drag on a %s at %p\n",
	    gtk_type_name(GTK_OBJECT(applet)->klass->type), applet);
#endif
1342 1343
	panel->currently_dragged_applet =
		gtk_object_get_data(GTK_OBJECT(applet), PANEL_APPLET_DATA);
1344 1345
}

1346

1347
void
1348 1349 1350 1351 1352 1353 1354 1355
panel_widget_applet_drag_start_no_grab(PanelWidget *panel, GtkWidget *applet)
{
	panel_applet_in_drag = TRUE;
	_panel_widget_applet_drag_start_no_grab(panel,applet);
}

void
_panel_widget_applet_drag_end_no_grab(PanelWidget *panel)
1356
{
1357 1358 1359
#ifdef DEBUG
  g_message("Ending drag\n");
#endif
1360 1361 1362 1363
	panel->currently_dragged_applet = NULL;
}

void
1364
panel_widget_applet_drag_end_no_grab(PanelWidget *panel)
1365
{
1366 1367 1368 1369 1370 1371 1372
	_panel_widget_applet_drag_end_no_grab(panel);
	panel_applet_in_drag = FALSE;
}

static void
_panel_widget_applet_drag_start(PanelWidget *panel, GtkWidget *applet)
{
1373 1374 1375 1376
#ifdef DEBUG
  g_message("Starting drag [grabbed] on a %s at %p\n",
	    gtk_type_name(GTK_OBJECT(applet)->klass->type), applet);
#endif
1377
	_panel_widget_applet_drag_start_no_grab(panel,applet);
1378 1379

	gtk_grab_add(applet);
1380 1381 1382 1383 1384 1385 1386 1387
	if(applet->window) {
		gdk_pointer_grab(applet->window,
				 FALSE,
				 APPLET_EVENT_MASK,
				 NULL,
				 fleur_cursor,
				 GDK_CURRENT_TIME);
	}
1388 1389
}

1390
void
1391 1392 1393 1394 1395 1396 1397 1398
panel_widget_applet_drag_start(PanelWidget *panel, GtkWidget *applet)
{
	panel_applet_in_drag = TRUE;
	_panel_widget_applet_drag_start(panel, applet);
}

static void
_panel_widget_applet_drag_end(PanelWidget *panel)
1399 1400
{
	gdk_pointer_ungrab(GDK_CURRENT_TIME);
1401
	gtk_grab_remove(panel->currently_dragged_applet->applet);
1402
	_panel_widget_applet_drag_end_no_grab(panel);
1403
	panel->currently_dragged_applet = NULL;
1404 1405 1406 1407 1408 1409 1410
}

void
panel_widget_applet_drag_end(PanelWidget *panel)
{
	_panel_widget_applet_drag_end(panel);
	panel_applet_in_drag = FALSE;
1411 1412
}

1413
/*calculates the value to move the applet by*/
1414 1415
static int
panel_widget_get_moveby(PanelWidget *panel, int pos)
1416
{
1417
	int x,y;
1418

1419
	gtk_widget_get_pointer(GTK_WIDGET(panel), &x, &y);
1420 1421

	if(panel->orient == PANEL_HORIZONTAL)
1422
		return x - pos;
1423
	else
1424
		return y - pos;
1425 1426
}

1427
static GList *
1428
walk_up_to(int pos, GList *list)
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
{
	AppletData *ad = list->data;
	if(ad->pos <= pos && ad->pos+ad->cells > pos)
		return list;
	while(g_list_next(list) && ad->pos+ad->cells <= pos) {
		list = g_list_next(list);
		ad = list->data;
	}
	while(g_list_previous(list) && ad->pos > pos) {
		list = g_list_previous(list);
		ad = list->data;
	}
	return list;
}

static GtkWidget *
1445
is_in_applet(int pos, AppletData *ad)
1446 1447 1448 1449 1450 1451
{
	if(ad->pos <= pos && ad->pos+ad->cells > pos)
		return ad->applet;
	return NULL;
}

1452
static int
1453 1454 1455
panel_widget_get_free_spot(PanelWidget *panel, AppletData *ad)
{
	int i,e;
1456 1457 1458 1459
	int x,y;
	int place;
	int start;
	int right=-1,left=-1;
1460
	GList *list;
1461

1462
	gtk_widget_get_pointer(GTK_WIDGET(panel), &x, &y);
1463 1464

	if(panel->orient == PANEL_HORIZONTAL)
1465
		place = x;
1466
	else
1467
		place = y;
1468 1469 1470 1471

	if(ad->pos>=panel->size)
		return -1;

1472 1473 1474 1475 1476 1477 1478
	if(!panel->applet_list) {
		if(place+ad->cells>panel->size)
			return panel->size-ad->cells;
		else
			return place;
	}

1479 1480
	list = panel->applet_list;

1481
	start = place-(ad->cells/2);
1482