BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / apps / sudoku / ProgressWindow.cpp
blob50cb6402eae48907577601a63dc2f51d74d2270d
1 /*
2 * Copyright 2007-2015, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "ProgressWindow.h"
9 #include <Autolock.h>
10 #include <Button.h>
11 #include <Catalog.h>
12 #include <MessageRunner.h>
13 #include <Screen.h>
14 #include <StatusBar.h>
16 #include <stdio.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),
32 fRunner(NULL)
34 BRect rect = Bounds();
36 BView *view = new BView(rect, NULL, B_FOLLOW_ALL, B_WILL_DRAW);
37 view->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
38 AddChild(view);
40 rect = view->Bounds().InsetByCopy(8, 8);
41 fStatusBar = new BStatusBar(rect, "status", NULL, NULL);
42 float width, height;
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"),
51 abortMessage);
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);
61 Run();
65 ProgressWindow::~ProgressWindow()
67 delete fRunner;
71 void
72 ProgressWindow::_Center(BWindow* referenceWindow)
74 BRect frame;
75 if (referenceWindow != NULL)
76 frame = referenceWindow->Frame();
77 else
78 frame = BScreen().Frame();
80 MoveTo(frame.left + (frame.Width() - Bounds().Width()) / 2,
81 frame.top + (frame.Height() - Bounds().Height()) / 2);
84 void
85 ProgressWindow::Start(BWindow* referenceWindow)
87 BAutolock _(this);
89 _Center(referenceWindow);
91 if (referenceWindow != NULL)
92 SetWorkspaces(referenceWindow->Workspaces());
94 fRetrievedUpdate = false;
95 fRetrievedShow = false;
96 delete fRunner;
98 BMessage show(kMsgShow);
99 fRunner = new BMessageRunner(this, &show, 1000000, 1);
103 void
104 ProgressWindow::Stop()
106 BAutolock _(this);
108 delete fRunner;
109 fRunner = NULL;
111 if (!IsHidden())
112 Hide();
116 void
117 ProgressWindow::MessageReceived(BMessage *message)
119 switch (message->what) {
120 case kMsgShow:
121 if (fRetrievedUpdate && IsHidden()) {
122 Show();
123 Minimize(false);
126 fRetrievedShow = true;
127 break;
129 case kMsgProgressStatusUpdate:
130 float percent;
131 if (message->FindFloat("percent", &percent) == B_OK)
132 fStatusBar->Update(percent - fStatusBar->CurrentValue());
134 const char *text;
135 if (message->FindString("message", &text) == B_OK)
136 fStatusBar->SetText(text);
138 fRetrievedUpdate = true;
140 if (fRetrievedShow && IsHidden()) {
141 Show();
142 Minimize(false);
144 break;
146 default:
147 BWindow::MessageReceived(message);