summaryrefslogtreecommitdiff
path: root/src/nautilus-information-panel.c
blob: 04b5591b7b8b53fe0ac53f30d59940b752bd156a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
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
676
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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* Nautilus
 * Copyright (C) 1999, 2000 Eazel, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: Andy Hertzfeld <andy@eazel.com>
 *
 * This is the index panel widget, which displays overview information
 * in a vertical panel and hosts the meta-views.
 *
 */

#include <config.h>
#include "ntl-index-panel.h"

#include <math.h>
#include <libgnomeui/gnome-uidefs.h>
#include <libgnomevfs/gnome-vfs-types.h>
#include <libgnomevfs/gnome-vfs-uri.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <libnautilus-extensions/nautilus-background.h>
#include <libnautilus-extensions/nautilus-directory.h>
#include <libnautilus-extensions/nautilus-file.h>
#include <libnautilus-extensions/nautilus-glib-extensions.h>
#include <libnautilus-extensions/nautilus-global-preferences.h>
#include <libnautilus-extensions/nautilus-gtk-extensions.h>
#include <libnautilus-extensions/nautilus-gtk-macros.h>
#include <libnautilus-extensions/nautilus-keep-last-vertical-box.h>
#include <libnautilus-extensions/nautilus-metadata.h>
#include <libnautilus-extensions/nautilus-mime-type.h>
#include <libnautilus-extensions/nautilus-program-choosing.h>
#include <libnautilus-extensions/nautilus-string.h>
#include <nautilus-widgets/nautilus-preferences.h>
#include "ntl-meta-view.h"
#include "nautilus-index-tabs.h"
#include "nautilus-index-title.h"

struct NautilusIndexPanelDetails {
	GtkVBox *container;
	NautilusIndexTitle *title;
	GtkNotebook *notebook;
	NautilusIndexTabs *index_tabs;
	NautilusIndexTabs *title_tab;
	GtkHBox *button_box_centerer;
	GtkVBox *button_box;
	gboolean has_buttons;
	char *uri;
	int selected_index;
	NautilusDirectory *directory;
	int background_connection;
    int old_width;
};

static void     nautilus_index_panel_initialize_class   (GtkObjectClass     *object_klass);
static void     nautilus_index_panel_initialize         (GtkObject          *object);
static gboolean nautilus_index_panel_press_event        (GtkWidget          *widget,
							 GdkEventButton     *event);
static gboolean nautilus_index_panel_leave_event        (GtkWidget          *widget,
							 GdkEventCrossing   *event);
static gboolean nautilus_index_panel_motion_event       (GtkWidget          *widget,
							 GdkEventMotion     *event);
static void     nautilus_index_panel_destroy            (GtkObject          *object);
static void     nautilus_index_panel_drag_data_received (GtkWidget          *widget,
							 GdkDragContext     *context,
							 int                 x,
							 int                 y,
							 GtkSelectionData   *selection_data,
							 guint               info,
							 guint               time);
static void 	nautilus_index_panel_size_allocate       (GtkWidget              *widget,
						  	 GtkAllocation         *allocation);
static void     nautilus_index_panel_update_info        (NautilusIndexPanel *index_panel,
							 const char         *title);
static void     nautilus_index_panel_update_buttons     (NautilusIndexPanel *index_panel);
static void     add_command_buttons                     (NautilusIndexPanel *index_panel,
							 GList              *command_list);

#define DEFAULT_BACKGROUND_COLOR "rgb:DDDD/DDDD/FFFF"
#define DEFAULT_TAB_COLOR "rgb:9999/9999/9999"

#define INDEX_PANEL_WIDTH 136
#define INDEX_PANEL_HEIGHT 400

/* drag and drop definitions */

enum {
	TARGET_URI_LIST,
	TARGET_COLOR,
	TARGET_BGIMAGE,
	TARGET_KEYWORD,
	TARGET_GNOME_URI_LIST
};

static GtkTargetEntry target_table[] = {
	{ "text/uri-list",  0, TARGET_URI_LIST },
	{ "application/x-color", 0, TARGET_COLOR },
	{ "property/bgimage", 0, TARGET_BGIMAGE },
	{ "property/keyword", 0, TARGET_KEYWORD },
	{ "special/x-gnome-icon-list",  0, TARGET_GNOME_URI_LIST }
};

typedef enum {
	NO_PART,
	BACKGROUND_PART,
	ICON_PART,
	TITLE_TAB_PART,
	TABS_PART
} IndexPanelPart;

NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusIndexPanel, nautilus_index_panel, GTK_TYPE_EVENT_BOX)

/* initializing the class object by installing the operations we override */
static void
nautilus_index_panel_initialize_class (GtkObjectClass *object_klass)
{
	GtkWidgetClass *widget_class;
	NautilusIndexPanelClass *klass;

	widget_class = GTK_WIDGET_CLASS (object_klass);
	klass = NAUTILUS_INDEX_PANEL_CLASS (object_klass);

	object_klass->destroy = nautilus_index_panel_destroy;

	widget_class->drag_data_received  = nautilus_index_panel_drag_data_received;
	widget_class->motion_notify_event = nautilus_index_panel_motion_event;
	widget_class->leave_notify_event = nautilus_index_panel_leave_event;
	widget_class->button_press_event  = nautilus_index_panel_press_event;
	widget_class->size_allocate = nautilus_index_panel_size_allocate;
}

/* utility routine to allocate the box the holds the command buttons */
static void
make_button_box (NautilusIndexPanel *index_panel)
{
	index_panel->details->button_box_centerer = GTK_HBOX (gtk_hbox_new (FALSE, 0));
	gtk_widget_show (GTK_WIDGET (index_panel->details->button_box_centerer));
	gtk_box_pack_start_defaults (GTK_BOX (index_panel->details->container),
			    	     GTK_WIDGET (index_panel->details->button_box_centerer));

	index_panel->details->button_box = GTK_VBOX (nautilus_keep_last_vertical_box_new (GNOME_PAD_SMALL));
	gtk_container_set_border_width (GTK_CONTAINER (index_panel->details->button_box), GNOME_PAD);				
	gtk_widget_show (GTK_WIDGET (index_panel->details->button_box));
	gtk_box_pack_start (GTK_BOX (index_panel->details->button_box_centerer),
			    GTK_WIDGET (index_panel->details->button_box),
			    TRUE, FALSE, 0);
	index_panel->details->has_buttons = FALSE;
}

/* initialize the instance's fields, create the necessary subviews, etc. */

static void
nautilus_index_panel_initialize (GtkObject *object)
{
	NautilusIndexPanel *index_panel;
	GtkWidget* widget;
	
	index_panel = NAUTILUS_INDEX_PANEL (object);
	widget = GTK_WIDGET (object);

	index_panel->details = g_new0 (NautilusIndexPanelDetails, 1);
	
	/* set the size of the index panel */
	gtk_widget_set_usize (widget, INDEX_PANEL_WIDTH, INDEX_PANEL_HEIGHT);
  	
	/* create the container box */
  	index_panel->details->container = GTK_VBOX (gtk_vbox_new (FALSE, 0));
	gtk_container_set_border_width (GTK_CONTAINER (index_panel->details->container), 0);				
	gtk_widget_show (GTK_WIDGET (index_panel->details->container));
	gtk_container_add (GTK_CONTAINER (index_panel),
			   GTK_WIDGET (index_panel->details->container));

	/* allocate and install the index title widget */ 
	index_panel->details->title = NAUTILUS_INDEX_TITLE (nautilus_index_title_new ());
	gtk_widget_show (GTK_WIDGET (index_panel->details->title));
	gtk_box_pack_start (GTK_BOX (index_panel->details->container),
			    GTK_WIDGET (index_panel->details->title),
			    FALSE, FALSE, GNOME_PAD);
	
	/* first, allocate the index tabs */
	index_panel->details->index_tabs = NAUTILUS_INDEX_TABS (nautilus_index_tabs_new ());
	index_panel->details->selected_index = -1;

	/* also, allocate the title tab */
	index_panel->details->title_tab = NAUTILUS_INDEX_TABS (nautilus_index_tabs_new ());
	nautilus_index_tabs_set_title_mode (index_panel->details->title_tab, TRUE);	
	
	gtk_widget_show (GTK_WIDGET (index_panel->details->index_tabs));
	gtk_box_pack_end (GTK_BOX (index_panel->details->container),
			  GTK_WIDGET (index_panel->details->index_tabs),
			  FALSE, FALSE, 0);

	index_panel->details->old_width = widget->allocation.width;
	
	/* allocate and install the meta-tabs */
  	index_panel->details->notebook = GTK_NOTEBOOK (gtk_notebook_new ());
	gtk_object_ref (GTK_OBJECT (index_panel->details->notebook));
	gtk_object_sink (GTK_OBJECT (index_panel->details->notebook));
		
	gtk_notebook_set_show_tabs (index_panel->details->notebook, FALSE);
	
	/* allocate and install the command button container */
	make_button_box (index_panel);

	/* prepare ourselves to receive dropped objects */
	gtk_drag_dest_set (GTK_WIDGET (index_panel),
			   GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP, 
			   target_table, NAUTILUS_N_ELEMENTS (target_table),
			   GDK_ACTION_COPY | GDK_ACTION_MOVE);
}

static void
nautilus_index_panel_destroy (GtkObject *object)
{
	NautilusIndexPanel *index_panel;

	index_panel = NAUTILUS_INDEX_PANEL (object);

	gtk_object_unref (GTK_OBJECT (index_panel->details->notebook));

	nautilus_directory_unref (index_panel->details->directory);

	g_free (index_panel->details->uri);
	g_free (index_panel->details);

	NAUTILUS_CALL_PARENT_CLASS (GTK_OBJECT_CLASS, destroy, (object));
}

/* create a new instance */
NautilusIndexPanel *
nautilus_index_panel_new (void)
{
	return NAUTILUS_INDEX_PANEL (gtk_type_new (nautilus_index_panel_get_type ()));
}

static IndexPanelPart
hit_test (NautilusIndexPanel *index_panel,
	  int x, int y)
{
	if (nautilus_point_in_widget (GTK_WIDGET (index_panel->details->index_tabs), x, y)) {
		return TABS_PART;
	}
	
	if (nautilus_point_in_widget (GTK_WIDGET (index_panel->details->title_tab), x, y)) {
		return TITLE_TAB_PART;
	}
	
	if (nautilus_index_title_hit_test_icon (index_panel->details->title, x, y)) {
		return ICON_PART;
	}
	
	if (nautilus_point_in_widget (GTK_WIDGET (index_panel), x, y)) {
		return BACKGROUND_PART;
	}

	return NO_PART;
}

/* FIXME bugzilla.eazel.com 606: 
 * If passed a bogus URI this could block for a long time. 
 */
static gboolean
uri_is_local_image (const char *uri)
{
	GdkPixbuf *pixbuf;
	
	/* FIXME bugzilla.eazel.com 607: 
	 * Perhaps this should not be hardcoded like this. 
	 */
	if (!nautilus_str_has_prefix (uri, "file://")) {
		return FALSE;
	}
	pixbuf = gdk_pixbuf_new_from_file (uri + 7);
	if (pixbuf == NULL) {
		return FALSE;
	}
	gdk_pixbuf_unref (pixbuf);
	return TRUE;
}

static void
receive_dropped_uri_list (NautilusIndexPanel *index_panel,
			  int x, int y,
			  GtkSelectionData *selection_data)
{
	char **uris;
	gboolean exactly_one;
	NautilusFile *file;

	uris = g_strsplit (selection_data->data, "\r\n", 0);
	exactly_one = uris[0] != NULL && uris[1] == NULL;

	/* FIXME bugzilla.eazel.com 604: handle files by setting the location to the file */
	
	switch (hit_test (index_panel, x, y)) {
	case NO_PART:
	case BACKGROUND_PART:
		if (exactly_one && uri_is_local_image (uris[0])) {
			nautilus_background_set_tile_image_uri
			(nautilus_get_widget_background (GTK_WIDGET (index_panel)),
		 	uris[0]);
		}
		break;
	case TABS_PART:
	case TITLE_TAB_PART:
		break;
	case ICON_PART:
		/* handle images dropped on the logo specially */
		/* FIXME bugzilla.eazel.com 605: 
		 * Need feedback for cases where there is more than one URI 
		 * and where the URI is not alocal image.
		 */
		if (exactly_one && uri_is_local_image (uris[0])) {
			file = nautilus_file_get (index_panel->details->uri);
			if (file != NULL) {
				nautilus_file_set_metadata (file,
							    NAUTILUS_METADATA_KEY_CUSTOM_ICON,
							    NULL,
							    uris[0]);
				nautilus_file_unref (file);
			}
		}
		break;
	}

	g_strfreev (uris);
}

static void
receive_dropped_color (NautilusIndexPanel *index_panel,
		       int x, int y,
		       GtkSelectionData *selection_data)
{
	guint16 *channels;
	char *color_spec;

	if (selection_data->length != 8 || selection_data->format != 16) {
		g_warning ("received invalid color data");
		return;
	}
	
	channels = (guint16 *) selection_data->data;
	color_spec = g_strdup_printf ("rgb:%04hX/%04hX/%04hX", channels[0], channels[1], channels[2]);

	switch (hit_test (index_panel, x, y)) {
	case NO_PART:
		g_warning ("dropped color, but not on any part of sidebar");
		break;
	case TABS_PART:
		/* color dropped on main tabs */
		nautilus_index_tabs_receive_dropped_color
			(index_panel->details->index_tabs,
			 x, y, selection_data);
		
		nautilus_directory_set_metadata (index_panel->details->directory,
					 NAUTILUS_METADATA_KEY_SIDEBAR_TAB_COLOR,
					 DEFAULT_TAB_COLOR,
					 color_spec);
		
		break;
	case TITLE_TAB_PART:
		/* color dropped on title tab */
		nautilus_index_tabs_receive_dropped_color
			(index_panel->details->title_tab,
			 x, y, selection_data);
		
		nautilus_directory_set_metadata (index_panel->details->directory,
					 NAUTILUS_METADATA_KEY_SIDEBAR_TITLE_TAB_COLOR,
					 DEFAULT_TAB_COLOR,
					 color_spec);
		break;
	case ICON_PART:
	case BACKGROUND_PART:
		/* Let the background change based on the dropped color. */
		nautilus_background_receive_dropped_color
			(nautilus_get_widget_background (GTK_WIDGET (index_panel)),
			 GTK_WIDGET (index_panel), x, y, selection_data);
		break;
	}
	g_free(color_spec);
}

/* handle receiving a dropped keyword */

static void
receive_dropped_keyword (NautilusIndexPanel *index_panel,
		       int x, int y,
		       GtkSelectionData *selection_data)
{
	NautilusFile *file;
	GList *keywords, *word;
	char *title;
			
	/* OK, now we've got the keyword, so add it to the metadata */

	file = nautilus_file_get (index_panel->details->uri);
	if (file == NULL)
		return;

	/* Check and see if it's already there. */
	keywords = nautilus_file_get_keywords (file);
	word = g_list_find_custom (keywords, selection_data->data, (GCompareFunc) strcmp);
	if (word == NULL)
		keywords = g_list_append (keywords, g_strdup (selection_data->data));
	else
		keywords = g_list_remove_link (keywords, word);

	nautilus_file_set_keywords (file, keywords);
	nautilus_file_unref(file);
	
	/* regenerate the display */
	title = nautilus_index_title_get_text(index_panel->details->title);
	nautilus_index_panel_update_info (index_panel, title);  	
	g_free(title);
}

static void  
nautilus_index_panel_drag_data_received (GtkWidget *widget, GdkDragContext *context,
					 int x, int y,
					 GtkSelectionData *selection_data,
					 guint info, guint time)
{
	NautilusIndexPanel *index_panel;

	g_return_if_fail (NAUTILUS_IS_INDEX_PANEL (widget));

	index_panel = NAUTILUS_INDEX_PANEL (widget);

	switch (info) {
	case TARGET_GNOME_URI_LIST:
	case TARGET_URI_LIST:
		receive_dropped_uri_list (index_panel, x, y, selection_data);
		break;
		
	case TARGET_COLOR:
		receive_dropped_color (index_panel, x, y, selection_data);
		break;
	case TARGET_BGIMAGE:
	
		if (hit_test (index_panel, x, y) == BACKGROUND_PART)
			receive_dropped_uri_list (index_panel, x, y, selection_data);
		break;	
	case TARGET_KEYWORD:
		receive_dropped_keyword(index_panel, x, y, selection_data);
		break;
	default:
		g_warning ("unknown drop type");
	}
}

/* add a new meta-view to the index panel */
void
nautilus_index_panel_add_meta_view (NautilusIndexPanel *index_panel, NautilusViewFrame *meta_view)
{
	GtkWidget *label;
	char cbuf[32];
	const char *description;
	int page_num;
	
	g_return_if_fail (NAUTILUS_IS_INDEX_PANEL (index_panel));
	g_return_if_fail (NAUTILUS_IS_VIEW_FRAME (meta_view));
	
	description = nautilus_meta_view_frame_get_label (NAUTILUS_META_VIEW_FRAME (meta_view));
	if (description == NULL) {
		/* FIXME bugzilla.eazel.com 599: 
		 * Why is a hex address better than an empty string? 
		 */
		g_snprintf (cbuf, sizeof (cbuf), "%p", meta_view);
		description = cbuf;
	} 
	
	label = gtk_label_new (description);
	gtk_widget_show (label);
	
	gtk_notebook_append_page (GTK_NOTEBOOK (index_panel->details->notebook),
				  GTK_WIDGET (meta_view), label);
	page_num = gtk_notebook_page_num (GTK_NOTEBOOK (index_panel->details->notebook),
					  GTK_WIDGET (meta_view));

	/* tell the index tabs about it */
	nautilus_index_tabs_add_view (index_panel->details->index_tabs,
				      description, GTK_WIDGET (meta_view), page_num);
	
	gtk_widget_show (GTK_WIDGET (meta_view));
}

/* remove the passed-in meta-view from the index panel */
void
nautilus_index_panel_remove_meta_view (NautilusIndexPanel *index_panel,
				       NautilusViewFrame *meta_view)
{
	int page_num;
	
	page_num = gtk_notebook_page_num (GTK_NOTEBOOK (index_panel->details->notebook),
					  GTK_WIDGET (meta_view));
	if (page_num < 0) {
		return;
	}

	gtk_notebook_remove_page (GTK_NOTEBOOK (index_panel->details->notebook),
				  page_num);
}

/* utility to activate the metaview corresponding to the passed in index  */
static void
nautilus_index_panel_activate_meta_view (NautilusIndexPanel *index_panel, int which_view)
{
	char *title;
	GtkNotebook *notebook;

	notebook = index_panel->details->notebook;
	if (index_panel->details->selected_index < 0) {
		gtk_widget_show (GTK_WIDGET (notebook));
		if (GTK_WIDGET (notebook)->parent == NULL) {
			gtk_box_pack_end (GTK_BOX (index_panel->details->container),
					  GTK_WIDGET (notebook),
					  TRUE, TRUE, 0);
		}
		
		gtk_widget_show (GTK_WIDGET (index_panel->details->title_tab));
		if (GTK_WIDGET (index_panel->details->title_tab)->parent == NULL) {
			gtk_box_pack_end (GTK_BOX (index_panel->details->container),
					  GTK_WIDGET (index_panel->details->title_tab),
					  FALSE, FALSE, 0);
		}
	}
	
	index_panel->details->selected_index = which_view;
	title = nautilus_index_tabs_get_title_from_index (index_panel->details->index_tabs,
							  which_view);
	nautilus_index_tabs_set_title (index_panel->details->title_tab, title);
	nautilus_index_tabs_prelight_tab (index_panel->details->title_tab, -1);
    
	g_free (title);
	
	/* hide the buttons, since they look confusing when partially overlapped */
	gtk_widget_hide (GTK_WIDGET (index_panel->details->button_box));
	
	gtk_notebook_set_page (notebook, which_view);
}

/* utility to deactivate the active metaview */
static void
nautilus_index_panel_deactivate_meta_view(NautilusIndexPanel *index_panel)
{
	if (index_panel->details->selected_index >= 0) {
		gtk_widget_hide (GTK_WIDGET (index_panel->details->notebook));
		gtk_widget_hide (GTK_WIDGET (index_panel->details->title_tab));
	}
	
	gtk_widget_show (GTK_WIDGET (index_panel->details->button_box));
	index_panel->details->selected_index = -1;
	nautilus_index_tabs_select_tab (index_panel->details->index_tabs, -1);
}

/* handle mouse motion events by passing it to the tabs if necessary for pre-lighting */
static gboolean
nautilus_index_panel_motion_event (GtkWidget *widget, GdkEventMotion *event)
{
	int x, y;
	int which_tab;
	int title_top, title_bottom;
	NautilusIndexPanel *index_panel;
	NautilusIndexTabs *index_tabs, *title_tab;

	index_panel = NAUTILUS_INDEX_PANEL (widget);

	gtk_widget_get_pointer(widget, &x, &y);
	
	/* if the click is in the main tabs, tell them about it */
	index_tabs = index_panel->details->index_tabs;
	if (y >= GTK_WIDGET (index_tabs)->allocation.y) {
		which_tab = nautilus_index_tabs_hit_test (index_tabs, x, y);
		nautilus_index_tabs_prelight_tab (index_tabs, which_tab);
	}

	/* also handle prelighting in the title tab if necessary */
	if (index_panel->details->selected_index >= 0) {
		title_tab = index_panel->details->title_tab;
		title_top = GTK_WIDGET (title_tab)->allocation.y;
		title_bottom = title_top + GTK_WIDGET (title_tab)->allocation.height;
		if (y >= title_top && y < title_bottom) {
			which_tab = nautilus_index_tabs_hit_test (title_tab, x, y);
		} else {
			which_tab = -1;
		}
		nautilus_index_tabs_prelight_tab (title_tab, which_tab);
	}

	return TRUE;
}

/* handle the leave event by turning off the preliting */

static gboolean
nautilus_index_panel_leave_event (GtkWidget *widget, GdkEventCrossing *event)
{
	NautilusIndexPanel *index_panel;
	NautilusIndexTabs *index_tabs;

	index_panel = NAUTILUS_INDEX_PANEL (widget);
	index_tabs = index_panel->details->index_tabs; 
	nautilus_index_tabs_prelight_tab (index_tabs, -1);

	return TRUE;
}

/* hit-test the index tabs and activate if necessary */

static gboolean
nautilus_index_panel_press_event (GtkWidget *widget, GdkEventButton *event)
{
	int title_top, title_bottom;
	NautilusIndexPanel *index_panel;
	NautilusIndexTabs *index_tabs;
	NautilusIndexTabs *title_tab;
	int rounded_y;
	int which_tab;
		
	index_panel = NAUTILUS_INDEX_PANEL (widget);
	index_tabs = index_panel->details->index_tabs;
	title_tab = index_panel->details->title_tab;
	rounded_y = floor (event->y + .5);

	/* if the click is in the main tabs, tell them about it */
	if (rounded_y >= GTK_WIDGET (index_panel->details->index_tabs)->allocation.y) {
		which_tab = nautilus_index_tabs_hit_test (index_tabs, event->x, event->y);
		if (which_tab >= 0) {
			nautilus_index_tabs_select_tab (index_tabs, which_tab);
			nautilus_index_panel_activate_meta_view (index_panel, which_tab);
			gtk_widget_queue_draw (widget);	
		}
	} 
	
	/* also handle clicks in the title tab if necessary */
	if (index_panel->details->selected_index >= 0) {
		title_top = GTK_WIDGET (index_panel->details->title_tab)->allocation.y;
		title_bottom = title_top + GTK_WIDGET (index_panel->details->title_tab)->allocation.height;
		if (rounded_y >= title_top && rounded_y <= title_bottom) {
			which_tab = nautilus_index_tabs_hit_test (title_tab, event->x, event->y);
			if (which_tab >= 0) {
				/* the user clicked in the title tab, so deactivate the metaview */
				nautilus_index_panel_deactivate_meta_view (index_panel);
			}
		}
	}
	return TRUE;
}

static void
nautilus_index_panel_background_changed (NautilusIndexPanel *index_panel)
{
	NautilusBackground *background;
	char *color_spec, *image;
	
	if (index_panel->details->directory == NULL) {
		return;
	}
	
	background = nautilus_get_widget_background (GTK_WIDGET (index_panel));
	
	color_spec = nautilus_background_get_color (background);
	nautilus_directory_set_metadata (index_panel->details->directory,
					 NAUTILUS_METADATA_KEY_SIDEBAR_BACKGROUND_COLOR,
					 DEFAULT_BACKGROUND_COLOR,
					 color_spec);
	g_free (color_spec);

	image = nautilus_background_get_tile_image_uri (background);
	nautilus_directory_set_metadata (index_panel->details->directory,
					 NAUTILUS_METADATA_KEY_SIDEBAR_BACKGROUND_IMAGE,
					 NULL,
					 image);	
	g_free (image);
}

static void
command_button_callback (GtkWidget *button, char *command_str)
{
	NautilusIndexPanel *index_panel;
	
	index_panel = NAUTILUS_INDEX_PANEL (gtk_object_get_user_data (GTK_OBJECT (button)));

	nautilus_launch_application (command_str, index_panel->details->uri);	
}

static void
nautilus_index_panel_chose_application_callback (const char *command_string,
						 gpointer callback_data)
{
	g_assert (NAUTILUS_IS_INDEX_PANEL (callback_data));

	if (command_string != NULL) {
		nautilus_launch_application 
			(command_string, 
			 NAUTILUS_INDEX_PANEL (callback_data)->details->uri);
	}
}						 

static void
open_with_callback (GtkWidget *button, gpointer ignored)
{
	NautilusIndexPanel *index_panel;
	NautilusFile *file;
	
	index_panel = NAUTILUS_INDEX_PANEL (gtk_object_get_user_data (GTK_OBJECT (button)));

	file = nautilus_file_get (index_panel->details->uri);
	g_return_if_fail (file != NULL);

	nautilus_choose_application_for_file
		(file,
		 GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (index_panel))),
		 nautilus_index_panel_chose_application_callback,
		 index_panel);

	nautilus_file_unref (file);
}

/* utility routine that allocates the command buttons from the command list */

static void
add_command_buttons (NautilusIndexPanel *index_panel, GList *command_list)
{
	char *command_string, *temp_str;
	GList *p;
	GtkWidget *temp_button;
	NautilusCommandInfo *info;
	
	for (p = command_list; p != NULL; p = p->next) {
	        info = p->data;
	        
		index_panel->details->has_buttons = TRUE;

		temp_str = g_strdup_printf (_("Open with %s"), info->display_name);
	        temp_button = gtk_button_new_with_label (temp_str);		    
		gtk_box_pack_start (GTK_BOX (index_panel->details->button_box), 
				    temp_button, 
				    FALSE, FALSE, 
				    0);

		temp_str = g_strdup_printf("'%s'", 
		             nautilus_str_has_prefix (index_panel->details->uri, "file://") ?
			     index_panel->details->uri + 7 : index_panel->details->uri);

		command_string = g_strdup_printf (info->command_string, temp_str); 		
		g_free(temp_str);

		nautilus_gtk_signal_connect_free_data 
			(GTK_OBJECT (temp_button), "clicked",
			 GTK_SIGNAL_FUNC (command_button_callback), command_string);
                gtk_object_set_user_data (GTK_OBJECT (temp_button), index_panel);
		
		gtk_widget_show (temp_button);
	}

	/* Catch-all button after all the others. */
	temp_button = gtk_button_new_with_label (_("Open with ..."));
	gtk_signal_connect (GTK_OBJECT (temp_button),  "clicked",
			    open_with_callback, NULL);
	gtk_object_set_user_data (GTK_OBJECT (temp_button), index_panel);
	gtk_widget_show (temp_button);
	gtk_box_pack_start (GTK_BOX (index_panel->details->button_box),
			    temp_button, FALSE, FALSE, 0);
}

/* here's where we set up the command buttons, based on the mime-type of the associated URL */
/* FIXME bugzilla.eazel.com 596:  eventually, we need a way to 
 * override/augment the type from info in the metadata.
 */
void
nautilus_index_panel_update_buttons (NautilusIndexPanel *index_panel)
{
	NautilusFile *file;
	GList *command_list;
	char *mime_type;
	
	/* dispose any existing buttons */
	if (index_panel->details->has_buttons) {
		gtk_container_remove (GTK_CONTAINER (index_panel->details->container),
				      GTK_WIDGET (index_panel->details->button_box_centerer)); 
		make_button_box (index_panel);
	}
	
	/* allocate a file object and fetch the associated mime-type */
	
	file = nautilus_file_get (index_panel->details->uri);
	if (file != NULL) {
		mime_type = nautilus_file_get_mime_type (file);
	
		/* generate a command list from the mime-type */
		if (mime_type != NULL) {
			command_list = nautilus_mime_type_get_commands (mime_type);
			g_free (mime_type);

			/* install a button for each command in the list */
			if (command_list != NULL) {
				add_command_buttons (index_panel, command_list);
			        nautilus_mime_type_dispose_list (command_list);
			
				if (index_panel->details->selected_index != -1)
					gtk_widget_hide (GTK_WIDGET (index_panel->details->button_box));
			}
		}

		nautilus_file_unref (file);	
	}
}

/* this routine populates the index panel with the per-uri information */

void
nautilus_index_panel_update_info (NautilusIndexPanel *index_panel,
				  const char* initial_title)
{
	NautilusDirectory *directory;
	NautilusBackground *background;
	char *background_color, *color_spec;
	char *background_image;
	
	directory = nautilus_directory_get (index_panel->details->uri);
	nautilus_directory_unref (index_panel->details->directory);
	index_panel->details->directory = directory;
	
	/* Connect the background changed signal to code that writes the color. */
	background = nautilus_get_widget_background (GTK_WIDGET (index_panel));
        if (index_panel->details->background_connection == 0) {
		index_panel->details->background_connection =
			gtk_signal_connect_object (GTK_OBJECT (background),
						   "changed",
						   nautilus_index_panel_background_changed,
						   GTK_OBJECT (index_panel));
	}

	/* Set up the background color and image from the metadata. */
	background_color = nautilus_directory_get_metadata (directory,
							    NAUTILUS_METADATA_KEY_SIDEBAR_BACKGROUND_COLOR,
							    DEFAULT_BACKGROUND_COLOR);
	background_image = nautilus_directory_get_metadata (directory,
							    NAUTILUS_METADATA_KEY_SIDEBAR_BACKGROUND_IMAGE,
							    NULL);
	
	nautilus_background_set_color (background, background_color);	
	g_free (background_color);
	
	nautilus_background_set_tile_image_uri (background, background_image);
	g_free (background_image);
	
	/* set up the color for the tabs */
	color_spec = nautilus_directory_get_metadata (directory,
							    NAUTILUS_METADATA_KEY_SIDEBAR_TAB_COLOR,
							    DEFAULT_TAB_COLOR);
	nautilus_index_tabs_set_color(index_panel->details->index_tabs, color_spec);
	g_free (color_spec);

	color_spec = nautilus_directory_get_metadata (directory,
							    NAUTILUS_METADATA_KEY_SIDEBAR_TITLE_TAB_COLOR,
							    DEFAULT_TAB_COLOR);
	nautilus_index_tabs_set_color(index_panel->details->title_tab, color_spec);
	g_free (color_spec);

	
	/* tell the title widget about it */
	nautilus_index_title_set_uri (index_panel->details->title,
		                      index_panel->details->uri,
				      initial_title);
	
	/* set up the command buttons */
	nautilus_index_panel_update_buttons (index_panel);
}

/* here is the key routine that populates the index panel with the appropriate information when the uri changes */

void
nautilus_index_panel_set_uri (NautilusIndexPanel *index_panel, 
			      const char* new_uri,
			      const char* initial_title)
{       
	/* there's nothing to do if the uri is the same as the current one */ 
	if (nautilus_strcmp (index_panel->details->uri, new_uri) == 0) {
		return;
	}
	
	g_free (index_panel->details->uri);
	index_panel->details->uri = g_strdup (new_uri);
		
	/* populate the per-uri box with the info */
	nautilus_index_panel_update_info (index_panel, initial_title);  	
}

void
nautilus_index_panel_set_title (NautilusIndexPanel *index_panel, const char* new_title)
{       
	nautilus_index_title_set_text (index_panel->details->title,
				       new_title);
}

/* we override size allocate so we can remember our size when it changes, since the paned widget
   doesn't generate a signal */
   
static void
nautilus_index_panel_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
{
	NautilusIndexPanel *index_panel = NAUTILUS_INDEX_PANEL(widget);
	
	NAUTILUS_CALL_PARENT_CLASS (GTK_WIDGET_CLASS, size_allocate, (widget, allocation));

	/* remember the size if it changed */
	
	if (widget->allocation.width != index_panel->details->old_width) {
		index_panel->details->old_width = widget->allocation.width;
 		nautilus_preferences_set_enum(NAUTILUS_PREFERENCES_SIDEBAR_WIDTH, widget->allocation.width);
	
	}	
}