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 @implementation VLCDialogButton
34 @implementation VLCDialogPopUpButton
39 @implementation VLCDialogTextField
44 @implementation VLCDialogWindow
50 @implementation VLCDialogList
52 @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 @implementation VLCDialogGridView
68 - (NSUInteger)numViews
70 return [_griddedViews count];
75 if ((self = [super init])) {
78 _griddedViews = [[NSMutableArray alloc] init];
85 [_griddedViews release];
89 - (void)recomputeCount
93 for (NSDictionary *obj in _griddedViews) {
94 NSUInteger row = [[obj objectForKey:@"row"] intValue];
95 NSUInteger col = [[obj objectForKey:@"col"] intValue];
96 if (col + 1 > _colCount)
98 if (row + 1 > _rowCount)
103 - (void)recomputeWindowSize
105 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(recomputeWindowSize) object:nil];
107 NSWindow *window = [self window];
108 NSRect frame = [window frame];
109 NSRect contentRect = [window contentRectForFrameRect:frame];
110 contentRect.size = [self flexSize:frame.size];
111 NSRect newFrame = [window frameRectForContentRect:contentRect];
112 newFrame.origin.y -= newFrame.size.height - frame.size.height;
113 newFrame.origin.x -= (newFrame.size.width - frame.size.width) / 2;
114 [window setFrame:newFrame display:YES animate:YES];
117 - (NSSize)objectSizeToFit:(NSView *)view
119 if ([view isKindOfClass:[NSControl class]]) {
120 NSControl *control = (NSControl *)view;
121 return [[control cell] cellSize];
123 return [view frame].size;
135 - (CGFloat)constrainedHeightOfRow:(NSUInteger)targetRow
138 for(NSDictionary *obj in _griddedViews) {
139 NSUInteger row = [[obj objectForKey:@"row"] intValue];
140 if (row != targetRow)
142 NSUInteger rowSpan = [[obj objectForKey:@"rowSpan"] intValue];
145 NSView *view = [obj objectForKey:@"view"];
146 if ([view autoresizingMask] & NSViewHeightSizable)
148 NSSize sizeToFit = [self objectSizeToFit:view];
149 if (height < sizeToFit.height)
150 height = sizeToFit.height;
155 - (CGFloat)remainingRowsHeight
157 NSUInteger height = [self marginY];
160 NSUInteger autosizedRows = 0;
161 for (NSUInteger i = 0; i < _rowCount; i++) {
162 CGFloat constrainedHeight = [self constrainedHeightOfRow:i];
163 if (!constrainedHeight)
165 height += constrainedHeight + [self marginY];
167 CGFloat remaining = 0;
168 if (height < self.bounds.size.height && autosizedRows)
169 remaining = (self.bounds.size.height - height) / autosizedRows;
176 - (CGFloat)heightOfRow:(NSUInteger)targetRow
178 NSAssert(targetRow < _rowCount, @"accessing a non existing row");
179 CGFloat height = [self constrainedHeightOfRow:targetRow];
181 height = [self remainingRowsHeight];
186 - (CGFloat)topOfRow:(NSUInteger)targetRow
188 CGFloat top = [self marginY];
189 for (NSUInteger i = 1; i < _rowCount - targetRow; i++)
190 top += [self heightOfRow:_rowCount - i] + [self marginY];
195 - (CGFloat)constrainedWidthOfColumn:(NSUInteger)targetColumn
198 for(NSDictionary *obj in _griddedViews) {
199 NSUInteger col = [[obj objectForKey:@"col"] intValue];
200 if (col != targetColumn)
202 NSUInteger colSpan = [[obj objectForKey:@"colSpan"] intValue];
205 NSView *view = [obj objectForKey:@"view"];
206 if ([view autoresizingMask] & NSViewWidthSizable)
208 NSSize sizeToFit = [self objectSizeToFit:view];
209 if (width < sizeToFit.width)
210 width = sizeToFit.width;
215 - (CGFloat)remainingColumnWidth
217 NSUInteger width = [self marginX];
220 NSUInteger autosizedCol = 0;
221 for (NSUInteger i = 0; i < _colCount; i++) {
222 CGFloat constrainedWidth = [self constrainedWidthOfColumn:i];
223 if (!constrainedWidth)
225 width += constrainedWidth + [self marginX];
227 CGFloat remaining = 0;
228 if (width < self.bounds.size.width && autosizedCol)
229 remaining = (self.bounds.size.width - width) / autosizedCol;
235 - (CGFloat)widthOfColumn:(NSUInteger)targetColumn
237 CGFloat width = [self constrainedWidthOfColumn:targetColumn];
239 width = [self remainingColumnWidth];
244 - (CGFloat)leftOfColumn:(NSUInteger)targetColumn
246 CGFloat left = [self marginX];
247 for (NSUInteger i = 0; i < targetColumn; i++) {
248 left += [self widthOfColumn:i] + [self marginX];
255 for(NSDictionary *obj in _griddedViews) {
256 NSUInteger row = [[obj objectForKey:@"row"] intValue];
257 NSUInteger col = [[obj objectForKey:@"col"] intValue];
258 NSUInteger rowSpan = [[obj objectForKey:@"rowSpan"] intValue];
259 NSUInteger colSpan = [[obj objectForKey:@"colSpan"] intValue];
260 NSView *view = [obj objectForKey:@"view"];
264 if ([view autoresizingMask] & NSViewHeightSizable || rowSpan > 1) {
266 for (NSUInteger r = 0; r < rowSpan; r++) {
267 if (row + r >= _rowCount)
269 height += [self heightOfRow:row + r] + [self marginY];
271 rect.size.height = height - [self marginY];
274 rect.size.height = [self objectSizeToFit:view].height;
277 if ([view autoresizingMask] & NSViewWidthSizable) {
279 for (NSUInteger c = 0; c < colSpan; c++)
280 width += [self widthOfColumn:col + c] + [self marginX];
281 rect.size.width = width - [self marginX];
284 rect.size.width = [self objectSizeToFit:view].width;
287 rect.origin.y = [self topOfRow:row] + ([self heightOfRow:row] - rect.size.height) / 2;
288 rect.origin.x = [self leftOfColumn:col];
290 [view setFrame:rect];
291 [view setNeedsDisplay:YES];
295 - (NSMutableDictionary *)objectForView:(NSView *)view
297 for (NSMutableDictionary *dict in _griddedViews)
299 if ([dict objectForKey:@"view"] == view)
305 - (void)addSubview:(NSView *)view atRow:(NSUInteger)row column:(NSUInteger)column
306 rowSpan:(NSUInteger)rowSpan
307 colSpan:(NSUInteger)colSpan
309 if (row + 1 > _rowCount)
311 if (column + 1 > _colCount)
312 _colCount = column + 1;
314 NSMutableDictionary *dict = [self objectForView:view];
316 dict = [NSMutableDictionary dictionary];
317 [dict setObject:view forKey:@"view"];
318 [_griddedViews addObject:dict];
320 [dict setObject:@(rowSpan) forKey:@"rowSpan"];
321 [dict setObject:@(colSpan) forKey:@"colSpan"];
322 [dict setObject:@(row) forKey:@"row"];
323 [dict setObject:@(column) forKey:@"col"];
325 [self addSubview:view];
328 // Recompute the size of the window after making sure we won't see anymore update
329 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(recomputeWindowSize) object:nil];
330 [self performSelector:@selector(recomputeWindowSize) withObject:nil afterDelay:0.1];
333 - (void)removeSubview:(NSView *)view
335 NSDictionary *dict = [self objectForView:view];
337 [_griddedViews removeObject:dict];
338 [view removeFromSuperview];
340 [self recomputeCount];
341 [self recomputeWindowSize];
344 [self setNeedsDisplay:YES];
347 - (void)setFrame:(NSRect)frameRect
349 [super setFrame:frameRect];
353 - (NSSize)flexSize:(NSSize)size
355 if (!_rowCount || !_colCount)
358 CGFloat minHeight = [self marginY];
359 BOOL canFlexHeight = NO;
360 for (NSUInteger i = 0; i < _rowCount; i++) {
361 CGFloat constrained = [self constrainedHeightOfRow:i];
366 minHeight += constrained + [self marginY];
369 CGFloat minWidth = [self marginX];
370 BOOL canFlexWidth = NO;
371 for (NSUInteger i = 0; i < _colCount; i++) {
372 CGFloat constrained = [self constrainedWidthOfColumn:i];
377 minWidth += constrained + [self marginX];
379 if (size.width < minWidth)
380 size.width = minWidth;
381 if (size.height < minHeight)
382 size.height = minHeight;
384 size.height = minHeight;
386 size.width = minWidth;