custom_drag.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
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;
18class SkBitmap;
19
20// Base class for programatically generated drags.
21class CustomDrag {
22 protected:
23  explicit CustomDrag(SkBitmap* icon, int code_mask, GdkDragAction action);
24  virtual ~CustomDrag();
25
26  virtual void OnDragDataGet(GtkWidget* widget, GdkDragContext* context,
27                             GtkSelectionData* selection_data,
28                             guint target_type, guint time) = 0;
29
30 private:
31  CHROMEGTK_CALLBACK_1(CustomDrag, void, OnDragBegin, GdkDragContext*);
32  CHROMEGTK_CALLBACK_1(CustomDrag, void, OnDragEnd, GdkDragContext*);
33
34  // Since this uses a virtual function, we can't use a macro.
35  static void OnDragDataGetThunk(GtkWidget* widget, GdkDragContext* context,
36                                 GtkSelectionData* selection_data,
37                                 guint target_type, guint time,
38                                 CustomDrag* custom_drag) {
39    return custom_drag->OnDragDataGet(widget, context, selection_data,
40                                      target_type, time);
41  }
42
43  // Can't use a OwnedWidgetGtk because the initialization of GtkInvisible
44  // sinks the reference.
45  GtkWidget* drag_widget_;
46
47  GdkPixbuf* pixbuf_;
48
49  DISALLOW_COPY_AND_ASSIGN(CustomDrag);
50};
51
52// Encapsulates functionality for drags of download items.
53class DownloadItemDrag : public CustomDrag {
54 public:
55  // Sets |widget| as a source for drags pertaining to |item|. No
56  // DownloadItemDrag object is created.
57  // It is safe to call this multiple times with different values of |icon|.
58  static void SetSource(GtkWidget* widget, DownloadItem* item, SkBitmap* icon);
59
60  // Creates a new DownloadItemDrag, the lifetime of which is tied to the
61  // system drag.
62  static void BeginDrag(const DownloadItem* item, SkBitmap* icon);
63
64 private:
65  DownloadItemDrag(const DownloadItem* item, SkBitmap* icon);
66  virtual ~DownloadItemDrag();
67
68  virtual void OnDragDataGet(GtkWidget* widget, GdkDragContext* context,
69                             GtkSelectionData* selection_data,
70                             guint target_type, guint time);
71
72  const DownloadItem* download_item_;
73
74  DISALLOW_COPY_AND_ASSIGN(DownloadItemDrag);
75};
76
77// Encapsulates functionality for drags of one or more bookmarks.
78class BookmarkDrag : public CustomDrag {
79 public:
80  // Creates a new BookmarkDrag, the lifetime of which is tied to the
81  // system drag.
82  static void BeginDrag(Profile* profile,
83                        const std::vector<const BookmarkNode*>& nodes);
84
85 private:
86  BookmarkDrag(Profile* profile,
87               const std::vector<const BookmarkNode*>& nodes);
88  virtual ~BookmarkDrag();
89
90  virtual void OnDragDataGet(GtkWidget* widget, GdkDragContext* context,
91                             GtkSelectionData* selection_data,
92                             guint target_type, guint time);
93
94  Profile* profile_;
95  std::vector<const BookmarkNode*> nodes_;
96
97  DISALLOW_COPY_AND_ASSIGN(BookmarkDrag);
98};
99
100#endif  // CHROME_BROWSER_UI_GTK_CUSTOM_DRAG_H_
101