Advertisement
Guest User

Drag n drop, GTK and C

a guest
Jul 21st, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. /*
  2. * icon.c - GtkIconView DnD sample (non-working) application
  3. *
  4. * For non-working example compile with:
  5. * gcc -o icon icon.c $(pkg-config --cflags --libs gtk+-2.0)
  6. *
  7. * else
  8. * gcc -DNONEMPTY -o icon icon.c $(pkg-config --cflags --libs gtk+-2.0)
  9. *
  10. */
  11.  
  12. #include <gtk/gtk.h>
  13.  
  14. enum
  15. {
  16. COL_TEXT = 0,
  17. COL_PICT,
  18. N_COLS
  19. };
  20.  
  21. static GtkTreeModel *create_model( gboolean populate );
  22.  
  23. static GtkWidget *create_icon_view( gboolean populate );
  24.  
  25. static gboolean cb_drag_drop( GtkWidget *icon,
  26. GdkDragContext *context,
  27. gint x,
  28. gint y,
  29. guint time,
  30. gpointer data );
  31.  
  32. int
  33. main( int argc,
  34. char **argv )
  35. {
  36. GtkWidget *window;
  37. GtkWidget *hbox;
  38. GtkWidget *icon1;
  39. GtkWidget *icon2;
  40.  
  41. gtk_init( &argc, &argv );
  42.  
  43. window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
  44. gtk_window_set_default_size( GTK_WINDOW( window ), 400, 300 );
  45. gtk_container_set_border_width( GTK_CONTAINER( window ), 6 );
  46. g_signal_connect( G_OBJECT( window ), "destroy",
  47. G_CALLBACK( gtk_main_quit ), NULL );
  48.  
  49. hbox = gtk_hbox_new( TRUE, 6 );
  50. gtk_container_add( GTK_CONTAINER( window ), hbox );
  51.  
  52. icon1 = create_icon_view( TRUE );
  53. gtk_widget_set_name( icon1, "Left iconview" );
  54. gtk_box_pack_start( GTK_BOX( hbox ), icon1, TRUE, TRUE, 0 );
  55.  
  56. icon2 = create_icon_view( FALSE );
  57. gtk_widget_set_name( icon2, "Right iconview" );
  58. gtk_box_pack_start( GTK_BOX( hbox ), icon2, TRUE, TRUE, 0 );
  59.  
  60. gtk_widget_show_all( window );
  61.  
  62. gtk_main();
  63.  
  64. return( 0 );
  65. }
  66.  
  67. static GtkWidget *
  68. create_icon_view( gboolean populate )
  69. {
  70. GtkWidget *icon;
  71. GtkTreeModel *model;
  72.  
  73. static GtkTargetEntry *target = NULL;
  74.  
  75. if( ! target )
  76. {
  77. target = g_slice_new(GtkTargetEntry);
  78. target->target = "InternalTarget";
  79. target->flags = GTK_TARGET_SAME_APP;
  80. target->info = 0;
  81. }
  82.  
  83. model = create_model( populate );
  84. icon = gtk_icon_view_new_with_model( model );
  85. g_object_unref( G_OBJECT( model ) );
  86.  
  87. gtk_icon_view_set_text_column( GTK_ICON_VIEW( icon ), COL_TEXT );
  88. gtk_icon_view_set_pixbuf_column( GTK_ICON_VIEW( icon ), COL_PICT );
  89.  
  90. /* DnD setup */
  91. gtk_icon_view_enable_model_drag_source( GTK_ICON_VIEW( icon ),
  92. GDK_BUTTON1_MASK, target, 1,
  93. GDK_ACTION_MOVE );
  94. gtk_icon_view_enable_model_drag_dest( GTK_ICON_VIEW( icon ), target, 1,
  95. GDK_ACTION_MOVE );
  96.  
  97. g_signal_connect( G_OBJECT( icon ), "drag-drop",
  98. G_CALLBACK( cb_drag_drop ), NULL );
  99.  
  100. return( icon );
  101. }
  102.  
  103. static GtkTreeModel *
  104. create_model( gboolean populate )
  105. {
  106. GtkListStore *store;
  107. GtkTreeIter iter;
  108. GdkPixbuf *pix;
  109.  
  110. store = gtk_list_store_new( N_COLS, G_TYPE_STRING, GDK_TYPE_PIXBUF );
  111.  
  112. if( ! populate )
  113. {
  114. /* This piece of code makes all the difference */
  115. #ifdef NONEMPTY
  116. pix = gdk_pixbuf_new( GDK_COLORSPACE_RGB, FALSE, 8, 50, 50 );
  117. gdk_pixbuf_fill( pix, 0xff000000 ); /* Red */
  118. gtk_list_store_append( store, &iter );
  119. gtk_list_store_set( store, &iter, COL_TEXT, "R 4", COL_PICT, pix, -1 );
  120. g_object_unref( G_OBJECT( pix ) );
  121. #endif /* NONEMPTY */
  122. }
  123. else
  124. {
  125. pix = gdk_pixbuf_new( GDK_COLORSPACE_RGB, FALSE, 8, 50, 50 );
  126. gdk_pixbuf_fill( pix, 0x00000000 ); /* Black */
  127. gtk_list_store_append( store, &iter );
  128. gtk_list_store_set( store, &iter, COL_TEXT, "R 1", COL_PICT, pix, -1 );
  129. g_object_unref( G_OBJECT( pix ) );
  130.  
  131. pix = gdk_pixbuf_new( GDK_COLORSPACE_RGB, FALSE, 8, 50, 50 );
  132. gdk_pixbuf_fill( pix, 0x0000ff00 ); /* Blue */
  133. gtk_list_store_append( store, &iter );
  134. gtk_list_store_set( store, &iter, COL_TEXT, "R 2", COL_PICT, pix, -1 );
  135. g_object_unref( G_OBJECT( pix ) );
  136.  
  137. pix = gdk_pixbuf_new( GDK_COLORSPACE_RGB, FALSE, 8, 50, 50 );
  138. gdk_pixbuf_fill( pix, 0x00ff0000 ); /* Green */
  139. gtk_list_store_append( store, &iter );
  140. gtk_list_store_set( store, &iter, COL_TEXT, "R 3", COL_PICT, pix, -1 );
  141. g_object_unref( G_OBJECT( pix ) );
  142. }
  143.  
  144. return( GTK_TREE_MODEL( store ) );
  145. }
  146.  
  147. static gboolean
  148. cb_drag_drop( GtkWidget *icon,
  149. GdkDragContext *context,
  150. gint x,
  151. gint y,
  152. guint time,
  153. gpointer data )
  154. {
  155. GdkAtom target_type;
  156.  
  157. g_print( "Detected drop on %s\n", gtk_widget_get_name( icon ) );
  158.  
  159. target_type = GDK_POINTER_TO_ATOM( context->targets->data );
  160. gtk_drag_get_data( icon, context, target_type, time );
  161.  
  162. return( TRUE );
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement