summaryrefslogtreecommitdiffstats
path: root/macosx/HBTitleSelectionController.m
blob: 63622a0932637777552eb3b01a2607303957330c (plain)
1
2
3
4
5
6
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
/* HBTitleSelectionController.h

 This file is part of the HandBrake source code.
 Homepage: <http://handbrake.fr/>.
 It may be used under the terms of the GNU General Public License. */

#import "HBTitleSelectionController.h"
#import "HBTitle.h"

@interface HBTitleSelectionController () <NSTableViewDataSource, NSTableViewDelegate>

@property (nonatomic, readonly) NSArray *titles;
@property (nonatomic, readonly) NSMutableArray *selection;

@property (nonatomic, readonly, weak) id<HBTitleSelectionDelegate> delegate;

@end

@implementation HBTitleSelectionController

- (instancetype)initWithTitles:(NSArray *)titles delegate:(id<HBTitleSelectionDelegate>)delegate
{
    self = [super initWithWindowNibName:@"HBTitleSelection"];
    if (self)
    {
        _titles = titles;
        _selection = [[NSMutableArray alloc] initWithCapacity:titles.count];
        _delegate = delegate;

        for (NSUInteger i = 0; i < titles.count; i++)
        {
            _selection[i] = @YES;
        }
    }

    return self;
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return self.titles.count;
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    HBTitle *title = self.titles[row];

    if ([tableColumn.identifier isEqualTo:@"index"])
    {
        return @(title.index);
    }
    else if ([tableColumn.identifier isEqualTo:@"title"])
    {
        return self.selection[row];
    }
    else if ([tableColumn.identifier isEqualTo:@"duration"])
    {
        return title.timeCode;
    }

    return nil;
}

- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;
{
    if ([tableColumn.identifier isEqualTo:@"title"])
    {
        self.selection[row] = object;
    }
}

- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    if ([tableColumn.identifier isEqualTo:@"title"])
    {
        HBTitle *title = self.titles[row];
        [aCell setTitle:title.name];
    }
}

- (IBAction)add:(id)sender
{
    NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];

    [self.selection enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj boolValue])
        {
            HBTitle *title = self.titles[idx];
            [indexes addIndex:title.index];
        }

    }];
    [self.delegate didSelectIndexes:indexes];
}

- (IBAction)cancel:(id)sender
{
    [self.delegate didSelectIndexes:[NSIndexSet indexSet]];
}

@end