Set the Focus ring around a Canvas at drag 'n' Drop time

Hi all,

teh title says all:

How can I set the Focus ring around a Canvas at drag ‘n’ Drop time ?

Do you mean, when the mouse enters the Canvas?

Yes.

You never used to drag and drop an image from the hard disk (Finder, Explorer, whatever) into a window or a Canvas ?

In carbon, nothing had to be done. This is no more the case on Cocoa.

BTW: and clears teh focus when the mouse goes out of the Canvas.
Yes, Self.SetFocus works fine, but where must place that code to make it works ?
And to be able to set the focus to the window (lear it from the Canvas), it have to be set on the Canvas first.

You’re looking for DragEnter and DragExit. And just in case, you might need DropObject.

Here. Check this: https://drive.google.com/file/d/0B6w6JZen_vW2dzhBZVZXZUsyX2M/edit?usp=sharing
Adjust for your needs. :wink:

I think Emile’s problem is that these events don’t fire when you’re dragging an item from outside of the app (such as a picture from Finder), and your app isn’t the frontmost.

Apologies if I’ve misunderstood.

for dragging an item from outside

Canvas - DragOver Event

  declare function NSClassFromString lib "Cocoa" ( inName as CFStringRef ) as Ptr
  declare function sharedApplication lib "Cocoa" selector "sharedApplication" ( classRef as Ptr ) as Ptr
  Dim myApplication as Ptr =  sharedApplication( NSClassFromString( "NSApplication" ) )
  declare sub activateIgnoringOtherApps lib "Cocoa" selector "activateIgnoringOtherApps:" ( appRef as Ptr, flag as boolean )
  activateIgnoringOtherApps( myApplication, True )
  me.AcceptFocus = True
  me.UseFocusRing = True
  me.SetFocus()

Should work without declares. When I say setFocus() the focus and focus ring should work anyway. The framework should take care of making it work seamlessly reliable on all the platforms.

It’s a Bug, isn’t? Works once, then starts to fail: https://drive.google.com/file/d/0B6w6JZen_vW2S2Iza2VwMWFfVjA/edit?usp=sharing

It works fine with me.SetFocus in the DragEvent. But indeed, what is strange is that although a beep, for instance, takes effect immediately, the focus ring appears only after the file has been dropped. One would like the focus ring to appear immediately.

I tried the declare above, but the same occurs.

To get the focus ring immediately when I drag a file over, here is what I do :

Function DragOver(x As Integer, y As Integer, obj As DragItem, action As Integer) As Boolean dim p as new picture(me.width,me.height,32) p.Transparent = 1 p.graphics.ForeColor=&c4EE3FF00 p.graphics.PenHeight=2 p.graphics.PenWidth=2 p.graphics.DrawRect(0,0,me.width,me.height) me.backdrop = p End Function

and this removes the ring when the object is out of the canvas.

Sub DragExit(obj As DragItem, action As Integer) me.backdrop = nil End Sub

Of course to avoid interferences, I set the canvas UseFocusRing to false in the inspector.

Just noticed I used the “old” syntax to get a transparent picture. Here with alpha channel

Function DragOver(x As Integer, y As Integer, obj As DragItem, action As Integer) As Boolean dim p as new picture(me.width,me.height p.graphics.ForeColor=&c4EE3FF00 p.graphics.PenHeight=2 p.graphics.PenWidth=2 p.graphics.DrawRect(0,0,me.width,me.height) me.backdrop = p End Function

Thanks Michel, all,

I used:

Self.SetFocus

to send the focus to the window [this clears the focus from the control(s)].

All my attempts to set the Focus (à la Carbon / Windows) failed; this was the reason I asked here.

[quote=114069:@Emile Schwarz]I used:

Self.SetFocus

to send the focus to the window [this clears the focus from the control(s)].[/quote]

I used:

control.AcceptFocus = False control.UseFocusRing = False

To disable the focus ring because in some systems moving the focus to an unfocused window could mean “bring the windows to front, start to receive events on the current focused control” and that is not an “unfocus control”. :expressionless:

[quote=114069:@Emile Schwarz]Thanks Michel, all,

I used:

Self.SetFocus

to send the focus to the window [this clears the focus from the control(s)].

All my attempts to set the Focus (à la Carbon / Windows) failed; this was the reason I asked here.[/quote]

Please note that the method I posted does not require that the canvas get the focus. Actually, it does not matter. What I do is draw a blue rectangle in DragEnter and remove it in DragExit. Focus has absolutely nothing to do with it.

With all the respect Michel, we are (at least myself) not talking about workarounds, but making things to work as they should.

With all due respect, Rick, I did try myself making it work as it should before realizing it very well maybe a Cocoa ‘feature’. I even used Axel declare and never was able to get the focus ring to appear upon DragEnter before the file was actually dropped. Last minute : I just tried to set the focus on the canvas in the window DragEnter event : the focus does not appear until the file is dropped. It seems the SetFocus cannot take place while a drag is in motion.

Besides, I was not responding to you but to Emile, who replied specifically to my post in :

[quote=114069:@Emile Schwarz]Michel Bujardet this removes the ring when the object is out of the canvas.
I used:

Self.SetFocus
to send the focus to the window [this clears the focus from the control(s)].[/quote]

So excuse me, but I felt necessary to remind him that my workaround he was referring to did not use the focus.

Good luck in your endeavor to fix that SetFocus bug. Really. Workaround are usually necessary when all attempts have failed.

Very well probably a framework bug. :wink:

Have you seen my demo? I do prove there that it works and that there is a bug triggered by an specific condition.

Thank you. :wink:

Rick, your demo works fine, but only for objects. So indeed when you drag a rectangle the focus appears instantly when you set the focus to TheCanvas in DragEnter.

The framework bug is of a different nature : it manifests when you try to drag and drop a file. When you drag a file into the canvas, DragEnter fires fine, but the focus does not change until the file has been dropped.

I verified by adding the TheCanvas.SetFocus to the window DragEnter but same thing : until the file is dropped, no ring.

I even wanted to verify that indeed, until the file is dropped, the focus cannot be set. So I had a timer do the SetFocus while I was holding the file. Nope, the file had to be released for the focus ring to appear. And setting the focus to another control while dragging a file is frozen as well.

Which leads me to conclude it is a Cocoa bug which prevents change in focus while a file is dragged.

This is not the only time I see such thing. I tried once to get the position of a window while moving it (add a .Move event). Until the move is complete the window does not report anything but the starting position.

It works fine for the first file but AFTER the file drop the Xojo focus system becomes unstable, it does not work at all for ANY case after that, object or file. It could be Cocoa or Xojo Framework. Let’s Analise: Cocoa is kind of mature to present such kind of lame bug nowadays or they would have a huge pile of people blaming them. In the other hand if you go to the Feedback system and do a research about dragging bugs without fixing reported, you start to think that this one is more suitable than Apple’s fault.
Verdict: Needs research and fix. Xojo or Apple. By the way, every feature needs to work in Windows/Linux too.

No, it does not work. I simply modified your project with Me.AcceptFileDrop in TheCanvas Open event, and the focus ring appears only after the file has dropped, first time around. Any version of Xojo. Quit Xojo and restarted, same thing.

I just tried the same manipulation with a TextField : only after the file has been dropped do the SetFocus happen.

In case the problem happened only when I try to set the focus in DragEnter, I used a timer to change the focus to the TextField. If by the time the timer fires the user is in the process of dragging a file, the change of focus happens only after the file has been dropped.

At this stage, I am 100% sure that while a file is dragged, no focus can be changed. So, is it a Cocoa bug or a Xojo bug ? I have the strong feeling it is a Cocoa bug because the DragEvent does fire fine and the Xojo code in it is executed, let alone when as a workaround I draw a rectangle into it to mimic a focus ring.

You cannot verify that for Windows and Linux, where the focus ring simply does not exist.

If you think Apple is as responsive as Xojo and fears the opinion of developers, pay a visit to Apple Developers forum and see how many bug reports gets fixed, and how many remain ignored. To be blunt, the focus ring will not stop the earth rotation, and Apple may have numerous other issues to care about before they condescend fixing this.

You don’t know what you are talking about, so please, stop.

[quote=114271:@Michel Bujardet] Rick A By the way, every feature needs to work in Windows/Linux too.

You cannot verify that for Windows and Linux, where the focus ring simply does not exist.[/quote]

Again, you don’t know what you’re talking about. Both have focus control and the visual feedback.

Michel, please, find someone else to bother.