VLCUIWidgets.m 11.6 KB
Newer Older
1
/*****************************************************************************
2
 * VLCUIWidgets.m: Widgets for VLC's extensions dialogs for Mac OS X
3
 *****************************************************************************
4
 * Copyright (C) 2009-2015 the VideoLAN team and authors
5 6
 * $Id$
 *
7 8
 * Authors: Pierre d'Herbemont <pdherbemont # videolan dot>,
 *          Brendon Justin <brendonjustin@gmail.com>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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.
 *****************************************************************************/

25
#import "CompatibilityFixes.h"
26 27 28 29
#import "VLCUIWidgets.h"

#import <stdlib.h>

30
@implementation VLCDialogButton
31

32
@end
33 34


35
@implementation VLCDialogPopUpButton
36

37
@end
38 39


40
@implementation VLCDialogTextField
41

42
@end
43

44 45 46 47 48
@implementation VLCDialogLabel
- (void)resetCursorRects {
    [self addCursorRect:[self bounds] cursor:[NSCursor arrowCursor]];
}
@end
49

50
@implementation VLCDialogWindow
51

52
@end
53 54


55
@implementation VLCDialogList
56 57 58

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
59
    return [self.contentArray count];
60 61 62 63
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
64
    return [[self.contentArray objectAtIndex:rowIndex] objectForKey:@"text"];
65
}
66
@end
67

68 69 70 71 72 73
@interface VLCDialogGridView()
{
    NSUInteger _rowCount, _colCount;
    NSMutableArray *_griddedViews;
}
@end
74

75
@implementation VLCDialogGridView
76 77 78 79 80 81 82 83

- (NSUInteger)numViews
{
    return [_griddedViews count];
}

- (id)init
{
84
    if ((self = [super init])) {
85 86 87 88 89 90 91 92 93 94 95 96
        _colCount = 0;
        _rowCount = 0;
        _griddedViews = [[NSMutableArray alloc] init];
    }

    return self;
}

- (void)recomputeCount
{
    _colCount = 0;
    _rowCount = 0;
97
    for (NSDictionary *obj in _griddedViews) {
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
        NSUInteger row = [[obj objectForKey:@"row"] intValue];
        NSUInteger col = [[obj objectForKey:@"col"] intValue];
        if (col + 1 > _colCount)
            _colCount = col + 1;
        if (row + 1 > _rowCount)
            _rowCount = row + 1;
    }
}

- (void)recomputeWindowSize
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(recomputeWindowSize) object:nil];

    NSWindow *window = [self window];
    NSRect frame = [window frame];
    NSRect contentRect = [window contentRectForFrameRect:frame];
    contentRect.size = [self flexSize:frame.size];
    NSRect newFrame = [window frameRectForContentRect:contentRect];
    newFrame.origin.y -= newFrame.size.height - frame.size.height;
    newFrame.origin.x -= (newFrame.size.width - frame.size.width) / 2;
    [window setFrame:newFrame display:YES animate:YES];
}

- (NSSize)objectSizeToFit:(NSView *)view
{
    if ([view isKindOfClass:[NSControl class]]) {
        NSControl *control = (NSControl *)view;
        return [[control cell] cellSize];
    }
    return [view frame].size;
}

- (CGFloat)marginX
{
    return 16;
}
- (CGFloat)marginY
{
    return 8;
}

- (CGFloat)constrainedHeightOfRow:(NSUInteger)targetRow
{
    CGFloat height = 0;
    for(NSDictionary *obj in _griddedViews) {
        NSUInteger row = [[obj objectForKey:@"row"] intValue];
        if (row != targetRow)
            continue;
        NSUInteger rowSpan = [[obj objectForKey:@"rowSpan"] intValue];
        if (rowSpan != 1)
            continue;
        NSView *view = [obj objectForKey:@"view"];
        if ([view autoresizingMask] & NSViewHeightSizable)
            continue;
        NSSize sizeToFit = [self objectSizeToFit:view];
        if (height < sizeToFit.height)
            height = sizeToFit.height;
    }
    return height;
}

- (CGFloat)remainingRowsHeight
{
    NSUInteger height = [self marginY];
    if (!_rowCount)
        return 0;
    NSUInteger autosizedRows = 0;
    for (NSUInteger i = 0; i < _rowCount; i++) {
        CGFloat constrainedHeight = [self constrainedHeightOfRow:i];
        if (!constrainedHeight)
            autosizedRows++;
        height += constrainedHeight + [self marginY];
    }
    CGFloat remaining = 0;
    if (height < self.bounds.size.height && autosizedRows)
        remaining = (self.bounds.size.height - height) / autosizedRows;
    if (remaining < 0)
        remaining = 0;

    return remaining;
}

- (CGFloat)heightOfRow:(NSUInteger)targetRow
{
    NSAssert(targetRow < _rowCount, @"accessing a non existing row");
    CGFloat height = [self constrainedHeightOfRow:targetRow];
    if (!height)
        height = [self remainingRowsHeight];
    return height;
}


- (CGFloat)topOfRow:(NSUInteger)targetRow
{
    CGFloat top = [self marginY];
    for (NSUInteger i = 1; i < _rowCount - targetRow; i++)
        top += [self heightOfRow:_rowCount - i] + [self marginY];
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
    return top;
}

- (CGFloat)constrainedWidthOfColumn:(NSUInteger)targetColumn
{
    CGFloat width = 0;
    for(NSDictionary *obj in _griddedViews) {
        NSUInteger col = [[obj objectForKey:@"col"] intValue];
        if (col != targetColumn)
            continue;
        NSUInteger colSpan = [[obj objectForKey:@"colSpan"] intValue];
        if (colSpan != 1)
            continue;
        NSView *view = [obj objectForKey:@"view"];
        if ([view autoresizingMask] & NSViewWidthSizable)
            return 0;
        NSSize sizeToFit = [self objectSizeToFit:view];
        if (width < sizeToFit.width)
            width = sizeToFit.width;
    }
    return width;
}

- (CGFloat)remainingColumnWidth
{
    NSUInteger width = [self marginX];
    if (!_colCount)
        return 0;
    NSUInteger autosizedCol = 0;
    for (NSUInteger i = 0; i < _colCount; i++) {
        CGFloat constrainedWidth = [self constrainedWidthOfColumn:i];
        if (!constrainedWidth)
            autosizedCol++;
        width += constrainedWidth + [self marginX];
    }
    CGFloat remaining = 0;
    if (width < self.bounds.size.width && autosizedCol)
        remaining = (self.bounds.size.width - width) / autosizedCol;
    if (remaining < 0)
        remaining = 0;
    return remaining;
}

- (CGFloat)widthOfColumn:(NSUInteger)targetColumn
{
    CGFloat width = [self constrainedWidthOfColumn:targetColumn];
    if (!width)
        width = [self remainingColumnWidth];
    return width;
}


- (CGFloat)leftOfColumn:(NSUInteger)targetColumn
{
    CGFloat left = [self marginX];
251
    for (NSUInteger i = 0; i < targetColumn; i++) {
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 302 303 304 305 306 307 308
        left += [self widthOfColumn:i] + [self marginX];
    }
    return left;
}

- (void)relayout
{
    for(NSDictionary *obj in _griddedViews) {
        NSUInteger row = [[obj objectForKey:@"row"] intValue];
        NSUInteger col = [[obj objectForKey:@"col"] intValue];
        NSUInteger rowSpan = [[obj objectForKey:@"rowSpan"] intValue];
        NSUInteger colSpan = [[obj objectForKey:@"colSpan"] intValue];
        NSView *view = [obj objectForKey:@"view"];
        NSRect rect;

        // Get the height
        if ([view autoresizingMask] & NSViewHeightSizable || rowSpan > 1) {
            CGFloat height = 0;
            for (NSUInteger r = 0; r < rowSpan; r++) {
                if (row + r >= _rowCount)
                    break;
                height += [self heightOfRow:row + r] + [self marginY];
            }
            rect.size.height = height - [self marginY];
        }
        else
            rect.size.height = [self objectSizeToFit:view].height;

        // Get the width
        if ([view autoresizingMask] & NSViewWidthSizable) {
            CGFloat width = 0;
            for (NSUInteger c = 0; c < colSpan; c++)
                width += [self widthOfColumn:col + c] + [self marginX];
            rect.size.width = width - [self marginX];
        }
        else
            rect.size.width = [self objectSizeToFit:view].width;

        // Top corner
        rect.origin.y = [self topOfRow:row] + ([self heightOfRow:row] - rect.size.height) / 2;
        rect.origin.x = [self leftOfColumn:col];

        [view setFrame:rect];
        [view setNeedsDisplay:YES];
    }
}

- (NSMutableDictionary *)objectForView:(NSView *)view
{
    for (NSMutableDictionary *dict in _griddedViews)
    {
        if ([dict objectForKey:@"view"] == view)
            return dict;
    }
    return nil;
}

309 310 311 312 313
- (void)addSubview:(NSView *)view
             atRow:(NSUInteger)row
            column:(NSUInteger)column
           rowSpan:(NSUInteger)rowSpan
           colSpan:(NSUInteger)colSpan
314 315 316 317 318 319 320 321 322 323 324 325
{
    if (row + 1 > _rowCount)
        _rowCount = row + 1;
    if (column + 1 > _colCount)
        _colCount = column + 1;

    NSMutableDictionary *dict = [self objectForView:view];
    if (!dict) {
        dict = [NSMutableDictionary dictionary];
        [dict setObject:view forKey:@"view"];
        [_griddedViews addObject:dict];
    }
326 327 328 329
    [dict setObject:[NSNumber numberWithUnsignedInteger:rowSpan] forKey:@"rowSpan"];
    [dict setObject:[NSNumber numberWithUnsignedInteger:colSpan] forKey:@"colSpan"];
    [dict setObject:[NSNumber numberWithUnsignedInteger:row] forKey:@"row"];
    [dict setObject:[NSNumber numberWithUnsignedInteger:column] forKey:@"col"];
330 331 332 333 334 335 336 337 338

    [self addSubview:view];
    [self relayout];

    // Recompute the size of the window after making sure we won't see anymore update
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(recomputeWindowSize) object:nil];
    [self performSelector:@selector(recomputeWindowSize) withObject:nil afterDelay:0.1];
}

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
- (void)updateSubview:(NSView *)view
                atRow:(NSUInteger)row
               column:(NSUInteger)column
              rowSpan:(NSUInteger)rowSpan
              colSpan:(NSUInteger)colSpan
{
    NSDictionary *oldDict = [self objectForView:view];
    if (!oldDict) {
        [self addSubview:view
                   atRow:row
                  column:column
                 rowSpan:rowSpan
                 colSpan:colSpan];
        return;
    }
    [self relayout];
}

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
- (void)removeSubview:(NSView *)view
{
    NSDictionary *dict = [self objectForView:view];
    if (dict)
        [_griddedViews removeObject:dict];
    [view removeFromSuperview];

    [self recomputeCount];
    [self recomputeWindowSize];

    [self relayout];
    [self setNeedsDisplay:YES];
}

- (void)setFrame:(NSRect)frameRect
{
    [super setFrame:frameRect];
    [self relayout];
}

- (NSSize)flexSize:(NSSize)size
{
    if (!_rowCount || !_colCount)
        return size;

    CGFloat minHeight = [self marginY];
    BOOL canFlexHeight = NO;
    for (NSUInteger i = 0; i < _rowCount; i++) {
        CGFloat constrained = [self constrainedHeightOfRow:i];
        if (!constrained) {
            canFlexHeight = YES;
            constrained = 128;
        }
        minHeight += constrained + [self marginY];
    }

    CGFloat minWidth = [self marginX];
    BOOL canFlexWidth = NO;
    for (NSUInteger i = 0; i < _colCount; i++) {
        CGFloat constrained = [self constrainedWidthOfColumn:i];
        if (!constrained) {
            canFlexWidth = YES;
            constrained = 128;
        }
        minWidth += constrained + [self marginX];
    }
    if (size.width < minWidth)
        size.width = minWidth;
    if (size.height < minHeight)
        size.height = minHeight;
    if (!canFlexHeight)
        size.height = minHeight;
    if (!canFlexWidth)
        size.width = minWidth;
    return size;
}

414
@end