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