From 1d529ef7eed851db9d007821decb9639e4e7fc16 Mon Sep 17 00:00:00 2001 From: Robert Roebling Date: Sun, 15 Feb 2004 00:35:16 +0000 Subject: [PATCH] Updates to MIME-types and wxFileDialog code for better native looking image handling. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@25809 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/generic/dirctrlg.cpp | 30 ++++++--- src/generic/filedlgg.cpp | 9 ++- src/unix/mimetype.cpp | 140 +++++++++++++++++++++++++++++++++------ 3 files changed, 146 insertions(+), 33 deletions(-) diff --git a/src/generic/dirctrlg.cpp b/src/generic/dirctrlg.cpp index 4b8b766b8c..ef8e77e1ad 100644 --- a/src/generic/dirctrlg.cpp +++ b/src/generic/dirctrlg.cpp @@ -1534,7 +1534,9 @@ wxImageList *wxFileIconsTable::GetSmallImageList() static wxBitmap CreateAntialiasedBitmap(const wxImage& img) { - wxImage smallimg (16, 16); + const unsigned int size = 16; + + wxImage smallimg (size, size); unsigned char *p1, *p2, *ps; unsigned char mr = img.GetMaskRed(), mg = img.GetMaskGreen(), @@ -1543,12 +1545,12 @@ static wxBitmap CreateAntialiasedBitmap(const wxImage& img) unsigned x, y; unsigned sr, sg, sb, smask; - p1 = img.GetData(), p2 = img.GetData() + 3 * 32, ps = smallimg.GetData(); + p1 = img.GetData(), p2 = img.GetData() + 3 * size*2, ps = smallimg.GetData(); smallimg.SetMaskColour(mr, mr, mr); - for (y = 0; y < 16; y++) + for (y = 0; y < size; y++) { - for (x = 0; x < 16; x++) + for (x = 0; x < size; x++) { sr = sg = sb = smask = 0; if (p1[0] != mr || p1[1] != mg || p1[2] != mb) @@ -1574,9 +1576,9 @@ static wxBitmap CreateAntialiasedBitmap(const wxImage& img) ps[0] = sr >> 2, ps[1] = sg >> 2, ps[2] = sb >> 2; ps += 3; } - p1 += 32 * 3, p2 += 32 * 3; + p1 += size*2 * 3, p2 += size*2 * 3; } - + return wxBitmap(smallimg); } @@ -1648,10 +1650,10 @@ int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime) wxLogNull logNull; if ( ft && ft->GetIcon(&iconLoc) ) { - ic = wxIcon(iconLoc); + ic = wxIcon( iconLoc.GetFileName() ); } } - + delete ft; if ( !ic.Ok() ) @@ -1671,18 +1673,24 @@ int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime) return newid; } + const unsigned int size = 16; + int id = m_smallImageList->GetImageCount(); - if ((bmp.GetWidth() == 16) && (bmp.GetHeight() == 16)) + if ((bmp.GetWidth() == size) && (bmp.GetHeight() == size)) + { m_smallImageList->Add(bmp); + } else { wxImage img = bmp.ConvertToImage(); - if ((img.GetWidth() != 32) || (img.GetHeight() != 32)) - m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(32, 32))); + if ((img.GetWidth() != size*2) || (img.GetHeight() != size*2)) +// m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2))); + m_smallImageList->Add(CreateAntialiasedBitmap(img.Rescale(size*2, size*2))); else m_smallImageList->Add(CreateAntialiasedBitmap(img)); } + m_HashTable->Put(extension, new wxFileIconEntry(id)); return id; diff --git a/src/generic/filedlgg.cpp b/src/generic/filedlgg.cpp index bcb63b71d0..835bd1ea3b 100644 --- a/src/generic/filedlgg.cpp +++ b/src/generic/filedlgg.cpp @@ -208,10 +208,13 @@ void wxFileData::ReadData() // try to get a better icon if (m_image == wxFileIconsTable::file) { - if (IsExe()) + if (m_fileName.Find(wxT('.'), TRUE) != wxNOT_FOUND) + { + m_image = wxTheFileIconsTable->GetIconID( m_fileName.AfterLast(wxT('.'))); + } else if (IsExe()) + { m_image = wxFileIconsTable::executable; - else if (m_fileName.Find(wxT('.'), TRUE) != wxNOT_FOUND) - m_image = wxTheFileIconsTable->GetIconID(m_fileName.AfterLast(wxT('.'))); + } } m_size = buff.st_size; diff --git a/src/unix/mimetype.cpp b/src/unix/mimetype.cpp index d5f4415784..7d60dc8922 100644 --- a/src/unix/mimetype.cpp +++ b/src/unix/mimetype.cpp @@ -74,6 +74,8 @@ #include "wx/utils.h" #include "wx/tokenzr.h" #include "wx/iconloc.h" +#include "wx/filename.h" +#include "wx/fileconf.h" #include "wx/unix/mimetype.h" @@ -571,17 +573,19 @@ void wxMimeTypesManagerImpl::LoadGnomeDataFromKeyFile(const wxString& filename, else if (sTmp.Contains( wxT("icon_filename=") ) ) { curIconFile = sTmp.AfterFirst(wxT('=')); + if (!wxFileExists(curIconFile)) { size_t nDirs = dirs.GetCount(); for (size_t nDir = 0; nDir < nDirs; nDir++) { - wxString newFile; - newFile.Printf(wxT("%s/pixmaps/document-icons/%s.png"), - dirs[nDir].c_str(), - curIconFile.c_str()); - if (wxFileExists(newFile)) - curIconFile = newFile; + wxFileName newFile( curIconFile ); + newFile.SetPath( dirs[nDir] ); + newFile.AppendDir( wxT("pixmaps") ); + newFile.AppendDir( wxT("document-icons") ); + newFile.SetExt( wxT("png") ); + if (newFile.FileExists()) + curIconFile = newFile.GetFullPath(); } } } @@ -628,11 +632,9 @@ void wxMimeTypesManagerImpl::LoadGnomeDataFromKeyFile(const wxString& filename, // ignore blank lines nLine ++; } // end of while, save any data + if (! curMimeType.empty()) - { AddToMimeData ( curMimeType, curIconFile, entry, strExtensions, strDesc); - } - } @@ -677,7 +679,7 @@ void wxMimeTypesManagerImpl::LoadGnomeMimeTypesFromMimeFile(const wxString& file wxLogTrace(TRACE_MIME, wxT("--- At end of Gnome file finding mimetype %s ---"), curMimeType.c_str()); - + AddMimeTypeInfo(curMimeType, curExtList, wxEmptyString); } @@ -735,7 +737,7 @@ void wxMimeTypesManagerImpl::LoadGnomeMimeFilesFromDir( wxString dirname = dirbase; dirname << wxT("/mime-info"); - + if ( !wxDir::Exists(dirname) ) return; @@ -747,7 +749,8 @@ void wxMimeTypesManagerImpl::LoadGnomeMimeFilesFromDir( dirname += wxT('/'); wxString filename; - bool cont = dir.GetFirst(&filename, _T("*.mime"), wxDIR_FILES); + bool cont; + cont = dir.GetFirst(&filename, _T("*.mime"), wxDIR_FILES); while ( cont ) { LoadGnomeMimeTypesFromMimeFile(dirname + filename); @@ -762,14 +765,45 @@ void wxMimeTypesManagerImpl::LoadGnomeMimeFilesFromDir( cont = dir.GetNext(&filename); } -} + // Hack alert: We scan all icons and deduce the + // mime-type from the file name. + dirname = dirbase; + dirname << wxT("/pixmaps/document-icons"); + + // these are always empty in this file + wxArrayString strExtensions; + wxString strDesc; + + if ( !wxDir::Exists(dirname) ) + return; + wxDir dir2( dirname ); + cont = dir2.GetFirst(&filename, wxT("gnome-*.png"), wxDIR_FILES); + while ( cont ) + { + wxString mimeType = filename; + mimeType.Remove( 0, 6 ); // remove "gnome-" + mimeType.Remove( mimeType.Len()-4, 4 ); // remove ".png" + int pos = mimeType.Find( wxT("-") ); + if (pos != wxNOT_FOUND) + { + mimeType.SetChar( pos, wxT('/') ); + wxString iconFile = dirname; + iconFile << wxT("/"); + iconFile << filename; + AddToMimeData ( mimeType, iconFile, NULL, strExtensions, strDesc, TRUE ); + } + + cont = dir2.GetNext(&filename); + } +} void wxMimeTypesManagerImpl::GetGnomeMimeInfo(const wxString& sExtraDir) { wxArrayString dirs; + dirs.Add(wxT("/opt/gnome/share")); dirs.Add(wxT("/usr/share")); dirs.Add(wxT("/usr/local/share")); @@ -1012,6 +1046,8 @@ void wxMimeTypesManagerImpl::LoadKDELinksForMimeSubtype(const wxString& dirbase, strIcon = file.GetCmd(nIndex); wxLogTrace(TRACE_MIME, wxT(" icon %s"), strIcon.c_str()); //it could be the real path, but more often a short name + + if (!wxFileExists(strIcon)) { // icon is just the short name @@ -1021,12 +1057,16 @@ void wxMimeTypesManagerImpl::LoadKDELinksForMimeSubtype(const wxString& dirbase, // in many locations, at least ~/.kde and $KDEDIR size_t nDir, nDirs = icondirs.GetCount(); for ( nDir = 0; nDir < nDirs; nDir++ ) - if (wxFileExists(icondirs[nDir] + strIcon)) + { + wxFileName fname( icondirs[nDir], strIcon ); + fname.SetExt( wxT("png") ); + if (fname.FileExists()) { - strIcon.Prepend(icondirs[nDir]); + strIcon = fname.GetFullPath(); wxLogTrace(TRACE_MIME, wxT(" iconfile %s"), strIcon.c_str()); break; } + } } } } @@ -1119,13 +1159,55 @@ void wxMimeTypesManagerImpl::GetKDEMimeInfo(const wxString& sExtraDir) { wxArrayString dirs; wxArrayString icondirs; + + // the variable KDEDIR is set when KDE is running + const wxChar *kdedir = wxGetenv( wxT("KDEDIR") ); + + if (kdedir) + { + wxFileName configFile( kdedir, wxEmptyString ); + configFile.AppendDir( wxT("share") ); +#if 0 + configFile.AppendDir( wxT("config") ); + configFile.SetName( wxT("kdeglobals") ); + + wxFileConfig config( wxEmptyString, wxEmptyString, configFile.GetFullPath() ); + wxString theme; + config.SetPath( wxT("Icons") ); + config.Read( wxT("Theme"), theme ); + + configFile.RemoveDir( configFile.GetDirCount()-1 ); + configFile.AppendDir( wxT("icons") ); + configFile.AppendDir( theme ); +#else + configFile.AppendDir( wxT("icons") ); + configFile.AppendDir( wxT("crystalsvg") ); +#endif + + if (!wxDir::Exists(configFile.GetPath())) + { + configFile.RemoveDir( configFile.GetDirCount()-1 ); + configFile.AppendDir( wxT("crystal") ); + } + + if (!wxDir::Exists(configFile.GetPath())) + { + configFile.RemoveDir( configFile.GetDirCount()-1 ); + configFile.AppendDir( wxT("default.kde") ); + } + + if (wxDir::Exists(configFile.GetPath())) + { + configFile.AppendDir( wxT("32x32") ); + configFile.AppendDir( wxT("mimetypes") ); + icondirs.Add( configFile.GetFullPath() ); + } + } // settings in ~/.kde have maximal priority dirs.Add(wxGetHomeDir() + wxT("/.kde/share")); icondirs.Add(wxGetHomeDir() + wxT("/.kde/share/icons/")); - // the variable KDEDIR is set when KDE is running - const wxChar *kdedir = wxGetenv( wxT("KDEDIR") ); if ( kdedir ) { dirs.Add( wxString(kdedir) + wxT("/share") ); @@ -1341,6 +1423,26 @@ wxMimeTypesManagerImpl::wxMimeTypesManagerImpl() m_mailcapStylesInited = 0; } +void wxMimeTypesManagerImpl::InitIfNeeded() +{ + if ( !m_initialized ) + { + // set the flag first to prevent recursion + m_initialized = TRUE; + +#if 0 + wxString wm = wxGetenv( wxT("WINDOWMANAGER") ); + + if (wm.Find( wxT("kde") ) != wxNOT_FOUND) + Initialize( wxMAILCAP_KDE|wxMAILCAP_STANDARD ); + else if (wm.Find( wxT("gnome") ) != wxNOT_FOUND) + Initialize( wxMAILCAP_GNOME|wxMAILCAP_STANDARD ); + else +#endif + Initialize(); + } +} + // read system and user mailcaps and other files void wxMimeTypesManagerImpl::Initialize(int mailcapStyles, const wxString& sExtraDir) @@ -1351,11 +1453,11 @@ void wxMimeTypesManagerImpl::Initialize(int mailcapStyles, GetMimeInfo(sExtraDir); // read GNOME tables - if ( mailcapStyles & wxMAILCAP_GNOME) + if (mailcapStyles & wxMAILCAP_GNOME) GetGnomeMimeInfo(sExtraDir); // read KDE tables - if ( mailcapStyles & wxMAILCAP_KDE) + if (mailcapStyles & wxMAILCAP_KDE) GetKDEMimeInfo(sExtraDir); m_mailcapStylesInited |= mailcapStyles; -- 2.45.2