summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/gtk/custom_drag.h
blob: 788325b37d713be6d1541deee8a7bc47e5d3843d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_GTK_CUSTOM_DRAG_H_
#define CHROME_BROWSER_UI_GTK_CUSTOM_DRAG_H_

#include <gtk/gtk.h>
#include <vector>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "ui/base/gtk/gtk_signal.h"

class BookmarkNode;
class Profile;

namespace content {
class DownloadItem;
}

namespace gfx {
class Image;
}

// Base class for programatically generated drags.
class CustomDrag {
 protected:
  CustomDrag(gfx::Image* icon, int code_mask, GdkDragAction action);
  virtual ~CustomDrag();

  virtual void OnDragDataGet(GtkWidget* widget, GdkDragContext* context,
                             GtkSelectionData* selection_data,
                             guint target_type, guint time) = 0;

 private:
  CHROMEGTK_CALLBACK_1(CustomDrag, void, OnDragBegin, GdkDragContext*);
  CHROMEGTK_CALLBACK_1(CustomDrag, void, OnDragEnd, GdkDragContext*);

  // Since this uses a virtual function, we can't use a macro.
  static void OnDragDataGetThunk(GtkWidget* widget, GdkDragContext* context,
                                 GtkSelectionData* selection_data,
                                 guint target_type, guint time,
                                 CustomDrag* custom_drag) {
    return custom_drag->OnDragDataGet(widget, context, selection_data,
                                      target_type, time);
  }

  // Can't use a OwnedWidgetGtk because the initialization of GtkInvisible
  // sinks the reference.
  GtkWidget* drag_widget_;

  // The image for the drag. The lifetime of the image should be managed outside
  // this object. Most icons are owned by the IconManager.
  gfx::Image* image_;

  DISALLOW_COPY_AND_ASSIGN(CustomDrag);
};

// Encapsulates functionality for drags of download items.
class DownloadItemDrag : public CustomDrag {
 public:
  // Sets |widget| as a source for drags pertaining to |item|. No
  // DownloadItemDrag object is created.
  // It is safe to call this multiple times with different values of |icon|.
  static void SetSource(GtkWidget* widget,
                        const content::DownloadItem* item,
                        gfx::Image* icon);

  // Creates a new DownloadItemDrag, the lifetime of which is tied to the
  // system drag.
  static void BeginDrag(const content::DownloadItem* item, gfx::Image* icon);

 private:
  class DragData;

  DownloadItemDrag(const content::DownloadItem* item, gfx::Image* icon);
  virtual ~DownloadItemDrag();

  virtual void OnDragDataGet(GtkWidget* widget, GdkDragContext* context,
                             GtkSelectionData* selection_data,
                             guint target_type, guint time) OVERRIDE;

  scoped_ptr<DragData> drag_data_;

  DISALLOW_COPY_AND_ASSIGN(DownloadItemDrag);
};

// Encapsulates functionality for drags of one or more bookmarks.
class BookmarkDrag : public CustomDrag {
 public:
  // Creates a new BookmarkDrag, the lifetime of which is tied to the
  // system drag.
  static void BeginDrag(Profile* profile,
                        const std::vector<const BookmarkNode*>& nodes);

 private:
  BookmarkDrag(Profile* profile,
               const std::vector<const BookmarkNode*>& nodes);
  virtual ~BookmarkDrag();

  virtual void OnDragDataGet(GtkWidget* widget, GdkDragContext* context,
                             GtkSelectionData* selection_data,
                             guint target_type, guint time) OVERRIDE;

  Profile* profile_;
  std::vector<const BookmarkNode*> nodes_;

  DISALLOW_COPY_AND_ASSIGN(BookmarkDrag);
};

#endif  // CHROME_BROWSER_UI_GTK_CUSTOM_DRAG_H_