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