gtkpathbar.c 39.9 KB
Newer Older
1
/* gtkpathbar.c
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 * Copyright (C) 2004  Red Hat, Inc.,  Jonathan Blandford <jrb@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

20
#include <config.h>
21
#include <string.h>
22
#include "gtkalias.h"
23
24
#include "gtkpathbar.h"
#include "gtktogglebutton.h"
25
#include "gtkalignment.h"
26
#include "gtkarrow.h"
Federico Mena Quintero's avatar
Federico Mena Quintero committed
27
#include "gtkdnd.h"
28
29
#include "gtkimage.h"
#include "gtkintl.h"
30
31
#include "gtkicontheme.h"
#include "gtkiconfactory.h"
32
#include "gtklabel.h"
33
#include "gtkhbox.h"
34
35
36
37
38
39
40
41
#include "gtkmain.h"
#include "gtkmarshalers.h"

enum {
  PATH_CLICKED,
  LAST_SIGNAL
};

42
43
44
45
typedef enum {
  NORMAL_BUTTON,
  ROOT_BUTTON,
  HOME_BUTTON,
46
  DESKTOP_BUTTON
47
48
} ButtonType;

49
50
#define BUTTON_DATA(x) ((ButtonData *)(x))

51
52
53
#define SCROLL_TIMEOUT           150
#define INITIAL_SCROLL_TIMEOUT   300

54
55
static guint path_bar_signals [LAST_SIGNAL] = { 0 };

56
57
/* Icon size for if we can't get it from the theme */
#define FALLBACK_ICON_SIZE 20
58

59
60
61
62
63
64
65
66
67
68
typedef struct _ButtonData ButtonData;

struct _ButtonData
{
  GtkWidget *button;
  ButtonType type;
  char *dir_name;
  GtkFilePath *path;
  GtkWidget *image;
  GtkWidget *label;
Matthias Clasen's avatar
Matthias Clasen committed
69
70
  guint ignore_changes : 1;
  guint file_is_hidden : 1;
71
};
72
73
74
75
76

G_DEFINE_TYPE (GtkPathBar,
	       gtk_path_bar,
	       GTK_TYPE_CONTAINER);

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
static void gtk_path_bar_finalize                 (GObject          *object);
static void gtk_path_bar_dispose                  (GObject          *object);
static void gtk_path_bar_size_request             (GtkWidget        *widget,
						   GtkRequisition   *requisition);
static void gtk_path_bar_size_allocate            (GtkWidget        *widget,
						   GtkAllocation    *allocation);
static void gtk_path_bar_add                      (GtkContainer     *container,
						   GtkWidget        *widget);
static void gtk_path_bar_remove                   (GtkContainer     *container,
						   GtkWidget        *widget);
static void gtk_path_bar_forall                   (GtkContainer     *container,
						   gboolean          include_internals,
						   GtkCallback       callback,
						   gpointer          callback_data);
static void gtk_path_bar_scroll_up                (GtkWidget        *button,
						   GtkPathBar       *path_bar);
static void gtk_path_bar_scroll_down              (GtkWidget        *button,
						   GtkPathBar       *path_bar);
95
96
97
98
99
100
101
102
103
104
static gboolean gtk_path_bar_slider_button_press  (GtkWidget        *widget,
						   GdkEventButton   *event,
						   GtkPathBar       *path_bar);
static gboolean gtk_path_bar_slider_button_release(GtkWidget        *widget,
						   GdkEventButton   *event,
						   GtkPathBar       *path_bar);
static void gtk_path_bar_grab_notify              (GtkWidget        *widget,
						   gboolean          was_grabbed);
static void gtk_path_bar_state_changed            (GtkWidget        *widget,
						   GtkStateType      previous_state);
105
106
107
108
109
110
111
112
static void gtk_path_bar_style_set                (GtkWidget        *widget,
						   GtkStyle         *previous_style);
static void gtk_path_bar_screen_changed           (GtkWidget        *widget,
						   GdkScreen        *previous_screen);
static void gtk_path_bar_check_icon_theme         (GtkPathBar       *path_bar);
static void gtk_path_bar_update_button_appearance (GtkPathBar       *path_bar,
						   ButtonData       *button_data,
						   gboolean          current_dir);
113
114

static GtkWidget *
115
116
get_slider_button (GtkPathBar  *path_bar,
		   GtkArrowType arrow_type)
117
118
{
  GtkWidget *button;
119
120
121

  gtk_widget_push_composite_child ();

122
  button = gtk_button_new ();
123
  gtk_container_add (GTK_CONTAINER (button), gtk_arrow_new (arrow_type, GTK_SHADOW_OUT));
124
125
126
  gtk_container_add (GTK_CONTAINER (path_bar), button);
  gtk_widget_show_all (button);

127
128
  gtk_widget_pop_composite_child ();

129
130
131
132
133
134
135
136
137
138
  return button;
}

static void
gtk_path_bar_init (GtkPathBar *path_bar)
{
  GTK_WIDGET_SET_FLAGS (path_bar, GTK_NO_WINDOW);
  gtk_widget_set_redraw_on_allocate (GTK_WIDGET (path_bar), FALSE);

  path_bar->spacing = 3;
139
140
  path_bar->up_slider_button = get_slider_button (path_bar, GTK_ARROW_LEFT);
  path_bar->down_slider_button = get_slider_button (path_bar, GTK_ARROW_RIGHT);
141
  path_bar->icon_size = FALLBACK_ICON_SIZE;
142
  
143
144
  g_signal_connect (path_bar->up_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_up), path_bar);
  g_signal_connect (path_bar->down_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_down), path_bar);
145
146
147
148
149

  g_signal_connect (path_bar->up_slider_button, "button_press_event", G_CALLBACK (gtk_path_bar_slider_button_press), path_bar);
  g_signal_connect (path_bar->up_slider_button, "button_release_event", G_CALLBACK (gtk_path_bar_slider_button_release), path_bar);
  g_signal_connect (path_bar->down_slider_button, "button_press_event", G_CALLBACK (gtk_path_bar_slider_button_press), path_bar);
  g_signal_connect (path_bar->down_slider_button, "button_release_event", G_CALLBACK (gtk_path_bar_slider_button_release), path_bar);
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
}

static void
gtk_path_bar_class_init (GtkPathBarClass *path_bar_class)
{
  GObjectClass *gobject_class;
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

  gobject_class = (GObjectClass *) path_bar_class;
  object_class = (GtkObjectClass *) path_bar_class;
  widget_class = (GtkWidgetClass *) path_bar_class;
  container_class = (GtkContainerClass *) path_bar_class;

165
  gobject_class->finalize = gtk_path_bar_finalize;
166
  gobject_class->dispose = gtk_path_bar_dispose;
167
168
169

  widget_class->size_request = gtk_path_bar_size_request;
  widget_class->size_allocate = gtk_path_bar_size_allocate;
170
171
  widget_class->style_set = gtk_path_bar_style_set;
  widget_class->screen_changed = gtk_path_bar_screen_changed;
172
173
  widget_class->grab_notify = gtk_path_bar_grab_notify;
  widget_class->state_changed = gtk_path_bar_state_changed;
174
175
176
177

  container_class->add = gtk_path_bar_add;
  container_class->forall = gtk_path_bar_forall;
  container_class->remove = gtk_path_bar_remove;
178
179
  /* FIXME: */
  /*  container_class->child_type = gtk_path_bar_child_type;*/
180
181

  path_bar_signals [PATH_CLICKED] =
Federico Mena Quintero's avatar
Federico Mena Quintero committed
182
    g_signal_new ("path-clicked",
183
184
185
186
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkPathBarClass, path_clicked),
		  NULL, NULL,
Federico Mena Quintero's avatar
Federico Mena Quintero committed
187
188
189
190
		  _gtk_marshal_VOID__POINTER_BOOLEAN,
		  G_TYPE_NONE, 2,
		  G_TYPE_POINTER,
		  G_TYPE_BOOLEAN);
191
192
193
194
}


static void
195
gtk_path_bar_finalize (GObject *object)
196
197
198
199
{
  GtkPathBar *path_bar;

  path_bar = GTK_PATH_BAR (object);
200
  g_list_free (path_bar->button_list);
201
202
  if (path_bar->root_path)
    gtk_file_path_free (path_bar->root_path);
203
204
205
206
207
  if (path_bar->home_path)
    gtk_file_path_free (path_bar->home_path);
  if (path_bar->desktop_path)
    gtk_file_path_free (path_bar->desktop_path);

208
  if (path_bar->root_icon)
Jonathan Blandford's avatar
Jonathan Blandford committed
209
    g_object_unref (path_bar->root_icon);
210
211
212
213
214
  if (path_bar->home_icon)
    g_object_unref (path_bar->home_icon);
  if (path_bar->desktop_icon)
    g_object_unref (path_bar->desktop_icon);

215
216
  if (path_bar->file_system)
    g_object_unref (path_bar->file_system);
217

218
  G_OBJECT_CLASS (gtk_path_bar_parent_class)->finalize (object);
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
/* Removes the settings signal handler.  It's safe to call multiple times */
static void
remove_settings_signal (GtkPathBar *path_bar,
			GdkScreen  *screen)
{
  if (path_bar->settings_signal_id)
    {
      GtkSettings *settings;

      settings = gtk_settings_get_for_screen (screen);
      g_signal_handler_disconnect (settings,
				   path_bar->settings_signal_id);
      path_bar->settings_signal_id = 0;
    }
}

static void
gtk_path_bar_dispose (GObject *object)
{
  remove_settings_signal (GTK_PATH_BAR (object),
			  gtk_widget_get_screen (GTK_WIDGET (object)));

  G_OBJECT_CLASS (gtk_path_bar_parent_class)->dispose (object);
}

246
247
248
249
250
251
252
253
254
/* Size requisition:
 * 
 * Ideally, our size is determined by another widget, and we are just filling
 * available space.
 */
static void
gtk_path_bar_size_request (GtkWidget      *widget,
			   GtkRequisition *requisition)
{
255
  ButtonData *button_data;
256
257
258
259
260
261
262
263
264
265
266
  GtkPathBar *path_bar;
  GtkRequisition child_requisition;
  GList *list;

  path_bar = GTK_PATH_BAR (widget);

  requisition->width = 0;
  requisition->height = 0;

  for (list = path_bar->button_list; list; list = list->next)
    {
267
268
      button_data = BUTTON_DATA (list->data);
      gtk_widget_size_request (button_data->button, &child_requisition);
269
270
271
272
273
274
275
276
      requisition->width = MAX (child_requisition.width, requisition->width);
      requisition->height = MAX (child_requisition.height, requisition->height);
    }

  /* Add space for slider, if we have more than one path */
  /* Theoretically, the slider could be bigger than the other button.  But we're
   * not going to worry about that now.
   */
277
  path_bar->slider_width = MIN(requisition->height * 2 / 3 + 5, requisition->height);
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
  if (path_bar->button_list && path_bar->button_list->next != NULL)
    requisition->width += (path_bar->spacing + path_bar->slider_width) * 2;

  gtk_widget_size_request (path_bar->up_slider_button, &child_requisition);
  gtk_widget_size_request (path_bar->down_slider_button, &child_requisition);

  requisition->width += GTK_CONTAINER (widget)->border_width * 2;
  requisition->height += GTK_CONTAINER (widget)->border_width * 2;

  widget->requisition = *requisition;
}

static void
gtk_path_bar_update_slider_buttons (GtkPathBar *path_bar)
{
  if (path_bar->button_list)
    {
      GtkWidget *button;

297
      button = BUTTON_DATA (path_bar->button_list->data)->button;
298
299
300
301
302
      if (gtk_widget_get_child_visible (button))
	gtk_widget_set_sensitive (path_bar->down_slider_button, FALSE);
      else
	gtk_widget_set_sensitive (path_bar->down_slider_button, TRUE);

303
      button = BUTTON_DATA (g_list_last (path_bar->button_list)->data)->button;
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
      if (gtk_widget_get_child_visible (button))
	gtk_widget_set_sensitive (path_bar->up_slider_button, FALSE);
      else
	gtk_widget_set_sensitive (path_bar->up_slider_button, TRUE);
    }
}

/* This is a tad complicated
 */
static void
gtk_path_bar_size_allocate (GtkWidget     *widget,
			    GtkAllocation *allocation)
{
  GtkWidget *child;
  GtkPathBar *path_bar = GTK_PATH_BAR (widget);
  GtkTextDirection direction;
  GtkAllocation child_allocation;
  GList *list, *first_button;
  gint width;
  gint allocation_width;
  gint border_width;
  gboolean need_sliders = FALSE;
  gint up_slider_offset = 0;
  gint down_slider_offset = 0;

  widget->allocation = *allocation;

  /* No path is set; we don't have to allocate anything. */
  if (path_bar->button_list == NULL)
    return;

  direction = gtk_widget_get_direction (widget);
  border_width = (gint) GTK_CONTAINER (path_bar)->border_width;
  allocation_width = allocation->width - 2 * border_width;

  /* First, we check to see if we need the scrollbars. */
340
  width = BUTTON_DATA (path_bar->button_list->data)->button->requisition.width;
341
342
  for (list = path_bar->button_list->next; list; list = list->next)
    {
343
      child = BUTTON_DATA (list->data)->button;
344
345
346
347
348
349
350
351
352
353
354
355
356

      width += child->requisition.width + path_bar->spacing;
    }

  if (width <= allocation_width)
    {
      first_button = g_list_last (path_bar->button_list);
    }
  else
    {
      gboolean reached_end = FALSE;
      gint slider_space = 2 * (path_bar->spacing + path_bar->slider_width);

357
358
359
360
      if (path_bar->first_scrolled_button)
	first_button = path_bar->first_scrolled_button;
      else
	first_button = path_bar->button_list;
361
362
363
364
365
366
367
      need_sliders = TRUE;
      
      /* To see how much space we have, and how many buttons we can display.
       * We start at the first button, count forward until hit the new
       * button, then count backwards.
       */
      /* Count down the path chain towards the end. */
368
      width = BUTTON_DATA (first_button->data)->button->requisition.width;
369
370
371
      list = first_button->prev;
      while (list && !reached_end)
	{
372
	  child = BUTTON_DATA (list->data)->button;
373

374
375
	  if (width + child->requisition.width +
	      path_bar->spacing + slider_space > allocation_width)
376
377
378
379
380
381
382
383
384
385
386
	    reached_end = TRUE;
	  else
	    width += child->requisition.width + path_bar->spacing;

	  list = list->prev;
	}

      /* Finally, we walk up, seeing how many of the previous buttons we can
       * add */
      while (first_button->next && ! reached_end)
	{
387
	  child = BUTTON_DATA (first_button->next->data)->button;
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

	  if (width + child->requisition.width + path_bar->spacing + slider_space > allocation_width)
	    {
	      reached_end = TRUE;
	    }
	  else
	    {
	      width += child->requisition.width + path_bar->spacing;
	      first_button = first_button->next;
	    }
	}
    }

  /* Now, we allocate space to the buttons */
  child_allocation.y = allocation->y + border_width;
  child_allocation.height = MAX (1, (gint) allocation->height - border_width * 2);

  if (direction == GTK_TEXT_DIR_RTL)
    {
      child_allocation.x = allocation->x + allocation->width - border_width;
      if (need_sliders)
	{
	  child_allocation.x -= (path_bar->spacing + path_bar->slider_width);
	  up_slider_offset = allocation->width - border_width - path_bar->slider_width;
	}
    }
  else
    {
      child_allocation.x = allocation->x + border_width;
      if (need_sliders)
	{
	  up_slider_offset = border_width;
	  child_allocation.x += (path_bar->spacing + path_bar->slider_width);
	}
    }

  for (list = first_button; list; list = list->prev)
    {
426
      child = BUTTON_DATA (list->data)->button;
427
428

      child_allocation.width = child->requisition.width;
429
430
      if (direction == GTK_TEXT_DIR_RTL)
	child_allocation.x -= child_allocation.width;
431
432
433
434

      /* Check to see if we've don't have any more space to allocate buttons */
      if (need_sliders && direction == GTK_TEXT_DIR_RTL)
	{
435
	  if (child_allocation.x - path_bar->spacing - path_bar->slider_width < widget->allocation.x + border_width)
436
437
438
439
	    break;
	}
      else if (need_sliders && direction == GTK_TEXT_DIR_LTR)
	{
440
	  if (child_allocation.x + child_allocation.width + path_bar->spacing + path_bar->slider_width >
441
442
443
444
	      widget->allocation.x + border_width + allocation_width)
	    break;
	}

445
      gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, TRUE);
446
447
448
449
450
451
      gtk_widget_size_allocate (child, &child_allocation);

      if (direction == GTK_TEXT_DIR_RTL)
	{
	  child_allocation.x -= path_bar->spacing;
	  down_slider_offset = child_allocation.x - widget->allocation.x - path_bar->slider_width;
452
	  down_slider_offset = border_width;
453
454
455
456
	}
      else
	{
	  down_slider_offset = child_allocation.x - widget->allocation.x;
457
458
	  down_slider_offset = allocation->width - border_width - path_bar->slider_width;
	  child_allocation.x += child_allocation.width + path_bar->spacing;
459
460
461
462
463
	}
    }
  /* Now we go hide all the widgets that don't fit */
  while (list)
    {
464
      gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
465
466
467
468
      list = list->prev;
    }
  for (list = first_button->next; list; list = list->next)
    {
469
      gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
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
    }

  if (need_sliders)
    {
      child_allocation.width = path_bar->slider_width;
      
      child_allocation.x = up_slider_offset + allocation->x;
      gtk_widget_size_allocate (path_bar->up_slider_button, &child_allocation);

      child_allocation.x = down_slider_offset + allocation->x;
      gtk_widget_size_allocate (path_bar->down_slider_button, &child_allocation);

      gtk_widget_set_child_visible (path_bar->up_slider_button, TRUE);
      gtk_widget_set_child_visible (path_bar->down_slider_button, TRUE);
      gtk_widget_show_all (path_bar->up_slider_button);
      gtk_widget_show_all (path_bar->down_slider_button);
      gtk_path_bar_update_slider_buttons (path_bar);
    }
  else
    {
      gtk_widget_set_child_visible (path_bar->up_slider_button, FALSE);
      gtk_widget_set_child_visible (path_bar->down_slider_button, FALSE);
    }
}

495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
static void
gtk_path_bar_style_set (GtkWidget *widget,
			GtkStyle  *previous_style)
{
  if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set)
    GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set (widget, previous_style);

  gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
}

static void
gtk_path_bar_screen_changed (GtkWidget *widget,
			     GdkScreen *previous_screen)
{
  if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed)
    GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed (widget, previous_screen);

  /* We might nave a new settings, so we remove the old one */
  if (previous_screen)
    remove_settings_signal (GTK_PATH_BAR (widget), previous_screen);

  gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
}

519
520
521
522
523
524
525
static void
gtk_path_bar_add (GtkContainer *container,
		  GtkWidget    *widget)
{
  gtk_widget_set_parent (widget, GTK_WIDGET (container));
}

526
527
528
529
530
531
532
533
534
535
static void
gtk_path_bar_remove_1 (GtkContainer *container,
		       GtkWidget    *widget)
{
  gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
  gtk_widget_unparent (widget);
  if (was_visible)
    gtk_widget_queue_resize (GTK_WIDGET (container));
}

536
537
538
539
540
541
542
543
544
static void
gtk_path_bar_remove (GtkContainer *container,
		     GtkWidget    *widget)
{
  GtkPathBar *path_bar;
  GList *children;

  path_bar = GTK_PATH_BAR (container);

545
546
547
548
549
550
551
552
553
554
555
556
557
  if (widget == path_bar->up_slider_button)
    {
      gtk_path_bar_remove_1 (container, widget);
      path_bar->up_slider_button = NULL;
      return;
    }

  if (widget == path_bar->down_slider_button)
    {
      gtk_path_bar_remove_1 (container, widget);
      path_bar->down_slider_button = NULL;
      return;
    }
558

559
  children = path_bar->button_list;
560
561
  while (children)
    {
562
      if (widget == BUTTON_DATA (children->data)->button)
563
	{
564
	  gtk_path_bar_remove_1 (container, widget);
565
566
	  path_bar->button_list = g_list_remove_link (path_bar->button_list, children);
	  g_list_free (children);
567
	  return;
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
	}
      
      children = children->next;
    }
}

static void
gtk_path_bar_forall (GtkContainer *container,
		     gboolean      include_internals,
		     GtkCallback   callback,
		     gpointer      callback_data)
{
  GtkPathBar *path_bar;
  GList *children;

  g_return_if_fail (callback != NULL);
  path_bar = GTK_PATH_BAR (container);

  children = path_bar->button_list;
  while (children)
    {
      GtkWidget *child;
590
      child = BUTTON_DATA (children->data)->button;
591
592
593
594
595
      children = children->next;

      (* callback) (child, callback_data);
    }

596
597
598
599
600
  if (path_bar->up_slider_button)
    (* callback) (path_bar->up_slider_button, callback_data);

  if (path_bar->down_slider_button)
    (* callback) (path_bar->down_slider_button, callback_data);
601
602
}

603
static void
604
gtk_path_bar_scroll_down (GtkWidget *button, GtkPathBar *path_bar)
605
606
{
  GList *list;
607
608
  GList *down_button = NULL;
  GList *up_button = NULL;
609
610
611
612
  gint space_available;
  gint space_needed;
  gint border_width;
  GtkTextDirection direction;
613
614
615
616
617
618
619

  if (path_bar->ignore_click)
    {
      path_bar->ignore_click = FALSE;
      return;   
    }

620
621
  gtk_widget_queue_resize (GTK_WIDGET (path_bar));

622
623
624
625
626
627
628
  border_width = GTK_CONTAINER (path_bar)->border_width;
  direction = gtk_widget_get_direction (GTK_WIDGET (path_bar));
  
  /* We find the button at the 'down' end that we have to make
   * visible */
  for (list = path_bar->button_list; list; list = list->next)
    {
629
      if (list->next && gtk_widget_get_child_visible (BUTTON_DATA (list->next->data)->button))
630
631
632
633
634
635
636
637
638
639
	{
	  down_button = list;
	  break;
	}
    }
  
  /* Find the last visible button on the 'up' end
   */
  for (list = g_list_last (path_bar->button_list); list; list = list->prev)
    {
640
      if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
641
642
643
644
645
646
	{
	  up_button = list;
	  break;
	}
    }

647
  space_needed = BUTTON_DATA (down_button->data)->button->allocation.width + path_bar->spacing;
648
  if (direction == GTK_TEXT_DIR_RTL)
649
    space_available = path_bar->down_slider_button->allocation.x - GTK_WIDGET (path_bar)->allocation.x;
650
651
652
653
654
655
656
657
658
659
  else
    space_available = (GTK_WIDGET (path_bar)->allocation.x + GTK_WIDGET (path_bar)->allocation.width - border_width) -
      (path_bar->down_slider_button->allocation.x + path_bar->down_slider_button->allocation.width);

  /* We have space_available extra space that's not being used.  We
   * need space_needed space to make the button fit.  So we walk down
   * from the end, removing buttons until we get all the space we
   * need. */
  while (space_available < space_needed)
    {
660
      space_available += BUTTON_DATA (up_button->data)->button->allocation.width + path_bar->spacing;
661
662
663
664
665
666
      up_button = up_button->prev;
      path_bar->first_scrolled_button = up_button;
    }
}

static void
667
gtk_path_bar_scroll_up (GtkWidget *button, GtkPathBar *path_bar)
668
669
670
{
  GList *list;

671
672
673
674
675
676
  if (path_bar->ignore_click)
    {
      path_bar->ignore_click = FALSE;
      return;   
    }

677
678
  gtk_widget_queue_resize (GTK_WIDGET (path_bar));

679
680
  for (list = g_list_last (path_bar->button_list); list; list = list->prev)
    {
681
      if (list->prev && gtk_widget_get_child_visible (BUTTON_DATA (list->prev->data)->button))
682
683
684
685
686
687
688
	{
	  path_bar->first_scrolled_button = list;
	  return;
	}
    }
}

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
static gboolean
gtk_path_bar_scroll_timeout (GtkPathBar *path_bar)
{
  gboolean retval = FALSE;

  GDK_THREADS_ENTER ();

  if (path_bar->timer)
    {
      if (GTK_WIDGET_HAS_FOCUS (path_bar->up_slider_button))
	gtk_path_bar_scroll_up (path_bar->up_slider_button, path_bar);
      else if (GTK_WIDGET_HAS_FOCUS (path_bar->down_slider_button))
	gtk_path_bar_scroll_down (path_bar->down_slider_button, path_bar);

      if (path_bar->need_timer) 
	{
	  path_bar->need_timer = FALSE;

	  path_bar->timer = g_timeout_add (SCROLL_TIMEOUT,
					   (GSourceFunc)gtk_path_bar_scroll_timeout,
					   path_bar);
	  
	}
      else
	retval = TRUE;
      
    }

  GDK_THREADS_LEAVE ();

  return retval;
}

static void 
gtk_path_bar_stop_scrolling (GtkPathBar *path_bar)
{
  if (path_bar->timer)
    {
      g_source_remove (path_bar->timer);
      path_bar->timer = 0;
      path_bar->need_timer = FALSE;
    }
}

static gboolean
gtk_path_bar_slider_button_press (GtkWidget      *widget, 
				  GdkEventButton *event,
				  GtkPathBar     *path_bar)
{
  if (!GTK_WIDGET_HAS_FOCUS (widget))
    gtk_widget_grab_focus (widget);

  if (event->type != GDK_BUTTON_PRESS || event->button != 1)
    return FALSE;

  path_bar->ignore_click = FALSE;

  if (widget == path_bar->up_slider_button)
    gtk_path_bar_scroll_up (path_bar->up_slider_button, path_bar);
  else if (widget == path_bar->down_slider_button)
     gtk_path_bar_scroll_down (path_bar->down_slider_button, path_bar);

  if (!path_bar->timer)
    {
      path_bar->need_timer = TRUE;
      path_bar->timer = g_timeout_add (INITIAL_SCROLL_TIMEOUT,
				       (GSourceFunc)gtk_path_bar_scroll_timeout,
				       path_bar);
    }

  return FALSE;
}

static gboolean
gtk_path_bar_slider_button_release (GtkWidget      *widget, 
				    GdkEventButton *event,
				    GtkPathBar     *path_bar)
{
  if (event->type != GDK_BUTTON_RELEASE)
    return FALSE;

  path_bar->ignore_click = TRUE;
  gtk_path_bar_stop_scrolling (path_bar);

  return FALSE;
}

static void
gtk_path_bar_grab_notify (GtkWidget *widget,
			  gboolean   was_grabbed)
{
  if (!was_grabbed)
    gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
}

static void
gtk_path_bar_state_changed (GtkWidget    *widget,
			    GtkStateType  previous_state)
{
  if (!GTK_WIDGET_IS_SENSITIVE (widget)) 
    gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
}


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
/* Changes the icons wherever it is needed */
static void
reload_icons (GtkPathBar *path_bar)
{
  GList *list;

  if (path_bar->root_icon)
    {
      g_object_unref (path_bar->root_icon);
      path_bar->root_icon = NULL;
    }
  if (path_bar->home_icon)
    {
      g_object_unref (path_bar->home_icon);
      path_bar->home_icon = NULL;
    }
  if (path_bar->desktop_icon)
    {
      g_object_unref (path_bar->desktop_icon);
      path_bar->desktop_icon = NULL;
    }

  for (list = path_bar->button_list; list; list = list->next)
    {
      ButtonData *button_data;
      gboolean current_dir;

      button_data = BUTTON_DATA (list->data);
      if (button_data->type != NORMAL_BUTTON)
	{
	  current_dir = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button));
	  gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
	}
    }
  
}

static void
change_icon_theme (GtkPathBar *path_bar)
{
  GtkSettings *settings;
  gint width, height;

  settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));

  if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_BUTTON, &width, &height))
    path_bar->icon_size = MAX (width, height);
  else
    path_bar->icon_size = FALLBACK_ICON_SIZE;

  reload_icons (path_bar);
}
/* Callback used when a GtkSettings value changes */
static void
settings_notify_cb (GObject    *object,
		    GParamSpec *pspec,
		    GtkPathBar *path_bar)
{
  const char *name;

  name = g_param_spec_get_name (pspec);

  if (! strcmp (name, "gtk-icon-theme-name") ||
      ! strcmp (name, "gtk-icon-sizes"))
    change_icon_theme (path_bar);
}

static void
gtk_path_bar_check_icon_theme (GtkPathBar *path_bar)
{
  GtkSettings *settings;

  if (path_bar->settings_signal_id)
    return;

  settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
  path_bar->settings_signal_id = g_signal_connect (settings, "notify", G_CALLBACK (settings_notify_cb), path_bar);

  change_icon_theme (path_bar);
}

/* Public functions and their helpers */
875
876
877
878
879
static void
gtk_path_bar_clear_buttons (GtkPathBar *path_bar)
{
  while (path_bar->button_list != NULL)
    {
880
      gtk_container_remove (GTK_CONTAINER (path_bar), BUTTON_DATA (path_bar->button_list->data)->button);
881
    }
882
  path_bar->first_scrolled_button = NULL;
883
884
}

885
886
887
888
static void
button_clicked_cb (GtkWidget *button,
		   gpointer   data)
{
889
  ButtonData *button_data;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
890
891
892
  GtkPathBar *path_bar;
  GList *button_list;
  gboolean child_is_hidden;
893
894
895
896

  button_data = BUTTON_DATA (data);
  if (button_data->ignore_changes)
    return;
897

Federico Mena Quintero's avatar
Federico Mena Quintero committed
898
899
900
901
902
  path_bar = GTK_PATH_BAR (button->parent);

  button_list = g_list_find (path_bar->button_list, button_data);
  g_assert (button_list != NULL);

903
904
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);

Federico Mena Quintero's avatar
Federico Mena Quintero committed
905
906
907
908
909
910
911
912
913
914
915
  if (button_list->prev)
    {
      ButtonData *child_data;

      child_data = BUTTON_DATA (button_list->prev->data);
      child_is_hidden = child_data->file_is_hidden;
    }
  else
    child_is_hidden = FALSE;

  g_signal_emit (path_bar, path_bar_signals [PATH_CLICKED], 0, button_data->path, child_is_hidden);
916
917
}

918
919
920
921
static GdkPixbuf *
get_button_image (GtkPathBar *path_bar,
		  ButtonType  button_type)
{
922
923
924
  GtkFileSystemVolume *volume;

  switch (button_type)
925
    {
926
    case ROOT_BUTTON:
927
928
929
930
931
932
933
934
935
936
937

      if (path_bar->root_icon != NULL)
	return path_bar->root_icon;
      
      volume = gtk_file_system_get_volume_for_path (path_bar->file_system, path_bar->root_path);
      if (volume == NULL)
	return NULL;

      path_bar->root_icon = gtk_file_system_volume_render_icon (path_bar->file_system,
								volume,
								GTK_WIDGET (path_bar),
938
								path_bar->icon_size,
939
940
941
942
								NULL);
      gtk_file_system_volume_free (path_bar->file_system, volume);

      return path_bar->root_icon;
943
    case HOME_BUTTON:
944
945
946
947
948
949
      if (path_bar->home_icon != NULL)
	return path_bar->home_icon;

      path_bar->home_icon = gtk_file_system_render_icon (path_bar->file_system,
							 path_bar->home_path,
							 GTK_WIDGET (path_bar),
950
951
							 path_bar->icon_size,
							 NULL);
952
      return path_bar->home_icon;
953
954
955
956
957
958
959
    case DESKTOP_BUTTON:
      if (path_bar->desktop_icon != NULL)
	return path_bar->desktop_icon;

      path_bar->desktop_icon = gtk_file_system_render_icon (path_bar->file_system,
							    path_bar->desktop_path,
							    GTK_WIDGET (path_bar),
960
961
							    path_bar->icon_size,
							    NULL);
962
963
964
      return path_bar->desktop_icon;
    default:
      return NULL;
965
966
967
968
969
    }
  
  return NULL;
}

970
971
972
973
974
975
976
977
static void
button_data_free (ButtonData *button_data)
{
  gtk_file_path_free (button_data->path);
  g_free (button_data->dir_name);
  g_free (button_data);
}

978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
static const char *
get_dir_name (ButtonData *button_data)
{
  if (button_data->type == HOME_BUTTON)
    return _("Home");
  else if (button_data->type == DESKTOP_BUTTON)
    return _("Desktop");
  else
    return button_data->dir_name;

}

/* We always want to request the same size for the label, whether
 * or not the contents are bold
 */
static void
label_size_request_cb (GtkWidget      *widget,
		       GtkRequisition *requisition,
		       ButtonData     *button_data)
{
  const gchar *dir_name = get_dir_name (button_data);
  PangoLayout *layout = gtk_widget_create_pango_layout (button_data->label, dir_name);
  gint bold_width, bold_height;