xref: /haiku/src/apps/showimage/ShowImageStatusView.cpp (revision 9f3bdf3d039430b5172c424def20ce5d9f7367d4)
1 /*
2  * Copyright 2003-2010, Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Fernando Francisco de Oliveira
7  *		Michael Wilber
8  *		Axel Dörfler, axeld@pinc-software.de
9  */
10 
11 
12 #include "ShowImageStatusView.h"
13 
14 #include <ControlLook.h>
15 #include <Entry.h>
16 #include <MenuItem.h>
17 #include <NumberFormat.h>
18 #include <Path.h>
19 #include <PopUpMenu.h>
20 #include <ScrollView.h>
21 #include <StatusView.h>
22 
23 #include <tracker_private.h>
24 #include "DirMenu.h"
25 
26 #include "ShowImageView.h"
27 #include "ShowImageWindow.h"
28 
29 const float kHorzSpacing = 5.f;
30 
31 
32 ShowImageStatusView::ShowImageStatusView()
33 	:
34 	BView("statusview", B_WILL_DRAW),
35 	fPreferredSize(0.0, 0.0)
36 {
37 	memset(fCellWidth, 0, sizeof(fCellWidth));
38 }
39 
40 
41 void
42 ShowImageStatusView::AttachedToWindow()
43 {
44 	SetFont(be_plain_font);
45 	BPrivate::AdoptScrollBarFontSize(this);
46 
47 	AdoptParentColors();
48 
49 	ResizeToPreferred();
50 }
51 
52 
53 void
54 ShowImageStatusView::GetPreferredSize(float* _width, float* _height)
55 {
56 	_ValidatePreferredSize();
57 
58 	if (_width)
59 		*_width = fPreferredSize.width;
60 
61 	if (_height)
62 		*_height = fPreferredSize.height;
63 }
64 
65 
66 void
67 ShowImageStatusView::ResizeToPreferred()
68 {
69 	float width, height;
70 	GetPreferredSize(&width, &height);
71 
72 	if (Bounds().Width() > width)
73 		width = Bounds().Width();
74 
75 	BView::ResizeTo(width, height);
76 }
77 
78 
79 void
80 ShowImageStatusView::Draw(BRect updateRect)
81 {
82 	if (fPreferredSize.width <= 0)
83 		return;
84 
85 	if (be_control_look != NULL) {
86 		BRect bounds(Bounds());
87 		be_control_look->DrawMenuBarBackground(this,
88 			bounds, updateRect,	ViewColor());
89 	}
90 
91 	BRect bounds(Bounds());
92 	rgb_color highColor = ui_color(B_PANEL_TEXT_COLOR);
93 
94 	SetHighColor(tint_color(ViewColor(), B_DARKEN_2_TINT));
95 	StrokeLine(bounds.LeftTop(), bounds.RightTop());
96 
97 	float x = bounds.left;
98 	for (size_t i = 0; i < kStatusCellCount - 1; i++) {
99 		x += fCellWidth[i];
100 		StrokeLine(BPoint(x, bounds.top + 3), BPoint(x, bounds.bottom - 3));
101 	}
102 
103 	SetLowColor(ViewColor());
104 	SetHighColor(highColor);
105 
106 	font_height fontHeight;
107 	GetFontHeight(&fontHeight);
108 
109 	x = bounds.left;
110 	float y = (bounds.bottom + bounds.top
111 		+ ceilf(fontHeight.ascent) - ceilf(fontHeight.descent)) / 2;
112 
113 	for (size_t i = 0; i < kStatusCellCount; i++) {
114 		if (fCellText[i].Length() == 0)
115 			continue;
116 		DrawString(fCellText[i], BPoint(x + kHorzSpacing, y));
117 		x += fCellWidth[i];
118 	}
119 }
120 
121 
122 void
123 ShowImageStatusView::MouseDown(BPoint where)
124 {
125 	BPrivate::BDirMenu* menu = new BDirMenu(NULL, BMessenger(kTrackerSignature),
126 		B_REFS_RECEIVED);
127 	BEntry entry;
128 	if (entry.SetTo(&fRef) == B_OK)
129 		menu->Populate(&entry, Window(), false, false, true, false, true);
130 	else
131 		menu->Populate(NULL, Window(), false, false, true, false, true);
132 
133 	BPoint point = Bounds().LeftBottom();
134 	point.y += 3;
135 	ConvertToScreen(&point);
136 	BRect clickToOpenRect(Bounds());
137 	ConvertToScreen(&clickToOpenRect);
138 	menu->Go(point, true, true, clickToOpenRect);
139 	delete menu;
140 }
141 
142 
143 void
144 ShowImageStatusView::Update(const entry_ref& ref, const BString& text,
145 	const BString& pages, const BString& imageType, float zoom)
146 {
147 	fRef = ref;
148 
149 	_SetFrameText(text);
150 	_SetZoomText(zoom);
151 	_SetPagesText(pages);
152 	_SetImageTypeText(imageType);
153 
154 	_ValidatePreferredSize();
155 	Invalidate();
156 }
157 
158 
159 void
160 ShowImageStatusView::SetZoom(float zoom)
161 {
162 	_SetZoomText(zoom);
163 	_ValidatePreferredSize();
164 	Invalidate();
165 }
166 
167 
168 void
169 ShowImageStatusView::_SetFrameText(const BString& text)
170 {
171 	fCellText[kFrameSizeCell] = text;
172 }
173 
174 
175 void
176 ShowImageStatusView::_SetZoomText(float zoom)
177 {
178 	BNumberFormat numberFormat;
179 	BString data;
180 	numberFormat.FormatPercent(data, zoom);
181 
182 	fCellText[kZoomCell] = data;
183 }
184 
185 
186 void
187 ShowImageStatusView::_SetPagesText(const BString& pages)
188 {
189 	fCellText[kPagesCell] = pages;
190 }
191 
192 
193 void
194 ShowImageStatusView::_SetImageTypeText(const BString& imageType)
195 {
196 	fCellText[kImageTypeCell] = imageType;
197 }
198 
199 
200 void
201 ShowImageStatusView::_ValidatePreferredSize()
202 {
203 	// width
204 	fPreferredSize.width = 0.f;
205 	for (size_t i = 0; i < kStatusCellCount; i++) {
206 		if (fCellText[i].Length() == 0) {
207 			fCellWidth[i] = 0;
208 			continue;
209 		}
210 		float width = ceilf(StringWidth(fCellText[i]));
211 		if (width > 0)
212 			width += kHorzSpacing * 2;
213 		fCellWidth[i] = width;
214 		fPreferredSize.width += fCellWidth[i];
215 	}
216 
217 	// height
218 	font_height fontHeight;
219 	GetFontHeight(&fontHeight);
220 
221 	fPreferredSize.height = ceilf(fontHeight.ascent + fontHeight.descent
222 		+ fontHeight.leading);
223 
224 	float scrollBarSize = be_control_look->GetScrollBarWidth(B_HORIZONTAL);
225 	if (fPreferredSize.height < scrollBarSize)
226 		fPreferredSize.height = scrollBarSize;
227 
228 	SetExplicitMinSize(fPreferredSize);
229 	SetExplicitMaxSize(fPreferredSize);
230 }
231