xref: /haiku/src/apps/diskusage/StatusView.cpp (revision c2f0a314a012bea8e4ebb35b8ce9e1a85c798727)
1 /*
2  * Copyright (c) 2010 Philippe St-Pierre <stpere@gmail.com>. All rights reserved.
3  * Copyright (c) 2008 Stephan Aßmus <superstippi@gmx.de>. All rights reserved.
4  * Distributed under the terms of the MIT/X11 license.
5  *
6  * Copyright (c) 1999 Mike Steed. You are free to use and distribute this software
7  * as long as it is accompanied by it's documentation and this copyright notice.
8  * The software comes with no warranty, etc.
9  */
10 
11 
12 #include "StatusView.h"
13 
14 #include <math.h>
15 #include <stdio.h>
16 
17 #include <Box.h>
18 #include <Button.h>
19 #include <Node.h>
20 #include <String.h>
21 #include <StringView.h>
22 
23 #include <LayoutBuilder.h>
24 
25 #include "Common.h"
26 #include "Scanner.h"
27 
28 
29 StatusView::StatusView()
30 	:
31 	BView(NULL, B_WILL_DRAW),
32 	fCurrentFileInfo(NULL)
33 {
34 	SetViewColor(kPieBGColor);
35 	SetLowColor(kPieBGColor);
36 
37 	fSizeView = new BStringView(NULL, kEmptyStr);
38 	fSizeView->SetExplicitMinSize(BSize(StringWidth("9999.99 GB"),
39 		B_SIZE_UNSET));
40 	fSizeView->SetExplicitMaxSize(BSize(StringWidth("9999.99 GB"),
41 		B_SIZE_UNSET));
42 
43 	char testLabel[256];
44 	sprintf(testLabel, kManyFiles, 999999);
45 
46 	fCountView = new BStringView(NULL, kEmptyStr);
47 	fCountView->SetExplicitMinSize(BSize(StringWidth(testLabel), B_SIZE_UNSET));
48 	fCountView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
49 
50 	fPathView = new BStringView(NULL, kEmptyStr);
51 	fPathView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
52 
53 	fRefreshBtn = new BButton(NULL, kStrScan, new BMessage(kBtnRescan));
54 
55 	fRefreshBtn->SetExplicitMaxSize(BSize(B_SIZE_UNSET, B_SIZE_UNLIMITED));
56 
57 	BBox* divider1 = new BBox(BRect(), B_EMPTY_STRING, B_FOLLOW_ALL_SIDES,
58 		B_WILL_DRAW | B_FRAME_EVENTS, B_FANCY_BORDER);
59 
60 	BBox* divider2 = new BBox(BRect(), B_EMPTY_STRING, B_FOLLOW_ALL_SIDES,
61 		B_WILL_DRAW | B_FRAME_EVENTS, B_FANCY_BORDER);
62 
63 	divider1->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 1));
64 	divider2->SetExplicitMaxSize(BSize(1, B_SIZE_UNLIMITED));
65 
66 	SetLayout(new BGroupLayout(B_VERTICAL));
67 
68 	AddChild(BLayoutBuilder::Group<>(B_HORIZONTAL)
69 		.AddGroup(B_VERTICAL)
70 			.Add(fPathView)
71 			.Add(divider1)
72 			.AddGroup(B_HORIZONTAL)
73 				.Add(fCountView)
74 				.Add(divider2)
75 				.Add(fSizeView)
76 				.End()
77 			.End()
78 		.AddStrut(kSmallHMargin)
79 		.Add(fRefreshBtn)
80 		.SetInsets(kSmallHMargin, kSmallVMargin, kSmallVMargin, kSmallVMargin)
81 	);
82 }
83 
84 
85 StatusView::~StatusView()
86 {
87 }
88 
89 
90 void
91 StatusView::SetRescanEnabled(bool enabled)
92 {
93 	fRefreshBtn->SetEnabled(enabled);
94 }
95 
96 
97 void
98 StatusView::SetBtnLabel(const char* label)
99 {
100 	fRefreshBtn->SetLabel(label);
101 }
102 
103 
104 void
105 StatusView::ShowInfo(const FileInfo* info)
106 {
107 	if (info == fCurrentFileInfo)
108 		return;
109 
110 	fCurrentFileInfo = info;
111 
112 	if (info == NULL) {
113 		fPathView->SetText(kEmptyStr);
114 		fSizeView->SetText(kEmptyStr);
115 		fCountView->SetText(kEmptyStr);
116 		return;
117 	}
118 
119 	if (!info->pseudo) {
120 		BNode node(&info->ref);
121 		if (node.InitCheck() != B_OK) {
122 			fPathView->SetText(kStrUnavail);
123 			fSizeView->SetText(kEmptyStr);
124 			fCountView->SetText(kEmptyStr);
125 			return;
126 		}
127 	}
128 
129 	float viewWidth = fPathView->Bounds().Width();
130 	string path;
131 	info->GetPath(path);
132 	BString pathLabel = path.c_str();
133 	be_plain_font->TruncateString(&pathLabel, B_TRUNCATE_BEGINNING, viewWidth);
134 	fPathView->SetText(pathLabel.String());
135 
136 	char label[B_PATH_NAME_LENGTH];
137 	size_to_string(info->size, label);
138 	fSizeView->SetText(label);
139 
140 	if (info->count > 0) {
141 		char label[256];
142 		sprintf(label, (info->count == 1) ? kOneFile : kManyFiles, info->count);
143 		fCountView->SetText(label);
144 	} else {
145 		fCountView->SetText(kEmptyStr);
146 	}
147 }
148