diff --git a/src/apps/showimage/EntryMenuItem.cpp b/src/apps/showimage/EntryMenuItem.cpp new file mode 100644 index 0000000000..b2f1d0e8b8 --- /dev/null +++ b/src/apps/showimage/EntryMenuItem.cpp @@ -0,0 +1,117 @@ +/*****************************************************************************/ +// EntryMenuItem +// Written by Michael Pfeiffer +// +// EntryMenuItem.cpp +// +// +// Copyright (c) 2003 OpenBeOS Project +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +/*****************************************************************************/ + +#include +#include + +#include "EntryMenuItem.h" + +EntryMenuItem::EntryMenuItem(entry_ref* ref, const char* label, BMessage* message, char shortcut, uint32 modifiers) + : BMenuItem(label, message, shortcut, modifiers) + , fEntry(*ref) + , fSmallIcon(NULL) +{ +} + +EntryMenuItem::~EntryMenuItem() +{ + delete fSmallIcon; +} + +void +EntryMenuItem::GetContentSize(float* width, float* height) +{ + BMenuItem::GetContentSize(width, height); + *width += kTextIndent; + if (*height < kIconSize) { + *height = kIconSize; + } +} + +void +EntryMenuItem::DrawContent() +{ + BView* view = Menu(); + BPoint pos(view->PenLocation()); + + if (fSmallIcon == NULL) { + fSmallIcon = LoadIcon(); // load on demand + } + + view->MovePenBy(kTextIndent, 0); + BMenuItem::DrawContent(); + + if (fSmallIcon) { + view->SetDrawingMode(B_OP_OVER); + view->DrawBitmap(fSmallIcon, pos); + } +} + +BBitmap* +EntryMenuItem::GetIcon(BMimeType* mimeType) +{ + BBitmap* icon; + icon = new BBitmap(BRect(0, 0, kIconSize-1, kIconSize-1), B_CMAP8); + if (mimeType->GetIcon(icon, B_MINI_ICON) != B_OK) { + delete icon; icon = NULL; + } + return icon; +} + +BBitmap* +EntryMenuItem::LoadIcon() +{ + BBitmap* icon = NULL; + BNode node(&fEntry); + BNodeInfo info(&node); + char type[B_MIME_TYPE_LENGTH+1]; + + // Note: BNodeInfo::GetTrackerIcon does not work as expected! + + // try to get the icon stored in file attribute + icon = new BBitmap(BRect(0, 0, kIconSize-1, kIconSize-1), B_CMAP8); + if (info.GetIcon(icon, B_MINI_ICON) == B_OK) { + return icon; + } + delete icon; icon = NULL; + + // get the icon from type + if (info.GetType(type) == B_OK) { + BMimeType mimeType(type); + BMimeType superType; + if (mimeType.InitCheck() == B_OK) { + icon = GetIcon(&mimeType); + // or super type + if (icon == NULL && mimeType.GetSupertype(&superType) == B_OK) { + icon = GetIcon(&superType); + } + } + } + + return icon; +} diff --git a/src/apps/showimage/EntryMenuItem.h b/src/apps/showimage/EntryMenuItem.h new file mode 100644 index 0000000000..07f66a697c --- /dev/null +++ b/src/apps/showimage/EntryMenuItem.h @@ -0,0 +1,59 @@ +/*****************************************************************************/ +// EntryMenuItem +// Written by Michael Pfeiffer +// +// EntryMenuItem.h +// +// +// Copyright (c) 2003 OpenBeOS Project +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +/*****************************************************************************/ + +#ifndef _EntryMenuItem_h +#define _EntryMenuItem_h + +#include +#include +#include +#include + +class EntryMenuItem : public BMenuItem +{ +public: + EntryMenuItem(entry_ref* entry, const char* label, BMessage* message, char shortcut = 0, uint32 modifiers = 0); + ~EntryMenuItem(); + + void GetContentSize(float* width, float* height); + void DrawContent(); + +private: + enum { + kIconSize = 16, + kTextIndent = kIconSize + 4, + }; + + BBitmap* GetIcon(BMimeType* mimeType); + BBitmap* LoadIcon(); + + entry_ref fEntry; + BBitmap* fSmallIcon; +}; + +#endif diff --git a/src/apps/showimage/Jamfile b/src/apps/showimage/Jamfile index cb229272bd..7506214a9a 100644 --- a/src/apps/showimage/Jamfile +++ b/src/apps/showimage/Jamfile @@ -10,6 +10,7 @@ App ShowImage : ShowImageApp.cpp ShowImageWindow.cpp PrintOptionsWindow.cpp Filter.cpp + EntryMenuItem.cpp ; LinkSharedOSLibs ShowImage : be tracker translation ; diff --git a/src/apps/showimage/ShowImageWindow.cpp b/src/apps/showimage/ShowImageWindow.cpp index b6cce699eb..8a4f5fd12f 100644 --- a/src/apps/showimage/ShowImageWindow.cpp +++ b/src/apps/showimage/ShowImageWindow.cpp @@ -26,7 +26,6 @@ // DEALINGS IN THE SOFTWARE. /*****************************************************************************/ -#include #include #include #include @@ -52,6 +51,7 @@ #include "ShowImageWindow.h" #include "ShowImageView.h" #include "ShowImageStatusView.h" +#include "EntryMenuItem.h" ShowImageWindow::ShowImageWindow(const entry_ref *pref) : BWindow(BRect(50, 50, 350, 250), "", B_DOCUMENT_WINDOW, 0) @@ -180,10 +180,10 @@ ShowImageWindow::UpdateRecentDocumentsMenu() be_roster->GetRecentDocuments(&list, 20, NULL, APP_SIG); for (int i = 0; list.FindRef("refs", i, &ref) == B_OK; i++) { BEntry entry(&ref); - if (entry.GetName(name) == B_OK) { + if (entry.Exists() && entry.GetName(name) == B_OK) { msg = new BMessage(B_REFS_RECEIVED); msg->AddRef("refs", &ref); - item = new BMenuItem(name, msg, 0, 0); + item = new EntryMenuItem(&ref, name, msg, 0, 0); fOpenMenu->AddItem(item); item->SetTarget(be_app, NULL); } diff --git a/src/apps/showimage/ShowImageX.proj b/src/apps/showimage/ShowImageX.proj index 68e1110c7f..791c0caf58 100644 Binary files a/src/apps/showimage/ShowImageX.proj and b/src/apps/showimage/ShowImageX.proj differ