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