2006-01-16 22:02:54 +01:00
|
|
|
// Copyright (c) 2005, Rodrigo Braz Monteiro
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file timeedit_ctrl.cpp
|
|
|
|
/// @brief Edit-control for editing SSA-format timestamps
|
|
|
|
/// @ingroup custom_control
|
|
|
|
///
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-12-22 22:18:16 +01:00
|
|
|
#include "timeedit_ctrl.h"
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
#include "compat.h"
|
2011-12-22 22:18:16 +01:00
|
|
|
#include "include/aegisub/context.h"
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.h"
|
2014-05-22 01:23:28 +02:00
|
|
|
#include "project.h"
|
2012-10-25 17:13:13 +02:00
|
|
|
#include "utils.h"
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2014-07-06 16:28:58 +02:00
|
|
|
#include <libaegisub/ass/time.h>
|
|
|
|
|
|
|
|
#include <functional>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <wx/menu.h>
|
|
|
|
#include <wx/valtext.h>
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
#define TimeEditWindowStyle
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-12-22 22:18:16 +01:00
|
|
|
enum {
|
|
|
|
Time_Edit_Copy = 1320,
|
|
|
|
Time_Edit_Paste
|
|
|
|
};
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
TimeEdit::TimeEdit(wxWindow* parent, wxWindowID id, agi::Context *c, const std::string& value, const wxSize& size, bool asEnd)
|
|
|
|
: wxTextCtrl(parent, id, to_wx(value), wxDefaultPosition, size, wxTE_CENTRE | wxTE_PROCESS_ENTER)
|
2011-12-22 22:18:16 +01:00
|
|
|
, c(c)
|
|
|
|
, isEnd(asEnd)
|
|
|
|
, insert(!OPT_GET("Subtitle/Time Edit/Insert Mode")->GetBool())
|
|
|
|
, insert_opt(OPT_SUB("Subtitle/Time Edit/Insert Mode", &TimeEdit::OnInsertChanged, this))
|
2006-01-16 22:02:54 +01:00
|
|
|
{
|
2007-06-23 08:07:32 +02:00
|
|
|
// Set validator
|
|
|
|
wxTextValidator val(wxFILTER_INCLUDE_CHAR_LIST);
|
2013-10-08 16:19:56 +02:00
|
|
|
wxString includes[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", ".", ":", ","};
|
|
|
|
val.SetIncludes(wxArrayString(countof(includes), includes));
|
2007-06-23 08:07:32 +02:00
|
|
|
SetValidator(val);
|
|
|
|
|
|
|
|
// Other stuff
|
2014-07-06 16:28:58 +02:00
|
|
|
if (value.empty()) SetValue(to_wx(time.GetAssFormatted()));
|
2010-07-20 05:11:11 +02:00
|
|
|
|
2013-12-12 03:25:13 +01:00
|
|
|
Bind(wxEVT_MENU, std::bind(&TimeEdit::CopyTime, this), Time_Edit_Copy);
|
|
|
|
Bind(wxEVT_MENU, std::bind(&TimeEdit::PasteTime, this), Time_Edit_Paste);
|
|
|
|
Bind(wxEVT_TEXT, &TimeEdit::OnModified, this);
|
2011-08-31 06:17:37 +02:00
|
|
|
Bind(wxEVT_CONTEXT_MENU, &TimeEdit::OnContextMenu, this);
|
2012-11-26 16:38:50 +01:00
|
|
|
Bind(wxEVT_CHAR_HOOK, &TimeEdit::OnKeyDown, this);
|
2013-11-24 00:07:53 +01:00
|
|
|
Bind(wxEVT_CHAR, &TimeEdit::OnChar, this);
|
2011-12-22 22:29:18 +01:00
|
|
|
Bind(wxEVT_KILL_FOCUS, &TimeEdit::OnFocusLost, this);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2014-07-06 16:28:58 +02:00
|
|
|
void TimeEdit::SetTime(agi::Time new_time) {
|
2011-12-22 22:28:51 +01:00
|
|
|
if (time != new_time) {
|
|
|
|
time = new_time;
|
2010-07-20 05:11:11 +02:00
|
|
|
UpdateText();
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:30:22 +01:00
|
|
|
int TimeEdit::GetFrame() const {
|
2014-05-22 01:23:28 +02:00
|
|
|
return c->project->Timecodes().FrameAtTime(time, isEnd ? agi::vfr::END : agi::vfr::START);
|
2011-12-22 22:30:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TimeEdit::SetFrame(int fn) {
|
2014-05-22 01:23:28 +02:00
|
|
|
SetTime(c->project->Timecodes().TimeAtFrame(fn, isEnd ? agi::vfr::END : agi::vfr::START));
|
2011-12-22 22:30:22 +01:00
|
|
|
}
|
|
|
|
|
2010-07-20 05:11:11 +02:00
|
|
|
void TimeEdit::SetByFrame(bool enableByFrame) {
|
|
|
|
if (enableByFrame == byFrame) return;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2014-05-22 01:23:28 +02:00
|
|
|
byFrame = enableByFrame && c->project->Timecodes().IsLoaded();
|
2011-12-22 22:18:16 +01:00
|
|
|
UpdateText();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:18:16 +01:00
|
|
|
void TimeEdit::OnModified(wxCommandEvent &event) {
|
|
|
|
event.Skip();
|
2006-01-16 22:02:54 +01:00
|
|
|
if (byFrame) {
|
2011-12-22 22:30:14 +01:00
|
|
|
long temp = 0;
|
2006-01-16 22:02:54 +01:00
|
|
|
GetValue().ToLong(&temp);
|
2014-05-22 01:23:28 +02:00
|
|
|
time = c->project->Timecodes().TimeAtFrame(temp, isEnd ? agi::vfr::END : agi::vfr::START);
|
2007-06-23 08:07:32 +02:00
|
|
|
}
|
2011-12-22 22:18:16 +01:00
|
|
|
else if (insert)
|
2013-01-04 16:01:50 +01:00
|
|
|
time = from_wx(GetValue());
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:18:16 +01:00
|
|
|
void TimeEdit::UpdateText() {
|
|
|
|
if (byFrame)
|
2014-05-22 01:23:28 +02:00
|
|
|
ChangeValue(std::to_wstring(c->project->Timecodes().FrameAtTime(time, isEnd ? agi::vfr::END : agi::vfr::START)));
|
2011-12-22 22:18:16 +01:00
|
|
|
else
|
2014-07-06 16:28:58 +02:00
|
|
|
ChangeValue(to_wx(time.GetAssFormatted()));
|
2007-06-23 08:07:32 +02:00
|
|
|
}
|
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
void TimeEdit::OnKeyDown(wxKeyEvent &event) {
|
2013-11-24 00:07:53 +01:00
|
|
|
int kc = event.GetKeyCode();
|
|
|
|
|
|
|
|
// Needs to be done here to trump user-defined hotkeys
|
|
|
|
int key = event.GetUnicodeKey();
|
2011-12-22 22:18:16 +01:00
|
|
|
if (event.CmdDown()) {
|
|
|
|
if (key == 'C' || key == 'X')
|
2006-12-17 18:19:45 +01:00
|
|
|
CopyTime();
|
2011-12-22 22:18:16 +01:00
|
|
|
else if (key == 'V')
|
2006-12-17 18:19:45 +01:00
|
|
|
PasteTime();
|
2011-12-22 22:18:16 +01:00
|
|
|
else
|
2011-12-22 22:18:07 +01:00
|
|
|
event.Skip();
|
2012-05-28 16:18:40 +02:00
|
|
|
return;
|
2006-12-17 18:19:45 +01:00
|
|
|
}
|
2012-05-28 16:18:40 +02:00
|
|
|
|
2012-05-28 16:18:10 +02:00
|
|
|
// Shift-Insert would paste the stuff anyway
|
|
|
|
// but no one updates the private "time" variable.
|
2013-11-24 00:07:53 +01:00
|
|
|
if (event.ShiftDown() && kc == WXK_INSERT) {
|
2012-05-28 16:18:40 +02:00
|
|
|
PasteTime();
|
|
|
|
return;
|
2012-05-28 16:18:10 +02:00
|
|
|
}
|
2012-05-28 16:18:40 +02:00
|
|
|
|
2013-11-24 00:07:53 +01:00
|
|
|
if (byFrame || insert) {
|
|
|
|
event.Skip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Overwrite mode stuff
|
2012-05-28 16:18:40 +02:00
|
|
|
|
2013-11-24 00:07:53 +01:00
|
|
|
// On OS X backspace is reported as delete
|
|
|
|
#ifdef __APPLE__
|
|
|
|
if (kc == WXK_DELETE)
|
|
|
|
kc = WXK_BACK;
|
|
|
|
#endif
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2012-05-28 16:18:40 +02:00
|
|
|
// Back just moves cursor back one without deleting
|
2013-11-24 00:07:53 +01:00
|
|
|
if (kc == WXK_BACK) {
|
|
|
|
long start = GetInsertionPoint();
|
2012-05-28 16:18:40 +02:00
|
|
|
if (start > 0)
|
|
|
|
SetInsertionPoint(start - 1);
|
2011-12-22 22:18:16 +01:00
|
|
|
}
|
2013-11-24 00:07:53 +01:00
|
|
|
// Delete just does nothing
|
|
|
|
else if (kc != WXK_DELETE)
|
|
|
|
event.Skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimeEdit::OnChar(wxKeyEvent &event) {
|
|
|
|
event.Skip();
|
|
|
|
if (byFrame || insert) return;
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2013-11-24 00:07:53 +01:00
|
|
|
int key = event.GetUnicodeKey();
|
|
|
|
if ((key < '0' || key > '9') && key != ';' && key != '.' && key != ',') return;
|
|
|
|
|
|
|
|
event.Skip(false);
|
|
|
|
|
|
|
|
long start = GetInsertionPoint();
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string text = from_wx(GetValue());
|
2012-05-28 16:18:40 +02:00
|
|
|
// Cursor is at the end so do nothing
|
|
|
|
if (start >= (long)text.size()) return;
|
|
|
|
|
|
|
|
// If the cursor is at punctuation, move it forward to the next digit
|
2013-10-08 16:19:56 +02:00
|
|
|
if (text[start] == ':' || text[start] == '.' || text[start] == ',')
|
2012-05-28 16:18:40 +02:00
|
|
|
++start;
|
|
|
|
|
|
|
|
// : and . hop over punctuation but never insert anything
|
2013-11-24 00:07:53 +01:00
|
|
|
if (key == ':' || key == ';' || key == '.' || key == ',') {
|
2012-05-28 16:18:40 +02:00
|
|
|
SetInsertionPoint(start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overwrite the digit
|
2013-01-04 16:01:50 +01:00
|
|
|
text[start] = (char)key;
|
|
|
|
time = text;
|
2014-07-06 16:28:58 +02:00
|
|
|
SetValue(to_wx(time.GetAssFormatted()));
|
2012-05-28 16:18:40 +02:00
|
|
|
SetInsertionPoint(start + 1);
|
2007-06-23 08:07:32 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:18:16 +01:00
|
|
|
void TimeEdit::OnInsertChanged(agi::OptionValue const& opt) {
|
|
|
|
insert = !opt.GetBool();
|
2007-06-23 08:07:32 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:18:16 +01:00
|
|
|
void TimeEdit::OnContextMenu(wxContextMenuEvent &evt) {
|
|
|
|
if (byFrame || insert) {
|
|
|
|
evt.Skip();
|
2006-12-17 18:19:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:18:16 +01:00
|
|
|
wxMenu menu;
|
|
|
|
menu.Append(Time_Edit_Copy, _("&Copy"));
|
|
|
|
menu.Append(Time_Edit_Paste, _("&Paste"));
|
|
|
|
PopupMenu(&menu);
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:29:18 +01:00
|
|
|
void TimeEdit::OnFocusLost(wxFocusEvent &evt) {
|
|
|
|
if (insert || byFrame)
|
|
|
|
UpdateText();
|
|
|
|
evt.Skip();
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:18:16 +01:00
|
|
|
void TimeEdit::CopyTime() {
|
2013-01-04 16:01:50 +01:00
|
|
|
SetClipboard(from_wx(GetValue()));
|
2006-12-17 18:19:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TimeEdit::PasteTime() {
|
|
|
|
if (byFrame) {
|
|
|
|
Paste();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string text(GetClipboard());
|
|
|
|
if (text.empty()) return;
|
2012-10-25 17:13:13 +02:00
|
|
|
|
2014-07-06 16:28:58 +02:00
|
|
|
agi::Time tempTime(text);
|
|
|
|
if (tempTime.GetAssFormatted() == text) {
|
2012-10-25 17:13:13 +02:00
|
|
|
SetTime(tempTime);
|
|
|
|
SetSelection(0, GetValue().size());
|
|
|
|
|
2013-12-12 03:25:13 +01:00
|
|
|
wxCommandEvent evt(wxEVT_TEXT, GetId());
|
2012-10-25 17:13:13 +02:00
|
|
|
evt.SetEventObject(this);
|
|
|
|
HandleWindowEvent(evt);
|
2006-12-17 18:19:45 +01:00
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|