summaryrefslogtreecommitdiff
path: root/Source/Core/DolphinWX/WxUtils.cpp
blob: 14f5977e53c050557c25eb2b761e94c89f683d40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2009 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <string>
#include <wx/app.h>
#include <wx/bitmap.h>
#include <wx/gdicmn.h>
#include <wx/image.h>
#include <wx/msgdlg.h>
#include <wx/mstream.h>
#include <wx/toolbar.h>
#include <wx/utils.h>

#include "Common/CommonPaths.h"
#include "Common/FileUtil.h"

#include "DolphinWX/WxUtils.h"

#ifdef __APPLE__
#import <AppKit/AppKit.h>
#endif

namespace WxUtils
{

// Launch a file according to its mime type
void Launch(const std::string& filename)
{
	if (! ::wxLaunchDefaultBrowser(StrToWxStr(filename)))
	{
		// WARN_LOG
	}
}

// Launch an file explorer window on a certain path
void Explore(const std::string& path)
{
	wxString wxPath = StrToWxStr(path);
#ifndef _WIN32
	// Default to file
	if (! wxPath.Contains("://"))
	{
		wxPath = "file://" + wxPath;
	}
#endif

#ifdef __WXGTK__
	wxPath.Replace(" ", "\\ ");
#endif

	if (! ::wxLaunchDefaultBrowser(wxPath))
	{
		// WARN_LOG
	}
}

void ShowErrorDialog(const wxString& error_msg)
{
	wxMessageBox(error_msg, _("Error"), wxOK | wxICON_ERROR);
}

wxBitmap LoadResourceBitmap(const std::string& name, const wxSize& padded_size)
{
	const std::string path_base = File::GetSysDirectory() + RESOURCES_DIR + DIR_SEP + name;
	std::string path = path_base + ".png";
	double scale_factor = 1.0;
#ifdef __APPLE__
	if (wxTheApp->GetTopWindow()->GetContentScaleFactor() >= 2)
	{
		const std::string path_2x = path_base + "@2x.png";
		if (File::Exists(path_2x))
		{
			path = path_2x;
			scale_factor = 2.0;
		}
	}
#endif
	wxImage image(StrToWxStr(path), wxBITMAP_TYPE_PNG);

	if (padded_size != wxSize())
	{
		// Add padding if necessary (or crop, but images aren't supposed to be large enough to require that).
		// The image will be left-aligned and vertically centered.
		const wxSize scaled_padded_size = padded_size * scale_factor;
		image.Resize(scaled_padded_size, wxPoint(0, (scaled_padded_size.GetHeight() - image.GetHeight()) / 2));
	}

#ifdef __APPLE__
	return wxBitmap(image, -1, scale_factor);
#else
	return wxBitmap(image);
#endif
}

wxBitmap CreateDisabledButtonBitmap(const wxBitmap& original)
{
	wxImage image = original.ConvertToImage();
	return wxBitmap(image.ConvertToDisabled(240));
}

void AddToolbarButton(wxToolBar* toolbar, int toolID, const wxString& label, const wxBitmap& bitmap, const wxString& shortHelp)
{
	// Must explicitly set the disabled button bitmap because wxWidgets
	// incorrectly desaturates it instead of lightening it.
	toolbar->AddTool(toolID, label, bitmap, WxUtils::CreateDisabledButtonBitmap(bitmap), wxITEM_NORMAL, shortHelp);
}

}  // namespace

std::string WxStrToStr(const wxString& str)
{
	return str.ToUTF8().data();
}

wxString StrToWxStr(const std::string& str)
{
	//return wxString::FromUTF8Unchecked(str.c_str());
	return wxString::FromUTF8(str.c_str());
}