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