xref: /haiku/src/apps/showimage/ProgressWindow.cpp (revision 9760dcae2038d47442f4658c2575844c6cf92c40)
1 /*
2  * Copyright 2007-2009, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "ProgressWindow.h"
8 
9 #include <stdio.h>
10 
11 #include <Autolock.h>
12 #include <MessageRunner.h>
13 #include <Screen.h>
14 #include <StatusBar.h>
15 
16 #include "ShowImageConstants.h"
17 
18 
19 static const uint32 kMsgShow = 'show';
20 
21 
22 ProgressWindow::ProgressWindow(BWindow* referenceWindow, bool center)
23 	:
24 	BWindow(BRect(0, 0, 250, 100), "Progress monitor",
25 		B_MODAL_WINDOW_LOOK, B_FLOATING_APP_WINDOW_FEEL,
26 		B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS),
27 	fRunner(NULL)
28 {
29 	BRect rect = Bounds();
30 
31 	BView *view = new BView(rect, NULL, B_FOLLOW_ALL, B_WILL_DRAW);
32 	view->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
33 	AddChild(view);
34 
35 	rect = view->Bounds().InsetByCopy(5, 5);
36 	fStatusBar = new BStatusBar(rect, "status", NULL, NULL);
37 	float width, height;
38 	fStatusBar->GetPreferredSize(&width, &height);
39 	fStatusBar->ResizeTo(rect.Width(), height);
40 	fStatusBar->SetResizingMode(B_FOLLOW_TOP | B_FOLLOW_LEFT_RIGHT);
41 	view->AddChild(fStatusBar);
42 
43 	BScreen screen(referenceWindow);
44 	if (!center) {
45 		ResizeTo(Bounds().Width(), height + 9);
46 		// TODO: frame width!
47 		MoveTo(screen.Frame().left + 5,
48 			screen.Frame().bottom - Bounds().Height() - 5);
49 	} else
50 		CenterIn(screen.Frame());
51 
52 	Run();
53 }
54 
55 
56 ProgressWindow::~ProgressWindow()
57 {
58 	delete fRunner;
59 }
60 
61 
62 void
63 ProgressWindow::Start()
64 {
65 	BAutolock _(this);
66 
67 	fRetrievedUpdate = false;
68 	fRetrievedShow = false;
69 	delete fRunner;
70 
71 	BMessage show(kMsgShow);
72 	fRunner = new BMessageRunner(this, &show, 1000000, 1);
73 }
74 
75 
76 void
77 ProgressWindow::Stop()
78 {
79 	BAutolock _(this);
80 
81 	delete fRunner;
82 	fRunner = NULL;
83 
84 	if (!IsHidden())
85 		Hide();
86 }
87 
88 
89 void
90 ProgressWindow::MessageReceived(BMessage *message)
91 {
92 	switch (message->what) {
93 		case kMsgShow:
94 			if (fRetrievedUpdate && IsHidden()) {
95 				Show();
96 				Minimize(false);
97 			}
98 
99 			fRetrievedShow = true;
100 			break;
101 
102 		case kMsgProgressStatusUpdate:
103 			float percent;
104 			if (message->FindFloat("percent", &percent) == B_OK)
105 				fStatusBar->Update(percent - fStatusBar->CurrentValue());
106 
107 			const char *text;
108 			if (message->FindString("message", &text) == B_OK)
109 				fStatusBar->SetText(text);
110 
111 			fRetrievedUpdate = true;
112 
113 			if (fRetrievedShow && IsHidden()) {
114 				Show();
115 				Minimize(false);
116 			}
117 			break;
118 
119 		default:
120 			BWindow::MessageReceived(message);
121 	}
122 }
123 
124