BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / apps / mediaconverter / MediaFileInfoView.cpp
blobaa844110897d8740fda18e09fc328d19047f0739
1 // Copyright 1999, Be Incorporated. All Rights Reserved.
2 // Copyright 2000-2004, Jun Suzuki. All Rights Reserved.
3 // Copyright 2007, Stephan Aßmus. All Rights Reserved.
4 // This file may be used under the terms of the Be Sample Code License.
5 #include "MediaFileInfoView.h"
7 #include <Alert.h>
8 #include <Catalog.h>
9 #include <ControlLook.h>
10 #include <Locale.h>
11 #include <MediaFile.h>
12 #include <MediaTrack.h>
13 #include <String.h>
16 #undef B_TRANSLATION_CONTEXT
17 #define B_TRANSLATION_CONTEXT "MediaConverter-FileInfo"
20 const float kSpacing = 5.0f;
23 MediaFileInfoView::MediaFileInfoView()
25 BView("MediaFileInfoView", B_WILL_DRAW | B_SUPPORTS_LAYOUT),
26 fMinMaxValid(false),
27 fRef(),
28 fMediaFile(NULL)
30 SetFont(be_plain_font);
34 MediaFileInfoView::~MediaFileInfoView()
39 void
40 MediaFileInfoView::Draw(BRect /*update*/)
42 _ValidateMinMax();
44 _SetFontFace(B_BOLD_FACE);
46 font_height fh;
47 GetFontHeight(&fh);
48 BPoint labelStart(kSpacing, fh.ascent + fh.leading + 1);
50 if (fMediaFile == NULL) {
51 DrawString(B_TRANSLATE("No file selected"), labelStart);
52 return;
55 // draw filename
56 DrawString(fRef.name, labelStart);
57 labelStart.y += fLineHeight + kSpacing;
58 BPoint infoStart(labelStart.x + fMaxLabelWidth + kSpacing, labelStart.y);
60 // draw labels
61 DrawString(B_TRANSLATE("Audio:"), labelStart);
62 labelStart.y += fLineHeight * 2;
64 DrawString(B_TRANSLATE("Video:"), labelStart);
65 labelStart.y += fLineHeight * 2;
67 DrawString(B_TRANSLATE("Duration:"), labelStart);
68 labelStart.y += fLineHeight * 2;
70 // draw audio/video/duration info
71 _SetFontFace(B_REGULAR_FACE);
73 BString* infoStrings[5] = {&fInfo.audio.format, &fInfo.audio.details,
74 &fInfo.video.format, &fInfo.video.details, &fInfo.duration};
75 for (int32 i = 0; i < 5; i++) {
76 DrawString(*infoStrings[i], infoStart);
77 infoStart.y += fLineHeight;
82 BSize
83 MediaFileInfoView::MinSize()
85 _ValidateMinMax();
86 return fMinSize;
90 BSize
91 MediaFileInfoView::MaxSize()
93 return BSize(B_SIZE_UNLIMITED, fMinSize.height);
97 BSize
98 MediaFileInfoView::PreferredSize()
100 _ValidateMinMax();
101 return fMinSize;
105 BAlignment
106 MediaFileInfoView::LayoutAlignment()
108 return BAlignment(B_ALIGN_LEFT, B_ALIGN_TOP);
112 void
113 MediaFileInfoView::InvalidateLayout(bool /*children*/)
115 fMinMaxValid = false;
116 BView::InvalidateLayout();
120 void
121 MediaFileInfoView::SetFont(const BFont* font, uint32 mask)
123 BView::SetFont(font, mask);
124 if (mask == B_FONT_FACE)
125 return;
127 fLineHeight = _LineHeight();
128 BFont bold(font);
129 bold.SetFace(B_BOLD_FACE);
130 fMaxLabelWidth = 0;
132 BString labels[] = {B_TRANSLATE("Video:"), B_TRANSLATE("Duration:"), B_TRANSLATE("Audio:")};
133 int32 labelCount = sizeof(labels) / sizeof(BString);
134 fMaxLabelWidth = _MaxLineWidth(labels, labelCount, bold);
136 fNoFileLabelWidth = ceilf(bold.StringWidth(B_TRANSLATE("No file selected")));
137 InvalidateLayout();
141 void
142 MediaFileInfoView::AttachedToWindow()
144 rgb_color c = Parent()->LowColor();
145 SetViewColor(c);
146 SetLowColor(c);
150 void
151 MediaFileInfoView::Update(BMediaFile* file, entry_ref* ref)
153 if (fMediaFile == file)
154 return;
156 fMediaFile = file;
158 if (file != NULL && ref != NULL) {
159 fRef = *ref;
160 status_t result = fInfo.LoadInfo(file);
161 if (result != B_OK) {
162 BString error(B_TRANSLATE("An error has occurred reading the "
163 "file info.\n\nError : "));
164 error << strerror(result);
165 BAlert* alert = new BAlert(
166 B_TRANSLATE("File Error"), error.String(),
167 B_TRANSLATE("OK"), NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT);
168 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
169 alert->Go(NULL);
171 } else
172 fRef = entry_ref();
174 InvalidateLayout();
175 Invalidate();
179 float
180 MediaFileInfoView::_LineHeight()
182 font_height fontHeight;
183 GetFontHeight(&fontHeight);
184 return ceilf(fontHeight.ascent + fontHeight.descent + fontHeight.leading);
188 float
189 MediaFileInfoView::_MaxLineWidth(BString* strings, int32 count,
190 const BFont& font)
192 float width = 0;
193 for (int32 i = 0; i < count; i++)
194 width = max_c(font.StringWidth(strings[i]), width);
196 return ceilf(width);
200 void
201 MediaFileInfoView::_ValidateMinMax()
203 if (fMinMaxValid)
204 return;
206 BFont font;
207 GetFont(&font);
209 BFont bold(font);
210 bold.SetFace(B_BOLD_FACE);
211 fMinSize.Set(0, 0);
213 if (fMediaFile == NULL) {
214 fMinSize.width = fNoFileLabelWidth + kSpacing * 2;
215 fMinSize.height = fLineHeight + kSpacing;
216 return;
219 fMinSize.height += fLineHeight + kSpacing + 1;
220 fMinSize.width = ceilf(bold.StringWidth(fRef.name));
222 BString strings[5] = {fInfo.audio.format, fInfo.audio.details,
223 fInfo.video.format, fInfo.video.details, fInfo.duration};
224 float maxInfoWidth = _MaxLineWidth(strings, 5, font);
226 fMinSize.width = max_c(fMinSize.width, fMaxLabelWidth
227 + maxInfoWidth + kSpacing);
228 fMinSize.width += kSpacing;
230 fMinSize.height += fLineHeight * 5 + 2 * kSpacing;
231 // 5 lines of info, w/ spacing above and below (not between lines)
233 ResetLayoutInvalidation();
234 fMinMaxValid = true;
238 void
239 MediaFileInfoView::_SetFontFace(uint16 face)
241 BFont font;
242 font.SetFace(face);
243 SetFont(&font, B_FONT_FACE);