2
0
Эх сурвалжийг харах

Added support for drag and drop. Fixes #21.

woollybah 6 жил өмнө
parent
commit
0411c55f1a

+ 20 - 0
gtk3maxgui.mod/gtkcommon.bmx

@@ -233,6 +233,7 @@ Extern
 	Function g_signal_cb4:Int(gtkwidget:Byte Ptr, name:Byte Ptr, callback(widget:Byte Ptr,url:Byte Ptr,stream:Byte Ptr,gadget:Object),gadget:Object,destroyhandler(data:Byte Ptr,user: Byte Ptr),flag:Int) = "int g_signal_connect_data(BBBYTE*, BBBYTE*, BBBYTE*, BBBYTE*, BBBYTE*, int) !"
 	Function g_signal_cb4a:Int(gtkwidget:Byte Ptr, name:Byte Ptr, callback:Int(widget:Byte Ptr,val1:Int,val2:Double,gadget:Object),gadget:Object,destroyhandler(data:Byte Ptr,user: Byte Ptr),flag:Int) = "int g_signal_connect_data(BBBYTE*, BBBYTE*, BBBYTE*, BBBYTE*, BBBYTE*, int) !"
 	Function g_signal_cb5:Int(gtkwidget:Byte Ptr, name:Byte Ptr, callback(widget:Byte Ptr,val1:Int,val2:Int,val3:Int,gadget:Object),gadget:Object,destroyhandler(data:Byte Ptr,user: Byte Ptr),flag:Int) = "int g_signal_connect_data(BBBYTE*, BBBYTE*, BBBYTE*, BBBYTE*, BBBYTE*, int) !"
+	Function g_signal_cb8:Int(gtkwidget:Byte Ptr, name:Byte Ptr, callback(widget:Byte Ptr,context:Byte Ptr, val1:Int,val2:Int,data:Byte Ptr,val3:Int,val4:Int,gadget:Object),gadget:Object,destroyhandler(data:Byte Ptr,user: Byte Ptr),flag:Int) = "int g_signal_connect_data(BBBYTE*, BBBYTE*, BBBYTE*, BBBYTE*, BBBYTE*, int) !"
 	Function g_signal_handler_disconnect(gtkwidget:Byte Ptr, handlerid:Long)
 	Function g_signal_tabchange:Int(gtkwidget:Byte Ptr, name:Byte Ptr, callback(widget:Byte Ptr,a:Byte Ptr, index:Int,gadget:Object),gadget:Object,destroyhandler(data:Byte Ptr,user: Byte Ptr),flag:Int) = "int g_signal_connect_data(BBBYTE*, BBBYTE*, BBBYTE*, BBBYTE*, BBBYTE*, int) !"
 	
@@ -560,6 +561,10 @@ Extern
 	' GdkMonitor
 	'Function gdk_display_get_primary_monitor:Byte Ptr(display:Byte Ptr)
 	
+	' drag n drop
+	Function gtk_drag_dest_set(handle:Byte Ptr, flags:Int, targets:Byte Ptr, numTargets:Int, actions:Int)
+	Function gtk_drag_dest_add_uri_targets(handle:Byte Ptr)
+	
 	' glue
 	Function bmx_gtk3_gtkdesktop_gethertz:Int()
 	Function bmx_gtk3_gvalue_new:Byte Ptr(_type:Int)
@@ -585,6 +590,7 @@ Extern
 	Function bmx_gtk3maxgui_gdkeventwindowstate(event:Byte Ptr, state:Int Ptr)
 	Function bmx_gtk3maxgui_gdkeventmotiondevice:Byte Ptr(event:Byte Ptr)
 
+	Function bmx_gtk3_selection_data_get_uris:String[](data:Byte Ptr)
 End Extern
 
 ' gadget identifiers
@@ -913,6 +919,20 @@ Const GDK_XTERM:Int = 152
 Const GDK_BLANK_CURSOR:Int = -2
 Const GDK_CURSOR_IS_PIXMAP:Int = -1
 
+' GtkDestDefaults
+Const GTK_DEST_DEFAULT_MOTION:Int = 1 Shl 0
+Const GTK_DEST_DEFAULT_HIGHLIGHT:Int = 1 Shl 1
+Const GTK_DEST_DEFAULT_DROP:Int = 1 Shl 2
+Const GTK_DEST_DEFAULT_ALL:Int = $07
+
+' GdkDragAction
+Const GDK_ACTION_DEFAULT:Int = 1 Shl 0
+Const GDK_ACTION_COPY:Int = 1 Shl 1
+Const GDK_ACTION_MOVE:Int = 1 Shl 2
+Const GDK_ACTION_LINK:Int = 1 Shl 3
+Const GDK_ACTION_PRIVATE:Int = 1 Shl 4
+Const GDK_ACTION_ASK:Int = 1 Shl 5
+
 ' List of application windows
 ' We use it for SetPointer etc.
 Global gtkWindows:TList = New TList

+ 9 - 7
gtk3maxgui.mod/gtkgadget.bmx

@@ -153,6 +153,7 @@ End Rem
 		' remove reference from global reference map
 		If handle Then
 			GadgetMap.Remove(handle)
+			handle = Null
 		End If
 		
 		connectionMap.Clear()
@@ -596,14 +597,12 @@ Type TGTKWindow Extends TGTKContainer
 		addConnection("window-state-event", g_signal_cb3_ret(handle, "window-state-event", OnWindowStateChange, Self, Destroy, 0))
 
 		If style & WINDOW_ACCEPTFILES Then
-
 			'Local entries:Byte Ptr
 			
-			'gtk_drag_dest_set(container, GTK_DEST_DEFAULT_DROP | GTK_DEST_DEFAULT_HIGHLIGHT, Varptr entries, 0, GDK_ACTION_COPY)
-			'gtk_drag_dest_add_uri_targets(container)
-			'gtk_drag_dest_add_text_targets(container)
+			gtk_drag_dest_set(handle, GTK_DEST_DEFAULT_ALL, Null, 0, GDK_ACTION_COPY)
+			gtk_drag_dest_add_uri_targets(handle)
 			
-			'g_signal_cb6(container, "drag-drop", OnDragDrop, Self, Destroy, 0)
+			addConnection("drag-drop", g_signal_cb8(handle, "drag-data-received", OnDragDrop, Self, Destroy, 0))
 		End If
 		
 		' used for tabbers - ensure they are redrawn properly when required
@@ -797,8 +796,11 @@ Type TGTKWindow Extends TGTKContainer
 		Return False
 	End Function
 	
-	Function OnDragDrop:Int(widget:Byte Ptr, context:Byte Ptr, x:Int, y:Int, time:Int, obj:Object)
-Print "OnDragDrop"
+	Function OnDragDrop(widget:Byte Ptr, context:Byte Ptr, x:Int, y:Int, data:Byte Ptr, info:Int, time:Int, obj:Object)
+		Local uris:String[] = bmx_gtk3_selection_data_get_uris(data)
+		If uris Then
+			PostGuiEvent EVENT_WINDOWACCEPT,TGadget(obj),0,0,x,y,uris[0].Replace("file://", "")
+		End If
 	End Function
 
 	Method SetShape:Int(x:Int,y:Int,w:Int,h:Int)

+ 97 - 72
gtk3maxgui.mod/gtkglue.c

@@ -1,72 +1,97 @@
-/*
-  Copyright (c) 2006-2018 Bruce A Henderson
- 
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-  
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-  
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*/ 
-#include "gtk/gtk.h"
-#include "gdk/gdk.h"
-
-int bmx_gtk3_gtkdesktop_gethertz() {
-
-#if GTK_MINOR_VERSION >= 22
-	GdkMonitor * monitor = gdk_display_get_primary_monitor(gdk_display_get_default());
-	int rate = gdk_monitor_get_refresh_rate(monitor);
-	return rate / 1000;
-#else
-	return 60;
-#endif
-
-}
-
-GValue * bmx_gtk3_gvalue_new(int type) {
-	GValue * val = (GValue*)calloc(1, sizeof(GValue));
-	return g_value_init(val, type);
-}
-
-void bmx_gtk3_gvalue_free(GValue * val) {
-	free(val);
-}
-
-GtkTreeIter * bmx_gtk3_gtktreeiter_new() {
-	return (GtkTreeIter*)calloc(1, sizeof(GtkTreeIter));
-}
-
-void bmx_gtk3_gtktreeiter_free(GtkTreeIter * iter) {
-	free(iter);
-}
-
-PangoFontDescription * bmx_gtk3_stylecontext_get_fontdesc(GtkStyleContext *context) {
-	return gtk_style_context_get_font(context, GTK_STATE_FLAG_NORMAL);
-}
-
-GtkTextIter * bmx_gtk3_gtktextiter_new() {
-	return (GtkTextIter*)calloc(1, sizeof(GtkTextIter));
-}
-
-void bmx_gtk3_gtktextiter_free(GtkTextIter * iter) {
-	free(iter);
-}
-
-GtkTextTag * bmx_gtk3_set_text_tag_style(GtkTextBuffer *buffer, const gchar *tag, GdkRGBA * color, int _style, int _weight, int _under, int _strike) { 
-	return gtk_text_buffer_create_tag(buffer, tag, "foreground-gdk", color, "style", _style, "weight", _weight, "underline", _under, "strikethrough", _strike, 0);
-}
-
-GtkTextTag * bmx_gtk3_set_text_bg_tag(GtkTextBuffer *buffer, const gchar *tag, GdkRGBA * color) {
-	return gtk_text_buffer_create_tag(buffer, tag, "background-gdk", color, 0);
-}
+/*
+  Copyright (c) 2006-2018 Bruce A Henderson
+ 
+  Permission is hereby granted, free of charge, to any person obtaining a copy
+  of this software and associated documentation files (the "Software"), to deal
+  in the Software without restriction, including without limitation the rights
+  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+  copies of the Software, and to permit persons to whom the Software is
+  furnished to do so, subject to the following conditions:
+  
+  The above copyright notice and this permission notice shall be included in
+  all copies or substantial portions of the Software.
+  
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+  THE SOFTWARE.
+*/ 
+#include "gtk/gtk.h"
+#include "gdk/gdk.h"
+
+#include "brl.mod/blitz.mod/blitz.h"
+
+int bmx_gtk3_gtkdesktop_gethertz() {
+
+#if GTK_MINOR_VERSION >= 22
+	GdkMonitor * monitor = gdk_display_get_primary_monitor(gdk_display_get_default());
+	int rate = gdk_monitor_get_refresh_rate(monitor);
+	return rate / 1000;
+#else
+	return 60;
+#endif
+
+}
+
+GValue * bmx_gtk3_gvalue_new(int type) {
+	GValue * val = (GValue*)calloc(1, sizeof(GValue));
+	return g_value_init(val, type);
+}
+
+void bmx_gtk3_gvalue_free(GValue * val) {
+	free(val);
+}
+
+GtkTreeIter * bmx_gtk3_gtktreeiter_new() {
+	return (GtkTreeIter*)calloc(1, sizeof(GtkTreeIter));
+}
+
+void bmx_gtk3_gtktreeiter_free(GtkTreeIter * iter) {
+	free(iter);
+}
+
+PangoFontDescription * bmx_gtk3_stylecontext_get_fontdesc(GtkStyleContext *context) {
+	return gtk_style_context_get_font(context, GTK_STATE_FLAG_NORMAL);
+}
+
+GtkTextIter * bmx_gtk3_gtktextiter_new() {
+	return (GtkTextIter*)calloc(1, sizeof(GtkTextIter));
+}
+
+void bmx_gtk3_gtktextiter_free(GtkTextIter * iter) {
+	free(iter);
+}
+
+GtkTextTag * bmx_gtk3_set_text_tag_style(GtkTextBuffer *buffer, const gchar *tag, GdkRGBA * color, int _style, int _weight, int _under, int _strike) { 
+	return gtk_text_buffer_create_tag(buffer, tag, "foreground-gdk", color, "style", _style, "weight", _weight, "underline", _under, "strikethrough", _strike, 0);
+}
+
+GtkTextTag * bmx_gtk3_set_text_bg_tag(GtkTextBuffer *buffer, const gchar *tag, GdkRGBA * color) {
+	return gtk_text_buffer_create_tag(buffer, tag, "background-gdk", color, 0);
+}
+
+BBArray * bmx_gtk3_selection_data_get_uris(GtkSelectionData * data) {
+	gchar ** uris = gtk_selection_data_get_uris(data);
+	
+	if (uris == NULL) {
+		return &bbEmptyString;
+	}
+	
+	int count = 0;
+	while (uris[count] && count < 128) {
+		count++;
+	}
+	
+	BBArray *p=bbArrayNew1D( "$",count );
+	BBString **s=(BBString**)BBARRAYDATA( p,p->dims );
+	for( int i=0;i<count;++i ){
+		s[i]=bbStringFromUTF8String( uris[i] );
+	}
+	
+	g_strfreev(uris);
+	
+	return p;
+}