4f88d6d22a79dca16c227889b5ee72d7a25bdf2c
[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
24     playlist = [[VLCMediaList alloc] init];
25     [playlist addObserver:self forKeyPath:@"media" options:NSKeyValueObservingOptionNew context:nil];
26     
27     player = [[VLCMediaPlayer alloc] initWithVideoView:videoView];
28     mediaIndex = -1;
29
30     [playlistOutline registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, NSURLPboardType, nil]];
31     [playlistOutline setDoubleAction:@selector(changeAndPlay:)];
32 }
33
34 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
35 {
36 }
37
38 - (void)applicationWillTerminate:(NSNotification *)aNotification
39 {
40     [playlist removeObserver:self forKeyPath:@"media"];
41     
42     [player pause];
43     [player setMedia:nil];
44     [player release];
45     [playlist release];
46     [videoView release];
47 }
48
49 - (void)changeAndPlay:(id)sender
50 {
51     if ([playlistOutline selectedRow] != mediaIndex)
52     {
53                 [self setMediaIndex:[playlistOutline selectedRow]];
54                 if (![player isPlaying])
55                         [player play];
56     }
57 }
58
59 - (void)setMediaIndex:(int)value
60 {
61     if ([playlist count] <= 0)
62                 return;
63     
64     if (value < 0)
65                 value = 0;
66     if (value > [playlist count] - 1)
67                 value = [playlist count] - 1;
68     
69     mediaIndex = value;
70     [player setMedia:[playlist mediaAtIndex:mediaIndex]];
71 }
72
73 - (void)play:(id)sender
74 {
75     [self setMediaIndex:mediaIndex+1];
76     if (![player isPlaying])
77                 [player play];
78 }
79
80 - (void)pause:(id)sender
81 {
82     NSLog(@"Sending pause message to media player...");
83     [player pause];
84 }
85
86 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
87 {
88     if ([keyPath isEqualToString:@"media"] && object == playlist) {
89         [playlistOutline reloadData];
90     }
91 }
92
93 // NSTableView Implementation
94 - (int)numberOfRowsInTableView:(NSTableView *)tableView
95 {
96     return [playlist count];
97 }
98
99 - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn
100                         row:(int)row
101 {
102     return [(VLCMedia *)[playlist mediaAtIndex:row].metaDictionary valueForKey:VLCMetaInformationTitle];
103 }
104
105 - (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info 
106                                  proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)op
107 {
108     return NSDragOperationEvery; /* This is for now */
109 }
110
111 - (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
112                           row:(int)row dropOperation:(NSTableViewDropOperation)operation
113 {
114     int i;
115     NSArray *droppedItems = [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType];
116     
117     for (i = 0; i < [droppedItems count]; i++)
118     {
119         NSString * filename = [droppedItems objectAtIndex:i];
120                 VLCMedia * media = [VLCMedia mediaWithURL:[NSURL fileURLWithPath:filename]];
121                 NSLog(@"%@ length = %@", media, [media lengthWaitUntilDate:[NSDate dateWithTimeIntervalSinceNow:60]]);
122                 [playlist addMedia:media];
123     }
124     return YES;
125 }    
126
127 @end