You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.9KB

  1. //
  2. // "$Id: howto-drag-and-drop.cxx 8221 2011-01-08 18:58:50Z greg.ercolano $"
  3. //
  4. // A simple demo of drag+drop with FLTK.
  5. // Originally from erco's cheat sheet, permission by author.
  6. // Inspired by Michael Sephton's original example posted on fltk.general.
  7. //
  8. // When you run the program, just drag the red square over
  9. // to the green square to show a 'drag and drop' sequence.
  10. //
  11. // Copyright 1998-2010 by Bill Spitzak and others.
  12. //
  13. // This library is free software; you can redistribute it and/or
  14. // modify it under the terms of the GNU Library General Public
  15. // License as published by the Free Software Foundation; either
  16. // version 2 of the License, or (at your option) any later version.
  17. //
  18. // This library is distributed in the hope that it will be useful,
  19. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. // Library General Public License for more details.
  22. //
  23. // You should have received a copy of the GNU Library General Public
  24. // License along with this library; if not, write to the Free Software
  25. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  26. // USA.
  27. //
  28. // Please report all bugs and problems on the following page:
  29. //
  30. // http://www.fltk.org/str.php
  31. //
  32. #include <stdio.h>
  33. #include <FL/Fl.H>
  34. #include <FL/Fl_Window.H>
  35. #include <FL/Fl_Box.H>
  36. // SIMPLE SENDER CLASS
  37. class Sender : public Fl_Box {
  38. public:
  39. // Ctor
  40. Sender(int x,int y,int w,int h) : Fl_Box(x,y,w,h) {
  41. box(FL_FLAT_BOX);
  42. color(9);
  43. label("Drag\nfrom\nhere..");
  44. }
  45. // Sender event handler
  46. int handle(int event) {
  47. int ret = Fl_Box::handle(event);
  48. switch ( event ) {
  49. case FL_PUSH: { // do 'copy/dnd' when someone clicks on box
  50. const char *msg = "It works!";
  51. Fl::copy(msg,strlen(msg),0);
  52. Fl::dnd();
  53. ret = 1;
  54. break;
  55. }
  56. }
  57. return(ret);
  58. }
  59. };
  60. // SIMPLE RECEIVER CLASS
  61. class Receiver : public Fl_Box {
  62. public:
  63. // Ctor
  64. Receiver(int x,int y,int w,int h) : Fl_Box(x,y,w,h) {
  65. box(FL_FLAT_BOX); color(10); label("..to\nhere");
  66. }
  67. // Receiver event handler
  68. int handle(int event) {
  69. int ret = Fl_Box::handle(event);
  70. switch ( event ) {
  71. case FL_DND_ENTER: // return(1) for these events to 'accept' dnd
  72. case FL_DND_DRAG:
  73. case FL_DND_RELEASE:
  74. ret = 1;
  75. break;
  76. case FL_PASTE: // handle actual drop (paste) operation
  77. label(Fl::event_text());
  78. fprintf(stderr, "Pasted '%s'\n", Fl::event_text());
  79. ret = 1;
  80. break;
  81. }
  82. return(ret);
  83. }
  84. };
  85. int main(int argc, char **argv) {
  86. // Create sender window and widget
  87. Fl_Window win_a(0,0,200,100,"Sender");
  88. Sender a(0,0,100,100);
  89. win_a.end();
  90. win_a.show();
  91. // Create receiver window and widget
  92. Fl_Window win_b(400,0,200,100,"Receiver");
  93. Receiver b(100,0,100,100);
  94. win_b.end();
  95. win_b.show();
  96. return(Fl::run());
  97. }
  98. //
  99. // End of "$Id: howto-drag-and-drop.cxx 8221 2011-01-08 18:58:50Z greg.ercolano $".
  100. //