📄 timetextctrl.cpp
字号:
/********************************************************************** Audacity: A Digital Audio Editor TimeTextCtrl.cpp Dominic Mazzoni * * TimeTextCtrl Format String Documentation * The TimeTextCtrl makes use of a format string to specify the exact way that a single time value is split into several fields, such as the hh:mm:ss format. The advantage of this format string is that it is very small and compact, but human-readable and somewhat intuitive, so that it's easy to add new time layouts in the future. It's also designed to make it easier to add i18n support, since the way that times are displayed in different languages could conceivably vary a lot. The time to be formatted is expressed in seconds, so the format string specifies the relationship of each field to the number of seconds. Let's start by considering an example: here's the format string that prints an integer number of seconds in the hour minute second h:m:s format: *:60:60 The "*" is a wildcard, saying that the leftmost field can contain numbers of arbitrary magnitude. The next character, ':', since it is not a digit or a wildcard, is interpreted as a delimiter, and will be displayed between those fields. The next number, 60, indicates that the range of the next field (minutes) is 60. Then there's another ':' delimiter, and finally the last field (seconds) is 60. So, if you give it a number like 3758 (note format it as: 3758 seconds, "#:60:60" -> "1:2:38" Note that 3758 = 1*60*60 + 2*60 + 38. When TimeTextCtrl formats an integer, you can think of its process as working from right to left. Given the value "3758", it fills in the seconds by dividing by 60, sticking the remainder in the seconds field and then passing the quotient to the next field to the left. In order to format a field with leading zeros, simply add a leading zero to that field, like this: 3758 seconds, "#:060:060" -> "1:02:38" In order to format fractions, simply include a field delimiter ending with a decimal point. If the delimiter is simply '.' with nothing else, then the '.' is actually displayed. Otherwise the '.' is dropped, and the other characters in the delimiter are displayed instead. Here's how we'd display hours, minutes, and seconds with three decimal places after the seconds: 3758.5 seconds, "#:060:060.01000" -> "1:02:38.500" Similarly, here's how we'd display the fractional part of seconds as film frames (24 per second) instead of milliseconds: 3758.5 seconds, "#:060:060 and .24 frames" -> "1:02:38 and 12 frames" Note that the decimal '.' is associated with the delimeter, not with the 24. Additionally, the special character '#' can be used in place of a number to represent the current sample rate. Use '0#' to add leading zeros to that field. For example: 3758.5 seconds, "#:060:060+.#samples" -> "1:02:38+22050samples" Finally, there is a rule that allows you to change the units into something other than seconds. To do this, put a "|" character on the far right, followed by a number specifying the scaling factor. As an exception to previous rules, decimal points are allowed in the final scaling factor - the period is not interpreted as it would be before the "|" character. (This is fine, because all previous fields must be integers to make sense.) Anyway, if you include a scaling factor after a "|", the time will be multiplied by this factor before it is formatted. For example, to express the current time in NTSC frames (~29.97 fps), you could use the following formatting: 3758.5 seconds, "*.01000 frames|29.97002997" -> "112642.358 frames" Summary of format string rules: * The characters '0-9', '*', and '#' are numeric. Any sequence of these characters is treated as defining a new field by specifying its range. All other characters become delimiters between fields. (The one exception is that '.' is treated as numeric after the optional '|'.) * A field with a range of '*', which only makes sense as the leftmost field, means the field should display as large a number as necessary. * The character '#' represents the current sample rate. * If a field specifier beings with a leading zero, it will be formatted with leading zeros, too - enough to display the maximum value that field can display. So the number 7 in a field specified as '01000' would be formatted as '007'. Bond. James Bond. * Any non-numeric characters before the first field are treated as a prefix, and will be displayed to the left of the first field. * A delimiter ending in '.' is treated specially. All fields after this delimeter are fractional fields, after the decimal point. * The '|' character is treated as a special delimiter. The number to the right of this character (which is allowed to contain a decimal point) is treated as a scaling factor. The time is multiplied by this factor before multiplying.**********************************************************************/#include "TimeTextCtrl.h"#include <math.h>#include <wx/dcmemory.h>#include <wx/font.h>#include <wx/intl.h>#include <wx/sizer.h>#include <wx/stattext.h>// staticint TimeTextCtrl::sTextHeight = 0;int TimeTextCtrl::sTextWidth[11];enum { AnyTextCtrlID = 2700};BEGIN_EVENT_TABLE(TimeTextCtrl, wxPanel) EVT_TEXT(AnyTextCtrlID, TimeTextCtrl::OnText)END_EVENT_TABLE()struct BuiltinFormatString { const wxChar *name; const wxChar *formatStr;};const int kNumBuiltinFormatStrings = 8;BuiltinFormatString BuiltinFormatStrings[kNumBuiltinFormatStrings] = {{_("mm:ss"), _("*,01000m060.01000s")}, {_("hh:mm:ss + milliseconds"), _("*h060m060.01000s")}, {_("hh:mm:ss + samples"), _("*h060m060s+.#samples")}, {_("hh:mm:ss + film frames (24 fps)"), _("*h060m060s+.24 frames")}, {_("hh:mm:ss + CDDA frames (75 fps)"), _("*h060m060s+.75 frames")}, {_("seconds"), _("*,01000,01000.01000 seconds")}, {_("samples"), _("*,01000,01000,01000samples|#")}, {_("NTSC frames"), _("*,01000,01000.01000 NTSC frames|29.97002997")}};class TimeField {public: TimeField(int _base, int _range, bool _zeropad) { base = _base; range = _range; zeropad = _zeropad; digits = 0; textCtrl = NULL; staticCtrl = NULL; } int base; // divide by this (multiply, after decimal point) int range; // then take modulo this int digits; bool zeropad; wxString label; wxString formatStr; wxString str; wxTextCtrl *textCtrl; wxStaticText *staticCtrl; void CreateDigitFormatStr() { if (range > 1) digits = (int)ceil(log10(range-1.0)); if (zeropad && range>1) formatStr.Printf(wxT("%%0%dd"), digits); // ex. "%03d" if digits is 3 else formatStr = wxT("%d"); }};#include <wx/arrimpl.cpp>WX_DEFINE_OBJARRAY(TimeFieldArray);TimeTextCtrl::TimeTextCtrl(wxWindow *parent, wxWindowID id, wxString formatString, double timeValue, double sampleRate, const wxPoint &pos, const wxSize &size): wxPanel(parent, id, pos, size), mTimeValue(timeValue), mSampleRate(sampleRate), mFormatString(formatString), mModifyingText(false), mPrefixStaticText(NULL){ ParseFormatString(); CreateControls(); ValueToControls();}void TimeTextCtrl::SetFormatString(wxString formatString){ mFormatString = formatString; DeleteControls(); ParseFormatString(); CreateControls(); ValueToControls();}void TimeTextCtrl::SetSampleRate(double sampleRate){ mSampleRate = sampleRate; DeleteControls(); ParseFormatString(); CreateControls(); ValueToControls();}void TimeTextCtrl::SetTimeValue(double newTime){ mTimeValue = newTime; ValueToControls(); }const double TimeTextCtrl::GetTimeValue(){ ControlsToValue(); return mTimeValue;}int TimeTextCtrl::GetNumBuiltins(){ return kNumBuiltinFormatStrings;}wxString TimeTextCtrl::GetBuiltinName(int index){ if (index >= 0 && index < kNumBuiltinFormatStrings) return BuiltinFormatStrings[index].name; else return wxT("");}wxString TimeTextCtrl::GetBuiltinFormat(int index){ if (index >= 0 && index < kNumBuiltinFormatStrings) return BuiltinFormatStrings[index].formatStr; else return wxT("");}void TimeTextCtrl::ParseFormatString(){ mPrefix = wxT(""); mWholeFields.Clear(); mFracFields.Clear(); mScalingFactor = 1.0; const wxString format = mFormatString; bool inFrac = false; int fracMult = 1; wxString numStr; wxString delimStr; unsigned int i; for(i=0; i<format.Length(); i++) { bool handleDelim = false; bool handleNum = false; if (format[i] == '|') { wxString remainder = format.Right(format.Length() - i - 1); if (remainder == wxT("#")) mScalingFactor = mSampleRate; else remainder.ToDouble(&mScalingFactor); i = format.Length()-1; // force break out of loop if (delimStr != wxT("")) handleDelim = true; if (numStr != wxT("")) handleNum = true; } else if ((format[i] >= '0' && format[i] <='9') || format[i] == wxT('*') || format[i] == wxT('#')) { numStr += format[i]; if (delimStr != wxT("")) handleDelim = true; } else { delimStr += format[i]; if (numStr != wxT("")) handleNum = true; } if (i == format.Length() - 1) { if (numStr != wxT("")) handleNum = true; if (delimStr != wxT("")) handleDelim = true; } if (handleNum) { bool zeropad = false; long range = 0; if (numStr.Right(1) == wxT("#")) range = (long int)mSampleRate; else if (numStr.Right(1) != wxT("*")) { numStr.ToLong(&range); } if (numStr.GetChar(0)=='0' && numStr.Length()>1) zeropad = true; if (inFrac) { int base = fracMult * range; mFracFields.Add(TimeField(base, range, zeropad)); fracMult *= range; } else { unsigned int j; for(j=0; j<mWholeFields.GetCount(); j++) mWholeFields[j].base *= range; mWholeFields.Add(TimeField(1, range, zeropad)); } numStr = wxT(""); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -