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