1 /*****************************************************************************
2 * VLCUIWidgets.m: Widgets for VLC's extensions dialogs for Mac OS X
3 *****************************************************************************
4 * Copyright (C) 2009-2012 the VideoLAN team and authors
7 * Authors: Pierre d'Herbemont <pdherbemont # videolan dot>,
8 * Brendon Justin <brendonjustin@gmail.com>
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.
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.
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 *****************************************************************************/
25 #import "VLCUIWidgets.h"
29 at implementation VLCDialogButton
34 at implementation VLCDialogPopUpButton
39 at implementation VLCDialogTextField
44 at implementation VLCDialogWindow
46 at synthesize has_lock;
50 at implementation VLCDialogList
52 at synthesize contentArray;
54 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
56 return [contentArray count];
59 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
61 return [[contentArray objectAtIndex:rowIndex] objectForKey:@"text"];
66 at implementation VLCDialogGridView
68 - (NSUInteger)numViews
70 return [_griddedViews count];
75 if ((self = [super init]))
79 _griddedViews = [[NSMutableArray alloc] init];
86 [_griddedViews release];
90 - (void)recomputeCount
94 for (NSDictionary *obj in _griddedViews)
96 NSUInteger row = [[obj objectForKey:@"row"] intValue];
97 NSUInteger col = [[obj objectForKey:@"col"] intValue];
98 if (col + 1 > _colCount)
100 if (row + 1 > _rowCount)
105 - (void)recomputeWindowSize
107 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(recomputeWindowSize) object:nil];
109 NSWindow *window = [self window];
110 NSRect frame = [window frame];
111 NSRect contentRect = [window contentRectForFrameRect:frame];
112 contentRect.size = [self flexSize:frame.size];
113 NSRect newFrame = [window frameRectForContentRect:contentRect];
114 newFrame.origin.y -= newFrame.size.height - frame.size.height;
115 newFrame.origin.x -= (newFrame.size.width - frame.size.width) / 2;
116 [window setFrame:newFrame display:YES animate:YES];
119 - (NSSize)objectSizeToFit:(NSView *)view
121 if ([view isKindOfClass:[NSControl class]]) {
122 NSControl *control = (NSControl *)view;
123 return [[control cell] cellSize];
125 return [view frame].size;
137 - (CGFloat)constrainedHeightOfRow:(NSUInteger)targetRow
140 for(NSDictionary *obj in _griddedViews) {
141 NSUInteger row = [[obj objectForKey:@"row"] intValue];
142 if (row != targetRow)
144 NSUInteger rowSpan = [[obj objectForKey:@"rowSpan"] intValue];
147 NSView *view = [obj objectForKey:@"view"];
148 if ([view autoresizingMask] & NSViewHeightSizable)
150 NSSize sizeToFit = [self objectSizeToFit:view];
151 if (height < sizeToFit.height)
152 height = sizeToFit.height;
157 - (CGFloat)remainingRowsHeight
159 NSUInteger height = [self marginY];
162 NSUInteger autosizedRows = 0;
163 for (NSUInteger i = 0; i < _rowCount; i++) {
164 CGFloat constrainedHeight = [self constrainedHeightOfRow:i];
165 if (!constrainedHeight)
167 height += constrainedHeight + [self marginY];
169 CGFloat remaining = 0;
170 if (height < self.bounds.size.height && autosizedRows)
171 remaining = (self.bounds.size.height - height) / autosizedRows;
178 - (CGFloat)heightOfRow:(NSUInteger)targetRow
180 NSAssert(targetRow < _rowCount, @"accessing a non existing row");
181 CGFloat height = [self constrainedHeightOfRow:targetRow];
183 height = [self remainingRowsHeight];
188 - (CGFloat)topOfRow:(NSUInteger)targetRow
190 CGFloat top = [self marginY];
191 for (NSUInteger i = 1; i < _rowCount - targetRow; i++)
193 top += [self heightOfRow:_rowCount - i] + [self marginY];
198 - (CGFloat)constrainedWidthOfColumn:(NSUInteger)targetColumn
201 for(NSDictionary *obj in _griddedViews) {
202 NSUInteger col = [[obj objectForKey:@"col"] intValue];
203 if (col != targetColumn)
205 NSUInteger colSpan = [[obj objectForKey:@"colSpan"] intValue];
208 NSView *view = [obj objectForKey:@"view"];
209 if ([view autoresizingMask] & NSViewWidthSizable)
211 NSSize sizeToFit = [self objectSizeToFit:view];
212 if (width < sizeToFit.width)
213 width = sizeToFit.width;
218 - (CGFloat)remainingColumnWidth
220 NSUInteger width = [self marginX];
223 NSUInteger autosizedCol = 0;
224 for (NSUInteger i = 0; i < _colCount; i++) {
225 CGFloat constrainedWidth = [self constrainedWidthOfColumn:i];
226 if (!constrainedWidth)
228 width += constrainedWidth + [self marginX];
230 CGFloat remaining = 0;
231 if (width < self.bounds.size.width && autosizedCol)
232 remaining = (self.bounds.size.width - width) / autosizedCol;
238 - (CGFloat)widthOfColumn:(NSUInteger)targetColumn
240 CGFloat width = [self constrainedWidthOfColumn:targetColumn];
242 width = [self remainingColumnWidth];
247 - (CGFloat)leftOfColumn:(NSUInteger)targetColumn
249 CGFloat left = [self marginX];
250 for (NSUInteger i = 0; i < targetColumn; i++)
252 left += [self widthOfColumn:i] + [self marginX];
259 for(NSDictionary *obj in _griddedViews) {
260 NSUInteger row = [[obj objectForKey:@"row"] intValue];
261 NSUInteger col = [[obj objectForKey:@"col"] intValue];
262 NSUInteger rowSpan = [[obj objectForKey:@"rowSpan"] intValue];
263 NSUInteger colSpan = [[obj objectForKey:@"colSpan"] intValue];
264 NSView *view = [obj objectForKey:@"view"];
268 if ([view autoresizingMask] & NSViewHeightSizable || rowSpan > 1) {
270 for (NSUInteger r = 0; r < rowSpan; r++) {
271 if (row + r >= _rowCount)
273 height += [self heightOfRow:row + r] + [self marginY];
275 rect.size.height = height - [self marginY];
278 rect.size.height = [self objectSizeToFit:view].height;
281 if ([view autoresizingMask] & NSViewWidthSizable) {
283 for (NSUInteger c = 0; c < colSpan; c++)
284 width += [self widthOfColumn:col + c] + [self marginX];
285 rect.size.width = width - [self marginX];
288 rect.size.width = [self objectSizeToFit:view].width;
291 rect.origin.y = [self topOfRow:row] + ([self heightOfRow:row] - rect.size.height) / 2;
292 rect.origin.x = [self leftOfColumn:col];
294 [view setFrame:rect];
295 [view setNeedsDisplay:YES];
299 - (NSMutableDictionary *)objectForView:(NSView *)view
301 for (NSMutableDictionary *dict in _griddedViews)
303 if ([dict objectForKey:@"view"] == view)
309 - (void)addSubview:(NSView *)view atRow:(NSUInteger)row column:(NSUInteger)column
310 rowSpan:(NSUInteger)rowSpan
311 colSpan:(NSUInteger)colSpan
313 if (row + 1 > _rowCount)
315 if (column + 1 > _colCount)
316 _colCount = column + 1;
318 NSMutableDictionary *dict = [self objectForView:view];
320 dict = [NSMutableDictionary dictionary];
321 [dict setObject:view forKey:@"view"];
322 [_griddedViews addObject:dict];
324 [dict setObject:[NSNumber numberWithInt:rowSpan] forKey:@"rowSpan"];
325 [dict setObject:[NSNumber numberWithInt:colSpan] forKey:@"colSpan"];
326 [dict setObject:[NSNumber numberWithInt:row] forKey:@"row"];
327 [dict setObject:[NSNumber numberWithInt:column] forKey:@"col"];
329 [self addSubview:view];
332 // Recompute the size of the window after making sure we won't see anymore update
333 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(recomputeWindowSize) object:nil];
334 [self performSelector:@selector(recomputeWindowSize) withObject:nil afterDelay:0.1];
337 - (void)removeSubview:(NSView *)view
339 NSDictionary *dict = [self objectForView:view];
341 [_griddedViews removeObject:dict];
342 [view removeFromSuperview];
344 [self recomputeCount];
345 [self recomputeWindowSize];
348 [self setNeedsDisplay:YES];
351 - (void)setFrame:(NSRect)frameRect
353 [super setFrame:frameRect];
357 - (NSSize)flexSize:(NSSize)size
359 if (!_rowCount || !_colCount)
362 CGFloat minHeight = [self marginY];
363 BOOL canFlexHeight = NO;
364 for (NSUInteger i = 0; i < _rowCount; i++) {
365 CGFloat constrained = [self constrainedHeightOfRow:i];
370 minHeight += constrained + [self marginY];
373 CGFloat minWidth = [self marginX];
374 BOOL canFlexWidth = NO;
375 for (NSUInteger i = 0; i < _colCount; i++) {
376 CGFloat constrained = [self constrainedWidthOfColumn:i];
381 minWidth += constrained + [self marginX];
383 if (size.width < minWidth)
384 size.width = minWidth;
385 if (size.height < minHeight)
386 size.height = minHeight;
388 size.height = minHeight;
390 size.width = minWidth;