repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / apps / showimage / ProgressWindow.cpp
blobe22fadb0382e1f3b00524fb44a3316c2c3179008
1 /*
2 * Copyright 2007-2010, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "ProgressWindow.h"
9 #include <stdio.h>
10 #include <string.h>
12 #include <Autolock.h>
13 #include <Catalog.h>
14 #include <Locale.h>
15 #include <MessageRunner.h>
16 #include <Screen.h>
17 #include <StatusBar.h>
19 #include "ShowImageConstants.h"
22 static const uint32 kMsgShow = 'show';
24 #undef B_TRANSLATION_CONTEXT
25 #define B_TRANSLATION_CONTEXT "ProgressWindow"
28 ProgressWindow::ProgressWindow()
30 BWindow(BRect(0, 0, 250, 100), B_TRANSLATE("Progress monitor"),
31 B_MODAL_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,// B_FLOATING_APP_WINDOW_FEEL,
32 // TODO: a bug in the app_server prevents an initial floating-app feel
33 // to work correctly; the window will then not be visible for the first
34 // image, even though it's later set to normal feel in that case.
35 B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS),
36 fRunner(NULL)
38 BRect rect = Bounds();
40 BView* view = new BView(rect, NULL, B_FOLLOW_ALL, B_WILL_DRAW);
41 view->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
42 AddChild(view);
44 rect = view->Bounds().InsetByCopy(5, 5);
45 fStatusBar = new BStatusBar(rect, "status", NULL, NULL);
46 float width, height;
47 fStatusBar->GetPreferredSize(&width, &height);
48 fStatusBar->ResizeTo(rect.Width(), height);
49 fStatusBar->SetResizingMode(B_FOLLOW_TOP | B_FOLLOW_LEFT_RIGHT);
50 view->AddChild(fStatusBar);
52 ResizeTo(Bounds().Width(), height + 9);
54 Run();
58 ProgressWindow::~ProgressWindow()
60 delete fRunner;
64 void
65 ProgressWindow::Start(BWindow* referenceWindow, bool center)
67 BAutolock _(this);
69 BScreen screen(referenceWindow);
70 if (!center) {
71 BMessage settings;
72 GetDecoratorSettings(&settings);
74 int32 borderWidth;
75 if (settings.FindInt32("border width", &borderWidth) != B_OK)
76 borderWidth = 5;
78 MoveTo(screen.Frame().left + borderWidth,
79 screen.Frame().bottom - Bounds().Height() - borderWidth);
80 } else
81 CenterIn(screen.Frame());
83 SetFeel(referenceWindow->IsHidden()
84 ? B_NORMAL_WINDOW_FEEL : B_FLOATING_APP_WINDOW_FEEL);
86 fRetrievedUpdate = false;
87 fRetrievedShow = false;
88 delete fRunner;
90 BMessage show(kMsgShow);
91 fRunner = new BMessageRunner(this, &show, 1000000, 1);
95 void
96 ProgressWindow::Stop()
98 BAutolock _(this);
100 delete fRunner;
101 fRunner = NULL;
103 if (!IsHidden())
104 Hide();
108 void
109 ProgressWindow::MessageReceived(BMessage* message)
111 switch (message->what) {
112 case kMsgShow:
113 if (fRetrievedUpdate && IsHidden()) {
114 Show();
115 Minimize(false);
118 fRetrievedShow = true;
119 break;
121 case kMsgProgressUpdate:
122 float percent;
123 if (message->FindFloat("percent", &percent) == B_OK)
124 fStatusBar->Update(percent - fStatusBar->CurrentValue());
126 const char* text;
127 if (message->FindString("message", &text) == B_OK)
128 fStatusBar->SetText(text);
130 fRetrievedUpdate = true;
132 if (fRetrievedShow && IsHidden()) {
133 Show();
134 Minimize(false);
136 break;
138 default:
139 BWindow::MessageReceived(message);