~ubuntu-branches/ubuntu/precise/vice/precise

« back to all changes in this revision

Viewing changes to src/arch/unix/macosx/cocoa/dialog/recordmediawindowcontroller.m

  • Committer: Bazaar Package Importer
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2009-03-31 00:37:15 UTC
  • mfrom: (1.1.7 upstream) (9.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090331003715-i5yisvcfv7mgz3eh
Tags: 2.1.dfsg-1
* New major upstream release (closes: #495937).
* Add desktop files (closes: #501181).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * recordmediawindowcontroller.m - record media
 
3
 *
 
4
 * Written by
 
5
 *  Christian Vogelgsang <chris@vogelgsang.org>
 
6
 *
 
7
 * This file is part of VICE, the Versatile Commodore Emulator.
 
8
 * See README for copyright notice.
 
9
 *
 
10
 *  This program is free software; you can redistribute it and/or modify
 
11
 *  it under the terms of the GNU General Public License as published by
 
12
 *  the Free Software Foundation; either version 2 of the License, or
 
13
 *  (at your option) any later version.
 
14
 *
 
15
 *  This program is distributed in the hope that it will be useful,
 
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 *  GNU General Public License for more details.
 
19
 *
 
20
 *  You should have received a copy of the GNU General Public License
 
21
 *  along with this program; if not, write to the Free Software
 
22
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 
23
 *  02111-1307  USA.
 
24
 *
 
25
 */
 
26
 
 
27
#import "recordmediawindowcontroller.h"
 
28
#import "viceapplication.h"
 
29
#import "vicenotifications.h"
 
30
 
 
31
@implementation RecordMediaWindowController
 
32
 
 
33
-(id)init
 
34
{
 
35
    self = [super initWithWindowNibName:@"RecordMedia"];
 
36
    [self registerForResourceUpdate:@selector(updateResources:)];
 
37
    return self;
 
38
}
 
39
 
 
40
-(void)dealloc
 
41
{
 
42
    [currentMediaType release];
 
43
    [currentMediaFormats release];
 
44
    [super dealloc];
 
45
}
 
46
 
 
47
-(void)windowDidLoad
 
48
{
 
49
    [mediaFileName setStringValue:@"capture"];
 
50
    [stopRecord setEnabled:FALSE];
 
51
 
 
52
    [self setupMediaType];
 
53
    [self updateMediaType];
 
54
    [self updateResources:nil];
 
55
 
 
56
    [super windowDidLoad];
 
57
}
 
58
 
 
59
-(void)updateAudioBitrate
 
60
{
 
61
    NSString *res = [NSString stringWithFormat:@"%@AudioBitrate",currentMediaType];
 
62
    int value = [self getIntResource:res];
 
63
    [audioBitrate setIntValue:value];
 
64
}
 
65
 
 
66
-(void)updateVideoBitrate
 
67
{
 
68
    NSString *res = [NSString stringWithFormat:@"%@VideoBitrate",currentMediaType];
 
69
    int value = [self getIntResource:res];
 
70
    [videoBitrate setIntValue:value];    
 
71
}
 
72
 
 
73
-(void)updateVideoFormat
 
74
{        
 
75
    // get current video format
 
76
    NSString *res = [NSString stringWithFormat:@"%@VideoCodec",currentMediaType];
 
77
    int codecId = [self getIntResource:res];
 
78
    NSString *codecName = (NSString *)[currentVideoFormats objectForKey:[NSNumber numberWithInt:codecId]];
 
79
    if(codecName!=nil) {
 
80
        [videoFormat selectItemWithTitle:codecName];
 
81
    }
 
82
}
 
83
 
 
84
-(void)updateAudioFormat
 
85
{
 
86
    // get current video format
 
87
    NSString *res = [NSString stringWithFormat:@"%@AudioCodec",currentMediaType];
 
88
    int codecId = [self getIntResource:res];
 
89
    NSString *codecName = (NSString *)[currentAudioFormats objectForKey:[NSNumber numberWithInt:codecId]];
 
90
    if(codecName!=nil) {
 
91
        [audioFormat selectItemWithTitle:codecName];
 
92
    }
 
93
}
 
94
 
 
95
-(void)replaceFileNameExtension:(BOOL)force
 
96
{
 
97
    NSString *fileName = [mediaFileName stringValue];
 
98
 
 
99
    // if not forced only replace if there is no extension
 
100
    BOOL doit = force;
 
101
    if(!doit) {
 
102
        doit = [[fileName pathExtension] length] == 0;
 
103
    }
 
104
    
 
105
    if(doit) {
 
106
        fileName = [fileName stringByDeletingPathExtension];
 
107
        NSString *result = [NSString stringWithFormat:@"%@.%@",fileName,currentDefaultExtension];
 
108
        [mediaFileName setStringValue:result];
 
109
    }
 
110
}
 
111
 
 
112
-(void)fetchDefaultExtension
 
113
{
 
114
    [currentDefaultExtension release];
 
115
    currentDefaultExtension = [[VICEApplication theMachineController] 
 
116
        defaultExtensionForMediaDriver:currentMediaType
 
117
                             andFormat:currentMediaFormat];
 
118
    [currentDefaultExtension retain];
 
119
    
 
120
    [self replaceFileNameExtension:TRUE];
 
121
}
 
122
 
 
123
-(void)updateMediaFormat
 
124
{
 
125
    if(!currentMediaTypeHasOptions)
 
126
        return;
 
127
    
 
128
    // get current format
 
129
    NSString *res = [NSString stringWithFormat:@"%@Format",currentMediaType];
 
130
    [currentMediaFormat release];
 
131
    currentMediaFormat = [self getStringResource:res];
 
132
    [currentMediaFormat retain];
 
133
 
 
134
    [self fetchDefaultExtension];
 
135
    
 
136
    // set media format
 
137
    [mediaFormat selectItemWithTitle:currentMediaFormat];
 
138
 
 
139
    // find media format in description and setup audio and video codecs
 
140
    int i;
 
141
    for(i=0;i<[currentMediaFormats count];i++) {
 
142
        NSArray *fa = (NSArray *)[currentMediaFormats objectAtIndex:i];
 
143
        NSString *formatName = (NSString *)[fa objectAtIndex:0];
 
144
 
 
145
        // set audio and video codecs for current format
 
146
        if([currentMediaFormat compare:formatName]==NSOrderedSame) {
 
147
            currentVideoFormats = (NSDictionary *)[fa objectAtIndex:1];
 
148
            currentAudioFormats = (NSDictionary *)[fa objectAtIndex:2];
 
149
 
 
150
            [videoFormat removeAllItems];
 
151
            [videoFormat addItemsWithTitles:[currentVideoFormats allValues]];
 
152
 
 
153
            [audioFormat removeAllItems];
 
154
            [audioFormat addItemsWithTitles:[currentAudioFormats allValues]];
 
155
            
 
156
            BOOL hasVideoFormats = [currentVideoFormats count] > 0;
 
157
            BOOL hasAudioFormats = [currentAudioFormats count] > 0;
 
158
            
 
159
            [videoFormat  setEnabled:hasVideoFormats];
 
160
            [videoBitrate setEnabled:hasVideoFormats];
 
161
            [audioFormat  setEnabled:hasAudioFormats];
 
162
            [audioBitrate setEnabled:hasAudioFormats];
 
163
            
 
164
            break;
 
165
        }
 
166
    }
 
167
    
 
168
    // set the correct format
 
169
    [self updateAudioFormat];
 
170
    [self updateVideoFormat];
 
171
    [self updateAudioBitrate];
 
172
    [self updateVideoBitrate];
 
173
}
 
174
 
 
175
-(void)updateMediaType
 
176
{
 
177
    // fetch media type from pop up button    
 
178
    [currentMediaType release];
 
179
    currentMediaType = [[mediaType selectedItem] title];
 
180
    [currentMediaType retain];
 
181
    
 
182
    // ask the machine controller if the media type has options
 
183
    currentMediaTypeHasOptions = [[VICEApplication theMachineController] mediaDriverHasOptions:currentMediaType];
 
184
    BOOL hasOptions = currentMediaTypeHasOptions;
 
185
 
 
186
    // enable/disable options
 
187
    [mediaFormat setEnabled:hasOptions];
 
188
    [videoFormat setEnabled:hasOptions];
 
189
    [videoBitrate setEnabled:hasOptions];
 
190
    [audioFormat setEnabled:hasOptions];
 
191
    [audioBitrate setEnabled:hasOptions];
 
192
    
 
193
    if(hasOptions) {
 
194
        // fetch all formats and video/audio codecs from machine controller
 
195
        [currentMediaFormats release];
 
196
        currentMediaFormats = [[VICEApplication theMachineController] enumMediaFormats:currentMediaType];
 
197
        [currentMediaFormats retain];
 
198
                
 
199
        // fill in formats
 
200
        [mediaFormat removeAllItems];
 
201
        int i;
 
202
        for(i=0;i<[currentMediaFormats count];i++) {
 
203
            NSArray *fa = (NSArray *)[currentMediaFormats objectAtIndex:i];
 
204
            NSString *formatName = (NSString *)[fa objectAtIndex:0];
 
205
            [mediaFormat addItemWithTitle:formatName];
 
206
        }
 
207
        
 
208
        // set current format and setup audio video codecs
 
209
        [self updateMediaFormat];
 
210
    } else {
 
211
        [currentMediaFormat release];
 
212
        currentMediaFormat = nil;
 
213
        [self fetchDefaultExtension];
 
214
    }
 
215
}
 
216
 
 
217
-(void)setupMediaType
 
218
{
 
219
    [mediaType removeAllItems];
 
220
    [mediaType addItemsWithTitles:[[VICEApplication theMachineController] enumMediaDrivers]];
 
221
}
 
222
 
 
223
-(void)updateResources:(NSNotification *)notification
 
224
{
 
225
    [self updateMediaFormat];
 
226
}
 
227
 
 
228
// ----- Actions -----
 
229
 
 
230
-(IBAction)startRecording:(id)sender
 
231
{
 
232
    int canvasId = [VICEApplication currentCanvasId];
 
233
    NSString *fileName = [mediaFileName stringValue];
 
234
    NSString *driver = [[mediaType selectedItem] title];
 
235
    
 
236
    BOOL ok = [[VICEApplication theMachineController] startRecordingMedia:driver
 
237
                                                               fromCanvas:canvasId
 
238
                                                                   toFile:fileName];
 
239
    if(!ok) {
 
240
        [VICEApplication runErrorMessage:@"Can't start media recording!"];
 
241
    } else {
 
242
        // still recording? enable stop button
 
243
        if([[VICEApplication theMachineController] isRecordingMedia]) {
 
244
            [stopRecord setEnabled:TRUE];
 
245
            [startRecord setEnabled:FALSE];
 
246
            [mediaFileName setEnabled:FALSE];
 
247
            [mediaFileNamePicker setEnabled:FALSE];
 
248
        }
 
249
    }
 
250
}
 
251
 
 
252
-(IBAction)stopRecording:(id)sender
 
253
{
 
254
    [[VICEApplication theMachineController] stopRecordingMedia];
 
255
    
 
256
    [stopRecord setEnabled:FALSE];
 
257
    [startRecord setEnabled:TRUE];
 
258
    [mediaFileName setEnabled:TRUE];
 
259
    [mediaFileNamePicker setEnabled:TRUE];
 
260
}
 
261
 
 
262
-(IBAction)changedMediaFileName:(id)sender
 
263
{
 
264
    [self replaceFileNameExtension:FALSE];
 
265
}
 
266
 
 
267
-(IBAction)pickMediaFileName:(id)sender
 
268
{
 
269
    NSString *title = [NSString stringWithFormat:@"Record %@ Media",currentDefaultExtension];
 
270
    NSString *fileName = [self pickSaveFileWithTitle:title types:[NSArray arrayWithObjects:currentDefaultExtension,nil]];
 
271
    if(fileName!=nil) {
 
272
        [mediaFileName setStringValue:fileName];
 
273
    }
 
274
}
 
275
 
 
276
-(IBAction)changedMediaType:(id)sender
 
277
{
 
278
    [self updateMediaType];
 
279
}
 
280
 
 
281
-(IBAction)changedMediaFormat:(id)sender
 
282
{
 
283
    NSString *res = [NSString stringWithFormat:@"%@Format",currentMediaType];
 
284
    [self setStringResource:res toValue:[mediaFormat titleOfSelectedItem]];
 
285
    [self updateMediaFormat];
 
286
}
 
287
 
 
288
-(IBAction)changedAudioFormat:(id)sender
 
289
{
 
290
    NSString *curFormat = [audioFormat titleOfSelectedItem];
 
291
    NSArray *keys = [currentAudioFormats allKeysForObject:curFormat];
 
292
    if([keys count]==1) {
 
293
        NSString *res = [NSString stringWithFormat:@"%@AudioCodec",currentMediaType];
 
294
        int value = [(NSNumber *)[keys objectAtIndex:0] intValue];
 
295
        [self setIntResource:res toValue:value];
 
296
        [self updateAudioFormat];
 
297
    } 
 
298
}
 
299
 
 
300
-(IBAction)changedAudioBitrate:(id)sender
 
301
{
 
302
    NSString *res = [NSString stringWithFormat:@"%@AudioBitrate",currentMediaType];
 
303
    int value = [audioBitrate intValue];
 
304
    [self setIntResource:res toValue:value];
 
305
    [self updateAudioBitrate];
 
306
}
 
307
 
 
308
-(IBAction)changedVideoFormat:(id)sender
 
309
{
 
310
    NSString *curFormat = [videoFormat titleOfSelectedItem];
 
311
    NSArray *keys = [currentVideoFormats allKeysForObject:curFormat];
 
312
    if([keys count]==1) {
 
313
        NSString *res = [NSString stringWithFormat:@"%@VideoCodec",currentMediaType];
 
314
        int value = [(NSNumber *)[keys objectAtIndex:0] intValue];
 
315
        [self setIntResource:res toValue:value];
 
316
        [self updateVideoFormat];
 
317
    } 
 
318
}
 
319
 
 
320
-(IBAction)changedVideoBitrate:(id)sender
 
321
{
 
322
    NSString *res = [NSString stringWithFormat:@"%@VideoBitrate",currentMediaType];
 
323
    int value = [videoBitrate intValue];
 
324
    [self setIntResource:res toValue:value];
 
325
    [self updateVideoBitrate];
 
326
}
 
327
 
 
328
@end