~ubuntu-branches/ubuntu/trusty/aegisub/trusty

« back to all changes in this revision

Viewing changes to src/dialog_export.cpp

  • Committer: Package Import Robot
  • Author(s): Sebastian Reichel
  • Date: 2012-03-16 22:58:00 UTC
  • Revision ID: package-import@ubuntu.com-20120316225800-yfb8h9e5n04rk46a
Tags: upstream-2.1.9
ImportĀ upstreamĀ versionĀ 2.1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2005, Rodrigo Braz Monteiro, Niels Martin Hansen
 
2
// All rights reserved.
 
3
//
 
4
// Redistribution and use in source and binary forms, with or without
 
5
// modification, are permitted provided that the following conditions are met:
 
6
//
 
7
//   * Redistributions of source code must retain the above copyright notice,
 
8
//     this list of conditions and the following disclaimer.
 
9
//   * Redistributions in binary form must reproduce the above copyright notice,
 
10
//     this list of conditions and the following disclaimer in the documentation
 
11
//     and/or other materials provided with the distribution.
 
12
//   * Neither the name of the Aegisub Group nor the names of its contributors
 
13
//     may be used to endorse or promote products derived from this software
 
14
//     without specific prior written permission.
 
15
//
 
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
17
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
18
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
19
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
20
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
21
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
22
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
23
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
24
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
25
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
26
// POSSIBILITY OF SUCH DAMAGE.
 
27
//
 
28
// -----------------------------------------------------------------------------
 
29
//
 
30
// AEGISUB
 
31
//
 
32
// Website: http://aegisub.cellosoft.com
 
33
// Contact: mailto:zeratul@cellosoft.com
 
34
//
 
35
 
 
36
 
 
37
///////////
 
38
// Headers
 
39
#include "config.h"
 
40
 
 
41
#include <wx/tokenzr.h>
 
42
#include <wx/button.h>
 
43
#include <wx/stattext.h>
 
44
#include <wx/filedlg.h>
 
45
#include <wx/msgdlg.h>
 
46
#include "dialog_export.h"
 
47
#include "ass_file.h"
 
48
#include "ass_exporter.h"
 
49
#include "frame_main.h"
 
50
#include "help_button.h"
 
51
#include "include/aegisub/exception.h"
 
52
 
 
53
 
 
54
///////////////
 
55
// Constructor
 
56
DialogExport::DialogExport (wxWindow *parent)
 
57
: wxDialog (parent, -1, _("Export"), wxDefaultPosition, wxSize(200,100), wxCAPTION | wxCLOSE_BOX, _T("Export"))
 
58
{
 
59
        // Filter list
 
60
        wxSizer *TopSizer = new wxStaticBoxSizer(wxVERTICAL,this,_("Filters"));
 
61
        Export = new AssExporter(AssFile::top);
 
62
        wxArrayString filters = Export->GetAllFilterNames();
 
63
        FilterList = new wxCheckListBox(this, Filter_List_Box, wxDefaultPosition, wxSize(200,100), filters);
 
64
 
 
65
        // Get selected filters
 
66
        wxString selected = Export->GetOriginalSubs()->GetScriptInfo(_T("Export filters"));
 
67
        wxStringTokenizer token(selected, _T("|"));
 
68
        int n = 0;
 
69
        while (token.HasMoreTokens()) {
 
70
                wxString cur = token.GetNextToken();
 
71
                if (!cur.IsEmpty()) {
 
72
                        n++;
 
73
                        for (unsigned int i=0;i<FilterList->GetCount();i++) {
 
74
                                if (FilterList->GetString(i) == cur) {
 
75
                                        FilterList->Check(i);
 
76
                                        break;
 
77
                                }
 
78
                        }
 
79
                }
 
80
        }
 
81
 
 
82
        // No filters listed on header, select all
 
83
        /*if (n == 0) {
 
84
                for (unsigned int i=0;i<FilterList->GetCount();i++) {
 
85
                        FilterList->Check(i);
 
86
                }
 
87
        }*/
 
88
 
 
89
        // Top buttons
 
90
        wxSizer *TopButtons = new wxBoxSizer(wxHORIZONTAL);
 
91
        TopButtons->Add(new wxButton(this,Button_Move_Up,_("Move up"),wxDefaultPosition,wxSize(90,-1)),1,wxEXPAND | wxRIGHT,0);
 
92
        TopButtons->Add(new wxButton(this,Button_Move_Down,_("Move down"),wxDefaultPosition,wxSize(90,-1)),1,wxEXPAND | wxRIGHT,5);
 
93
        TopButtons->Add(new wxButton(this,Button_Select_All,_("Select all"),wxDefaultPosition,wxSize(80,-1)),1,wxEXPAND | wxRIGHT,0);
 
94
        TopButtons->Add(new wxButton(this,Button_Select_None,_("Select none"),wxDefaultPosition,wxSize(80,-1)),1,wxEXPAND | wxRIGHT,0);
 
95
 
 
96
        // Description field
 
97
        Description = new wxTextCtrl(this, -1, _T(""), wxDefaultPosition, wxSize(200,60), wxTE_MULTILINE | wxTE_READONLY);
 
98
 
 
99
        // Charset dropdown list
 
100
        wxStaticText *charset_list_label = new wxStaticText(this, -1, _("Text encoding:"));
 
101
        CharsetList = new wxChoice(this, Charset_List_Box, wxDefaultPosition, wxDefaultSize, FrameMain::GetEncodings());
 
102
        wxSizer *charset_list_sizer = new wxBoxSizer(wxHORIZONTAL);
 
103
        charset_list_sizer->Add(charset_list_label, 0, wxALIGN_CENTER | wxRIGHT, 5);
 
104
        charset_list_sizer->Add(CharsetList, 1, wxEXPAND);
 
105
        if (!CharsetList->SetStringSelection(Export->GetOriginalSubs()->GetScriptInfo(_T("Export Encoding")))) {
 
106
                CharsetList->SetStringSelection(_T("UTF-8"));
 
107
        }
 
108
 
 
109
        // Top sizer
 
110
        TopSizer->Add(FilterList,1,wxEXPAND,0);
 
111
        TopSizer->Add(TopButtons,0,wxEXPAND,0);
 
112
        TopSizer->Add(Description,0,wxEXPAND | wxTOP,5);
 
113
        TopSizer->Add(charset_list_sizer, 0, wxEXPAND | wxTOP, 5);
 
114
 
 
115
        // Button sizer
 
116
        wxStdDialogButtonSizer *ButtonSizer = new wxStdDialogButtonSizer();
 
117
        wxButton *process = new wxButton(this,Button_Process,_("Export..."));
 
118
        ButtonSizer->AddButton(process);
 
119
        ButtonSizer->AddButton(new wxButton(this,wxID_CANCEL));
 
120
        ButtonSizer->AddButton(new HelpButton(this,_T("Export")));
 
121
        ButtonSizer->SetAffirmativeButton(process);
 
122
        ButtonSizer->Realize();
 
123
 
 
124
        // Draw stuff sizer
 
125
        HorizSizer = new wxBoxSizer(wxHORIZONTAL);
 
126
        OptionsSizer = new wxBoxSizer(wxVERTICAL);
 
127
        Export->DrawSettings(this,OptionsSizer);
 
128
        HorizSizer->Add(TopSizer,0,wxEXPAND | wxLEFT | wxTOP | wxBOTTOM,5);
 
129
        HorizSizer->Add(OptionsSizer,1,wxEXPAND | wxTOP | wxRIGHT | wxBOTTOM,5);
 
130
 
 
131
        // Main sizer
 
132
        MainSizer = new wxBoxSizer(wxVERTICAL);
 
133
        MainSizer->Add(HorizSizer,1,wxEXPAND,0);
 
134
        MainSizer->Add(ButtonSizer,0,wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM,5);
 
135
        MainSizer->SetSizeHints(this);
 
136
        SetSizer(MainSizer);
 
137
        RefreshOptions();
 
138
        CenterOnParent();
 
139
        FilterList->SetFocus();
 
140
}
 
141
 
 
142
 
 
143
//////////////
 
144
// Destructor
 
145
DialogExport::~DialogExport() {
 
146
        // Set script info data
 
147
        int n = 0;
 
148
        wxString infoList;
 
149
        for (unsigned int i=0;i<FilterList->GetCount();i++) {
 
150
                if (FilterList->IsChecked(i)) {
 
151
                        infoList += FilterList->GetString(i) + _T("|");
 
152
                        n++;
 
153
                }
 
154
        }
 
155
        if (n > 0) infoList = infoList.Left(infoList.Length()-1);
 
156
        Export->GetOriginalSubs()->SetScriptInfo(_T("Export filters"),infoList);
 
157
 
 
158
        // Delete exporter
 
159
        if (Export) delete Export;
 
160
        Export = NULL;
 
161
}
 
162
 
 
163
 
 
164
/////////////////////////////////
 
165
// Refresh displaying of options
 
166
void DialogExport::RefreshOptions() {
 
167
        int num = FilterList->GetCount();
 
168
        for (int i=0;i<num;i++) {
 
169
                wxSizer *sizer = Export->GetSettingsSizer(FilterList->GetString(i));
 
170
                if (sizer) OptionsSizer->Show(sizer,FilterList->IsChecked(i)?1:0,true);
 
171
        }
 
172
        Layout();
 
173
        MainSizer->Fit(this);
 
174
}
 
175
 
 
176
 
 
177
///////////////
 
178
// Event table
 
179
BEGIN_EVENT_TABLE(DialogExport,wxDialog)
 
180
        EVT_BUTTON(Button_Process,DialogExport::OnProcess)
 
181
        EVT_BUTTON(Button_Move_Up,DialogExport::OnMoveUp)
 
182
        EVT_BUTTON(Button_Move_Down,DialogExport::OnMoveDown)
 
183
        EVT_BUTTON(Button_Select_All,DialogExport::OnSelectAll)
 
184
        EVT_BUTTON(Button_Select_None,DialogExport::OnSelectNone)
 
185
        EVT_CHECKLISTBOX(Filter_List_Box,DialogExport::OnCheck)
 
186
        EVT_LISTBOX(Filter_List_Box, DialogExport::OnChange)
 
187
END_EVENT_TABLE()
 
188
 
 
189
 
 
190
/////////////////
 
191
// Process start
 
192
void DialogExport::OnProcess(wxCommandEvent &event) {
 
193
        // Get destination
 
194
        wxString filename = wxFileSelector(_("Export subtitles file"),_T(""),_T(""),_T(""),AssFile::GetWildcardList(2),wxFD_SAVE | wxFD_OVERWRITE_PROMPT,this);
 
195
        if (filename.empty()) return;
 
196
 
 
197
        // Add filters
 
198
        for (unsigned int i=0;i<FilterList->GetCount();i++) {
 
199
                if (FilterList->IsChecked(i)) {
 
200
                        Export->AddFilter(FilterList->GetString(i));
 
201
                }
 
202
        }
 
203
 
 
204
        // Export
 
205
        try {
 
206
                wxBusyCursor busy;
 
207
                Export->GetOriginalSubs()->SetScriptInfo(_T("Export Encoding"), CharsetList->GetStringSelection());
 
208
                Export->Export(filename, CharsetList->GetStringSelection(), this);
 
209
        }
 
210
        catch (Aegisub::UserCancelException *) {
 
211
                // ignore
 
212
        }
 
213
        catch (const wchar_t *error) {
 
214
                wxString err(error);
 
215
                wxMessageBox(err, _T("Error exporting subtitles"), wxOK | wxICON_ERROR, this);
 
216
        }
 
217
        catch (...) {
 
218
                wxMessageBox(_T("Unknown error"), _T("Error exporting subtitles"), wxOK | wxICON_ERROR, this);
 
219
        }
 
220
 
 
221
        // Close dialog
 
222
        EndModal(0);
 
223
}
 
224
 
 
225
 
 
226
/////////////////////////////
 
227
// Checked or unchecked item
 
228
void DialogExport::OnCheck(wxCommandEvent &event) {
 
229
        int n = event.GetInt();
 
230
        wxSizer *sizer = Export->GetSettingsSizer(FilterList->GetString(n));
 
231
        if (sizer) MainSizer->Show(sizer,FilterList->IsChecked(n)?1:0,true);
 
232
        Layout();
 
233
        MainSizer->Fit(this);
 
234
}
 
235
 
 
236
 
 
237
////////////////
 
238
// Changed item
 
239
void DialogExport::OnChange(wxCommandEvent &event) {
 
240
        int n = FilterList->GetSelection();
 
241
        if (n != wxNOT_FOUND) {
 
242
                wxString name = FilterList->GetString(n);
 
243
                //Description->SetValue(wxGetTranslation(Export->GetDescription(name)));
 
244
                Description->SetValue(Export->GetDescription(name));
 
245
        }
 
246
}
 
247
 
 
248
 
 
249
///////////
 
250
// Move up
 
251
void DialogExport::OnMoveUp(wxCommandEvent &event) {
 
252
        int pos = FilterList->GetSelection();
 
253
        if (pos <= 0) return;
 
254
        FilterList->Freeze();
 
255
        wxString tempname = FilterList->GetString(pos);
 
256
        bool tempval = FilterList->IsChecked(pos);
 
257
        FilterList->SetString(pos,FilterList->GetString(pos-1));
 
258
        FilterList->Check(pos,FilterList->IsChecked(pos-1));
 
259
        FilterList->SetString(pos-1,tempname);
 
260
        FilterList->Check(pos-1,tempval);
 
261
        FilterList->SetSelection(pos-1);
 
262
        FilterList->Thaw();
 
263
}
 
264
 
 
265
 
 
266
/////////////
 
267
// Move down
 
268
void DialogExport::OnMoveDown(wxCommandEvent &event) {
 
269
        int pos = FilterList->GetSelection();
 
270
        int n = FilterList->GetCount();
 
271
        if (pos == n-1 || pos == -1) return;
 
272
        FilterList->Freeze();
 
273
        wxString tempname = FilterList->GetString(pos);
 
274
        bool tempval = FilterList->IsChecked(pos);
 
275
        FilterList->SetString(pos,FilterList->GetString(pos+1));
 
276
        FilterList->Check(pos,FilterList->IsChecked(pos+1));
 
277
        FilterList->SetString(pos+1,tempname);
 
278
        FilterList->Check(pos+1,tempval);
 
279
        FilterList->SetSelection(pos+1);
 
280
        FilterList->Thaw();
 
281
}
 
282
 
 
283
 
 
284
//////////////
 
285
// Select all
 
286
void DialogExport::OnSelectAll(wxCommandEvent &event) {
 
287
        Freeze();
 
288
        FilterList->Freeze();
 
289
        for (unsigned int i=0;i<FilterList->GetCount();i++) {
 
290
                FilterList->Check(i,true);
 
291
                wxSizer *sizer = Export->GetSettingsSizer(FilterList->GetString(i));
 
292
                if (sizer) MainSizer->Show(sizer,true,true);
 
293
        }
 
294
 
 
295
        // Update dialog
 
296
        Layout();
 
297
        MainSizer->Fit(this);
 
298
        FilterList->Thaw();
 
299
        Thaw();
 
300
}
 
301
 
 
302
 
 
303
///////////////
 
304
// Select none
 
305
void DialogExport::OnSelectNone(wxCommandEvent &event) {
 
306
        Freeze();
 
307
        FilterList->Freeze();
 
308
        for (unsigned int i=0;i<FilterList->GetCount();i++) {
 
309
                FilterList->Check(i,false);
 
310
                wxSizer *sizer = Export->GetSettingsSizer(FilterList->GetString(i));
 
311
                if (sizer) MainSizer->Show(sizer,false,true);
 
312
        }
 
313
 
 
314
        // Update dialog
 
315
        FilterList->Thaw();
 
316
        Thaw();
 
317
        Layout();
 
318
        MainSizer->Fit(this);
 
319
}