]> git.sesse.net Git - vlc/blob - modules/gui/macosx/sidebarview.m
macosx: merge Eric Dudiak's code from GSoC 2008
[vlc] / modules / gui / macosx / sidebarview.m
1 /*****************************************************************************
2  * sidebarview.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2005-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Eric Dudiak <dudiak at gmail dot com>
8  *          Colloquy <http://colloquy.info/>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #import "sidebarview.h"
26 #import "intf.h"
27 #import "playlist.h"
28
29 @implementation sidebarview
30 - (void) resetCursorRects 
31 {
32         if( ! [self isPaneSplitter] )
33                 [super resetCursorRects];
34 }
35
36 - (id) initWithCoder:(NSCoder *) decoder {
37         if( ( self = [super initWithCoder:decoder] ) )
38                 _mainSubviewIndex = 1;
39         return self;
40 }
41
42 - (float) dividerThickness
43 {
44         return 1.0;
45 }
46
47 - (BOOL) isVertical
48 {
49     return YES;
50 }
51
52 - (void) drawDividerInRect:(NSRect) rect
53 {
54         [[NSColor colorWithCalibratedWhite:0.65 alpha:1.] set];
55         NSRectFill( rect );
56 }
57
58 - (void) adjustSubviews
59 {
60         if( _mainSubviewIndex == -1 || [[self subviews] count] != 2 ) {
61                 [super adjustSubviews];
62                 return;
63         }
64     
65         float dividerThickness = [self dividerThickness];
66         NSRect newFrame = [self frame];
67     
68         NSView *mainView = [[self subviews] objectAtIndex:_mainSubviewIndex];
69         NSView *otherView = ( _mainSubviewIndex ? [[self subviews] objectAtIndex:0] : [[self subviews] objectAtIndex:1] );
70     
71         NSRect mainFrame = [mainView frame];
72         NSRect otherFrame = [otherView frame];
73     
74
75         mainFrame.size.width = NSWidth( newFrame ) - dividerThickness - NSWidth( otherFrame );
76         mainFrame.size.height = NSHeight( newFrame );
77         mainFrame.origin.x = ( _mainSubviewIndex ? NSWidth( otherFrame ) + dividerThickness : 0. );
78         mainFrame.origin.y = 0.;
79
80         otherFrame.size.width = NSWidth( otherFrame );
81         otherFrame.size.height = NSHeight( newFrame );
82         otherFrame.origin.x = ( _mainSubviewIndex ? 0. : NSWidth( mainFrame ) + dividerThickness );
83         otherFrame.origin.y = 0.;
84     
85         [mainView setFrame:mainFrame];
86         [otherView setFrame:otherFrame];
87     
88         [self setNeedsDisplay:YES];
89 }
90 @end
91
92 /*****************************************************************************
93  * VLCPlaylist implementation
94  *****************************************************************************/
95 @implementation VLCSidebar
96
97 - (void)awakeFromNib
98 {
99     [o_outline_view setTarget: self];
100     [o_outline_view setDelegate: self];
101     [o_outline_view setDataSource: self];
102     [o_outline_view setAllowsEmptySelection: NO];
103 }
104
105 - (NSOutlineView *)outlineView
106 {
107     return o_outline_view;
108 }
109
110 - (void)outlineView:(NSOutlineView *)outlineView
111     willDisplayCell:(id)cell
112      forTableColumn:(NSTableColumn *)tableColumn
113                item:(id)item
114 {
115     if ( ![outlineView isExpandable:item] )
116     {
117         [cell setFont: [NSFont systemFontOfSize: 12]];
118         [cell setTextColor:[NSColor blackColor]];
119     }
120     else
121     {
122         [cell setFont: [NSFont boldSystemFontOfSize: 10]];
123         [cell setTextColor:[NSColor colorWithCalibratedWhite:0.365 alpha:1.0]];
124     }
125 }
126
127 - (void)updateSidebar:(id)item
128 {
129     int i_row = -1;
130     [o_outline_view reloadData];
131     i_row = [o_outline_view rowForItem:item];
132     if( i_row > -1 )
133     {
134         [o_outline_view selectRow:i_row byExtendingSelection: NO];
135         [o_outline_view scrollRowToVisible: i_row];
136     }
137 }
138
139 - (CGFloat)outlineView:(NSOutlineView *)outlineView heightOfRowByItem:(id)item
140 {
141     if( [outlineView isExpandable:item] )
142         return 12.;
143     else
144         return 20.;
145 }
146
147 - (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
148 {
149     if( [outlineView isExpandable:item] )
150         return NO;
151     else
152     {
153         if( ![[o_playlist playingItem] isEqual: item] )
154             [o_playlist playSidebarItem:item];
155         return YES;
156     }
157 }
158
159 - (void)outlineViewItemDidExpand:(NSNotification *)notification
160 {
161     int i_row = -1;
162     i_row = [o_outline_view rowForItem:[o_playlist playingItem]];
163     if( i_row > -1 )
164     {
165         [o_outline_view selectRow:i_row byExtendingSelection: NO];
166         [o_outline_view scrollRowToVisible: i_row];
167     }
168 }
169
170 @end
171
172 @implementation VLCSidebar (NSOutlineViewDataSource)
173
174 /* return the number of children for Obj-C pointer item */ /* DONE */
175 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
176 {
177     return [o_playlist outlineView:outlineView numberOfChildrenOfItem:item];
178 }
179
180 /* return the child at index for the Obj-C pointer item */ /* DONE */
181 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
182 {
183     return [o_playlist outlineView:outlineView child:index ofItem:item];
184 }
185
186 /* is the item expandable */
187 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
188 {
189     return [o_playlist outlineView:outlineView isItemExpandable:item];
190 }
191
192 /* retrieve the string values for the cells */
193 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)o_tc byItem:(id)item
194 {
195     if( [outlineView isExpandable:item] )
196         return [[o_playlist outlineView:outlineView objectValueForTableColumn:o_tc byItem:item] uppercaseString];
197     else
198         return [o_playlist outlineView:outlineView objectValueForTableColumn:o_tc byItem:item];
199 }
200
201 @end