1 /* 2 * Copyright 2010, Stephan Aßmus <superstippi@gmx.de>. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "DurationView.h" 8 9 #include <LayoutUtils.h> 10 11 #include "DurationToString.h" 12 13 14 DurationView::DurationView(const char* name) 15 : 16 BStringView(name, ""), 17 fMode(kTimeToFinish), 18 fPosition(0), 19 fDuration(0), 20 fDisplayDuration(0) 21 { 22 SetSymbolScale(1.0f); 23 24 SetAlignment(B_ALIGN_RIGHT); 25 26 _Update(); 27 } 28 29 30 void 31 DurationView::AttachedToWindow() 32 { 33 BStringView::AttachedToWindow(); 34 SetHighColor(tint_color(ViewColor(), B_DARKEN_4_TINT)); 35 } 36 37 38 void 39 DurationView::MouseDown(BPoint where) 40 { 41 // Switch through the modes 42 uint32 mode = fMode + 1; 43 if (mode == kLastMode) 44 mode = 0; 45 SetMode(mode); 46 } 47 48 49 BSize 50 DurationView::MinSize() 51 { 52 BSize size; 53 char string[64]; 54 duration_to_string(int32(fDuration / -1000000LL), string, sizeof(string)); 55 size.width = StringWidth(string); 56 font_height fontHeight; 57 GetFontHeight(&fontHeight); 58 size.height = ceilf(fontHeight.ascent) + ceilf(fontHeight.descent); 59 return BLayoutUtils::ComposeSize(ExplicitMinSize(), size); 60 } 61 62 63 BSize 64 DurationView::MaxSize() 65 { 66 return BLayoutUtils::ComposeSize(ExplicitMaxSize(), MinSize()); 67 } 68 69 70 // #pragma mark - 71 72 73 void 74 DurationView::Update(bigtime_t position, bigtime_t duration) 75 { 76 if (position == fPosition && duration == fDuration) 77 return; 78 79 fPosition = position; 80 if (fDuration != duration) { 81 fDuration = duration; 82 InvalidateLayout(); 83 } 84 _Update(); 85 } 86 87 88 void 89 DurationView::SetMode(uint32 mode) 90 { 91 if (mode == fMode) 92 return; 93 94 fMode = mode; 95 _Update(); 96 } 97 98 99 void 100 DurationView::SetSymbolScale(float scale) 101 { 102 if (scale != 1.0f) { 103 BFont font(be_bold_font); 104 font.SetSize(font.Size() * scale * 1.2); 105 SetFont(&font); 106 } else 107 SetFont(be_plain_font); 108 109 InvalidateLayout(); 110 } 111 112 113 void 114 DurationView::_Update() 115 { 116 switch (fMode) { 117 case kTimeElapsed: 118 _GenerateString(fPosition); 119 break; 120 default: 121 case kTimeToFinish: 122 _GenerateString(fPosition - fDuration); 123 break; 124 case kDuration: 125 _GenerateString(fDuration); 126 break; 127 } 128 } 129 130 131 void 132 DurationView::_GenerateString(bigtime_t duration) 133 { 134 duration /= 1000000; 135 if (fDisplayDuration == duration) 136 return; 137 138 fDisplayDuration = duration; 139 140 char string[64]; 141 duration_to_string(duration, string, sizeof(string)); 142 143 SetText(string); 144 } 145 146