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