1a58eacecSAxel Dörfler/*
2e2069723SAxel Dörfler * Copyright 2007-2010, Axel D��rfler, axeld@pinc-software.de.
3a58eacecSAxel Dörfler * Distributed under the terms of the MIT License.
4a58eacecSAxel Dörfler */
5a58eacecSAxel Dörfler
68e52aee9SAxel Dörfler
7a58eacecSAxel Dörfler#include "ProgressWindow.h"
88c1ad886SRyan Leavengood
98c1ad886SRyan Leavengood#include <stdio.h>
10e2069723SAxel Dörfler#include <string.h>
11a58eacecSAxel Dörfler
12a58eacecSAxel Dörfler#include <Autolock.h>
1322719833SStephan Aßmus#include <Catalog.h>
1422719833SStephan Aßmus#include <Locale.h>
15a58eacecSAxel Dörfler#include <MessageRunner.h>
16a58eacecSAxel Dörfler#include <Screen.h>
17a58eacecSAxel Dörfler#include <StatusBar.h>
18a58eacecSAxel Dörfler
198c1ad886SRyan Leavengood#include "ShowImageConstants.h"
20a58eacecSAxel Dörfler
21a58eacecSAxel Dörfler
22a58eacecSAxel Dörflerstatic const uint32 kMsgShow = 'show';
23a58eacecSAxel Dörfler
24546208a5SOliver Tappe#undef B_TRANSLATION_CONTEXT
25546208a5SOliver Tappe#define B_TRANSLATION_CONTEXT "ProgressWindow"
2622719833SStephan Aßmus
27a58eacecSAxel Dörfler
28e2069723SAxel DörflerProgressWindow::ProgressWindow()
298c1ad886SRyan Leavengood	:
30905c8c8cSMatt Madia	BWindow(BRect(0, 0, 250, 100), B_TRANSLATE("Progress monitor"),
31e2069723SAxel Dörfler		B_MODAL_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,// B_FLOATING_APP_WINDOW_FEEL,
32e2069723SAxel Dörfler		// TODO: a bug in the app_server prevents an initial floating-app feel
33e2069723SAxel Dörfler		// to work correctly; the window will then not be visible for the first
34e2069723SAxel Dörfler		// image, even though it's later set to normal feel in that case.
35a58eacecSAxel Dörfler		B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS),
36a58eacecSAxel Dörfler	fRunner(NULL)
37a58eacecSAxel Dörfler{
38a58eacecSAxel Dörfler	BRect rect = Bounds();
39a58eacecSAxel Dörfler
40537a273cSHumdinger	BView* view = new BView(rect, NULL, B_FOLLOW_ALL, B_WILL_DRAW);
41fa19dd44Slooncraz	view->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
42a58eacecSAxel Dörfler	AddChild(view);
43a58eacecSAxel Dörfler
44a58eacecSAxel Dörfler	rect = view->Bounds().InsetByCopy(5, 5);
45a58eacecSAxel Dörfler	fStatusBar = new BStatusBar(rect, "status", NULL, NULL);
46a58eacecSAxel Dörfler	float width, height;
47a58eacecSAxel Dörfler	fStatusBar->GetPreferredSize(&width, &height);
48a58eacecSAxel Dörfler	fStatusBar->ResizeTo(rect.Width(), height);
49a58eacecSAxel Dörfler	fStatusBar->SetResizingMode(B_FOLLOW_TOP | B_FOLLOW_LEFT_RIGHT);
50a58eacecSAxel Dörfler	view->AddChild(fStatusBar);
51a58eacecSAxel Dörfler
52e2069723SAxel Dörfler	ResizeTo(Bounds().Width(), height + 9);
53a58eacecSAxel Dörfler
54a58eacecSAxel Dörfler	Run();
55a58eacecSAxel Dörfler}
56a58eacecSAxel Dörfler
57a58eacecSAxel Dörfler
58a58eacecSAxel DörflerProgressWindow::~ProgressWindow()
59a58eacecSAxel Dörfler{
60a58eacecSAxel Dörfler	delete fRunner;
61a58eacecSAxel Dörfler}
62a58eacecSAxel Dörfler
63a58eacecSAxel Dörfler
64a58eacecSAxel Dörflervoid
65e2069723SAxel DörflerProgressWindow::Start(BWindow* referenceWindow, bool center)
66a58eacecSAxel Dörfler{
67a58eacecSAxel Dörfler	BAutolock _(this);
68a58eacecSAxel Dörfler
69e2069723SAxel Dörfler	BScreen screen(referenceWindow);
70e2069723SAxel Dörfler	if (!center) {
71e2069723SAxel Dörfler		BMessage settings;
729c34e0e3SRyan Leavengood		GetDecoratorSettings(&settings);
739c34e0e3SRyan Leavengood
749c34e0e3SRyan Leavengood		int32 borderWidth;
759c34e0e3SRyan Leavengood		if (settings.FindInt32("border width", &borderWidth) != B_OK)
769c34e0e3SRyan Leavengood			borderWidth = 5;
77e2069723SAxel Dörfler
78e2069723SAxel Dörfler		MoveTo(screen.Frame().left + borderWidth,
79e2069723SAxel Dörfler			screen.Frame().bottom - Bounds().Height() - borderWidth);
80e2069723SAxel Dörfler	} else
81e2069723SAxel Dörfler		CenterIn(screen.Frame());
82e2069723SAxel Dörfler
83e2069723SAxel Dörfler	SetFeel(referenceWindow->IsHidden()
84e2069723SAxel Dörfler		? B_NORMAL_WINDOW_FEEL : B_FLOATING_APP_WINDOW_FEEL);
85e2069723SAxel Dörfler
86a58eacecSAxel Dörfler	fRetrievedUpdate = false;
87a58eacecSAxel Dörfler	fRetrievedShow = false;
88a58eacecSAxel Dörfler	delete fRunner;
89a58eacecSAxel Dörfler
90a58eacecSAxel Dörfler	BMessage show(kMsgShow);
91a58eacecSAxel Dörfler	fRunner = new BMessageRunner(this, &show, 1000000, 1);
92a58eacecSAxel Dörfler}
93a58eacecSAxel Dörfler
94a58eacecSAxel Dörfler
95a58eacecSAxel Dörflervoid
96a58eacecSAxel DörflerProgressWindow::Stop()
97a58eacecSAxel Dörfler{
98a58eacecSAxel Dörfler	BAutolock _(this);
99a58eacecSAxel Dörfler
100a58eacecSAxel Dörfler	delete fRunner;
101a58eacecSAxel Dörfler	fRunner = NULL;
102a58eacecSAxel Dörfler
103a58eacecSAxel Dörfler	if (!IsHidden())
104a58eacecSAxel Dörfler		Hide();
105a58eacecSAxel Dörfler}
106a58eacecSAxel Dörfler
107a58eacecSAxel Dörfler
108a58eacecSAxel Dörflervoid
109537a273cSHumdingerProgressWindow::MessageReceived(BMessage* message)
110a58eacecSAxel Dörfler{
111a58eacecSAxel Dörfler	switch (message->what) {
112a58eacecSAxel Dörfler		case kMsgShow:
113a58eacecSAxel Dörfler			if (fRetrievedUpdate && IsHidden()) {
114a58eacecSAxel Dörfler				Show();
115a58eacecSAxel Dörfler				Minimize(false);
116a58eacecSAxel Dörfler			}
117a58eacecSAxel Dörfler
118a58eacecSAxel Dörfler			fRetrievedShow = true;
119a58eacecSAxel Dörfler			break;
120a58eacecSAxel Dörfler
121e2069723SAxel Dörfler		case kMsgProgressUpdate:
122a58eacecSAxel Dörfler			float percent;
123a58eacecSAxel Dörfler			if (message->FindFloat("percent", &percent) == B_OK)
124a58eacecSAxel Dörfler				fStatusBar->Update(percent - fStatusBar->CurrentValue());
125a58eacecSAxel Dörfler
126537a273cSHumdinger			const char* text;
127a58eacecSAxel Dörfler			if (message->FindString("message", &text) == B_OK)
128a58eacecSAxel Dörfler				fStatusBar->SetText(text);
129a58eacecSAxel Dörfler
130a58eacecSAxel Dörfler			fRetrievedUpdate = true;
131a58eacecSAxel Dörfler
132a58eacecSAxel Dörfler			if (fRetrievedShow && IsHidden()) {
133a58eacecSAxel Dörfler				Show();
134a58eacecSAxel Dörfler				Minimize(false);
135a58eacecSAxel Dörfler			}
136a58eacecSAxel Dörfler			break;
137a58eacecSAxel Dörfler
138a58eacecSAxel Dörfler		default:
139a58eacecSAxel Dörfler			BWindow::MessageReceived(message);
140a58eacecSAxel Dörfler	}
141a58eacecSAxel Dörfler}
142a58eacecSAxel Dörfler
143