Code cleanup, no functional change except for changing BWindow::Run() into

a BWindow::Hide()-BWindow::Show() combo.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34095 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2009-11-17 16:10:18 +00:00
parent dcf71e1cae
commit 9d6405b724
2 changed files with 357 additions and 319 deletions
+239 -221
View File
@@ -32,8 +32,10 @@ names are registered trademarks or trademarks of their respective holders.
All rights reserved. All rights reserved.
*/ */
// A subclass of BWindow that is used to display /*! A subclass of BWindow that is used to display the status of the Tracker
// the status of the tracker (Copying, Deleting, etc.). operations (copying, deleting, etc.).
*/
#include <Application.h> #include <Application.h>
#include <Button.h> #include <Button.h>
@@ -60,17 +62,15 @@ const BRect kStatusRect(200, 200, 550, 200);
class TCustomButton : public BButton { class TCustomButton : public BButton {
public: public:
TCustomButton(BRect frame, uint32 command); TCustomButton(BRect frame, uint32 command);
virtual void Draw(BRect); virtual void Draw(BRect updateRect);
private: private:
typedef BButton _inherited; typedef BButton _inherited;
}; };
class BStatusMouseFilter : public BMessageFilter { class BStatusMouseFilter : public BMessageFilter {
public: public:
BStatusMouseFilter() BStatusMouseFilter();
: BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE, B_MOUSE_DOWN)
{}
virtual filter_result Filter(BMessage* message, BHandler** target); virtual filter_result Filter(BMessage* message, BHandler** target);
}; };
@@ -80,15 +80,24 @@ BStatusWindow *gStatusWindow = NULL;
} }
filter_result BStatusMouseFilter::BStatusMouseFilter()
BStatusMouseFilter::Filter(BMessage *, BHandler **target) :
BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE, B_MOUSE_DOWN)
{ {
if ((*target)->Name() }
filter_result
BStatusMouseFilter::Filter(BMessage* message, BHandler** target)
{
// If the target is the status bar, make sure the message goes to the
// parent view instead.
if ((*target)->Name() != NULL
&& strcmp((*target)->Name(), "StatusBar") == 0) { && strcmp((*target)->Name(), "StatusBar") == 0) {
BView* view = dynamic_cast<BView*>(*target); BView* view = dynamic_cast<BView*>(*target);
if (view) if (view != NULL)
view = view->Parent(); view = view->Parent();
if (view) if (view != NULL)
*target = view; *target = view;
} }
@@ -97,7 +106,8 @@ BStatusMouseFilter::Filter(BMessage *, BHandler **target)
TCustomButton::TCustomButton(BRect frame, uint32 what) TCustomButton::TCustomButton(BRect frame, uint32 what)
: BButton(frame, "", "", new BMessage(what), B_FOLLOW_LEFT | B_FOLLOW_TOP, :
BButton(frame, "", "", new BMessage(what), B_FOLLOW_LEFT | B_FOLLOW_TOP,
B_WILL_DRAW) B_WILL_DRAW)
{ {
} }
@@ -135,7 +145,8 @@ TCustomButton::Draw(BRect updateRect)
BStatusWindow::BStatusWindow() BStatusWindow::BStatusWindow()
: BWindow(kStatusRect, "Tracker Status", B_TITLED_WINDOW, :
BWindow(kStatusRect, "Tracker Status", B_TITLED_WINDOW,
B_NOT_CLOSABLE | B_NOT_RESIZABLE | B_NOT_ZOOMABLE, B_NOT_CLOSABLE | B_NOT_RESIZABLE | B_NOT_ZOOMABLE,
B_ALL_WORKSPACES), B_ALL_WORKSPACES),
fRetainDesktopFocus(false) fRetainDesktopFocus(false)
@@ -150,7 +161,8 @@ BStatusWindow::BStatusWindow()
view->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); view->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
AddChild(view); AddChild(view);
Run(); Hide();
Show();
} }
@@ -159,71 +171,6 @@ BStatusWindow::~BStatusWindow()
} }
bool
BStatusWindow::CheckCanceledOrPaused(thread_id thread)
{
bool wasCanceled = false;
bool isPaused = false;
BStatusView *view = NULL;
for (;;) {
AutoLock<BWindow> lock(this);
// check if cancel or pause hit
for (int32 index = fViewList.CountItems() - 1; index >= 0; index--) {
view = fViewList.ItemAt(index);
if (view && view->Thread() == thread) {
isPaused = view->IsPaused();
wasCanceled = view->WasCanceled();
break;
}
}
lock.Unlock();
if (wasCanceled || !isPaused)
break;
if (isPaused && view) {
AutoLock<BWindow> lock(this);
// say we are paused
view->Invalidate();
lock.Unlock();
ASSERT(find_thread(NULL) == view->Thread());
// and suspend ourselves
// we will get resumend from BStatusView::MessageReceived
suspend_thread(view->Thread());
}
break;
}
return wasCanceled;
}
bool
BStatusWindow::AttemptToQuit()
{
// called when tracker is quitting
// try to cancel all the move/copy/empty trash threads in a nice way
// by issuing cancels
int32 count = fViewList.CountItems();
if (count == 0)
return true;
for (int32 index = 0; index < count; index++)
fViewList.ItemAt(index)->SetWasCanceled();
// maybe next time everything will have been canceled
return false;
}
void void
BStatusWindow::CreateStatusItem(thread_id thread, StatusWindowState type) BStatusWindow::CreateStatusItem(thread_id thread, StatusWindowState type)
{ {
@@ -268,12 +215,37 @@ BStatusWindow::CreateStatusItem(thread_id thread, StatusWindowState type)
void void
BStatusWindow::WindowActivated(bool state) BStatusWindow::InitStatusItem(thread_id thread, int32 totalItems,
off_t totalSize, const entry_ref* destDir, bool showCount)
{ {
if (!state) AutoLock<BWindow> lock(this);
fRetainDesktopFocus = false;
return _inherited::WindowActivated(state); int32 numItems = fViewList.CountItems();
for (int32 index = 0; index < numItems; index++) {
BStatusView* view = fViewList.ItemAt(index);
if (view->Thread() == thread) {
view->InitStatus(totalItems, totalSize, destDir, showCount);
break;
}
}
}
void
BStatusWindow::UpdateStatus(thread_id thread, const char* curItem,
off_t itemSize, bool optional)
{
AutoLock<BWindow> lock(this);
int32 numItems = fViewList.CountItems();
for (int32 index = 0; index < numItems; index++) {
BStatusView* view = fViewList.ItemAt(index);
if (view->Thread() == thread) {
view->UpdateStatus(curItem, itemSize, optional);
break;
}
}
} }
@@ -293,8 +265,9 @@ BStatusWindow::RemoveStatusItem(thread_id thread)
} }
} }
if (winner) { if (winner != NULL) {
// the height by which the other views will have to be moved (in pixel count) // The height by which the other views will have to be moved (in pixel
// count).
float height = winner->Bounds().Height() + 1; float height = winner->Bounds().Height() + 1;
fViewList.RemoveItem(winner); fViewList.RemoveItem(winner);
winner->RemoveSelf(); winner->RemoveSelf();
@@ -306,24 +279,25 @@ BStatusWindow::RemoveStatusItem(thread_id thread)
AutoLock<BLooper> lock(be_app); AutoLock<BLooper> lock(be_app);
int32 count = be_app->CountWindows(); int32 count = be_app->CountWindows();
for (int32 index = 0; index < count; index++) { for (int32 index = 0; index < count; index++) {
desktop = dynamic_cast<BDeskWindow *>(be_app->WindowAt(index)); desktop = dynamic_cast<BDeskWindow*>(
if (desktop) be_app->WindowAt(index));
if (desktop != NULL)
break; break;
} }
} }
Hide(); Hide();
if (desktop) if (desktop != NULL) {
// desktop was active when we first started, // desktop was active when we first started,
// make it active again // make it active again
desktop->Activate(); desktop->Activate();
} }
}
for (; index < numItems; index++) for (; index < numItems; index++)
fViewList.ItemAt(index)->MoveBy(0, -height); fViewList.ItemAt(index)->MoveBy(0, -height);
ResizeTo(Bounds().Width(), Bounds().Height() - height); ResizeTo(Bounds().Width(), Bounds().Height() - height);
} }
} }
@@ -344,51 +318,94 @@ BStatusWindow::HasStatus(thread_id thread)
} }
void bool
BStatusWindow::UpdateStatus(thread_id thread, const char *curItem, off_t itemSize, BStatusWindow::CheckCanceledOrPaused(thread_id thread)
bool optional)
{ {
AutoLock<BWindow> lock(this); bool wasCanceled = false;
bool isPaused = false;
int32 numItems = fViewList.CountItems(); BStatusView* view = NULL;
for (int32 index = 0; index < numItems; index++) {
BStatusView *view = fViewList.ItemAt(index); for (;;) {
if (view->Thread() == thread) {
view->UpdateStatus(curItem, itemSize, optional); AutoLock<BWindow> lock(this);
// check if cancel or pause hit
for (int32 index = fViewList.CountItems() - 1; index >= 0; index--) {
view = fViewList.ItemAt(index);
if (view && view->Thread() == thread) {
isPaused = view->IsPaused();
wasCanceled = view->WasCanceled();
break; break;
} }
} }
lock.Unlock();
if (wasCanceled || !isPaused)
break;
if (isPaused && view != NULL) {
AutoLock<BWindow> lock(this);
// say we are paused
view->Invalidate();
lock.Unlock();
ASSERT(find_thread(NULL) == view->Thread());
// and suspend ourselves
// we will get resumend from BStatusView::MessageReceived
suspend_thread(view->Thread());
}
break;
}
return wasCanceled;
}
bool
BStatusWindow::AttemptToQuit()
{
// called when tracker is quitting
// try to cancel all the move/copy/empty trash threads in a nice way
// by issuing cancels
int32 count = fViewList.CountItems();
if (count == 0)
return true;
for (int32 index = 0; index < count; index++)
fViewList.ItemAt(index)->SetWasCanceled();
// maybe next time everything will have been canceled
return false;
} }
void void
BStatusWindow::InitStatusItem(thread_id thread, int32 totalItems, off_t totalSize, BStatusWindow::WindowActivated(bool state)
const entry_ref *destDir, bool showCount)
{ {
AutoLock<BWindow> lock(this); if (!state)
fRetainDesktopFocus = false;
int32 numItems = fViewList.CountItems();
for (int32 index = 0; index < numItems; index++) {
BStatusView *view = fViewList.ItemAt(index);
if (view->Thread() == thread) {
view->InitStatus(totalItems, totalSize, destDir, showCount);
break;
}
}
return _inherited::WindowActivated(state);
} }
BStatusView::BStatusView(BRect bounds, thread_id thread, StatusWindowState type) // #pragma mark - BStatusView
: BView(bounds, "StatusView", B_FOLLOW_NONE, B_WILL_DRAW),
fBitmap(NULL)
BStatusView::BStatusView(BRect bounds, thread_id thread,
StatusWindowState type)
:
BView(bounds, "StatusView", B_FOLLOW_NONE, B_WILL_DRAW),
fType(type),
fBitmap(NULL),
fThread(thread)
{ {
Init(); Init();
fThread = thread;
fType = type;
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
SetLowColor(ViewColor()); SetLowColor(ViewColor());
SetHighColor(20, 20, 20); SetHighColor(20, 20, 20);
@@ -404,7 +421,6 @@ BStatusView::BStatusView(BRect bounds, thread_id thread, StatusWindowState type)
rect.top += 6; rect.top += 6;
rect.bottom = rect.top + 15; rect.bottom = rect.top + 15;
const char* caption = NULL; const char* caption = NULL;
int32 id = 0; int32 id = 0;
@@ -429,7 +445,6 @@ BStatusView::BStatusView(BRect bounds, thread_id thread, StatusWindowState type)
id = kResTrashStatusBitmap; id = kResTrashStatusBitmap;
break; break;
case kVolumeState: case kVolumeState:
caption = "Searching for disks to mount" B_UTF8_ELLIPSIS; caption = "Searching for disks to mount" B_UTF8_ELLIPSIS;
break; break;
@@ -448,7 +463,7 @@ BStatusView::BStatusView(BRect bounds, thread_id thread, StatusWindowState type)
break; break;
} }
if (caption) { if (caption != NULL) {
fStatusBar = new BStatusBar(rect, "StatusBar", caption); fStatusBar = new BStatusBar(rect, "StatusBar", caption);
fStatusBar->SetBarHeight(12); fStatusBar->SetBarHeight(12);
float width, height; float width, height;
@@ -456,16 +471,18 @@ BStatusView::BStatusView(BRect bounds, thread_id thread, StatusWindowState type)
fStatusBar->ResizeTo(fStatusBar->Frame().Width(), height); fStatusBar->ResizeTo(fStatusBar->Frame().Width(), height);
AddChild(fStatusBar); AddChild(fStatusBar);
// figure out how much room we need to display // Figure out how much room we need to display the additional status
// the additional status message below the bar // message below the bar
font_height fh; font_height fh;
GetFontHeight(&fh); GetFontHeight(&fh);
BRect f = fStatusBar->Frame(); BRect f = fStatusBar->Frame();
// height is 3 x the "room from the top" + bar height + room for string // Height is 3 x the "room from the top" + bar height + room for
ResizeTo(Bounds().Width(), f.top + f.Height() + fh.leading + fh.ascent + fh.descent + f.top); // string.
ResizeTo(Bounds().Width(), f.top + f.Height() + fh.leading + fh.ascent
+ fh.descent + f.top);
} }
if (id) if (id != 0)
GetTrackerResources()->GetBitmapResource(B_MESSAGE_TYPE, id, &fBitmap); GetTrackerResources()->GetBitmapResource(B_MESSAGE_TYPE, id, &fBitmap);
rect = Bounds(); rect = Bounds();
@@ -504,14 +521,6 @@ BStatusView::Init()
} }
void
BStatusView::AttachedToWindow()
{
fPauseButton->SetTarget(this);
fStopButton->SetTarget(this);
}
void void
BStatusView::InitStatus(int32 totalItems, off_t totalSize, BStatusView::InitStatus(int32 totalItems, off_t totalSize,
const entry_ref* destDir, bool showCount) const entry_ref* destDir, bool showCount)
@@ -545,7 +554,8 @@ BStatusView::InitStatus(int32 totalItems, off_t totalSize,
break; break;
case kTrashState: case kTrashState:
fStatusBar->Reset("Emptying Trash" B_UTF8_ELLIPSIS " ", buffer.String()); fStatusBar->Reset("Emptying Trash" B_UTF8_ELLIPSIS " ",
buffer.String());
break; break;
case kDeleteState: case kDeleteState:
@@ -566,93 +576,6 @@ BStatusView::InitStatus(int32 totalItems, off_t totalSize,
} }
void
BStatusView::UpdateStatus(const char *curItem, off_t itemSize, bool optional)
{
float currentTime = system_time();
if (fShowCount) {
if (curItem)
fCurItem++;
fItemSize += itemSize;
if (!optional || ((currentTime - fLastUpdateTime) > kUpdateGrain)) {
if (curItem != NULL || fPendingStatusString[0]) {
// forced update or past update time
BString buffer;
buffer << fCurItem << " ";
// if we don't have curItem, take the one from the stash
const char *statusItem = curItem != NULL
? curItem : fPendingStatusString;
fStatusBar->Update((float)fItemSize / fTotalSize, statusItem,
buffer.String());
// we already displayed this item, clear the stash
fPendingStatusString[0] = '\0';
fLastUpdateTime = currentTime;
}
else
// don't have a file to show, just update the bar
fStatusBar->Update((float)fItemSize / fTotalSize);
fItemSize = 0;
} else if (curItem != NULL) {
// stash away the name of the item we are currently processing
// so we can show it when the time comes
strncpy(fPendingStatusString, curItem, 127);
fPendingStatusString[127] = '0';
}
} else {
fStatusBar->Update((float)fItemSize / fTotalSize);
fItemSize = 0;
}
}
void
BStatusView::MessageReceived(BMessage *message)
{
switch (message->what) {
case kPauseButton:
fIsPaused = !fIsPaused;
fPauseButton->SetValue(fIsPaused ? B_CONTROL_ON : B_CONTROL_OFF);
if (!fIsPaused) {
// force window update
Invalidate();
// let 'er rip
resume_thread(Thread());
}
break;
case kStopButton:
fWasCanceled = true;
if (fIsPaused) {
// resume so that the copy loop gets a chance to finish up
fIsPaused = false;
// force window update
Invalidate();
// let 'er rip
resume_thread(Thread());
}
break;
default:
_inherited::MessageReceived(message);
break;
}
}
void void
BStatusView::Draw(BRect updateRect) BStatusView::Draw(BRect updateRect)
{ {
@@ -702,6 +625,101 @@ BStatusView::Draw(BRect updateRect)
} }
void
BStatusView::AttachedToWindow()
{
fPauseButton->SetTarget(this);
fStopButton->SetTarget(this);
}
void
BStatusView::MessageReceived(BMessage *message)
{
switch (message->what) {
case kPauseButton:
fIsPaused = !fIsPaused;
fPauseButton->SetValue(fIsPaused ? B_CONTROL_ON : B_CONTROL_OFF);
if (!fIsPaused) {
// force window update
Invalidate();
// let 'er rip
resume_thread(Thread());
}
break;
case kStopButton:
fWasCanceled = true;
if (fIsPaused) {
// resume so that the copy loop gets a chance to finish up
fIsPaused = false;
// force window update
Invalidate();
// let 'er rip
resume_thread(Thread());
}
break;
default:
_inherited::MessageReceived(message);
break;
}
}
void
BStatusView::UpdateStatus(const char *curItem, off_t itemSize, bool optional)
{
float currentTime = system_time();
if (fShowCount) {
if (curItem)
fCurItem++;
fItemSize += itemSize;
if (!optional || ((currentTime - fLastUpdateTime) > kUpdateGrain)) {
if (curItem != NULL || fPendingStatusString[0]) {
// forced update or past update time
BString buffer;
buffer << fCurItem << " ";
// if we don't have curItem, take the one from the stash
const char *statusItem = curItem != NULL
? curItem : fPendingStatusString;
fStatusBar->Update((float)fItemSize / fTotalSize, statusItem,
buffer.String());
// we already displayed this item, clear the stash
fPendingStatusString[0] = '\0';
fLastUpdateTime = currentTime;
}
else
// don't have a file to show, just update the bar
fStatusBar->Update((float)fItemSize / fTotalSize);
fItemSize = 0;
} else if (curItem != NULL) {
// stash away the name of the item we are currently processing
// so we can show it when the time comes
strncpy(fPendingStatusString, curItem, 127);
fPendingStatusString[127] = '0';
}
} else {
fStatusBar->Update((float)fItemSize / fTotalSize);
fItemSize = 0;
}
}
void void
BStatusView::SetWasCanceled() BStatusView::SetWasCanceled()
{ {
+42 -22
View File
@@ -31,10 +31,10 @@ of Be Incorporated in the United States and other countries. Other brand product
names are registered trademarks or trademarks of their respective holders. names are registered trademarks or trademarks of their respective holders.
All rights reserved. All rights reserved.
*/ */
#ifndef STATUS_WINDOW_H #ifndef STATUS_WINDOW_H
#define STATUS_WINDOW_H #define STATUS_WINDOW_H
#include <Window.h> #include <Window.h>
#include <View.h> #include <View.h>
#include <Bitmap.h> #include <Bitmap.h>
@@ -43,8 +43,10 @@ All rights reserved.
#include "ObjectList.h" #include "ObjectList.h"
namespace BPrivate { namespace BPrivate {
enum StatusWindowState { enum StatusWindowState {
kCopyState, kCopyState,
kMoveState, kMoveState,
@@ -57,25 +59,34 @@ enum StatusWindowState {
class BStatusView; class BStatusView;
class BStatusWindow : public BWindow { class BStatusWindow : public BWindow {
public: public:
BStatusWindow(); BStatusWindow();
~BStatusWindow(); virtual ~BStatusWindow();
void CreateStatusItem(thread_id, StatusWindowState);
void InitStatusItem(thread_id, int32 totalItems, off_t totalSize, void CreateStatusItem(thread_id,
const entry_ref *destDir = NULL, bool showCount = true); StatusWindowState);
void UpdateStatus(thread_id, const char *curItem, off_t itemSize, bool optional = false); void InitStatusItem(thread_id, int32 totalItems,
// if true is passed in <optional> status will only off_t totalSize,
// be updated if 0.2 seconds elapsed since the last update const entry_ref* destDir = NULL,
bool showCount = true);
void UpdateStatus(thread_id, const char* curItem,
off_t itemSize, bool optional = false);
// If true is passed in <optional> status
// will only be updated if 0.2 seconds
// elapsed since the last update
void RemoveStatusItem(thread_id); void RemoveStatusItem(thread_id);
bool HasStatus(thread_id); bool HasStatus(thread_id);
bool CheckCanceledOrPaused(thread_id); bool CheckCanceledOrPaused(thread_id);
void UpdateButtonState();
bool AttemptToQuit(); bool AttemptToQuit();
// called by the tracker app during quit time, before // Called by the tracker app during quit
// inherited QuitRequested; kills all the copy/move/empty trash // time, before inherited QuitRequested;
// threads in a clean way by issuing a cancel // kills all the copy/move/empty trash
// threads in a clean way by issuing a
// cancel.
protected: protected:
virtual void WindowActivated(bool state); virtual void WindowActivated(bool state);
@@ -88,29 +99,33 @@ private:
typedef BWindow _inherited; typedef BWindow _inherited;
}; };
class BStatusView : public BView { class BStatusView : public BView {
public: public:
BStatusView(BRect, thread_id, StatusWindowState); BStatusView(BRect frame, thread_id,
StatusWindowState state);
virtual ~BStatusView(); virtual ~BStatusView();
void Init(); void Init();
void InitStatus(int32 totalItems, off_t totalSize, const entry_ref *destDir, void InitStatus(int32 totalItems, off_t totalSize,
bool showCount); const entry_ref* destDir, bool showCount);
// BView overrides // BView overrides
virtual void Draw(BRect); virtual void Draw(BRect updateRect);
virtual void AttachedToWindow(); virtual void AttachedToWindow();
virtual void MessageReceived(BMessage *); virtual void MessageReceived(BMessage* message);
void UpdateStatus(const char *curItem, off_t itemSize, bool optional = false);
// if true is passed in <optional> status will only void UpdateStatus(const char* currentItem,
// be updated if 0.2 seconds elapsed since the last update off_t itemSize, bool optional = false);
// If true is passed in <optional> status
// will only be updated if 0.2 seconds
// elapsed since the last update.
bool WasCanceled() const; bool WasCanceled() const;
bool IsPaused() const; bool IsPaused() const;
thread_id Thread() const; thread_id Thread() const;
void ForceQuit();
void SetWasCanceled(); void SetWasCanceled();
// called by AboutToQuit // called by AboutToQuit
@@ -134,28 +149,33 @@ private:
typedef BView _inherited; typedef BView _inherited;
}; };
inline bool inline bool
BStatusView::IsPaused() const BStatusView::IsPaused() const
{ {
return fIsPaused; return fIsPaused;
} }
inline bool inline bool
BStatusView::WasCanceled() const BStatusView::WasCanceled() const
{ {
return fWasCanceled; return fWasCanceled;
} }
inline thread_id inline thread_id
BStatusView::Thread() const BStatusView::Thread() const
{ {
return fThread; return fThread;
} }
extern BStatusWindow *gStatusWindow; extern BStatusWindow *gStatusWindow;
} // namespace BPrivate } // namespace BPrivate
using namespace BPrivate; using namespace BPrivate;
#endif #endif // STATUS_WINDOW_H