float TruncStringBase(BString* outString, const char* inString, int32 length, const View* view, float width, uint32 truncMode = B_TRUNCATE_MIDDLE) { // we are using a template version of this call to make sure // the right StringWidth gets picked up for BView x BPoseView // for max speed and flexibility // a standard ellipsis inserting fitting algorithm if (view->StringWidth(inString, length) <= width) *outString = inString; else { const char* source[1]; char* results[1]; source[0] = inString; results[0] = outString->LockBuffer(length + 3); BFont font; view->GetFont(&font); font.GetTruncatedStrings(source, 1, truncMode, width, results); outString->UnlockBuffer(); } return view->StringWidth(outString->String(), outString->Length()); }
void TToolTipView::Draw(BRect /* where */) { char *src_strings[1]; char *tmp_string; char *truncated_strings[1]; BFont font; BRect r = Bounds(); font_height finfo; // draw border around window #ifdef kDRAW_WINDOW_FRAME SetHighColor(0, 0, 0, 255); StrokeRect(r); r.InsetBy(1, 1); #endif SetHighColor(kLIGHT_VIEW_COLOR); StrokeLine(BPoint(r.left, r.bottom), BPoint(r.left, r.top)); StrokeLine(BPoint(r.left + 1, r.top), BPoint(r.right - 1, r.top)); SetHighColor(kDARK_VIEW_COLOR); StrokeLine(BPoint(r.right, r.top), BPoint(r.right, r.bottom)); StrokeLine(BPoint(r.right - 1, r.bottom), BPoint(r.left + 1, r.bottom)); // set pen position GetFont(&font); font.GetHeight(&finfo); MovePenTo(kHOR_MARGIN + 1, kVER_MARGIN + finfo.ascent); // truncate string if needed src_strings[0] = fString; tmp_string = (char *)malloc(strlen(fString) + 16); truncated_strings[0] = tmp_string; font.GetTruncatedStrings((const char **)src_strings, 1, B_TRUNCATE_END, Bounds().Width() - (2 * kHOR_MARGIN) + 1, truncated_strings); // draw string SetLowColor(kVIEW_COLOR); SetHighColor(kTEXT_COLOR); DrawString(tmp_string); free(tmp_string); }
//: BeOS hook function // Draws a BeOS style title tab. This method class the two attached // decorations to draw the maximize and close buttons. void CMDITitleView::Draw(BRect updateRect) { Window()->BeginViewTransaction(); // --- Calc colors CColor background = BackgroundColor(); CColor highlight( MIN(background.red+80, 255), MIN(background.green+80, 255), MIN(background.blue+80, 255) ); CColor darkShadow( MAX(background.red-81, 0), MAX(background.green-81, 0), MAX(background.blue-81, 0) ); CColor textColor = Active() ? CColor::Black : CColor(80, 80, 80); // --- Fill background SetHighColor(background); FillRect(updateRect); // --- Draw borders BRect bounds = Bounds(); float width = bounds.Width(); float height = bounds.Height(); SetHighColor(CColor::BeShadow); MovePenTo(0, height); StrokeLine(BPoint(0, 0)); StrokeLine(BPoint(width, 0)); SetHighColor(CColor::BeDarkShadow); MovePenTo(width, 1); StrokeLine(BPoint(width, height)); SetHighColor(highlight); MovePenTo(1, height); StrokeLine(BPoint(1, 1)); StrokeLine(BPoint(width-1, 1)); SetHighColor(darkShadow); MovePenTo(width-1, 2); StrokeLine(BPoint(width-1, height)); // --- Draw close button if(DisplayCloseButton()) { closeButton->SetPosition(CloseButtonRect().LeftTop()); closeButton->SetBackgroundColor(background); closeButton->Draw(); } // --- Draw maximize button if(DisplayMaximizeButton()) { maximizeButton->SetPosition(MaximizeButtonRect().LeftTop()); maximizeButton->SetBackgroundColor(background); maximizeButton->Draw(); } // --- Draw title BFont titleFont; GetTitleFont(&titleFont); const char *title = target->Title(); char *truncatedTitle = new char [strlen(title)+3]; float titleWidth; BPoint titlePos; GetTitleWidthAndPos(titleWidth, titlePos); titleFont.GetTruncatedStrings(&title, 1, B_TRUNCATE_END, titleWidth, &truncatedTitle); SetHighColor(textColor); SetLowColor(background); SetFont(&titleFont); DrawString(truncatedTitle, strlen(truncatedTitle), titlePos); delete [] truncatedTitle; Window()->EndViewTransaction(); }