gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
DragNDrop.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 #ifndef DRAGNDROP_H_
18 #define DRAGNDROP_H_
19 
20 #include <vector>
21 #include <string.h>
22 #include "Debug.H"
23 
24 #define DND_EMPTY_ERROR -0+DRAGNDROP_ERROR_OFFSET
25 #define DND_WHICHTARGET_ERROR -1+DRAGNDROP_ERROR_OFFSET
26 
27 class DragNDropDebug : public Debug {
28 public:
30 #ifndef NDEBUG
31  errors[DND_EMPTY_ERROR]=std::string("The DragNDrop known target list is empty. Fill the target list by constructing with a GdkDragContext, or possibly by using the operator<<. ");
32  errors[DND_WHICHTARGET_ERROR]=std::string("The DragNDrop known target list is not empty and has more the one entry, please overload the Widget::chooseTheTargetToUse method to specify which target you want to use in this drop method. ");
33 #endif
34  }
35 };
36 
37 class Widget;
38 
66 class DragNDrop : public std::vector<GtkTargetEntry> {
67  gpointer userData;
68 
69  GtkDestDefaults destFlags;
70  GdkDragAction dragAction;
71  GdkModifierType modifierType;
72 
77  static void addToTargetsStatic(gpointer targetData, gpointer data) {
78  static_cast<DragNDrop*>(data)->addToTargets(GDK_POINTER_TO_ATOM (targetData));
79  }
80 
83  void addToTargets(GdkAtom targetData) {
84  GtkTargetEntry gte;
85  gte.target=gdk_atom_name(targetData);
86  gte.flags=0;
87  gte.info=0;
88  push_back(gte);
89  }
90 
101  void init() {
102  userData=NULL;
103  destFlags=(GtkDestDefaults)(GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT);
104  dragAction=GDK_ACTION_COPY;
105  modifierType=GDK_BUTTON1_MASK; // first mouse button
106  }
107 
108 public:
112  init();
113  }
114 
118  DragNDrop(GdkDragContext *context) {
119  init();
120  g_list_foreach(gdk_drag_context_list_targets(context), reinterpret_cast<GFunc>(addToTargetsStatic), this);
121  //for(GList *iter = gdk_drag_context_list_targets(context); iter != NULL; iter = g_list_next(iter))
122  // addToTargets((GtkTargetEntry*)iter->data);
123  }
124 
126  virtual ~DragNDrop() {}
127 
131  void setUserData(gpointer w){
132  userData=w;
133  }
134 
138  gpointer getUserData(){
139  return userData;
140  }
141 
156  void setDestDefaults(GtkDestDefaults destFlagsIn) {
157  destFlags=destFlagsIn;
158  }
159 
162  GtkDestDefaults getDestDefaults() {
163  return destFlags;
164  }
165 
181  void setDragAction(GdkDragAction dragActionIn) {
182  dragAction=dragActionIn;
183  }
184 
188  GdkDragAction getDragAction() {
189  return dragAction;
190  }
191 
233  void getModifierType(GdkModifierType modifierTypeIn) {
234  modifierType=modifierTypeIn;
235  }
236 
239  GdkModifierType getModifierType() {
240  return modifierType;
241  }
242 
246  GtkTargetEntry *getTargetArray() {
247  if (size()<1) {
248  DragNDropDebug().evaluateError(DND_EMPTY_ERROR);
249  return NULL;
250  }
251  return &operator[](0);
252  }
253 
257  DragNDrop &operator<<(const GtkTargetEntry &gte) {
258  push_back(gte);
259  return *this;
260  }
261 
265  DragNDrop &operator>>(const GtkTargetEntry &gte) {
266  for (std::vector<GtkTargetEntry>::iterator gtei=begin(); gtei<end(); gtei++)
267  if (strcmp(gtei->target, gte.target)==0)
268  if (gtei->flags==gte.flags)
269  if (gtei->info==gte.info) {
270  erase(gtei);
271  break;
272  }
273  return *this;
274  }
275 
279  setUserData(&w);
280  }
281 };
282 #endif // DRAGNDROP_H_
GdkDragAction dragAction
Action to take once the data is dropped.
Definition: DragNDrop.H:70
#define DND_WHICHTARGET_ERROR
Definition: DragNDrop.H:25
GdkModifierType getModifierType()
Definition: DragNDrop.H:239
void setDestDefaults(GtkDestDefaults destFlagsIn)
Definition: DragNDrop.H:156
void getModifierType(GdkModifierType modifierTypeIn)
Definition: DragNDrop.H:233
static void addToTargetsStatic(gpointer targetData, gpointer data)
Definition: DragNDrop.H:77
size(signal)
GtkDestDefaults getDestDefaults()
Definition: DragNDrop.H:162
#define DND_EMPTY_ERROR
Definition: DragNDrop.H:24
void setDragAction(GdkDragAction dragActionIn)
Definition: DragNDrop.H:181
std::map< int, std::string > errors
This will contain a map between error numbers and descriptive std::strings for the errors...
Definition: Debug.H:115
DragNDrop & operator<<(const GtkTargetEntry &gte)
Definition: DragNDrop.H:257
DragNDrop & operator>>(const GtkTargetEntry &gte)
Definition: DragNDrop.H:265
gpointer userData
If we want DND callbacks to operate on different user data, then this will store it here...
Definition: DragNDrop.H:67
void setUserData(gpointer w)
Definition: DragNDrop.H:131
virtual ~DragNDrop()
Destructor.
Definition: DragNDrop.H:126
GtkDestDefaults destFlags
The drop flags, see setDestDefaults.
Definition: DragNDrop.H:69
gpointer getUserData()
Definition: DragNDrop.H:138
void addToTargets(GdkAtom targetData)
Definition: DragNDrop.H:83
Definition: Widget.H:31
Definition: Debug.H:112
void init()
Definition: DragNDrop.H:101
GdkDragAction getDragAction()
Definition: DragNDrop.H:188
GdkModifierType modifierType
The modifier which initiates the drag and drop process.
Definition: DragNDrop.H:71
DragNDrop & operator<<(Widget &w)
Definition: DragNDrop.H:278
GtkTargetEntry * getTargetArray()
Definition: DragNDrop.H:246
DragNDrop(GdkDragContext *context)
Definition: DragNDrop.H:118
DragNDropDebug(void)
Definition: DragNDrop.H:29
gtkIOStream: /tmp/gtkiostream/include/DragNDrop.H Source File
GTK+ IOStream  Beta