Skip to content

Commit 19f0511

Browse files
徐扬斌coremail-cyt
authored andcommitted
wxOSX: Add support for wxDropSource icons.
The wxDropSource has API for icons of different dnd states indication. But it is not supported at all on cocoa. No icons at all on OSX. Now add the proper support for this API.
1 parent 4647c3c commit 19f0511

File tree

4 files changed

+97
-20
lines changed

4 files changed

+97
-20
lines changed

include/wx/dnd.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ class WXDLLIMPEXP_CORE wxDropSourceBase
9595
// give the default feedback
9696
virtual bool GiveFeedback(wxDragResult WXUNUSED(effect)) { return false; }
9797

98-
protected:
9998
const wxCursor& GetCursor(wxDragResult res) const
10099
{
101100
if ( res == wxDragCopy )
@@ -105,7 +104,8 @@ class WXDLLIMPEXP_CORE wxDropSourceBase
105104
else
106105
return m_cursorStop;
107106
}
108-
107+
108+
protected:
109109
// the data we're dragging
110110
wxDataObject *m_data;
111111

include/wx/osx/cocoa/private/dnd.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "wx/wxprec.h"
2+
3+
#if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
4+
5+
#ifndef WX_PRECOMP
6+
#include "wx/object.h"
7+
#endif
8+
9+
#include "wx/dnd.h"
10+
11+
#include "wx/osx/private.h"
12+
#include "wx/osx/private/datatransfer.h"
13+
14+
@interface DropSourceDelegate : NSObject<NSDraggingSource>
15+
{
16+
BOOL dragFinished;
17+
int resultCode;
18+
wxDropSource* impl;
19+
20+
// Flags for drag and drop operations (wxDrag_* ).
21+
int m_dragFlags;
22+
NSImage* m_copy_cursor;
23+
NSImage* m_move_cursor;
24+
NSImage* m_none_cursor;
25+
}
26+
27+
- (void)setImplementation:(nonnull wxDropSource *)dropSource flags:(int)flags;
28+
- (BOOL)finished;
29+
- (NSDragOperation)code;
30+
- (NSDragOperation)draggingSession:(nonnull NSDraggingSession *)session sourceOperationMaskForDraggingContext:(NSDraggingContext)context;
31+
- (void)draggedImage:(nonnull NSImage *)anImage movedTo:(NSPoint)aPoint;
32+
- (void)draggedImage:(nonnull NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation;
33+
- (nullable NSImage*)cursorForStatus:(wxDragResult)status;
34+
@end
35+
36+
#endif

src/osx/cocoa/dnd.mm

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#endif
1818

1919
#include "wx/dnd.h"
20+
#include "wx/osx/cocoa/private/dnd.h"
2021
#include "wx/clipbrd.h"
2122
#include "wx/filename.h"
2223

@@ -268,24 +269,6 @@ wxDragResult NSDragOperationToWxDragResult(NSDragOperation code)
268269
return wxDragNone;
269270
}
270271

271-
@interface DropSourceDelegate : NSObject<NSDraggingSource>
272-
{
273-
BOOL dragFinished;
274-
int resultCode;
275-
wxDropSource* impl;
276-
277-
// Flags for drag and drop operations (wxDrag_* ).
278-
int m_dragFlags;
279-
}
280-
281-
- (void)setImplementation:(wxDropSource *)dropSource flags:(int)flags;
282-
- (BOOL)finished;
283-
- (NSDragOperation)code;
284-
- (NSDragOperation)draggingSession:(nonnull NSDraggingSession *)session sourceOperationMaskForDraggingContext:(NSDraggingContext)context;
285-
- (void)draggedImage:(NSImage *)anImage movedTo:(NSPoint)aPoint;
286-
- (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation;
287-
@end
288-
289272
@implementation DropSourceDelegate
290273

291274
- (id)init
@@ -296,6 +279,9 @@ - (id)init
296279
resultCode = NSDragOperationNone;
297280
impl = 0;
298281
m_dragFlags = wxDrag_CopyOnly;
282+
m_copy_cursor = nil;
283+
m_move_cursor = nil;
284+
m_none_cursor = nil;
299285
}
300286
return self;
301287
}
@@ -397,6 +383,42 @@ - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDra
397383
dragFinished = YES;
398384
}
399385

386+
- (NSImage*)cursorForStatus:(wxDragResult)status
387+
{
388+
NSImage* cursor_img = nil;
389+
switch(status){
390+
case wxDragCopy:
391+
cursor_img = m_copy_cursor;
392+
break;
393+
case wxDragMove:
394+
cursor_img = m_move_cursor;
395+
break;
396+
default:
397+
cursor_img = m_none_cursor;
398+
break;
399+
}
400+
if (cursor_img != nil)
401+
return cursor_img;
402+
403+
wxCursor indicate_cursor = impl->GetCursor(status);
404+
405+
if (indicate_cursor.IsOk()) {
406+
NSCursor *cursor = (NSCursor*)indicate_cursor.GetHCURSOR();
407+
cursor_img = [[cursor image] retain];
408+
switch(status){
409+
case wxDragCopy:
410+
m_copy_cursor = cursor_img;
411+
break;
412+
case wxDragMove:
413+
m_move_cursor = cursor_img;
414+
break;
415+
default:
416+
m_none_cursor = cursor_img;
417+
break;
418+
}
419+
}
420+
return cursor_img;
421+
}
400422
@end
401423

402424
wxDropTarget::wxDropTarget( wxDataObject *data )

src/osx/cocoa/window.mm

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#if wxUSE_DRAG_AND_DROP
3838
#include "wx/dnd.h"
3939
#include "wx/clipbrd.h"
40+
#ifdef __WXOSX_MAC__
41+
#include "wx/osx/cocoa/private/dnd.h"
42+
#endif
4043
#endif
4144

4245
#if wxUSE_TOOLTIPS
@@ -1374,6 +1377,22 @@ When dragging the bottom part of the DND sample ("Drag text from here!")
13741377
default :
13751378
break;
13761379
}
1380+
if (!entered){
1381+
NSView* the_view = viewImpl->GetWXWidget();
1382+
[sender enumerateDraggingItemsWithOptions:NSDraggingItemEnumerationConcurrent forView:the_view
1383+
classes:[NSArray arrayWithObject:[NSPasteboardItem class]] searchOptions:nil
1384+
usingBlock:^(NSDraggingItem *draggingItem, NSInteger idx, BOOL *stop) {
1385+
wxUnusedVar(idx);
1386+
NSRect theFrame = draggingItem.draggingFrame;
1387+
DropSourceDelegate* drop_src_delegate = sender.draggingSource;
1388+
NSImage* newDragImage = [drop_src_delegate cursorForStatus:result];
1389+
if (newDragImage != nil){
1390+
NSRect newFrame = NSMakeRect(theFrame.origin.x, theFrame.origin.y, newDragImage.size.width, newDragImage.size.height);
1391+
[draggingItem setDraggingFrame:newFrame contents:newDragImage];
1392+
}
1393+
*stop = NO;
1394+
}];
1395+
}
13771396
return nsresult;
13781397
}
13791398

0 commit comments

Comments
 (0)