gtkpathbar.c 51.1 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
#include <string.h>
23 24
#include "gtkpathbar.h"
#include "gtktogglebutton.h"
25
#include "gtkalignment.h"
26
#include "gtkarrow.h"
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
#include "gtkmain.h"
#include "gtkmarshalers.h"
36
#include "gtkalias.h"
37 38 39 40 41 42

enum {
  PATH_CLICKED,
  LAST_SIGNAL
};

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

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

52
#define SCROLL_DELAY_FACTOR 5
53

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

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

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

struct _ButtonData
{
  GtkWidget *button;
  ButtonType type;
  char *dir_name;
66
  GFile *file;
67 68
  GtkWidget *image;
  GtkWidget *label;
69
  GCancellable *cancellable;
70 71
  guint ignore_changes : 1;
  guint file_is_hidden : 1;
72
};
73 74 75 76 77
/* 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)
78

Matthias Clasen's avatar
Matthias Clasen committed
79
G_DEFINE_TYPE (GtkPathBar, gtk_path_bar, GTK_TYPE_CONTAINER)
80

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

129 130 131 132 133
static void
on_slider_unmap (GtkWidget  *widget,
		 GtkPathBar *path_bar)
{
  if (path_bar->timer &&
134 135 136
      ((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);
137 138
}

139
static GtkWidget *
140 141
get_slider_button (GtkPathBar  *path_bar,
		   GtkArrowType arrow_type)
142 143
{
  GtkWidget *button;
144
  AtkObject *atk_obj;
145 146 147

  gtk_widget_push_composite_child ();

148
  button = gtk_button_new ();
149 150 151 152 153 154
  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"));

155
  gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
156 157
  gtk_container_add (GTK_CONTAINER (button),
                     gtk_arrow_new (arrow_type, GTK_SHADOW_OUT));
158 159 160
  gtk_container_add (GTK_CONTAINER (path_bar), button);
  gtk_widget_show_all (button);

161 162 163
  g_signal_connect (G_OBJECT (button), "unmap",
		    G_CALLBACK (on_slider_unmap), path_bar);

164 165
  gtk_widget_pop_composite_child ();

166 167 168 169 170 171 172 173 174
  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);

175
  path_bar->get_info_cancellable = NULL;
176

177
  path_bar->spacing = 0;
178 179
  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);
180
  path_bar->icon_size = FALLBACK_ICON_SIZE;
181
  
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
  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);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
}

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;

215
  gobject_class->finalize = gtk_path_bar_finalize;
216
  gobject_class->dispose = gtk_path_bar_dispose;
217 218

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

  container_class->add = gtk_path_bar_add;
  container_class->forall = gtk_path_bar_forall;
  container_class->remove = gtk_path_bar_remove;
233 234
  /* FIXME: */
  /*  container_class->child_type = gtk_path_bar_child_type;*/
235 236

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


static void
251
gtk_path_bar_finalize (GObject *object)
252 253 254 255
{
  GtkPathBar *path_bar;

  path_bar = GTK_PATH_BAR (object);
256 257 258

  gtk_path_bar_stop_scrolling (path_bar);

259
  g_list_free (path_bar->button_list);
260 261 262 263 264 265
  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);
266

267
  if (path_bar->root_icon)
268
    g_object_unref (path_bar->root_icon);
269 270 271 272 273
  if (path_bar->home_icon)
    g_object_unref (path_bar->home_icon);
  if (path_bar->desktop_icon)
    g_object_unref (path_bar->desktop_icon);

274 275
  if (path_bar->file_system)
    g_object_unref (path_bar->file_system);
276

277
  G_OBJECT_CLASS (gtk_path_bar_parent_class)->finalize (object);
278 279
}

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

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

303 304 305
  if (path_bar->get_info_cancellable)
    g_cancellable_cancel (path_bar->get_info_cancellable);
  path_bar->get_info_cancellable = NULL;
306 307 308 309

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

310 311 312 313 314 315 316 317 318
/* 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)
{
319
  ButtonData *button_data;
320 321 322 323 324 325 326 327 328 329 330
  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)
    {
331 332
      button_data = BUTTON_DATA (list->data);
      gtk_widget_size_request (button_data->button, &child_requisition);
333 334 335 336 337 338 339 340
      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.
   */
341
  path_bar->slider_width = MIN(requisition->height * 2 / 3 + 5, requisition->height);
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
  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;

361
      button = BUTTON_DATA (path_bar->button_list->data)->button;
362
      if (gtk_widget_get_child_visible (button))
363 364 365 366
	{
	  gtk_path_bar_stop_scrolling (path_bar);
	  gtk_widget_set_sensitive (path_bar->down_slider_button, FALSE);
	}
367 368 369
      else
	gtk_widget_set_sensitive (path_bar->down_slider_button, TRUE);

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

381 382 383 384 385 386 387 388
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);
}

389 390 391 392
static void
gtk_path_bar_unmap (GtkWidget *widget)
{
  gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
393
  gdk_window_hide (GTK_PATH_BAR (widget)->event_window);
394 395 396 397

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

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
static void
gtk_path_bar_realize (GtkWidget *widget)
{
  GtkPathBar *path_bar;
  GdkWindowAttr attributes;
  gint attributes_mask;

  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);

  path_bar = GTK_PATH_BAR (widget);
  widget->window = gtk_widget_get_parent_window (widget);
  g_object_ref (widget->window);

  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.x = widget->allocation.x;
  attributes.y = widget->allocation.y;
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  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),
							&attributes, attributes_mask);
  gdk_window_set_user_data (path_bar->event_window, widget);

  widget->style = gtk_style_attach (widget->style, widget->window);
}

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;

439
  GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->unrealize (widget);
440 441
}

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
/* 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;

462 463 464 465 466
  if (GTK_WIDGET_REALIZED (widget))
    gdk_window_move_resize (path_bar->event_window,
			    allocation->x, allocation->y,
			    allocation->width, allocation->height);

467 468 469 470 471 472 473 474 475
  /* 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. */
476 477 478 479 480 481
  if (path_bar->fake_root)
    width = path_bar->spacing + path_bar->slider_width;
  else
      width = 0;

  for (list = path_bar->button_list; list; list = list->next)
482
    {
483
      child = BUTTON_DATA (list->data)->button;
484 485

      width += child->requisition.width + path_bar->spacing;
486 487
      if (list == path_bar->fake_root)
	break;
488 489 490 491
    }

  if (width <= allocation_width)
    {
492 493 494 495
      if (path_bar->fake_root)
	first_button = path_bar->fake_root;
      else
	first_button = g_list_last (path_bar->button_list);
496 497 498 499 500 501
    }
  else
    {
      gboolean reached_end = FALSE;
      gint slider_space = 2 * (path_bar->spacing + path_bar->slider_width);

502 503 504 505
      if (path_bar->first_scrolled_button)
	first_button = path_bar->first_scrolled_button;
      else
	first_button = path_bar->button_list;
506 507 508 509 510 511 512
      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. */
513
      width = BUTTON_DATA (first_button->data)->button->requisition.width;
514 515 516
      list = first_button->prev;
      while (list && !reached_end)
	{
517
	  child = BUTTON_DATA (list->data)->button;
518

519 520
	  if (width + child->requisition.width +
	      path_bar->spacing + slider_space > allocation_width)
521
	    reached_end = TRUE;
522 523
	  else if (list == path_bar->fake_root)
	    break;
524 525 526 527 528 529 530 531
	  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 */
532
      while (first_button->next && !reached_end)
533
	{
534
	  child = BUTTON_DATA (first_button->next->data)->button;
535 536 537 538 539 540 541 542

	  if (width + child->requisition.width + path_bar->spacing + slider_space > allocation_width)
	    {
	      reached_end = TRUE;
	    }
	  else
	    {
	      width += child->requisition.width + path_bar->spacing;
543 544
	      if (first_button == path_bar->fake_root)
		break;
545 546 547 548 549 550 551 552 553 554 555 556
	      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;
557
      if (need_sliders || path_bar->fake_root)
558 559 560 561 562 563 564 565
	{
	  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;
566
      if (need_sliders || path_bar->fake_root)
567 568 569 570 571 572 573 574
	{
	  up_slider_offset = border_width;
	  child_allocation.x += (path_bar->spacing + path_bar->slider_width);
	}
    }

  for (list = first_button; list; list = list->prev)
    {
575
      child = BUTTON_DATA (list->data)->button;
576 577

      child_allocation.width = child->requisition.width;
578 579
      if (direction == GTK_TEXT_DIR_RTL)
	child_allocation.x -= child_allocation.width;
580 581 582 583

      /* Check to see if we've don't have any more space to allocate buttons */
      if (need_sliders && direction == GTK_TEXT_DIR_RTL)
	{
584
	  if (child_allocation.x - path_bar->spacing - path_bar->slider_width < widget->allocation.x + border_width)
585 586 587 588
	    break;
	}
      else if (need_sliders && direction == GTK_TEXT_DIR_LTR)
	{
589
	  if (child_allocation.x + child_allocation.width + path_bar->spacing + path_bar->slider_width >
590 591 592 593
	      widget->allocation.x + border_width + allocation_width)
	    break;
	}

594
      gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, TRUE);
595 596 597 598 599 600
      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;
601
	  down_slider_offset = border_width;
602 603 604 605
	}
      else
	{
	  down_slider_offset = child_allocation.x - widget->allocation.x;
606 607
	  down_slider_offset = allocation->width - border_width - path_bar->slider_width;
	  child_allocation.x += child_allocation.width + path_bar->spacing;
608 609 610 611 612
	}
    }
  /* Now we go hide all the widgets that don't fit */
  while (list)
    {
613
      gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
614 615 616 617
      list = list->prev;
    }
  for (list = first_button->next; list; list = list->next)
    {
618
      gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
619 620
    }

621
  if (need_sliders || path_bar->fake_root)
622 623 624 625 626
    {
      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);

627 628 629 630 631 632 633 634 635
      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;
636 637 638 639 640 641 642 643
      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->down_slider_button, TRUE);
      gtk_widget_show_all (path_bar->down_slider_button);
      gtk_path_bar_update_slider_buttons (path_bar);
    }
  else
644
    gtk_widget_set_child_visible (path_bar->down_slider_button, FALSE);
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
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));
}

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
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;
}

690 691 692 693 694 695 696
static void
gtk_path_bar_add (GtkContainer *container,
		  GtkWidget    *widget)
{
  gtk_widget_set_parent (widget, GTK_WIDGET (container));
}

697 698 699 700 701 702 703 704 705 706
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));
}

707 708 709 710 711 712 713 714 715
static void
gtk_path_bar_remove (GtkContainer *container,
		     GtkWidget    *widget)
{
  GtkPathBar *path_bar;
  GList *children;

  path_bar = GTK_PATH_BAR (container);

716 717 718 719 720 721 722 723 724 725 726 727 728
  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;
    }
729

730
  children = path_bar->button_list;
731 732
  while (children)
    {
733
      if (widget == BUTTON_DATA (children->data)->button)
734
	{
735
	  gtk_path_bar_remove_1 (container, widget);
736 737
	  path_bar->button_list = g_list_remove_link (path_bar->button_list, children);
	  g_list_free (children);
738
	  return;
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
	}
      
      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;
761
      child = BUTTON_DATA (children->data)->button;
762 763 764 765 766
      children = children->next;

      (* callback) (child, callback_data);
    }

767 768 769 770 771
  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);
772 773
}

774
static void
775
gtk_path_bar_scroll_down (GtkPathBar *path_bar)
776 777
{
  GList *list;
778 779
  GList *down_button = NULL;
  GList *up_button = NULL;
780 781 782 783
  gint space_available;
  gint space_needed;
  gint border_width;
  GtkTextDirection direction;
784 785 786 787 788 789 790

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

791 792 793 794 795 796
  if (gtk_widget_get_child_visible (BUTTON_DATA (path_bar->button_list->data)->button))
    {
      /* Return if the last button is already visible */
      return;
    }

797 798
  gtk_widget_queue_resize (GTK_WIDGET (path_bar));

799 800
  border_width = GTK_CONTAINER (path_bar)->border_width;
  direction = gtk_widget_get_direction (GTK_WIDGET (path_bar));
801

802 803 804 805
  /* We find the button at the 'down' end that we have to make
   * visible */
  for (list = path_bar->button_list; list; list = list->next)
    {
806
      if (list->next && gtk_widget_get_child_visible (BUTTON_DATA (list->next->data)->button))
807 808 809 810 811 812 813 814 815 816
	{
	  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)
    {
817
      if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
818 819 820 821 822 823
	{
	  up_button = list;
	  break;
	}
    }

824 825 826 827
  /* We check if down_button might be NULL in cases where the pathbar's horizontal size is smaller
   * than the button and it doesn't get displayed. e.g., on Windows it might be "My Documents and Settings"
   */
  space_needed = down_button ? BUTTON_DATA (down_button->data)->button->allocation.width : 0 + path_bar->spacing;
828
  if (direction == GTK_TEXT_DIR_RTL)
829
    space_available = path_bar->down_slider_button->allocation.x - GTK_WIDGET (path_bar)->allocation.x;
830 831 832 833 834 835 836 837 838 839
  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)
    {
840
      space_available += BUTTON_DATA (up_button->data)->button->allocation.width + path_bar->spacing;
841 842 843 844 845 846
      up_button = up_button->prev;
      path_bar->first_scrolled_button = up_button;
    }
}

static void
847
gtk_path_bar_scroll_up (GtkPathBar *path_bar)
848 849 850
{
  GList *list;

851 852 853 854 855 856
  if (path_bar->ignore_click)
    {
      path_bar->ignore_click = FALSE;
      return;   
    }

857 858 859 860 861 862 863 864
  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;
    }

865 866
  gtk_widget_queue_resize (GTK_WIDGET (path_bar));

867
  for ( ; list; list = list->prev)
868
    {
869
      if (list->prev && gtk_widget_get_child_visible (BUTTON_DATA (list->prev->data)->button))
870
	{
871 872
	  if (list->prev == path_bar->fake_root)
	    path_bar->fake_root = NULL;
873 874 875 876 877 878
	  path_bar->first_scrolled_button = list;
	  return;
	}
    }
}

879 880 881 882 883 884 885
static gboolean
gtk_path_bar_scroll_timeout (GtkPathBar *path_bar)
{
  gboolean retval = FALSE;

  if (path_bar->timer)
    {
886
      if (path_bar->scrolling_up)
887
	gtk_path_bar_scroll_up (path_bar);
888
      else if (path_bar->scrolling_down)
889
	gtk_path_bar_scroll_down (path_bar);
890 891 892

      if (path_bar->need_timer) 
	{
893 894 895 896 897
          GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (path_bar));
          guint        timeout;

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

898 899
	  path_bar->need_timer = FALSE;

900
	  path_bar->timer = gdk_threads_add_timeout (timeout * SCROLL_DELAY_FACTOR,
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
					   (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;
    }
}

922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
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 */
  if ((!GTK_WIDGET_IS_SENSITIVE (path_bar->up_slider_button)) || 
      (!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 */
  if ((!GTK_WIDGET_IS_SENSITIVE (path_bar->down_slider_button)) || 
      (!gtk_widget_get_child_visible (path_bar->down_slider_button)))
    gtk_widget_grab_focus (BUTTON_DATA (down_button->data)->button);

  return FALSE;
}

978 979 980 981 982 983 984 985 986 987 988
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)
989
    {
990
      path_bar->scrolling_down = FALSE;
991
      path_bar->scrolling_up = TRUE;
992
      gtk_path_bar_scroll_up (path_bar);
993
    }
994
  else if (widget == path_bar->down_slider_button)
995 996
    {
      path_bar->scrolling_up = FALSE;
997
      path_bar->scrolling_down = TRUE;
998
      gtk_path_bar_scroll_down (path_bar);
999
    }
1000 1001 1002

  if (!path_bar->timer)
    {
1003 1004 1005 1006 1007
      GtkSettings *settings = gtk_widget_get_settings (widget);
      guint        timeout;

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

1008
      path_bar->need_timer = TRUE;
1009
      path_bar->timer = gdk_threads_add_timeout (timeout,
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
				       (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));
}


1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
/* 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)));

1093
  if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
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 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
    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 */
1130 1131 1132 1133 1134
static void
gtk_path_bar_clear_buttons (GtkPathBar *path_bar)
{
  while (path_bar->button_list != NULL)
    {
1135
      gtk_container_remove (GTK_CONTAINER (path_bar), BUTTON_DATA (path_bar->button_list->data)->button);
1136
    }
1137
  path_bar->first_scrolled_button = NULL;
1138
  path_bar->fake_root = NULL;
1139 1140
}

1141 1142 1143 1144
static void
button_clicked_cb (GtkWidget *button,
		   gpointer   data)
{
1145
  ButtonData *button_data;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1146 1147 1148
  GtkPathBar *path_bar;
  GList *button_list;
  gboolean child_is_hidden;
1149
  GFile *child_file;
1150 1151 1152 1153

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

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1155 1156 1157 1158 1159
  path_bar = GTK_PATH_BAR (button->parent);

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

1160 1161
  g_signal_handlers_block_by_func (button,
				   G_CALLBACK (button_clicked_cb), data);
1162
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
1163 1164
  g_signal_handlers_unblock_by_func (button,
				     G_CALLBACK (button_clicked_cb), data);
1165

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1166 1167 1168 1169 1170
  if (button_list->prev)
    {
      ButtonData *child_data;

      child_data = BUTTON_DATA (button_list->prev->data);
1171
      child_file = child_data->file;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1172 1173 1174
      child_is_hidden = child_data->file_is_hidden;
    }
  else
1175
    {
1176
      child_file = NULL;
1177 1178
      child_is_hidden = FALSE;
    }
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1179

1180
  g_signal_emit (path_bar, path_bar_signals [PATH_CLICKED], 0,
1181
		 button_data->file, child_file, child_is_hidden);
1182 1183
}

1184 1185 1186 1187 1188 1189 1190
struct SetButtonImageData
{
  GtkPathBar *path_bar;
  ButtonData *button_data;
};

static void
1191 1192 1193 1194
set_button_image_get_info_cb (GCancellable *cancellable,
			      GFileInfo    *info,
			      const GError *error,
			      gpointer      user_data)
1195
{
1196
  gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1197 1198 1199
  GdkPixbuf *pixbuf;
  struct SetButtonImageData *data = user_data;

1200
  if (cancellable != data->button_data->cancellable)
1201 1202
    goto out;

1203
  data->button_data->cancellable = NULL;
1204

1205 1206 1207 1208 1209 1210
  if (!data->button_data->button)
    {
      g_free (data->button_data);
      goto out;
    }

1211 1212 1213
  if (cancelled || error)
    goto out;

1214 1215
  pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->path_bar),
			 	       data->path_bar->icon_size);
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
  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);
1240
  g_object_unref (cancellable);
1241 1242 1243 1244 1245
}

static void
set_button_image (GtkPathBar *path_bar,
		  ButtonData *button_data)
1246
{
1247
  GtkFileSystemVolume *volume;
1248
  struct SetButtonImageData *data;
1249

1250
  switch (button_data->type)
1251
    {
1252
    case ROOT_BUTTON:
1253 1254

      if (path_bar->root_icon != NULL)
1255 1256 1257 1258
        {
          gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->root_icon);
	  break;
	}
1259

1260
      volume = _gtk_file_system_get_volume_for_file (path_bar->file_system, path_bar->root_file);
1261
      if (volume == NULL)
1262
	return;
1263

1264 1265 1266 1267 1268
      path_bar->root_icon = _gtk_file_system_volume_render_icon (volume,
								 GTK_WIDGET (path_bar),
								 path_bar->icon_size,
								 NULL);
      _gtk_file_system_volume_free (volume);
1269

1270 1271 1272
      gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->root_icon);
      break;

1273
    case HOME_BUTTON:
1274
      if (path_bar->home_icon != NULL)
1275 1276 1277 1278 1279 1280 1281 1282 1283
        {
	  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;

1284 1285
      if (button_data->cancellable)
	g_cancellable_cancel (button_data->cancellable);
1286

1287
      button_data->cancellable =
1288 1289 1290 1291 1292
        _gtk_file_system_get_info (path_bar->file_system,
				   path_bar->home_file,
				   "standard::icon",
				   set_button_image_get_info_cb,
				   data);
1293 1294
      break;

1295 1296
    case DESKTOP_BUTTON:
      if (path_bar->desktop_icon != NULL)
1297 1298 1299 1300 1301 1302 1303 1304 1305
        {
	  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;

1306 1307
      if (button_data->cancellable)
	g_cancellable_cancel (button_data->cancellable);
1308

1309
      button_data->cancellable =
1310 1311 1312 1313 1314
        _gtk_file_system_get_info (path_bar->file_system,
				   path_bar->desktop_file,
				   "standard::icon",
				   set_button_image_get_info_cb,
				   data);
1315
      break;
1316
    default:
1317
      break;
1318 1319 1320
    }
}

1321 1322 1323
static void
button_data_free (ButtonData *button_data)
{
1324 1325 1326
  if (button_data->file)
    g_object_unref (button_data->file);
  button_data->file = NULL;
1327

1328
  g_free (button_data->dir_name);
1329 1330 1331 1332
  button_data->dir_name = NULL;

  button_data->button = NULL;

1333 1334
  if (button_data->cancellable)
    g_cancellable_cancel (button_data->cancellable);
1335 1336
  else
    g_free (button_data);
1337 1338
}

1339 1340 1341
static const char *
get_dir_name (ButtonData *button_data)
{
1342
  return button_data->dir_name;
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
}

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

1371
static void
1372 1373 1374
gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
				       ButtonData *button_data,
				       gboolean    current_dir)
1375
{
1376
  const gchar *dir_name = get_dir_name (button_data);
1377

1378
  if (button_data->label != NULL)
1379
    {
1380 1381 1382
      if (current_dir)
	{
	  char *markup;
1383

1384
	  markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1385
	  gtk_label_set_markup (GTK_LABEL (button_data->label), markup);
1386 1387 1388 1389
	  g_free (markup);
	}
      else
	{
1390
	  gtk_label_set_text (GTK_LABEL (button_data->label), dir_name);
1391
	}
1392
    }
1393

1394
  if (button_data->image != NULL)
1395
    {
1396
      set_button_image (path_bar, button_data);
1397 1398
    }

1399
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button)) != current_dir)
1400
    {
1401 1402 1403
      button_data->ignore_changes = TRUE;
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_data->button), current_dir);
      button_data->ignore_changes = FALSE;
1404 1405 1406
    }
}

1407 1408
static ButtonType
find_button_type (GtkPathBar  *path_bar,
1409
		  GFile       *file)
1410
{
1411 1412
  if (path_bar->root_file != NULL &&
      g_file_equal (file, path_bar->root_file))
1413
    return ROOT_BUTTON;
1414 1415
  if (path_bar->home_file != NULL &&
      g_file_equal (file, path_bar->home_file))
1416
    return HOME_BUTTON;
1417 1418
  if (path_bar->desktop_file != NULL &&
      g_file_equal (file, path_bar->desktop_file))
1419 1420 1421 1422 1423
    return DESKTOP_BUTTON;

 return NORMAL_BUTTON;
}

1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
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;
1434
  char *uris[2];
1435 1436 1437 1438

  button_data = data;
  path_bar = GTK_PATH_BAR (widget->parent); /* the button's parent *is* the path bar */

1439
  uris[0] = g_file_get_uri (button_data->file);
1440
  uris[1] = NULL;
1441

1442 1443
  gtk_selection_data_set_uris (selection_data, uris);
  g_free (uris[0]);
1444 1445
}

1446
static ButtonData *
1447 1448
make_directory_button (GtkPathBar  *path_bar,
		       const char  *dir_name,
1449
		       GFile       *file,
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1450 1451
		       gboolean     current_dir,
		       gboolean     file_is_hidden)
1452
{
1453
  AtkObject *atk_obj;
1454
  GtkWidget *child = NULL;
1455
  GtkWidget *label_alignment = NULL;
1456
  ButtonData *button_data;
1457

1458
  file_is_hidden = !! file_is_hidden;
1459
  /* Is it a special button? */
1460 1461
  button_data = g_new0 (ButtonData, 1);

1462
  button_data->type = find_button_type (path_bar, file);
1463
  button_data->button = gtk_toggle_button_new ();
1464
  atk_obj = gtk_widget_get_accessible (button_data->button);
1465
  gtk_button_set_focus_on_click (GTK_BUTTON (button_data->button), FALSE);
1466

1467
  switch (button_data->type)
1468 1469
    {
    case ROOT_BUTTON:
1470 1471
      button_data->image = gtk_image_new ();
      child = button_data->image;
1472
      button_data->label = NULL;
1473
      atk_object_set_name (atk_obj, _("File System Root"));
1474 1475
      break;
    case HOME_BUTTON:
1476
    case DESKTOP_BUTTON:
1477 1478
      button_data->image = gtk_image_new ();
      button_data->label = gtk_label_new (NULL);
1479
      label_alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
1480
      gtk_container_add (GTK_CONTAINER (label_alignment), button_data->label);
1481
      child = gtk_hbox_new (FALSE, 2);
1482
      gtk_box_pack_start (GTK_BOX (child), button_data->image, FALSE, FALSE, 0);
1483
      gtk_box_pack_start (GTK_BOX (child), label_alignment, FALSE, FALSE, 0);
1484 1485
      break;
    case NORMAL_BUTTON:
1486
    default:
1487
      button_data->label = gtk_label_new (NULL);
1488
      label_alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
1489 1490
      gtk_container_add (GTK_CONTAINER (label_alignment), button_data->label);
      child = label_alignment;
1491
      button_data->image = NULL;
1492
    }
1493

1494 1495 1496 1497 1498 1499 1500 1501
  /* label_alignment is created because we can't override size-request
   * on label itself and still have the contents of the label centered
   * properly in the label's requisition
   */
  if (label_alignment)
    g_signal_connect (label_alignment, "size-request",
		      G_CALLBACK (label_size_request_cb), button_data);

1502
  button_data->dir_name = g_strdup (dir_name);
1503
  button_data->file = g_object_ref (file);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1504
  button_data->file_is_hidden = file_is_hidden;
1505

1506 1507
  gtk_container_add (GTK_CONTAINER (button_data->button), child);
  gtk_widget_show_all (button_data->button);
1508

1509
  gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
1510

1511 1512 1513
  g_signal_connect (button_data->button, "clicked",
		    G_CALLBACK (button_clicked_cb),
		    button_data);
1514 1515
  g_object_weak_ref (G_OBJECT (button_data->button),
		     (GWeakNotify) button_data_free, button_data);
1516

1517 1518
  gtk_drag_source_set (button_data->button,
		       GDK_BUTTON1_MASK,
1519
		       NULL, 0,
1520
		       GDK_ACTION_COPY);
1521
  gtk_drag_source_add_uri_targets (button_data->button);
1522
  g_signal_connect (button_data->button, "drag-data-get",
1523 1524
		    G_CALLBACK (button_drag_data_get_cb), button_data);

1525
  return button_data;
1526 1527
}

1528 1529
static gboolean
gtk_path_bar_check_parent_path (GtkPathBar         *path_bar,
1530
				GFile              *file,
1531 1532 1533 1534
				GtkFileSystem      *file_system)
{
  GList *list;
  GList *current_path = NULL;
1535
  gboolean need_new_fake_root = FALSE;
Jonathan Blandford's avatar