2
* This file is part of SubLib.
3
* Copyright (C) 2005-2006 Pedro Castro
5
* SubLib is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* SubLib is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
using System.Collections;
25
/// <summary>A container that represents all the subtitles.</summary>
26
public class SubtitleCollection {
27
private ArrayList subtitles = new ArrayList();
29
/// <summary>The number of subtitles in the collection.</summary>
31
get { return subtitles.Count; }
34
/// <summary>Returns an enumerator that can iterate through the collection.</summary>
35
/// <returns>An <see cref="IEnumerator" /> for the entire <see cref="SubtitleCollection" />.</returns>
36
public IEnumerator GetEnumerator () {
37
return subtitles.GetEnumerator();
40
public Subtitle this [int index] {
43
return subtitles[index] as Subtitle;
45
catch (ArgumentOutOfRangeException) {
51
/// <summary>Returns the subtitle at the specified index.</summary>
52
/// <param name="index">The zero-based subtitle's index.</param>
53
/// <returns>The subtitle at the specified index, or null in case the index is invalid.</returns>
54
public Subtitle Get (int index) {
55
if ((index >= 0) && (index < Count))
56
return (Subtitle)subtitles[index];
61
/// <summary>Adds a subtitle to the end of the collection.</summary>
62
/// <param name="subtitle">The subtitle to add.</param>
63
public void Add (Subtitle subtitle){
64
subtitles.Add(subtitle);
67
/// <summary>Adds a subtitle to the collection, inserting it at the specified index.</summary>
68
/// <param name="subtitle">The subtitle to add.</param>
69
/// <param name="index">The zero-based index at which the subtitle should be inserted.</param>
70
public void Add (Subtitle subtitle, int index){
71
subtitles.Insert(index, subtitle);
74
/// <summary>Creates a subtitle based on the subtitle at the specified index and adds it to the
75
/// collection, inserting it right before that index.</summary>
76
/// <remarks>The newly created subtitle's times will be based on the specified subtitle. Its end
77
/// time will be the start time of the existing subtitle minus <see cref="SubtitleConstants.MinTimeBetweenSubtitles" />.
78
/// Its duration will be <see cref="SubtitleConstants.MaxSingleLineSubtitleDuration" />. Both times will be wrapped to
79
/// zero if they are less than zero.</remarks>
80
/// <param name="index">The zero-based index before which the subtitle should be inserted.</param>
81
/// <param name="subtitleProperties">The SubtitleProperties of the subtitles.</param>
82
/// <returns>True if the subtitle could be added, false otherwise.</returns>
83
public bool AddNewBefore (int index, SubtitleProperties subtitleProperties) {
84
Subtitle existing = Get(index);
88
TimeSpan subtitleEnd = existing.Times.Start - TimeSpan.FromSeconds(SubtitleConstants.MinTimeBetweenSubtitles);
89
if (subtitleEnd < TimeSpan.Zero)
90
subtitleEnd = TimeSpan.FromSeconds(0);
92
TimeSpan subtitleStart = subtitleEnd - TimeSpan.FromSeconds(SubtitleConstants.MaxSingleLineSubtitleDuration);
93
if (subtitleStart < TimeSpan.Zero)
94
subtitleStart = TimeSpan.FromSeconds(0);
96
Subtitle subtitle = new Subtitle(subtitleProperties, subtitleStart, subtitleEnd);
101
/// <summary>Creates a subtitle based on the subtitle at the specified index and adds it to the
102
/// collection, inserting it right after that index.</summary>
103
/// <remarks>The newly created subtitle's times will be based on the specified subtitle. Its start
104
/// time will be the start time of the existing subtitle plus <see cref="SubtitleConstants.MinTimeBetweenSubtitles" />.
105
/// Its duration will be <see cref="SubtitleConstants.MaxSingleLineSubtitleDuration" />.</remarks>
106
/// <param name="index">The zero-based index after which the subtitle should be inserted.</param>
107
/// <param name="subtitleProperties">The SubtitleProperties of the subtitles.</param>
108
/// <returns>True if the subtitle could be added, false otherwise.</returns>
109
public bool AddNewAfter (int index, SubtitleProperties subtitleProperties) {
110
Subtitle existing = Get(index);
111
if (existing == null)
114
TimeSpan subtitleStart = existing.Times.End + TimeSpan.FromSeconds(SubtitleConstants.MinTimeBetweenSubtitles);
115
TimeSpan subtitleEnd = subtitleStart + TimeSpan.FromSeconds(SubtitleConstants.MaxSingleLineSubtitleDuration);
116
Subtitle subtitle = new Subtitle(subtitleProperties, subtitleStart, subtitleEnd);
117
Add(subtitle, index + 1);
121
/// <summary>Creates a subtitle and adds it to the collection, inserting it at the specified index.</summary>
122
/// <remarks>The newly created subtitle's start time will be zero and its duration will be
123
/// <see cref="SubtitleConstants.MaxSingleLineSubtitleDuration" />.</remarks>
124
/// <param name="index">The zero-based index at which the subtitle should be inserted.</param>
125
/// <param name="subtitleProperties">The SubtitleProperties of the subtitles.</param>
126
/// <returns>True if the subtitle could be added, false otherwise.</returns>
127
public bool AddNewAt (int index, SubtitleProperties subtitleProperties) {
128
if ((index < 0) || (index > Count))
131
TimeSpan subtitleStart = TimeSpan.FromSeconds(0);
132
TimeSpan subtitleEnd = TimeSpan.FromSeconds(SubtitleConstants.MaxSingleLineSubtitleDuration);
133
Subtitle subtitle = new Subtitle(subtitleProperties, subtitleStart, subtitleEnd);
134
Add(subtitle, index);
138
/// <summary>Checks whether a subtitle with the specified index exists in the collection.</summary>
139
/// <param name="index">The zero-based index.</param>
140
/// <returns>Whether the index is contained within the collection.</returns>
141
public bool Contains (int index) {
142
return (index >= 0) && (index < Count);
145
/// <summary>Removes a subtitle from the collection, given its index.</summary>
146
/// <param name="index">The zero-based index of the subtitle to be removed.</param>
147
public void Remove (int index) {
148
subtitles.RemoveAt(index);
151
public override string ToString(){
152
string result = "\t* SUBTITLE LIST *\n";
153
foreach(Subtitle subtitle in subtitles){
154
result += subtitle.ToString();
159
/* Internal methods */
161
internal void SetPropertiesForAll (SubtitleProperties properties) {
162
foreach (Subtitle subtitle in subtitles)
163
subtitle.Properties = properties;