xref: /haiku/src/apps/showimage/ShowImageStatusView.cpp (revision 1a76488fc88584bf66b9751d7fb9b6527ac20d87)
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 <Path.h>
18 #include <PopUpMenu.h>
19 #include <ScrollView.h>
20 #include <StatusView.h>
21 
22 #include <tracker_private.h>
23 #include "DirMenu.h"
24 
25 #include "ShowImageView.h"
26 #include "ShowImageWindow.h"
27 
28 const float kHorzSpacing = 5.f;
29 
30 
31 ShowImageStatusView::ShowImageStatusView(BScrollView* scrollView)
32 	:
33 	BView(BRect(), "statusview", B_FOLLOW_BOTTOM | B_FOLLOW_LEFT, B_WILL_DRAW),
34 	fScrollView(scrollView),
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 	BScrollBar* scrollBar = fScrollView->ScrollBar(B_HORIZONTAL);
48 	MoveTo(0.0, scrollBar->Frame().top);
49 
50 	AdoptParentColors();
51 
52 	ResizeToPreferred();
53 }
54 
55 
56 void
57 ShowImageStatusView::GetPreferredSize(float* _width, float* _height)
58 {
59 	_ValidatePreferredSize();
60 
61 	if (_width)
62 		*_width = fPreferredSize.width;
63 
64 	if (_height)
65 		*_height = fPreferredSize.height;
66 }
67 
68 
69 void
70 ShowImageStatusView::ResizeToPreferred()
71 {
72 	float width, height;
73 	GetPreferredSize(&width, &height);
74 
75 	if (Bounds().Width() > width)
76 		width = Bounds().Width();
77 
78 	BView::ResizeTo(width, height);
79 }
80 
81 
82 void
83 ShowImageStatusView::Draw(BRect updateRect)
84 {
85 	if (fPreferredSize.width <= 0)
86 		return;
87 
88 	if (be_control_look != NULL) {
89 		BRect bounds(Bounds());
90 		be_control_look->DrawMenuBarBackground(this,
91 			bounds, updateRect,	ViewColor());
92 	}
93 
94 	BRect bounds(Bounds());
95 	rgb_color highColor = ui_color(B_PANEL_TEXT_COLOR);
96 
97 	SetHighColor(tint_color(ViewColor(), B_DARKEN_2_TINT));
98 	StrokeLine(bounds.LeftTop(), bounds.RightTop());
99 
100 	float x = bounds.left;
101 	for (size_t i = 0; i < kStatusCellCount - 1; i++) {
102 		x += fCellWidth[i];
103 		StrokeLine(BPoint(x, bounds.top + 3), BPoint(x, bounds.bottom - 3));
104 	}
105 
106 	SetLowColor(ViewColor());
107 	SetHighColor(highColor);
108 
109 	font_height fontHeight;
110 	GetFontHeight(&fontHeight);
111 
112 	x = bounds.left;
113 	float y = (bounds.bottom + bounds.top
114 		+ ceilf(fontHeight.ascent) - ceilf(fontHeight.descent)) / 2;
115 
116 	for (size_t i = 0; i < kStatusCellCount; i++) {
117 		if (fCellText[i].Length() == 0)
118 			continue;
119 		DrawString(fCellText[i], BPoint(x + kHorzSpacing, y));
120 		x += fCellWidth[i];
121 	}
122 }
123 
124 
125 void
126 ShowImageStatusView::MouseDown(BPoint where)
127 {
128 	BPrivate::BDirMenu* menu = new BDirMenu(NULL, BMessenger(kTrackerSignature),
129 		B_REFS_RECEIVED);
130 	BEntry entry;
131 	if (entry.SetTo(&fRef) == B_OK)
132 		menu->Populate(&entry, Window(), false, false, true, false, true);
133 	else
134 		menu->Populate(NULL, Window(), false, false, true, false, true);
135 
136 	BPoint point = Bounds().LeftBottom();
137 	point.y += 3;
138 	ConvertToScreen(&point);
139 	BRect clickToOpenRect(Bounds());
140 	ConvertToScreen(&clickToOpenRect);
141 	menu->Go(point, true, true, clickToOpenRect);
142 	delete menu;
143 }
144 
145 
146 void
147 ShowImageStatusView::Update(const entry_ref& ref, const BString& text,
148 	const BString& pages, const BString& imageType, float zoom)
149 {
150 	fRef = ref;
151 
152 	_SetFrameText(text);
153 	_SetZoomText(zoom);
154 	_SetPagesText(pages);
155 	_SetImageTypeText(imageType);
156 
157 	_ValidatePreferredSize();
158 	Invalidate();
159 }
160 
161 
162 void
163 ShowImageStatusView::SetZoom(float zoom)
164 {
165 	_SetZoomText(zoom);
166 	_ValidatePreferredSize();
167 	Invalidate();
168 }
169 
170 
171 void
172 ShowImageStatusView::_SetFrameText(const BString& text)
173 {
174 	fCellText[kFrameSizeCell] = text;
175 }
176 
177 
178 void
179 ShowImageStatusView::_SetZoomText(float zoom)
180 {
181 	fCellText[kZoomCell].SetToFormat("%.0f%%", zoom * 100);
182 }
183 
184 
185 void
186 ShowImageStatusView::_SetPagesText(const BString& pages)
187 {
188 	fCellText[kPagesCell] = pages;
189 }
190 
191 
192 void
193 ShowImageStatusView::_SetImageTypeText(const BString& imageType)
194 {
195 	fCellText[kImageTypeCell] = imageType;
196 }
197 
198 
199 void
200 ShowImageStatusView::_ValidatePreferredSize()
201 {
202 	float orgWidth = fPreferredSize.width;
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 	float delta = fPreferredSize.width - orgWidth;
229 	ResizeBy(delta, 0);
230 	BScrollBar* scrollBar = fScrollView->ScrollBar(B_HORIZONTAL);
231 	scrollBar->ResizeBy(-delta, 0);
232 	scrollBar->MoveBy(delta, 0);
233 }
234