Flu_DND.H 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // $Id: Flu_DND.h,v 1.17 2003/08/20 16:29:41 jbryan Exp $
  2. /***************************************************************
  3. * FLU - FLTK Utility Widgets
  4. * Copyright (C) 2002 Ohio Supercomputer Center, Ohio State University
  5. *
  6. * This file and its content is protected by a software license.
  7. * You should have received a copy of this license with this file.
  8. * If not, please contact the Ohio Supercomputer Center immediately:
  9. * Attn: Jason Bryan Re: FLU 1224 Kinnear Rd, Columbus, Ohio 43212
  10. *
  11. ***************************************************************/
  12. #ifndef _FLU_DND_H
  13. #define _FLU_DND_H
  14. #include <FL/Fl.H>
  15. #include <FL/fl_draw.H>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "FLU/Flu_Enumerations.H"
  19. #define FLU_DND_MAX_TYPES 32
  20. //! This class encapsulates the event state for a single dragged object, and is designed to work exclusively with Flu_DND
  21. class FLU_EXPORT Flu_DND_Event
  22. {
  23. friend class Flu_DND;
  24. public:
  25. //! Default constructor
  26. Flu_DND_Event();
  27. //! Default destructor
  28. ~Flu_DND_Event();
  29. //! \return \c true if currently dragging an object, \c false otherwise
  30. inline bool event_is_valid() const
  31. { return dragging; }
  32. //! \return \c true if the dragged object is normal FLTK text (see Fl::copy() ), else \c false if it is an object derived from Flu_DND
  33. inline bool event_is_text() const
  34. { return( _text != 0 ); }
  35. //! \return \c true if the dragged object is an object derived from Flu_DND, else \c false if it is normal FLTK text (see Fl::copy() )
  36. inline bool event_is_other() const
  37. { return( _text == 0 ); }
  38. //! \return the text from Fl::copy() if this event is a text event (see event_is_text() )
  39. /*! \note This is valid only after the object has been dropped (i.e. in Flu_DND::on_dnd_drop() )*/
  40. inline const char* text() const
  41. { return _text; }
  42. //! \return the dragged object data as added in Flu_DND::dnd_grab() if this event is an "other" event (see event_is_other() )
  43. /*! \note This is valid only after the object has been dropped (i.e. in Flu_DND::on_dnd_drop() )*/
  44. inline void* data() const
  45. { return _data; }
  46. //! \return the type of the data object that was dropped. This is \c NULL for FLTK text events
  47. /*! \note This is valid only after the object has been dropped (i.e. in Flu_DND::on_dnd_drop() )*/
  48. inline const char* data_type() const
  49. { return _dataType; }
  50. //! \return \c true if the type of the data object that was dropped is equal to \b t, \c false otherwise
  51. inline bool is_data_type( const char *t ) const
  52. { if( !_dataType ) return 0; else return( strcmp( _dataType, t ) == 0 ); }
  53. //! \return the x coordinate (from Fl::event_x() ) of when the object was grabbed
  54. inline int grab_x() const
  55. { return _grab_x; }
  56. //! \return the y coordinate (from Fl::event_y() ) of when the object was grabbed
  57. inline int grab_y() const
  58. { return _grab_y; }
  59. //! \return the x coordinate (from Fl::event_x() ) of when the object was dropped
  60. inline int drop_x() const
  61. { return _drop_x; }
  62. //! \return the y coordinate (from Fl::event_y() ) of when the object was dropped
  63. inline int drop_y() const
  64. { return _drop_y; }
  65. private:
  66. bool dragging;
  67. void *objUnderMouse;
  68. char *_text, *_dataType;
  69. void *_data;
  70. int _grab_x, _grab_y, _drop_x, _drop_y;
  71. bool exit;
  72. void clear();
  73. };
  74. //! This class augments the existing FLTK drag-n-drop feature, working with Flu_DND_Event to achieve new functionality
  75. /*! It adds the ability to create specific DND objects, allowing classes that handle
  76. DND to discriminate between which objects are "allowed" to be dropped on them.
  77. Almost all functions are protected, since only a derived class should be in charge of what kinds of events
  78. to handle and what kinds of objects to support.
  79. */
  80. class FLU_EXPORT Flu_DND
  81. {
  82. public:
  83. //! Set the function that is called when the dragged object is dropped. This is called in addition to the member function on_dnd_drop()
  84. inline void dnd_callback( void (*cb)(const Flu_DND_Event*,void*), void *cbd = 0 )
  85. { dndCallback = cb; dndCallbackData = cbd; }
  86. protected:
  87. //! Default constructor
  88. Flu_DND( const char *thisType );
  89. //! Default destructor
  90. virtual ~Flu_DND();
  91. //! See Flu_DND_Event::event_is_text()
  92. inline bool dnd_event_is_text() const
  93. { return( dndEvent.dragging && !dndEvent._data ); }
  94. //! See Flu_DND_Event::event_is_other()
  95. inline bool dnd_event_is_other() const
  96. { return( dndEvent.dragging && dndEvent._data ); }
  97. //! See Flu_DND_Event::event_is_valid()
  98. inline bool dnd_is_dragging() const
  99. { return dndEvent.dragging; }
  100. //! See Flu_DND_Event::is_data_type()
  101. inline bool dnd_is_data_type( const char *t ) const
  102. { return dndEvent.is_data_type( t ); }
  103. //! Set whether standard FLTK text events can be dropped on this object
  104. inline void dnd_allow_text( bool b )
  105. { allowTextEvents = b; }
  106. //! Get whether standard FLTK text events can be dropped on this object
  107. inline bool dnd_allow_text() const
  108. { return allowTextEvents; }
  109. //! Set whether this object can be dragged to another object
  110. inline void dnd_allow_dragging( bool b )
  111. { allowDragging = b; }
  112. //! Get whether this object can be dragged to another object
  113. inline bool dnd_allow_dragging() const
  114. { return allowDragging; }
  115. //! Set whether this object can have other objects dropped on it
  116. inline void dnd_allow_dropping( bool b )
  117. { allowDropping = b; }
  118. //! Get whether this object can have other objects dropped on it
  119. inline bool dnd_allow_dropping() const
  120. { return allowDropping; }
  121. //! Add type \b t to the list of types that are allowed to be dropped on this object (up to a compiled maximum of \c \b FLU_DND_MAX_TYPES)
  122. void dnd_allow_type( const char *t );
  123. //! \return \c true if type \b t is allowed to be dropped on this object, \c false otherwise
  124. bool dnd_type_allowed( const char *t ) const;
  125. //! Descendents should call this when they detect themselves being grabbed for a drag-n-drop
  126. void dnd_grab( void *data, const char *type );
  127. //! Descendents should call this at the start of their handle() method to handle DND processing
  128. int dnd_handle( int event );
  129. //! Descendents can override this to respond to when the mouse is let go during a drag-n-drop (regardless of whether the item was dropped on you)
  130. virtual void on_dnd_release();
  131. //! Descendents can override this to respond to when the mouse has entered you during a drag-n-drop
  132. virtual void on_dnd_enter();
  133. //! Descendents can override this to respond to when the mouse has left you during a drag-n-drop
  134. virtual void on_dnd_leave();
  135. //! Descendents should override this to respond to when the dragged object has been dropped on you
  136. virtual void on_dnd_drop( const Flu_DND_Event *e );
  137. //! Descendents should override this to indicate whether the currently dragged item is allowed to be dropped on you (for example, if the item can only be dropped at certain locations)
  138. virtual bool on_dnd_drag( int X, int Y );
  139. private:
  140. bool ok2drop();
  141. static Flu_DND_Event dndEvent;
  142. bool allowTextEvents, allowDragging, allowDropping;
  143. char* _thisType;
  144. char* allowedTypes[FLU_DND_MAX_TYPES];
  145. int nTypes;
  146. void (*dndCallback)(const Flu_DND_Event*,void*);
  147. void *dndCallbackData;
  148. };
  149. #endif