1/* ProgressIndicator.m 2 * progress indicator 3 * 4 * Copyright (C) 2004 by vhf interservice GmbH 5 * Author: Georg Fleischmann 6 * 7 * created: 2004-07-07 8 * modified: 2004-08-21 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the vhf Public License as 12 * published by vhf interservice GmbH. Among other things, the 13 * License requires that the copyright notices and this notice 14 * be preserved on all copies. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 19 * See the vhf Public License for more details. 20 * 21 * You should have received a copy of the vhf Public License along 22 * with this program; see the file LICENSE. If not, write to vhf. 23 * 24 * vhf interservice GmbH, Im Marxle 3, 72119 Altingen, Germany 25 * eMail: info@vhf.de 26 * http://www.vhf.de 27 */ 28 29#include <AppKit/AppKit.h> 30#include <math.h> // floor() 31#include <VHFShared/vhfCompatibility.h> // PSWait() 32#include "ProgressIndicator.h" 33 34@implementation ProgressIndicator 35 36- initWithFrame:(NSRect)frameRect 37{ 38 [super initWithFrame:frameRect]; 39 displayCells = YES; 40 return self; 41} 42 43- (void)setDisplayText:(BOOL)flag { displayText = flag; } 44- (void)setDisplayCells:(BOOL)flag { displayCells = flag; } 45 46- (void)setPercentNumber:(NSNumber*)p 47{ 48 percent = [p floatValue]; 49 [self display]; 50 PSWait(); 51} 52- (void)setPercent:(float)p 53{ 54 percent = p; 55 [self setNeedsDisplay:YES]; 56} 57- (float)percent 58{ 59 return percent; 60} 61 62/* set the background title under the progress bar 63 */ 64- (void)setTitle:(NSString*)string 65{ 66 [title release]; 67 title = [string retain]; 68 [self display]; 69 PSWait(); 70} 71 72/* draw a growing bar from left to right 73 * The progress is indicated by colors from red to green 74 */ 75#define TEXT_WIDTH 35 // with of text ("100%") 76#define CELL_DIVISION 20 77- (void)draw 78{ static id sharedTextCell = nil; 79 NSRect bounds = [self bounds]; 80 NSRect insetRect = NSInsetRect(bounds, 1, 1), r; 81 int i; 82 float cellWidth, x; 83 84 if (!sharedTextCell) 85 { sharedTextCell = [[NSCell alloc] init]; 86 [sharedTextCell setWraps:NO]; 87 [sharedTextCell setFont:[NSFont systemFontOfSize:10.0]]; 88 } 89 90 [self lockFocus]; 91 92 /* color bar */ 93 for (x=1; x<percent*insetRect.size.width; x++) 94 { float h = x/(insetRect.size.width-1) * 1.0/3.0; // red = 0, green = 1/3 95 96 [[NSColor colorWithCalibratedHue:h saturation:0.7 brightness:1.0 alpha:1.0] set]; 97 r = NSMakeRect(x, insetRect.origin.y, 1.0, insetRect.size.height); 98 NSRectFill(r); 99 } 100 101 /* cell frames every 5% */ 102 if (displayCells) 103 { 104 cellWidth = insetRect.size.width / CELL_DIVISION; 105 [[NSColor darkGrayColor] set]; 106 for (i=0; i<=CELL_DIVISION; i++) 107 { 108 r = NSMakeRect(insetRect.origin.x, insetRect.origin.y, 109 floor(i*cellWidth+.5), insetRect.size.height); 110 NSFrameRect(r); 111 } 112 } 113 114 /* title */ 115 if (title) 116 { 117 r = NSMakeRect(bounds.origin.x, bounds.origin.y + bounds.size.height / 2.0 - 5.0, 118 bounds.size.width, 12); 119 [sharedTextCell setEnabled:YES]; 120 [sharedTextCell setAlignment:NSCenterTextAlignment]; 121 [sharedTextCell setStringValue:title]; 122 [sharedTextCell drawInteriorWithFrame:r inView:self]; 123 } 124 125 /* text "100%" */ 126 if (displayText) 127 { 128 r = NSMakeRect(bounds.origin.x + bounds.size.width - TEXT_WIDTH, 129 bounds.origin.y + bounds.size.height / 2.0 - 3.0, 130 TEXT_WIDTH, 10); 131 [sharedTextCell setEnabled:YES]; 132 [sharedTextCell setStringValue:[NSString stringWithFormat:@"%.0f%%", percent*100.0]]; 133 [sharedTextCell drawInteriorWithFrame:r inView:self]; 134 } 135 136 /* gray progress bar */ 137 //r = NSMakeRect(insetRect.origin.x, insetRect.origin.y, 138 // percent * insetRect.size.width, insetRect.size.height); 139 //[[NSColor blackColor] set]; 140 //NSDrawButton(rect, rect); 141 142 [self unlockFocus]; 143} 144- (void)setEnabled:(BOOL)flag 145{ 146 enabled = flag; 147 [self setNeedsDisplay:YES]; 148} 149 150- (void)drawRect:(NSRect)rect 151{ 152 [super drawRect:rect]; 153 NSDrawGrayBezel(rect, rect); 154 if (enabled) 155 [self draw]; 156} 157 158- (void)dealloc 159{ 160 [[NSNotificationCenter defaultCenter] removeObserver:self]; 161 [super dealloc]; 162} 163 164 165/* 166 * Notifications 167 */ 168 169/* update progress indicator 170 */ 171- (void)progress:(NSNotification*)notification 172{ NSDictionary *dict = [notification object]; 173 float p = [[dict objectForKey:@"percent"] floatValue]; 174 175 [self setPercent:p]; 176 //printf("%d%%\n", percent); 177} 178 179@end 180