File: TimeMonColors.m

package info (click to toggle)
timemon.app 4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 580 kB
  • sloc: objc: 698; ansic: 161; makefile: 23; sh: 9
file content (232 lines) | stat: -rw-r--r-- 6,777 bytes parent folder | download | duplicates (2)
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
// Copyright 1994, 1997 Scott Hess.  Permission to use, copy, modify,
// and distribute this software and its documentation for any
// purpose and without fee is hereby granted, provided that this
// copyright notice appear in all copies.
// 
// Scott Hess makes no representations about the suitability of
// this software for any purpose.  It is provided "as is" without
// express or implied warranty.
//
#import "TimeMonColors.h"
#import "TimeMonWraps.h"
#import "NSColorExtensions.h"

@interface NSTextFieldCell (TimeMonTextFieldCell)
- (void)setColor:(NSColor *)color;
@end

@implementation TimeMonColors
// Have to set up the cells and stuff manually since we're a
// custom view.
- (id)initWithFrame:(NSRect)frameRect
{
  self = [super initWithFrame:frameRect mode:0 cellClass:[NSTextFieldCell class] numberOfRows:1 numberOfColumns:4];
  if (self) 
    {
      NSSize size = { 1, frameRect.size.height};
      [self setIntercellSpacing:size];
      size.width = ((int)frameRect.size.width-3)/4;
      [self setCellSize:size];
      [self setAutosizesCells:YES];
      [[self window] setAcceptsMouseMovedEvents:YES];
      [self registerForDraggedTypes:[NSArray arrayWithObject:NSColorPboardType]];
    }
  return self;
}

- (BOOL)isFlipped
{
    return NO;
}

static const id titles[] = {
    @"Idle", @"Nice", @"User", @"System",
};

// Read the colors for each field and stuff them into it.
- (void)readColors
{
    int i;
    BOOL shouldDrawColor = [self shouldDrawColor];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    for(i = 0; i < 4; i++) {
        NSTextFieldCell *tCell = [self cellAtRow:0 column:i];
        [tCell setStringValue: titles[i]];
	// If self is in a color window, then load color
	// information.
        if (shouldDrawColor) 
	  {
            NSString *key = [NSString stringWithFormat:@"%@Color",titles[i]];
            NSString *string = [defaults objectForKey:key];
            [tCell setColor:[NSColor colorFromStringRepresentation:string]];
	    
            // Otherwise, load grayscale information.
	  } 
	else 
	  {
            NSString *key = [NSString stringWithFormat:@"%@Gray",titles[i]];
            CGFloat color = [defaults floatForKey:key];
            [tCell setColor:[NSColor colorWithCalibratedWhite:color alpha:1.0]];
	  }
        [tCell setBezeled:YES];
        [tCell setAlignment:NSCenterTextAlignment];
    }
}

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

- (BOOL)acceptsFirstResponder
{
    return YES;
}

- (BOOL)shouldDelayWindowOrderingForEvent:(NSEvent *)theEvent
{
    return YES;
}

// Intercept the mousedown and cause it to drag a color.  If
// double-click, pull up the color panel.
- (void)mouseDown:(NSEvent *)e 
{
    NSPoint loc = [e locationInWindow];
    NSInteger row, col;
    NSColor * color;
    
    // Find the color of the cell the click is in.
    loc = [self convertPoint:loc fromView:nil];
    [self getRow:&row column:&col forPoint:loc];
    color = [[self cellAtRow:row column:col] backgroundColor];
    
    // If it's a double-click, load the color into the color
    // panel and bring that panel up.
    if ([e clickCount] > 1) 
      {
        id newVar = [NSColorPanel sharedColorPanel];
        [newVar setColor:color];
        [newVar orderFront:nil];
      }
    else 
      {
        NSEvent *theEvent = e;
	
        // Get the next mouse up or dragged event.
        e = [[self window] nextEventMatchingMask:(NSLeftMouseUpMask|NSLeftMouseDraggedMask)];
	
        // If it was a drag, initiate the color drag from
        // ourselves.
        if (e) 
	  {
            if ([e type] == NSLeftMouseDragged) 
	      {
		[NSColorPanel dragColor:color withEvent:theEvent fromView:self];
	      }
	  }
      }
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    NSPasteboard *pboard;

    pboard = [sender draggingPasteboard];
    if ([[pboard types] indexOfObject:NSColorPboardType] != NSNotFound) 
      {
        return NSDragOperationAll;
      }
    return NSDragOperationNone;
}

- (NSDragOperation)draggingSourceOperationMaskForLocal: (BOOL)isLocal
{
  return NSDragOperationGeneric;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    NSInteger row, col;
    NSPoint loc = [sender draggingLocation];
    NSTextFieldCell *tCell;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSPasteboard *pasteboard = [sender draggingPasteboard];
    NSColor *color = nil;

    if ([[pasteboard types] containsObject:NSColorPboardType]) 
      {
        color = [NSColor colorFromPasteboard:pasteboard];
      }
    else 
      {
        NSLog(@"No color");
        return NO;
      }

    // Find the cell for the point.
    loc = [self convertPoint:loc fromView:nil];
    [self getRow:&row column:&col forPoint:loc];
    tCell = [self cellAtRow:row column:col];

    // Set the color for the cell and redraw it.
    [tCell setColor:color];
    [self lockFocus];
    [self drawCellInside:tCell];
    [[self window] flushWindow];
    [self unlockFocus];

    // If self is in a color window, save color information.
    if ([self shouldDrawColor]) 
      {
        NSString *key = [NSString stringWithFormat:@"%@Color",titles[col]];
        [defaults setObject:[color stringRepresentation] forKey:key];
	// Otherwise, save grayscale information.
      } 
    else 
      {
        CGFloat gray, alpha;
        NSString *key = [NSString stringWithFormat:@"%@Gray",titles[col]];
        [[color colorUsingColorSpaceName:NSCalibratedWhiteColorSpace] getWhite:&gray alpha:&alpha];
        [defaults setFloat:gray forKey:key];
      }
    [defaults synchronize];
    
    return YES;
}

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
  [[NSApp delegate] display];
}
@end

@implementation NSTextFieldCell (TimeMonTextFieldCell)
// Set the colors for the field.  Adjust the foreground color
// so that it's visible against the background.
- (void)setColor:(NSColor *)color
{
    CGFloat gray;
    CGFloat r, g, b;
    
    // Remove any alpha component.
    color = [color colorWithAlphaComponent:1.0];
    
    // Get the grayscale version.
    gray = [[color colorUsingColorSpaceName:NSCalibratedWhiteColorSpace] whiteComponent];
    
    // Set the text to be visible.
    [self setTextColor:[NSColor colorWithCalibratedWhite:(gray < 0.33 ? NSWhite : NSBlack) alpha:1.0]];
    
    // Set the color and gray for the field.
    [self setBackgroundColor:color];
    
    // Tell the windowserver code that we want to use this
    // color for drawing the appropriate element.  [Note that
    // stringValue should be one of "Idle", "Nice", "User", or
    // "System".]
    [[color colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&r green:&g blue:&b alpha:NULL];
}
@end