xref: /haiku/src/apps/showimage/ShowImageStatusView.cpp (revision ed24eb5ff12640d052171c6a7feba37fab8a75d1)
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(BScrollView* scrollView)
33 	:
34 	BView(BRect(), "statusview", B_FOLLOW_BOTTOM | B_FOLLOW_LEFT, B_WILL_DRAW),
35 	fScrollView(scrollView),
36 	fPreferredSize(0.0, 0.0)
37 {
38 	memset(fCellWidth, 0, sizeof(fCellWidth));
39 }
40 
41 
42 void
43 ShowImageStatusView::AttachedToWindow()
44 {
45 	SetFont(be_plain_font);
46 	BPrivate::AdoptScrollBarFontSize(this);
47 
48 	BScrollBar* scrollBar = fScrollView->ScrollBar(B_HORIZONTAL);
49 	MoveTo(0.0, scrollBar->Frame().top);
50 
51 	AdoptParentColors();
52 
53 	ResizeToPreferred();
54 }
55 
56 
57 void
58 ShowImageStatusView::GetPreferredSize(float* _width, float* _height)
59 {
60 	_ValidatePreferredSize();
61 
62 	if (_width)
63 		*_width = fPreferredSize.width;
64 
65 	if (_height)
66 		*_height = fPreferredSize.height;
67 }
68 
69 
70 void
71 ShowImageStatusView::ResizeToPreferred()
72 {
73 	float width, height;
74 	GetPreferredSize(&width, &height);
75 
76 	if (Bounds().Width() > width)
77 		width = Bounds().Width();
78 
79 	BView::ResizeTo(width, height);
80 }
81 
82 
83 void
84 ShowImageStatusView::Draw(BRect updateRect)
85 {
86 	if (fPreferredSize.width <= 0)
87 		return;
88 
89 	if (be_control_look != NULL) {
90 		BRect bounds(Bounds());
91 		be_control_look->DrawMenuBarBackground(this,
92 			bounds, updateRect,	ViewColor());
93 	}
94 
95 	BRect bounds(Bounds());
96 	rgb_color highColor = ui_color(B_PANEL_TEXT_COLOR);
97 
98 	SetHighColor(tint_color(ViewColor(), B_DARKEN_2_TINT));
99 	StrokeLine(bounds.LeftTop(), bounds.RightTop());
100 
101 	float x = bounds.left;
102 	for (size_t i = 0; i < kStatusCellCount - 1; i++) {
103 		x += fCellWidth[i];
104 		StrokeLine(BPoint(x, bounds.top + 3), BPoint(x, bounds.bottom - 3));
105 	}
106 
107 	SetLowColor(ViewColor());
108 	SetHighColor(highColor);
109 
110 	font_height fontHeight;
111 	GetFontHeight(&fontHeight);
112 
113 	x = bounds.left;
114 	float y = (bounds.bottom + bounds.top
115 		+ ceilf(fontHeight.ascent) - ceilf(fontHeight.descent)) / 2;
116 
117 	for (size_t i = 0; i < kStatusCellCount; i++) {
118 		if (fCellText[i].Length() == 0)
119 			continue;
120 		DrawString(fCellText[i], BPoint(x + kHorzSpacing, y));
121 		x += fCellWidth[i];
122 	}
123 }
124 
125 
126 void
127 ShowImageStatusView::MouseDown(BPoint where)
128 {
129 	BPrivate::BDirMenu* menu = new BDirMenu(NULL, BMessenger(kTrackerSignature),
130 		B_REFS_RECEIVED);
131 	BEntry entry;
132 	if (entry.SetTo(&fRef) == B_OK)
133 		menu->Populate(&entry, Window(), false, false, true, false, true);
134 	else
135 		menu->Populate(NULL, Window(), false, false, true, false, true);
136 
137 	BPoint point = Bounds().LeftBottom();
138 	point.y += 3;
139 	ConvertToScreen(&point);
140 	BRect clickToOpenRect(Bounds());
141 	ConvertToScreen(&clickToOpenRect);
142 	menu->Go(point, true, true, clickToOpenRect);
143 	delete menu;
144 }
145 
146 
147 void
148 ShowImageStatusView::Update(const entry_ref& ref, const BString& text,
149 	const BString& pages, const BString& imageType, float zoom)
150 {
151 	fRef = ref;
152 
153 	_SetFrameText(text);
154 	_SetZoomText(zoom);
155 	_SetPagesText(pages);
156 	_SetImageTypeText(imageType);
157 
158 	_ValidatePreferredSize();
159 	Invalidate();
160 }
161 
162 
163 void
164 ShowImageStatusView::SetZoom(float zoom)
165 {
166 	_SetZoomText(zoom);
167 	_ValidatePreferredSize();
168 	Invalidate();
169 }
170 
171 
172 void
173 ShowImageStatusView::_SetFrameText(const BString& text)
174 {
175 	fCellText[kFrameSizeCell] = text;
176 }
177 
178 
179 void
180 ShowImageStatusView::_SetZoomText(float zoom)
181 {
182 	BNumberFormat numberFormat;
183 	BString data;
184 
185 	if (numberFormat.FormatPercent(data, zoom) != B_OK)
186 		data.SetToFormat("%.0f%%", zoom * 100);
187 
188 	fCellText[kZoomCell] = data;
189 }
190 
191 
192 void
193 ShowImageStatusView::_SetPagesText(const BString& pages)
194 {
195 	fCellText[kPagesCell] = pages;
196 }
197 
198 
199 void
200 ShowImageStatusView::_SetImageTypeText(const BString& imageType)
201 {
202 	fCellText[kImageTypeCell] = imageType;
203 }
204 
205 
206 void
207 ShowImageStatusView::_ValidatePreferredSize()
208 {
209 	float orgWidth = fPreferredSize.width;
210 	// width
211 	fPreferredSize.width = 0.f;
212 	for (size_t i = 0; i < kStatusCellCount; i++) {
213 		if (fCellText[i].Length() == 0) {
214 			fCellWidth[i] = 0;
215 			continue;
216 		}
217 		float width = ceilf(StringWidth(fCellText[i]));
218 		if (width > 0)
219 			width += kHorzSpacing * 2;
220 		fCellWidth[i] = width;
221 		fPreferredSize.width += fCellWidth[i];
222 	}
223 
224 	// height
225 	font_height fontHeight;
226 	GetFontHeight(&fontHeight);
227 
228 	fPreferredSize.height = ceilf(fontHeight.ascent + fontHeight.descent
229 		+ fontHeight.leading);
230 
231 	float scrollBarSize = be_control_look->GetScrollBarWidth(B_HORIZONTAL);
232 	if (fPreferredSize.height < scrollBarSize)
233 		fPreferredSize.height = scrollBarSize;
234 
235 	float delta = fPreferredSize.width - orgWidth;
236 	ResizeBy(delta, 0);
237 	BScrollBar* scrollBar = fScrollView->ScrollBar(B_HORIZONTAL);
238 	scrollBar->ResizeBy(-delta, 0);
239 	scrollBar->MoveBy(delta, 0);
240 }
241