Can an Editor Gadget do both #PB_Drop_Text and #PB_Drop_File

Just starting out? Need help? Post your questions and find answers here.
percy_b
User
User
Posts: 72
Joined: Mon Jan 12, 2015 10:25 am

Can an Editor Gadget do both #PB_Drop_Text and #PB_Drop_File

Post by percy_b »

I'm trying to set up one editor gadget to handle both #PB_Drop_Text and #PB_Drop_Files depending on the Drag & Drop event. However, whenever I make the attempt, I end up receiving the error message "[ERROR] No Drag&Drop event has take place".

Here is a code snippet of what I'm attempting:

Code: Select all

EnableGadgetDrop(#My_Editor_Gadget, #PB_Drop_Text, #PB_Drag_Copy)
EnableGadgetDrop(#My_Editor_Gadget, #PB_Drop_Files, #PB_Drag_Copy)
.
.
.
Case #My_Editor_Gadget
      sFilesList = EventDropFiles()

      If iEventDropType = #PB_Drop_Text
        SetGadgetText(#My_Editor_Gadget, EventDropText())
        
      ElseIf iEventDropType = #PB_Drop_Files
        If iFilesCount = 1
          SetGadgetText(#My_Editor_Gadget, sFilesList)
        EndIf
      EndIf


Is this approach legal in PureBasic? If not, is there any way for me to do this?
User avatar
HeX0R
Addict
Addict
Posts: 992
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Can an Editor Gadget do both #PB_Drop_Text and #PB_Drop_

Post by HeX0R »

You would find more helpful souls if you had posted a running example and not just a non-working snippet!

Code: Select all

If OpenWindow(0, 200, 200, 400, 400, "Drag & Drop", #PB_Window_SystemMenu)
	EditorGadget(0, 10, 10, 380, 380)
	EnableGadgetDrop(0, #PB_Drop_Files, #PB_Drag_Copy)
	EnableGadgetDrop(0, #PB_Drop_Text, #PB_Drag_Copy)
	
	Repeat
		Select WaitWindowEvent()  
			Case #PB_Event_CloseWindow
				Break
			Case #PB_Event_GadgetDrop
				Select EventGadget()
					Case 0
						Select EventDropType()
							Case #PB_Drop_Files
								Debug "file"
								a$ = EventDropFiles()
								For i = 0 To CountString(a$, #LF$)
									If ReadFile(0, StringField(a$, i + 1, #LF$))
										BOM = ReadStringFormat(0)
										b$ = ReadString(0, BOM | #PB_File_IgnoreEOL, Lof(0))
										AddGadgetItem(0, -1, b$)
										CloseFile(0)
									EndIf
								Next i
							Case #PB_Drop_Text
								Debug "text"
								AddGadgetItem(0, -1, EventDropText())
							Default
								Debug EventDropType()
						EndSelect
				EndSelect
		EndSelect     
		
	ForEver
EndIf
percy_b
User
User
Posts: 72
Joined: Mon Jan 12, 2015 10:25 am

Re: Can an Editor Gadget do both #PB_Drop_Text and #PB_Drop_

Post by percy_b »

Greetings HeX0R,

Your point is well taken. The least I could have done was provide a working example.

After taking a second look at my own code, I believe that I must have done something wrong. I will re-post here later after making some changes.

Thanks.
percy_b
User
User
Posts: 72
Joined: Mon Jan 12, 2015 10:25 am

Re: Can an Editor Gadget do both #PB_Drop_Text and #PB_Drop_

Post by percy_b »

I didn't want to post my entire program here, because the application itself is over 75 pages long. In any case, I should have still posted a working prototype.

Having said that, I was able to resolve my problem. In my application, I have a panel gadget with three sections. Each section (or tab) contains an editor gadget. The first of these tabs is supposed to handle both text and file drag/drop events.

For some reason, when did the following where I call a procedure "Do_Drag_and_Drop", I would get an error (see below).

Code: Select all

If EventDropType() = #PB_Drop_Files
  If iFilesCount = 1
    Do_Drag_and_Drop(EventDropType(), #Gadget_frmMain_edInput, sFilesList, EventDropText())
  EndIf 
Else
  Do_Drag_and_Drop(EventDropType(), #Gadget_frmMain_edInput, sFilesList, EventDropText())
EndIf
However, when I changed the order of the test conditions around and handle the drag and drop events locally rather than calling a procedure, things worked fine (see below).

Code: Select all

If EventDropType() = #PB_Drop_Text
  SetGadgetText(#Gadget_frmMain_edInput, EventDropText())
Else
  If iFilesCount = 1
    SetGadgetText(#Gadget_frmMain_edInput, sFilesList)
  EndIf 
EndIf
I'm sure it was something I did wrong (maybe in the procedure call itself). But, the important thing is that it works now.

Thank you for your response HeX0R. Your example made me re-think what I was doing. And, sorry for the second code snippet. :D
percy_b
User
User
Posts: 72
Joined: Mon Jan 12, 2015 10:25 am

Re: Can an Editor Gadget do both #PB_Drop_Text and #PB_Drop_

Post by percy_b »

Hi Guys,

This is a eureka moment for me, but it's probably really a rookie mistake for a seasoned PB developer.

Here is a piece of code that is in my program that handles drag and drop and has been causing problems.

Code: Select all

If iActiveTab = 1
  SetGadgetState(#Gadget_frmMain_pnlTabs, 0)  ;Move to the correct tab
  SetGadgetText(#Gadget_frmMain_edInput, EventDropText())
Else
  SetGadgetText(#Gadget_frmMain_edInput, EventDropText())
EndIf
This code causes a 'No Drag & Drop Event occurred' error and the program stops.

I finally discovered that the problem was one of my own making. When I make a call to SetGadgetState(), I triggered a new event that superseded the Drag & Drop event.

By doing the following, I was able to fix the problem.

Code: Select all

If iActiveTab = 1
  SetClipboardText(EventDropText())
  SetGadgetState(#Gadget_frmMain_pnlTabs, 0)  ;Move to the correct tab
  SetGadgetText(#Gadget_frmMain_edInput, GetClipboardText())
Else
  SetGadgetText(#Gadget_frmMain_edInput, EventDropText())
EndIf
Since I want to switch the active panel before dropping the text, I needed to store the contents of EventDropText() first and then paste it from the clipboard.

To quote someone else's famous saying "Mistakes are the best teachers".
Post Reply