My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
Changes to /AppKit/NSComboBox.m
e5277a40a7e8 vs. 2470bd80abf4 Compare: vs.  Format:
Revision 2470bd80abf4
Go to: 
Project members, sign in to write a code review
/AppKit/NSComboBox.m   e5277a40a7e8 /AppKit/NSComboBox.m   2470bd80abf4
1 /* Copyright (c) 2006 Christopher J. W. Lloyd 1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
2 2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4 4
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 6
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ 7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8 8
9 // Original - Christopher Lloyd <cjwl@objc.net> 9 // Original - Christopher Lloyd <cjwl@objc.net>
10 #import <AppKit/NSComboBox.h> 10 #import <AppKit/NSComboBox.h>
11 #import <AppKit/NSComboBoxCell.h> 11 #import <AppKit/NSComboBoxCell.h>
12 #import <AppKit/NSApplication.h> 12 #import <AppKit/NSApplication.h>
13 #import <AppKit/NSTextView.h> 13 #import <AppKit/NSTextView.h>
14 14
15 @implementation NSComboBox 15 @implementation NSComboBox
16 16
17 -(void)addItemWithObjectValue:(id)object { 17 -(void)addItemWithObjectValue:(id)object {
18 [[self cell] addItemWithObjectValue:object]; 18 [[self cell] addItemWithObjectValue:object];
19 } 19 }
20 20
21 -(void)addItemsWithObjectValues:(NSArray *)objects { 21 -(void)addItemsWithObjectValues:(NSArray *)objects {
22 [[self cell] addItemsWithObjectValues:objects]; 22 [[self cell] addItemsWithObjectValues:objects];
23 } 23 }
24 24
25 -(void)removeAllItems { 25 -(void)removeAllItems {
26 [[self cell] removeAllItems]; 26 [[self cell] removeAllItems];
27 } 27 }
28 28
29 -(int)indexOfItemWithObjectValue:(id)object { 29 -(int)indexOfItemWithObjectValue:(id)object {
30 return [_cell indexOfItemWithObjectValue:object]; 30 return [_cell indexOfItemWithObjectValue:object];
31 } 31 }
32 32
33 -(void)scrollItemAtIndexToVisible:(int)index { 33 -(void)scrollItemAtIndexToVisible:(int)index {
34 [[self cell] scrollItemAtIndexToVisible:index]; 34 [[self cell] scrollItemAtIndexToVisible:index];
35 } 35 }
36 36
37 -(void)selectItemAtIndex:(int)index { 37 -(void)selectItemAtIndex:(int)index {
38 [[self cell] selectItemAtIndex:index]; 38 [[self cell] selectItemAtIndex:index];
39 } 39 }
40 40
41 -(BOOL)completes { 41 -(BOOL)completes {
42 return [[self cell] completes]; 42 return [[self cell] completes];
43 } 43 }
44 44
45 -(void)setCompletes:(BOOL)completes { 45 -(void)setCompletes:(BOOL)completes {
46 [[self cell] setCompletes:completes]; 46 [[self cell] setCompletes:completes];
47 } 47 }
48 48
49 -(void)mouseDown:(NSEvent *)event { 49 -(void)mouseDown:(NSEvent *)event {
50 if(![[self cell] trackMouse:event inRect:[self bounds] ofView:self untilMouseUp:YES]) 50 if(![[self cell] trackMouse:event inRect:[self bounds] ofView:self untilMouseUp:YES])
51 [super mouseDown:event]; 51 [super mouseDown:event];
52 } 52 }
53 53
54 // hrm.. since the field editor has focus, we can use this delegate method to effect keyboard 54 // hrm.. since the field editor has focus, we can use this delegate method to effect keyboard
55 // navigation. 55 // navigation.
56 // ...i also thought it might be fun to preserve the half-typed text in objectValue index 0... 56 // ...i also thought it might be fun to preserve the half-typed text in objectValue index 0...
57 - (BOOL)textView:(NSTextView *)textView doCommandBySelector:(SEL)selector { 57 - (BOOL)textView:(NSTextView *)textView doCommandBySelector:(SEL)selector {
58 int index = [[self cell] indexOfItemWithObjectValue:[self objectValue]]; 58 int index = [[self cell] indexOfItemWithObjectValue:[self objectValue]];
59 id objectCache = nil; 59 id objectCache = nil;
60 60
61 if ([textView rangeForUserCompletion].location != NSNotFound) 61 if ([textView rangeForUserCompletion].location != NSNotFound)
62 return NO; 62 return NO;
63 63
64 if (index == NSNotFound) { 64 if (index == NSNotFound) {
65 objectCache = [[self objectValue] retain]; 65 objectCache = [[self objectValue] retain];
66 index = -1; 66 index = -1;
67 } 67 }
68 68
69 if (selector == @selector(moveUp:)) { 69 if (selector == @selector(moveUp:)) {
70 [[self cell] selectItemAtIndex:index-1]; 70 [[self cell] selectItemAtIndex:index-1];
71 [_currentEditor setString:[[self cell] objectValue]]; 71 [_currentEditor setString:[[self cell] objectValue]];
72 if (objectCache != nil) 72 if (objectCache != nil)
73 [[self cell] insertItemWithObjectValue:[objectCache autorelease] atIndex:0]; 73 [[self cell] insertItemWithObjectValue:[objectCache autorelease] atIndex:0];
74 return YES; 74 return YES;
75 } 75 }
76 else if (selector == @selector(moveDown:)) { 76 else if (selector == @selector(moveDown:)) {
77 [[self cell] selectItemAtIndex:index+1]; 77 [[self cell] selectItemAtIndex:index+1];
78 [_currentEditor setString:[[self cell] objectValue]]; 78 [_currentEditor setString:[[self cell] objectValue]];
79 if (objectCache != nil) 79 if (objectCache != nil)
80 [[self cell] insertItemWithObjectValue:[objectCache autorelease] atIndex:0]; 80 [[self cell] insertItemWithObjectValue:[objectCache autorelease] atIndex:0];
81 return YES; 81 return YES;
82 } 82 }
83 83
84 return NO; 84 return NO;
85 } 85 }
86 86
87 - (NSArray *)textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)range indexOfSelectedItem:(int *)index { 87 - (NSArray *)textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)range indexOfSelectedItem:(int *)index {
88 NSString *string = [[self cell] completedString:[[textView string] substringWithRange:range]]; 88 NSString *string = [[self cell] completedString:[[textView string] substringWithRange:range]];
89 89
90 // NSLog(@"NSComboBox delegate OK: %@", string); 90 // NSLog(@"NSComboBox delegate OK: %@", string);
91 91
92 if (string != nil) { 92 if (string != nil) {
93 *index = 0; 93 *index = 0;
94 return [NSArray arrayWithObject:string]; 94 return [NSArray arrayWithObject:string];
95 } 95 }
96 else { 96 else {
97 *index = -1; 97 *index = -1;
98 return nil; 98 return nil;
99 } 99 }
100 } 100 }
101 101
102 @end 102 @end
Powered by Google Project Hosting