Ellipsise file name and properties

This commit is contained in:
Alexander Kojevnikov 2012-08-17 22:26:02 -07:00
parent 93b5dc79a7
commit 703ade32d7

View File

@ -47,6 +47,9 @@ enum
RULER = 10,
};
// Forward declarations.
static wxString trim(wxDC& dc, const wxString& s, int length, bool trim_end);
SpekSpectrogram::SpekSpectrogram(wxFrame *parent) :
wxPanel(parent, -1, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE),
pipeline(NULL),
@ -176,13 +179,19 @@ void SpekSpectrogram::render(wxDC& dc)
// File name.
dc.SetFont(large_font);
// TODO: ellipsize
dc.DrawText(this->path, LPAD, TPAD - 2 * GAP - normal_height - large_height);
dc.DrawText(
trim(dc, this->path, w - LPAD - RPAD, false),
LPAD,
TPAD - 2 * GAP - normal_height - large_height
);
// File properties.
dc.SetFont(normal_font);
// TODO: ellipsize
dc.DrawText(this->desc, LPAD, TPAD - GAP - normal_height);
dc.DrawText(
trim(dc, this->desc, w - LPAD - RPAD, true),
LPAD,
TPAD - GAP - normal_height
);
// Prepare to draw the rulers.
dc.SetFont(small_font);
@ -308,3 +317,33 @@ void SpekSpectrogram::start()
Refresh();
}
// Trim `s` so that it fits into `length`.
static wxString trim(wxDC& dc, const wxString& s, int length, bool trim_end)
{
if (length <= 0) {
return wxEmptyString;
}
// Check if the entire string fits.
wxSize size = dc.GetTextExtent(s);
if (size.GetWidth() <= length) {
return s;
}
// Binary search FTW!
wxString fix(wxT("..."));
int i = 0;
int k = s.length();
while (k - i > 1) {
int j = (i + k) / 2;
size = dc.GetTextExtent(trim_end ? s.substr(0, j) + fix : fix + s.substr(j));
if (trim_end != (size.GetWidth() > length)) {
i = j;
} else {
k = j;
}
}
return trim_end ? s.substr(0, i) + fix : fix + s.substr(k);
}