Add support for returning item location to wxGTK wxMimeTypesManager.

This is a squash merge of gtk_mimetype branch from
https://github.com/Hanmac/wxWidgets.git with some extra minor cleanup.

Closes https://github.com/wxWidgets/wxWidgets/pull/293
This commit is contained in:
Vadim Zeitlin
2016-06-07 14:27:10 +02:00
parent 5f01172165
commit 4dfde501df
8 changed files with 107 additions and 3 deletions

View File

@@ -92,6 +92,7 @@ wxGTK:
- Implement setting link colours in wxHyperlinkCtrl for GTK+3 (Hanmac).
- Support background colour in wxDataViewCtrl attributes.
- Improve wxSpinCtrl best size calculation.
- Implement support for icon locations in wxMimeTypesManager (Hanmac).
wxMSW:

32
include/wx/gtk/mimetype.h Normal file
View File

@@ -0,0 +1,32 @@
/////////////////////////////////////////////////////////////////////////////
// Name: wx/gtk/mimetype.h
// Purpose: classes and functions to manage MIME types
// Author: Hans Mackowiak
// Created: 2016-06-05
// Copyright: (c) 2016 Hans Mackowiak <hanmac@gmx.de>
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#ifndef _WX_GTK_MIMETYPE_IMPL_H
#define _WX_GTK_MIMETYPE_IMPL_H
#include "wx/unix/mimetype.h"
#if wxUSE_MIMETYPE
class WXDLLIMPEXP_CORE wxGTKMimeTypesManagerImpl : public wxMimeTypesManagerImpl
{
protected:
wxString GetIconFromMimeType(const wxString& mime) wxOVERRIDE;
};
class WXDLLIMPEXP_CORE wxGTKMimeTypesManagerFactory : public wxMimeTypesManagerFactory
{
public:
wxMimeTypesManagerImpl *CreateMimeTypesManagerImpl() wxOVERRIDE;
};
#endif // wxUSE_MIMETYPE
#endif // _WX_GTK_MIMETYPE_IMPL_H

View File

@@ -19,7 +19,7 @@ class wxGtkObject
{
public:
explicit wxGtkObject(T *p) : m_ptr(p) { }
~wxGtkObject() { g_object_unref(m_ptr); }
~wxGtkObject() { if ( m_ptr ) g_object_unref(m_ptr); }
operator T *() const { return m_ptr; }

View File

@@ -99,6 +99,8 @@ protected:
const wxArrayString& strExtensions,
const wxString& strDesc);
virtual wxString GetIconFromMimeType(const wxString& mime);
// give it access to m_aXXX variables
friend class WXDLLIMPEXP_FWD_BASE wxFileTypeImpl;
};

View File

@@ -945,8 +945,11 @@ void MyApp::DoMIMEDemo(wxCommandEvent& WXUNUSED(event))
else
{
wxString type, desc, open;
wxIconLocation loc;
filetype->GetMimeType(&type);
filetype->GetDescription(&desc);
filetype->GetIcon(&loc);
wxString filename = wxT("filename");
filename << wxT(".") << ext;
@@ -958,6 +961,8 @@ void MyApp::DoMIMEDemo(wxCommandEvent& WXUNUSED(event))
<< wxT("\tDescription: ") << ( !desc ? wxString(wxEmptyString) : desc )
<< wxT('\n')
<< wxT("\tCommand to open: ") << ( !open ? wxString("no") : open )
<< wxT('\n')
<< wxT("\tIcon: ") << ( loc.IsOk() ? loc.GetFileName() : wxString("no") )
<< wxT('\n');
delete filetype;

View File

@@ -31,6 +31,7 @@
#include <gtk/gtk.h>
#include "wx/gtk/private.h"
#include "wx/gtk/mimetype.h"
//-----------------------------------------------------------------------------
// link GnomeVFS
//-----------------------------------------------------------------------------
@@ -435,6 +436,10 @@ bool wxApp::Initialize(int& argc_, wxChar **argv_)
return false;
}
#if wxUSE_MIMETYPE
wxMimeTypesManagerFactory::Set(new wxGTKMimeTypesManagerFactory());
#endif
// we cannot enter threads before gtk_init is done
gdk_threads_enter();

52
src/gtk/mimetype.cpp Normal file
View File

@@ -0,0 +1,52 @@
/////////////////////////////////////////////////////////////////////////////
// Name: src/gtk/mimetype.cpp
// Purpose: classes and functions to manage MIME types
// Author: Hans Mackowiak
// Created: 2016-06-05
// Copyright: (c) 2016 Hans Mackowiak <hanmac@gmx.de>
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#include "wx/wxprec.h"
#if wxUSE_MIMETYPE
#include "wx/gtk/mimetype.h"
#include <gio/gio.h>
#include <gtk/gtk.h>
#include "wx/gtk/private/string.h"
#include "wx/gtk/private/object.h"
wxString wxGTKMimeTypesManagerImpl::GetIconFromMimeType(const wxString& mime)
{
wxGtkString type(g_content_type_from_mime_type(mime.utf8_str()));
wxGtkObject<GIcon> gicon(g_content_type_get_icon(type));
if ( !gicon )
return wxString();
GtkIconTheme *theme(gtk_icon_theme_get_default());
if ( !theme )
return wxString();
wxGtkObject<GtkIconInfo> giconinfo(gtk_icon_theme_lookup_by_gicon
(
theme,
gicon,
256,
GTK_ICON_LOOKUP_NO_SVG
));
if ( !giconinfo )
return wxString();
return wxString::FromUTF8(gtk_icon_info_get_filename(giconinfo));
}
wxMimeTypesManagerImpl *wxGTKMimeTypesManagerFactory::CreateMimeTypesManagerImpl()
{
return new wxGTKMimeTypesManagerImpl();
}
#endif // wxUSE_MIMETYPE

View File

@@ -262,7 +262,9 @@ void wxMimeTypesManagerImpl::LoadXDGGlobs(const wxString& filename)
wxArrayString exts;
exts.Add( ext );
AddToMimeData(mime, wxEmptyString, NULL, exts, wxEmptyString, true );
wxString icon = GetIconFromMimeType(mime);
AddToMimeData(mime, icon, NULL, exts, wxEmptyString, true );
}
}
@@ -687,6 +689,11 @@ wxFileType * wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo& ftInfo)
return GetFileTypeFromMimeType(strType);
}
wxString wxMimeTypesManagerImpl::GetIconFromMimeType(const wxString& WXUNUSED(mime))
{
return wxString();
}
bool wxMimeTypesManagerImpl::DoAssociation(const wxString& strType,
const wxString& strIcon,
wxMimeTypeCommands *entry,
@@ -967,7 +974,7 @@ void wxMimeTypesManagerImpl::AddMimeTypeInfo(const wxString& strMimeType,
// reading mailcap may find image/* , while
// reading mime.types finds image/gif and no match is made
// this means all the get functions don't work fix this
wxString strIcon;
const wxString strIcon = GetIconFromMimeType(strMimeType);
wxString sTmp = strExtensions;
wxArrayString sExts;