summaryrefslogtreecommitdiffstats
path: root/win/C#/Controls/Subtitles.cs
blob: 56edcc852aaa9d76724912f65d76245ba983bdf0 (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
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
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
using System;
using System.Windows.Forms;

namespace Handbrake.Controls
{
    public partial class Subtitles : UserControl
    {

        // TODO
        // - Right click menu for adding/removal of tracks.
        // - Multi-select for removal.

        public Subtitles()
        {
            InitializeComponent();

        }

        private int fileContainer;
        public void setContainer(int value)
        {
            fileContainer = value;
            Boolean trigger = false;
            if (fileContainer != 2)
                foreach (ListViewItem item in lv_subList.Items)
                {
                    if (item.SubItems[1].Text.Contains("Bitmap"))
                    {
                        if (trigger)
                            lv_subList.Items.Remove(item);
                        trigger = true;
                    }
                }
        }




        // Controls
        private void btn_addSubTrack_Click(object sender, EventArgs e)
        {
            string forcedVal = "No";
            string burnedVal = "No";
            string defaultSub = "No";

            if (check_forced.Checked)
                forcedVal = "Yes";

            if (check_burned.Checked)
            {
                if (!drp_subtitleTracks.Text.Contains("Text"))
                {
                    burnedVal = "Yes";
                    setNoBurned();
                }
            }

            if (check_default.Checked)
            {
                defaultSub = "Yes";
                setNoDefault();
            }

            Boolean addTrack = true;
            if (fileContainer == 0 || fileContainer == 1)
            {
                burnedVal = "Yes";  // MP4 must have bitmap subs burned in.

                // Make sure we only have 1 bitmap track.
                if (drp_subtitleTracks.SelectedItem.ToString().Contains("Bitmap"))
                    foreach (ListViewItem item in lv_subList.Items)
                    {
                        if (item.SubItems[1].Text.Contains("Bitmap"))
                        {
                            MessageBox.Show(this,
                                            "More than one vobsub is not supported in mp4... Your first vobsub track will now be used.",
                                            "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            addTrack = false;
                        }
                    }
            }

            // Add the track if allowed.
            if (addTrack)
            {
                ListViewItem newTrack = new ListViewItem(getNewID().ToString());

                newTrack.SubItems.Add(drp_subtitleTracks.SelectedItem.ToString());
                newTrack.SubItems.Add(forcedVal);
                newTrack.SubItems.Add(burnedVal);
                newTrack.SubItems.Add(defaultSub);

                lv_subList.Items.Add(newTrack);
            }
        }
        private void btn_RemoveSubTrack_Click(object sender, EventArgs e)
        {
            removeTrack();
        }
        private void lb_subList_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Set the dropdown controls based on the selected item in the List.
            if (lv_subList.Items.Count != 0 && lv_subList.SelectedIndices.Count != 0)
            {
                // Reset the checkboxes
                check_forced.CheckState = CheckState.Unchecked;
                check_burned.CheckState = CheckState.Unchecked;
                check_default.CheckState = CheckState.Unchecked;

                // Setup the controls
                int c = 0;
                foreach (var item in drp_subtitleTracks.Items)
                {
                    if (item.ToString() == lv_subList.Items[lv_subList.SelectedIndices[0]].SubItems[1].Text)
                        drp_subtitleTracks.SelectedIndex = c;
                    c++;
                }
                drp_subtitleTracks.SelectedItem = lv_subList.Items[lv_subList.SelectedIndices[0]].SubItems[1];

                if (lv_subList.Items[lv_subList.SelectedIndices[0]].SubItems[2].Text == "Yes")
                    check_forced.CheckState = CheckState.Checked;

                if (lv_subList.Items[lv_subList.SelectedIndices[0]].SubItems[3].Text == "Yes")
                    check_burned.CheckState = CheckState.Checked;

                if (lv_subList.Items[lv_subList.SelectedIndices[0]].SubItems[4].Text == "Yes")
                    check_default.CheckState = CheckState.Checked;

                AudioTrackGroup.Text = "Selected Track: " + lv_subList.Items[lv_subList.SelectedIndices[0]].SubItems[0].Text;
            }
            else
                AudioTrackGroup.Text = "Selected Track: None (Click \"Add Track\" to add)";
        }

        private void drp_subtitleTracks_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Update an item in the  list if required.
            if (lv_subList.Items.Count != 0 && lv_subList.SelectedIndices.Count != 0)
            {
                lv_subList.Items[lv_subList.SelectedIndices[0]].SubItems[1].Text = drp_subtitleTracks.SelectedItem.ToString();
                lv_subList.Select();
            }
        }
        private void check_forced_CheckedChanged(object sender, EventArgs e)
        {
            // Update an item in the  list if required.
            if (lv_subList.Items.Count != 0 && lv_subList.SelectedIndices.Count != 0)
            {
                lv_subList.Items[lv_subList.SelectedIndices[0]].SubItems[2].Text = check_forced.Checked ? "Yes" : "No";
                lv_subList.Select();
            }
        }
        private void check_burned_CheckedChanged(object sender, EventArgs e)
        {
            // Update an item in the  list if required.
            if (lv_subList.Items.Count != 0 && lv_subList.SelectedIndices.Count != 0)
            {
                if (check_burned.Checked) // Make sure we only have 1 burned track
                    setNoBurned();

                lv_subList.Items[lv_subList.SelectedIndices[0]].SubItems[3].Text = check_burned.Checked ? "Yes" : "No";
                lv_subList.Select();
            }
        }
        private void check_default_CheckedChanged(object sender, EventArgs e)
        {
            // Update an item in the  list if required.
            if (lv_subList.Items.Count != 0 && lv_subList.SelectedIndices.Count != 0)
            {
                if (check_default.Checked) // Make sure we only have 1 default track
                    setNoDefault();

                lv_subList.Items[lv_subList.SelectedIndices[0]].SubItems[4].Text = check_default.Checked ? "Yes" : "No";
                lv_subList.Select();
            }
        }

        // Functions
        private void setNoDefault()
        {
            foreach (ListViewItem item in lv_subList.Items)
            {
                if (item.SubItems[4].Text == "Yes")
                    item.SubItems[4].Text = "No";
            }
        }
        private void setNoBurned()
        {
            foreach (ListViewItem item in lv_subList.Items)
            {
                if (item.SubItems[3].Text == "Yes")
                    item.SubItems[3].Text = "No";
            }
        }
        private void removeTrack()
        {
            // Remove the Item and reselect the control if the following conditions are met.
            if (lv_subList.SelectedItems.Count != 0)
            {
                // Record the current selected index.
                int currentPosition = lv_subList.SelectedIndices[0];

                lv_subList.Items.RemoveAt(lv_subList.SelectedIndices[0]);

                // Now reslect the correct item and give focus to the list.
                if (lv_subList.Items.Count != 0)
                {
                    if (currentPosition <= (lv_subList.Items.Count - 1))
                        lv_subList.Items[currentPosition].Selected = true;
                    else if (currentPosition > (lv_subList.Items.Count - 1))
                        lv_subList.Items[lv_subList.Items.Count - 1].Selected = true;

                    lv_subList.Select();
                }

                // Regenerate the ID numers
                reGenerateListIDs();
            }
        }

        // Helper Functions
        private int getNewID()
        {
            return lv_subList.Items.Count + 1;
        }
        private void reGenerateListIDs()
        {
            int i = 1;
            foreach (ListViewItem item in lv_subList.Items)
            {
                item.SubItems[0].Text = i.ToString();
                i++;
            }
        }

    }
}