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