File: GNRatingCell.m

package info (click to toggle)
grr.app 1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 3,948 kB
  • ctags: 118
  • sloc: objc: 4,019; sh: 72; makefile: 18
file content (173 lines) | stat: -rw-r--r-- 5,359 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
/*
   Grr RSS Reader
   
   Copyright (C) 2006, 2007 Guenther Noack <guenther@unix-ag.uni-kl.de>
   Copyright (C) 2009  GNUstep Application Team
                       Riccardo Mottola

   This application 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 3 of the License, or (at your option) any later version.
 
   This application 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
   Library General Public License for more details.
 
   You should have received a copy of the GNU General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA. 
*/

#import "GNRatingCell.h"

#ifdef __APPLE__
#import "GNUstep.h"
#endif

/**
 * This cell shows a rating displayed as 0 to 5 horizontally aligned stars. You can
 * modify the rating by clicking the desired position of the rightmost star. A click
 * left or right beyond the borders of the rectangle that would surround five stars
 * sets the number of stars to the maximum 5 or the minimum 0.
 * 
 * Revision History
 *   Nov 20 2006 - Initial version
 * 
 * FIXME: Why does NSTableView open a *text editing field* when you double click on this
 *        view in a table?!?
 */
@implementation GNRatingCell

// ----------------------------------------------------------------
//    initializers
// ----------------------------------------------------------------

-(id) initTextCell: (NSString*) text
{
    return [self initImageCell: [NSImage imageNamed: @"Star"]];
}

-(id) initImageCell: (NSImage*) image
{
    if ((self = [super initImageCell: image]) != nil) {
        ASSIGN(star, image);
    }
    
    return self;
}

// ----------------------------------------------------------------
//    mouse tracking
// ----------------------------------------------------------------

-(BOOL) trackMouse: (NSEvent*) theEvent
            inRect: (NSRect) cellFrame
            ofView: (NSView*) controlView
      untilMouseUp: (BOOL) flag
{
    // Just remember the rect of the cell, then go on with tracking
    // and wait for the call to stopTracking:at:inView:mouseIsUp:
    _currentTrackingRect = cellFrame;
    
    return [super trackMouse: theEvent
                      inRect: cellFrame
                      ofView: controlView
                untilMouseUp: flag];
}

- (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
{
    return YES;
}

-(void) stopTracking: (NSPoint)lastPoint
                  at: (NSPoint)stopPoint
              inView: (NSView*)aView
           mouseIsUp: (BOOL)isUpFlag
{
    if (isUpFlag) {
        NSSize size = [star size];
        
        // The field _currentTrackingRect is set by the trackMouse:inRect:ofView:untilMouseUp:
        // method before it calls the stopTracking:at:inView:mouseIsUp: (this) method. At the
        // moment I just trust nobody will call stopTracking:... manually.
        float leftestStarX = NSMidX(_currentTrackingRect) - size.width * 2.5;
        float rightestStarX = leftestStarX + size.width * 5.0;
        
        if (stopPoint.x <= leftestStarX) {
            [self setIntValue: 0];
        } else if (stopPoint.x <= rightestStarX) {
            float res = ((stopPoint.x - leftestStarX) / size.width) + 1.0;
            [self setFloatValue: res];
        } else {
            [self setIntValue: 5];
        }
    }
}


// ----------------------------------------------------------------
//    GNUstep workaround for buggy NSTableView behaviour
// ----------------------------------------------------------------
#ifdef GNUSTEP
/* 
 * According to Matt Rice, switching off editability for a cell
 * lets the mouse tracking still work. As long as the table column
 * is still editable, updating changed table cells works, too.
 * This is undefined behaviour, though, and may not work in future
 * releases of GNUstep.
 * 
 * This doesn't work for me on GNUstep stable in Nov 2006. It's
 * probably different for the SVN version.
 */
-(BOOL) isEditable {
    return NO;
}
#endif

// ----------------------------------------------------------------
//    drawing
// ----------------------------------------------------------------

-(void) drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView*)controlView
{
    NSSize size;
    NSPoint position;
    int i;
    int num;

    if (star == nil) {
        // init didn't take place, let's do something about it fast!
        ASSIGN(star, [NSImage imageNamed: @"Star"]);
    }
    
    size = [star size];
    
    position.x = MAX(NSMidX(cellFrame) - (size.width * 2.5),  0.0);
    position.y = MAX(NSMidY(cellFrame) - (size.height / 2.0), 0.0);
    
    if ([controlView isFlipped]) {
        position.y += size.height;
    }
    
    num = [self intValue];
    
    for (i=0; i<num; i++) {
        [star compositeToPoint: position
                     operation: NSCompositeSourceOver];
        
        position.x += size.width;
    }
    
    for (;i<5;i++) {
        [star dissolveToPoint: position
                     fraction: 0.2];
        
        position.x += size.width;
    }
}

@end