1/* GSStandardColorPicker.m
2
3   Copyright (C) 2001 Free Software Foundation, Inc.
4
5   Author:  Fred Kiefer <FredKiefer@gmx.de>
6   Date: Febuary 2001
7   Author: Alexander Malmberg <alexander@malmberg.org>
8   Date: May 2002
9
10   This file is part of GNUstep.
11
12   This library is free software; you can redistribute it and/or
13   modify it under the terms of the GNU Lesser General Public
14   License as published by the Free Software Foundation; either
15   version 2 of the License, or (at your option) any later version.
16
17   This library is distributed in the hope that it will be useful,
18   but WITHOUT ANY WARRANTY; without even the implied warranty of
19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
20   Lesser General Public License for more details.
21
22   You should have received a copy of the GNU Lesser General Public
23   License along with this library; see the file COPYING.LIB.
24   If not, see <http://www.gnu.org/licenses/> or write to the
25   Free Software Foundation, 51 Franklin Street, Fifth Floor,
26   Boston, MA 02110-1301, USA.
27*/
28
29#import <Foundation/Foundation.h>
30#import <AppKit/AppKit.h>
31#import <GNUstepGUI/GSVbox.h>
32#import <GNUstepGUI/GSHbox.h>
33#import "GSRGBColorPicker.h"
34#import "GSHSBColorPicker.h"
35#import "GSCMYKColorPicker.h"
36#import "GSGrayColorPicker.h"
37#import "GSStandardColorPicker.h"
38
39@implementation GSStandardColorPicker
40
41- (void) dealloc
42{
43  RELEASE(pickers);
44  RELEASE(pickerBox);
45  RELEASE(pickerMatrix);
46  RELEASE(baseView);
47  [super dealloc];
48}
49
50- (id)initWithPickerMask:(int)aMask
51              colorPanel:(NSColorPanel *)colorPanel
52{
53  if (aMask & (NSColorPanelRGBModeMask | NSColorPanelHSBModeMask |
54               NSColorPanelCMYKModeMask | NSColorPanelGrayModeMask))
55  {
56    NSColorPicker *picker;
57
58    pickers = [[NSMutableArray alloc] init];
59    picker = [[GSGrayColorPicker alloc] initWithPickerMask: aMask
60                                        colorPanel: colorPanel];
61    if (picker != nil)
62      {
63        [pickers addObject: picker];
64        RELEASE(picker);
65      }
66    picker = [[GSRGBColorPicker alloc] initWithPickerMask: aMask
67                                       colorPanel: colorPanel];
68    if (picker != nil)
69      {
70        [pickers addObject: picker];
71        RELEASE(picker);
72      }
73    picker = [[GSCMYKColorPicker alloc] initWithPickerMask: aMask
74                                        colorPanel: colorPanel];
75    if (picker != nil)
76      {
77        [pickers addObject: picker];
78        RELEASE(picker);
79      }
80    picker = [[GSHSBColorPicker alloc] initWithPickerMask: aMask
81                                       colorPanel: colorPanel];
82    if (picker != nil)
83      {
84        [pickers addObject: picker];
85        RELEASE(picker);
86      }
87
88    currentPicker = [pickers lastObject];
89    return [super initWithPickerMask: aMask
90                  colorPanel: colorPanel];
91  }
92  RELEASE(self);
93  return nil;
94}
95
96- (int)currentMode
97{
98  return [currentPicker currentMode];
99}
100
101- (void)setMode:(int)mode
102{
103  int i, count;
104
105  if (mode == [self currentMode])
106    return;
107
108  count = [pickers count];
109  for (i = 0; i < count; i++)
110    {
111      if ([[pickers objectAtIndex: i] supportsMode: mode])
112        {
113          [pickerMatrix selectCellWithTag: i];
114          [self _showNewPicker: pickerMatrix];
115          [currentPicker setMode: mode];
116          break;
117        }
118    }
119}
120
121- (BOOL)supportsMode:(int)mode
122{
123  return ((mode == NSGrayModeColorPanel) ||
124          (mode == NSRGBModeColorPanel)  ||
125          (mode == NSCMYKModeColorPanel) ||
126          (mode == NSHSBModeColorPanel));
127}
128
129- (void)insertNewButtonImage:(NSImage *)newImage
130                          in:(NSButtonCell *)newButtonCell
131{
132  // Store the image button cell
133  imageCell = newButtonCell;
134  [super insertNewButtonImage: newImage
135         in: newButtonCell];
136}
137
138- (NSView *)provideNewView:(BOOL)initialRequest
139{
140  if (initialRequest)
141    {
142      [self loadViews];
143    }
144  return baseView;
145}
146
147- (NSImage *)provideNewButtonImage
148{
149  return [currentPicker provideNewButtonImage];
150}
151
152- (void)setColor:(NSColor *)color
153{
154  [currentPicker setColor: color];
155}
156
157- (void) loadViews
158{
159  NSEnumerator *enumerator;
160  id<NSColorPickingCustom, NSColorPickingDefault> picker;
161  NSButtonCell *cell;
162  NSMutableArray *cells = [NSMutableArray new];
163  int i, count;
164
165  // Initaliase all the sub pickers
166  enumerator = [pickers objectEnumerator];
167
168  while ((picker = [enumerator nextObject]) != nil)
169    [picker provideNewView: YES];
170
171  baseView = [[GSTable alloc] initWithNumberOfRows: 3 numberOfColumns: 1];
172  [baseView setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
173  [baseView setYResizingEnabled: NO forRow: 1];
174  [baseView setYResizingEnabled: NO forRow: 2];
175
176  // Prototype cell for the matrix
177  cell = [[NSButtonCell alloc] initImageCell: nil];
178  [cell setButtonType: NSOnOffButton];
179  [cell setBordered: YES];
180
181  count = [pickers count];
182  pickerMatrix = [[NSMatrix alloc] initWithFrame: NSMakeRect(0,0,0,0)
183                                   mode: NSRadioModeMatrix
184                                   prototype: cell
185                                   numberOfRows: 0
186                                   numberOfColumns: count];
187  RELEASE(cell);
188  [pickerMatrix setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];
189  [pickerMatrix setIntercellSpacing: NSMakeSize(1, 0)];
190  [pickerMatrix setAutosizesCells: YES];
191
192  for (i = 0; i < count; i++)
193    {
194      cell = [[pickerMatrix prototype] copy];
195      [cell setTag: i];
196      picker = [pickers objectAtIndex: i];
197      [picker insertNewButtonImage: [picker provideNewButtonImage] in: cell];
198      [cells addObject: cell];
199      RELEASE(cell);
200    }
201
202  [pickerMatrix addRowWithCells: cells];
203  RELEASE(cells);
204  [pickerMatrix setCellSize: NSMakeSize(1, 36)];
205  [pickerMatrix setTarget: self];
206  [pickerMatrix setAction: @selector(_showNewPicker:)];
207
208  pickerBox = [[NSBox alloc] init];
209  [pickerBox setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
210  [baseView putView: pickerBox
211        atRow: 0
212        column: 0
213        withMargins: 0];
214  [pickerBox setTitlePosition: NSNoTitle];
215  [pickerBox setBorderType: NSNoBorder];
216  [pickerBox setContentView: [currentPicker provideNewView: NO]];
217
218  [baseView putView: pickerMatrix
219        atRow: 1
220        column: 0
221        withMargins: 0];
222
223  {
224    NSBox *b = [[NSBox alloc] initWithFrame: NSMakeRect(0,0,0,2)];
225    [b setAutoresizingMask: NSViewWidthSizable];
226    [b setTitlePosition: NSNoTitle];
227    [b setBorderType: NSGrooveBorder];
228    [baseView putView: b
229        atRow: 2
230        column: 0
231        withMinXMargin: 0
232        maxXMargin: 0
233        minYMargin: 4
234        maxYMargin: 0];
235    DESTROY(b);
236  }
237}
238
239- (void) _showNewPicker: (id) sender
240{
241  currentPicker = [pickers objectAtIndex: [sender selectedColumn]];
242  [currentPicker setColor: [_colorPanel color]];
243  [pickerBox setContentView: [currentPicker provideNewView: NO]];
244
245  // Show the new image
246  [imageCell setImage: [[sender selectedCell] image]];
247}
248
249@end
250
251
252
253@implementation GSStandardCSColorPicker
254
255- (void) dealloc
256{
257  int i;
258  for (i = 0; i < numFields; i++)
259    {
260      RELEASE(sliders[i]);
261      RELEASE(fields[i]);
262    }
263  RELEASE(baseView);
264  [super dealloc];
265}
266
267- (int)currentMode
268{
269  return currentMode;
270}
271
272- (BOOL)supportsMode:(int)mode
273{
274  return currentMode == NSRGBModeColorPanel;
275}
276
277- (NSView *)provideNewView:(BOOL)initialRequest
278{
279  if (initialRequest)
280    {
281      [self loadViews];
282    }
283  return baseView;
284}
285
286- (void)setColor:(NSColor *)color
287{
288  [self subclassResponsibility: _cmd];
289}
290
291- (void) loadViews
292{
293  int i;
294
295  baseView = [[GSTable alloc] initWithNumberOfRows: numFields + 2 numberOfColumns: 2];
296  /* keep a single resizable but empty row at the bottom so it sucks up any
297  extra space in the box that contains us */
298  for (i = 1; i < numFields + 2; i++)
299    [baseView setYResizingEnabled: NO forRow: i];
300  [baseView setXResizingEnabled: YES forColumn: 0];
301  [baseView setXResizingEnabled: NO forColumn: 1];
302  [baseView setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin];
303
304  for (i = 0; i < numFields; i++)
305    {
306      NSSlider *s;
307      NSCell *c;
308
309      s = sliders[i] = [[NSSlider alloc] initWithFrame: NSMakeRect(0, 0, 0, 16)];
310      c = [[GSColorSliderCell alloc] init];
311      [s setCell: c];
312      RELEASE(c);
313      [s setContinuous: YES];
314      [s setMinValue: 0.0];
315      [s setTitle: names[i]];
316      [s setTarget: self];
317      [s setAction: @selector(sliderChanged:)];
318
319      [[s cell] setBezeled: YES];
320      [s setAutoresizingMask: NSViewWidthSizable];
321
322      [baseView putView: s
323        atRow: numFields - i
324        column: 0
325        withMinXMargin: 0
326        maxXMargin: 0
327        minYMargin: 0
328        maxYMargin: i?6:2];
329    }
330
331  if (maxValue)
332    {
333      NSTextField *tv;
334      GSHbox *hb=[[GSHbox alloc] init];
335
336      tv=[[NSTextField alloc] init];
337      [tv setStringValue: @"0"];
338      [tv setEditable: 0];
339      [tv setFont: [NSFont userFontOfSize: 10.0]];
340      [tv setTextColor: [NSColor darkGrayColor]];
341      [tv setDrawsBackground: NO];
342      [tv setBordered: NO];
343      [tv setSelectable: NO];
344      [tv setBezeled: NO];
345      [tv sizeToFit];
346      [tv setAutoresizingMask: NSViewMaxXMargin];
347      [hb addView: tv];
348      DESTROY(tv);
349
350      tv=[[NSTextField alloc] init];
351      [tv setIntValue: maxValue];
352      [tv setEditable: 0];
353      [tv setFont: [NSFont userFontOfSize: 10.0]];
354      [tv setTextColor: [NSColor darkGrayColor]];
355      [tv setDrawsBackground: NO];
356      [tv setBordered: NO];
357      [tv setSelectable: NO];
358      [tv setBezeled: NO];
359      [tv sizeToFit];
360      [tv setAutoresizingMask: NSViewMinXMargin];
361      [hb addView: tv];
362      DESTROY(tv);
363
364      [hb setAutoresizingMask: NSViewWidthSizable];
365      [baseView putView: hb
366        atRow: numFields + 1
367        column: 0
368        withMargins: 0];
369      DESTROY(hb);
370    }
371  else
372    {
373      NSTextField *tv;
374      NSView *v;
375      NSRect frame;
376
377      tv=[[NSTextField alloc] init];
378      [tv setStringValue: @"0"];
379      [tv setEditable: 0];
380      [tv setFont: [NSFont userFontOfSize: 10.0]];
381      [tv setTextColor: [NSColor darkGrayColor]];
382      [tv setDrawsBackground: NO];
383      [tv setBordered: NO];
384      [tv setSelectable: NO];
385      [tv setBezeled: NO];
386      [tv sizeToFit];
387      frame=[tv frame];
388      DESTROY(tv);
389      v = [[NSView alloc] initWithFrame: frame];
390      [baseView putView: v
391        atRow: numFields + 1
392        column: 0
393        withMargins: 0];
394      DESTROY(v);
395    }
396
397  for (i = 0; i < numFields; i++)
398    {
399      NSTextField *f;
400      f = fields[i] = [[NSTextField alloc] init];
401      [f setStringValue: @"255"]; /* just to get a good size */
402      [f setFont: [NSFont userFontOfSize: 10.0]];
403      [f sizeToFit];
404      [f setFrameSize: NSMakeSize([f frame].size.width * 1.5, [f frame].size.height)];
405      [f setDelegate: self];
406      [baseView putView: f
407        atRow: numFields - i
408        column: 1
409        withMinXMargin: 3
410        maxXMargin: 0
411        minYMargin: 0
412        maxYMargin: 0];
413    }
414}
415
416- (void) sliderChanged: (id) sender
417{
418  int i;
419
420  if (updating)
421    return;
422  updating = YES;
423
424  for (i = 0; i < numFields; i++)
425    {
426      values[i] = [sliders[i] floatValue];
427      [fields[i] setIntValue: (int)values[i]];
428    }
429
430  [self _setColorFromValues];
431
432  updating = NO;
433}
434
435-(void) controlTextDidChange: (NSNotification *)n
436{
437  int i;
438
439  if (updating)
440    return;
441  updating = YES;
442
443  for (i = 0; i < numFields; i++)
444    {
445      values[i] = [fields[i] floatValue];
446      [sliders[i] setIntValue: (int)values[i]];
447    }
448
449  [self _setColorFromValues];
450
451  updating = NO;
452}
453
454-(void) _valuesChanged
455{
456  int i;
457
458  for (i = 0; i < numFields; i++)
459    {
460      [fields[i] setIntValue: (int)values[i]];
461      [sliders[i] setIntValue: (int)values[i]];
462    }
463}
464
465-(void) _setColorFromValues
466{
467  [self subclassResponsibility: _cmd];
468}
469
470@end
471