MacOSX/Framework/Examples: Updatee example to properly scale video within window.
[vlc.git] / extras / MacOSX / Framework / Examples / test / Controller.m
1 #import "Controller.h"
2
3 static void *sleepForMe(void)
4 {
5     while (1) sleep(60);
6 }
7
8 @implementation Controller
9
10 - (void)awakeFromNib
11 {
12 //    atexit((void*)sleepForMe);    // Only used for memory leak debugging
13
14     [NSApp setDelegate:self];
15
16     // Allocate a VLCVideoView instance and tell it what area to occupy.
17     NSRect rect = NSMakeRect(0, 0, 0, 0);
18     rect.size = [videoHolderView frame].size;
19     
20     videoView = [[VLCVideoView alloc] initWithFrame:rect];
21     [videoHolderView addSubview:videoView];
22     [videoView setAutoresizingMask: NSViewHeightSizable|NSViewWidthSizable];
23     videoView.fillScreen = YES;
24     
25     playlist = [[VLCMediaList alloc] init];
26     [playlist addObserver:self forKeyPath:@"media" options:NSKeyValueObservingOptionNew context:nil];
27     
28     player = [[VLCMediaPlayer alloc] initWithVideoView:videoView];
29     mediaIndex = -1;
30
31     [playlistOutline registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, NSURLPboardType, nil]];
32     [playlistOutline setDoubleAction:@selector(changeAndPlay:)];
33 }
34
35 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
36 {
37 }
38
39 - (void)applicationWillTerminate:(NSNotification *)aNotification
40 {
41     [playlist removeObserver:self forKeyPath:@"media"];
42     
43     [player pause];
44     [player setMedia:nil];
45     [player release];
46     [playlist release];
47     [videoView release];
48 }
49
50 - (void)changeAndPlay:(id)sender
51 {
52     if ([playlistOutline selectedRow] != mediaIndex)
53     {
54                 [self setMediaIndex:[playlistOutline selectedRow]];
55                 if (![player isPlaying])
56                         [player play];
57     }
58 }
59
60 - (void)setMediaIndex:(int)value
61 {
62     if ([playlist count] <= 0)
63                 return;
64     
65     if (value < 0)
66                 value = 0;
67     if (value > [playlist count] - 1)
68                 value = [playlist count] - 1;
69     
70     mediaIndex = value;
71     [player setMedia:[playlist mediaAtIndex:mediaIndex]];
72 }
73
74 - (void)play:(id)sender
75 {
76     [self setMediaIndex:mediaIndex+1];
77     if (![player isPlaying])
78                 [player play];
79 }
80
81 - (void)pause:(id)sender
82 {
83     NSLog(@"Sending pause message to media player...");
84     [player pause];
85 }
86
87 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
88 {
89     if ([keyPath isEqualToString:@"media"] && object == playlist) {
90         [playlistOutline reloadData];
91     }
92 }
93
94 // NSTableView Implementation
95 - (int)numberOfRowsInTableView:(NSTableView *)tableView
96 {
97     return [playlist count];
98 }
99
100 - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn
101                         row:(int)row
102 {
103     return [(VLCMedia *)[playlist mediaAtIndex:row].metaDictionary valueForKey:VLCMetaInformationTitle];
104 }
105
106 - (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info 
107                                  proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)op
108 {
109     return NSDragOperationEvery; /* This is for now */
110 }
111
112 - (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
113                           row:(int)row dropOperation:(NSTableViewDropOperation)operation
114 {
115     int i;
116     NSArray *droppedItems = [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType];
117     
118     for (i = 0; i < [droppedItems count]; i++)
119     {
120         NSString * filename = [droppedItems objectAtIndex:i];
121                 VLCMedia * media = [VLCMedia mediaWithURL:[NSURL fileURLWithPath:filename]];
122                 NSLog(@"%@ length = %@", media, [media lengthWaitUntilDate:[NSDate dateWithTimeIntervalSinceNow:60]]);
123                 [playlist addMedia:media];
124     }
125     return YES;
126 }    
127
128 @end