bool ClipView::GetToolTipAt(BPoint point, BToolTip** _tip) { ClipItem* item = static_cast<ClipItem*>(this->ItemAt(this->IndexOf(point))); if (item == NULL) return false; BString dateString = ""; bigtime_t added = item->GetTimeAdded(); if (BDateFormat().Format(dateString, added, B_MEDIUM_DATE_FORMAT) != B_OK) return false; BString timeString = ""; added = item->GetTimeAdded(); if (BTimeFormat().Format(timeString, added, B_SHORT_TIME_FORMAT) != B_OK) return false; BString toolTip(B_TRANSLATE_COMMENT("Added:\n%time%\n%date%", "Tooltip, don't change the variables %time% and %date%.")); toolTip.ReplaceAll("%time%", timeString.String()); toolTip.ReplaceAll("%date%", dateString.String()); SetToolTip(toolTip.String()); *_tip = ToolTip(); return true; }
float TruncTimeBase(BString* outString, int64 value, const View* view, float width) { float resultWidth = width + 1; time_t timeValue = (time_t)value; // Find the longest possible format that will fit the available space struct { BDateFormatStyle dateStyle; BTimeFormatStyle timeStyle; } formats[] = { { B_LONG_DATE_FORMAT, B_MEDIUM_TIME_FORMAT }, { B_LONG_DATE_FORMAT, B_SHORT_TIME_FORMAT }, { B_MEDIUM_DATE_FORMAT, B_SHORT_TIME_FORMAT }, { B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT }, }; BString date; BDateTimeFormat formatter; for (unsigned int i = 0; i < B_COUNT_OF(formats); ++i) { if (formatter.Format(date, timeValue, formats[i].dateStyle, formats[i].timeStyle) == B_OK) { resultWidth = view->StringWidth(date.String(), date.Length()); if (resultWidth <= width) { // Found a format that fits the available space, stop searching break; } } } // If we couldn't fit the date, try with just the time // TODO we could use only the time for "today" dates if (resultWidth > width && BDateFormat().Format(date, timeValue, B_SHORT_DATE_FORMAT) == B_OK) { resultWidth = view->StringWidth(date.String(), date.Length()); } if (resultWidth > width) { // even the shortest format string didn't do it, insert ellipsis resultWidth = TruncStringBase(outString, date.String(), (ssize_t)date.Length(), view, width); } else *outString = date; return resultWidth; }