File: NSColorWell.m

package info (click to toggle)
gnustep-gui 0.32.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,348 kB
  • sloc: objc: 178,763; ansic: 24,089; cpp: 664; yacc: 464; sh: 90; makefile: 72
file content (504 lines) | stat: -rw-r--r-- 12,621 bytes parent folder | download | duplicates (5)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/** <title>NSColorWell</title>

   <abstract>Control for selecting and display a single color value.</abstract>

   Copyright (C) 1996 Free Software Foundation, Inc.

   Author: Scott Christley <scottc@net-community.com>
   Date: 1996
   Author: Felipe A. Rodriguez <far@ix.netcom.com>
   Date: May 1998
   
   This file is part of the GNUstep GUI Library.

   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.
*/ 

#include "config.h"
#import "AppKit/NSActionCell.h"
#import "AppKit/NSApplication.h"
#import "AppKit/NSBezierPath.h"
#import "AppKit/NSColorPanel.h"
#import "AppKit/NSColorWell.h"
#import "AppKit/NSColor.h"
#import "AppKit/NSDragging.h"
#import "AppKit/NSEvent.h"
#import "AppKit/NSGraphics.h"
#import "AppKit/NSPasteboard.h"
#import "AppKit/NSWindow.h"
#import "GNUstepGUI/GSTheme.h"
#import <Foundation/NSDebug.h>
#import <Foundation/NSNotification.h>
#include <math.h>

static NSString *GSColorWellDidBecomeExclusiveNotification =
                    @"GSColorWellDidBecomeExclusiveNotification";

@implementation NSColorWell

/*
 * Class methods
 */
+ (void) initialize
{
  if (self == [NSColorWell class])
    {
      [self setVersion: 1];
    }
}

/*
 * Instance methods
 */

- (BOOL) acceptsFirstMouse: (NSEvent *)event
{
  return YES;
}

- (SEL) action
{
  return _action;
}

/**<p>Activates the NSColorWell and displays the NSColorPanel with the current
   NSColorWell's color. The NSColorWell can take color from the NSColorPanel.
   If exclusive is YES other NSColorWells are desacivated
   (through notifications).</p><p>See Also: -deactivate</p>
 */
- (void) activate: (BOOL)exclusive
{
  NSNotificationCenter	*nc = [NSNotificationCenter defaultCenter];
  NSColorPanel		*colorPanel = [NSColorPanel sharedColorPanel];

  if (exclusive == YES)
    {
      [nc postNotificationName: GSColorWellDidBecomeExclusiveNotification
                        object: self];
    }

  [nc addObserver: self
         selector: @selector(deactivate)
             name: GSColorWellDidBecomeExclusiveNotification
           object: nil];

  [nc addObserver: self
         selector: @selector(_takeColorFromPanel:)
             name: NSColorPanelColorDidChangeNotification
           object: nil];

  [nc addObserver: self
         selector: @selector(deactivate)
             name: NSWindowWillCloseNotification
           object: colorPanel];

  _is_active = YES;

  [colorPanel setColor: _the_color];
  [colorPanel orderFront: self];

  [self setNeedsDisplay: YES];
}

/**<p> Returns the current NSColor of the NSColorWell.</p>
   <p> See Also: -setColor:</p>
 */
- (NSColor *) color
{
  return _the_color;
}

/** <p>Deactivates the NSColorWell and marks self for display.
    It is usally call from an observer, when another NSColorWell is 
    activate.</p><p>See Also: -activate:</p>
 */
- (void) deactivate
{
  _is_active = NO;

  [[NSNotificationCenter defaultCenter] removeObserver: self];

  [self setNeedsDisplay: YES];
}

- (void) dealloc
{
  if (_is_active == YES)
    {
      [self deactivate];
    }
  TEST_RELEASE(_the_color);
  [self unregisterDraggedTypes];
  [super dealloc];
}

- (NSDragOperation) draggingEntered: (id <NSDraggingInfo>)sender
{
  NSPasteboard *pb;
  NSDragOperation sourceDragMask;
       
  NSDebugLLog(@"NSColorWell", @"%@: draggingEntered", self);

  if ([self isEnabled] == NO)
    return NSDragOperationNone;
 
  sourceDragMask = [sender draggingSourceOperationMask];
  pb = [sender draggingPasteboard];
 
  if ([[pb types] indexOfObject: NSColorPboardType] != NSNotFound)
    {
      if (sourceDragMask & NSDragOperationCopy)
        {
          return NSDragOperationCopy;
        }
    }
 
  return NSDragOperationNone;
} 

- (NSDragOperation) draggingSourceOperationMaskForLocal: (BOOL)flag
{
  return NSDragOperationCopy;
}

- (void) drawRect: (NSRect)clipRect
{
  if (NSIntersectsRect(_bounds, clipRect) == NO)
    {
      return;
    }

  _wellRect = [[GSTheme theme] drawColorWellBorder: self
                               withBounds: _bounds
                               withClip: clipRect];
  [self drawWellInside: _wellRect];
}

/**<p>Draws the NSColorWell inside the rectangle <var>insideRect</var>.</p>
   <p>See Also: [NSColor-drawSwatchInRect:]</p>
 */
- (void) drawWellInside: (NSRect)insideRect
{
  if (NSIsEmptyRect(insideRect))
    {
      return;
    }
  [_the_color drawSwatchInRect: insideRect];
}

- (void) encodeWithCoder: (NSCoder*)aCoder
{
  [super encodeWithCoder: aCoder];
  if ([aCoder allowsKeyedCoding])
    {
      [aCoder encodeObject: _the_color forKey: @"NSColor"];
      // [aCoder encodeBool: _is_active forKey: @"NSEnabled"];
      [aCoder encodeBool: _is_bordered forKey: @"NSIsBordered"];
      [aCoder encodeConditionalObject: _target forKey: @"NSTarget"];
      [aCoder encodeConditionalObject: NSStringFromSelector(_action) forKey: @"NSAction"];
    }
  else
    {
      [aCoder encodeObject: _the_color];
      [aCoder encodeValueOfObjCType: @encode(BOOL) at: &_is_active];
      [aCoder encodeValueOfObjCType: @encode(BOOL) at: &_is_bordered];
      [aCoder encodeConditionalObject: _target];
      [aCoder encodeValueOfObjCType: @encode(SEL) at: &_action];
    }
}

- (id) initWithCoder: (NSCoder*)aDecoder
{
  self = [super initWithCoder: aDecoder];
  if (self != nil)
    {
      if ([aDecoder allowsKeyedCoding])
	{
	  NSString *action;

	  ASSIGN(_the_color, [aDecoder decodeObjectForKey: @"NSColor"]);
	  // _is_active =  [aDecoder decodeBoolForKey: @"NSEnabled"];
	  _is_bordered = [aDecoder decodeBoolForKey: @"NSIsBordered"];
	  _target = [aDecoder decodeObjectForKey: @"NSTarget"];
	  action = [aDecoder decodeObjectForKey: @"NSAction"];
	  _action = NSSelectorFromString(action);
	  [self registerForDraggedTypes:
		  [NSArray arrayWithObjects: NSColorPboardType, nil]];
	}
      else
	{
	  [aDecoder decodeValueOfObjCType: @encode(id) at: &_the_color];
	  [aDecoder decodeValueOfObjCType: @encode(BOOL) at: &_is_active];
	  [aDecoder decodeValueOfObjCType: @encode(BOOL) at: &_is_bordered];
	  [aDecoder decodeValueOfObjCType: @encode(id) at: &_target];
	  // Undo RETAIN by decoder
	  TEST_RELEASE(_target);
	  [aDecoder decodeValueOfObjCType: @encode(SEL) at: &_action];
	  [self registerForDraggedTypes:
		  [NSArray arrayWithObjects: NSColorPboardType, nil]];
	}
    }
  return self;
}

- (id) initWithFrame: (NSRect)frameRect
{
  self = [super initWithFrame: frameRect];
  if (self != nil)
    {
      _is_bordered = YES;
      _is_active = NO;
      _the_color = RETAIN([NSColor blackColor]);

      [self registerForDraggedTypes:
	[NSArray arrayWithObjects: NSColorPboardType, nil]];
    }
  return self;
}

/** <p>Returns whether the NSColorWell is active. By default a NSColorWell
    is not active.</p>
    <p>See Also: -activate: -deactivate</p>
 */
- (BOOL) isActive
{
  return _is_active;
}

/** <p>Returns whether the NSColorWell has border. By default a NSColorWell
    has border.</p><p>See Also: -setBordered:</p>
 */
- (BOOL) isBordered
{
  return _is_bordered;
}

- (BOOL) isOpaque
{
  // May not be opaque, due to themes
  return NO;
}

- (void) mouseDown: (NSEvent *)theEvent
{
  //
  // OPENSTEP 4.2 and OSX behavior indicates that the colorwell doesn't
  // work when the widget is marked as disabled.
  //
  if ([self isEnabled] == NO)
    return;

  // Unbordered color wells start a drag immediately upon mouse down
  if ([self isBordered] == NO)
    {
      [NSColorPanel dragColor: _the_color
		    withEvent: theEvent
		    fromView: self];
      return;
    }

  _mouseDownPoint = [self convertPoint: [theEvent locationInWindow]
			  fromView: nil];
  [[self cell] setHighlighted: YES];
  [self setNeedsDisplay: YES];
}

- (void) mouseDragged: (NSEvent *)theEvent
{
  NSPoint point = [self convertPoint: [theEvent locationInWindow]
			fromView: nil];
  BOOL inside = [self mouse: point inRect: [self bounds]];
  BOOL startedInWell = [self mouse: _mouseDownPoint inRect: _wellRect];

  NSSize delta = NSMakeSize(_mouseDownPoint.x - point.x,
                            _mouseDownPoint.y - point.y);
  double distance = sqrt(delta.width*delta.width + delta.height*delta.height);

  // FIXME: Make the dragging threshold a user default
  if (distance < 4)
    return;

  if ([self isEnabled] == NO)
    return;

  if (startedInWell)
    {
      [[self cell] setHighlighted: NO];
      [self setNeedsDisplay: YES];

      [NSColorPanel dragColor: _the_color
		    withEvent: theEvent
		    fromView: self];
      return;
    }
  else
    {
      [[self cell] setHighlighted: inside];
      [self setNeedsDisplay: YES];
    }
}

- (void) mouseUp: (NSEvent *)theEvent
{
  NSPoint point = [self convertPoint: [theEvent locationInWindow]
			fromView: nil];
  BOOL inside = [self mouse: point inRect: [self bounds]];

  if ([self isEnabled] == NO)
    return;

  [[self cell] setHighlighted: NO];
  [self setNeedsDisplay: YES];

  if (inside)
    {
      [self performClick: self];
    }
}

- (id) objectValue
{
  return [self color];
}

- (void) performClick: (id)sender
{
  if ([self isActive])
    {
      [self deactivate];
    }
  else
    {
      [self activate: YES];
    }
}

- (BOOL) performDragOperation: (id <NSDraggingInfo>)sender
{
  NSPasteboard *pb = [sender draggingPasteboard];
         
  NSDebugLLog(@"NSColorWell", @"%@: performDragOperation", self);
  [self setColor: [NSColor colorFromPasteboard: pb]];
  /* When our color is changed by having a new color dropped on us,
   * we send our action.
   */
  [self sendAction: _action to: _target];
  return YES;
}
 
- (BOOL) prepareForDragOperation: (id <NSDraggingInfo>)sender
{
  return YES;
}
 
- (void) setAction: (SEL)action
{
  _action = action;
}

/**<p>Sets whether the NSColorWell has border and marks self for display.
   By default a NSColorWell has border.</p><p>See Also: -isBordered</p>
 */
- (void) setBordered: (BOOL)bordered
{
  _is_bordered = bordered;
  [self setNeedsDisplay: YES];
}

/** <p>Sets the NSColorWell to color and marks self for display.<br />
 * Sets the NSColorPanel if active.<br />
 * Does NOT notify target of color change.
 * </p>
 * <p>See Also: -color</p>
 */
- (void) setColor: (NSColor *)color
{
  ASSIGN(_the_color, color);
  [self setNeedsDisplay: YES];
  /*
   * Experimentation with NeXTstep shows that when the color of an active
   * colorwell is set, the color of the shared color panel is set too,
   * though this does not raise the color panel, only the event of
   * activation does that.
   */
  if ([self isActive])
    {
      NSColorPanel	*colorPanel = [NSColorPanel sharedColorPanel];

      [colorPanel setColor: _the_color];
    }
}

- (void) setObjectValue: (id)anObject
{
  [self setColor: anObject];
}

- (void) setTarget: (id)target
{
  _target = target;
}

/** <p>Sets the NSColorWell's color to the sender color.</p>
 *  <p>See Also: -setColor: </p>
 */
- (void) takeColorFrom: (id)sender
{
  if ([sender respondsToSelector: @selector(color)])
    {
      [self setColor: [sender color]];
    }
}

- (void) _takeColorFromPanel: (NSNotification *) notification
{
  id sender = [notification object];

  if ([sender respondsToSelector: @selector(color)])
    {
      NSColor	*c = [(id)sender color];

      /* Don't use -setColor: as that would send a message back to the
       * panel telling it to se its color again.
       * Instead we assign the color and mark for redisplay directly.
       * NB. For MacOS-X compatibility, we only send the action if the
       * coor has actually changed.
       */
      if (c != nil && [c isEqual: _the_color] == NO)
	{
	  ASSIGN(_the_color, [(id)sender color]);
	  [self setNeedsDisplay: YES];
	  /* When our color is changed from the color panel, we should
	   * send our action.
	   */
	  [self sendAction: _action to: _target];
	}
    }
}

- (void) takeObjectValueFrom: (id)sender
{
  [self takeColorFrom: sender];
}

- (id) target
{
  return _target;
}

@end