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