Skip to content

Commit c1186b0

Browse files
dsward2dsward2
authored andcommitted
In the XMLOutlineView, when using drag-and-drop to move elements within the same document or to a different document, try to preserve the hierarchical order of the elements.
1 parent 55b5470 commit c1186b0

File tree

4 files changed

+89
-41
lines changed

4 files changed

+89
-41
lines changed

macSVG/Cocoa Extensions/NSOutlineView_Extensions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262

6363
//- (void)highlightSelectionInClipRect:(NSRect)theClipRect;
6464

65+
- (NSArray *)selectedItemsFlat;
66+
6567
@end
6668

6769

macSVG/Cocoa Extensions/NSOutlineView_Extensions.m

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ @implementation NSOutlineView(MyExtensions)
6969

7070

7171

72-
- (NSArray *)selectedItems
72+
- (NSArray *)selectedItemsFlat
7373
{
7474
// returns a flat array of selected items
7575
NSMutableArray *items = [NSMutableArray array];
@@ -91,10 +91,14 @@ - (NSArray *)selectedItems
9191
*/
9292

9393
[selectedRows enumerateIndexesUsingBlock:^(NSUInteger i, BOOL *stop) {
94+
9495
NSXMLNode * aItemNode = [self itemAtRow:i];
9596

9697
if (aItemNode.kind == NSXMLElementKind)
9798
{
99+
//NSXMLElement * parentElement = (NSXMLElement *)[aItemNode parent];
100+
//NSInteger parentRow = [self rowForItem:parentElement];
101+
98102
[items addObject:aItemNode];
99103
}
100104
}];
@@ -236,53 +240,54 @@ - (void)mouseDown:(NSEvent *)theEvent
236240
// workaround for NSXMLOutlineView problem when click-selecting on a child item in an active selection path
237241
// in XMLOutlineController, outlineView:writeItems:toPasteboard: may reselect the items
238242

239-
BOOL doDeselectAll = YES;
243+
if (theEvent.clickCount == 2)
244+
{
245+
// A double-click was detected
240246

241-
XMLOutlineController * xmlOutlineController = (XMLOutlineController *)self.delegate;
242-
MacSVGDocumentWindowController * macSVGDocumentWindowController = xmlOutlineController.macSVGDocumentWindowController;
243-
244-
xmlOutlineController.holdSelectedItems = NULL;
247+
BOOL doDeselectAll = YES;
245248

246-
if (macSVGDocumentWindowController.currentToolMode == toolModeCrosshairCursor)
247-
{
248-
NSPoint globalLocation = theEvent.locationInWindow;
249-
NSPoint localLocation = [self convertPoint:globalLocation fromView:nil];
250-
NSInteger clickedRow = [self rowAtPoint:localLocation];
251-
252-
NSXMLNode * aRowItem = [self itemAtRow:clickedRow];
249+
XMLOutlineController * xmlOutlineController = (XMLOutlineController *)self.delegate;
250+
MacSVGDocumentWindowController * macSVGDocumentWindowController = xmlOutlineController.macSVGDocumentWindowController;
253251

254-
if (aRowItem.kind == NSXMLElementKind)
252+
xmlOutlineController.holdSelectedItems = NULL;
253+
254+
if (macSVGDocumentWindowController.currentToolMode == toolModeCrosshairCursor)
255255
{
256-
NSXMLElement * aElement = (NSXMLElement *)aRowItem;
256+
NSPoint globalLocation = theEvent.locationInWindow;
257+
NSPoint localLocation = [self convertPoint:globalLocation fromView:nil];
258+
NSInteger clickedRow = [self rowAtPoint:localLocation];
259+
260+
NSXMLNode * aRowItem = [self itemAtRow:clickedRow];
257261

258-
NSString * elementName = aElement.name;
259-
if ([elementName isEqualToString:@"path"])
262+
if (aRowItem.kind == NSXMLElementKind)
260263
{
261-
doDeselectAll = NO;
264+
NSXMLElement * aElement = (NSXMLElement *)aRowItem;
265+
266+
NSString * elementName = aElement.name;
267+
if ([elementName isEqualToString:@"path"])
268+
{
269+
doDeselectAll = NO;
270+
}
262271
}
263272
}
264-
}
265273

266-
if (macSVGDocumentWindowController.currentToolMode == toolModeText)
267-
{
268-
doDeselectAll = NO;
269-
}
274+
if (macSVGDocumentWindowController.currentToolMode == toolModeText)
275+
{
276+
doDeselectAll = NO;
277+
}
270278

271-
if (macSVGDocumentWindowController.currentToolMode == toolModeImage)
272-
{
273-
doDeselectAll = NO;
274-
}
279+
if (macSVGDocumentWindowController.currentToolMode == toolModeImage)
280+
{
281+
doDeselectAll = NO;
282+
}
275283

276-
if (theEvent.clickCount == 2)
277-
{
278-
// A double-click was detected
279-
284+
280285
if (doDeselectAll == YES)
281286
{
282287
// Workaround an issue when selection changes from a group of items to a single item within the group
283288
// see outlineView:writeItems:toPasteboard: for more info
284289

285-
xmlOutlineController.holdSelectedItems = [self selectedItems]; // these items should be reselected if dragging occurs
290+
xmlOutlineController.holdSelectedItems = [self selectedItemsFlat]; // these items should be reselected if dragging occurs
286291

287292
XMLAttributesTableController * xmlAttributesTableController = xmlOutlineController.macSVGDocumentWindowController.xmlAttributesTableController;
288293
[xmlAttributesTableController removeAllTableRows]; // to reduce animation

macSVG/MacSVG-Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
<key>CFBundleSignature</key>
106106
<string>????</string>
107107
<key>CFBundleVersion</key>
108-
<string>1631</string>
108+
<string>1647</string>
109109
<key>LSApplicationCategoryType</key>
110110
<string>public.app-category.graphics-design</string>
111111
<key>LSMinimumSystemVersion</key>

macSVG/SVGDocument Classes/XMLOutlineController.m

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ - (void)reloadView
174174

175175
//[xmlOutlineView expandItem:NULL expandChildren:YES];
176176

177-
NSArray * selectedItems = [self.xmlOutlineView selectedItems];
177+
NSArray * selectedItems = [self.xmlOutlineView selectedItemsFlat];
178178

179179
for (NSXMLNode * aXMLNode in selectedItems)
180180
{
@@ -186,7 +186,7 @@ - (void)reloadView
186186

187187
- (NSArray *)selectedNodes
188188
{
189-
return [self.xmlOutlineView selectedItems];
189+
return [self.xmlOutlineView selectedItemsFlat];
190190
}
191191

192192
//==================================================================================
@@ -316,7 +316,7 @@ - (NSArray *)selectedElementIDs
316316
{
317317
NSMutableArray * resultArray = [NSMutableArray array];
318318

319-
NSArray * selectedItemsArray = [self.xmlOutlineView selectedItems];
319+
NSArray * selectedItemsArray = [self.xmlOutlineView selectedItemsFlat];
320320

321321
if (selectedItemsArray != NULL)
322322
{
@@ -1055,7 +1055,7 @@ - (void) expandElementInOutline:(NSXMLElement *)aElement
10551055

10561056
- (NSArray *)selectedItems
10571057
{
1058-
NSArray * selectedItemsArray = [self.xmlOutlineView selectedItems];
1058+
NSArray * selectedItemsArray = [self.xmlOutlineView selectedItemsFlat];
10591059

10601060
return selectedItemsArray;
10611061
}
@@ -1328,7 +1328,7 @@ - (void) moveSelectedElementIDs:(NSArray *)selectedElementIDsArray toGroupElemen
13281328

13291329
- (IBAction)editXMLTextAction:(id)sender
13301330
{
1331-
NSArray * selectedItems = [self.xmlOutlineView selectedItems];
1331+
NSArray * selectedItems = [self.xmlOutlineView selectedItemsFlat];
13321332

13331333
NSInteger selectedItemsCount = selectedItems.count;
13341334

@@ -1648,7 +1648,7 @@ - (void)nudgeSelectedItemsRight
16481648

16491649
- (void)nudgeSelectedItemsWithDeltaX:(CGFloat)deltaX deltaY:(CGFloat)deltaY
16501650
{
1651-
//NSArray * selectedItems = [self.xmlOutlineView selectedItems];
1651+
//NSArray * selectedItems = [self.xmlOutlineView selectedItemsFlat];
16521652

16531653
NSArray * selectedElementsDictionariesArray =
16541654
[self.macSVGDocumentWindowController.svgXMLDOMSelectionManager.selectedElementsManager selectedElementsArray];
@@ -2314,7 +2314,7 @@ - (void)updateRowIcons
23142314
{
23152315
NSInteger selectedRow = self.xmlOutlineView.selectedRow;
23162316

2317-
NSArray * selectedItems = [self.xmlOutlineView selectedItems];
2317+
NSArray * selectedItems = [self.xmlOutlineView selectedItemsFlat];
23182318
if (selectedItems.count > 0)
23192319
{
23202320
NSXMLNode * firstSelectedItem = selectedItems[0];
@@ -2518,7 +2518,46 @@ - (void)selectItemsForCurrentElement:(NSXMLElement *)currentElement
25182518

25192519
- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
25202520
{
2521-
self.draggedNodes = items;
2521+
//self.draggedNodes = items;
2522+
2523+
NSMutableArray * newDraggedNodes = [NSMutableArray array];
2524+
for (NSXMLNode * aNode in items)
2525+
{
2526+
if (aNode.kind == NSXMLElementKind)
2527+
{
2528+
NSXMLNode * parentNode = aNode.parent;
2529+
2530+
BOOL parentFound = NO;
2531+
BOOL continueSearch = YES;
2532+
2533+
while (continueSearch == YES)
2534+
{
2535+
if (parentNode == NULL)
2536+
{
2537+
continueSearch = NO;
2538+
}
2539+
else
2540+
{
2541+
NSInteger parentIndex = [newDraggedNodes indexOfObject:parentNode];
2542+
2543+
if (parentIndex != NSNotFound)
2544+
{
2545+
parentFound = YES;
2546+
continueSearch = NO;
2547+
}
2548+
}
2549+
2550+
parentNode = parentNode.parent;
2551+
}
2552+
2553+
if (parentFound == NO)
2554+
{
2555+
[newDraggedNodes addObject:aNode];
2556+
}
2557+
}
2558+
}
2559+
2560+
self.draggedNodes = newDraggedNodes;
25222561

25232562
/*
25242563
if (self.holdSelectedItems != NULL)
@@ -2546,12 +2585,14 @@ - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toP
25462585
[pboard setData:[NSData data] forType:XML_OUTLINE_PBOARD_TYPE];
25472586

25482587
NSMutableString * nodesString = [NSMutableString string];
2588+
25492589
//for (NSXMLNode * aNode in items)
25502590
for (NSXMLNode * aNode in self.draggedNodes)
25512591
{
25522592
NSString * nodeXml = [aNode XMLStringWithOptions:NSXMLNodePreserveCDATA];
25532593
[nodesString appendString:nodeXml];
25542594
}
2595+
25552596
[pboard setString:nodesString forType:NSStringPboardType];
25562597

25572598
// Put the promised type we handle on the pasteboard.

0 commit comments

Comments
 (0)