1 | // $Id: TRInfoWindow.cpp 261 2006-05-29 21:27:31Z titer $ |
---|
2 | |
---|
3 | #include "TRInfoWindow.h" |
---|
4 | |
---|
5 | #include <Box.h> |
---|
6 | #include <String.h> |
---|
7 | #include <TextView.h> |
---|
8 | #include <ScrollView.h> |
---|
9 | |
---|
10 | #include <malloc.h> |
---|
11 | #include <stdio.h> |
---|
12 | |
---|
13 | |
---|
14 | TRInfoWindow::TRInfoWindow(tr_stat_t status) : BWindow(BRect(0, 0, 250, 175), "Info", |
---|
15 | B_FLOATING_WINDOW, B_ASYNCHRONOUS_CONTROLS | /*B_NOT_RESIZABLE*/ B_NOT_ZOOMABLE, |
---|
16 | B_CURRENT_WORKSPACE) |
---|
17 | { |
---|
18 | BRect viewRect = Bounds(); |
---|
19 | |
---|
20 | // Single header, Font Size 14. |
---|
21 | BFont headerFont(be_bold_font); |
---|
22 | headerFont.SetSize(14.0f); |
---|
23 | font_height fh; |
---|
24 | headerFont.GetHeight(&fh); |
---|
25 | if (headerFont.StringWidth(status.info.name) > Bounds().Width() - 10) { |
---|
26 | ResizeBy(headerFont.StringWidth(status.info.name) - Bounds().Width() + 10, 0); |
---|
27 | } |
---|
28 | |
---|
29 | viewRect = Bounds(); |
---|
30 | viewRect.bottom = fh.ascent + fh.descent; |
---|
31 | BStringView *strView = new BStringView(viewRect, "header", status.info.name, B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP); |
---|
32 | strView->SetFont(&headerFont); |
---|
33 | strView->SetAlignment(B_ALIGN_CENTER); |
---|
34 | |
---|
35 | viewRect.left = 5; |
---|
36 | viewRect.top = 10; |
---|
37 | viewRect.bottom = Bounds().bottom - 5; |
---|
38 | BTextView *txtView = new BTextView(viewRect, "infoText", viewRect, B_FOLLOW_LEFT | B_FOLLOW_TOP); |
---|
39 | txtView->MakeEditable(false); |
---|
40 | |
---|
41 | BString strTracker(status.info.trackerAddress); |
---|
42 | strTracker << ":" << status.info.trackerPort; |
---|
43 | |
---|
44 | BString strPieceSize(""); |
---|
45 | StringForFileSize(status.info.pieceSize, &strPieceSize); |
---|
46 | |
---|
47 | BString strTotalSize(""); |
---|
48 | StringForFileSize(status.info.totalSize, &strTotalSize); |
---|
49 | |
---|
50 | BString strDownloaded(""); |
---|
51 | StringForFileSize(status.downloaded, &strDownloaded); |
---|
52 | |
---|
53 | BString strUploaded(""); |
---|
54 | StringForFileSize(status.uploaded, &strUploaded); |
---|
55 | |
---|
56 | BString info(""); |
---|
57 | info << "Tracker: " << strTracker << "\n" |
---|
58 | << "Announce: " << status.info.trackerAnnounce << "\n" |
---|
59 | << "Piece Size: " << strPieceSize << "\n" |
---|
60 | << "Pieces: " << status.info.pieceCount << "\n" |
---|
61 | << "Total Size: " << strTotalSize << "\n" |
---|
62 | << "\n" |
---|
63 | << "Folder: " << status.folder << "\n" |
---|
64 | << "Downloaded: " << strDownloaded << "\n" |
---|
65 | << "Uploaded: " << strUploaded << "\n"; |
---|
66 | txtView->SetText(info.String()); |
---|
67 | |
---|
68 | Lock(); |
---|
69 | AddChild(strView); |
---|
70 | AddChild(txtView); |
---|
71 | Unlock(); |
---|
72 | } |
---|
73 | |
---|
74 | TRInfoWindow::~TRInfoWindow() { |
---|
75 | |
---|
76 | } |
---|
77 | |
---|
78 | void TRInfoWindow::FrameResized(float width, float height) { |
---|
79 | } |
---|
80 | |
---|
81 | void TRInfoWindow::StringForFileSize(uint64_t size, BString *str) { |
---|
82 | char *s = (char*)calloc(512, sizeof(char)); |
---|
83 | if (size < 1024) { |
---|
84 | sprintf(s, "%lld bytes", size); |
---|
85 | } else if (size < 1048576) { |
---|
86 | sprintf(s, "%lld.%lld KB", size / 1024, (size % 1024 ) / 103); |
---|
87 | } else if (size < 1073741824 ) { |
---|
88 | sprintf(s, "%lld.%lld MB", size / 1048576, (size % 1048576) / 104858); |
---|
89 | } else { |
---|
90 | sprintf(s, "%lld.%lld GB", size / 1073741824, (size % 1073741824) / 107374183); |
---|
91 | } |
---|
92 | |
---|
93 | str->SetTo(s); |
---|
94 | free(s); |
---|
95 | } |
---|