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