Page 1 of 1
Drag'n drop a button
Posted: Fri Aug 13, 2010 7:22 pm
by bombseb
Hello world,
I would like to drag a button from a window in my app and drop it into another window in my app
but it doesn't seems to be possible :
here is a piece of my run loop code :
Code: Select all
Case #PB_Event_Gadget
If EventType () = #PB_EventType_DragStart
Select EventGadget ()
Case #ListeGadgets
DragPrivate (GetGadgetState (#ListeGadgets), #PB_Drag_Copy) ; -> It works for my ListIconGadget
Case #MonBouton
Debug ("Drag") ; -> Never called for my ButtonGadget :-(
EndSelect
EndIf
Re: Drag'n drop a button
Posted: Fri Aug 13, 2010 10:58 pm
by WilliamL
After reading the manual again, it appears to say you can drag 'text' from gadget to gadget. I don't see how that includes dragging a gadget.
I think there is a good example included in the pb package showing dragging between gadgets.
Maybe someone has some more info about dragging gadgets?
Re: Drag'n drop a button
Posted: Sat Aug 14, 2010 9:37 am
by bombseb
Perhaps with DragOSFormat () but there is no example for this function, and I don't understand how to use it

Re: Drag'n drop a button
Posted: Sat Aug 14, 2010 6:06 pm
by WilliamL
On MacOSX, the 'Format' field specifies a clipboard scrap type
Don't know... it appears that DragOSFormats(Formats(), Count [, Actions]) is still text.
I don't think what you want to do can be done by any pb command.
Re: Drag'n drop a button
Posted: Sat Aug 14, 2010 7:35 pm
by TomS
I have no idea, if this works under Mac OS.
I just re-defined the constants with their value.
Maybe, if it doesn't work, there is another way to check if a mousebutton is down or up.
There is no shadow of the gadget, and the cursor stays the same. You'll only see if it worked, when the button disappears on Window 0 and re-appears on Window 1. Very basic.
Code: Select all
EnableExplicit
#WM_LBUTTONDOWN = 513
#WM_LBUTTONUP = 514
Define.i dragging = 0
Define.i x_new, y_new
Define.i event
Enumeration
#Window_0
#Window_1
EndEnumeration
Enumeration
#Button_0
EndEnumeration
OpenWindow(#Window_0, 0, 0, 200, 200, "Window 1", #PB_Window_SystemMenu)
ButtonGadget(#Button_0, 5, 5, 100, 100, "Drag Me")
OpenWindow(#Window_1, 210, 0, 200, 200, "Window 2", #PB_Window_SystemMenu)
Repeat
event = WaitWindowEvent(20)
If event = #WM_LBUTTONDOWN
If WindowMouseX(0)>=5 And WindowMouseX(0)<=100 And WindowMouseY(0)>=5 And WindowMouseY(0)<=100
dragging=1
EndIf
EndIf
If event= #WM_LBUTTONUP
If WindowMouseX(1)>0 And WindowMouseX(1)<200 And WindowMouseY(1)>0 And WindowMouseY(1)<200
If dragging=1
x_new = WindowMouseX(1)
y_new = WindowMouseY(1)
FreeGadget(0)
UseGadgetList(WindowID(1))
ButtonGadget(0,x_new,y_new,100,100,"Dropped")
EndIf
dragging=0
EndIf
EndIf
Until event = #PB_Event_CloseWindow
End
Re: Drag'n drop a button
Posted: Sat Aug 14, 2010 7:44 pm
by WilliamL
Interesting, almost works but #wm_buttonup/#wmbuttondown don't work on the Mac. As far as I can tell, the only way to read mouse up/down is with API and I haven't figured that out yet.
I bet somebody knows how to read the mouse up/down on the Mac... post it here!
Re: Drag'n drop a button
Posted: Sat Aug 14, 2010 8:06 pm
by TomS
Is the Mac OS-Api as easy accessable as the Win-Api?
Just a long shot, but does #kEventMouseDown / #kEventMouseUp work?
Re: Drag'n drop a button
Posted: Sat Aug 14, 2010 8:38 pm
by WilliamL
In a nutshell... this is how it works. This bit of code will put a black dot in the red 'close window' button (usually means the window is modified).
Niffo posted some interesting code that reads the mouse in this thread:
http://www.purebasic.fr/english/viewtop ... ilit=mouse I would hope there would be an easier way that using a 'Callback'!
Code: Select all
ImportC "/System/Library/Frameworks/Carbon.framework/Carbon"
SetWindowModified(WindowRef.l, State.l)
IsWindowModified(WindowRef.l) ; returns Boolean
EndImport
#SearchWnd=1
If OpenWindow(#SearchWnd, 0, 0, 230, 120, "Eventtypes example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
setwindowmodified(WindowID(#SearchWnd),1) ; close (red) window button with black dot
setwindowmodified(WindowID(#SearchWnd),0) ; no black dot
Repeat
Until WindowEvent() = #PB_Event_CloseWindow
EndIf
Re: Drag'n drop a button
Posted: Sun Aug 15, 2010 4:58 pm
by bombseb
I think I'm going to check the mouse click to drag my gadgets
I found a code here to track Mouse up / Mouse down events with OSX api (this is not possible with PB)
But I don't understand where those functions are defined :
InstallEventHandler_ ()
GetWindowEventTarget_ ()
GetControlEventTarget_ ()
are they in the OSX api ? I thought we must use a importC () function to import api functions... like GetEventClass () or HIViewNewTrackingArea ()
Code: Select all
#kEventClassMouse = 'mous'
#kEventClassControl = 'cntl'
#kEventMouseDown = 1
#kEventMouseUp = 2
#kEventMouseMoved = 5
#kEventMouseDragged = 6
#kEventControlTrackingAreaEntered = 23
#kEventControlTrackingAreaExited = 24
ImportC "/System/Library/Frameworks/Carbon.framework/Carbon"
GetEventClass.i(inEvent.l)
HIViewNewTrackingArea(inView.l, inShape.l, inID.q, *outRef)
EndImport
Structure EventTypeSpec
eventClass.l
eventKind.l
EndStructure
OpenWindow(0, 100,100, 300, 200, "Test Window")
ContainerGadget(0, 100, 100, 50, 50, #PB_Container_Single)
ProcedureDLL CallBackEventsHandler(*nextHandler, theEvent, *CallBackProc)
Static OnTheControl.b
Select GetEventClass(theEvent)
Case #kEventClassMouse
If OnTheControl
Select GetEventKind_(theEvent)
Case #kEventMouseDown
Debug "MouseDown"
Case #kEventMouseMoved, #kEventMouseDragged
Debug "MouseMove"
Case #kEventMouseUp
Debug "MouseUp"
EndSelect
EndIf
Case #kEventClassControl ;{
Select GetEventKind_(theEvent)
Case #kEventControlTrackingAreaEntered
OnTheControl = #True
Case #kEventControlTrackingAreaExited
OnTheControl = #False
EndSelect
EndSelect
If *nexthandler : CallNextEventHandler_(*nextHandler, theEvent) : EndIf
EndProcedure
; Window handler
EventHandlerUPP = NewEventHandlerUPP_(@CallBackEventsHandler())
Events_Count = 4
Dim eventTypes.EventTypeSpec(Events_Count - 1)
eventTypes(0)\eventClass = #kEventClassMouse
eventtypes(0)\eventKind = #kEventMouseDown
eventTypes(1)\eventClass = #kEventClassMouse
eventtypes(1)\eventKind = #kEventMouseMoved
eventTypes(2)\eventClass = #kEventClassMouse
eventtypes(2)\eventKind = #kEventMouseDragged
eventTypes(3)\eventClass = #kEventClassMouse
eventtypes(3)\eventKind = #kEventMouseUp
InstallEventHandler_(GetWindowEventTarget_(WindowID(0)), EventHandlerUPP, Events_Count, @eventtypes(), @CallBackEventsHandler(), @handlerref)
; Gadget hanlder
EventHandlerUPP = NewEventHandlerUPP_(@CallBackEventsHandler())
Events_Count = 2
Dim eventTypes.EventTypeSpec(Events_Count - 1)
eventTypes(0)\eventClass = #kEventClassControl
eventtypes(0)\eventKind = #kEventControlTrackingAreaEntered
eventTypes(1)\eventClass = #kEventClassControl
eventtypes(1)\eventKind = #kEventControlTrackingAreaExited
HIViewNewTrackingArea(GadgetID(0), #Null, 0, #Null)
InstallEventHandler_(GetControlEventTarget_(GadgetID(0)), EventHandlerUPP, Events_Count, @eventtypes(), @CallBackEventsHandler(), @handlerref)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
Re: Drag'n drop a button
Posted: Sun Aug 15, 2010 8:05 pm
by WilliamL
It is my understanding that not all API commands have to be imported because they are already used (and imported) by pb.
I am interested in how this works out.. I'd like to use it! I was hoping some of the other Mac posters would contribute what they have learned.