]> git.sesse.net Git - vlc/blob - extras/MacOSX/Framework/Examples/test/Controller.m
2c669a87cb50b44201e6e9ce379a9a5c11e0a306
[vlc] / 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     {
79                 NSLog(@"%@ length = %@", [playlist mediaAtIndex:mediaIndex], [[playlist mediaAtIndex:mediaIndex] lengthWaitUntilDate:[NSDate dateWithTimeIntervalSinceNow:60]]);
80                 [player play];
81     }
82 }
83
84 - (void)pause:(id)sender
85 {
86     NSLog(@"Sending pause message to media player...");
87     [player pause];
88 }
89
90 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
91 {
92     if ([keyPath isEqualToString:@"media"] && object == playlist) {
93         [playlistOutline reloadData];
94     }
95 }
96
97 // NSTableView Implementation
98 - (int)numberOfRowsInTableView:(NSTableView *)tableView
99 {
100     return [playlist count];
101 }
102
103 - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn
104                         row:(int)row
105 {
106     return [(VLCMedia *)[playlist mediaAtIndex:row].metaDictionary valueForKey:VLCMetaInformationTitle];
107 }
108
109 - (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info 
110                                  proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)op
111 {
112     return NSDragOperationEvery; /* This is for now */
113 }
114
115 - (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
116                           row:(int)row dropOperation:(NSTableViewDropOperation)operation
117 {
118     int i;
119     NSArray *droppedItems = [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType];
120     
121     for (i = 0; i < [droppedItems count]; i++)
122     {
123         NSString * filename = [droppedItems objectAtIndex:i];
124                 VLCMedia * media = [VLCMedia mediaWithURL:[NSURL fileURLWithPath:filename]];
125                 [playlist addMedia:media];
126     }
127     return YES;
128 }    
129
130 @end