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"
9 #include <ControlLook.h>
11 #include <MediaFile.h>
12 #include <MediaTrack.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
),
30 SetFont(be_plain_font
);
34 MediaFileInfoView::~MediaFileInfoView()
40 MediaFileInfoView::Draw(BRect
/*update*/)
44 _SetFontFace(B_BOLD_FACE
);
48 BPoint
labelStart(kSpacing
, fh
.ascent
+ fh
.leading
+ 1);
50 if (fMediaFile
== NULL
) {
51 DrawString(B_TRANSLATE("No file selected"), labelStart
);
56 DrawString(fRef
.name
, labelStart
);
57 labelStart
.y
+= fLineHeight
+ kSpacing
;
58 BPoint
infoStart(labelStart
.x
+ fMaxLabelWidth
+ kSpacing
, labelStart
.y
);
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
;
83 MediaFileInfoView::MinSize()
91 MediaFileInfoView::MaxSize()
93 return BSize(B_SIZE_UNLIMITED
, fMinSize
.height
);
98 MediaFileInfoView::PreferredSize()
106 MediaFileInfoView::LayoutAlignment()
108 return BAlignment(B_ALIGN_LEFT
, B_ALIGN_TOP
);
113 MediaFileInfoView::InvalidateLayout(bool /*children*/)
115 fMinMaxValid
= false;
116 BView::InvalidateLayout();
121 MediaFileInfoView::SetFont(const BFont
* font
, uint32 mask
)
123 BView::SetFont(font
, mask
);
124 if (mask
== B_FONT_FACE
)
127 fLineHeight
= _LineHeight();
129 bold
.SetFace(B_BOLD_FACE
);
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")));
142 MediaFileInfoView::AttachedToWindow()
144 rgb_color c
= Parent()->LowColor();
151 MediaFileInfoView::Update(BMediaFile
* file
, entry_ref
* ref
)
153 if (fMediaFile
== file
)
158 if (file
!= NULL
&& ref
!= NULL
) {
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
);
180 MediaFileInfoView::_LineHeight()
182 font_height fontHeight
;
183 GetFontHeight(&fontHeight
);
184 return ceilf(fontHeight
.ascent
+ fontHeight
.descent
+ fontHeight
.leading
);
189 MediaFileInfoView::_MaxLineWidth(BString
* strings
, int32 count
,
193 for (int32 i
= 0; i
< count
; i
++)
194 width
= max_c(font
.StringWidth(strings
[i
]), width
);
201 MediaFileInfoView::_ValidateMinMax()
210 bold
.SetFace(B_BOLD_FACE
);
213 if (fMediaFile
== NULL
) {
214 fMinSize
.width
= fNoFileLabelWidth
+ kSpacing
* 2;
215 fMinSize
.height
= fLineHeight
+ kSpacing
;
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();
239 MediaFileInfoView::_SetFontFace(uint16 face
)
243 SetFont(&font
, B_FONT_FACE
);