commit 703ade32d78676ed30b68ca4c663c06686d10850
parent 93b5dc79a7c72f20fac69af4cfdf3674601ca33b
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date: Fri, 17 Aug 2012 22:26:02 -0700
Ellipsise file name and properties
Diffstat:
1 file changed, 43 insertions(+), 4 deletions(-)
diff --git a/src/spek-spectrogram.cc b/src/spek-spectrogram.cc
@@ -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);
+}