gtkpathbar.c 51.7 KB
Newer Older
1
/* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2
/* gtkpathbar.c
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.
 */

21
#include "config.h"
22

23
#include "gtkpathbar.h"
24 25 26

#include <string.h>

27
#include "gtktogglebutton.h"
28
#include "gtkalignment.h"
29
#include "gtkarrow.h"
30
#include "gtkdnd.h"
31 32
#include "gtkimage.h"
#include "gtkintl.h"
33 34
#include "gtkicontheme.h"
#include "gtkiconfactory.h"
35
#include "gtklabel.h"
36
#include "gtkhbox.h"
37 38
#include "gtkmain.h"
#include "gtkmarshalers.h"
39

40 41 42 43 44 45

enum {
  PATH_CLICKED,
  LAST_SIGNAL
};

46 47 48 49
typedef enum {
  NORMAL_BUTTON,
  ROOT_BUTTON,
  HOME_BUTTON,
50
  DESKTOP_BUTTON
51 52
} ButtonType;

53 54
#define BUTTON_DATA(x) ((ButtonData *)(x))

55
#define SCROLL_DELAY_FACTOR 5
56

57 58
static guint path_bar_signals [LAST_SIGNAL] = { 0 };

59
/* Icon size for if we can't get it from the theme */
60
#define FALLBACK_ICON_SIZE 16
61

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

struct _ButtonData
{
  GtkWidget *button;
  ButtonType type;
  char *dir_name;
69
  GFile *file;
70 71
  GtkWidget *image;
  GtkWidget *label;
72
  GCancellable *cancellable;
73 74
  guint ignore_changes : 1;
  guint file_is_hidden : 1;
75
};
76 77 78 79 80
/* This macro is used to check if a button can be used as a fake root.
 * All buttons in front of a fake root are automatically hidden when in a
 * directory below a fake root and replaced with the "<" arrow button.
 */
#define BUTTON_IS_FAKE_ROOT(button) ((button)->type == HOME_BUTTON)
81

Matthias Clasen's avatar
Matthias Clasen committed
82
G_DEFINE_TYPE (GtkPathBar, gtk_path_bar, GTK_TYPE_CONTAINER)
83

84 85
static void gtk_path_bar_finalize                 (GObject          *object);
static void gtk_path_bar_dispose                  (GObject          *object);
86 87
static void gtk_path_bar_realize                  (GtkWidget        *widget);
static void gtk_path_bar_unrealize                (GtkWidget        *widget);
88 89
static void gtk_path_bar_size_request             (GtkWidget        *widget,
						   GtkRequisition   *requisition);
90 91
static void gtk_path_bar_map                      (GtkWidget        *widget);
static void gtk_path_bar_unmap                    (GtkWidget        *widget);
92 93 94 95 96 97 98 99 100 101
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);
102 103 104 105
static gboolean gtk_path_bar_scroll               (GtkWidget        *widget,
						   GdkEventScroll   *event);
static void gtk_path_bar_scroll_up                (GtkPathBar       *path_bar);
static void gtk_path_bar_scroll_down              (GtkPathBar       *path_bar);
106
static void gtk_path_bar_stop_scrolling           (GtkPathBar       *path_bar);
107 108 109 110 111 112
static gboolean gtk_path_bar_slider_up_defocus    (GtkWidget        *widget,
						   GdkEventButton   *event,
						   GtkPathBar       *path_bar);
static gboolean gtk_path_bar_slider_down_defocus  (GtkWidget        *widget,
						   GdkEventButton   *event,
						   GtkPathBar       *path_bar);
113 114 115 116 117 118 119 120 121 122
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);
123 124 125 126 127 128 129 130
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);
131

132 133 134 135 136
static void
on_slider_unmap (GtkWidget  *widget,
		 GtkPathBar *path_bar)
{
  if (path_bar->timer &&
137 138 139
      ((widget == path_bar->up_slider_button && path_bar->scrolling_up) ||
       (widget == path_bar->down_slider_button && path_bar->scrolling_down)))
    gtk_path_bar_stop_scrolling (path_bar);
140 141
}

142
static GtkWidget *
143 144
get_slider_button (GtkPathBar  *path_bar,
		   GtkArrowType arrow_type)
145 146
{
  GtkWidget *button;
147
  AtkObject *atk_obj;
148 149 150

  gtk_widget_push_composite_child ();

151
  button = gtk_button_new ();
152 153 154 155 156 157
  atk_obj = gtk_widget_get_accessible (button);
  if (arrow_type == GTK_ARROW_LEFT)
    atk_object_set_name (atk_obj, _("Up Path"));
  else
    atk_object_set_name (atk_obj, _("Down Path"));

158
  gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
159 160
  gtk_container_add (GTK_CONTAINER (button),
                     gtk_arrow_new (arrow_type, GTK_SHADOW_OUT));
161 162 163
  gtk_container_add (GTK_CONTAINER (path_bar), button);
  gtk_widget_show_all (button);

164 165 166
  g_signal_connect (G_OBJECT (button), "unmap",
		    G_CALLBACK (on_slider_unmap), path_bar);

167 168
  gtk_widget_pop_composite_child ();

169 170 171 172 173 174
  return button;
}

static void
gtk_path_bar_init (GtkPathBar *path_bar)
{
175
  gtk_widget_set_has_window (GTK_WIDGET (path_bar), FALSE);
176 177
  gtk_widget_set_redraw_on_allocate (GTK_WIDGET (path_bar), FALSE);

178
  path_bar->get_info_cancellable = NULL;
179

180
  path_bar->spacing = 0;
181 182
  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);
183
  path_bar->icon_size = FALLBACK_ICON_SIZE;
184
  
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  g_signal_connect_swapped (path_bar->up_slider_button, "clicked",
                            G_CALLBACK (gtk_path_bar_scroll_up), path_bar);
  g_signal_connect_swapped (path_bar->down_slider_button, "clicked",
                            G_CALLBACK (gtk_path_bar_scroll_down), path_bar);

  g_signal_connect (path_bar->up_slider_button, "focus-out-event",
                    G_CALLBACK (gtk_path_bar_slider_up_defocus), path_bar);
  g_signal_connect (path_bar->down_slider_button, "focus-out-event",
                    G_CALLBACK (gtk_path_bar_slider_down_defocus), path_bar);

  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);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
}

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;

218
  gobject_class->finalize = gtk_path_bar_finalize;
219
  gobject_class->dispose = gtk_path_bar_dispose;
220 221

  widget_class->size_request = gtk_path_bar_size_request;
222 223 224
  widget_class->realize = gtk_path_bar_realize;
  widget_class->unrealize = gtk_path_bar_unrealize;
  widget_class->map = gtk_path_bar_map;
225
  widget_class->unmap = gtk_path_bar_unmap;
226
  widget_class->size_allocate = gtk_path_bar_size_allocate;
227 228
  widget_class->style_set = gtk_path_bar_style_set;
  widget_class->screen_changed = gtk_path_bar_screen_changed;
229 230
  widget_class->grab_notify = gtk_path_bar_grab_notify;
  widget_class->state_changed = gtk_path_bar_state_changed;
231
  widget_class->scroll_event = gtk_path_bar_scroll;
232 233 234 235

  container_class->add = gtk_path_bar_add;
  container_class->forall = gtk_path_bar_forall;
  container_class->remove = gtk_path_bar_remove;
236 237
  /* FIXME: */
  /*  container_class->child_type = gtk_path_bar_child_type;*/
238 239

  path_bar_signals [PATH_CLICKED] =
240
    g_signal_new (I_("path-clicked"),
241 242 243 244
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkPathBarClass, path_clicked),
		  NULL, NULL,
245 246 247
		  _gtk_marshal_VOID__POINTER_POINTER_BOOLEAN,
		  G_TYPE_NONE, 3,
		  G_TYPE_POINTER,
Federico Mena Quintero's avatar
Federico Mena Quintero committed
248 249
		  G_TYPE_POINTER,
		  G_TYPE_BOOLEAN);
250 251 252 253
}


static void
254
gtk_path_bar_finalize (GObject *object)
255 256 257 258
{
  GtkPathBar *path_bar;

  path_bar = GTK_PATH_BAR (object);
259 260 261

  gtk_path_bar_stop_scrolling (path_bar);

262
  g_list_free (path_bar->button_list);
263 264 265 266 267 268
  if (path_bar->root_file)
    g_object_unref (path_bar->root_file);
  if (path_bar->home_file)
    g_object_unref (path_bar->home_file);
  if (path_bar->desktop_file)
    g_object_unref (path_bar->desktop_file);
269

270
  if (path_bar->root_icon)
271
    g_object_unref (path_bar->root_icon);
272 273 274 275 276
  if (path_bar->home_icon)
    g_object_unref (path_bar->home_icon);
  if (path_bar->desktop_icon)
    g_object_unref (path_bar->desktop_icon);

277 278
  if (path_bar->file_system)
    g_object_unref (path_bar->file_system);
279

280
  G_OBJECT_CLASS (gtk_path_bar_parent_class)->finalize (object);
281 282
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
/* 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)
{
302 303 304 305
  GtkPathBar *path_bar = GTK_PATH_BAR (object);

  remove_settings_signal (path_bar, gtk_widget_get_screen (GTK_WIDGET (object)));

306 307 308
  if (path_bar->get_info_cancellable)
    g_cancellable_cancel (path_bar->get_info_cancellable);
  path_bar->get_info_cancellable = NULL;
309 310 311 312

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

313 314 315 316 317 318 319 320 321
/* 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)
{
322
  ButtonData *button_data;
323 324 325
  GtkPathBar *path_bar;
  GtkRequisition child_requisition;
  GList *list;
326
  guint border_width;
327 328 329 330 331 332 333 334

  path_bar = GTK_PATH_BAR (widget);

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

  for (list = path_bar->button_list; list; list = list->next)
    {
335
      button_data = BUTTON_DATA (list->data);
336 337
      gtk_widget_get_preferred_size (button_data->button,
                                     &child_requisition, NULL);
338

339 340 341 342 343 344
      if (button_data->type == NORMAL_BUTTON)
	/* Use 2*Height as button width because of ellipsized label.  */
	requisition->width = MAX (child_requisition.height * 2, requisition->width);
      else
	requisition->width = MAX (child_requisition.width, requisition->width);

345 346 347 348 349 350 351
      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.
   */
352
  path_bar->slider_width = MIN(requisition->height * 2 / 3 + 5, requisition->height);
353 354 355
  if (path_bar->button_list && path_bar->button_list->next != NULL)
    requisition->width += (path_bar->spacing + path_bar->slider_width) * 2;

356 357 358 359
  gtk_widget_get_preferred_size (path_bar->up_slider_button,
                                 &child_requisition, NULL);
  gtk_widget_get_preferred_size (path_bar->down_slider_button,
                                 &child_requisition, NULL);
360

361 362 363
  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
  requisition->width += border_width * 2;
  requisition->height += border_width * 2;
364 365 366 367 368 369 370 371 372
}

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

373
      button = BUTTON_DATA (path_bar->button_list->data)->button;
374
      if (gtk_widget_get_child_visible (button))
375 376 377 378
	{
	  gtk_path_bar_stop_scrolling (path_bar);
	  gtk_widget_set_sensitive (path_bar->down_slider_button, FALSE);
	}
379 380 381
      else
	gtk_widget_set_sensitive (path_bar->down_slider_button, TRUE);

382
      button = BUTTON_DATA (g_list_last (path_bar->button_list)->data)->button;
383
      if (gtk_widget_get_child_visible (button))
384 385 386 387
	{
	  gtk_path_bar_stop_scrolling (path_bar);
	  gtk_widget_set_sensitive (path_bar->up_slider_button, FALSE);
	}
388 389 390 391 392
      else
	gtk_widget_set_sensitive (path_bar->up_slider_button, TRUE);
    }
}

393 394 395 396 397 398 399 400
static void
gtk_path_bar_map (GtkWidget *widget)
{
  gdk_window_show (GTK_PATH_BAR (widget)->event_window);

  GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->map (widget);
}

401 402 403 404
static void
gtk_path_bar_unmap (GtkWidget *widget)
{
  gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
405
  gdk_window_hide (GTK_PATH_BAR (widget)->event_window);
406 407 408 409

  GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->unmap (widget);
}

410 411 412 413
static void
gtk_path_bar_realize (GtkWidget *widget)
{
  GtkPathBar *path_bar;
414 415
  GtkAllocation allocation;
  GdkWindow *window;
416 417 418
  GdkWindowAttr attributes;
  gint attributes_mask;

419
  gtk_widget_set_realized (widget, TRUE);
420 421

  path_bar = GTK_PATH_BAR (widget);
422 423 424 425 426
  window = gtk_widget_get_parent_window (widget);
  gtk_widget_set_window (widget, window);
  g_object_ref (window);

  gtk_widget_get_allocation (widget, &allocation);
427 428

  attributes.window_type = GDK_WINDOW_CHILD;
429 430 431 432
  attributes.x = allocation.x;
  attributes.y = allocation.y;
  attributes.width = allocation.width;
  attributes.height = allocation.height;
433 434 435 436 437 438
  attributes.wclass = GDK_INPUT_ONLY;
  attributes.event_mask = gtk_widget_get_events (widget);
  attributes.event_mask |= GDK_SCROLL_MASK;
  attributes_mask = GDK_WA_X | GDK_WA_Y;

  path_bar->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
439
                                           &attributes, attributes_mask);
440 441
  gdk_window_set_user_data (path_bar->event_window, widget);

442
  gtk_widget_style_attach (widget);
443 444 445 446 447 448 449 450 451 452 453 454 455
}

static void
gtk_path_bar_unrealize (GtkWidget *widget)
{
  GtkPathBar *path_bar;

  path_bar = GTK_PATH_BAR (widget);

  gdk_window_set_user_data (path_bar->event_window, NULL);
  gdk_window_destroy (path_bar->event_window);
  path_bar->event_window = NULL;

456
  GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->unrealize (widget);
457 458
}

459 460 461 462 463 464 465 466 467 468 469 470 471
/* 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;
472
  guint border_width;
473 474
  gboolean need_sliders = FALSE;
  gint up_slider_offset = 0;
475
  GtkRequisition child_requisition;
476

477
  gtk_widget_set_allocation (widget, allocation);
478

479
  if (gtk_widget_get_realized (widget))
480 481 482 483
    gdk_window_move_resize (path_bar->event_window,
			    allocation->x, allocation->y,
			    allocation->width, allocation->height);

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

  direction = gtk_widget_get_direction (widget);
489
  border_width = gtk_container_get_border_width (GTK_CONTAINER (path_bar));
490 491 492
  allocation_width = allocation->width - 2 * border_width;

  /* First, we check to see if we need the scrollbars. */
493 494 495
  if (path_bar->fake_root)
    width = path_bar->spacing + path_bar->slider_width;
  else
496
    width = 0;
497 498

  for (list = path_bar->button_list; list; list = list->next)
499
    {
500
      child = BUTTON_DATA (list->data)->button;
501

502
      gtk_widget_get_preferred_size (child, &child_requisition, NULL);
503 504

      width += child_requisition.width + path_bar->spacing;
505 506
      if (list == path_bar->fake_root)
	break;
507 508 509 510
    }

  if (width <= allocation_width)
    {
511 512 513 514
      if (path_bar->fake_root)
	first_button = path_bar->fake_root;
      else
	first_button = g_list_last (path_bar->button_list);
515 516 517 518 519 520
    }
  else
    {
      gboolean reached_end = FALSE;
      gint slider_space = 2 * (path_bar->spacing + path_bar->slider_width);

521 522 523 524
      if (path_bar->first_scrolled_button)
	first_button = path_bar->first_scrolled_button;
      else
	first_button = path_bar->button_list;
525 526 527 528 529 530 531
      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. */
532 533
      gtk_widget_get_preferred_size (BUTTON_DATA (first_button->data)->button,
                                     &child_requisition, NULL);
534 535

      width = child_requisition.width;
536 537 538
      list = first_button->prev;
      while (list && !reached_end)
	{
539
	  child = BUTTON_DATA (list->data)->button;
540

541
          gtk_widget_get_preferred_size (child, &child_requisition, NULL);
542 543

	  if (width + child_requisition.width +
544
	      path_bar->spacing + slider_space > allocation_width)
545
	    reached_end = TRUE;
546 547
	  else if (list == path_bar->fake_root)
	    break;
548
	  else
549
	    width += child_requisition.width + path_bar->spacing;
550 551 552 553 554 555

	  list = list->prev;
	}

      /* Finally, we walk up, seeing how many of the previous buttons we can
       * add */
556
      while (first_button->next && !reached_end)
557
	{
558
	  child = BUTTON_DATA (first_button->next->data)->button;
559

560
          gtk_widget_get_preferred_size (child, &child_requisition, NULL);
561 562

	  if (width + child_requisition.width + path_bar->spacing + slider_space > allocation_width)
563 564 565 566 567
	    {
	      reached_end = TRUE;
	    }
	  else
	    {
568
	      width += child_requisition.width + path_bar->spacing;
569 570
	      if (first_button == path_bar->fake_root)
		break;
571 572 573 574 575 576 577 578 579 580 581 582
	      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;
583
      if (need_sliders || path_bar->fake_root)
584 585 586 587 588 589 590 591
	{
	  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;
592
      if (need_sliders || path_bar->fake_root)
593 594 595 596 597 598 599 600
	{
	  up_slider_offset = border_width;
	  child_allocation.x += (path_bar->spacing + path_bar->slider_width);
	}
    }

  for (list = first_button; list; list = list->prev)
    {
601
      GtkAllocation widget_allocation;
602 603 604 605 606
      ButtonData *button_data;

      button_data = BUTTON_DATA (list->data);
      child = button_data->button;

607
      gtk_widget_get_preferred_size (child, &child_requisition, NULL);
608 609

      child_allocation.width = MIN (child_requisition.width,
610
				    allocation_width - (path_bar->spacing + path_bar->slider_width) * 2);
611

612 613
      if (direction == GTK_TEXT_DIR_RTL)
	child_allocation.x -= child_allocation.width;
614 615 616 617

      /* Check to see if we've don't have any more space to allocate buttons */
      if (need_sliders && direction == GTK_TEXT_DIR_RTL)
	{
618 619
          gtk_widget_get_allocation (widget, &widget_allocation);
	  if (child_allocation.x - path_bar->spacing - path_bar->slider_width < widget_allocation.x + border_width)
620 621 622 623
	    break;
	}
      else if (need_sliders && direction == GTK_TEXT_DIR_LTR)
	{
624
          gtk_widget_get_allocation (widget, &widget_allocation);
625
	  if (child_allocation.x + child_allocation.width + path_bar->spacing + path_bar->slider_width >
626
	      widget_allocation.x + border_width + allocation_width)
627 628 629
	    break;
	}

630
      if (child_allocation.width < child_requisition.width)
631 632 633 634 635 636 637 638
	{
	  if (!gtk_widget_get_has_tooltip (child))
	    gtk_widget_set_tooltip_text (child, button_data->dir_name);
	}
      else if (gtk_widget_get_has_tooltip (child))
	gtk_widget_set_tooltip_text (child, NULL);
      
      gtk_widget_set_child_visible (child, TRUE);
639 640 641
      gtk_widget_size_allocate (child, &child_allocation);

      if (direction == GTK_TEXT_DIR_RTL)
642
	child_allocation.x -= path_bar->spacing;
643
      else
644
	child_allocation.x += child_allocation.width + path_bar->spacing;
645 646 647 648
    }
  /* Now we go hide all the widgets that don't fit */
  while (list)
    {
649
      gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
650 651 652 653
      list = list->prev;
    }
  for (list = first_button->next; list; list = list->next)
    {
654
      gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
655 656
    }

657
  if (need_sliders || path_bar->fake_root)
658 659 660 661 662
    {
      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);

663 664 665 666 667 668 669 670 671
      gtk_widget_set_child_visible (path_bar->up_slider_button, TRUE);
      gtk_widget_show_all (path_bar->up_slider_button);
    }
  else
    gtk_widget_set_child_visible (path_bar->up_slider_button, FALSE);
      
  if (need_sliders)
    {
      child_allocation.width = path_bar->slider_width;
672 673 674 675 676 677 678 679

      if (direction == GTK_TEXT_DIR_RTL)
	child_allocation.x = border_width;
      else
	child_allocation.x = allocation->width - border_width - path_bar->slider_width;

      child_allocation.x += allocation->x;
      
680 681 682 683 684 685 686
      gtk_widget_size_allocate (path_bar->down_slider_button, &child_allocation);

      gtk_widget_set_child_visible (path_bar->down_slider_button, TRUE);
      gtk_widget_show_all (path_bar->down_slider_button);
      gtk_path_bar_update_slider_buttons (path_bar);
    }
  else
687
    gtk_widget_set_child_visible (path_bar->down_slider_button, FALSE);
688 689
}

690 691 692 693
static void
gtk_path_bar_style_set (GtkWidget *widget,
			GtkStyle  *previous_style)
{
694
  GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set (widget, previous_style);
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712

  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));
}

713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
static gboolean
gtk_path_bar_scroll (GtkWidget      *widget,
		     GdkEventScroll *event)
{
  switch (event->direction)
    {
    case GDK_SCROLL_RIGHT:
    case GDK_SCROLL_DOWN:
      gtk_path_bar_scroll_down (GTK_PATH_BAR (widget));
      break;
    case GDK_SCROLL_LEFT:
    case GDK_SCROLL_UP:
      gtk_path_bar_scroll_up (GTK_PATH_BAR (widget));
      break;
    }

  return TRUE;
}

732 733 734 735 736 737 738
static void
gtk_path_bar_add (GtkContainer *container,
		  GtkWidget    *widget)
{
  gtk_widget_set_parent (widget, GTK_WIDGET (container));
}

739 740 741 742
static void
gtk_path_bar_remove_1 (GtkContainer *container,
		       GtkWidget    *widget)
{
743
  gboolean was_visible = gtk_widget_get_visible (widget);
744 745 746 747 748
  gtk_widget_unparent (widget);
  if (was_visible)
    gtk_widget_queue_resize (GTK_WIDGET (container));
}

749 750 751 752 753 754 755 756 757
static void
gtk_path_bar_remove (GtkContainer *container,
		     GtkWidget    *widget)
{
  GtkPathBar *path_bar;
  GList *children;

  path_bar = GTK_PATH_BAR (container);

758 759 760 761 762 763 764 765 766 767 768 769 770
  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;
    }
771

772
  children = path_bar->button_list;
773 774
  while (children)
    {
775
      if (widget == BUTTON_DATA (children->data)->button)
776
	{
777
	  gtk_path_bar_remove_1 (container, widget);
778 779
	  path_bar->button_list = g_list_remove_link (path_bar->button_list, children);
	  g_list_free (children);
780
	  return;
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
	}
      
      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;
803
      child = BUTTON_DATA (children->data)->button;
804 805 806 807 808
      children = children->next;

      (* callback) (child, callback_data);
    }

809 810 811 812 813
  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);
814 815
}

816
static void
817
gtk_path_bar_scroll_down (GtkPathBar *path_bar)
818
{
819
  GtkAllocation allocation, button_allocation;
820
  GList *list;
821
  GList *down_button = NULL;
822
  gint space_available;
823 824 825 826 827 828 829

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

830 831 832 833 834 835
  if (gtk_widget_get_child_visible (BUTTON_DATA (path_bar->button_list->data)->button))
    {
      /* Return if the last button is already visible */
      return;
    }

836 837
  gtk_widget_queue_resize (GTK_WIDGET (path_bar));

838 839 840 841
  /* We find the button at the 'down' end that we have to make
   * visible */
  for (list = path_bar->button_list; list; list = list->next)
    {
842
      if (list->next && gtk_widget_get_child_visible (BUTTON_DATA (list->next->data)->button))
843 844 845 846 847 848
	{
	  down_button = list;
	  break;
	}
    }

849 850 851 852
  gtk_widget_get_allocation (GTK_WIDGET (path_bar), &allocation);
  gtk_widget_get_allocation (BUTTON_DATA (down_button->data)->button, &button_allocation);

  space_available = (allocation.width
853
		     - 2 * gtk_container_get_border_width (GTK_CONTAINER (path_bar))
854
		     - 2 * path_bar->spacing - 2 * path_bar->slider_width
855
                     - button_allocation.width);
856 857 858 859
  path_bar->first_scrolled_button = down_button;
  
  /* We have space_available free space that's not being used.  
   * So we walk down from the end, adding buttons until we use all free space.
860
   */
861
  while (space_available > 0)
862
    {
863 864 865 866
      path_bar->first_scrolled_button = down_button;
      down_button = down_button->next;
      if (!down_button)
	break;
867
      space_available -= (button_allocation.width
868
			  + path_bar->spacing);
869 870 871 872
    }
}

static void
873
gtk_path_bar_scroll_up (GtkPathBar *path_bar)
874 875 876
{
  GList *list;

877 878 879 880 881 882
  if (path_bar->ignore_click)
    {
      path_bar->ignore_click = FALSE;
      return;   
    }

883 884 885 886 887 888 889 890
  list = g_list_last (path_bar->button_list);

  if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
    {
      /* Return if the first button is already visible */
      return;
    }

891 892
  gtk_widget_queue_resize (GTK_WIDGET (path_bar));

893
  for ( ; list; list = list->prev)
894
    {
895
      if (list->prev && gtk_widget_get_child_visible (BUTTON_DATA (list->prev->data)->button))
896
	{
897 898
	  if (list->prev == path_bar->fake_root)
	    path_bar->fake_root = NULL;
899 900 901 902 903 904
	  path_bar->first_scrolled_button = list;
	  return;
	}
    }
}

905 906 907 908 909 910 911
static gboolean
gtk_path_bar_scroll_timeout (GtkPathBar *path_bar)
{
  gboolean retval = FALSE;

  if (path_bar->timer)
    {
912
      if (path_bar->scrolling_up)
913
	gtk_path_bar_scroll_up (path_bar);
914
      else if (path_bar->scrolling_down)
915
	gtk_path_bar_scroll_down (path_bar);
916 917 918

      if (path_bar->need_timer) 
	{
919 920 921 922 923
          GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (path_bar));
          guint        timeout;

          g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);

924 925
	  path_bar->need_timer = FALSE;

926
	  path_bar->timer = gdk_threads_add_timeout (timeout * SCROLL_DELAY_FACTOR,
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
					   (GSourceFunc)gtk_path_bar_scroll_timeout,
					   path_bar);
	}
      else
	retval = TRUE;
    }

  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;
    }
}

948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
static gboolean
gtk_path_bar_slider_up_defocus (GtkWidget      *widget,
                                    GdkEventButton *event,
                                    GtkPathBar     *path_bar)
{
  GList *list;
  GList *up_button = NULL;

  if (event->type != GDK_FOCUS_CHANGE)
    return FALSE;

  for (list = g_list_last (path_bar->button_list); list; list = list->prev)
    {
      if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
        {
          up_button = list;
          break;
        }
    }

  /* don't let the focus vanish */
969
  if ((!gtk_widget_is_sensitive (path_bar->up_slider_button)) ||
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
      (!gtk_widget_get_child_visible (path_bar->up_slider_button)))
    gtk_widget_grab_focus (BUTTON_DATA (up_button->data)->button);

  return FALSE;
}

static gboolean
gtk_path_bar_slider_down_defocus (GtkWidget      *widget,
                                    GdkEventButton *event,
                                    GtkPathBar     *path_bar)
{
  GList *list;
  GList *down_button = NULL;

  if (event->type != GDK_FOCUS_CHANGE)
    return FALSE;

  for (list = path_bar->button_list; list; list = list->next)
    {
      if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
        {
          down_button = list;
          break;
        }
    }

  /* don't let the focus vanish */
997
  if ((!gtk_widget_is_sensitive (path_bar->down_slider_button)) ||
998 999 1000 1001 1002 1003
      (!gtk_widget_get_child_visible (path_bar->down_slider_button)))
    gtk_widget_grab_focus (BUTTON_DATA (down_button->data)->button);

  return FALSE;
}

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
static gboolean
gtk_path_bar_slider_button_press (GtkWidget      *widget, 
				  GdkEventButton *event,
				  GtkPathBar     *path_bar)
{
  if (event->type != GDK_BUTTON_PRESS || event->button != 1)
    return FALSE;

  path_bar->ignore_click = FALSE;

  if (widget == path_bar->up_slider_button)
1015
    {
1016
      path_bar->scrolling_down = FALSE;
1017
      path_bar->scrolling_up = TRUE;
1018
      gtk_path_bar_scroll_up (path_bar);
1019
    }
1020
  else if (widget == path_bar->down_slider_button)
1021 1022
    {
      path_bar->scrolling_up = FALSE;
1023
      path_bar->scrolling_down = TRUE;
1024
      gtk_path_bar_scroll_down (path_bar);
1025
    }
1026 1027 1028

  if (!path_bar->timer)
    {
1029 1030 1031 1032 1033
      GtkSettings *settings = gtk_widget_get_settings (widget);
      guint        timeout;

      g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);

1034
      path_bar->need_timer = TRUE;
1035
      path_bar->timer = gdk_threads_add_timeout (timeout,
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
				       (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)
{
1069
  if (!gtk_widget_is_sensitive (widget))
1070 1071 1072 1073
    gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
}


1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
/* 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)));

1119
  if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
    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 */
1156 1157 1158 1159 1160
static void
gtk_path_bar_clear_buttons (GtkPathBar *path_bar)
{
  while (path_bar->button_list != NULL)
    {
1161
      gtk_container_remove (GTK_CONTAINER (path_bar), BUTTON_DATA (path_bar->button_list->data)->button);
1162
    }
1163
  path_bar->first_scrolled_button = NULL;
1164
  path_bar->fake_root = NULL;
1165 1166
}

1167 1168 1169 1170
static void
button_clicked_cb (GtkWidget *button,
		   gpointer   data)
{
1171
  ButtonData *button_data;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1172 1173 1174
  GtkPathBar *path_bar;
  GList *button_list;
  gboolean child_is_hidden;
1175
  GFile *child_file;
1176 1177 1178 1179

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

1181
  path_bar = GTK_PATH_BAR (gtk_widget_get_parent (button));
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1182 1183 1184 1185

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

1186 1187
  g_signal_handlers_block_by_func (button,
				   G_CALLBACK (button_clicked_cb), data);
1188
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
1189 1190
  g_signal_handlers_unblock_by_func (button,
				     G_CALLBACK (button_clicked_cb), data);
1191

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1192 1193 1194 1195 1196
  if (button_list->prev)
    {
      ButtonData *child_data;

      child_data = BUTTON_DATA (button_list->prev->data);
1197
      child_file = child_data->file;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1198 1199 1200
      child_is_hidden = child_data->file_is_hidden;
    }
  else
1201
    {
1202
      child_file = NULL;
1203 1204
      child_is_hidden = FALSE;
    }
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1205

1206
  g_signal_emit (path_bar, path_bar_signals [PATH_CLICKED], 0,
1207
		 button_data->file, child_file, child_is_hidden);
1208 1209
}

1210 1211 1212 1213 1214 1215 1216
struct SetButtonImageData
{
  GtkPathBar *path_bar;
  ButtonData *button_data;
};

static void
1217 1218 1219 1220
set_button_image_get_info_cb (GCancellable *cancellable,
			      GFileInfo    *info,
			      const GError *error,
			      gpointer      user_data)
1221
{
1222
  gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1223 1224 1225
  GdkPixbuf *pixbuf;
  struct SetButtonImageData *data = user_data;

1226
  if (cancellable != data->button_data->cancellable)
1227 1228
    goto out;

1229
  data->button_data->cancellable = NULL;
1230

1231 1232 1233 1234 1235 1236
  if (!data->button_data->button)
    {
      g_free (data->button_data);
      goto out;
    }

1237 1238 1239
  if (cancelled || error)
    goto out;

1240 1241
  pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->path_bar),
			 	       data->path_bar->icon_size);
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
  gtk_image_set_from_pixbuf (GTK_IMAGE (data->button_data->image), pixbuf);

  switch (data->button_data->type)
    {
      case HOME_BUTTON:
	if (data->path_bar->home_icon)
	  g_object_unref (pixbuf);
	else
	  data->path_bar->home_icon = pixbuf;
	break;

      case DESKTOP_BUTTON:
	if (data->path_bar->desktop_icon)
	  g_object_unref (pixbuf);
	else
	  data->path_bar->desktop_icon = pixbuf;
	break;

      default:
	break;
    };

out:
  g_free (data);
1266
  g_object_unref (cancellable);
1267 1268 1269 1270 1271
}

static void
set_button_image (GtkPathBar *path_bar,
		  ButtonData *button_data)
1272
{
1273
  GtkFileSystemVolume *volume;
1274
  struct SetButtonImageData *data;
1275

1276
  switch (button_data->type)
1277
    {
1278
    case ROOT_BUTTON:
1279 1280

      if (path_bar->root_icon != NULL)
1281 1282 1283 1284
        {
          gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->root_icon);
	  break;
	}
1285

1286
      volume = _gtk_file_system_get_volume_for_file (path_bar->file_system, path_bar->root_file);
1287
      if (volume == NULL)
1288
	return;
1289

1290 1291 1292 1293
      path_bar->root_icon = _gtk_file_system_volume_render_icon (volume,
								 GTK_WIDGET (path_bar),
								 path_bar->icon_size,
								 NULL);
1294
      _gtk_file_system_volume_unref (volume);
1295

1296 1297 1298
      gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->root_icon);
      break;

1299
    case HOME_BUTTON:
1300
      if (path_bar->home_icon != NULL)
1301 1302 1303 1304 1305 1306 1307 1308 1309
        {
	  gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->home_icon);
	  break;
	}

      data = g_new0 (struct SetButtonImageData, 1);
      data->path_bar = path_bar;
      data->button_data = button_data;

1310 1311
      if (button_data->cancellable)
	g_cancellable_cancel (button_data->cancellable);
1312

1313
      button_data->cancellable =
1314 1315 1316 1317 1318
        _gtk_file_system_get_info (path_bar->file_system,
				   path_bar->home_file,
				   "standard::icon",
				   set_button_image_get_info_cb,
				   data);
1319 1320
      break;

1321 1322
    case DESKTOP_BUTTON:
      if (path_bar->desktop_icon != NULL)
1323 1324 1325 1326 1327 1328 1329 1330 1331
        {
	  gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->desktop_icon);
	  break;
	}

      data = g_new0 (struct SetButtonImageData, 1);
      data->path_bar = path_bar;
      data->button_data = button_data;

1332 1333
      if (button_data->cancellable)
	g_cancellable_cancel (button_data->cancellable);
1334

1335
      button_data->cancellable =
1336 1337 1338 1339 1340
        _gtk_file_system_get_info (path_bar->file_system,
				   path_bar->desktop_file,
				   "standard::icon",
				   set_button_image_get_info_cb,
				   data);
1341
      break;
1342
    default:
1343
      break;
1344 1345 1346
    }
}

1347 1348 1349
static void
button_data_free (ButtonData *button_data)
{
1350 1351 1352
  if (button_data->file)
    g_object_unref (button_data->file);
  button_data->file = NULL;
1353

1354
  g_free (button_data->dir_name);
1355 1356 1357 1358
  button_data->dir_name = NULL;

  button_data->button = NULL;

1359 1360
  if (button_data->cancellable)
    g_cancellable_cancel (button_data->cancellable);
1361 1362
  else
    g_free (button_data);
1363 1364
}

1365 1366 1367
static const char *
get_dir_name (ButtonData *button_data)
{
1368
  return button_data->dir_name;
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
}

/* 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;
  gchar *markup;

  pango_layout_get_pixel_size (layout, &requisition->width, &requisition->height);
  
  markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
  pango_layout_set_markup (layout, markup, -1);
  g_free (markup);

  pango_layout_get_pixel_size (layout, &bold_width, &bold_height);
  requisition->width = MAX (requisition->width, bold_width);
  requisition->height = MAX (requisition->height, bold_height);
  
  g_object_unref (layout);
}

1397
static void
1398 1399 1400
gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
				       ButtonData *button_data,
				       gboolean    current_dir)
1401
{
1402
  const gchar *dir_name = get_dir_name (button_data);
1403

1404
  if (button_data->label != NULL)
1405
    {
1406 1407 1408
      if (current_dir)
	{
	  char *markup;
1409

1410
	  markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1411
	  gtk_label_set_markup (GTK_LABEL (button_data->label), markup);
1412 1413 1414 1415
	  g_free (markup);
	}
      else
	{
1416
	  gtk_label_set_text (GTK_LABEL (button_data->label), dir_name);
1417
	}
1418
    }
1419

1420
  if (button_data->image != NULL)
1421
    {
1422
      set_button_image (path_bar, button_data);
1423 1424
    }

1425
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button)) != current_dir)
1426
    {
1427 1428 1429
      button_data->ignore_changes = TRUE;
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_data->button), current_dir);
      button_data->ignore_changes = FALSE;
1430 1431 1432
    }
}

1433 1434
static ButtonType
find_button_type (GtkPathBar  *path_bar,
1435
		  GFile       *file)
1436
{
1437 1438
  if (path_bar->root_file != NULL &&
      g_file_equal (file, path_bar->root_file))
1439
    return ROOT_BUTTON;
1440 1441
  if (path_bar->home_file != NULL &&
      g_file_equal (file, path_bar->home_file))
1442
    return HOME_BUTTON;
1443 1444
  if (path_bar->desktop_file != NULL &&
      g_file_equal (file, path_bar->desktop_file))
1445 1446 1447 1448 1449
    return DESKTOP_BUTTON;

 return NORMAL_BUTTON;
}

1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
static void
button_drag_data_get_cb (GtkWidget          *widget,
			 GdkDragContext     *context,
			 GtkSelectionData   *selection_data,
			 guint               info,
			 guint               time_,
			 gpointer            data)
{
  ButtonData *button_data;
  GtkPathBar *path_bar;
1460
  char *uris[2];
1461 1462

  button_data = data;
1463
  path_bar = GTK_PATH_BAR (gtk_widget_get_parent (widget)); /* the button's parent *is* the path bar */
1464

1465
  uris[0] = g_file_get_uri (button_data->file);
1466
  uris[1] = NULL;
1467