-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathZXReportTextView.m
More file actions
126 lines (106 loc) · 3.78 KB
/
ZXReportTextView.m
File metadata and controls
126 lines (106 loc) · 3.78 KB
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
/*
* Name: ZXReportTextView.m
* Project: Strongbox
* Created on: 2008-07-04
*
* Copyright (C) 2008 Pierre-Hans Corcoran
*
* --------------------------------------------------------------------------
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License (version 2) as published
* by the Free Software Foundation. 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., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* --------------------------------------------------------------------------
*/
#import "ZXReportTextView.h"
#import "ZXReportSection.h"
enum {
ZXMoneyReportResult = 0,
ZXPercentReportResult = 1,
};
@interface ZXReportTextView (Private)
- (void)clearAllSubviews;
- (NSAttributedString *)attributedStringForSection:(ZXReportSection *)section;
@end
@implementation ZXReportTextView
@synthesize lastWidthModification, reportResultControl;
- (id)initWithFrame:(NSRect)frameRect
{
if ((self = [super initWithFrame:frameRect]) != nil) {
self.lastWidthModification = [NSNumber numberWithInt:0];
}
return self;
}
- (void)drawRect:(NSRect)frame
{
[self updateView:self];
[super drawRect:frame];
}
- (IBAction)updateView:(id)sender
{
[self clearAllSubviews];
int count = 1;
for(ZXReportSection *section in allSections) {
// FIXME: Magic rect
NSRect r = NSMakeRect(0, 0, 600, 20);
NSTextField *text = [[NSTextField alloc] initWithFrame:r];
[text setBordered:NO];
[text setEditable:NO];
[text setSelectable:NO];
[text setDrawsBackground:NO];
[text setAttributedStringValue:[self attributedStringForSection:section]];
[text sizeToFit];
NSRect frame = [text frame];
// Position to top of frame, below last added entry.
frame.origin.y = [self frame].size.height - frame.size.height * count++;
// Right align
frame.origin.x = [self frame].size.width - [text frame].size.width;
[text setFrame:frame];
[self addSubview:text];
[text setAutoresizingMask:NSViewMinYMargin | NSViewMinXMargin];
// If allowed space is smaller than required.
if ([self frame].size.width < [text frame].size.width) {
float difference = [text frame].size.width - [self frame].size.width;
frame = [self frame];
frame.size.width += difference;
frame.origin.x -= difference;
[self setFrame:frame];
self.lastWidthModification = [NSNumber numberWithFloat:difference];
}
[text release];
}
}
- (void)clearAllSubviews
{
while ([[self subviews] count] > 0) {
[[[self subviews] objectAtIndex:0] removeFromSuperview];
}
}
- (void)removeAllSections
{
[super removeAllSections];
[self clearAllSubviews];
}
- (NSAttributedString *)attributedStringForSection:(ZXReportSection *)section
{
id amount;
if([self.reportResultControl selectedSegment] == ZXMoneyReportResult) {
amount = [currencyFormatter stringFromNumber:section.amount];
} else {
double totalAmount = [[self valueForKeyPath:@"allSections.@sum.amount"] doubleValue];
amount = [NSNumber numberWithDouble:[section fractionForTotal:totalAmount] * 100.0];
amount = [percentFormatter stringFromNumber:amount];
}
// FIXME: Hard-coded string
NSString *content = [NSString stringWithFormat:@"%@: %@", section.name, amount];
id attr = [NSDictionary dictionaryWithObjectsAndKeys:section.color, @"NSColor", nil];
id string = [[NSAttributedString alloc] initWithString:content
attributes:attr];
return [string autorelease];
}
@end