-
Notifications
You must be signed in to change notification settings - Fork 1
/
wxecharts.cpp
146 lines (120 loc) · 4.12 KB
/
wxecharts.cpp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
///////////////////////////////////////////////////////////////////////////////
// Project: wxECharts
// Home: https://github.com/PBfordev/wxecharts
// File Name: wxecharts.cpp
// Purpose: Implementation of the application class
// Author: PB
// Created: 2024-08-22
// Copyright: (c) 2024 PB
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#include <wx/wx.h>
#include <wx/config.h>
#include <wx/filedlg.h>
#include <wx/filename.h>
#include <wx/stdpaths.h>
#include <wx/webview.h>
#include "wxecharts.h"
#include "mainframe.h"
bool wxEChartsApp::OnInit()
{
if ( !wxApp::OnInit() )
return false;
#if USING_WEBVIEW_EDGE
if ( !wxWebView::IsBackendAvailable(wxWebViewBackendEdge) )
{
wxLogError(_("Cannot use wxWebViewEdge backend: Is 'WebView2Loader.dll' in the same folder as the executable?"));
return false;
}
#endif // #if USING_WEBVIEW_EDGE
SetAppName("wxECharts");
SetVendorName("PB");
#ifdef __WXMSW__
wxStandardPaths& standardPaths = wxStandardPaths::Get();
standardPaths.IgnoreAppSubDir("Debug DLL");
standardPaths.IgnoreAppSubDir("Release DLL");
standardPaths.IgnoreAppSubDir("x64");
#endif
delete wxConfigBase::Set(new wxConfig(GetAppName(), GetVendorName()));
const wxString assetsFolder = GetChartAssetsFolder();
if ( assetsFolder.empty() )
{
wxLogError(_("Could not establish the chart assets folder: The application will terminate."));
return false;
}
wxInitAllImageHandlers();
wxEChartsMainFrame* mainFrame = new wxEChartsMainFrame(nullptr, assetsFolder);
mainFrame->Show();
return true;
}
int wxEChartsApp::OnExit()
{
delete wxConfigBase::Set(nullptr);
return wxApp::OnExit();
}
// for demonstration, be flexible when it comes to data assets folder location
wxString wxEChartsApp::GetChartAssetsFolder()
{
static constexpr const char* chartAssets[] =
{"wxecharts.html", "wxecharts.js", "echarts.min.js", };
static constexpr auto assetsFolderName = "chart-assets";
static constexpr auto configKeyName = "ChartAssetsFolder";
const wxStandardPaths& standardPaths = wxStandardPaths::Get();
wxConfigBase* config = wxConfigBase::Get();
const wxConfigPathChanger changer(config, "/");
wxString folder;
wxFileName fn;
auto HasChartAssets = [](const wxString& folder)
{
for ( const auto name : chartAssets )
{
if ( !wxFileName(folder, name).FileExists() )
return false;
}
return true;
};
// first try the path stored in the config, if any
if ( config->Read(configKeyName, &folder) )
{
if ( HasChartAssets(folder) )
return folder;
}
// the standard data dir
fn.AssignDir(standardPaths.GetDataDir());
fn.AppendDir(assetsFolderName);
folder = fn.GetPath();
if ( HasChartAssets(folder) )
return folder;
// the folder with the executable
fn.Assign(standardPaths.GetExecutablePath());
fn.AppendDir(assetsFolderName);
folder = fn.GetPath();
if ( HasChartAssets(folder) )
return folder;
// try the folder above the one with the executable
if ( fn.GetDirCount() > 2 )
{
fn.RemoveDir(fn.GetDirCount() - 2);
folder = fn.GetPath();
if ( HasChartAssets(folder) )
return folder;
}
// could not find, ask the user
for ( ;; )
{
wxFileDialog dlg(nullptr, _("Select the folder with chart assets"), "", chartAssets[0],
_("HTML Files (*.html)|*.html"), wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if ( dlg.ShowModal() != wxID_OK )
break;
fn.Assign(dlg.GetPath());
folder = fn.GetPath();
if ( HasChartAssets(folder) )
{
config->Write(configKeyName, folder);
return folder;
}
}
// assets folder undetermined
return wxEmptyString;
}
wxIMPLEMENT_APP(wxEChartsApp);