2 * Copyright 2007-2015, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
7 #include "ProgressWindow.h"
12 #include <MessageRunner.h>
14 #include <StatusBar.h>
19 #undef B_TRANSLATION_CONTEXT
20 #define B_TRANSLATION_CONTEXT "ProgressWindow"
23 static const uint32 kMsgShow
= 'show';
26 ProgressWindow::ProgressWindow(BWindow
* referenceWindow
,
27 BMessage
* abortMessage
)
28 : BWindow(BRect(0, 0, 250, 100), B_TRANSLATE("Progress monitor"),
29 B_MODAL_WINDOW_LOOK
, B_FLOATING_APP_WINDOW_FEEL
,
30 B_NOT_ZOOMABLE
| B_NOT_RESIZABLE
| B_ASYNCHRONOUS_CONTROLS
31 | B_NO_WORKSPACE_ACTIVATION
),
34 BRect rect
= Bounds();
36 BView
*view
= new BView(rect
, NULL
, B_FOLLOW_ALL
, B_WILL_DRAW
);
37 view
->SetViewUIColor(B_PANEL_BACKGROUND_COLOR
);
40 rect
= view
->Bounds().InsetByCopy(8, 8);
41 fStatusBar
= new BStatusBar(rect
, "status", NULL
, NULL
);
43 fStatusBar
->GetPreferredSize(&width
, &height
);
44 fStatusBar
->ResizeTo(rect
.Width(), height
);
45 fStatusBar
->SetResizingMode(B_FOLLOW_TOP
| B_FOLLOW_LEFT_RIGHT
);
46 view
->AddChild(fStatusBar
);
48 if (abortMessage
!= NULL
&& referenceWindow
) {
49 rect
.top
+= height
+ 8;
50 BButton
* button
= new BButton(rect
, "abort", B_TRANSLATE("Abort"),
52 button
->ResizeToPreferred();
53 button
->MoveBy((rect
.Width() - button
->Bounds().Width()) / 2, 0);
54 view
->AddChild(button
);
55 button
->SetTarget(referenceWindow
);
56 height
= button
->Frame().bottom
;
59 ResizeTo(Bounds().Width(), height
+ 8);
60 _Center(referenceWindow
);
65 ProgressWindow::~ProgressWindow()
72 ProgressWindow::_Center(BWindow
* referenceWindow
)
75 if (referenceWindow
!= NULL
)
76 frame
= referenceWindow
->Frame();
78 frame
= BScreen().Frame();
80 MoveTo(frame
.left
+ (frame
.Width() - Bounds().Width()) / 2,
81 frame
.top
+ (frame
.Height() - Bounds().Height()) / 2);
85 ProgressWindow::Start(BWindow
* referenceWindow
)
89 _Center(referenceWindow
);
91 if (referenceWindow
!= NULL
)
92 SetWorkspaces(referenceWindow
->Workspaces());
94 fRetrievedUpdate
= false;
95 fRetrievedShow
= false;
98 BMessage
show(kMsgShow
);
99 fRunner
= new BMessageRunner(this, &show
, 1000000, 1);
104 ProgressWindow::Stop()
117 ProgressWindow::MessageReceived(BMessage
*message
)
119 switch (message
->what
) {
121 if (fRetrievedUpdate
&& IsHidden()) {
126 fRetrievedShow
= true;
129 case kMsgProgressStatusUpdate
:
131 if (message
->FindFloat("percent", &percent
) == B_OK
)
132 fStatusBar
->Update(percent
- fStatusBar
->CurrentValue());
135 if (message
->FindString("message", &text
) == B_OK
)
136 fStatusBar
->SetText(text
);
138 fRetrievedUpdate
= true;
140 if (fRetrievedShow
&& IsHidden()) {
147 BWindow::MessageReceived(message
);