| 1 | /* |
|---|
| 2 | * SapphireWaitDisplay.m |
|---|
| 3 | * Sapphire |
|---|
| 4 | * |
|---|
| 5 | * Created by Graham Booker on Feb. 11 2009. |
|---|
| 6 | * Copyright 2008 Sapphire Development Team and/or www.nanopi.net |
|---|
| 7 | * All rights reserved. |
|---|
| 8 | * |
|---|
| 9 | * This program is free software; you can redistribute it and/or modify it under the terms of the GNU |
|---|
| 10 | * General Public License as published by the Free Software Foundation; either version 3 of the License, |
|---|
| 11 | * or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even |
|---|
| 14 | * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|---|
| 15 | * Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License along with this program; if not, |
|---|
| 18 | * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #import "SapphireWaitDisplay.h" |
|---|
| 22 | #import <SapphireCompatClasses/SapphireFrontRowCompat.h> |
|---|
| 23 | #import "SapphireApplianceController.h" |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | @implementation SapphireWaitDisplay |
|---|
| 27 | |
|---|
| 28 | - (id) initWithScene:(BRRenderScene *)scene title:(NSString *)title invokation:(NSInvocation *)invokation |
|---|
| 29 | { |
|---|
| 30 | self = [super initWithScene:scene]; |
|---|
| 31 | if(self == nil) |
|---|
| 32 | return self; |
|---|
| 33 | |
|---|
| 34 | [self setListTitle:title]; |
|---|
| 35 | invoke = [invokation retain]; |
|---|
| 36 | [invoke retainArguments]; |
|---|
| 37 | |
|---|
| 38 | status = [SapphireFrontRowCompat newTextControlWithScene:scene]; |
|---|
| 39 | if([BRWaitSpinnerControl instancesRespondToSelector:@selector(initWithScene:)]) |
|---|
| 40 | spinner = [[BRWaitSpinnerControl alloc] initWithScene:scene]; |
|---|
| 41 | else |
|---|
| 42 | spinner = [[BRWaitSpinnerControl alloc] init]; |
|---|
| 43 | |
|---|
| 44 | if([SapphireFrontRowCompat usingLeopard]) |
|---|
| 45 | { |
|---|
| 46 | [spinner release]; |
|---|
| 47 | spinner = [[BRWaitSpinnerLayer alloc] init]; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | [self doMyLayout]; |
|---|
| 51 | |
|---|
| 52 | [self addControl:status]; |
|---|
| 53 | if([SapphireFrontRowCompat usingLeopard]) |
|---|
| 54 | [SapphireFrontRowCompat addSublayer:spinner toControl:self]; |
|---|
| 55 | else |
|---|
| 56 | [self addControl:spinner]; |
|---|
| 57 | |
|---|
| 58 | [SapphireLayoutManager setCustomLayoutOnControl:self]; |
|---|
| 59 | |
|---|
| 60 | return self; |
|---|
| 61 | |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | - (void)doMyLayout |
|---|
| 65 | { |
|---|
| 66 | NSRect frame = [SapphireFrontRowCompat frameOfController:self]; |
|---|
| 67 | frame.origin.y += frame.size.height / 2.0f; |
|---|
| 68 | frame.origin.x = frame.size.width / 2.0f; |
|---|
| 69 | frame.size.width = frame.size.height = frame.size.height / 6.0f; |
|---|
| 70 | [spinner setFrame:frame] ; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | - (void) dealloc |
|---|
| 74 | { |
|---|
| 75 | [spinner release]; |
|---|
| 76 | [status release]; |
|---|
| 77 | [invoke release]; |
|---|
| 78 | [super dealloc]; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | - (void)realCurrentStatus:(NSString *)stat; |
|---|
| 82 | { |
|---|
| 83 | if(stat == nil) |
|---|
| 84 | stat = @""; |
|---|
| 85 | [SapphireFrontRowCompat setText:stat withAtrributes:[SapphireFrontRowCompat paragraphTextAttributes] forControl:status]; |
|---|
| 86 | |
|---|
| 87 | NSRect master = [SapphireFrontRowCompat frameOfController:self]; |
|---|
| 88 | NSSize progressSize = [SapphireFrontRowCompat textControl:status renderedSizeWithMaxSize:NSMakeSize(master.size.width, master.size.height * 0.3f)]; |
|---|
| 89 | |
|---|
| 90 | NSRect frame; |
|---|
| 91 | frame.origin.x = (master.size.width) * 0.1f; |
|---|
| 92 | frame.origin.y = (master.size.height * 0.12f) + master.origin.y; |
|---|
| 93 | frame.size = progressSize; |
|---|
| 94 | [status setFrame:frame]; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | - (void)setCurrentStatus:(NSString *)stat; |
|---|
| 98 | { |
|---|
| 99 | if(runInThread) |
|---|
| 100 | [self performSelectorOnMainThread:@selector(realCurrentStatus:) withObject:stat waitUntilDone:YES]; |
|---|
| 101 | else |
|---|
| 102 | { |
|---|
| 103 | [self realCurrentStatus:stat]; |
|---|
| 104 | [[NSRunLoop currentRunLoop] runUntilDate:[[NSDate date] addTimeInterval:1]]; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | - (void)finished |
|---|
| 109 | { |
|---|
| 110 | BRControl *ret = nil; |
|---|
| 111 | [invoke getReturnValue:&ret]; |
|---|
| 112 | completed = YES; |
|---|
| 113 | |
|---|
| 114 | if(!noPopWhenComplete) |
|---|
| 115 | { |
|---|
| 116 | if(ret == nil) |
|---|
| 117 | [[self stack] popController]; |
|---|
| 118 | else |
|---|
| 119 | [[self stack] swapController:ret]; |
|---|
| 120 | } |
|---|
| 121 | else if(ret != nil) |
|---|
| 122 | [[self stack] pushController:ret]; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | - (void)start |
|---|
| 126 | { |
|---|
| 127 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|---|
| 128 | |
|---|
| 129 | [invoke invoke]; |
|---|
| 130 | |
|---|
| 131 | [self performSelectorOnMainThread:@selector(finished) withObject:nil waitUntilDone:YES]; |
|---|
| 132 | [pool drain]; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | - (void)wasPushed |
|---|
| 136 | { |
|---|
| 137 | [super wasPushed]; |
|---|
| 138 | [SapphireFrontRowCompat setSpinner:spinner toSpin:YES]; |
|---|
| 139 | if(runInThread) |
|---|
| 140 | [NSThread detachNewThreadSelector:@selector(start) toTarget:self withObject:nil]; |
|---|
| 141 | else |
|---|
| 142 | [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(start) userInfo:nil repeats:NO]; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | - (BOOL)brEventAction:(BREvent *)event |
|---|
| 146 | { |
|---|
| 147 | if(!completed) |
|---|
| 148 | return YES; |
|---|
| 149 | return [super brEventAction:event]; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | @end |
|---|