1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_UI_GTK_CUSTOM_DRAG_H_
6#define CHROME_BROWSER_UI_GTK_CUSTOM_DRAG_H_
7#pragma once
8
9#include <gtk/gtk.h>
10#include <vector>
11
12#include "base/basictypes.h"
13#include "ui/base/gtk/gtk_signal.h"
14
15class BookmarkNode;
16class DownloadItem;
17class Profile;
18
19namespace gfx {
20class Image;
21}
22
23// Base class for programatically generated drags.
24class CustomDrag {
25 protected:
26  explicit CustomDrag(gfx::Image* icon, int code_mask, GdkDragAction action);
27  virtual ~CustomDrag();
28
29  virtual void OnDragDataGet(GtkWidget* widget, GdkDragContext* context,
30                             GtkSelectionData* selection_data,
31                             guint target_type, guint time) = 0;
32
33 private:
34  CHROMEGTK_CALLBACK_1(CustomDrag, void, OnDragBegin, GdkDragContext*);
35  CHROMEGTK_CALLBACK_1(CustomDrag, void, OnDragEnd, GdkDragContext*);
36
37  // Since this uses a virtual function, we can't use a macro.
38  static void OnDragDataGetThunk(GtkWidget* widget, GdkDragContext* context,
39                                 GtkSelectionData* selection_data,
40                                 guint target_type, guint time,
41                                 CustomDrag* custom_drag) {
42    return custom_drag->OnDragDataGet(widget, context, selection_data,
43                                      target_type, time);
44  }
45
46  // Can't use a OwnedWidgetGtk because the initialization of GtkInvisible
47  // sinks the reference.
48  GtkWidget* drag_widget_;
49
50  // The image for the drag. The lifetime of the image should be managed outside
51  // this object. Most icons are owned by the IconManager.
52  gfx::Image* image_;
53
54  DISALLOW_COPY_AND_ASSIGN(CustomDrag);
55};
56
57// Encapsulates functionality for drags of download items.
58class DownloadItemDrag : public CustomDrag {
59 public:
60  // Sets |widget| as a source for drags pertaining to |item|. No
61  // DownloadItemDrag object is created.
62  // It is safe to call this multiple times with different values of |icon|.
63  static void SetSource(GtkWidget* widget,
64                        DownloadItem* item,
65                        gfx::Image* icon);
66
67  // Creates a new DownloadItemDrag, the lifetime of which is tied to the
68  // system drag.
69  static void BeginDrag(const DownloadItem* item, gfx::Image* icon);
70
71 private:
72  DownloadItemDrag(const DownloadItem* item, gfx::Image* icon);
73  virtual ~DownloadItemDrag();
74
75  virtual void OnDragDataGet(GtkWidget* widget, GdkDragContext* context,
76                             GtkSelectionData* selection_data,
77                             guint target_type, guint time);
78
79  const DownloadItem* download_item_;
80
81  DISALLOW_COPY_AND_ASSIGN(DownloadItemDrag);
82};
83
84// Encapsulates functionality for drags of one or more bookmarks.
85class BookmarkDrag : public CustomDrag {
86 public:
87  // Creates a new BookmarkDrag, the lifetime of which is tied to the
88  // system drag.
89  static void BeginDrag(Profile* profile,
90                        const std::vector<const BookmarkNode*>& nodes);
91
92 private:
93  BookmarkDrag(Profile* profile,
94               const std::vector<const BookmarkNode*>& nodes);
95  virtual ~BookmarkDrag();
96
97  virtual void OnDragDataGet(GtkWidget* widget, GdkDragContext* context,
98                             GtkSelectionData* selection_data,
99                             guint target_type, guint time);
100
101  Profile* profile_;
102  std::vector<const BookmarkNode*> nodes_;
103
104  DISALLOW_COPY_AND_ASSIGN(BookmarkDrag);
105};
106
107#endif  // CHROME_BROWSER_UI_GTK_CUSTOM_DRAG_H_
108