SKOutlineView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // SKOutlineView.m
  3. // Skim
  4. //
  5. // Created by Christiaan Hofman on 8/22/07.
  6. /*
  7. This software is Copyright (c) 2007-2018
  8. Christiaan Hofman. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. - Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. - Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in
  16. the documentation and/or other materials provided with the
  17. distribution.
  18. - Neither the name of Christiaan Hofman nor the names of any
  19. contributors may be used to endorse or promote products derived
  20. from this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #import "SKOutlineView.h"
  34. #import <Quartz/Quartz.h>
  35. //#import "SKTypeSelectHelper.h"
  36. //#import "NSEvent_SKExtensions.h"
  37. //#import "NSFont_SKExtensions.h"
  38. @implementation SKOutlineView
  39. - (void)dealloc {
  40. // [typeSelectHelper setDelegate:nil];
  41. // SKDESTROY(typeSelectHelper);
  42. // [super dealloc];
  43. }
  44. - (NSArray *)itemsAtRowIndexes:(NSIndexSet *)indexes {
  45. NSMutableArray *items = [NSMutableArray array];
  46. [indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
  47. [items addObject:[self itemAtRow:idx]];
  48. }];
  49. return items;
  50. }
  51. - (NSArray *)selectedItems {
  52. return [self itemsAtRowIndexes:[self selectedRowIndexes]];
  53. }
  54. //- (void)setTypeSelectHelper:(SKTypeSelectHelper *)newTypeSelectHelper {
  55. // if (typeSelectHelper != newTypeSelectHelper) {
  56. // if ([typeSelectHelper delegate] == self)
  57. // [typeSelectHelper setDelegate:nil];
  58. // [typeSelectHelper release];
  59. // typeSelectHelper = [newTypeSelectHelper retain];
  60. // [typeSelectHelper setDelegate:self];
  61. // }
  62. //}
  63. - (void)expandItem:(id)item expandChildren:(BOOL)collapseChildren {
  64. [super expandItem:item expandChildren:collapseChildren];
  65. // [typeSelectHelper rebuildTypeSelectSearchCache];
  66. }
  67. - (void)collapseItem:(id)item collapseChildren:(BOOL)collapseChildren {
  68. [super collapseItem:item collapseChildren:collapseChildren];
  69. // [typeSelectHelper rebuildTypeSelectSearchCache];
  70. }
  71. - (void)reloadData{
  72. [super reloadData];
  73. // [typeSelectHelper rebuildTypeSelectSearchCache];
  74. }
  75. - (void)keyDown:(NSEvent *)theEvent {
  76. NSString *characters = [theEvent charactersIgnoringModifiers];
  77. unichar eventChar = [characters length] > 0 ? [characters characterAtIndex:0] : 0;
  78. NSUInteger modifierFlags = [theEvent modifierFlags] & NSEventModifierFlagDeviceIndependentFlagsMask;
  79. if ((eventChar == NSNewlineCharacter || eventChar == NSEnterCharacter || eventChar == NSCarriageReturnCharacter) && modifierFlags == 0) {
  80. if ([self doubleAction] == NULL || [self sendAction:[self doubleAction] to:[self target]] == NO)
  81. NSBeep();
  82. } else if ((eventChar == NSTabCharacter) && modifierFlags == 0) {
  83. if (supportsQuickLook == NO)
  84. [[self enclosingScrollView] pageDown:nil];
  85. else if ([QLPreviewPanel sharedPreviewPanelExists] && [[QLPreviewPanel sharedPreviewPanel] isVisible])
  86. [[QLPreviewPanel sharedPreviewPanel] orderOut:nil];
  87. else
  88. [[QLPreviewPanel sharedPreviewPanel] makeKeyAndOrderFront:nil];
  89. } else if ((eventChar == NSTabCharacter) && modifierFlags == NSShiftKeyMask) {
  90. if (supportsQuickLook == NO)
  91. [[self enclosingScrollView] pageUp:nil];
  92. } else if (eventChar == NSHomeFunctionKey && (modifierFlags & ~NSFunctionKeyMask) == 0) {
  93. [self scrollToBeginningOfDocument:nil];
  94. } else if (eventChar == NSEndFunctionKey && (modifierFlags & ~NSFunctionKeyMask) == 0) {
  95. [self scrollToEndOfDocument:nil];
  96. } else if ((eventChar == NSDeleteCharacter || eventChar == NSDeleteFunctionKey) && modifierFlags == 0 && [self canDelete]) {
  97. [self delete:self];
  98. }
  99. // else if ([typeSelectHelper handleEvent:theEvent] == NO) {
  100. // [super keyDown:theEvent];
  101. // }
  102. }
  103. - (void)scrollToBeginningOfDocument:(id)sender {
  104. if ([self numberOfRows])
  105. [self scrollRowToVisible:0];
  106. }
  107. - (void)scrollToEndOfDocument:(id)sender {
  108. if ([self numberOfRows])
  109. [self scrollRowToVisible:[self numberOfRows] - 1];
  110. }
  111. - (BOOL)canDelete {
  112. NSArray *items = [self selectedItems];
  113. if ([items count] && [[self delegate] respondsToSelector:@selector(outlineView:deleteItems:)]) {
  114. if ([[self delegate] respondsToSelector:@selector(outlineView:canDeleteItems:)])
  115. return [[self delegate] outlineView:self canDeleteItems:items];
  116. else
  117. return YES;
  118. }
  119. return NO;
  120. }
  121. - (void)delete:(id)sender {
  122. if ([self canDelete])
  123. [[self delegate] outlineView:self deleteItems:[self selectedItems]];
  124. else
  125. NSBeep();
  126. }
  127. - (BOOL)canCopy {
  128. NSArray *items = [self selectedItems];
  129. if ([items count] && [[self delegate] respondsToSelector:@selector(outlineView:copyItems:)]) {
  130. if ([[self delegate] respondsToSelector:@selector(outlineView:canCopyItems:)])
  131. return [[self delegate] outlineView:self canCopyItems:items];
  132. else
  133. return YES;
  134. }
  135. return NO;
  136. }
  137. - (void)copy:(id)sender {
  138. if ([self canCopy])
  139. [[self delegate] outlineView:self copyItems:[self selectedItems]];
  140. else
  141. NSBeep();
  142. }
  143. - (BOOL)canPaste {
  144. if ([[self delegate] respondsToSelector:@selector(outlineView:pasteFromPasteboard:)]) {
  145. if ([[self delegate] respondsToSelector:@selector(outlineView:canPasteFromPasteboard:)])
  146. return [[self delegate] outlineView:self canPasteFromPasteboard:[NSPasteboard generalPasteboard]];
  147. else
  148. return YES;
  149. }
  150. return NO;
  151. }
  152. - (void)paste:(id)sender {
  153. if ([self canPaste])
  154. [[self delegate] outlineView:self pasteFromPasteboard:[NSPasteboard generalPasteboard]];
  155. else
  156. NSBeep();
  157. }
  158. - (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
  159. if ([menuItem action] == @selector(delete:))
  160. return [self canDelete];
  161. else if ([menuItem action] == @selector(copy:))
  162. return [self canCopy];
  163. else if ([menuItem action] == @selector(paste:))
  164. return [self canPaste];
  165. else if ([menuItem action] == @selector(selectAll:))
  166. return [self allowsMultipleSelection];
  167. else if ([menuItem action] == @selector(deselectAll:))
  168. return [self allowsEmptySelection];
  169. else if ([[SKOutlineView superclass] instancesRespondToSelector:@selector(validateMenuItem:)])
  170. return [super validateMenuItem:menuItem];
  171. return YES;
  172. }
  173. - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation {
  174. if ([[SKOutlineView superclass] instancesRespondToSelector:_cmd])
  175. [super draggedImage:anImage endedAt:aPoint operation:operation];
  176. if ([[self dataSource] respondsToSelector:@selector(outlineView:dragEndedWithOperation:)])
  177. [[self dataSource] outlineView:self dragEndedWithOperation:operation];
  178. }
  179. - (NSFont *)font {
  180. for (NSTableColumn *tc in [self tableColumns]) {
  181. NSCell *cell = [tc dataCell];
  182. if ([cell type] == NSTextCellType)
  183. return [cell font];
  184. }
  185. return nil;
  186. }
  187. - (void)setFont:(NSFont *)font {
  188. for (NSTableColumn *tc in [self tableColumns]) {
  189. NSCell *cell = [tc dataCell];
  190. if ([cell type] == NSTextCellType)
  191. [cell setFont:font];
  192. }
  193. CGFloat rowHeight = [self defaultViewLineHeight:font];
  194. if ([self selectionHighlightStyle] == NSTableViewSelectionHighlightStyleSourceList)
  195. rowHeight += 2.0;
  196. [self setRowHeight:rowHeight];
  197. [self noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [self numberOfRows])]];
  198. }
  199. - (CGFloat)defaultViewLineHeight:(NSFont *)font {
  200. static NSLayoutManager *layoutManager = nil;
  201. if (layoutManager == nil) {
  202. layoutManager = [[NSLayoutManager alloc] init];
  203. [layoutManager setTypesetterBehavior:NSTypesetterBehavior_10_2_WithCompatibility];
  204. }
  205. return [layoutManager defaultLineHeightForFont:font];
  206. }
  207. #pragma mark SKTypeSelectHelper datasource protocol
  208. //- (NSArray *)typeSelectHelperSelectionStrings:(SKTypeSelectHelper *)aTypeSelectHelper {
  209. // if ([[self delegate] respondsToSelector:@selector(outlineView:typeSelectHelperSelectionStrings:)])
  210. // return [[self delegate] outlineView:self typeSelectHelperSelectionStrings:aTypeSelectHelper];
  211. // return nil;
  212. //}
  213. //- (NSUInteger)typeSelectHelperCurrentlySelectedIndex:(SKTypeSelectHelper *)aTypeSelectHelper {
  214. // return [[self selectedRowIndexes] lastIndex];
  215. //}
  216. //- (void)typeSelectHelper:(SKTypeSelectHelper *)aTypeSelectHelper selectItemAtIndex:(NSUInteger)itemIndex {
  217. // [self selectRowIndexes:[NSIndexSet indexSetWithIndex:itemIndex] byExtendingSelection:NO];
  218. // [self scrollRowToVisible:itemIndex];
  219. //}
  220. //
  221. //- (void)typeSelectHelper:(SKTypeSelectHelper *)aTypeSelectHelper didFailToFindMatchForSearchString:(NSString *)searchString {
  222. // if ([[self delegate] respondsToSelector:@selector(outlineView:typeSelectHelper:didFailToFindMatchForSearchString:)])
  223. // [[self delegate] outlineView:self typeSelectHelper:aTypeSelectHelper didFailToFindMatchForSearchString:searchString];
  224. //}
  225. //
  226. //- (void)typeSelectHelper:(SKTypeSelectHelper *)aTypeSelectHelper updateSearchString:(NSString *)searchString {
  227. // if ([[self delegate] respondsToSelector:@selector(outlineView:typeSelectHelper:updateSearchString:)])
  228. // [[self delegate] outlineView:self typeSelectHelper:aTypeSelectHelper updateSearchString:searchString];
  229. //}
  230. - (id <SKOutlineViewDelegate>)delegate { return (id <SKOutlineViewDelegate>)[super delegate]; }
  231. - (void)setDelegate:(id <SKOutlineViewDelegate>)newDelegate { [super setDelegate:newDelegate]; }
  232. - (id <SKOutlineViewDataSource>)dataSource { return (id <SKOutlineViewDataSource>)[super dataSource]; }
  233. - (void)setDataSource:(id <SKOutlineViewDataSource>)newDataSource { [super setDataSource:newDataSource]; }
  234. @end