float PieView::_DrawDirectory(BRect b, FileInfo* info, float parentSpan, float beginAngle, int colorIdx, int level) { if (b.Width() < 2.0 * (kPieCenterSize + level * kPieRingSize + kPieOuterMargin + kPieInnerMargin)) { return 0.0; } if (info != NULL && info->color >= 0 && level == 0) colorIdx = info->color % kBasePieColorCount; else if (info != NULL) info->color = colorIdx; VolumeSnapshot* snapshot = fScanner->Snapshot(); float cx = floorf(b.left + b.Width() / 2.0 + 0.5); float cy = floorf(b.top + b.Height() / 2.0 + 0.5); float mySpan; if (level == 0) { // Make room for mouse over info. fMouseOverInfo.clear(); fMouseOverInfo[0] = SegmentList(); // Draw the center circle. const char* displayName; if (info == NULL) { // NULL represents the entire volume. Show used and free space in // the center circle, with the used segment representing the // volume's root directory. off_t volCapacity = snapshot->capacity; mySpan = 360.0 * (volCapacity - snapshot->freeBytes) / volCapacity; SetHighColor(kEmptySpcColor); FillEllipse(BPoint(cx, cy), kPieCenterSize, kPieCenterSize); SetHighColor(kBasePieColor[0]); FillArc(BPoint(cx, cy), kPieCenterSize, kPieCenterSize, 0.0, mySpan); // Show total volume capacity. char label[B_PATH_NAME_LENGTH]; size_to_string(volCapacity, label, sizeof(label)); SetHighColor(kPieBGColor); SetDrawingMode(B_OP_OVER); DrawString(label, BPoint(cx - StringWidth(label) / 2.0, cy + fFontHeight + kSmallVMargin)); SetDrawingMode(B_OP_COPY); displayName = snapshot->name.c_str(); // Record in-use space and free space for use during MouseMoved(). info = snapshot->rootDir; info->color = colorIdx; fMouseOverInfo[0].push_back(Segment(0.0, mySpan, info)); if (mySpan < 360.0 - kMinSegmentSpan) { fMouseOverInfo[0].push_back(Segment(mySpan, 360.0, snapshot->freeSpace)); } } else { // Show a normal directory. SetHighColor(kBasePieColor[colorIdx]); FillEllipse(BRect(cx - kPieCenterSize, cy - kPieCenterSize, cx + kPieCenterSize + 0.5, cy + kPieCenterSize + 0.5)); displayName = info->ref.name; mySpan = 360.0; // Record the segment for use during MouseMoved(). fMouseOverInfo[0].push_back(Segment(0.0, mySpan, info)); } SetPenSize(1.0); SetHighColor(kOutlineColor); StrokeEllipse(BPoint(cx, cy), kPieCenterSize + 0.5, kPieCenterSize + 0.5); // Show the name of the volume or directory. BString label(displayName); BFont font; GetFont(&font); font.TruncateString(&label, B_TRUNCATE_END, 2.0 * (kPieCenterSize - kSmallHMargin)); float labelWidth = font.StringWidth(label.String()); SetHighColor(kPieBGColor); SetDrawingMode(B_OP_OVER); DrawString(label.String(), BPoint(cx - labelWidth / 2.0, cy)); SetDrawingMode(B_OP_COPY); beginAngle = 0.0; } else { // Draw an exterior segment. float parentSize; if (info->parent == NULL) parentSize = (float)snapshot->capacity; else parentSize = (float)info->parent->size; mySpan = parentSpan * (float)info->size / parentSize; if (mySpan >= kMinSegmentSpan) { const float tint = 1.4f - level * 0.08f; float radius = kPieCenterSize + level * kPieRingSize - kPieRingSize / 2.0; // Draw the grey border SetHighColor(tint_color(kOutlineColor, tint)); SetPenSize(kPieRingSize + 1.5f); StrokeArc(BPoint(cx, cy), radius, radius, beginAngle - 0.001f * radius, mySpan + 0.002f * radius); // Draw the colored area rgb_color color = tint_color(kBasePieColor[colorIdx], tint); SetHighColor(color); SetPenSize(kPieRingSize); StrokeArc(BPoint(cx, cy), radius, radius, beginAngle, mySpan); // Record the segment for use during MouseMoved(). if (fMouseOverInfo.find(level) == fMouseOverInfo.end()) fMouseOverInfo[level] = SegmentList(); fMouseOverInfo[level].push_back( Segment(beginAngle, beginAngle + mySpan, info)); } } // Draw children. vector<FileInfo*>::iterator i = info->children.begin(); while (i != info->children.end()) { float childSpan = _DrawDirectory(b, *i, mySpan, beginAngle, colorIdx, level + 1); if (childSpan >= kMinSegmentSpan) { beginAngle += childSpan; colorIdx = (colorIdx + 1) % kBasePieColorCount; } i++; } return mySpan; }
void FontSelectionView::UpdateFontsMenu() { int32 numFamilies = count_font_families(); fFontsMenu->RemoveItems(0, fFontsMenu->CountItems(), true); BFont font; fFontsMenu->GetFont(&font); font_family currentFamily; font_style currentStyle; fCurrentFont.GetFamilyAndStyle(¤tFamily, ¤tStyle); for (int32 i = 0; i < numFamilies; i++) { font_family family; uint32 flags; if (get_font_family(i, &family, &flags) != B_OK) continue; // if we're setting the fixed font, we only want to show fixed fonts if (!strcmp(Name(), "fixed") && (flags & B_IS_FIXED) == 0) continue; float width = font.StringWidth(family); if (width > fMaxFontNameWidth) fMaxFontNameWidth = width; BMenu* stylesMenu = new BMenu(family); stylesMenu->SetRadioMode(true); stylesMenu->SetFont(&font); BMessage* message = new BMessage(kMsgSetFamily); message->AddString("family", family); message->AddString("name", Name()); BMenuItem* familyItem = new BMenuItem(stylesMenu, message); fFontsMenu->AddItem(familyItem); int32 numStyles = count_font_styles(family); for (int32 j = 0; j < numStyles; j++) { font_style style; if (get_font_style(family, j, &style, &flags) != B_OK) continue; message = new BMessage(kMsgSetStyle); message->AddString("family", (char*)family); message->AddString("style", (char*)style); message->AddString("name", Name()); BMenuItem *item = new BMenuItem(style, message); if (!strcmp(style, currentStyle) && !strcmp(family, currentFamily)) { item->SetMarked(true); familyItem->SetMarked(true); } stylesMenu->AddItem(item); } } }
void ExpressionTextView::SetValue(BString value) { // save the value fCurrentValue = value; // calculate the width of the string BFont font; uint32 mode = B_FONT_ALL; GetFontAndColor(&font, &mode); float stringWidth = font.StringWidth(value); // make the string shorter if it does not fit in the view float viewWidth = Frame().Width(); if (value.CountChars() > 3 && stringWidth > viewWidth) { // get the position of the first digit int32 firstDigit = 0; if (value[0] == '-') firstDigit++; // calculate the value of the exponent int32 exponent = 0; int32 offset = value.FindFirst('.'); if (offset == B_ERROR) { exponent = value.CountChars() - 1 - firstDigit; value.Insert('.', 1, firstDigit + 1); } else { if (offset == firstDigit + 1) { // if the value is 0.01 or larger then scientific notation // won't shorten the string if (value[firstDigit] != '0' || value[firstDigit + 2] != '0' || value[firstDigit + 3] != '0') { exponent = 0; } else { // remove the period value.Remove(offset, 1); // check for negative exponent value exponent = 0; while (value[firstDigit] == '0') { value.Remove(firstDigit, 1); exponent--; } // add the period value.Insert('.', 1, firstDigit + 1); } } else { // if the period + 1 digit fits in the view scientific notation // won't shorten the string BString temp = value; temp.Truncate(offset + 2); stringWidth = font.StringWidth(temp); if (stringWidth < viewWidth) exponent = 0; else { // move the period value.Remove(offset, 1); value.Insert('.', 1, firstDigit + 1); exponent = offset - (firstDigit + 1); } } } // add the exponent offset = value.CountChars() - 1; if (exponent != 0) value << "E" << exponent; // reduce the number of digits until the string fits or can not be // made any shorter stringWidth = font.StringWidth(value); char lastRemovedDigit = '0'; while (offset > firstDigit && stringWidth > viewWidth) { if (value[offset] != '.') lastRemovedDigit = value[offset]; value.Remove(offset--, 1); stringWidth = font.StringWidth(value); } // there is no need to keep the period if no digits follow if (value[offset] == '.') { value.Remove(offset, 1); offset--; } // take care of proper rounding of the result int digit = (int)lastRemovedDigit - '0'; // ascii to int if (digit >= 5) { for (; offset >= firstDigit; offset--) { if (value[offset] == '.') continue; digit = (int)(value[offset]) - '0' + 1; // ascii to int + 1 if (digit != 10) break; value.Remove(offset, 1); } if (digit == 10) { // carry over, shift the result if (value[firstDigit+1] == '.') { value[firstDigit+1] = '0'; value[firstDigit] = '.'; } value.Insert('1', 1, firstDigit); // remove the exponent value and the last digit offset = value.FindFirst('E'); if (offset == B_ERROR) offset = value.CountChars(); value.Truncate(--offset); offset--; // offset now points to the last digit // increase the exponent and add it back to the string exponent++; value << 'E' << exponent; } else { // increase the current digit value with one value[offset] = char(digit + 48); } } // remove trailing zeros while (value[offset] == '0') value.Remove(offset--, 1); // there is no need to keep the period if no digits follow if (value[offset] == '.') value.Remove(offset, 1); } // set the new value SetExpression(value); }
void BRadioButton::Draw(BRect updateRect) { if (Window() == NULL) return; BFont font; GetFont(&font); font_height fontHeight; font.GetHeight(&fontHeight); float sHeight = fontHeight.ascent + fontHeight.descent; BRect rect = Frame().OffsetToSelf(B_ORIGIN); rect.InsetBy(5, (rect.Height() - sHeight) / 2); if (rect.IsValid() == false) return; if ((IsFocus() || IsFocusChanging()) && IsEnabled() && Label() != NULL) { BPoint penLocation; penLocation.Set(rect.left + rect.Height() + 5, rect.Center().y + sHeight / 2 + 1); SetHighColor((IsFocus() && Window()->IsActivate()) ? ui_color(B_NAVIGATION_BASE_COLOR) : ViewColor()); StrokeLine(penLocation, penLocation + BPoint(font.StringWidth(Label()), 0)); } if (IsFocusChanging()) return; rgb_color shineColor = ui_color(B_SHINE_COLOR); rgb_color shadowColor = ui_color(B_SHADOW_COLOR); if (!IsEnabled()) { shineColor.disable(ViewColor()); shadowColor.disable(ViewColor()); } rect.right = rect.left + rect.Height(); SetHighColor(shineColor.mix_copy(0, 0, 0, 5)); FillEllipse(rect); SetHighColor(shineColor); StrokeArc(rect, 225, 180); SetHighColor(shadowColor); StrokeArc(rect, 45, 180); if (Value() == B_CONTROL_ON) { SetHighColor(shadowColor.mix_copy(255, 255, 255, 50)); BRect r = rect.InsetByCopy(3, 3); FillEllipse(r); } if (Label() != NULL) { BPoint penLocation; penLocation.x = rect.right + 5; penLocation.y = rect.Center().y - sHeight / 2.f; penLocation.y += fontHeight.ascent + 1; SetHighColor(IsEnabled() ? ui_color(B_PANEL_TEXT_COLOR) : ui_color(B_SHINE_COLOR).disable(ViewColor())); SetLowColor(ViewColor()); DrawString(Label(), penLocation); if (!IsEnabled()) { SetHighColor(ui_color(B_SHADOW_COLOR).disable(ViewColor())); DrawString(Label(), penLocation - BPoint(1, 1)); } } }
//! Redraw the button depending on whether it's up or down void CPUButton::Draw(BRect rect) { bool value = (bool)Value(); SetHighColor(value ? fOnColor : fOffColor); if (!fReplicant) { SetLowColor(Parent()->LowColor()); FillRect(Bounds(), B_SOLID_LOW); } BRect bounds = Bounds(); if (fReplicant && !fReplicantInDeskbar) { bounds.bottom -= 4; bounds.right -= 4; } else if (!fReplicant) { bounds.bottom -= 7; bounds.right -= 7; } BRect color_rect(bounds); color_rect.InsetBy(2, 2); if (value) { color_rect.bottom -= 1; color_rect.right -= 1; } FillRect(bounds); if (value) SetHighColor(80, 80, 80); else SetHighColor(255, 255, 255); BPoint start(0, 0); BPoint end(bounds.right, 0); StrokeLine(start, end); end.Set(0, bounds.bottom); StrokeLine(start, end); if (value) SetHighColor(32, 32, 32); else SetHighColor(216, 216, 216); start.Set(1, 1); end.Set(bounds.right - 1, 1); StrokeLine(start, end); end.Set(1, bounds.bottom - 1); StrokeLine(start, end); if (value) SetHighColor(216, 216, 216); else SetHighColor(80, 80, 80); start.Set(bounds.left + 1, bounds.bottom - 1); end.Set(bounds.right - 1, bounds.bottom - 1); StrokeLine(start, end); start.Set(bounds.right - 1, bounds.top + 1); StrokeLine(start, end); if (value) SetHighColor(255, 255, 255); else SetHighColor(32, 32, 32); start.Set(bounds.left, bounds.bottom); end.Set(bounds.right, bounds.bottom); StrokeLine(start, end); start.Set(bounds.right, bounds.top); StrokeLine(start, end); if (value) { SetHighColor(0, 0, 0); start.Set(bounds.left + 2, bounds.bottom - 2); end.Set(bounds.right - 2, bounds.bottom - 2); StrokeLine(start, end); start.Set(bounds.right - 2, bounds.top + 2); StrokeLine(start, end); } // Try to keep the text centered BFont font; GetFont(&font); int label_width = (int)font.StringWidth(Label()); int rect_width = bounds.IntegerWidth() - 1; int rect_height = bounds.IntegerHeight(); font_height fh; font.GetHeight(&fh); int label_height = (int)fh.ascent; int x_pos = (int)(((double)(rect_width - label_width) / 2.0) + 0.5); int y_pos = (rect_height - label_height) / 2 + label_height; MovePenTo(x_pos, y_pos); SetHighColor(0, 0, 0); SetDrawingMode(B_OP_OVER); DrawString(Label()); }