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