xref: /haiku/src/apps/packageinstaller/PackageStatus.cpp (revision 0562493379cd52eb7103531f895f10bb8e77c085)
1 /*
2  * Copyright (c) 2007, Haiku, Inc.
3  * Distributed under the terms of the MIT license.
4  *
5  * Author:
6  *		Łukasz 'Sil2100' Zemczak <sil2100@vexillium.org>
7  */
8 
9 
10 #include "PackageStatus.h"
11 
12 #include <Autolock.h>
13 
14 #include <GroupLayoutBuilder.h>
15 #include <GroupLayout.h>
16 
17 #include <stdio.h>
18 #include <string.h>
19 
20 // Macro reserved for later localization
21 #define T(x) x
22 
23 
24 StopButton::StopButton()
25 	:	BButton(BRect(0, 0, 22, 18), "stop", "", new BMessage(P_MSG_STOP))
26 {
27 }
28 
29 
30 void
31 StopButton::Draw(BRect updateRect)
32 {
33 	BButton::Draw(updateRect);
34 
35 	updateRect = Bounds();
36 	updateRect.InsetBy((updateRect.Width() - 4) / 2, (updateRect.Height() - 4) / 2);
37 	//updateRect.InsetBy(9, 7);
38 	SetHighColor(0, 0, 0);
39 	FillRect(updateRect);
40 }
41 
42 
43 
44 
45 
46 // #pragma mark -
47 
48 
49 /*PackageStatus::PackageStatus(BHandler *parent, const char *title,
50 		const char *label, const char *trailing)
51 	:	BWindow(BRect(200, 200, 550, 275), title, B_TITLED_WINDOW,
52 			B_NOT_CLOSABLE | B_NOT_RESIZABLE | B_NOT_ZOOMABLE, 0)
53 {
54 	SetSizeLimits(0, 100000, 0, 100000);
55 	fBackground = new BView(Bounds(), "background", B_FOLLOW_NONE, B_WILL_DRAW);
56 	fBackground->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
57 
58 	BRect rect(Bounds());
59 	float width, height;
60 	rect.left += 6;
61 	rect.right -= 40;
62 	rect.top += 6;
63 	rect.bottom = rect.top + 15;
64 	fStatus = new BStatusBar(rect, "status_bar", T("Installing package"));
65 	fStatus->SetBarHeight(12);
66 	fStatus->GetPreferredSize(&width, &height);
67 	fStatus->ResizeTo(fStatus->Frame().Width(), height);
68 	fBackground->AddChild(fStatus);
69 
70 	font_height fontHeight;
71 	fBackground->GetFontHeight(&fontHeight);
72 	BRect frame = fStatus->Frame();
73 	fBackground->ResizeTo(Bounds().Width(), (2 * frame.top) + frame.Height() +
74 			fontHeight.leading + fontHeight.ascent + fontHeight.descent);
75 
76 	rect = Bounds();
77 	rect.left = rect.right - 32;
78 	//rect.right = rect.left + 17;
79 	rect.top += 18;
80 	//rect.bottom = rect.top + 10;
81 	fButton = new StopButton();
82 	fButton->MoveTo(BPoint(rect.left, rect.top));
83 	fButton->ResizeTo(22, 18);
84 	fBackground->AddChild(fButton);
85 
86 	AddChild(fBackground);
87 	fButton->SetTarget(parent);
88 
89 	ResizeTo(Bounds().Width(), fBackground->Bounds().Height());
90 	Run();
91 }*/
92 
93 
94 PackageStatus::PackageStatus(const char *title, const char *label,
95 		const char *trailing)
96 	:	BWindow(BRect(200, 200, 550, 255), title, B_TITLED_WINDOW,
97 			B_NOT_CLOSABLE | B_NOT_RESIZABLE | B_NOT_ZOOMABLE, 0),
98 	fIsStopped(false)
99 {
100 	SetLayout(new BGroupLayout(B_VERTICAL));
101 
102 	fStatus = new BStatusBar("status_bar", T("Installing package"));
103 	fStatus->SetBarHeight(12);
104 
105 	fButton = new StopButton();
106 	fButton->SetExplicitMaxSize(BSize(22, 18));
107 
108 	fBackground = BGroupLayoutBuilder(B_HORIZONTAL)
109 		.AddStrut(5.0f)
110 		.Add(fStatus)
111 		.Add(fButton);
112 	fBackground->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
113 
114 	AddChild(fBackground);
115 
116 	fButton->SetTarget(this);
117 	Run();
118 }
119 
120 
121 PackageStatus::~PackageStatus()
122 {
123 }
124 
125 
126 void
127 PackageStatus::MessageReceived(BMessage *msg)
128 {
129 	switch (msg->what) {
130 		case P_MSG_STOP:
131 			fIsStopped = true;
132 			break;
133 		default:
134 			BWindow::MessageReceived(msg);
135 	}
136 }
137 
138 
139 void
140 PackageStatus::Reset(uint32 stages, const char *label, const char *trailing)
141 {
142 	BAutolock lock(this);
143 
144 	if (lock.IsLocked()) {
145 		fStatus->Reset(label, trailing);
146 		fStatus->SetMaxValue(stages);
147 		fIsStopped = false;
148 	}
149 }
150 
151 
152 void
153 PackageStatus::StageStep(uint32 count, const char *text, const char *trailing)
154 {
155 	BAutolock lock(this);
156 
157 	if (lock.IsLocked()) {
158 		fStatus->Update(count, text, trailing);
159 	}
160 }
161 
162 
163 bool
164 PackageStatus::Stopped()
165 {
166 	BAutolock lock(this);
167 	return fIsStopped;
168 }
169 
170