VLCMainWindow.m 12.6 KB
Newer Older
1
2
3
4
5
/*****************************************************************************
 * VLCMainWindow.m: VLCMainWindow implementation
 *****************************************************************************
 * Copyright (C) 2007 Pierre d'Herbemont
 * Copyright (C) 2007 the VideoLAN team
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
6
 * $Id$
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#import "VLCMainWindow.h"
#import "ImageAndTextCell.h"
#import "VLCMediaArrayController.h"
#import "VLCBrowsableVideoView.h"
#import "VLCAppAdditions.h"

/******************************************************************************
 * VLCMainWindow (MasterViewDataSource)
 */
@implementation VLCMainWindow (MasterViewDataSource)
- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item
{
    return [[item representedObject] isKindOfClass:[NSDictionary class]];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
{
    return !([[item representedObject] isKindOfClass:[NSDictionary class]]);
}
- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
    [cell setRepresentedObject:[item representedObject]];
}
@end

/******************************************************************************
 * VLCMainWindow
 */
@implementation VLCMainWindow
- (void)awakeFromNib;
{
    NSTableColumn * tableColumn;
    
    /* This one will be removable, so we keep a reference of it so we can safely call removeFromSuperview */
    [navigatorView retain];    

    /* Check ib outlets */
    NSAssert( mainSplitView, @"No split view or wrong split view");
    NSAssert( fullScreenButton, @"No fullscreen button");

    /***********************************
     * Init the media player
     */
    mediaPlayer = [[VLCMediaPlayer alloc] initWithVideoView:videoView];

    /***********************************
     * MasterView OutlineView content
     */
    /* treeController */ 
    treeController = [[NSTreeController alloc] init];
    [treeController setContent:controller.arrayOfMasters];
  
    [treeController setChildrenKeyPath:@"childrenInMasterView"];
    //[treeController bind:@"contentArray" toObject:controller withKeyPath:@"arrayOfMasters" options:nil];

    /* Bind the "name" table column */
    tableColumn = [categoryList tableColumnWithIdentifier:@"name"];
	[tableColumn bind:@"value" toObject: treeController withKeyPath:@"arrangedObjects.descriptionInMasterView" options:nil];
    [tableColumn setEditable:YES];
    /* FIXME: this doesn't work obviously. */
	[tableColumn bind:@"editable" toObject: treeController withKeyPath:@"arrangedObjects.editableInMasterView" options:nil];

    /* Use an ImageAndTextCell in the "name" table column */
    ImageAndTextCell * cell = [[ImageAndTextCell alloc] init];
    [cell setFont:[[tableColumn dataCell] font]];
    [cell setImageKeyPath:@"image"];

    [tableColumn setDataCell:cell];

    /* Other setup */
    [categoryList setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleSourceList];
    [categoryList setDelegate:self];


    /***********************************
     * detailList setup
     */

    mediaArrayController = [[VLCMediaArrayController alloc] init];

    /* 1- Drag and drop */
    [detailList registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, NSURLPboardType, nil]];
    [detailList setDataSource:mediaArrayController];

    /* 2- Double click */
    [detailList setTarget:self];
    [detailList setDoubleAction:@selector(detailListItemDoubleClicked:)];

	/* 3- binding for "title" column */
    tableColumn = [detailList tableColumnWithIdentifier:@"title"];
	[tableColumn bind:@"value" toObject: mediaArrayController withKeyPath:@"arrangedObjects.metaDictionary.title" options:nil];

	/* 4- binding for "state" column */
    tableColumn = [detailList tableColumnWithIdentifier:@"state"];
	[tableColumn bind:@"value" toObject: mediaArrayController withKeyPath:@"arrangedObjects.stateAsImage" options:nil];

    /* 5- Search & Predicate */
    NSMutableDictionary * bindingOptions = [NSMutableDictionary dictionary];
    [bindingOptions setObject:@"metaDictionary.title contains[c] $value" forKey:NSPredicateFormatBindingOption];
    [bindingOptions setObject:@"No Title" forKey:NSDisplayNameBindingOption];
    [detailSearchField bind:@"predicate" toObject: mediaArrayController withKeyPath:@"filterPredicate" options:bindingOptions];
    
    /* 6- Bind the @"contentArray" and contentMediaList of the mediaArrayController */
    [mediaArrayController bind:@"contentArray" toObject:treeController withKeyPath:@"selection.childrenInMasterViewForDetailView.media" options:nil];
    [mediaArrayController bind:@"contentMediaList" toObject:treeController withKeyPath:@"selection.childrenInMasterViewForDetailView.parentMediaList" options:nil];
    
    /* 7- Aspect */
    [detailList setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleSourceList];
    [detailList setAllowsTypeSelect:YES];

    /***********************************
     * videoView setup
     */
    [videoView setItemsTree:controller.arrayOfVideoViewMasters];
    [videoView setNodeKeyPath:@"childrenInVideoView"];
    [videoView setContentKeyPath:@"descriptionInVideoView"];
    [videoView setTarget:self];
    [videoView setAction:@selector(videoViewItemClicked:)];
    

    /***********************************
     * Other interface element setup
     */

    [detailItemsCount bind:@"displayPatternValue1" toObject:mediaArrayController withKeyPath:@"arrangedObjects.@count" options:[NSDictionary dictionaryWithObject:@"%{value1}@ items" forKey:NSDisplayPatternBindingOption]];
    [detailItemFetchedStatus bind:@"animate" toObject:treeController withKeyPath:@"selection.currentlyFetchingItems" options:[NSDictionary dictionaryWithObject:@"%{value1}@ items" forKey:NSDisplayPatternBindingOption]];

    [fillScreenButton bind:@"value" toObject:videoView withKeyPath:@"fillScreen" options: [NSDictionary dictionaryWithObject:NSNegateBooleanTransformerName forKey:NSValueTransformerNameBindingOption]];
    [fullScreenButton bind:@"value" toObject:videoView withKeyPath:@"fullScreen" options: nil];
    [fullScreenButton bind:@"enabled" toObject:mediaPlayer withKeyPath:@"playing" options: nil];
    [fillScreenButton bind:@"enabled" toObject:mediaPlayer withKeyPath:@"playing" options: nil];

    [mediaReadingProgressSlider bind:@"enabled" toObject:mediaPlayer withKeyPath:@"media" options: [NSDictionary dictionaryWithObject:@"NonNilAsBoolTransformer" forKey:NSValueTransformerNameBindingOption]];
    [mediaReadingProgressSlider bind:@"enabled2" toObject:mediaPlayer withKeyPath:@"seekable" options: nil];

    [mediaReadingProgressSlider bind:@"value" toObject:mediaPlayer withKeyPath:@"position" options:
        [NSDictionary dictionaryWithObjectsAndKeys:@"Float10000FoldTransformer", NSValueTransformerNameBindingOption,
                                                  [NSNumber numberWithBool:NO], NSConditionallySetsEnabledBindingOption, nil ]];
    [mediaReadingProgressText bind:@"value" toObject:mediaPlayer withKeyPath:@"time.stringValue" options: nil];
    [mediaDescriptionText bind:@"value" toObject:mediaPlayer withKeyPath:@"description" options: nil];

    [navigatorViewToggleButton bind:@"value" toObject:self withKeyPath:@"navigatorViewVisible" options: nil];

    /* Playlist buttons */
    [removePlaylistButton bind:@"enabled" toObject:treeController withKeyPath:@"selection.editableInMasterView" options: nil];
    [removePlaylistButton setTarget:treeController];
    [removePlaylistButton setAction:@selector(remove:)];
    [addPlaylistButton setTarget:controller];
    [addPlaylistButton setAction:@selector(addPlaylist:)];

    [mainSplitView setDelegate:self];

    /* Last minute setup */
    [categoryList expandItem:nil expandChildren:YES];
    [categoryList selectRowIndexes:[NSIndexSet indexSetWithIndex:[categoryList numberOfRows] > 0 ? [categoryList numberOfRows]-1 : 0] byExtendingSelection:NO];
}

- (void)dealloc
{
    [navigatorView release];
    [mediaPlayer release];
    [treeController release];
    [mediaArrayController release];
    [super dealloc];
}

- (void)detailListItemDoubleClicked:(id)sender
{
    [mediaPlayer setMedia:[[mediaArrayController selectedObjects] objectAtIndex:0]];
    [mediaPlayer play];
}

- (void)videoViewItemClicked:(id)sender
{
    id object = [sender selectedObject];
    NSAssert( [object isKindOfClass:[VLCMedia class]], @"Object is not a VLCMedia" );

    [mediaPlayer setMedia:object];
    [mediaPlayer play];
}

- (BOOL)videoViewVisible
{
    NSAssert( mainSplitView && [[mainSplitView subviews] count] == 2, @"No split view or wrong split view");
    return  ([[[mainSplitView subviews] objectAtIndex:0] frame].size.height > 50.);
}

- (BOOL)navigatorViewVisible
{
    NSAssert( mainSplitView && [[mainSplitView subviews] count] == 2, @"No split view or wrong split view");
    return  ([[[mainSplitView subviews] objectAtIndex:1] frame].size.height > 6.);
}


- (void)setNavigatorViewVisible:(BOOL)visible
{
    NSAssert( mainSplitView && [[mainSplitView subviews] count] == 2, @"No split view or wrong split view");
    if(!([self navigatorViewVisible] ^ visible))
        return; /* Nothing to do */
    
    if(visible)
    {
        if( !navigatorHeight ) navigatorHeight = 100.f;
        if( ![self videoViewVisible] && ![self navigatorViewVisible] )
        {
            NSRect frame = [self frame];
            frame.origin.y -= navigatorHeight;
            frame.size.height += navigatorHeight;
            [[self animator] setFrame:frame display:YES];
        }
        else
            [[mainSplitView animator] setSliderPosition:([mainSplitView bounds].size.height - navigatorHeight - [mainSplitView dividerThickness])];
        /* Hack, because sliding cause some glitches */
        [navigatorView moveSubviewsToVisible];
    }
    else
    {
        navigatorHeight = [navigatorView bounds].size.height;
        NSRect frame0 = [self frame];
        NSRect frame1 = [[[mainSplitView subviews] objectAtIndex: 1] frame];
        frame0.size.height -= frame1.size.height;
        frame0.origin.y += frame1.size.height;
        frame1.size.height = 0;
        [[mainSplitView animator] setSliderPosition:([mainSplitView bounds].size.height)];
        /* Hack, because sliding cause some glitches */
        [navigatorView moveSubviewsToVisible];
    }
}
@end

@implementation VLCMainWindow (SplitViewDelegating)
- (CGFloat)splitView:(NSSplitView *)sender constrainSplitPosition:(CGFloat)proposedPosition ofSubviewAt:(NSInteger)offset
{
    CGFloat minHeight = 34.;

    /* Hack, because sliding cause some glitches */
    [navigatorView moveSubviewsToVisible];

    /* Get the bottom of the navigator view to get stuck at some points */
    if( [sender bounds].size.height - proposedPosition < minHeight*3./2. &&
        [sender bounds].size.height - proposedPosition >= minHeight/2 )
         return [sender bounds].size.height - minHeight;
    if( [sender bounds].size.height - proposedPosition < minHeight/2 )
         return [sender bounds].size.height;
    return proposedPosition;
}

- (void)splitView:(NSSplitView *)sender resizeSubviewsWithOldSize:(NSSize)oldSize
{
    [sender  adjustSubviews];

    /* Hack, because sliding cause some glitches */
    [navigatorView setFrame:[[navigatorView superview] bounds]];
    [navigatorView moveSubviewsToVisible];
}

- (void)splitViewWillResizeSubviews:(NSNotification *)aNotification
{
    /* Hack, because sliding cause some glitches */
    [navigatorView moveSubviewsToVisible];
    
    /* This could be changed from now on, so post a KVO notification */
    [self willChangeValueForKey:@"navigatorViewVisible"];
}
- (void)splitViewDidResizeSubviews:(NSNotification *)aNotification
{
    [self didChangeValueForKey:@"navigatorViewVisible"];
}
@end

@implementation VLCMainWindow (NSWindowDelegating)
- (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
{
    if( proposedFrameSize.height < 120.f)
        proposedFrameSize.height = [self minSize].height;
    return proposedFrameSize;
}
@end