File: GSNamedColorPicker.m

package info (click to toggle)
gnustep-gui 0.9.5-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 9,464 kB
  • ctags: 4,689
  • sloc: objc: 104,993; ansic: 28,794; cpp: 3,058; makefile: 353; yacc: 298; sh: 37
file content (234 lines) | stat: -rw-r--r-- 5,397 bytes parent folder | download
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
/* 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 program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program 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 General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <Foundation/Foundation.h>
#include <AppKit/AppKit.h>

@interface GSNamedColorPicker: NSColorPicker <NSColorPickingCustom>
{
  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)
    {
      lists =  [[NSColorList availableColorLists] mutableCopy];
      return [super initWithPickerMask: aMask
		    colorPanel: colorPanel];
    }
  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;

  if (c == nil)
    return;

  list = [c catalogNameComponent];
  name = [c colorNameComponent];
}

- (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: (int)column
{
  return nil;
}

-(void) browser: (NSBrowser *)sender 
createRowsForColumn: (int)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: (int)row
	inColumn: (int)column
{
}

- (void) browser: (NSBrowser *)sender 
 willDisplayCell: (id)cell
	   atRow: (int)row 
	  column: (int)column
{
}

- (int) numberOfItemsInComboBox: (NSComboBox *)aComboBox
{
   return [lists count];
}

- (id) comboBox: (NSComboBox *)aComboBox 
objectValueForItemAtIndex: (int)index
{
   return [(NSColorList*)[lists objectAtIndex: index] name];
}

- (unsigned int) comboBox: (NSComboBox *)aComboBox
indexOfItemWithStringValue: (NSString *)string
{
   return [lists indexOfObject: [NSColorList colorListNamed: string]];
}

- (void) comboBoxSelectionDidChange: (NSNotification *)notification
{
  [self listSelected: [cb indexOfSelectedItem]];
}

@end