1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
/* GSNamedColorPicker.m
Copyright (C) 2001 Free Software Foundation, Inc.
Author: Fred Kiefer <FredKiefer@gmx.de>
Date: January 2001
This file is part of GNUstep.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; see the file COPYING.LIB.
If not, see <http://www.gnu.org/licenses/> or write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
@interface GSNamedColorPicker: NSColorPicker <NSColorPickingCustom, NSTextFieldDelegate>
{
NSView *baseView;
NSComboBox *cb;
NSBrowser *browser;
NSColorList *currentList;
NSMutableArray *lists;
}
- (void) loadViews;
- (void) listSelected: (int)index;
- (void) colorSelected: (id)sender;
@end
@implementation GSNamedColorPicker
- (id) initWithPickerMask: (int)aMask
colorPanel: (NSColorPanel *)colorPanel
{
if (aMask & NSColorPanelColorListModeMask)
{
self = [super initWithPickerMask: aMask
colorPanel: colorPanel];
if (nil == self)
return nil;
lists = [[NSColorList availableColorLists] mutableCopy];
return self;
}
RELEASE(self);
return nil;
}
- (void) dealloc
{
RELEASE(cb);
RELEASE(browser);
RELEASE(baseView);
RELEASE(lists);
[super dealloc];
}
- (void) finishInstantiate
{
}
- (void) attachColorList: (NSColorList *)colorList
{
[lists addObject: colorList];
[cb noteNumberOfItemsChanged];
}
- (void) detachColorList: (NSColorList *)colorList
{
[lists removeObjectIdenticalTo: colorList];
[cb noteNumberOfItemsChanged];
}
- (int) currentMode
{
return NSColorListModeColorPanel;
}
- (BOOL) supportsMode: (int)mode
{
return mode == NSColorListModeColorPanel;
}
- (NSView *) provideNewView: (BOOL)initialRequest
{
if (initialRequest)
{
[self loadViews];
}
return baseView;
}
- (void) setColor: (NSColor *)color
{
NSColor *c = [color colorUsingColorSpaceName: NSNamedColorSpace];
NSString *list;
NSString *name;
NSUInteger index;
if (c == nil)
return;
list = [c catalogNameComponent];
name = [c colorNameComponent];
// Select the correspondig entries in the lists
index = [self comboBox: cb
indexOfItemWithStringValue: list];
if (index == NSNotFound)
return;
[cb selectItemAtIndex: index];
index = [[currentList allKeys] indexOfObject: name];
if (index == NSNotFound)
return;
[browser selectRow: index inColumn: 0];
}
- (void) loadViews
{
baseView = [[NSView alloc] initWithFrame: NSMakeRect(0, 0, 200, 110)];
[baseView setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];
cb = [[NSComboBox alloc] initWithFrame: NSMakeRect(0, 85, 196, 20)];
[cb setAutoresizingMask: (NSViewWidthSizable | NSViewMinYMargin)];
[cb setUsesDataSource: YES];
[cb setDataSource: self];
[cb setDelegate: self];
[cb setEditable: NO];
[baseView addSubview: cb];
browser = [[NSBrowser alloc] initWithFrame: NSMakeRect(0, 5, 196, 75)];
[browser setDelegate: self];
[browser setMaxVisibleColumns: 1];
[browser setAllowsMultipleSelection: NO];
[browser setAllowsEmptySelection: NO];
[browser setHasHorizontalScroller: NO];
[browser setTitled: NO];
[browser setTakesTitleFromPreviousColumn: NO];
[browser setPath: @"/"];
[browser setTarget: self];
[browser setDoubleAction: @selector(colorSelected:)];
[browser setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];
[baseView addSubview: browser];
}
- (void) listSelected: (int)index
{
currentList = [lists objectAtIndex: index];
[browser loadColumnZero];
}
- (void) colorSelected: (id)sender
{
NSCell *aCell;
NSColor *col;
aCell = [sender selectedCell];
col = [aCell representedObject];
[_colorPanel setColor: col];
}
-(NSString *) browser: (NSBrowser *)sender
titleOfColumn: (NSInteger)column
{
return nil;
}
-(void) browser: (NSBrowser *)sender
createRowsForColumn: (NSInteger)column
inMatrix: (NSMatrix *)matrix
{
int i;
int count;
NSBrowserCell *cell;
NSColor *cl;
NSArray *keys = [currentList allKeys];
NSString *name;
count = [keys count];
//NSLog(@"In create with %@ %d", currentList, count);
if (count)
{
[matrix addColumn];
for (i = 0; i < count; i++)
{
if (i > 0)
[matrix addRow];
name = [keys objectAtIndex: i];
//cl = [currentList colorWithKey: name];
cl = [NSColor colorWithCatalogName: [currentList name]
colorName: name];
cell = [matrix cellAtRow: i
column: 0];
[cell setStringValue: name];
[cell setRepresentedObject: cl];
[cell setLeaf: YES];
}
}
}
- (BOOL) browser: (NSBrowser*)sender
selectRow: (NSInteger)row
inColumn: (NSInteger)column
{
return NO;
}
- (void) browser: (NSBrowser *)sender
willDisplayCell: (id)cell
atRow: (NSInteger)row
column: (NSInteger)column
{
}
- (NSInteger) numberOfItemsInComboBox: (NSComboBox *)aComboBox
{
return [lists count];
}
- (id) comboBox: (NSComboBox *)aComboBox
objectValueForItemAtIndex: (NSInteger)index
{
return [(NSColorList*)[lists objectAtIndex: index] name];
}
- (NSUInteger) comboBox: (NSComboBox *)aComboBox
indexOfItemWithStringValue: (NSString *)string
{
return [lists indexOfObject: [NSColorList colorListNamed: string]];
}
- (void) comboBoxSelectionDidChange: (NSNotification *)notification
{
[self listSelected: [cb indexOfSelectedItem]];
}
@end
|