diff --git a/src/apps/webpositive/DownloadWindow.cpp b/src/apps/webpositive/DownloadWindow.cpp index a90b919fd7..b1ad727782 100644 --- a/src/apps/webpositive/DownloadWindow.cpp +++ b/src/apps/webpositive/DownloadWindow.cpp @@ -30,22 +30,56 @@ #include "WebDownload.h" #include "WebProcess.h" +#include #include #include #include #include #include +#include #include #include #include #include +enum { + REMOVE_FINISHED_DOWNLOADS = 'rmfd' +}; + DownloadWindow::DownloadWindow(BRect frame, bool visible) : BWindow(frame, "Downloads", B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL, B_AUTO_UPDATE_SIZE_LIMITS | B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE) { SetLayout(new BGroupLayout(B_VERTICAL)); + + BGroupView* downloadsGroupView = new BGroupView(B_VERTICAL); + downloadsGroupView->SetViewColor(245, 245, 245); + m_downloadViewsLayout = downloadsGroupView->GroupLayout(); + + BMenuBar* menuBar = new BMenuBar("Menu bar"); + BMenu* menu = new BMenu("Window"); + menu->AddItem(new BMenuItem("Minimize", new BMessage(B_QUIT_REQUESTED), 'M')); + menuBar->AddItem(menu); + + BScrollView* scrollView = new BScrollView("Downloads scroll view", + downloadsGroupView, 0, false, true, B_NO_BORDER); + + m_removeFinishedButton = new BButton("Remove finished", + new BMessage(REMOVE_FINISHED_DOWNLOADS)); + m_removeFinishedButton->SetEnabled(false); + + AddChild(BGroupLayoutBuilder(B_VERTICAL) + .Add(menuBar) + .Add(scrollView) + .Add(new BSeparatorView(B_HORIZONTAL, B_PLAIN_BORDER)) + .Add(BGroupLayoutBuilder(B_HORIZONTAL) + .AddGlue() + .Add(m_removeFinishedButton) + .SetInsets(5, 5, 5, 5) + ) + ); + if (!visible) Minimize(true); Show(); @@ -70,6 +104,9 @@ void DownloadWindow::MessageReceived(BMessage* message) downloadFinished(download); break; } + case REMOVE_FINISHED_DOWNLOADS: + removeFinishedDownloads(); + break; default: BWindow::MessageReceived(message); break; @@ -90,6 +127,7 @@ public: , m_download(download) , m_expectedSize(download->expectedSize()) { + SetViewColor(245, 245, 245); GroupLayout()->SetInsets(5, 5, 5, 5); m_statusBar = new BStatusBar("download progress", download->filename().String()); m_statusBar->SetMaxValue(100); @@ -120,6 +158,11 @@ public: return m_download; } + void downloadFinished() + { + m_download = NULL; + } + private: BStatusBar* m_statusBar; WebDownload* m_download; @@ -128,9 +171,36 @@ private: void DownloadWindow::downloadStarted(WebDownload* download) { - AddChild(new DownloadProgressView(download)); + m_downloadViewsLayout->AddView(new DownloadProgressView(download)); } void DownloadWindow::downloadFinished(WebDownload* download) { + int32 finishedCount = 0; + for (int32 i = 0; BLayoutItem* item = m_downloadViewsLayout->ItemAt(i); i++) { + DownloadProgressView* view = dynamic_cast(item->View()); + if (!view) + continue; + if (view->download() == download) { + view->downloadFinished(); + finishedCount++; + } else if (!view->download()) + finishedCount++; + } + m_removeFinishedButton->SetEnabled(finishedCount > 0); +} + +void DownloadWindow::removeFinishedDownloads() +{ + for (int32 i = m_downloadViewsLayout->CountItems() - 1; + BLayoutItem* item = m_downloadViewsLayout->ItemAt(i); i--) { + DownloadProgressView* view = dynamic_cast(item->View()); + if (!view) + continue; + if (!view->download()) { + view->RemoveSelf(); + delete view; + } + } + m_removeFinishedButton->SetEnabled(false); } diff --git a/src/apps/webpositive/DownloadWindow.h b/src/apps/webpositive/DownloadWindow.h index 66e88b2940..bd370adb17 100644 --- a/src/apps/webpositive/DownloadWindow.h +++ b/src/apps/webpositive/DownloadWindow.h @@ -30,6 +30,8 @@ #include +class BButton; +class BGroupLayout; class WebDownload; class DownloadWindow : public BWindow { @@ -40,10 +42,14 @@ public: virtual void MessageReceived(BMessage*); virtual bool QuitRequested(); +private: void downloadStarted(WebDownload* download); void downloadFinished(WebDownload* download); + void removeFinishedDownloads(); private: + BGroupLayout* m_downloadViewsLayout; + BButton* m_removeFinishedButton; }; #endif // DownloadWindow_h