xref: /haiku/src/apps/diskusage/StatusView.cpp (revision fc60ab16f5c51c3393ebb1d4095bac89931dbbe0)
1f75a7bf5SStephan Aßmus /*
2f75a7bf5SStephan Aßmus  * Copyright (c) 2008 Stephan Aßmus <superstippi@gmx.de>. All rights reserved.
3f75a7bf5SStephan Aßmus  * Distributed under the terms of the MIT/X11 license.
4f75a7bf5SStephan Aßmus  *
5f75a7bf5SStephan Aßmus  * Copyright (c) 1999 Mike Steed. You are free to use and distribute this software
6f75a7bf5SStephan Aßmus  * as long as it is accompanied by it's documentation and this copyright notice.
7f75a7bf5SStephan Aßmus  * The software comes with no warranty, etc.
8f75a7bf5SStephan Aßmus  */
9f75a7bf5SStephan Aßmus #include "StatusView.h"
10f75a7bf5SStephan Aßmus 
11f75a7bf5SStephan Aßmus #include <math.h>
12f75a7bf5SStephan Aßmus #include <stdio.h>
13f75a7bf5SStephan Aßmus 
14f75a7bf5SStephan Aßmus #include <Box.h>
15f75a7bf5SStephan Aßmus #include <Node.h>
16f75a7bf5SStephan Aßmus #include <String.h>
17f75a7bf5SStephan Aßmus #include <StringView.h>
18f75a7bf5SStephan Aßmus 
19f75a7bf5SStephan Aßmus #include "Common.h"
20f75a7bf5SStephan Aßmus #include "Scanner.h"
21f75a7bf5SStephan Aßmus 
22f75a7bf5SStephan Aßmus 
23*fc60ab16SRene Gollent StatusView::StatusView(BRect rect)
24*fc60ab16SRene Gollent 	: BView(rect, NULL, B_FOLLOW_LEFT_RIGHT | B_FOLLOW_BOTTOM, B_WILL_DRAW),
25f75a7bf5SStephan Aßmus 	  fCurrentFileInfo(NULL)
26f75a7bf5SStephan Aßmus {
27f75a7bf5SStephan Aßmus 	SetViewColor(kWindowColor);
28f75a7bf5SStephan Aßmus 
29f75a7bf5SStephan Aßmus 	struct font_height fh;
30f75a7bf5SStephan Aßmus 	be_plain_font->GetHeight(&fh);
31f75a7bf5SStephan Aßmus 	float plainHeight = ceilf(fh.ascent) + ceilf(fh.descent)
32f75a7bf5SStephan Aßmus 		+ ceilf(fh.leading);
33f75a7bf5SStephan Aßmus 	float fixedHeight = ceilf(fh.ascent) + ceilf(fh.descent)
34f75a7bf5SStephan Aßmus 		+ ceilf(fh.leading);
35f75a7bf5SStephan Aßmus 	ResizeTo(Bounds().Width(),
36*fc60ab16SRene Gollent 		2.0 * kSmallVMargin + max_c(plainHeight, fixedHeight));
37f75a7bf5SStephan Aßmus 
38f75a7bf5SStephan Aßmus 	BRect r = Bounds();
39f75a7bf5SStephan Aßmus 	r.top += kSmallVMargin;
40f75a7bf5SStephan Aßmus 
41f75a7bf5SStephan Aßmus 	// Size display
42f75a7bf5SStephan Aßmus 	r.right -= kSmallHMargin;
43f75a7bf5SStephan Aßmus 	r.left = r.right - StringWidth("9999.99 GB");
44f75a7bf5SStephan Aßmus 	fSizeView = new BStringView(r, NULL, kEmptyStr,
45f75a7bf5SStephan Aßmus 		B_FOLLOW_RIGHT | B_FOLLOW_TOP_BOTTOM);
46f75a7bf5SStephan Aßmus 	AddChild(fSizeView);
47f75a7bf5SStephan Aßmus 
48f75a7bf5SStephan Aßmus 	// Vertical divider
49f75a7bf5SStephan Aßmus 	r.right = r.left - kSmallHMargin;
50f75a7bf5SStephan Aßmus 	r.left = r.right - 1.0;
51f75a7bf5SStephan Aßmus 	AddChild(new BBox(r.InsetByCopy(0, -5), NULL,
52f75a7bf5SStephan Aßmus 		B_FOLLOW_RIGHT | B_FOLLOW_TOP_BOTTOM));
53f75a7bf5SStephan Aßmus 
54f75a7bf5SStephan Aßmus 	// Count display
55f75a7bf5SStephan Aßmus 	char testLabel[256];
56f75a7bf5SStephan Aßmus 	sprintf(testLabel, kManyFiles, 999999);
57f75a7bf5SStephan Aßmus 	r.right = r.left - kSmallHMargin;
58f75a7bf5SStephan Aßmus 	r.left = r.right - (StringWidth(testLabel) + kSmallHMargin);
59f75a7bf5SStephan Aßmus 	fCountView = new BStringView(r, NULL, kEmptyStr,
60f75a7bf5SStephan Aßmus 		B_FOLLOW_RIGHT|B_FOLLOW_TOP_BOTTOM);
61f75a7bf5SStephan Aßmus 	AddChild(fCountView);
62f75a7bf5SStephan Aßmus 
63f75a7bf5SStephan Aßmus 	// Vertical divider
64f75a7bf5SStephan Aßmus 	r.right = r.left - kSmallHMargin;
65f75a7bf5SStephan Aßmus 	r.left = r.right - 1.0;
66f75a7bf5SStephan Aßmus 	AddChild(new BBox(r.InsetByCopy(0, -5), NULL,
67f75a7bf5SStephan Aßmus 		B_FOLLOW_RIGHT | B_FOLLOW_TOP_BOTTOM));
68f75a7bf5SStephan Aßmus 
69f75a7bf5SStephan Aßmus 	// Path display
70f75a7bf5SStephan Aßmus 	r.right = r.left - kSmallHMargin;
71f75a7bf5SStephan Aßmus 	r.left = kSmallHMargin;
72f75a7bf5SStephan Aßmus 	fPathView = new BStringView(r, NULL, kEmptyStr,
73f75a7bf5SStephan Aßmus 		B_FOLLOW_LEFT_RIGHT|B_FOLLOW_TOP_BOTTOM);
74f75a7bf5SStephan Aßmus 	AddChild(fPathView);
75f75a7bf5SStephan Aßmus 
76f75a7bf5SStephan Aßmus 	// Horizontal divider
77f75a7bf5SStephan Aßmus 	r = Bounds();
78f75a7bf5SStephan Aßmus 	r.bottom = r.top + 1.0;
79f75a7bf5SStephan Aßmus 	AddChild(new BBox(r.InsetByCopy(-5, 0), NULL,
80f75a7bf5SStephan Aßmus 		B_FOLLOW_LEFT_RIGHT | B_FOLLOW_BOTTOM, B_WILL_DRAW));
81f75a7bf5SStephan Aßmus }
82f75a7bf5SStephan Aßmus 
83f75a7bf5SStephan Aßmus 
84f75a7bf5SStephan Aßmus StatusView::~StatusView()
85f75a7bf5SStephan Aßmus {
86f75a7bf5SStephan Aßmus }
87f75a7bf5SStephan Aßmus 
88f75a7bf5SStephan Aßmus 
89f75a7bf5SStephan Aßmus void
90f75a7bf5SStephan Aßmus StatusView::Show(const FileInfo* info)
91f75a7bf5SStephan Aßmus {
92f75a7bf5SStephan Aßmus 	if (info == fCurrentFileInfo)
93f75a7bf5SStephan Aßmus 		return;
94f75a7bf5SStephan Aßmus 
95f75a7bf5SStephan Aßmus 	fCurrentFileInfo = info;
96f75a7bf5SStephan Aßmus 
97f75a7bf5SStephan Aßmus 	if (info == NULL) {
98f75a7bf5SStephan Aßmus 		fPathView->SetText(kEmptyStr);
99f75a7bf5SStephan Aßmus 		fSizeView->SetText(kEmptyStr);
100f75a7bf5SStephan Aßmus 		fCountView->SetText(kEmptyStr);
101f75a7bf5SStephan Aßmus 		return;
102f75a7bf5SStephan Aßmus 	}
103f75a7bf5SStephan Aßmus 
104f75a7bf5SStephan Aßmus 	if (!info->pseudo) {
105f75a7bf5SStephan Aßmus 		BNode node(&info->ref);
106f75a7bf5SStephan Aßmus 		if (node.InitCheck() != B_OK) {
107f75a7bf5SStephan Aßmus 			fPathView->SetText(kStrUnavail);
108f75a7bf5SStephan Aßmus 			fSizeView->SetText(kEmptyStr);
109f75a7bf5SStephan Aßmus 			fCountView->SetText(kEmptyStr);
110f75a7bf5SStephan Aßmus 			return;
111f75a7bf5SStephan Aßmus 		}
112f75a7bf5SStephan Aßmus 	}
113f75a7bf5SStephan Aßmus 
114f75a7bf5SStephan Aßmus 	float viewWidth = fPathView->Bounds().Width();
115f75a7bf5SStephan Aßmus 	string path;
116f75a7bf5SStephan Aßmus 	info->GetPath(path);
117f75a7bf5SStephan Aßmus 	BString pathLabel = path.c_str();
118f75a7bf5SStephan Aßmus 	be_plain_font->TruncateString(&pathLabel, B_TRUNCATE_BEGINNING, viewWidth);
119f75a7bf5SStephan Aßmus 	fPathView->SetText(pathLabel.String());
120f75a7bf5SStephan Aßmus 
121f75a7bf5SStephan Aßmus 	char label[B_PATH_NAME_LENGTH];
122f75a7bf5SStephan Aßmus 	size_to_string(info->size, label);
123f75a7bf5SStephan Aßmus 	fSizeView->SetText(label);
124f75a7bf5SStephan Aßmus 
125f75a7bf5SStephan Aßmus 	if (info->count > 0) {
126f75a7bf5SStephan Aßmus 		char label[256];
127f75a7bf5SStephan Aßmus 		sprintf(label, (info->count == 1) ? kOneFile : kManyFiles, info->count);
128f75a7bf5SStephan Aßmus 		fCountView->SetText(label);
129f75a7bf5SStephan Aßmus 	} else {
130f75a7bf5SStephan Aßmus 		fCountView->SetText(kEmptyStr);
131f75a7bf5SStephan Aßmus 	}
132f75a7bf5SStephan Aßmus }
133f75a7bf5SStephan Aßmus 
134f75a7bf5SStephan Aßmus 
135