xref: /haiku/src/apps/diskusage/InfoWindow.cpp (revision ca8ed5ea660fb6275799a3b7f138b201c41a667b)
1 /*
2  * Copyright (c) 2008 Stephan Aßmus <superstippi@gmx.de>. All rights reserved.
3  * Distributed under the terms of the MIT/X11 license.
4  *
5  * Copyright (c) 1999 Mike Steed. You are free to use and distribute this software
6  * as long as it is accompanied by it's documentation and this copyright notice.
7  * The software comes with no warranty, etc.
8  */
9 
10 
11 #include "InfoWindow.h"
12 
13 #include <stdio.h>
14 #include <string.h>
15 #include <time.h>
16 #include <utility>
17 #include <vector>
18 
19 #include <Catalog.h>
20 #include <MessageFormat.h>
21 #include <StringForSize.h>
22 #include <StringView.h>
23 #include <Bitmap.h>
24 #include <NodeInfo.h>
25 
26 #include "DiskUsage.h"
27 #include "Snapshot.h"
28 
29 using std::string;
30 using std::vector;
31 using std::pair;
32 
33 #undef B_TRANSLATION_CONTEXT
34 #define B_TRANSLATION_CONTEXT "Info Window"
35 
36 LeftView::LeftView(BRect frame, BBitmap* icon)
37 	:
38 	BView(frame, NULL, B_FOLLOW_NONE, B_WILL_DRAW),
39 	fIcon(icon)
40 {
41 	SetViewColor(tint_color(kWindowColor, B_LIGHTEN_1_TINT));
42 }
43 
44 
45 LeftView::~LeftView()
46 {
47 	delete fIcon;
48 }
49 
50 
51 void
52 LeftView::Draw(BRect updateRect)
53 {
54 	float right = Bounds().Width() - kSmallHMargin;
55 	BRect iconRect(right - 31.0, kSmallVMargin, right, kSmallVMargin + 31.0);
56 	if (updateRect.Intersects(iconRect)) {
57 		SetDrawingMode(B_OP_OVER);
58 		DrawBitmap(fIcon, iconRect);
59 	}
60 }
61 
62 
63 InfoWin::InfoWin(BPoint p, FileInfo *f, BWindow* parent)
64 	: BWindow(BRect(p, p), kEmptyStr, B_FLOATING_WINDOW_LOOK,
65 		B_FLOATING_SUBSET_WINDOW_FEEL,
66 		B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_NOT_MINIMIZABLE)
67 {
68 	AddToSubset(parent);
69 
70 	typedef pair<string, string> Item;
71 	typedef vector<Item> InfoList;
72 
73 	BString stringTitle("%refName% info");
74 	stringTitle.ReplaceFirst("%refName%", f->ref.name);
75 	SetTitle(stringTitle.String());
76 
77 	InfoList info;
78 	Item item;
79 
80 	// Size
81 	BString name;
82 	if (f->count > 0) {
83 		// This is a directory, include file count information
84 		static BMessageFormat format(B_TRANSLATE(
85 		"%size% in {0, plural, one{# file} other{# files}}"));
86 
87 		format.Format(name, f->count);
88 	} else
89 		name = "%size%";
90 
91 	char tmp[B_PATH_NAME_LENGTH] = { 0 };
92 	string_for_size(f->size, tmp, sizeof(tmp));
93 	name.ReplaceFirst("%size%", tmp);
94 
95 	info.push_back(Item(B_TRANSLATE_MARK("Size"), name.String()));
96 
97 	// Created & modified dates
98 	BEntry entry(&f->ref);
99 	time_t t;
100 	entry.GetCreationTime(&t);
101 	strftime(tmp, 64, B_TRANSLATE("%a, %d %b %Y, %r"), localtime(&t));
102 	info.push_back(Item(B_TRANSLATE("Created"), tmp));
103 	entry.GetModificationTime(&t);
104 	strftime(tmp, 64, B_TRANSLATE("%a, %d %b %Y, %r"), localtime(&t));
105 	info.push_back(Item(B_TRANSLATE("Modified"), tmp));
106 
107 	// Kind
108 	BMimeType* type = f->Type();
109 	type->GetShortDescription(tmp);
110 	info.push_back(Item(B_TRANSLATE("Kind"), tmp));
111 	delete type;
112 
113 	// Path
114 	string path;
115 	f->GetPath(path);
116 	info.push_back(Item(B_TRANSLATE("Path"), path));
117 
118 	// Icon
119 	BBitmap *icon = new BBitmap(BRect(0.0, 0.0, 31.0, 31.0), B_RGBA32);
120 	entry_ref ref;
121 	entry.GetRef(&ref);
122 	BNodeInfo::GetTrackerIcon(&ref, icon, B_LARGE_ICON);
123 
124 	// Compute the window size and add the views.
125 	BFont smallFont(be_plain_font);
126 	smallFont.SetSize(floorf(smallFont.Size() * 0.95));
127 
128 	struct font_height fh;
129 	smallFont.GetHeight(&fh);
130 	float fontHeight = fh.ascent + fh.descent + fh.leading;
131 
132 	float leftWidth = 32.0;
133 	float rightWidth = 0.0;
134 	InfoList::iterator i = info.begin();
135 	while (i != info.end()) {
136 		float w = smallFont.StringWidth((*i).first.c_str())
137 			+ 2.0 * kSmallHMargin;
138 		leftWidth = max_c(leftWidth, w);
139 		w = smallFont.StringWidth((*i).second.c_str()) + 2.0 * kSmallHMargin;
140 		rightWidth = max_c(rightWidth, w);
141 		i++;
142 	}
143 
144 	float winHeight = 32.0 + 4.0 * kSmallVMargin + 5.0 * (fontHeight
145 		+ kSmallVMargin);
146 	float winWidth = leftWidth + rightWidth;
147 	ResizeTo(winWidth, winHeight);
148 
149 	LeftView *leftView = new LeftView(BRect(0.0, 0.0, leftWidth, winHeight),
150 		icon);
151 	BView *rightView = new BView(
152 		BRect(leftWidth + 1.0, 0.0, winWidth, winHeight), NULL,
153 		B_FOLLOW_NONE, B_WILL_DRAW);
154 
155 	AddChild(leftView);
156 	AddChild(rightView);
157 
158 	BStringView *sv = new BStringView(
159 		BRect(kSmallHMargin, kSmallVMargin, rightView->Bounds().Width(),
160 		kSmallVMargin + 30.0), NULL, f->ref.name, B_FOLLOW_ALL);
161 
162 	BFont largeFont(be_plain_font);
163 	largeFont.SetSize(ceilf(largeFont.Size() * 1.1));
164 	sv->SetFont(&largeFont);
165 	rightView->AddChild(sv);
166 
167 	float y = 32.0 + 4.0 * kSmallVMargin;
168 	i = info.begin();
169 	while (i != info.end()) {
170 		sv = new BStringView(
171 			BRect(kSmallHMargin, y, leftView->Bounds().Width(),
172 			y + fontHeight), NULL, (*i).first.c_str());
173 		sv->SetFont(&smallFont);
174 		sv->SetAlignment(B_ALIGN_RIGHT);
175 		sv->SetHighColor(kBasePieColor[1]); // arbitrary
176 		leftView->AddChild(sv);
177 
178 		sv = new BStringView(
179 			BRect(kSmallHMargin, y, rightView->Bounds().Width(),
180 			y + fontHeight), NULL, (*i).second.c_str());
181 		sv->SetFont(&smallFont);
182 		rightView->AddChild(sv);
183 
184 		y += fontHeight + kSmallVMargin;
185 		i++;
186 	}
187 
188 	Show();
189 }
190 
191 
192 InfoWin::~InfoWin()
193 {
194 }
195