From 07caaa8e88f971a4d184d6b1bf1c14768821e8bb Mon Sep 17 00:00:00 2001 From: Matthew Wilber Date: Sat, 2 Aug 2003 16:53:13 +0000 Subject: [PATCH] Simplified / cleaned up code, fixed close window behavior, changed status view text to show identify string git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4212 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/showimage/ShowImageApp.cpp | 133 ++++++++++-------------- src/apps/showimage/ShowImageApp.h | 7 +- src/apps/showimage/ShowImageConstants.h | 2 + src/apps/showimage/ShowImageWindow.cpp | 80 +++++++------- src/apps/showimage/ShowImageWindow.h | 5 +- 5 files changed, 98 insertions(+), 129 deletions(-) diff --git a/src/apps/showimage/ShowImageApp.cpp b/src/apps/showimage/ShowImageApp.cpp index 82004ec1d6..a744bb1e3b 100644 --- a/src/apps/showimage/ShowImageApp.cpp +++ b/src/apps/showimage/ShowImageApp.cpp @@ -17,22 +17,53 @@ int main(int, char**) return 0; } +#define WINDOWS_TO_IGNORE 1 + ShowImageApp::ShowImageApp() - : BApplication(APP_SIG), m_pOpenPanel(0) -{ } + : BApplication(APP_SIG) +{ + fbpulseStarted = false; + m_pOpenPanel = new BFilePanel(B_OPEN_PANEL); +} void ShowImageApp::AboutRequested() { - BAlert* pAlert = new BAlert( "About ShowImage", - "OBOS ShowImage\n\nRecreated by Fernando F.Oliveira", "OK"); + BAlert* pAlert = new BAlert("About ShowImage", + "OBOS ShowImage\n\nby Fernando F. Oliveira and Michael Wilber", "OK"); pAlert->Go(); } void ShowImageApp::ReadyToRun() { - int32 count = ShowImageWindow::CountWindows(); - if (count < 1) - OnOpen(); + if (CountWindows() == WINDOWS_TO_IGNORE) + m_pOpenPanel->Show(); + else + // If image windows are already open + // (paths supplied on the command line) + // start checking the number of open windows + StartPulse(); +} + +void +ShowImageApp::StartPulse() +{ + if (!fbpulseStarted) { + // Tell the app to begin checking + // for the number of open windows + fbpulseStarted = true; + SetPulseRate(250000); + // Set pulse to every 1/4 second + } +} + +void +ShowImageApp::Pulse() +{ + if (!IsLaunching() && CountWindows() <= WINDOWS_TO_IGNORE) + // If the application is not launching and + // all windows are closed except for the file open panel, + // quit the application + PostMessage(B_QUIT_REQUESTED); } void ShowImageApp::ArgvReceived(int32 argc, char** argv) @@ -49,37 +80,29 @@ void ShowImageApp::ArgvReceived(int32 argc, char** argv) message->AddRef("refs", &ref); } } - if (message) { + if (message) RefsReceived(message); - } } void ShowImageApp::MessageReceived(BMessage* message) { switch (message->what) { - case MSG_FILE_OPEN: - OnOpen(); - break; - case B_CANCEL: - if (ShowImageWindow::CountWindows() < 1) - PostMessage(B_QUIT_REQUESTED); - break; - default: - BApplication::MessageReceived(message); - break; - } -} + case MSG_FILE_OPEN: + m_pOpenPanel->Show(); + break; + case MSG_WINDOW_QUIT: + break; + + case B_CANCEL: + // File open panel was closed, + // start checking count of open windows + StartPulse(); + break; -bool ShowImageApp::QuitRequested() -{ - // Attempt to close all the document windows. - bool ok = QuitDudeWinLoop(); - if (ok) - // Everything's been saved, and only unimportant windows should remain. - // Now we can forcibly blow those away. - CloseAllWindows(); - - return ok; + default: + BApplication::MessageReceived(message); + break; + } } void ShowImageApp::RefsReceived(BMessage* message) @@ -99,54 +122,6 @@ void ShowImageApp::RefsReceived(BMessage* message) } } -void ShowImageApp::OnOpen() -{ - if (! m_pOpenPanel) { - m_pOpenPanel = new BFilePanel; - m_pOpenPanel->Window()->SetTitle("Open Image File"); - } - m_pOpenPanel->Show(); -} - -bool ShowImageApp::QuitDudeWinLoop() -{ - bool ok = true; - status_t err; - int32 i=0; - while (ok) { - BWindow* pWin = WindowAt(i++); - if (! pWin) - break; - - ShowImageWindow* pShowImageWindow = dynamic_cast(pWin); - if (pShowImageWindow && pShowImageWindow->Lock()) { - BMessage quitMsg(B_QUIT_REQUESTED); - BMessage reply; - BMessenger winMsgr(pShowImageWindow); - pShowImageWindow->Unlock(); - err = winMsgr.SendMessage(&quitMsg, &reply); - if (err == B_OK) { - bool result; - err = reply.FindBool("result", &result); - if (err == B_OK) { - ok = result; - } - } - } - } - return ok; -} - -void ShowImageApp::CloseAllWindows() -{ - int32 i = 0; - BWindow* pWin; - for (pWin = WindowAt(i++); pWin && pWin->Lock(); pWin = WindowAt(i++)) { - // don't take no for an answer - pWin->Quit(); - } -} - void ShowImageApp::Open(const entry_ref* ref) { if (ShowImageWindow::NewWindow(ref) != B_OK) { diff --git a/src/apps/showimage/ShowImageApp.h b/src/apps/showimage/ShowImageApp.h index 8bb6551b00..9c5a6ae6ad 100644 --- a/src/apps/showimage/ShowImageApp.h +++ b/src/apps/showimage/ShowImageApp.h @@ -16,18 +16,17 @@ public: virtual void AboutRequested(); virtual void ArgvReceived(int32 argc, char** argv); virtual void MessageReceived(BMessage* message); - virtual bool QuitRequested(); virtual void ReadyToRun(); + virtual void Pulse(); virtual void RefsReceived(BMessage* message); private: - void OnOpen(); - bool QuitDudeWinLoop(); - void CloseAllWindows(); + void StartPulse(); void Open(const entry_ref* ref); private: BFilePanel* m_pOpenPanel; + bool fbpulseStarted; }; #endif /* _ShowImageApp_h */ diff --git a/src/apps/showimage/ShowImageConstants.h b/src/apps/showimage/ShowImageConstants.h index 44162d747a..70bd8b4442 100644 --- a/src/apps/showimage/ShowImageConstants.h +++ b/src/apps/showimage/ShowImageConstants.h @@ -10,6 +10,8 @@ const uint32 MSG_CAPTURE_MOUSE = 'mCPM'; const uint32 MSG_CHANGE_FOCUS = 'mCFS'; const uint32 MSG_FILE_OPEN = 'mFOP'; +const uint32 MSG_CLOSE = 'mCLS'; +const uint32 MSG_WINDOW_QUIT = 'mWQT'; const uint32 MSG_OUTPUT_TYPE = 'BTMN'; const uint32 MSG_SAVE_PANEL = 'mFSP'; const uint32 MSG_CLEAR_SELECT = 'mCSL'; diff --git a/src/apps/showimage/ShowImageWindow.cpp b/src/apps/showimage/ShowImageWindow.cpp index 5f812306ac..c23b7c601d 100644 --- a/src/apps/showimage/ShowImageWindow.cpp +++ b/src/apps/showimage/ShowImageWindow.cpp @@ -3,6 +3,7 @@ */ #include +#include #include #include #include @@ -15,53 +16,49 @@ #include #include #include -#include #include +#include #include "ShowImageConstants.h" #include "ShowImageWindow.h" #include "ShowImageView.h" #include "ShowImageStatusView.h" -BLocker ShowImageWindow::s_winListLocker("ShowImageWindow list lock"); -BList ShowImageWindow::s_winList; - status_t ShowImageWindow::NewWindow(const entry_ref* ref) { - BBitmap* pBitmap = BTranslationUtils::GetBitmap(ref); + // Get identify string (image type) + BString strId = "Unknown"; + BTranslatorRoster *proster = BTranslatorRoster::Default(); + if (!proster) + return B_ERROR; + BFile file(ref, B_READ_ONLY); + translator_info info; + if (proster->Identify(&file, NULL, &info) == B_OK) + strId = info.name; + + // Translate image data and create a new ShowImage window + file.Seek(0, SEEK_SET); + BBitmap* pBitmap = BTranslationUtils::GetBitmap(&file); if (pBitmap) { - ShowImageWindow* pWin = new ShowImageWindow(ref, pBitmap); + ShowImageWindow* pWin = new ShowImageWindow(ref, pBitmap, strId); return pWin->InitCheck(); } return B_ERROR; } -int32 ShowImageWindow::CountWindows() -{ - int32 count = -1; - if (s_winListLocker.Lock()) { - count = s_winList.CountItems(); - s_winListLocker.Unlock(); - } - return count; -} - -ShowImageWindow::ShowImageWindow(const entry_ref* ref, BBitmap* pBitmap) +ShowImageWindow::ShowImageWindow(const entry_ref* ref, BBitmap* pBitmap, BString &strId) : BWindow(BRect(50, 50, 350, 250), "", B_DOCUMENT_WINDOW, 0), m_pReferences(0) { fpsavePanel = NULL; - SetPulseRate( 200000.0 ); - // create menu bar pBar = new BMenuBar( BRect(0,0, Bounds().right, 20), "menu_bar"); LoadMenus(pBar); AddChild(pBar); BRect viewFrame = Bounds(); -// viewFrame.left += 20; viewFrame.top = pBar->Bounds().bottom+1; viewFrame.right -= B_V_SCROLL_BAR_WIDTH; viewFrame.bottom -= B_H_SCROLL_BAR_HEIGHT; @@ -81,9 +78,10 @@ ShowImageWindow::ShowImageWindow(const entry_ref* ref, BBitmap* pBitmap) BRect rect; + const int32 kstatusWidth = 190; rect = Bounds(); rect.top = viewFrame.bottom + 1; - rect.left = viewFrame.left + 160; + rect.left = viewFrame.left + kstatusWidth; rect.right = viewFrame.right; hor_scroll = new BScrollBar( rect, "hor_scroll", m_PrivateView, 0,150, B_HORIZONTAL ); @@ -92,11 +90,11 @@ ShowImageWindow::ShowImageWindow(const entry_ref* ref, BBitmap* pBitmap) ShowImageStatusView * status_bar; rect.left = 0; - rect.right = 159; + rect.right = kstatusWidth - 1; status_bar = new ShowImageStatusView( rect, "status_bar", B_FOLLOW_BOTTOM, B_WILL_DRAW ); status_bar->SetViewColor( ui_color( B_MENU_BACKGROUND_COLOR ) ); - status_bar->SetCaption( "ImageShow" ); + status_bar->SetText(strId); AddChild( status_bar ); @@ -115,10 +113,6 @@ ShowImageWindow::ShowImageWindow(const entry_ref* ref, BBitmap* pBitmap) // finish creating window SetRef(ref); UpdateTitle(); - if (s_winListLocker.Lock()) { - s_winList.AddItem(this); - s_winListLocker.Unlock(); - } m_PrivateView->pBar = pBar; @@ -127,17 +121,7 @@ ShowImageWindow::ShowImageWindow(const entry_ref* ref, BBitmap* pBitmap) ShowImageWindow::~ShowImageWindow() { - if (m_pReferences) { - delete m_pReferences; - } - - if (s_winListLocker.Lock()) { - s_winList.RemoveItem(this); - s_winListLocker.Unlock(); - } - if (CountWindows() < 1) { - be_app->PostMessage(B_QUIT_REQUESTED); - } + delete m_pReferences; } void ShowImageWindow::WindowActivated(bool active) @@ -177,9 +161,6 @@ void ShowImageWindow::UpdateTitle() void ShowImageWindow::LoadMenus(BMenuBar* pBar) { - long unsigned int MSG_QUIT = B_QUIT_REQUESTED; - long unsigned int MSG_ABOUT = B_ABOUT_REQUESTED; - BMenu* pMenu = new BMenu("File"); AddItemMenu( pMenu, "Open", MSG_FILE_OPEN, 'O', 0, 'A', true ); @@ -191,11 +172,11 @@ void ShowImageWindow::LoadMenus(BMenuBar* pBar) // to from the Be bitmap image format pMenu->AddItem( pMenuSaveAs ); - AddItemMenu( pMenu, "Close", MSG_QUIT, 'W', 0, 'A', true); + AddItemMenu( pMenu, "Close", MSG_CLOSE, 'W', 0, 'W', true); pMenu->AddSeparatorItem(); - AddItemMenu( pMenu, "About ShowImage...", MSG_ABOUT, 0, 0, 'A', true); + AddItemMenu( pMenu, "About ShowImage...", B_ABOUT_REQUESTED, 0, 0, 'A', true); pMenu->AddSeparatorItem(); - AddItemMenu( pMenu, "Quit", MSG_QUIT, 'Q', 0, 'A', true); + AddItemMenu( pMenu, "Quit", B_QUIT_REQUESTED, 'Q', 0, 'A', true); pBar->AddItem(pMenu); @@ -282,6 +263,10 @@ void ShowImageWindow::MessageReceived(BMessage* message) // User specified where to save the output image SaveToFile(message); break; + + case MSG_CLOSE: + Quit(); + break; case B_UNDO : pAlert = new BAlert( "Edit/Undo", @@ -385,5 +370,12 @@ ShowImageWindow::SaveToFile(BMessage *pmsg) // detach so it doesn't get deleted } +void +ShowImageWindow::Quit() +{ + // tell the app to forget about this window + be_app->PostMessage(MSG_WINDOW_QUIT); + BWindow::Quit(); +} // BMenu* pMenuDither = ; diff --git a/src/apps/showimage/ShowImageWindow.h b/src/apps/showimage/ShowImageWindow.h index 66adf015ab..d1d31f6ef3 100644 --- a/src/apps/showimage/ShowImageWindow.h +++ b/src/apps/showimage/ShowImageWindow.h @@ -8,6 +8,7 @@ #include #include #include +#include class ShowImageView; @@ -15,14 +16,14 @@ class ShowImageWindow : public BWindow { public: static status_t NewWindow(const entry_ref* ref); - static int32 CountWindows(); - ShowImageWindow(const entry_ref* ref, BBitmap* pBitmap); + ShowImageWindow(const entry_ref* ref, BBitmap* pBitmap, BString &strId); virtual ~ShowImageWindow(); virtual void WindowActivated(bool active); virtual void FrameResized( float new_width, float new_height ); virtual void MessageReceived(BMessage* message); + virtual void Quit(); status_t InitCheck(); ShowImageView* GetShowImageView() const { return m_PrivateView; }