1 #include "libslic3r/Technologies.hpp"
2 #include "GUI_App.hpp"
3 #include "GUI_Init.hpp"
4 #include "GUI_ObjectList.hpp"
5 #include "GUI_ObjectManipulation.hpp"
6 #include "format.hpp"
7 #include "I18N.hpp"
8 
9 #include <algorithm>
10 #include <iterator>
11 #include <exception>
12 #include <cstdlib>
13 #include <regex>
14 #include <boost/algorithm/string.hpp>
15 #include <boost/format.hpp>
16 #include <boost/lexical_cast.hpp>
17 #include <boost/log/trivial.hpp>
18 #include <boost/nowide/convert.hpp>
19 
20 #include <wx/stdpaths.h>
21 #include <wx/imagpng.h>
22 #include <wx/display.h>
23 #include <wx/menu.h>
24 #include <wx/menuitem.h>
25 #include <wx/filedlg.h>
26 #include <wx/progdlg.h>
27 #include <wx/dir.h>
28 #include <wx/wupdlock.h>
29 #include <wx/filefn.h>
30 #include <wx/sysopt.h>
31 #include <wx/richmsgdlg.h>
32 #include <wx/log.h>
33 #include <wx/intl.h>
34 
35 #include <wx/dialog.h>
36 #include <wx/textctrl.h>
37 #include <wx/splash.h>
38 #include <wx/fontutil.h>
39 
40 #include "libslic3r/Utils.hpp"
41 #include "libslic3r/Model.hpp"
42 #include "libslic3r/I18N.hpp"
43 #include "libslic3r/PresetBundle.hpp"
44 
45 #include "GUI.hpp"
46 #include "GUI_Utils.hpp"
47 #include "3DScene.hpp"
48 #include "MainFrame.hpp"
49 #include "Plater.hpp"
50 #include "GLCanvas3D.hpp"
51 
52 #include "../Utils/PresetUpdater.hpp"
53 #include "../Utils/PrintHost.hpp"
54 #include "../Utils/Process.hpp"
55 #include "../Utils/MacDarkMode.hpp"
56 #include "slic3r/Config/Snapshot.hpp"
57 #include "ConfigSnapshotDialog.hpp"
58 #include "FirmwareDialog.hpp"
59 #include "Preferences.hpp"
60 #include "Tab.hpp"
61 #include "SysInfoDialog.hpp"
62 #include "KBShortcutsDialog.hpp"
63 #include "UpdateDialogs.hpp"
64 #include "Mouse3DController.hpp"
65 #include "RemovableDriveManager.hpp"
66 #include "InstanceCheck.hpp"
67 #include "NotificationManager.hpp"
68 #include "UnsavedChangesDialog.hpp"
69 #include "SavePresetDialog.hpp"
70 #include "PrintHostDialogs.hpp"
71 
72 #include "BitmapCache.hpp"
73 
74 #ifdef __WXMSW__
75 #include <dbt.h>
76 #include <shlobj.h>
77 #endif // __WXMSW__
78 
79 #if ENABLE_THUMBNAIL_GENERATOR_DEBUG
80 #include <boost/beast/core/detail/base64.hpp>
81 #include <boost/nowide/fstream.hpp>
82 #endif // ENABLE_THUMBNAIL_GENERATOR_DEBUG
83 
84 namespace Slic3r {
85 namespace GUI {
86 
87 class MainFrame;
88 
89 class SplashScreen : public wxSplashScreen
90 {
91 public:
SplashScreen(const wxBitmap & bitmap,long splashStyle,int milliseconds,wxPoint pos=wxDefaultPosition)92     SplashScreen(const wxBitmap& bitmap, long splashStyle, int milliseconds, wxPoint pos = wxDefaultPosition)
93         : wxSplashScreen(bitmap, splashStyle, milliseconds, static_cast<wxWindow*>(wxGetApp().mainframe), wxID_ANY, wxDefaultPosition, wxDefaultSize,
94 #ifdef __APPLE__
95             wxSIMPLE_BORDER | wxFRAME_NO_TASKBAR | wxSTAY_ON_TOP
96 #else
97             wxSIMPLE_BORDER | wxFRAME_NO_TASKBAR
98 #endif // !__APPLE__
99         )
100     {
101         wxASSERT(bitmap.IsOk());
102 
103         int init_dpi = get_dpi_for_window(this);
104         this->SetPosition(pos);
105         this->CenterOnScreen();
106         int new_dpi = get_dpi_for_window(this);
107 
108         m_scale         = (float)(new_dpi) / (float)(init_dpi);
109         m_main_bitmap   = bitmap;
110 
111         scale_bitmap(m_main_bitmap, m_scale);
112 
113         // init constant texts and scale fonts
114         init_constant_text();
115 
116         // this font will be used for the action string
117         m_action_font = m_constant_text.credits_font.Bold();
118 
119         // draw logo and constant info text
120         Decorate(m_main_bitmap);
121     }
122 
SetText(const wxString & text)123     void SetText(const wxString& text)
124     {
125         set_bitmap(m_main_bitmap);
126         if (!text.empty()) {
127             wxBitmap bitmap(m_main_bitmap);
128 
129             wxMemoryDC memDC;
130             memDC.SelectObject(bitmap);
131 
132             memDC.SetFont(m_action_font);
133             memDC.SetTextForeground(wxColour(237, 107, 33));
134             memDC.DrawText(text, int(m_scale * 60), m_action_line_y_position);
135 
136             memDC.SelectObject(wxNullBitmap);
137             set_bitmap(bitmap);
138 #ifdef __WXOSX__
139             // without this code splash screen wouldn't be updated under OSX
140             wxYield();
141 #endif
142         }
143     }
144 
MakeBitmap(wxBitmap bmp)145     static wxBitmap MakeBitmap(wxBitmap bmp)
146     {
147         if (!bmp.IsOk())
148             return wxNullBitmap;
149 
150         // create dark grey background for the splashscreen
151         // It will be 5/3 of the weight of the bitmap
152         int width = lround((double)5 / 3 * bmp.GetWidth());
153         int height = bmp.GetHeight();
154 
155         wxImage image(width, height);
156         unsigned char* imgdata_ = image.GetData();
157         for (int i = 0; i < width * height; ++i) {
158             *imgdata_++ = 51;
159             *imgdata_++ = 51;
160             *imgdata_++ = 51;
161         }
162 
163         wxBitmap new_bmp(image);
164 
165         wxMemoryDC memDC;
166         memDC.SelectObject(new_bmp);
167         memDC.DrawBitmap(bmp, width - bmp.GetWidth(), 0, true);
168 
169         return new_bmp;
170     }
171 
Decorate(wxBitmap & bmp)172     void Decorate(wxBitmap& bmp)
173     {
174         if (!bmp.IsOk())
175             return;
176 
177         // draw text to the box at the left of the splashscreen.
178         // this box will be 2/5 of the weight of the bitmap, and be at the left.
179         int width = lround(bmp.GetWidth() * 0.4);
180 
181         // load bitmap for logo
182         BitmapCache bmp_cache;
183         int logo_size = lround(width * 0.25);
184         wxBitmap logo_bmp = *bmp_cache.load_svg(wxGetApp().is_editor() ? "prusa_slicer_logo" : "add_gcode", logo_size, logo_size);
185 
186         wxCoord margin = int(m_scale * 20);
187 
188         wxRect banner_rect(wxPoint(0, logo_size), wxPoint(width, bmp.GetHeight()));
189         banner_rect.Deflate(margin, 2 * margin);
190 
191         // use a memory DC to draw directly onto the bitmap
192         wxMemoryDC memDc(bmp);
193 
194         // draw logo
195         memDc.DrawBitmap(logo_bmp, margin, margin, true);
196 
197         // draw the (white) labels inside of our black box (at the left of the splashscreen)
198         memDc.SetTextForeground(wxColour(255, 255, 255));
199 
200         memDc.SetFont(m_constant_text.title_font);
201         memDc.DrawLabel(m_constant_text.title,   banner_rect, wxALIGN_TOP | wxALIGN_LEFT);
202 
203         int title_height = memDc.GetTextExtent(m_constant_text.title).GetY();
204         banner_rect.SetTop(banner_rect.GetTop() + title_height);
205         banner_rect.SetHeight(banner_rect.GetHeight() - title_height);
206 
207         memDc.SetFont(m_constant_text.version_font);
208         memDc.DrawLabel(m_constant_text.version, banner_rect, wxALIGN_TOP | wxALIGN_LEFT);
209         int version_height = memDc.GetTextExtent(m_constant_text.version).GetY();
210 
211         memDc.SetFont(m_constant_text.credits_font);
212         memDc.DrawLabel(m_constant_text.credits, banner_rect, wxALIGN_BOTTOM | wxALIGN_LEFT);
213         int credits_height = memDc.GetMultiLineTextExtent(m_constant_text.credits).GetY();
214         int text_height    = memDc.GetTextExtent("text").GetY();
215 
216         // calculate position for the dynamic text
217         int logo_and_header_height = margin + logo_size + title_height + version_height;
218         m_action_line_y_position = logo_and_header_height + 0.5 * (bmp.GetHeight() - margin - credits_height - logo_and_header_height - text_height);
219     }
220 
221 private:
222     wxBitmap    m_main_bitmap;
223     wxFont      m_action_font;
224     int         m_action_line_y_position;
225     float       m_scale {1.0};
226 
227     struct ConstantText
228     {
229         wxString title;
230         wxString version;
231         wxString credits;
232 
233         wxFont   title_font;
234         wxFont   version_font;
235         wxFont   credits_font;
236 
initSlic3r::GUI::SplashScreen::ConstantText237         void init(wxFont init_font)
238         {
239             // title
240             title = wxGetApp().is_editor() ? SLIC3R_APP_NAME : GCODEVIEWER_APP_NAME;
241 
242             // dynamically get the version to display
243             version = _L("Version") + " " + std::string(SLIC3R_VERSION);
244 
245             // credits infornation
246             credits =   title + " " +
247                         _L("is based on Slic3r by Alessandro Ranellucci and the RepRap community.") + "\n\n" +
248                         title + " " + _L("is licensed under the") + " " + _L("GNU Affero General Public License, version 3") + "\n\n" +
249                         _L("Contributions by Vojtech Bubnik, Enrico Turri, Oleksandra Iushchenko, Tamas Meszaros, Lukas Matena, Vojtech Kral, David Kocik and numerous others.") + "\n\n" +
250                         _L("Artwork model by Nora Al-Badri and Jan Nikolai Nelles");
251 
252             title_font = version_font = credits_font = init_font;
253         }
254     }
255     m_constant_text;
256 
init_constant_text()257     void init_constant_text()
258     {
259         m_constant_text.init(get_default_font(this));
260 
261         // As default we use a system font for current display.
262         // Scale fonts in respect to banner width
263 
264         int text_banner_width = lround(0.4 * m_main_bitmap.GetWidth()) - roundl(m_scale * 50); // banner_width - margins
265 
266         float title_font_scale = (float)text_banner_width / GetTextExtent(m_constant_text.title).GetX();
267         scale_font(m_constant_text.title_font, title_font_scale > 3.5f ? 3.5f : title_font_scale);
268 
269         float version_font_scale = (float)text_banner_width / GetTextExtent(m_constant_text.version).GetX();
270         scale_font(m_constant_text.version_font, version_font_scale > 2.f ? 2.f : version_font_scale);
271 
272         // The width of the credits information string doesn't respect to the banner width some times.
273         // So, scale credits_font in the respect to the longest string width
274         int   longest_string_width = word_wrap_string(m_constant_text.credits);
275         float font_scale = (float)text_banner_width / longest_string_width;
276         scale_font(m_constant_text.credits_font, font_scale);
277     }
278 
set_bitmap(wxBitmap & bmp)279     void set_bitmap(wxBitmap& bmp)
280     {
281         m_window->SetBitmap(bmp);
282         m_window->Refresh();
283         m_window->Update();
284     }
285 
scale_bitmap(wxBitmap & bmp,float scale)286     void scale_bitmap(wxBitmap& bmp, float scale)
287     {
288         if (scale == 1.0)
289             return;
290 
291         wxImage image = bmp.ConvertToImage();
292         if (!image.IsOk() || image.GetWidth() == 0 || image.GetHeight() == 0)
293             return;
294 
295         int width   = int(scale * image.GetWidth());
296         int height  = int(scale * image.GetHeight());
297         image.Rescale(width, height, wxIMAGE_QUALITY_BILINEAR);
298 
299         bmp = wxBitmap(std::move(image));
300     }
301 
scale_font(wxFont & font,float scale)302     void scale_font(wxFont& font, float scale)
303     {
304 #ifdef __WXMSW__
305         // Workaround for the font scaling in respect to the current active display,
306         // not for the primary display, as it's implemented in Font.cpp
307         // See https://github.com/wxWidgets/wxWidgets/blob/master/src/msw/font.cpp
308         // void wxNativeFontInfo::SetFractionalPointSize(float pointSizeNew)
309         wxNativeFontInfo nfi= *font.GetNativeFontInfo();
310         float pointSizeNew  = scale * font.GetPointSize();
311         nfi.lf.lfHeight     = nfi.GetLogFontHeightAtPPI(pointSizeNew, get_dpi_for_window(this));
312         nfi.pointSize       = pointSizeNew;
313         font = wxFont(nfi);
314 #else
315         font.Scale(scale);
316 #endif //__WXMSW__
317     }
318 
319     // wrap a string for the strings no longer then 55 symbols
320     // return extent of the longest string
word_wrap_string(wxString & input)321     int word_wrap_string(wxString& input)
322     {
323         size_t line_len = 55;// count of symbols in one line
324         int idx = -1;
325         size_t cur_len = 0;
326 
327         wxString longest_sub_string;
328         auto get_longest_sub_string = [input](wxString &longest_sub_str, size_t cur_len, size_t i) {
329             if (cur_len > longest_sub_str.Len())
330                 longest_sub_str = input.SubString(i - cur_len + 1, i);
331         };
332 
333         for (size_t i = 0; i < input.Len(); i++)
334         {
335             cur_len++;
336             if (input[i] == ' ')
337                 idx = i;
338             if (input[i] == '\n')
339             {
340                 get_longest_sub_string(longest_sub_string, cur_len, i);
341                 idx = -1;
342                 cur_len = 0;
343             }
344             if (cur_len >= line_len && idx >= 0)
345             {
346                 get_longest_sub_string(longest_sub_string, cur_len, i);
347                 input[idx] = '\n';
348                 cur_len = i - static_cast<size_t>(idx);
349             }
350         }
351 
352         return GetTextExtent(longest_sub_string).GetX();
353     }
354 };
355 
356 
357 #ifdef __linux__
check_old_linux_datadir(const wxString & app_name)358 bool static check_old_linux_datadir(const wxString& app_name) {
359     // If we are on Linux and the datadir does not exist yet, look into the old
360     // location where the datadir was before version 2.3. If we find it there,
361     // tell the user that he might wanna migrate to the new location.
362     // (https://github.com/prusa3d/PrusaSlicer/issues/2911)
363     // To be precise, the datadir should exist, it is created when single instance
364     // lock happens. Instead of checking for existence, check the contents.
365 
366     namespace fs = boost::filesystem;
367 
368     std::string new_path = Slic3r::data_dir();
369 
370     wxString dir;
371     if (! wxGetEnv(wxS("XDG_CONFIG_HOME"), &dir) || dir.empty() )
372         dir = wxFileName::GetHomeDir() + wxS("/.config");
373     std::string default_path = (dir + "/" + app_name).ToUTF8().data();
374 
375     if (new_path != default_path) {
376         // This happens when the user specifies a custom --datadir.
377         // Do not show anything in that case.
378         return true;
379     }
380 
381     fs::path data_dir = fs::path(new_path);
382     if (! fs::is_directory(data_dir))
383         return true; // This should not happen.
384 
385     int file_count = std::distance(fs::directory_iterator(data_dir), fs::directory_iterator());
386 
387     if (file_count <= 1) { // just cache dir with an instance lock
388         std::string old_path = wxStandardPaths::Get().GetUserDataDir().ToUTF8().data();
389 
390         if (fs::is_directory(old_path)) {
391             wxString msg = from_u8((boost::format(_u8L("Starting with %1% 2.3, configuration "
392                 "directory on Linux has changed (according to XDG Base Directory Specification) to \n%2%.\n\n"
393                 "This directory did not exist yet (maybe you run the new version for the first time).\nHowever, "
394                 "an old %1% configuration directory was detected in \n%3%.\n\n"
395                 "Consider moving the contents of the old directory to the new location in order to access "
396                 "your profiles, etc.\nNote that if you decide to downgrade %1% in future, it will use the old "
397                 "location again.\n\n"
398                 "What do you want to do now?")) % SLIC3R_APP_NAME % new_path % old_path).str());
399             wxString caption = from_u8((boost::format(_u8L("%s - BREAKING CHANGE")) % SLIC3R_APP_NAME).str());
400             wxRichMessageDialog dlg(nullptr, msg, caption, wxYES_NO);
401             dlg.SetYesNoLabels(_L("Quit, I will move my data now"), _L("Start the application"));
402             if (dlg.ShowModal() != wxID_NO)
403                 return false;
404         }
405     } else {
406         // If the new directory exists, be silent. The user likely already saw the message.
407     }
408     return true;
409 }
410 #endif
411 
412 
file_wildcards(FileType file_type,const std::string & custom_extension)413 wxString file_wildcards(FileType file_type, const std::string &custom_extension)
414 {
415     static const std::string defaults[FT_SIZE] = {
416         /* FT_STL */     "STL files (*.stl)|*.stl;*.STL",
417         /* FT_OBJ */     "OBJ files (*.obj)|*.obj;*.OBJ",
418         /* FT_AMF */     "AMF files (*.amf)|*.zip.amf;*.amf;*.AMF;*.xml;*.XML",
419         /* FT_3MF */     "3MF files (*.3mf)|*.3mf;*.3MF;",
420         /* FT_PRUSA */   "Prusa Control files (*.prusa)|*.prusa;*.PRUSA",
421         /* FT_GCODE */   "G-code files (*.gcode, *.gco, *.g, *.ngc)|*.gcode;*.GCODE;*.gco;*.GCO;*.g;*.G;*.ngc;*.NGC",
422         /* FT_MODEL */   "Known files (*.stl, *.obj, *.amf, *.xml, *.3mf, *.prusa)|*.stl;*.STL;*.obj;*.OBJ;*.amf;*.AMF;*.xml;*.XML;*.3mf;*.3MF;*.prusa;*.PRUSA",
423         /* FT_PROJECT */ "Project files (*.3mf, *.amf)|*.3mf;*.3MF;*.amf;*.AMF",
424 
425         /* FT_INI */     "INI files (*.ini)|*.ini;*.INI",
426         /* FT_SVG */     "SVG files (*.svg)|*.svg;*.SVG",
427 
428         /* FT_TEX */     "Texture (*.png, *.svg)|*.png;*.PNG;*.svg;*.SVG",
429 
430         /* FT_SL1 */     "Masked SLA files (*.sl1, *.sl1s)|*.sl1;*.SL1;*.sl1s;*.SL1S",
431         // Workaround for OSX file picker, for some reason it always saves with the 1st extension.
432         /* FT_SL1S */    "Masked SLA files (*.sl1s, *.sl1)|*.sl1s;*.SL1S;*.sl1;*.SL1",
433     };
434 
435 	std::string out = defaults[file_type];
436     if (! custom_extension.empty()) {
437         // Find the custom extension in the template.
438         if (out.find(std::string("*") + custom_extension + ",") == std::string::npos && out.find(std::string("*") + custom_extension + ")") == std::string::npos) {
439             // The custom extension was not found in the template.
440             // Append the custom extension to the wildcards, so that the file dialog would not add the default extension to it.
441 			boost::replace_first(out, ")|", std::string(", *") + custom_extension + ")|");
442 			out += std::string(";*") + custom_extension;
443         }
444     }
445     return from_u8(out);
446 }
447 
libslic3r_translate_callback(const char * s)448 static std::string libslic3r_translate_callback(const char *s) { return wxGetTranslation(wxString(s, wxConvUTF8)).utf8_str().data(); }
449 
450 #ifdef WIN32
451 #if !wxVERSION_EQUAL_OR_GREATER_THAN(3,1,3)
register_win32_dpi_event()452 static void register_win32_dpi_event()
453 {
454     enum { WM_DPICHANGED_ = 0x02e0 };
455 
456     wxWindow::MSWRegisterMessageHandler(WM_DPICHANGED_, [](wxWindow *win, WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) {
457         const int dpi = wParam & 0xffff;
458         const auto rect = reinterpret_cast<PRECT>(lParam);
459         const wxRect wxrect(wxPoint(rect->top, rect->left), wxPoint(rect->bottom, rect->right));
460 
461         DpiChangedEvent evt(EVT_DPI_CHANGED_SLICER, dpi, wxrect);
462         win->GetEventHandler()->AddPendingEvent(evt);
463 
464         return true;
465     });
466 }
467 #endif // !wxVERSION_EQUAL_OR_GREATER_THAN
468 
469 static GUID GUID_DEVINTERFACE_HID = { 0x4D1E55B2, 0xF16F, 0x11CF, 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 };
470 
register_win32_device_notification_event()471 static void register_win32_device_notification_event()
472 {
473     wxWindow::MSWRegisterMessageHandler(WM_DEVICECHANGE, [](wxWindow *win, WXUINT /* nMsg */, WXWPARAM wParam, WXLPARAM lParam) {
474         // Some messages are sent to top level windows by default, some messages are sent to only registered windows, and we explictely register on MainFrame only.
475         auto main_frame = dynamic_cast<MainFrame*>(win);
476         auto plater = (main_frame == nullptr) ? nullptr : main_frame->plater();
477         if (plater == nullptr)
478             // Maybe some other top level window like a dialog or maybe a pop-up menu?
479             return true;
480 		PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
481         switch (wParam) {
482         case DBT_DEVICEARRIVAL:
483 			if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
484 		        plater->GetEventHandler()->AddPendingEvent(VolumeAttachedEvent(EVT_VOLUME_ATTACHED));
485 			else if (lpdb->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
486 				PDEV_BROADCAST_DEVICEINTERFACE lpdbi = (PDEV_BROADCAST_DEVICEINTERFACE)lpdb;
487 //				if (lpdbi->dbcc_classguid == GUID_DEVINTERFACE_VOLUME) {
488 //					printf("DBT_DEVICEARRIVAL %d - Media has arrived: %ws\n", msg_count, lpdbi->dbcc_name);
489 				if (lpdbi->dbcc_classguid == GUID_DEVINTERFACE_HID)
490 			        plater->GetEventHandler()->AddPendingEvent(HIDDeviceAttachedEvent(EVT_HID_DEVICE_ATTACHED, boost::nowide::narrow(lpdbi->dbcc_name)));
491 			}
492             break;
493 		case DBT_DEVICEREMOVECOMPLETE:
494 			if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
495                 plater->GetEventHandler()->AddPendingEvent(VolumeDetachedEvent(EVT_VOLUME_DETACHED));
496 			else if (lpdb->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
497 				PDEV_BROADCAST_DEVICEINTERFACE lpdbi = (PDEV_BROADCAST_DEVICEINTERFACE)lpdb;
498 //				if (lpdbi->dbcc_classguid == GUID_DEVINTERFACE_VOLUME)
499 //					printf("DBT_DEVICEARRIVAL %d - Media was removed: %ws\n", msg_count, lpdbi->dbcc_name);
500 				if (lpdbi->dbcc_classguid == GUID_DEVINTERFACE_HID)
501         			plater->GetEventHandler()->AddPendingEvent(HIDDeviceDetachedEvent(EVT_HID_DEVICE_DETACHED, boost::nowide::narrow(lpdbi->dbcc_name)));
502 			}
503 			break;
504         default:
505             break;
506         }
507         return true;
508     });
509 
510     wxWindow::MSWRegisterMessageHandler(MainFrame::WM_USER_MEDIACHANGED, [](wxWindow *win, WXUINT /* nMsg */, WXWPARAM wParam, WXLPARAM lParam) {
511         // Some messages are sent to top level windows by default, some messages are sent to only registered windows, and we explictely register on MainFrame only.
512         auto main_frame = dynamic_cast<MainFrame*>(win);
513         auto plater = (main_frame == nullptr) ? nullptr : main_frame->plater();
514         if (plater == nullptr)
515             // Maybe some other top level window like a dialog or maybe a pop-up menu?
516             return true;
517         wchar_t sPath[MAX_PATH];
518         if (lParam == SHCNE_MEDIAINSERTED || lParam == SHCNE_MEDIAREMOVED) {
519             struct _ITEMIDLIST* pidl = *reinterpret_cast<struct _ITEMIDLIST**>(wParam);
520             if (! SHGetPathFromIDList(pidl, sPath)) {
521                 BOOST_LOG_TRIVIAL(error) << "MediaInserted: SHGetPathFromIDList failed";
522                 return false;
523             }
524         }
525         switch (lParam) {
526         case SHCNE_MEDIAINSERTED:
527         {
528             //printf("SHCNE_MEDIAINSERTED %S\n", sPath);
529             plater->GetEventHandler()->AddPendingEvent(VolumeAttachedEvent(EVT_VOLUME_ATTACHED));
530             break;
531         }
532         case SHCNE_MEDIAREMOVED:
533         {
534             //printf("SHCNE_MEDIAREMOVED %S\n", sPath);
535             plater->GetEventHandler()->AddPendingEvent(VolumeDetachedEvent(EVT_VOLUME_DETACHED));
536             break;
537         }
538 	    default:
539 //          printf("Unknown\n");
540             break;
541 	    }
542         return true;
543     });
544 
545     wxWindow::MSWRegisterMessageHandler(WM_INPUT, [](wxWindow *win, WXUINT /* nMsg */, WXWPARAM wParam, WXLPARAM lParam) {
546         auto main_frame = dynamic_cast<MainFrame*>(Slic3r::GUI::find_toplevel_parent(win));
547         auto plater = (main_frame == nullptr) ? nullptr : main_frame->plater();
548 //        if (wParam == RIM_INPUTSINK && plater != nullptr && main_frame->IsActive()) {
549         if (wParam == RIM_INPUT && plater != nullptr && main_frame->IsActive()) {
550         RAWINPUT raw;
551 			UINT rawSize = sizeof(RAWINPUT);
552 			::GetRawInputData((HRAWINPUT)lParam, RID_INPUT, &raw, &rawSize, sizeof(RAWINPUTHEADER));
553 			if (raw.header.dwType == RIM_TYPEHID && plater->get_mouse3d_controller().handle_raw_input_win32(raw.data.hid.bRawData, raw.data.hid.dwSizeHid))
554 				return true;
555 		}
556         return false;
557     });
558 
559 	wxWindow::MSWRegisterMessageHandler(WM_COPYDATA, [](wxWindow* win, WXUINT /* nMsg */, WXWPARAM wParam, WXLPARAM lParam) {
560 		COPYDATASTRUCT* copy_data_structure = { 0 };
561 		copy_data_structure = (COPYDATASTRUCT*)lParam;
562 		if (copy_data_structure->dwData == 1) {
563 			LPCWSTR arguments = (LPCWSTR)copy_data_structure->lpData;
564 			Slic3r::GUI::wxGetApp().other_instance_message_handler()->handle_message(boost::nowide::narrow(arguments));
565 		}
566 		return true;
567 		});
568 }
569 #endif // WIN32
570 
generic_exception_handle()571 static void generic_exception_handle()
572 {
573     // Note: Some wxWidgets APIs use wxLogError() to report errors, eg. wxImage
574     // - see https://docs.wxwidgets.org/3.1/classwx_image.html#aa249e657259fe6518d68a5208b9043d0
575     //
576     // wxLogError typically goes around exception handling and display an error dialog some time
577     // after an error is logged even if exception handling and OnExceptionInMainLoop() take place.
578     // This is why we use wxLogError() here as well instead of a custom dialog, because it accumulates
579     // errors if multiple have been collected and displays just one error message for all of them.
580     // Otherwise we would get multiple error messages for one missing png, for example.
581     //
582     // If a custom error message window (or some other solution) were to be used, it would be necessary
583     // to turn off wxLogError() usage in wx APIs, most notably in wxImage
584     // - see https://docs.wxwidgets.org/trunk/classwx_image.html#aa32e5d3507cc0f8c3330135bc0befc6a
585 
586     try {
587         throw;
588     } catch (const std::bad_alloc& ex) {
589         // bad_alloc in main thread is most likely fatal. Report immediately to the user (wxLogError would be delayed)
590         // and terminate the app so it is at least certain to happen now.
591         wxString errmsg = wxString::Format(_L("%s has encountered an error. It was likely caused by running out of memory. "
592                               "If you are sure you have enough RAM on your system, this may also be a bug and we would "
593                               "be glad if you reported it.\n\nThe application will now terminate."), SLIC3R_APP_NAME);
594         wxMessageBox(errmsg + "\n\n" + wxString(ex.what()), _L("Fatal error"), wxOK | wxICON_ERROR);
595         BOOST_LOG_TRIVIAL(error) << boost::format("std::bad_alloc exception: %1%") % ex.what();
596         std::terminate();
597     } catch (const boost::io::bad_format_string& ex) {
598         wxString errmsg = _L("PrusaSlicer has encountered a localization error. "
599                              "Please report to PrusaSlicer team, what language was active and in which scenario "
600                              "this issue happened. Thank you.\n\nThe application will now terminate.");
601         wxMessageBox(errmsg + "\n\n" + wxString(ex.what()), _L("Critical error"), wxOK | wxICON_ERROR);
602         BOOST_LOG_TRIVIAL(error) << boost::format("Uncaught exception: %1%") % ex.what();
603         std::terminate();
604         throw;
605     } catch (const std::exception& ex) {
606         wxLogError("Internal error: %s", ex.what());
607         BOOST_LOG_TRIVIAL(error) << boost::format("Uncaught exception: %1%") % ex.what();
608         throw;
609     }
610 }
611 
post_init()612 void GUI_App::post_init()
613 {
614     assert(initialized());
615     if (! this->initialized())
616         throw Slic3r::RuntimeError("Calling post_init() while not yet initialized");
617 
618     if (this->init_params->start_as_gcodeviewer) {
619         if (! this->init_params->input_files.empty())
620             this->plater()->load_gcode(wxString::FromUTF8(this->init_params->input_files[0].c_str()));
621     }
622     else {
623         if (! this->init_params->preset_substitutions.empty())
624             show_substitutions_info(this->init_params->preset_substitutions);
625 
626 #if 0
627         // Load the cummulative config over the currently active profiles.
628         //FIXME if multiple configs are loaded, only the last one will have an effect.
629         // We need to decide what to do about loading of separate presets (just print preset, just filament preset etc).
630         // As of now only the full configs are supported here.
631         if (!m_print_config.empty())
632             this->gui->mainframe->load_config(m_print_config);
633 #endif
634         if (! this->init_params->load_configs.empty())
635             // Load the last config to give it a name at the UI. The name of the preset may be later
636             // changed by loading an AMF or 3MF.
637             //FIXME this is not strictly correct, as one may pass a print/filament/printer profile here instead of a full config.
638             this->mainframe->load_config_file(this->init_params->load_configs.back());
639         // If loading a 3MF file, the config is loaded from the last one.
640         if (! this->init_params->input_files.empty())
641             this->plater()->load_files(this->init_params->input_files, true, true);
642         if (! this->init_params->extra_config.empty())
643             this->mainframe->load_config(this->init_params->extra_config);
644     }
645 
646     // The extra CallAfter() is needed because of Mac, where this is the only way
647     // to popup a modal dialog on start without screwing combo boxes.
648     // This is ugly but I honestly found no better way to do it.
649     // Neither wxShowEvent nor wxWindowCreateEvent work reliably.
650     if (this->preset_updater) {
651         this->check_updates(false);
652         CallAfter([this] {
653             this->config_wizard_startup();
654             this->preset_updater->slic3r_update_notify();
655             this->preset_updater->sync(preset_bundle);
656         });
657     }
658 
659 #ifdef _WIN32
660     // Sets window property to mainframe so other instances can indentify it.
661     OtherInstanceMessageHandler::init_windows_properties(mainframe, m_instance_hash_int);
662 #endif //WIN32
663 }
664 
IMPLEMENT_APP(GUI_App)665 IMPLEMENT_APP(GUI_App)
666 
667 GUI_App::GUI_App(EAppMode mode)
668     : wxApp()
669     , m_app_mode(mode)
670     , m_em_unit(10)
671     , m_imgui(new ImGuiWrapper())
672     , m_wizard(nullptr)
673 	, m_removable_drive_manager(std::make_unique<RemovableDriveManager>())
674 	, m_other_instance_message_handler(std::make_unique<OtherInstanceMessageHandler>())
675 {
676 	//app config initializes early becasuse it is used in instance checking in PrusaSlicer.cpp
677 	this->init_app_config();
678 }
679 
~GUI_App()680 GUI_App::~GUI_App()
681 {
682     if (app_config != nullptr)
683         delete app_config;
684 
685     if (preset_bundle != nullptr)
686         delete preset_bundle;
687 
688     if (preset_updater != nullptr)
689         delete preset_updater;
690 }
691 
get_gl_info(bool format_as_html,bool extensions)692 std::string GUI_App::get_gl_info(bool format_as_html, bool extensions)
693 {
694     return OpenGLManager::get_gl_info().to_string(format_as_html, extensions);
695 }
696 
init_glcontext(wxGLCanvas & canvas)697 wxGLContext* GUI_App::init_glcontext(wxGLCanvas& canvas)
698 {
699     return m_opengl_mgr.init_glcontext(canvas);
700 }
701 
init_opengl()702 bool GUI_App::init_opengl()
703 {
704 #ifdef __linux__
705     bool status = m_opengl_mgr.init_gl();
706     m_opengl_initialized = true;
707     return status;
708 #else
709     return m_opengl_mgr.init_gl();
710 #endif
711 }
712 
init_app_config()713 void GUI_App::init_app_config()
714 {
715 	// Profiles for the alpha are stored into the PrusaSlicer-alpha directory to not mix with the current release.
716     SetAppName(SLIC3R_APP_KEY);
717 //	SetAppName(SLIC3R_APP_KEY "-beta");
718 //	SetAppDisplayName(SLIC3R_APP_NAME);
719 
720 	// Set the Slic3r data directory at the Slic3r XS module.
721 	// Unix: ~/ .Slic3r
722 	// Windows : "C:\Users\username\AppData\Roaming\Slic3r" or "C:\Documents and Settings\username\Application Data\Slic3r"
723 	// Mac : "~/Library/Application Support/Slic3r"
724 
725     if (data_dir().empty()) {
726         #ifndef __linux__
727             set_data_dir(wxStandardPaths::Get().GetUserDataDir().ToUTF8().data());
728         #else
729             // Since version 2.3, config dir on Linux is in ${XDG_CONFIG_HOME}.
730             // https://github.com/prusa3d/PrusaSlicer/issues/2911
731             wxString dir;
732             if (! wxGetEnv(wxS("XDG_CONFIG_HOME"), &dir) || dir.empty() )
733                 dir = wxFileName::GetHomeDir() + wxS("/.config");
734             set_data_dir((dir + "/" + GetAppName()).ToUTF8().data());
735         #endif
736     }
737 
738 	if (!app_config)
739         app_config = new AppConfig(is_editor() ? AppConfig::EAppMode::Editor : AppConfig::EAppMode::GCodeViewer);
740 
741 	// load settings
742 	m_app_conf_exists = app_config->exists();
743 	if (m_app_conf_exists) {
744         std::string error = app_config->load();
745         if (!error.empty()) {
746             // Error while parsing config file. We'll customize the error message and rethrow to be displayed.
747             if (is_editor()) {
748                 throw Slic3r::RuntimeError(
749                     _u8L("Error parsing PrusaSlicer config file, it is probably corrupted. "
750                         "Try to manually delete the file to recover from the error. Your user profiles will not be affected.") +
751                     "\n\n" + app_config->config_path() + "\n\n" + error);
752             }
753             else {
754                 throw Slic3r::RuntimeError(
755                     _u8L("Error parsing PrusaGCodeViewer config file, it is probably corrupted. "
756                         "Try to manually delete the file to recover from the error.") +
757                     "\n\n" + app_config->config_path() + "\n\n" + error);
758             }
759         }
760     }
761 }
762 
init_single_instance_checker(const std::string & name,const std::string & path)763 void GUI_App::init_single_instance_checker(const std::string &name, const std::string &path)
764 {
765     BOOST_LOG_TRIVIAL(debug) << "init wx instance checker " << name << " "<< path;
766     m_single_instance_checker = std::make_unique<wxSingleInstanceChecker>(boost::nowide::widen(name), boost::nowide::widen(path));
767 }
768 
OnInit()769 bool GUI_App::OnInit()
770 {
771     try {
772         return on_init_inner();
773     } catch (const std::exception&) {
774         generic_exception_handle();
775         return false;
776     }
777 }
778 
on_init_inner()779 bool GUI_App::on_init_inner()
780 {
781     // Verify resources path
782     const wxString resources_dir = from_u8(Slic3r::resources_dir());
783     wxCHECK_MSG(wxDirExists(resources_dir), false,
784         wxString::Format("Resources path does not exist or is not a directory: %s", resources_dir));
785 
786 #ifdef __linux__
787     if (! check_old_linux_datadir(GetAppName())) {
788         std::cerr << "Quitting, user chose to move their data to new location." << std::endl;
789         return false;
790     }
791 #endif
792 
793     // Enable this to get the default Win32 COMCTRL32 behavior of static boxes.
794 //    wxSystemOptions::SetOption("msw.staticbox.optimized-paint", 0);
795     // Enable this to disable Windows Vista themes for all wxNotebooks. The themes seem to lead to terrible
796     // performance when working on high resolution multi-display setups.
797 //    wxSystemOptions::SetOption("msw.notebook.themed-background", 0);
798 
799 //     Slic3r::debugf "wxWidgets version %s, Wx version %s\n", wxVERSION_STRING, wxVERSION;
800 
801     if (is_editor()) {
802         std::string msg = Http::tls_global_init();
803         std::string ssl_cert_store = app_config->get("tls_accepted_cert_store_location");
804         bool ssl_accept = app_config->get("tls_cert_store_accepted") == "yes" && ssl_cert_store == Http::tls_system_cert_store();
805 
806         if (!msg.empty() && !ssl_accept) {
807             wxRichMessageDialog
808                 dlg(nullptr,
809                     wxString::Format(_L("%s\nDo you want to continue?"), msg),
810                     "PrusaSlicer", wxICON_QUESTION | wxYES_NO);
811             dlg.ShowCheckBox(_L("Remember my choice"));
812             if (dlg.ShowModal() != wxID_YES) return false;
813 
814             app_config->set("tls_cert_store_accepted",
815                 dlg.IsCheckBoxChecked() ? "yes" : "no");
816             app_config->set("tls_accepted_cert_store_location",
817                 dlg.IsCheckBoxChecked() ? Http::tls_system_cert_store() : "");
818         }
819     }
820 
821     app_config->set("version", SLIC3R_VERSION);
822     app_config->save();
823 
824     wxInitAllImageHandlers();
825 
826     SplashScreen* scrn = nullptr;
827     if (app_config->get("show_splash_screen") == "1") {
828         // make a bitmap with dark grey banner on the left side
829         wxBitmap bmp = SplashScreen::MakeBitmap(wxBitmap(from_u8(var(is_editor() ? "splashscreen.jpg" : "splashscreen-gcodepreview.jpg")), wxBITMAP_TYPE_JPEG));
830 
831         // Detect position (display) to show the splash screen
832         // Now this position is equal to the mainframe position
833         wxPoint splashscreen_pos = wxDefaultPosition;
834         if (app_config->has("window_mainframe")) {
835             auto metrics = WindowMetrics::deserialize(app_config->get("window_mainframe"));
836             if (metrics)
837                 splashscreen_pos = metrics->get_rect().GetPosition();
838         }
839 
840         // create splash screen with updated bmp
841         scrn = new SplashScreen(bmp.IsOk() ? bmp : create_scaled_bitmap("prusa_slicer_logo", nullptr, 400),
842                                 wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_TIMEOUT, 4000, splashscreen_pos);
843 #ifndef __linux__
844         wxYield();
845 #endif
846         scrn->SetText(_L("Loading configuration")+ dots);
847     }
848 
849     preset_bundle = new PresetBundle();
850 
851     // just checking for existence of Slic3r::data_dir is not enough : it may be an empty directory
852     // supplied as argument to --datadir; in that case we should still run the wizard
853     preset_bundle->setup_directories();
854 
855     if (is_editor()) {
856 #ifdef __WXMSW__
857 #if ENABLE_CUSTOMIZABLE_FILES_ASSOCIATION_ON_WIN
858         if (app_config->get("associate_3mf") == "1")
859 #endif // ENABLE_CUSTOMIZABLE_FILES_ASSOCIATION_ON_WIN
860             associate_3mf_files();
861 #if ENABLE_CUSTOMIZABLE_FILES_ASSOCIATION_ON_WIN
862         if (app_config->get("associate_stl") == "1")
863             associate_stl_files();
864 #endif // ENABLE_CUSTOMIZABLE_FILES_ASSOCIATION_ON_WIN
865 #endif // __WXMSW__
866 
867         preset_updater = new PresetUpdater();
868         Bind(EVT_SLIC3R_VERSION_ONLINE, [this](const wxCommandEvent& evt) {
869             app_config->set("version_online", into_u8(evt.GetString()));
870             app_config->save();
871             if (this->plater_ != nullptr) {
872                 if (*Semver::parse(SLIC3R_VERSION) < *Semver::parse(into_u8(evt.GetString()))) {
873                     this->plater_->get_notification_manager()->push_notification(NotificationType::NewAppAvailable);
874                 }
875             }
876             });
877     }
878     else {
879 #ifdef __WXMSW__
880 #if ENABLE_CUSTOMIZABLE_FILES_ASSOCIATION_ON_WIN
881         if (app_config->get("associate_gcode") == "1")
882 #endif // ENABLE_CUSTOMIZABLE_FILES_ASSOCIATION_ON_WIN
883             associate_gcode_files();
884 #endif // __WXMSW__
885     }
886 
887     // initialize label colors and fonts
888     init_label_colours();
889     init_fonts();
890 
891     // If load_language() fails, the application closes.
892     load_language(wxString(), true);
893 
894     // Suppress the '- default -' presets.
895     preset_bundle->set_default_suppressed(app_config->get("no_defaults") == "1");
896     try {
897         // Enable all substitutions (in both user and system profiles), but log the substitutions in user profiles only.
898         // If there are substitutions in system profiles, then a "reconfigure" event shall be triggered, which will force
899         // installation of a compatible system preset, thus nullifying the system preset substitutions.
900         init_params->preset_substitutions = preset_bundle->load_presets(*app_config, ForwardCompatibilitySubstitutionRule::EnableSystemSilent);
901     } catch (const std::exception &ex) {
902         show_error(nullptr, ex.what());
903     }
904 
905 #ifdef WIN32
906 #if !wxVERSION_EQUAL_OR_GREATER_THAN(3,1,3)
907     register_win32_dpi_event();
908 #endif // !wxVERSION_EQUAL_OR_GREATER_THAN
909     register_win32_device_notification_event();
910 #endif // WIN32
911 
912     // Let the libslic3r know the callback, which will translate messages on demand.
913     Slic3r::I18N::set_translate_callback(libslic3r_translate_callback);
914 
915     // application frame
916     if (scrn && is_editor())
917         scrn->SetText(_L("Preparing settings tabs") + dots);
918 
919     mainframe = new MainFrame();
920     // hide settings tabs after first Layout
921     if (is_editor())
922         mainframe->select_tab(size_t(0));
923 
924     sidebar().obj_list()->init_objects(); // propagate model objects to object list
925 //     update_mode(); // !!! do that later
926     SetTopWindow(mainframe);
927 
928     m_printhost_job_queue.reset(new PrintHostJobQueue(mainframe->printhost_queue_dlg()));
929 
930     if (is_gcode_viewer()) {
931         mainframe->update_layout();
932         if (plater_ != nullptr)
933             // ensure the selected technology is ptFFF
934             plater_->set_printer_technology(ptFFF);
935     }
936     else
937         load_current_presets();
938     mainframe->Show(true);
939 
940     obj_list()->set_min_height();
941 
942     update_mode(); // update view mode after fix of the object_list size
943 
944 #ifdef __APPLE__
945     other_instance_message_handler()->bring_instance_forward();
946 #endif //__APPLE__
947 
948     Bind(wxEVT_IDLE, [this](wxIdleEvent& event)
949     {
950         if (! plater_)
951             return;
952 
953         if (app_config->dirty() && app_config->get("autosave") == "1")
954             app_config->save();
955 
956         this->obj_manipul()->update_if_dirty();
957 
958         static bool update_gui_after_init = true;
959 
960         // An ugly solution to GH #5537 in which GUI_App::init_opengl (normally called from events wxEVT_PAINT
961         // and wxEVT_SET_FOCUS before GUI_App::post_init is called) wasn't called before GUI_App::post_init and OpenGL wasn't initialized.
962 #ifdef __linux__
963         if (update_gui_after_init && m_opengl_initialized) {
964 #else
965         if (update_gui_after_init) {
966 #endif
967             update_gui_after_init = false;
968 #ifdef WIN32
969             this->mainframe->register_win32_callbacks();
970 #endif
971             this->post_init();
972         }
973     });
974 
975     m_initialized = true;
976     return true;
977 }
978 
979 unsigned GUI_App::get_colour_approx_luma(const wxColour &colour)
980 {
981     double r = colour.Red();
982     double g = colour.Green();
983     double b = colour.Blue();
984 
985     return std::round(std::sqrt(
986         r * r * .241 +
987         g * g * .691 +
988         b * b * .068
989         ));
990 }
991 
992 bool GUI_App::dark_mode()
993 {
994 #if __APPLE__
995     // The check for dark mode returns false positive on 10.12 and 10.13,
996     // which allowed setting dark menu bar and dock area, which is
997     // is detected as dark mode. We must run on at least 10.14 where the
998     // proper dark mode was first introduced.
999     return wxPlatformInfo::Get().CheckOSVersion(10, 14) && mac_dark_mode();
1000 #else
1001     const unsigned luma = get_colour_approx_luma(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
1002     return luma < 128;
1003 #endif
1004 }
1005 
1006 void GUI_App::init_label_colours()
1007 {
1008     if (dark_mode()) {
1009         m_color_label_modified = wxColour(253, 111, 40);
1010         m_color_label_sys = wxColour(115, 220, 103);
1011     }
1012     else {
1013         m_color_label_modified = wxColour(252, 77, 1);
1014         m_color_label_sys = wxColour(26, 132, 57);
1015     }
1016     m_color_label_default = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
1017 }
1018 
1019 void GUI_App::update_label_colours_from_appconfig()
1020 {
1021     if (app_config->has("label_clr_sys")) {
1022         auto str = app_config->get("label_clr_sys");
1023         if (str != "")
1024             m_color_label_sys = wxColour(str);
1025     }
1026 
1027     if (app_config->has("label_clr_modified")) {
1028         auto str = app_config->get("label_clr_modified");
1029         if (str != "")
1030             m_color_label_modified = wxColour(str);
1031     }
1032 }
1033 
1034 void GUI_App::init_fonts()
1035 {
1036     m_small_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
1037     m_bold_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT).Bold();
1038     m_normal_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
1039 
1040 #ifdef __WXMAC__
1041     m_small_font.SetPointSize(11);
1042     m_bold_font.SetPointSize(13);
1043 #endif /*__WXMAC__*/
1044 
1045     // wxSYS_OEM_FIXED_FONT and wxSYS_ANSI_FIXED_FONT use the same as
1046     // DEFAULT in wxGtk. Use the TELETYPE family as a work-around
1047     m_code_font = wxFont(wxFontInfo().Family(wxFONTFAMILY_TELETYPE));
1048     m_code_font.SetPointSize(m_normal_font.GetPointSize());
1049 }
1050 
1051 void GUI_App::update_fonts(const MainFrame *main_frame)
1052 {
1053     /* Only normal and bold fonts are used for an application rescale,
1054      * because of under MSW small and normal fonts are the same.
1055      * To avoid same rescaling twice, just fill this values
1056      * from rescaled MainFrame
1057      */
1058 	if (main_frame == nullptr)
1059 		main_frame = this->mainframe;
1060     m_normal_font   = main_frame->normal_font();
1061     m_small_font    = m_normal_font;
1062     m_bold_font     = main_frame->normal_font().Bold();
1063     m_em_unit       = main_frame->em_unit();
1064     m_code_font.SetPointSize(m_normal_font.GetPointSize());
1065 }
1066 
1067 void GUI_App::set_label_clr_modified(const wxColour& clr) {
1068     m_color_label_modified = clr;
1069     auto clr_str = wxString::Format(wxT("#%02X%02X%02X"), clr.Red(), clr.Green(), clr.Blue());
1070     std::string str = clr_str.ToStdString();
1071     app_config->set("label_clr_modified", str);
1072     app_config->save();
1073 }
1074 
1075 void GUI_App::set_label_clr_sys(const wxColour& clr) {
1076     m_color_label_sys = clr;
1077     auto clr_str = wxString::Format(wxT("#%02X%02X%02X"), clr.Red(), clr.Green(), clr.Blue());
1078     std::string str = clr_str.ToStdString();
1079     app_config->set("label_clr_sys", str);
1080     app_config->save();
1081 }
1082 
1083 wxSize GUI_App::get_min_size() const
1084 {
1085     return wxSize(76*m_em_unit, 49 * m_em_unit);
1086 }
1087 
1088 float GUI_App::toolbar_icon_scale(const bool is_limited/* = false*/) const
1089 {
1090 #ifdef __APPLE__
1091     const float icon_sc = 1.0f; // for Retina display will be used its own scale
1092 #else
1093     const float icon_sc = m_em_unit*0.1f;
1094 #endif // __APPLE__
1095 
1096     const std::string& use_val  = app_config->get("use_custom_toolbar_size");
1097     const std::string& val      = app_config->get("custom_toolbar_size");
1098     const std::string& auto_val = app_config->get("auto_toolbar_size");
1099 
1100     if (val.empty() || auto_val.empty() || use_val.empty())
1101         return icon_sc;
1102 
1103     int int_val = use_val == "0" ? 100 : atoi(val.c_str());
1104     // correct value in respect to auto_toolbar_size
1105     int_val = std::min(atoi(auto_val.c_str()), int_val);
1106 
1107     if (is_limited && int_val < 50)
1108         int_val = 50;
1109 
1110     return 0.01f * int_val * icon_sc;
1111 }
1112 
1113 void GUI_App::set_auto_toolbar_icon_scale(float scale) const
1114 {
1115 #ifdef __APPLE__
1116     const float icon_sc = 1.0f; // for Retina display will be used its own scale
1117 #else
1118     const float icon_sc = m_em_unit * 0.1f;
1119 #endif // __APPLE__
1120 
1121     long int_val = std::min(int(std::lround(scale / icon_sc * 100)), 100);
1122     std::string val = std::to_string(int_val);
1123 
1124     app_config->set("auto_toolbar_size", val);
1125 }
1126 
1127 // check user printer_presets for the containing information about "Print Host upload"
1128 void GUI_App::check_printer_presets()
1129 {
1130     std::vector<std::string> preset_names = PhysicalPrinter::presets_with_print_host_information(preset_bundle->printers);
1131     if (preset_names.empty())
1132         return;
1133 
1134     wxString msg_text =  _L("You have the following presets with saved options for \"Print Host upload\"") + ":";
1135     for (const std::string& preset_name : preset_names)
1136         msg_text += "\n    \"" + from_u8(preset_name) + "\",";
1137     msg_text.RemoveLast();
1138     msg_text += "\n\n" + _L("But since this version of PrusaSlicer we don't show this information in Printer Settings anymore.\n"
1139                             "Settings will be available in physical printers settings.") + "\n\n" +
1140                          _L("By default new Printer devices will be named as \"Printer N\" during its creation.\n"
1141                             "Note: This name can be changed later from the physical printers settings");
1142 
1143     wxMessageDialog(nullptr, msg_text, _L("Information"), wxOK | wxICON_INFORMATION).ShowModal();
1144 
1145     preset_bundle->physical_printers.load_printers_from_presets(preset_bundle->printers);
1146 }
1147 
1148 void GUI_App::recreate_GUI(const wxString& msg_name)
1149 {
1150     m_is_recreating_gui = true;
1151 
1152     mainframe->shutdown();
1153 
1154     wxProgressDialog dlg(msg_name, msg_name, 100, nullptr, wxPD_AUTO_HIDE);
1155     dlg.Pulse();
1156     dlg.Update(10, _L("Recreating") + dots);
1157 
1158     MainFrame *old_main_frame = mainframe;
1159     mainframe = new MainFrame();
1160     if (is_editor())
1161         // hide settings tabs after first Layout
1162         mainframe->select_tab(size_t(0));
1163     // Propagate model objects to object list.
1164     sidebar().obj_list()->init_objects();
1165     SetTopWindow(mainframe);
1166 
1167     dlg.Update(30, _L("Recreating") + dots);
1168     old_main_frame->Destroy();
1169     // For this moment ConfigWizard is deleted, invalidate it.
1170     m_wizard = nullptr;
1171 
1172     dlg.Update(80, _L("Loading of current presets") + dots);
1173     m_printhost_job_queue.reset(new PrintHostJobQueue(mainframe->printhost_queue_dlg()));
1174     load_current_presets();
1175     mainframe->Show(true);
1176 
1177     dlg.Update(90, _L("Loading of a mode view") + dots);
1178 
1179     obj_list()->set_min_height();
1180     update_mode();
1181 
1182     // #ys_FIXME_delete_after_testing  Do we still need this  ?
1183 //     CallAfter([]() {
1184 //         // Run the config wizard, don't offer the "reset user profile" checkbox.
1185 //         config_wizard_startup(true);
1186 //     });
1187 
1188     m_is_recreating_gui = false;
1189 }
1190 
1191 void GUI_App::system_info()
1192 {
1193     SysInfoDialog dlg;
1194     dlg.ShowModal();
1195 }
1196 
1197 void GUI_App::keyboard_shortcuts()
1198 {
1199     KBShortcutsDialog dlg;
1200     dlg.ShowModal();
1201 }
1202 
1203 // static method accepting a wxWindow object as first parameter
1204 bool GUI_App::catch_error(std::function<void()> cb,
1205     //                       wxMessageDialog* message_dialog,
1206     const std::string& err /*= ""*/)
1207 {
1208     if (!err.empty()) {
1209         if (cb)
1210             cb();
1211         //         if (message_dialog)
1212         //             message_dialog->(err, "Error", wxOK | wxICON_ERROR);
1213         show_error(/*this*/nullptr, err);
1214         return true;
1215     }
1216     return false;
1217 }
1218 
1219 // static method accepting a wxWindow object as first parameter
1220 void fatal_error(wxWindow* parent)
1221 {
1222     show_error(parent, "");
1223     //     exit 1; // #ys_FIXME
1224 }
1225 
1226 // Called after the Preferences dialog is closed and the program settings are saved.
1227 // Update the UI based on the current preferences.
1228 void GUI_App::update_ui_from_settings(bool apply_free_camera_correction)
1229 {
1230     mainframe->update_ui_from_settings(apply_free_camera_correction);
1231 }
1232 
1233 void GUI_App::persist_window_geometry(wxTopLevelWindow *window, bool default_maximized)
1234 {
1235     const std::string name = into_u8(window->GetName());
1236 
1237     window->Bind(wxEVT_CLOSE_WINDOW, [=](wxCloseEvent &event) {
1238         window_pos_save(window, name);
1239         event.Skip();
1240     });
1241 
1242     window_pos_restore(window, name, default_maximized);
1243 
1244     on_window_geometry(window, [=]() {
1245         window_pos_sanitize(window);
1246     });
1247 }
1248 
1249 void GUI_App::load_project(wxWindow *parent, wxString& input_file) const
1250 {
1251     input_file.Clear();
1252     wxFileDialog dialog(parent ? parent : GetTopWindow(),
1253         _L("Choose one file (3MF/AMF):"),
1254         app_config->get_last_dir(), "",
1255         file_wildcards(FT_PROJECT), wxFD_OPEN | wxFD_FILE_MUST_EXIST);
1256 
1257     if (dialog.ShowModal() == wxID_OK)
1258         input_file = dialog.GetPath();
1259 }
1260 
1261 void GUI_App::import_model(wxWindow *parent, wxArrayString& input_files) const
1262 {
1263     input_files.Clear();
1264     wxFileDialog dialog(parent ? parent : GetTopWindow(),
1265         _L("Choose one or more files (STL/OBJ/AMF/3MF/PRUSA):"),
1266         from_u8(app_config->get_last_dir()), "",
1267         file_wildcards(FT_MODEL), wxFD_OPEN | wxFD_MULTIPLE | wxFD_FILE_MUST_EXIST);
1268 
1269     if (dialog.ShowModal() == wxID_OK)
1270         dialog.GetPaths(input_files);
1271 }
1272 
1273 void GUI_App::load_gcode(wxWindow* parent, wxString& input_file) const
1274 {
1275     input_file.Clear();
1276     wxFileDialog dialog(parent ? parent : GetTopWindow(),
1277         _L("Choose one file (GCODE/.GCO/.G/.ngc/NGC):"),
1278         app_config->get_last_dir(), "",
1279         file_wildcards(FT_GCODE), wxFD_OPEN | wxFD_FILE_MUST_EXIST);
1280 
1281     if (dialog.ShowModal() == wxID_OK)
1282         input_file = dialog.GetPath();
1283 }
1284 
1285 bool GUI_App::switch_language()
1286 {
1287     if (select_language()) {
1288         recreate_GUI(_L("Changing of an application language") + dots);
1289         return true;
1290     } else {
1291         return false;
1292     }
1293 }
1294 
1295 #ifdef __linux__
1296 static const wxLanguageInfo* linux_get_existing_locale_language(const wxLanguageInfo* language,
1297                                                                 const wxLanguageInfo* system_language)
1298 {
1299     constexpr size_t max_len = 50;
1300     char path[max_len] = "";
1301     std::vector<std::string> locales;
1302     const std::string lang_prefix = into_u8(language->CanonicalName.BeforeFirst('_'));
1303 
1304     // Call locale -a so we can parse the output to get the list of available locales
1305     // We expect lines such as "en_US.utf8". Pick ones starting with the language code
1306     // we are switching to. Lines with different formatting will be removed later.
1307     FILE* fp = popen("locale -a", "r");
1308     if (fp != NULL) {
1309         while (fgets(path, max_len, fp) != NULL) {
1310             std::string line(path);
1311             line = line.substr(0, line.find('\n'));
1312             if (boost::starts_with(line, lang_prefix))
1313                 locales.push_back(line);
1314         }
1315         pclose(fp);
1316     }
1317 
1318     // locales now contain all candidates for this language.
1319     // Sort them so ones containing anything about UTF-8 are at the end.
1320     std::sort(locales.begin(), locales.end(), [](const std::string& a, const std::string& b)
1321     {
1322         auto has_utf8 = [](const std::string & s) {
1323             auto S = boost::to_upper_copy(s);
1324             return S.find("UTF8") != std::string::npos || S.find("UTF-8") != std::string::npos;
1325         };
1326         return ! has_utf8(a) && has_utf8(b);
1327     });
1328 
1329     // Remove the suffix behind a dot, if there is one.
1330     for (std::string& s : locales)
1331         s = s.substr(0, s.find("."));
1332 
1333     // We just hope that dear Linux "locale -a" returns country codes
1334     // in ISO 3166-1 alpha-2 code (two letter) format.
1335     // https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
1336     // To be sure, remove anything not looking as expected
1337     // (any number of lowercase letters, underscore, two uppercase letters).
1338     locales.erase(std::remove_if(locales.begin(),
1339                                  locales.end(),
1340                                  [](const std::string& s) {
1341                                      return ! std::regex_match(s,
1342                                          std::regex("^[a-z]+_[A-Z]{2}$"));
1343                                  }),
1344                    locales.end());
1345 
1346     // Is there a candidate matching a country code of a system language? Move it to the end,
1347     // while maintaining the order of matches, so that the best match ends up at the very end.
1348     std::string system_country = "_" + into_u8(system_language->CanonicalName.AfterFirst('_')).substr(0, 2);
1349     int cnt = locales.size();
1350     for (int i=0; i<cnt; ++i)
1351         if (locales[i].find(system_country) != std::string::npos) {
1352             locales.emplace_back(std::move(locales[i]));
1353             locales[i].clear();
1354         }
1355 
1356     // Now try them one by one.
1357     for (auto it = locales.rbegin(); it != locales.rend(); ++ it)
1358         if (! it->empty()) {
1359             const std::string &locale = *it;
1360             const wxLanguageInfo* lang = wxLocale::FindLanguageInfo(from_u8(locale));
1361             if (wxLocale::IsAvailable(lang->Language))
1362                 return lang;
1363         }
1364     return language;
1365 }
1366 #endif
1367 
1368 // select language from the list of installed languages
1369 bool GUI_App::select_language()
1370 {
1371 	wxArrayString translations = wxTranslations::Get()->GetAvailableTranslations(SLIC3R_APP_KEY);
1372     std::vector<const wxLanguageInfo*> language_infos;
1373     language_infos.emplace_back(wxLocale::GetLanguageInfo(wxLANGUAGE_ENGLISH));
1374     for (size_t i = 0; i < translations.GetCount(); ++ i) {
1375 	    const wxLanguageInfo *langinfo = wxLocale::FindLanguageInfo(translations[i]);
1376         if (langinfo != nullptr)
1377             language_infos.emplace_back(langinfo);
1378     }
1379     sort_remove_duplicates(language_infos);
1380 	std::sort(language_infos.begin(), language_infos.end(), [](const wxLanguageInfo* l, const wxLanguageInfo* r) { return l->Description < r->Description; });
1381 
1382     wxArrayString names;
1383     names.Alloc(language_infos.size());
1384 
1385     // Some valid language should be selected since the application start up.
1386     const wxLanguage current_language = wxLanguage(m_wxLocale->GetLanguage());
1387     int 		     init_selection   		= -1;
1388     int 			 init_selection_alt     = -1;
1389     int 			 init_selection_default = -1;
1390     for (size_t i = 0; i < language_infos.size(); ++ i) {
1391         if (wxLanguage(language_infos[i]->Language) == current_language)
1392         	// The dictionary matches the active language and country.
1393             init_selection = i;
1394         else if ((language_infos[i]->CanonicalName.BeforeFirst('_') == m_wxLocale->GetCanonicalName().BeforeFirst('_')) ||
1395         		 // if the active language is Slovak, mark the Czech language as active.
1396         	     (language_infos[i]->CanonicalName.BeforeFirst('_') == "cs" && m_wxLocale->GetCanonicalName().BeforeFirst('_') == "sk"))
1397         	// The dictionary matches the active language, it does not necessarily match the country.
1398         	init_selection_alt = i;
1399         if (language_infos[i]->CanonicalName.BeforeFirst('_') == "en")
1400         	// This will be the default selection if the active language does not match any dictionary.
1401         	init_selection_default = i;
1402         names.Add(language_infos[i]->Description);
1403     }
1404     if (init_selection == -1)
1405     	// This is the dictionary matching the active language.
1406     	init_selection = init_selection_alt;
1407     if (init_selection != -1)
1408     	// This is the language to highlight in the choice dialog initially.
1409     	init_selection_default = init_selection;
1410 
1411     const long index = wxGetSingleChoiceIndex(_L("Select the language"), _L("Language"), names, init_selection_default);
1412 	// Try to load a new language.
1413     if (index != -1 && (init_selection == -1 || init_selection != index)) {
1414     	const wxLanguageInfo *new_language_info = language_infos[index];
1415     	if (this->load_language(new_language_info->CanonicalName, false)) {
1416 			// Save language at application config.
1417             // Which language to save as the selected dictionary language?
1418             // 1) Hopefully the language set to wxTranslations by this->load_language(), but that API is weird and we don't want to rely on its
1419             //    stability in the future:
1420             //    wxTranslations::Get()->GetBestTranslation(SLIC3R_APP_KEY, wxLANGUAGE_ENGLISH);
1421             // 2) Current locale language may not match the dictionary name, see GH issue #3901
1422             //    m_wxLocale->GetCanonicalName()
1423             // 3) new_language_info->CanonicalName is a safe bet. It points to a valid dictionary name.
1424 			app_config->set("translation_language", new_language_info->CanonicalName.ToUTF8().data());
1425 			app_config->save();
1426     		return true;
1427     	}
1428     }
1429 
1430     return false;
1431 }
1432 
1433 // Load gettext translation files and activate them at the start of the application,
1434 // based on the "translation_language" key stored in the application config.
1435 bool GUI_App::load_language(wxString language, bool initial)
1436 {
1437     if (initial) {
1438     	// There is a static list of lookup path prefixes in wxWidgets. Add ours.
1439 	    wxFileTranslationsLoader::AddCatalogLookupPathPrefix(from_u8(localization_dir()));
1440     	// Get the active language from PrusaSlicer.ini, or empty string if the key does not exist.
1441         language = app_config->get("translation_language");
1442         if (! language.empty())
1443         	BOOST_LOG_TRIVIAL(trace) << boost::format("translation_language provided by PrusaSlicer.ini: %1%") % language;
1444 
1445         // Get the system language.
1446         {
1447 	        const wxLanguage lang_system = wxLanguage(wxLocale::GetSystemLanguage());
1448 	        if (lang_system != wxLANGUAGE_UNKNOWN) {
1449 				m_language_info_system = wxLocale::GetLanguageInfo(lang_system);
1450 	        	BOOST_LOG_TRIVIAL(trace) << boost::format("System language detected (user locales and such): %1%") % m_language_info_system->CanonicalName.ToUTF8().data();
1451 	        }
1452 		}
1453         {
1454 	    	// Allocating a temporary locale will switch the default wxTranslations to its internal wxTranslations instance.
1455 	    	wxLocale temp_locale;
1456 	    	// Set the current translation's language to default, otherwise GetBestTranslation() may not work (see the wxWidgets source code).
1457 	    	wxTranslations::Get()->SetLanguage(wxLANGUAGE_DEFAULT);
1458 	    	// Let the wxFileTranslationsLoader enumerate all translation dictionaries for PrusaSlicer
1459 	    	// and try to match them with the system specific "preferred languages".
1460 	    	// There seems to be a support for that on Windows and OSX, while on Linuxes the code just returns wxLocale::GetSystemLanguage().
1461 	    	// The last parameter gets added to the list of detected dictionaries. This is a workaround
1462 	    	// for not having the English dictionary. Let's hope wxWidgets of various versions process this call the same way.
1463 			wxString best_language = wxTranslations::Get()->GetBestTranslation(SLIC3R_APP_KEY, wxLANGUAGE_ENGLISH);
1464 			if (! best_language.IsEmpty()) {
1465 				m_language_info_best = wxLocale::FindLanguageInfo(best_language);
1466 	        	BOOST_LOG_TRIVIAL(trace) << boost::format("Best translation language detected (may be different from user locales): %1%") % m_language_info_best->CanonicalName.ToUTF8().data();
1467 			}
1468             #ifdef __linux__
1469             wxString lc_all;
1470             if (wxGetEnv("LC_ALL", &lc_all) && ! lc_all.IsEmpty()) {
1471                 // Best language returned by wxWidgets on Linux apparently does not respect LC_ALL.
1472                 // Disregard the "best" suggestion in case LC_ALL is provided.
1473                 m_language_info_best = nullptr;
1474             }
1475             #endif
1476 		}
1477     }
1478 
1479 	const wxLanguageInfo *language_info = language.empty() ? nullptr : wxLocale::FindLanguageInfo(language);
1480 	if (! language.empty() && (language_info == nullptr || language_info->CanonicalName.empty())) {
1481 		// Fix for wxWidgets issue, where the FindLanguageInfo() returns locales with undefined ANSII code (wxLANGUAGE_KONKANI or wxLANGUAGE_MANIPURI).
1482 		language_info = nullptr;
1483     	BOOST_LOG_TRIVIAL(error) << boost::format("Language code \"%1%\" is not supported") % language.ToUTF8().data();
1484 	}
1485 
1486 	if (language_info != nullptr && language_info->LayoutDirection == wxLayout_RightToLeft) {
1487     	BOOST_LOG_TRIVIAL(trace) << boost::format("The following language code requires right to left layout, which is not supported by PrusaSlicer: %1%") % language_info->CanonicalName.ToUTF8().data();
1488 		language_info = nullptr;
1489 	}
1490 
1491     if (language_info == nullptr) {
1492         // PrusaSlicer does not support the Right to Left languages yet.
1493         if (m_language_info_system != nullptr && m_language_info_system->LayoutDirection != wxLayout_RightToLeft)
1494             language_info = m_language_info_system;
1495         if (m_language_info_best != nullptr && m_language_info_best->LayoutDirection != wxLayout_RightToLeft)
1496         	language_info = m_language_info_best;
1497 	    if (language_info == nullptr)
1498 			language_info = wxLocale::GetLanguageInfo(wxLANGUAGE_ENGLISH_US);
1499     }
1500 
1501 	BOOST_LOG_TRIVIAL(trace) << boost::format("Switching wxLocales to %1%") % language_info->CanonicalName.ToUTF8().data();
1502 
1503     // Alternate language code.
1504     wxLanguage language_dict = wxLanguage(language_info->Language);
1505     if (language_info->CanonicalName.BeforeFirst('_') == "sk") {
1506     	// Slovaks understand Czech well. Give them the Czech translation.
1507     	language_dict = wxLANGUAGE_CZECH;
1508 		BOOST_LOG_TRIVIAL(trace) << "Using Czech dictionaries for Slovak language";
1509     }
1510 
1511     // Select language for locales. This language may be different from the language of the dictionary.
1512     if (language_info == m_language_info_best || language_info == m_language_info_system) {
1513         // The current language matches user's default profile exactly. That's great.
1514     } else if (m_language_info_best != nullptr && language_info->CanonicalName.BeforeFirst('_') == m_language_info_best->CanonicalName.BeforeFirst('_')) {
1515         // Use whatever the operating system recommends, if it the language code of the dictionary matches the recommended language.
1516         // This allows a Swiss guy to use a German dictionary without forcing him to German locales.
1517         language_info = m_language_info_best;
1518     } else if (m_language_info_system != nullptr && language_info->CanonicalName.BeforeFirst('_') == m_language_info_system->CanonicalName.BeforeFirst('_'))
1519         language_info = m_language_info_system;
1520 
1521 #ifdef __linux__
1522     // If we can't find this locale , try to use different one for the language
1523     // instead of just reporting that it is impossible to switch.
1524     if (! wxLocale::IsAvailable(language_info->Language)) {
1525         std::string original_lang = into_u8(language_info->CanonicalName);
1526         language_info = linux_get_existing_locale_language(language_info, m_language_info_system);
1527         BOOST_LOG_TRIVIAL(trace) << boost::format("Can't switch language to %1% (missing locales). Using %2% instead.")
1528                                     % original_lang % language_info->CanonicalName.ToUTF8().data();
1529     }
1530 #endif
1531 
1532     if (! wxLocale::IsAvailable(language_info->Language)) {
1533     	// Loading the language dictionary failed.
1534     	wxString message = "Switching PrusaSlicer to language " + language_info->CanonicalName + " failed.";
1535 #if !defined(_WIN32) && !defined(__APPLE__)
1536         // likely some linux system
1537         message += "\nYou may need to reconfigure the missing locales, likely by running the \"locale-gen\" and \"dpkg-reconfigure locales\" commands.\n";
1538 #endif
1539         if (initial)
1540         	message + "\n\nApplication will close.";
1541         wxMessageBox(message, "PrusaSlicer - Switching language failed", wxOK | wxICON_ERROR);
1542         if (initial)
1543 			std::exit(EXIT_FAILURE);
1544 		else
1545 			return false;
1546     }
1547 
1548     // Release the old locales, create new locales.
1549     //FIXME wxWidgets cause havoc if the current locale is deleted. We just forget it causing memory leaks for now.
1550     m_wxLocale.release();
1551     m_wxLocale = Slic3r::make_unique<wxLocale>();
1552     m_wxLocale->Init(language_info->Language);
1553     // Override language at the active wxTranslations class (which is stored in the active m_wxLocale)
1554     // to load possibly different dictionary, for example, load Czech dictionary for Slovak language.
1555     wxTranslations::Get()->SetLanguage(language_dict);
1556     m_wxLocale->AddCatalog(SLIC3R_APP_KEY);
1557     m_imgui->set_language(into_u8(language_info->CanonicalName));
1558     //FIXME This is a temporary workaround, the correct solution is to switch to "C" locale during file import / export only.
1559     wxSetlocale(LC_NUMERIC, "C");
1560     Preset::update_suffix_modified((" (" + _L("modified") + ")").ToUTF8().data());
1561 	return true;
1562 }
1563 
1564 Tab* GUI_App::get_tab(Preset::Type type)
1565 {
1566     for (Tab* tab: tabs_list)
1567         if (tab->type() == type)
1568             return tab->completed() ? tab : nullptr; // To avoid actions with no-completed Tab
1569     return nullptr;
1570 }
1571 
1572 ConfigOptionMode GUI_App::get_mode()
1573 {
1574     if (!app_config->has("view_mode"))
1575         return comSimple;
1576 
1577     const auto mode = app_config->get("view_mode");
1578     return mode == "expert" ? comExpert :
1579            mode == "simple" ? comSimple : comAdvanced;
1580 }
1581 
1582 void GUI_App::save_mode(const /*ConfigOptionMode*/int mode)
1583 {
1584     const std::string mode_str = mode == comExpert ? "expert" :
1585                                  mode == comSimple ? "simple" : "advanced";
1586     app_config->set("view_mode", mode_str);
1587     app_config->save();
1588     update_mode();
1589 }
1590 
1591 // Update view mode according to selected menu
1592 void GUI_App::update_mode()
1593 {
1594     sidebar().update_mode();
1595 
1596     for (auto tab : tabs_list)
1597         tab->update_mode();
1598 
1599     plater()->update_object_menu();
1600     plater()->canvas3D()->update_gizmos_on_off_state();
1601 }
1602 
1603 void GUI_App::add_config_menu(wxMenuBar *menu)
1604 {
1605     auto local_menu = new wxMenu();
1606     wxWindowID config_id_base = wxWindow::NewControlId(int(ConfigMenuCnt));
1607 
1608     const auto config_wizard_name = _(ConfigWizard::name(true));
1609     const auto config_wizard_tooltip = from_u8((boost::format(_utf8(L("Run %s"))) % config_wizard_name).str());
1610     // Cmd+, is standard on OS X - what about other operating systems?
1611     if (is_editor()) {
1612         local_menu->Append(config_id_base + ConfigMenuWizard, config_wizard_name + dots, config_wizard_tooltip);
1613         local_menu->Append(config_id_base + ConfigMenuSnapshots, _L("&Configuration Snapshots") + dots, _L("Inspect / activate configuration snapshots"));
1614         local_menu->Append(config_id_base + ConfigMenuTakeSnapshot, _L("Take Configuration &Snapshot"), _L("Capture a configuration snapshot"));
1615         local_menu->Append(config_id_base + ConfigMenuUpdate, _L("Check for updates"), _L("Check for configuration updates"));
1616         local_menu->AppendSeparator();
1617     }
1618     local_menu->Append(config_id_base + ConfigMenuPreferences, _L("&Preferences") + dots +
1619 #ifdef __APPLE__
1620         "\tCtrl+,",
1621 #else
1622         "\tCtrl+P",
1623 #endif
1624         _L("Application preferences"));
1625     wxMenu* mode_menu = nullptr;
1626     if (is_editor()) {
1627         local_menu->AppendSeparator();
1628         mode_menu = new wxMenu();
1629         mode_menu->AppendRadioItem(config_id_base + ConfigMenuModeSimple, _L("Simple"), _L("Simple View Mode"));
1630 //    mode_menu->AppendRadioItem(config_id_base + ConfigMenuModeAdvanced, _L("Advanced"), _L("Advanced View Mode"));
1631         mode_menu->AppendRadioItem(config_id_base + ConfigMenuModeAdvanced, _CTX(L_CONTEXT("Advanced", "Mode"), "Mode"), _L("Advanced View Mode"));
1632         mode_menu->AppendRadioItem(config_id_base + ConfigMenuModeExpert, _L("Expert"), _L("Expert View Mode"));
1633         Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& evt) { if (get_mode() == comSimple) evt.Check(true); }, config_id_base + ConfigMenuModeSimple);
1634         Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& evt) { if (get_mode() == comAdvanced) evt.Check(true); }, config_id_base + ConfigMenuModeAdvanced);
1635         Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& evt) { if (get_mode() == comExpert) evt.Check(true); }, config_id_base + ConfigMenuModeExpert);
1636 
1637         local_menu->AppendSubMenu(mode_menu, _L("Mode"), wxString::Format(_L("%s View Mode"), SLIC3R_APP_NAME));
1638     }
1639     local_menu->AppendSeparator();
1640     local_menu->Append(config_id_base + ConfigMenuLanguage, _L("&Language"));
1641     if (is_editor()) {
1642         local_menu->AppendSeparator();
1643         local_menu->Append(config_id_base + ConfigMenuFlashFirmware, _L("Flash printer &firmware"), _L("Upload a firmware image into an Arduino based printer"));
1644         // TODO: for when we're able to flash dictionaries
1645         // local_menu->Append(config_id_base + FirmwareMenuDict,  _L("Flash language file"),    _L("Upload a language dictionary file into a Prusa printer"));
1646     }
1647 
1648     local_menu->Bind(wxEVT_MENU, [this, config_id_base](wxEvent &event) {
1649         switch (event.GetId() - config_id_base) {
1650         case ConfigMenuWizard:
1651             run_wizard(ConfigWizard::RR_USER);
1652             break;
1653 		case ConfigMenuUpdate:
1654 			check_updates(true);
1655 			break;
1656         case ConfigMenuTakeSnapshot:
1657             // Take a configuration snapshot.
1658             if (check_unsaved_changes()) {
1659                 wxTextEntryDialog dlg(nullptr, _L("Taking configuration snapshot"), _L("Snapshot name"));
1660 
1661                 // set current normal font for dialog children,
1662                 // because of just dlg.SetFont(normal_font()) has no result;
1663                 for (auto child : dlg.GetChildren())
1664                     child->SetFont(normal_font());
1665 
1666                 if (dlg.ShowModal() == wxID_OK)
1667                     if (const Config::Snapshot *snapshot = Config::take_config_snapshot_report_error(
1668                             *app_config, Config::Snapshot::SNAPSHOT_USER, dlg.GetValue().ToUTF8().data());
1669                         snapshot != nullptr)
1670                         app_config->set("on_snapshot", snapshot->id);
1671             }
1672             break;
1673         case ConfigMenuSnapshots:
1674             if (check_unsaved_changes()) {
1675                 std::string on_snapshot;
1676                 if (Config::SnapshotDB::singleton().is_on_snapshot(*app_config))
1677                     on_snapshot = app_config->get("on_snapshot");
1678                 ConfigSnapshotDialog dlg(Slic3r::GUI::Config::SnapshotDB::singleton(), on_snapshot);
1679                 dlg.ShowModal();
1680                 if (!dlg.snapshot_to_activate().empty()) {
1681                     if (! Config::SnapshotDB::singleton().is_on_snapshot(*app_config) &&
1682                         ! Config::take_config_snapshot_cancel_on_error(*app_config, Config::Snapshot::SNAPSHOT_BEFORE_ROLLBACK, "",
1683                                 GUI::format(_L("Continue to activate a configuration snapshot %1%?"), dlg.snapshot_to_activate())))
1684                         break;
1685                     try {
1686                         app_config->set("on_snapshot", Config::SnapshotDB::singleton().restore_snapshot(dlg.snapshot_to_activate(), *app_config).id);
1687                         // Enable substitutions, log both user and system substitutions. There should not be any substitutions performed when loading system
1688                         // presets because compatibility of profiles shall be verified using the min_slic3r_version keys in config index, but users
1689                         // are known to be creative and mess with the config files in various ways.
1690                         if (PresetsConfigSubstitutions all_substitutions = preset_bundle->load_presets(*app_config, ForwardCompatibilitySubstitutionRule::Enable);
1691                             ! all_substitutions.empty())
1692                             show_substitutions_info(all_substitutions);
1693 
1694                         // Load the currently selected preset into the GUI, update the preset selection box.
1695                         load_current_presets();
1696 
1697                         // update config wizard in respect to the new config
1698                         update_wizard_from_config();
1699                     } catch (std::exception &ex) {
1700                         GUI::show_error(nullptr, _L("Failed to activate configuration snapshot.") + "\n" + into_u8(ex.what()));
1701                     }
1702                 }
1703             }
1704             break;
1705         case ConfigMenuPreferences:
1706         {
1707             bool app_layout_changed = false;
1708             {
1709                 // the dialog needs to be destroyed before the call to recreate_GUI()
1710                 // or sometimes the application crashes into wxDialogBase() destructor
1711                 // so we put it into an inner scope
1712                 PreferencesDialog dlg(mainframe);
1713                 dlg.ShowModal();
1714                 app_layout_changed = dlg.settings_layout_changed();
1715                 if (dlg.seq_top_layer_only_changed())
1716                     this->plater_->refresh_print();
1717 #if ENABLE_CUSTOMIZABLE_FILES_ASSOCIATION_ON_WIN
1718 #ifdef _WIN32
1719                 if (is_editor()) {
1720                     if (app_config->get("associate_3mf") == "1")
1721                         associate_3mf_files();
1722                     if (app_config->get("associate_stl") == "1")
1723                         associate_stl_files();
1724                 }
1725                 else {
1726                     if (app_config->get("associate_gcode") == "1")
1727                         associate_gcode_files();
1728                 }
1729 #endif // _WIN32
1730 #endif // ENABLE_CUSTOMIZABLE_FILES_ASSOCIATION_ON_WIN
1731             }
1732             if (app_layout_changed) {
1733                 // hide full main_sizer for mainFrame
1734                 mainframe->GetSizer()->Show(false);
1735                 mainframe->update_layout();
1736                 mainframe->select_tab(size_t(0));
1737             }
1738             break;
1739         }
1740         case ConfigMenuLanguage:
1741         {
1742             /* Before change application language, let's check unsaved changes on 3D-Scene
1743              * and draw user's attention to the application restarting after a language change
1744              */
1745             {
1746                 // the dialog needs to be destroyed before the call to switch_language()
1747                 // or sometimes the application crashes into wxDialogBase() destructor
1748                 // so we put it into an inner scope
1749                 wxString title = is_editor() ? wxString(SLIC3R_APP_NAME) : wxString(GCODEVIEWER_APP_NAME);
1750                 title += " - " + _L("Language selection");
1751                 wxMessageDialog dialog(nullptr,
1752                     _L("Switching the language will trigger application restart.\n"
1753                         "You will lose content of the plater.") + "\n\n" +
1754                     _L("Do you want to proceed?"),
1755                     title,
1756                     wxICON_QUESTION | wxOK | wxCANCEL);
1757                 if (dialog.ShowModal() == wxID_CANCEL)
1758                     return;
1759             }
1760 
1761             switch_language();
1762             break;
1763         }
1764         case ConfigMenuFlashFirmware:
1765             FirmwareDialog::run(mainframe);
1766             break;
1767         default:
1768             break;
1769         }
1770     });
1771 
1772     using std::placeholders::_1;
1773 
1774     if (mode_menu != nullptr) {
1775         auto modfn = [this](int mode, wxCommandEvent&) { if (get_mode() != mode) save_mode(mode); };
1776         mode_menu->Bind(wxEVT_MENU, std::bind(modfn, comSimple, _1), config_id_base + ConfigMenuModeSimple);
1777         mode_menu->Bind(wxEVT_MENU, std::bind(modfn, comAdvanced, _1), config_id_base + ConfigMenuModeAdvanced);
1778         mode_menu->Bind(wxEVT_MENU, std::bind(modfn, comExpert, _1), config_id_base + ConfigMenuModeExpert);
1779     }
1780 
1781     menu->Append(local_menu, _L("&Configuration"));
1782 }
1783 
1784 // This is called when closing the application, when loading a config file or when starting the config wizard
1785 // to notify the user whether he is aware that some preset changes will be lost.
1786 bool GUI_App::check_unsaved_changes(const wxString &header)
1787 {
1788     PrinterTechnology printer_technology = preset_bundle->printers.get_edited_preset().printer_technology();
1789 
1790     bool has_unsaved_changes = false;
1791     for (Tab* tab : tabs_list)
1792         if (tab->supports_printer_technology(printer_technology) && tab->current_preset_is_dirty()) {
1793             has_unsaved_changes = true;
1794             break;
1795         }
1796 
1797     if (has_unsaved_changes)
1798     {
1799         UnsavedChangesDialog dlg(header);
1800         if (wxGetApp().app_config->get("default_action_on_close_application") == "none" && dlg.ShowModal() == wxID_CANCEL)
1801             return false;
1802 
1803         if (dlg.save_preset())  // save selected changes
1804         {
1805             for (const std::pair<std::string, Preset::Type>& nt : dlg.get_names_and_types())
1806                 preset_bundle->save_changes_for_preset(nt.first, nt.second, dlg.get_unselected_options(nt.second));
1807 
1808             // if we saved changes to the new presets, we should to
1809             // synchronize config.ini with the current selections.
1810             preset_bundle->export_selections(*app_config);
1811 
1812             wxMessageBox(_L("The preset(s) modifications are successfully saved"));
1813         }
1814     }
1815 
1816     return true;
1817 }
1818 
1819 bool GUI_App::check_print_host_queue()
1820 {
1821     wxString dirty;
1822     std::vector<std::pair<std::string, std::string>> jobs;
1823     // Get ongoing jobs from dialog
1824     mainframe->m_printhost_queue_dlg->get_active_jobs(jobs);
1825     if (jobs.empty())
1826         return true;
1827     // Show dialog
1828     wxString job_string = wxString();
1829     for (const auto& job : jobs) {
1830         job_string += format_wxstr("   %1% : %2% \n", job.first, job.second);
1831     }
1832     wxString message;
1833     message += _(L("The uploads are still ongoing")) + ":\n\n" + job_string +"\n" + _(L("Stop them and continue anyway?"));
1834     wxMessageDialog dialog(mainframe,
1835         message,
1836         wxString(SLIC3R_APP_NAME) + " - " + _(L("Ongoing uploads")),
1837         wxICON_QUESTION | wxYES_NO | wxNO_DEFAULT);
1838     if (dialog.ShowModal() == wxID_YES)
1839         return true;
1840 
1841     // TODO: If already shown, bring forward
1842     mainframe->m_printhost_queue_dlg->Show();
1843     return false;
1844 }
1845 
1846 bool GUI_App::checked_tab(Tab* tab)
1847 {
1848     bool ret = true;
1849     if (find(tabs_list.begin(), tabs_list.end(), tab) == tabs_list.end())
1850         ret = false;
1851     return ret;
1852 }
1853 
1854 // Update UI / Tabs to reflect changes in the currently loaded presets
1855 void GUI_App::load_current_presets(bool check_printer_presets_ /*= true*/)
1856 {
1857     // check printer_presets for the containing information about "Print Host upload"
1858     // and create physical printer from it, if any exists
1859     if (check_printer_presets_)
1860         check_printer_presets();
1861 
1862     PrinterTechnology printer_technology = preset_bundle->printers.get_edited_preset().printer_technology();
1863 	this->plater()->set_printer_technology(printer_technology);
1864     for (Tab *tab : tabs_list)
1865 		if (tab->supports_printer_technology(printer_technology)) {
1866 			if (tab->type() == Preset::TYPE_PRINTER) {
1867 				static_cast<TabPrinter*>(tab)->update_pages();
1868 				// Mark the plater to update print bed by tab->load_current_preset() from Plater::on_config_change().
1869 				this->plater()->force_print_bed_update();
1870 			}
1871 			tab->load_current_preset();
1872 		}
1873 }
1874 
1875 void GUI_App::update_wizard_from_config()
1876 {
1877     if (!m_wizard)
1878         return;
1879     // If ConfigWizard was created before changing of the configuration,
1880     // we have to destroy it to have possibility to create it again in respect to the new config's parameters
1881     m_wizard->Reparent(nullptr);
1882     m_wizard->Destroy();
1883     m_wizard = nullptr;
1884 }
1885 
1886 bool GUI_App::OnExceptionInMainLoop()
1887 {
1888     generic_exception_handle();
1889     return false;
1890 }
1891 
1892 #ifdef __APPLE__
1893 // This callback is called from wxEntry()->wxApp::CallOnInit()->NSApplication run
1894 // that is, before GUI_App::OnInit(), so we have a chance to switch GUI_App
1895 // to a G-code viewer.
1896 void GUI_App::OSXStoreOpenFiles(const wxArrayString &fileNames)
1897 {
1898     size_t num_gcodes = 0;
1899     for (const wxString &filename : fileNames)
1900         if (is_gcode_file(into_u8(filename)))
1901             ++ num_gcodes;
1902     if (fileNames.size() == num_gcodes) {
1903         // Opening PrusaSlicer by drag & dropping a G-Code onto PrusaSlicer icon in Finder,
1904         // just G-codes were passed. Switch to G-code viewer mode.
1905         m_app_mode = EAppMode::GCodeViewer;
1906         unlock_lockfile(get_instance_hash_string() + ".lock", data_dir() + "/cache/");
1907         if(app_config != nullptr)
1908             delete app_config;
1909         app_config = nullptr;
1910         init_app_config();
1911     }
1912     wxApp::OSXStoreOpenFiles(fileNames);
1913 }
1914 // wxWidgets override to get an event on open files.
1915 void GUI_App::MacOpenFiles(const wxArrayString &fileNames)
1916 {
1917     std::vector<std::string> files;
1918     std::vector<wxString>    gcode_files;
1919     std::vector<wxString>    non_gcode_files;
1920     for (const auto& filename : fileNames) {
1921         if (is_gcode_file(into_u8(filename)))
1922             gcode_files.emplace_back(filename);
1923         else {
1924             files.emplace_back(into_u8(filename));
1925             non_gcode_files.emplace_back(filename);
1926         }
1927     }
1928     if (m_app_mode == EAppMode::GCodeViewer) {
1929         // Running in G-code viewer.
1930         // Load the first G-code into the G-code viewer.
1931         // Or if no G-codes, send other files to slicer.
1932         if (! gcode_files.empty())
1933             this->plater()->load_gcode(gcode_files.front());
1934         if (!non_gcode_files.empty())
1935             start_new_slicer(non_gcode_files, true);
1936     } else {
1937         if (! files.empty()) {
1938 #if ENABLE_DRAG_AND_DROP_FIX
1939             wxArrayString input_files;
1940             for (size_t i = 0; i < non_gcode_files.size(); ++i) {
1941                 input_files.push_back(non_gcode_files[i]);
1942             }
1943             this->plater()->load_files(input_files);
1944 #else
1945             this->plater()->load_files(files, true, true);
1946 #endif
1947         }
1948         for (const wxString &filename : gcode_files)
1949             start_new_gcodeviewer(&filename);
1950     }
1951 }
1952 #endif /* __APPLE */
1953 
1954 Sidebar& GUI_App::sidebar()
1955 {
1956     return plater_->sidebar();
1957 }
1958 
1959 ObjectManipulation* GUI_App::obj_manipul()
1960 {
1961     // If this method is called before plater_ has been initialized, return nullptr (to avoid a crash)
1962     return (plater_ != nullptr) ? sidebar().obj_manipul() : nullptr;
1963 }
1964 
1965 ObjectSettings* GUI_App::obj_settings()
1966 {
1967     return sidebar().obj_settings();
1968 }
1969 
1970 ObjectList* GUI_App::obj_list()
1971 {
1972     return sidebar().obj_list();
1973 }
1974 
1975 ObjectLayers* GUI_App::obj_layers()
1976 {
1977     return sidebar().obj_layers();
1978 }
1979 
1980 Plater* GUI_App::plater()
1981 {
1982     return plater_;
1983 }
1984 
1985 Model& GUI_App::model()
1986 {
1987     return plater_->model();
1988 }
1989 
1990 wxNotebook* GUI_App::tab_panel() const
1991 {
1992     return mainframe->m_tabpanel;
1993 }
1994 
1995 // extruders count from selected printer preset
1996 int GUI_App::extruders_cnt() const
1997 {
1998     const Preset& preset = preset_bundle->printers.get_selected_preset();
1999     return preset.printer_technology() == ptSLA ? 1 :
2000            preset.config.option<ConfigOptionFloats>("nozzle_diameter")->values.size();
2001 }
2002 
2003 // extruders count from edited printer preset
2004 int GUI_App::extruders_edited_cnt() const
2005 {
2006     const Preset& preset = preset_bundle->printers.get_edited_preset();
2007     return preset.printer_technology() == ptSLA ? 1 :
2008            preset.config.option<ConfigOptionFloats>("nozzle_diameter")->values.size();
2009 }
2010 
2011 wxString GUI_App::current_language_code_safe() const
2012 {
2013 	// Translate the language code to a code, for which Prusa Research maintains translations.
2014 	const std::map<wxString, wxString> mapping {
2015 		{ "cs", 	"cs_CZ", },
2016 		{ "sk", 	"cs_CZ", },
2017 		{ "de", 	"de_DE", },
2018 		{ "es", 	"es_ES", },
2019 		{ "fr", 	"fr_FR", },
2020 		{ "it", 	"it_IT", },
2021 		{ "ja", 	"ja_JP", },
2022 		{ "ko", 	"ko_KR", },
2023 		{ "pl", 	"pl_PL", },
2024 		{ "uk", 	"uk_UA", },
2025 		{ "zh", 	"zh_CN", },
2026 		{ "ru", 	"ru_RU", },
2027 	};
2028 	wxString language_code = this->current_language_code().BeforeFirst('_');
2029 	auto it = mapping.find(language_code);
2030 	if (it != mapping.end())
2031 		language_code = it->second;
2032 	else
2033 		language_code = "en_US";
2034 	return language_code;
2035 }
2036 
2037 void GUI_App::open_web_page_localized(const std::string &http_address)
2038 {
2039     wxLaunchDefaultBrowser(http_address + "&lng=" + this->current_language_code_safe());
2040 }
2041 
2042 bool GUI_App::run_wizard(ConfigWizard::RunReason reason, ConfigWizard::StartPage start_page)
2043 {
2044     wxCHECK_MSG(mainframe != nullptr, false, "Internal error: Main frame not created / null");
2045 
2046     if (reason == ConfigWizard::RR_USER)
2047         if (PresetUpdater::UpdateResult result = preset_updater->config_update(app_config->orig_version(), PresetUpdater::UpdateParams::FORCED_BEFORE_WIZARD);
2048             result == PresetUpdater::R_ALL_CANCELED)
2049             return false;
2050 
2051     if (! m_wizard) {
2052         wxBusyCursor wait;
2053         m_wizard = new ConfigWizard(mainframe);
2054     }
2055 
2056     const bool res = m_wizard->run(reason, start_page);
2057 
2058     if (res) {
2059         load_current_presets();
2060 
2061         if (preset_bundle->printers.get_edited_preset().printer_technology() == ptSLA
2062             && Slic3r::model_has_multi_part_objects(wxGetApp().model())) {
2063             GUI::show_info(nullptr,
2064                 _L("It's impossible to print multi-part object(s) with SLA technology.") + "\n\n" +
2065                 _L("Please check and fix your object list."),
2066                 _L("Attention!"));
2067         }
2068     }
2069 
2070     return res;
2071 }
2072 
2073 #if ENABLE_THUMBNAIL_GENERATOR_DEBUG
2074 void GUI_App::gcode_thumbnails_debug()
2075 {
2076     const std::string BEGIN_MASK = "; thumbnail begin";
2077     const std::string END_MASK = "; thumbnail end";
2078     std::string gcode_line;
2079     bool reading_image = false;
2080     unsigned int width = 0;
2081     unsigned int height = 0;
2082 
2083     wxFileDialog dialog(GetTopWindow(), _L("Select a gcode file:"), "", "", "G-code files (*.gcode)|*.gcode;*.GCODE;", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
2084     if (dialog.ShowModal() != wxID_OK)
2085         return;
2086 
2087     std::string in_filename = into_u8(dialog.GetPath());
2088     std::string out_path = boost::filesystem::path(in_filename).remove_filename().append(L"thumbnail").string();
2089 
2090     boost::nowide::ifstream in_file(in_filename.c_str());
2091     std::vector<std::string> rows;
2092     std::string row;
2093     if (in_file.good())
2094     {
2095         while (std::getline(in_file, gcode_line))
2096         {
2097             if (in_file.good())
2098             {
2099                 if (boost::starts_with(gcode_line, BEGIN_MASK))
2100                 {
2101                     reading_image = true;
2102                     gcode_line = gcode_line.substr(BEGIN_MASK.length() + 1);
2103                     std::string::size_type x_pos = gcode_line.find('x');
2104                     std::string width_str = gcode_line.substr(0, x_pos);
2105                     width = (unsigned int)::atoi(width_str.c_str());
2106                     std::string height_str = gcode_line.substr(x_pos + 1);
2107                     height = (unsigned int)::atoi(height_str.c_str());
2108                     row.clear();
2109                 }
2110                 else if (reading_image && boost::starts_with(gcode_line, END_MASK))
2111                 {
2112                     std::string out_filename = out_path + std::to_string(width) + "x" + std::to_string(height) + ".png";
2113                     boost::nowide::ofstream out_file(out_filename.c_str(), std::ios::binary);
2114                     if (out_file.good())
2115                     {
2116                         std::string decoded;
2117                         decoded.resize(boost::beast::detail::base64::decoded_size(row.size()));
2118                         decoded.resize(boost::beast::detail::base64::decode((void*)&decoded[0], row.data(), row.size()).first);
2119 
2120                         out_file.write(decoded.c_str(), decoded.size());
2121                         out_file.close();
2122                     }
2123 
2124                     reading_image = false;
2125                     width = 0;
2126                     height = 0;
2127                     rows.clear();
2128                 }
2129                 else if (reading_image)
2130                     row += gcode_line.substr(2);
2131             }
2132         }
2133 
2134         in_file.close();
2135     }
2136 }
2137 #endif // ENABLE_THUMBNAIL_GENERATOR_DEBUG
2138 
2139 void GUI_App::window_pos_save(wxTopLevelWindow* window, const std::string &name)
2140 {
2141     if (name.empty()) { return; }
2142     const auto config_key = (boost::format("window_%1%") % name).str();
2143 
2144     WindowMetrics metrics = WindowMetrics::from_window(window);
2145     app_config->set(config_key, metrics.serialize());
2146     app_config->save();
2147 }
2148 
2149 void GUI_App::window_pos_restore(wxTopLevelWindow* window, const std::string &name, bool default_maximized)
2150 {
2151     if (name.empty()) { return; }
2152     const auto config_key = (boost::format("window_%1%") % name).str();
2153 
2154     if (! app_config->has(config_key)) {
2155         window->Maximize(default_maximized);
2156         return;
2157     }
2158 
2159     auto metrics = WindowMetrics::deserialize(app_config->get(config_key));
2160     if (! metrics) {
2161         window->Maximize(default_maximized);
2162         return;
2163     }
2164 
2165     const wxRect& rect = metrics->get_rect();
2166     window->SetPosition(rect.GetPosition());
2167     window->SetSize(rect.GetSize());
2168     window->Maximize(metrics->get_maximized());
2169 }
2170 
2171 void GUI_App::window_pos_sanitize(wxTopLevelWindow* window)
2172 {
2173     /*unsigned*/int display_idx = wxDisplay::GetFromWindow(window);
2174     wxRect display;
2175     if (display_idx == wxNOT_FOUND) {
2176         display = wxDisplay(0u).GetClientArea();
2177         window->Move(display.GetTopLeft());
2178     } else {
2179         display = wxDisplay(display_idx).GetClientArea();
2180     }
2181 
2182     auto metrics = WindowMetrics::from_window(window);
2183     metrics.sanitize_for_display(display);
2184     if (window->GetScreenRect() != metrics.get_rect()) {
2185         window->SetSize(metrics.get_rect());
2186     }
2187 }
2188 
2189 bool GUI_App::config_wizard_startup()
2190 {
2191     if (!m_app_conf_exists || preset_bundle->printers.size() <= 1) {
2192         run_wizard(ConfigWizard::RR_DATA_EMPTY);
2193         return true;
2194     } else if (get_app_config()->legacy_datadir()) {
2195         // Looks like user has legacy pre-vendorbundle data directory,
2196         // explain what this is and run the wizard
2197 
2198         MsgDataLegacy dlg;
2199         dlg.ShowModal();
2200 
2201         run_wizard(ConfigWizard::RR_DATA_LEGACY);
2202         return true;
2203     }
2204     return false;
2205 }
2206 
2207 void GUI_App::check_updates(const bool verbose)
2208 {
2209 	PresetUpdater::UpdateResult updater_result;
2210 	try {
2211 		updater_result = preset_updater->config_update(app_config->orig_version(), verbose ? PresetUpdater::UpdateParams::SHOW_TEXT_BOX : PresetUpdater::UpdateParams::SHOW_NOTIFICATION);
2212 		if (updater_result == PresetUpdater::R_INCOMPAT_EXIT) {
2213 			mainframe->Close();
2214 		}
2215 		else if (updater_result == PresetUpdater::R_INCOMPAT_CONFIGURED) {
2216             m_app_conf_exists = true;
2217 		}
2218 		else if (verbose && updater_result == PresetUpdater::R_NOOP) {
2219 			MsgNoUpdates dlg;
2220 			dlg.ShowModal();
2221 		}
2222 	}
2223 	catch (const std::exception & ex) {
2224 		show_error(nullptr, ex.what());
2225 	}
2226 }
2227 
2228 // static method accepting a wxWindow object as first parameter
2229 // void warning_catcher{
2230 //     my($self, $message_dialog) = @_;
2231 //     return sub{
2232 //         my $message = shift;
2233 //         return if $message = ~/ GLUquadricObjPtr | Attempt to free unreferenced scalar / ;
2234 //         my @params = ($message, 'Warning', wxOK | wxICON_WARNING);
2235 //         $message_dialog
2236 //             ? $message_dialog->(@params)
2237 //             : Wx::MessageDialog->new($self, @params)->ShowModal;
2238 //     };
2239 // }
2240 
2241 // Do we need this function???
2242 // void GUI_App::notify(message) {
2243 //     auto frame = GetTopWindow();
2244 //     // try harder to attract user attention on OS X
2245 //     if (!frame->IsActive())
2246 //         frame->RequestUserAttention(defined(__WXOSX__/*&Wx::wxMAC */)? wxUSER_ATTENTION_ERROR : wxUSER_ATTENTION_INFO);
2247 //
2248 //     // There used to be notifier using a Growl application for OSX, but Growl is dead.
2249 //     // The notifier also supported the Linux X D - bus notifications, but that support was broken.
2250 //     //TODO use wxNotificationMessage ?
2251 // }
2252 
2253 
2254 #ifdef __WXMSW__
2255 static bool set_into_win_registry(HKEY hkeyHive, const wchar_t* pszVar, const wchar_t* pszValue)
2256 {
2257     // see as reference: https://stackoverflow.com/questions/20245262/c-program-needs-an-file-association
2258     wchar_t szValueCurrent[1000];
2259     DWORD dwType;
2260     DWORD dwSize = sizeof(szValueCurrent);
2261 
2262     int iRC = ::RegGetValueW(hkeyHive, pszVar, nullptr, RRF_RT_ANY, &dwType, szValueCurrent, &dwSize);
2263 
2264     bool bDidntExist = iRC == ERROR_FILE_NOT_FOUND;
2265 
2266     if ((iRC != ERROR_SUCCESS) && !bDidntExist)
2267         // an error occurred
2268         return false;
2269 
2270     if (!bDidntExist) {
2271         if (dwType != REG_SZ)
2272             // invalid type
2273             return false;
2274 
2275         if (::wcscmp(szValueCurrent, pszValue) == 0)
2276             // value already set
2277             return false;
2278     }
2279 
2280     DWORD dwDisposition;
2281     HKEY hkey;
2282     iRC = ::RegCreateKeyExW(hkeyHive, pszVar, 0, 0, 0, KEY_ALL_ACCESS, nullptr, &hkey, &dwDisposition);
2283     bool ret = false;
2284     if (iRC == ERROR_SUCCESS) {
2285         iRC = ::RegSetValueExW(hkey, L"", 0, REG_SZ, (BYTE*)pszValue, (::wcslen(pszValue) + 1) * sizeof(wchar_t));
2286         if (iRC == ERROR_SUCCESS)
2287             ret = true;
2288     }
2289 
2290     RegCloseKey(hkey);
2291     return ret;
2292 }
2293 
2294 void GUI_App::associate_3mf_files()
2295 {
2296     wchar_t app_path[MAX_PATH];
2297     ::GetModuleFileNameW(nullptr, app_path, sizeof(app_path));
2298 
2299     std::wstring prog_path = L"\"" + std::wstring(app_path) + L"\"";
2300     std::wstring prog_id = L"Prusa.Slicer.1";
2301     std::wstring prog_desc = L"PrusaSlicer";
2302     std::wstring prog_command = prog_path + L" \"%1\"";
2303     std::wstring reg_base = L"Software\\Classes";
2304     std::wstring reg_extension = reg_base + L"\\.3mf";
2305     std::wstring reg_prog_id = reg_base + L"\\" + prog_id;
2306     std::wstring reg_prog_id_command = reg_prog_id + L"\\Shell\\Open\\Command";
2307 
2308     bool is_new = false;
2309     is_new |= set_into_win_registry(HKEY_CURRENT_USER, reg_extension.c_str(), prog_id.c_str());
2310     is_new |= set_into_win_registry(HKEY_CURRENT_USER, reg_prog_id.c_str(), prog_desc.c_str());
2311     is_new |= set_into_win_registry(HKEY_CURRENT_USER, reg_prog_id_command.c_str(), prog_command.c_str());
2312 
2313     if (is_new)
2314         // notify Windows only when any of the values gets changed
2315         ::SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nullptr, nullptr);
2316 }
2317 
2318 #if ENABLE_CUSTOMIZABLE_FILES_ASSOCIATION_ON_WIN
2319 void GUI_App::associate_stl_files()
2320 {
2321     wchar_t app_path[MAX_PATH];
2322     ::GetModuleFileNameW(nullptr, app_path, sizeof(app_path));
2323 
2324     std::wstring prog_path = L"\"" + std::wstring(app_path) + L"\"";
2325     std::wstring prog_id = L"Prusa.Slicer.1";
2326     std::wstring prog_desc = L"PrusaSlicer";
2327     std::wstring prog_command = prog_path + L" \"%1\"";
2328     std::wstring reg_base = L"Software\\Classes";
2329     std::wstring reg_extension = reg_base + L"\\.stl";
2330     std::wstring reg_prog_id = reg_base + L"\\" + prog_id;
2331     std::wstring reg_prog_id_command = reg_prog_id + L"\\Shell\\Open\\Command";
2332 
2333     bool is_new = false;
2334     is_new |= set_into_win_registry(HKEY_CURRENT_USER, reg_extension.c_str(), prog_id.c_str());
2335     is_new |= set_into_win_registry(HKEY_CURRENT_USER, reg_prog_id.c_str(), prog_desc.c_str());
2336     is_new |= set_into_win_registry(HKEY_CURRENT_USER, reg_prog_id_command.c_str(), prog_command.c_str());
2337 
2338     if (is_new)
2339         // notify Windows only when any of the values gets changed
2340         ::SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nullptr, nullptr);
2341 }
2342 #endif // ENABLE_CUSTOMIZABLE_FILES_ASSOCIATION_ON_WIN
2343 
2344 void GUI_App::associate_gcode_files()
2345 {
2346     wchar_t app_path[MAX_PATH];
2347     ::GetModuleFileNameW(nullptr, app_path, sizeof(app_path));
2348 
2349     std::wstring prog_path = L"\"" + std::wstring(app_path) + L"\"";
2350     std::wstring prog_id = L"PrusaSlicer.GCodeViewer.1";
2351     std::wstring prog_desc = L"PrusaSlicerGCodeViewer";
2352     std::wstring prog_command = prog_path + L" \"%1\"";
2353     std::wstring reg_base = L"Software\\Classes";
2354     std::wstring reg_extension = reg_base + L"\\.gcode";
2355     std::wstring reg_prog_id = reg_base + L"\\" + prog_id;
2356     std::wstring reg_prog_id_command = reg_prog_id + L"\\Shell\\Open\\Command";
2357 
2358     bool is_new = false;
2359     is_new |= set_into_win_registry(HKEY_CURRENT_USER, reg_extension.c_str(), prog_id.c_str());
2360     is_new |= set_into_win_registry(HKEY_CURRENT_USER, reg_prog_id.c_str(), prog_desc.c_str());
2361     is_new |= set_into_win_registry(HKEY_CURRENT_USER, reg_prog_id_command.c_str(), prog_command.c_str());
2362 
2363     if (is_new)
2364         // notify Windows only when any of the values gets changed
2365         ::SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nullptr, nullptr);
2366 }
2367 #endif // __WXMSW__
2368 
2369 } // GUI
2370 } //Slic3r
2371