summaryrefslogtreecommitdiffstats
path: root/macosx/ChapterTitles.m
blob: 1ff8b1194038d4bcc58baf665aae8a457d360c96 (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
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
/*  ChapterTitles.m $

   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. */
   
#include "ChapterTitles.h"
#include "hb.h"

@implementation ChapterTitles
- (id)init 
{
    self = [super init];
    if (self != nil)
    {
        fTitle              = NULL;
        fChapterTitlesArray = [[[NSMutableArray alloc] init] retain];
    }
    return self;
}

- (void)dealloc
{
    [fChapterTitlesArray release];
    [super               dealloc];
}

- (void)resetWithTitle:(hb_title_t *)title
{
    fTitle = title;
    [fChapterTitlesArray removeAllObjects];

    if (fTitle == NULL)
        return;

    for (int i = 0; i < hb_list_count(fTitle->job->list_chapter); i++)
    {
        hb_chapter_t *chapter = hb_list_item(fTitle->job->list_chapter, i);
        if (chapter != NULL)
        {
            if (chapter->title != NULL)
            {
                [fChapterTitlesArray addObject:[NSString
                                                stringWithFormat:@"%s",
                                                chapter->title]];
            }
            else
            {
                [fChapterTitlesArray addObject:[NSString
                                                stringWithFormat:@"Chapter %d",
                                                i + 1]];
            }
        }
    }
}

- (NSArray*)chapterTitlesArray
{
    return [NSArray arrayWithArray:fChapterTitlesArray];
}

- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
    if (fTitle == NULL)
    {
        return 0;
    }
    else
    {
        return [fChapterTitlesArray count];
    }
}

- (void)tableView:(NSTableView *)aTableView
        setObjectValue:(id)anObject
        forTableColumn:(NSTableColumn *)aTableColumn
        row:(NSInteger)rowIndex
{
    if (aTableColumn != nil && [[aTableColumn identifier] intValue] == 2 &&
        fTitle       != NULL)
    {
        [fChapterTitlesArray replaceObjectAtIndex:rowIndex
                                       withObject:[NSString
                                                   stringWithString:anObject]];
    }
}

- (id)tableView:(NSTableView *)aTableView
      objectValueForTableColumn:(NSTableColumn *)aTableColumn
      row:(NSInteger)rowIndex
{
    if ([[aTableColumn identifier] intValue] == 1)
    {
        return [NSString stringWithFormat:@"%d", rowIndex + 1];
    }
    else if (fTitle != NULL)
    {
        return [NSString stringWithString:[fChapterTitlesArray
                                           objectAtIndex:rowIndex]];
    }
    return @"__DATA ERROR__";
}

/* Method to edit the next chapter when the user presses Return. We have to use
a timer to avoid interfering with the chain of events that handles the edit. */
- (void)controlTextDidEndEditing: (NSNotification *) notification
{
    NSTableView *chapterTable = [notification object];
    NSInteger column = [chapterTable editedColumn];
    NSInteger row = [chapterTable editedRow];
    NSInteger textMovement;

    // Edit the cell in the next row, same column
    row++;
    textMovement = [[[notification userInfo] objectForKey:@"NSTextMovement"] integerValue];
    if( textMovement == NSReturnTextMovement && row < [chapterTable numberOfRows] )
    {
        NSArray *info = [NSArray arrayWithObjects:chapterTable,
            [NSNumber numberWithInteger:column], [NSNumber numberWithInteger:row], nil];
        /* The delay is unimportant; editNextRow: won't be called until the responder
        chain finishes because the event loop containing the timer is on this thread */
        [self performSelector:@selector(editNextRow:) withObject:info afterDelay:0.0];
    }
}

- (void)editNextRow: (id) objects
{
    NSTableView *chapterTable = [objects objectAtIndex:0];
    NSInteger column = [[objects objectAtIndex:1] integerValue];
    NSInteger row = [[objects objectAtIndex:2] integerValue];

    if( row >= 0 && row < [chapterTable numberOfRows] )
    {
        [chapterTable selectRow:row byExtendingSelection:NO];
        [chapterTable editColumn:column row:row withEvent:nil select:YES];
    }
}
@end