-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathGUI_App.cpp
More file actions
10583 lines (9473 loc) · 426 KB
/
GUI_App.cpp
File metadata and controls
10583 lines (9473 loc) · 426 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "libslic3r/Technologies.hpp"
#include "GUI_App.hpp"
#include "GUI_Init.hpp"
#include "GUI_ObjectList.hpp"
#include "GUI_Factories.hpp"
#include "slic3r/GUI/UserManager.hpp"
#include "slic3r/GUI/TaskManager.hpp"
#include "format.hpp"
#include "libslic3r_version.h"
#include "Downloader.hpp"
#include <string>
#include <wx/colour.h>
#include <wx/event.h>
#include <wx/bitmap.h>
#include <wx/string.h>
#include <wx/dialog.h>
#include "slic3r/GUI/print_manage/utils/cxmdns.h"
#include "slic3r/GUI/print_manage/Utils.hpp"
#include "CrealityrintDump.hpp"
#include "slic3r/Utils/ColorSpaceConvert.hpp"
#include "ImGuiWrapper.hpp"
#include "slic3r/GUI/DeviceManager.hpp"
#include "slic3r/GUI/DeviceManager.hpp"
#include "slic3r/Utils/NetworkAgent.hpp"
#include "slic3r/GUI/HMS.hpp"
#include "slic3r/GUI/WebViewDialog.hpp"
#include "slic3r/GUI/WebUserLoginDialog.hpp"
#include "slic3r/GUI/FileDownloader.hpp"
#include "slic3r/GUI/print_manage/utils/cxmdns.h"
#include "slic3r/GUI/print_manage/Utils.hpp"
#include "Widgets/HoverBorderIcon.hpp"
// Localization headers: include libslic3r version first so everything in this file
// uses the slic3r/GUI version (the macros will take precedence over the functions).
// Also, there is a check that the former is not included from slic3r module.
// This is the only place where we want to allow that, so define an override macro.
#define SLIC3R_ALLOW_LIBSLIC3R_I18N_IN_SLIC3R
#include "libslic3r/I18N.hpp"
#undef SLIC3R_ALLOW_LIBSLIC3R_I18N_IN_SLIC3R
#include "slic3r/GUI/I18N.hpp"
#include <algorithm>
#include <iterator>
#include <exception>
#include <cstdlib>
#include <regex>
#include <thread>
#include <string_view>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/log/trivial.hpp>
#include <boost/nowide/convert.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <wx/stdpaths.h>
#include <wx/imagpng.h>
#include <wx/display.h>
#include <wx/menu.h>
#include <wx/menuitem.h>
#include <wx/filedlg.h>
#include <wx/progdlg.h>
#include <wx/dir.h>
#include <wx/wupdlock.h>
#include <wx/filefn.h>
#include <wx/sysopt.h>
#include <wx/richmsgdlg.h>
#include <wx/log.h>
#include <wx/intl.h>
#include <wx/tokenzr.h>
#include <wx/dialog.h>
#include <wx/textctrl.h>
#include <wx/splash.h>
#include <wx/fontutil.h>
#include <wx/glcanvas.h>
#include <wx/dcgraph.h>
#include <wx/evtloop.h>
#include "libslic3r/Utils.hpp"
#include "libslic3r/Model.hpp"
#include "libslic3r/PresetBundle.hpp"
#include "libslic3r/Thread.hpp"
#include "libslic3r/miniz_extension.hpp"
#include "libslic3r/Color.hpp"
#include "slic3r/GUI/Project.hpp"
#ifdef _WIN32
#include "libslic3r/UnittestFlow.hpp"
#include "libslic3r/AutomationMgr.hpp"
#endif
#include "GUI.hpp"
#include "GUI_Utils.hpp"
#include "3DScene.hpp"
#include "MainFrame.hpp"
#include "Plater.hpp"
#include "GLCanvas3D.hpp"
#include "AnalyticsDataUploadManager.hpp"
#include "../Utils/PresetUpdater.hpp"
#include "../Utils/PrintHost.hpp"
#include "../Utils/Process.hpp"
#include "../Utils/MacDarkMode.hpp"
#include "../Utils/Http.hpp"
#include "../Utils/UndoRedo.hpp"
#include "slic3r/Config/Snapshot.hpp"
#include "Preferences.hpp"
#include "ConfigRelateDialog.hpp"
#include "Tab.hpp"
#include "SysInfoDialog.hpp"
#include "UpdateDialogs.hpp"
#include "Mouse3DController.hpp"
#include "RemovableDriveManager.hpp"
#include "InstanceCheck.hpp"
#include "NotificationManager.hpp"
#include "UnsavedChangesDialog.hpp"
#include "SavePresetDialog.hpp"
#include "PrintHostDialogs.hpp"
#include "DesktopIntegrationDialog.hpp"
#include "SendSystemInfoDialog.hpp"
#include "ParamsDialog.hpp"
#include "KBShortcutsDialog.hpp"
#include "DownloadProgressDialog.hpp"
#include "BitmapCache.hpp"
#include "Notebook.hpp"
#include "Widgets/Label.hpp"
#include "Widgets/ProgressDialog.hpp"
//BBS: DailyTip and UserGuide Dialog
#include "WebDownPluginDlg.hpp"
#include "WebGuideDialog.hpp"
#include "ReleaseNote.hpp"
#include "PrivacyUpdateDialog.hpp"
#include "EnableLiteModeDialog.hpp"
#include "ModelMall.hpp"
#include "HintNotification.hpp"
#include <boost/url.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/regex.hpp>
#ifdef _WIN32
#include <boost/interprocess/sync/file_lock.hpp>
#endif
#include <slic3r/GUI/print_manage/AccountDeviceMgr.hpp>
#include "libslic3r/common_header/common_header.h"
#include "buildinfo.h"
//#ifdef WIN32
//#include "BaseException.h"
//#endif
#ifdef __WXMSW__
#include <dbt.h>
#include <shlobj.h>
#ifdef __WINDOWS__
#ifdef _MSW_DARK_MODE
#include "dark_mode.hpp"
#include "wx/headerctrl.h"
#include "wx/msw/headerctrl.h"
#endif // _MSW_DARK_MODE
#endif // __WINDOWS__
#endif
#ifdef _WIN32
#include <boost/dll/runtime_symbol_info.hpp>
#endif
#ifdef WIN32
#include "BaseException.h"
#include <iphlpapi.h>
#endif
#if ENABLE_THUMBNAIL_GENERATOR_DEBUG
#include <boost/beast/core/detail/base64.hpp>
#include <boost/nowide/fstream.hpp>
#endif // ENABLE_THUMBNAIL_GENERATOR_DEBUG
// Needed for forcing menu icons back under gtk2 and gtk3
#if defined(__WXGTK20__) || defined(__WXGTK3__)
#include <gtk/gtk.h>
#endif
#include "print_manage/UploadGcodeToCloud.hpp"
#include "print_manage/Upload3mfToCloud.hpp"
#include "libslic3r/GlobalConfig.hpp"
#include "SyncUserPresets.hpp"
#include "print_manage/AppMgr.hpp"
#include "UpdateParams.hpp"
#include "PrinterPresetConfig.hpp"
using namespace std::literals;
namespace pt = boost::property_tree;
namespace Slic3r {
namespace GUI {
class MainFrame;
void start_ping_test()
{
return;
wxArrayString output;
wxExecute("ping www.amazon.com", output, wxEXEC_NODISABLE);
wxString output_i;
std::string output_temp;
for (int i = 0; i < output.size(); i++) {
output_i = output[i].To8BitData();
output_temp = output_i.ToStdString(wxConvUTF8);
BOOST_LOG_TRIVIAL(info) << "ping amazon:" << output_temp;
}
wxExecute("ping www.apple.com", output, wxEXEC_NODISABLE);
for (int i = 0; i < output.size(); i++) {
output_i = output[i].To8BitData();
output_temp = output_i.ToStdString(wxConvUTF8);
BOOST_LOG_TRIVIAL(info) << "ping www.apple.com:" << output_temp;
}
wxExecute("ping www.bambulab.com", output, wxEXEC_NODISABLE);
for (int i = 0; i < output.size(); i++) {
output_i = output[i].To8BitData();
output_temp = output_i.ToStdString(wxConvUTF8);
BOOST_LOG_TRIVIAL(info) << "ping bambulab:" << output_temp;
}
//Get GateWay IP
wxExecute("ping 192.168.0.1", output, wxEXEC_NODISABLE);
for (int i = 0; i < output.size(); i++) {
output_i = output[i].To8BitData();
output_temp = output_i.ToStdString(wxConvUTF8);
BOOST_LOG_TRIVIAL(info) << "ping 192.168.0.1:" << output_temp;
}
}
VersionInfo::VersionInfo()
{
for (int i = 0; i < VERSION_LEN; i++) {
ver_items[i] = 0;
}
force_upgrade = false;
version_str = "";
}
void VersionInfo::parse_version_str(std::string str)
{
version_str = str;
std::vector<std::string> items;
boost::split(items, str, boost::is_any_of("."));
if (items.size() == VERSION_LEN) {
try {
for (int i = 0; i < VERSION_LEN; i++) {
ver_items[i] = stoi(items[i]);
}
}
catch (...) {
;
}
}
}
std::string VersionInfo::convert_full_version(std::string short_version)
{
std::string result = "";
std::vector<std::string> items;
boost::split(items, short_version, boost::is_any_of("."));
if (items.size() == VERSION_LEN) {
for (int i = 0; i < VERSION_LEN; i++) {
std::stringstream ss;
ss << std::setw(2) << std::setfill('0') << items[i];
result += ss.str();
if (i != VERSION_LEN - 1)
result += ".";
}
return result;
}
return result;
}
std::string VersionInfo::convert_short_version(std::string full_version)
{
full_version.erase(std::remove(full_version.begin(), full_version.end(), '0'), full_version.end());
return full_version;
}
int VersionInfo::compare(std::string ver_str)
{
if (version_str.empty()) return -1;
int ver_target[VERSION_LEN];
std::vector<std::string> items;
boost::split(items, ver_str, boost::is_any_of("."));
if (items.size() == VERSION_LEN) {
try {
for (int i = 0; i < VERSION_LEN; i++) {
ver_target[i] = stoi(items[i]);
if (ver_target[i] < ver_items[i]) {
return 1;
}
else if (ver_target[i] == ver_items[i]) {
continue;
}
else {
return -1;
}
}
}
catch (...) {
return -1;
}
}
return -1;
}
static std::string convert_studio_language_to_api(std::string lang_code)
{
boost::replace_all(lang_code, "_", "-");
return lang_code;
/*if (lang_code == "zh_CN")
return "zh-hans";
else if (lang_code == "zh_TW")
return "zh-hant";
else
return "en";*/
}
#ifdef _WIN32
bool is_associate_files(std::wstring extend)
{
wchar_t app_path[MAX_PATH];
::GetModuleFileNameW(nullptr, app_path, sizeof(app_path));
std::wstring prog_id = L" Orca.Slicer.1";
std::wstring reg_base = L"Software\\Classes";
std::wstring reg_extension = reg_base + L"\\." + extend;
wchar_t szValueCurrent[1000];
DWORD dwType;
DWORD dwSize = sizeof(szValueCurrent);
int iRC = ::RegGetValueW(HKEY_CURRENT_USER, reg_extension.c_str(), nullptr, RRF_RT_ANY, &dwType, szValueCurrent, &dwSize);
bool bDidntExist = iRC == ERROR_FILE_NOT_FOUND;
if (!bDidntExist && ::wcscmp(szValueCurrent, prog_id.c_str()) == 0)
return true;
return false;
}
#endif
class SplashScreen : public wxSplashScreen
{
public:
SplashScreen(const wxBitmap& bitmap, long splashStyle, int milliseconds, wxPoint pos = wxDefaultPosition)
: wxSplashScreen(bitmap, splashStyle, milliseconds, static_cast<wxWindow*>(wxGetApp().mainframe), wxID_ANY, wxDefaultPosition, wxDefaultSize,
#ifdef __APPLE__
wxBORDER_NONE | wxFRAME_NO_TASKBAR | wxSTAY_ON_TOP
#else
wxBORDER_NONE | wxFRAME_NO_TASKBAR
#endif // !__APPLE__
)
{
int init_dpi = get_dpi_for_window(this);
this->SetPosition(pos);
this->CenterOnScreen();
int new_dpi = get_dpi_for_window(this);
m_scale = (float)(new_dpi) / (float)(init_dpi);
m_main_bitmap = bitmap;
scale_bitmap(m_main_bitmap, m_scale);
// init constant texts and scale fonts
m_constant_text.init(Label::Body_16);
// ORCA scale all fonts with monitor scale
scale_font(m_constant_text.version_font, m_scale * 2);
scale_font(m_constant_text.based_on_font, m_scale * 1.5f);
scale_font(m_constant_text.credits_font, m_scale * 2);
// this font will be used for the action string
m_action_font = m_constant_text.credits_font;
// draw logo and constant info text
Decorate(m_main_bitmap);
wxGetApp().UpdateFrameDarkUI(this);
}
void SetText(const wxString& text)
{
set_bitmap(m_main_bitmap);
if (!text.empty()) {
wxBitmap bitmap(m_main_bitmap);
wxMemoryDC memDC;
memDC.SelectObject(bitmap);
memDC.SetFont(m_action_font);
memDC.SetTextForeground(StateColor::darkModeColorFor(wxColour(144, 144, 144)));
int width = bitmap.GetWidth();
//int text_height = memDC.GetTextExtent(text).GetHeight();
//int text_width = memDC.GetTextExtent(text).GetWidth();
//wxRect text_rect(wxPoint(0, m_action_line_y_position), wxPoint(width, m_action_line_y_position + text_height));
//memDC.DrawLabel(text, text_rect, wxALIGN_CENTER);
memDC.SelectObject(wxNullBitmap);
set_bitmap(bitmap);
#ifdef __WXOSX__
// without this code splash screen wouldn't be updated under OSX
wxYield();
#endif
}
}
// Split string by characters
std::vector<wxString> SplitStringByChar(const wxString& text)
{
std::vector<wxString> chars;
for (size_t i = 0; i < text.length(); ++i)
{
chars.push_back(text.Mid(i, 1));
}
return chars;
}
// Draw centered and wrapped text
void DrawCenteredWrappedText(wxGraphicsContext* gc, const wxString& text, const wxRect& rect, const wxFont& font, const wxColour& color)
{
//Set font and color
gc->SetFont(font, color);
// split string into characters
std::vector<wxString> chars = SplitStringByChar(text);
//Calculate the text of each line
wxString currentLine;
std::vector<wxString> lines;
wxDouble charWidth, charHeight;
gc->GetTextExtent(" ", &charWidth, &charHeight);
charHeight += 2;
for (const auto& ch : chars)
{
wxDouble currentLineWidth, currentLineHeight;
gc->GetTextExtent(currentLine, ¤tLineWidth, ¤tLineHeight);
wxDouble chWidth, chHeight;
gc->GetTextExtent(ch, &chWidth, &chHeight);
if (currentLineWidth + chWidth > rect.width)
{
lines.push_back(currentLine);
currentLine.clear();
}
currentLine += ch;
}
if (!currentLine.empty())
{
lines.push_back(currentLine);
}
wxDouble totalHeight = lines.size() * charHeight;
wxDouble y = rect.y;
//Draw each line of text
for (const auto& line : lines)
{
wxDouble lineWidth, lineHeight;
gc->GetTextExtent(line, &lineWidth, &lineHeight);
wxDouble x = rect.x + (rect.width - lineWidth) / 2;
gc->DrawText(line, x, y);
y += lineHeight;
}
}
void Decorate(wxBitmap& bmp)
{
if (!bmp.IsOk())
return;
bool is_dark = wxGetApp().app_config->get("dark_color_mode") == "1";
// use a memory DC to draw directly onto the bitmap
wxMemoryDC memDc(bmp);
int width = bmp.GetWidth();
int height = bmp.GetHeight();
// Logo
BitmapCache bmp_cache;
// wxBitmap logo_bmp = *bmp_cache.load_svg(is_dark ? "splash_logo_dark" : "splash_logo", width, height); // use with full width & height
wxBitmap logo_bmp = *bmp_cache.load_png("splash_logo", width, height);
memDc.DrawBitmap(logo_bmp, 0, 0, true);
// Create a graphics context from the memory DC
wxGraphicsContext* gc = wxGraphicsContext::Create(memDc);
if(!gc)
return;
gc->SetAntialiasMode(wxANTIALIAS_DEFAULT);
// const wxColor font_color(184, 188, 187);
const wxColor font_color(100, 100, 100);
if (Slic3r::CxBuildInfo::isCusotmized()) {
delete gc;
return ;
}
// Official brief introduction
wxString official_brief_introduction = m_loading_info + "\n\n" + _L("Official brief introduction");
wxRect rect(FromDIP(32), height * 0.50, FromDIP(210), FromDIP(100));
wxFont font(11, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
wxColour color(0, 0, 0);
DrawCenteredWrappedText(gc, official_brief_introduction, rect, font, font_color);
// Version
//gc->SetFont(m_constant_text.version_font, font_color);
wxString version_text = _L("Version:") + m_constant_text.version;
wxDouble text_width, text_height;
gc->GetTextExtent(version_text, &text_width, &text_height);
wxRect version_rect(FromDIP(32), height * 0.90, FromDIP(210), text_height);
DrawCenteredWrappedText(gc, version_text, version_rect, Label::Body_13, font_color);
// Clean up the graphics context
delete gc;
// Dynamic Text
// m_action_line_y_position = int(height * 0.83);
// // Based on Text
// memDc.SetFont(m_constant_text.based_on_font);
// auto bs_version = wxString::Format("Based on OrcaaSlicer").ToStdString();
// wxSize based_on_ext = memDc.GetTextExtent(bs_version);
// wxRect based_on_rect(
// wxPoint(0, height - based_on_ext.GetHeight() * 2),
// wxPoint(width, height - based_on_ext.GetHeight())
// );
// memDc.DrawLabel(bs_version, based_on_rect, wxALIGN_CENTER);
}
static wxBitmap MakeBitmap()
{
int width = FromDIP(860, nullptr);
int height = FromDIP(480, nullptr);
wxImage image(width, height, true);
image.InitAlpha();
unsigned char* alpha = image.GetAlpha();
memset(alpha, 0, width * height);
wxBitmap new_bmp(image);
wxMemoryDC memDC;
memDC.SelectObject(new_bmp);
// memDC.SetBrush(StateColor::darkModeColorFor(*wxWHITE));
memDC.SetBrush(*wxTRANSPARENT_BRUSH);
memDC.SetPen(*wxTRANSPARENT_PEN);
memDC.DrawRectangle(-1, -1, width + 2, height + 2);
memDC.DrawBitmap(new_bmp, 0, 0, true);
memDC.SelectObject(wxNullBitmap);
return new_bmp;
}
void set_bitmap(wxBitmap& bmp)
{
m_window->SetBitmap(bmp);
m_window->Refresh();
m_window->Update();
}
void scale_bitmap(wxBitmap& bmp, float scale)
{
if (scale == 1.0)
return;
wxImage image = bmp.ConvertToImage();
if (!image.IsOk() || image.GetWidth() == 0 || image.GetHeight() == 0)
return;
int width = int(scale * image.GetWidth());
int height = int(scale * image.GetHeight());
image.Rescale(width, height, wxIMAGE_QUALITY_BILINEAR);
bmp = wxBitmap(std::move(image));
}
void scale_font(wxFont& font, float scale)
{
#ifdef __WXMSW__
// Workaround for the font scaling in respect to the current active display,
// not for the primary display, as it's implemented in Font.cpp
// See https://github.com/wxWidgets/wxWidgets/blob/master/src/msw/font.cpp
// void wxNativeFontInfo::SetFractionalPointSize(float pointSizeNew)
wxNativeFontInfo nfi= *font.GetNativeFontInfo();
float pointSizeNew = scale * font.GetPointSize();
nfi.lf.lfHeight = nfi.GetLogFontHeightAtPPI(pointSizeNew, get_dpi_for_window(this));
nfi.pointSize = pointSizeNew;
font = wxFont(nfi);
#else
font.Scale(scale);
#endif //__WXMSW__
}
private:
wxStaticText* m_staticText_slicer_name;
wxStaticText* m_staticText_slicer_version;
wxStaticBitmap* m_bitmap;
wxStaticText* m_staticText_loading;
wxString m_loading_info = _L("Loading configuration") + dots;
wxBitmap m_main_bitmap;
wxFont m_action_font;
float m_scale {1.0};
struct ConstantText
{
wxString title;
wxString version;
wxString credits;
wxFont title_font;
wxFont version_font;
wxFont credits_font;
wxFont based_on_font;
void init(wxFont init_font)
{
// title
//title = wxGetApp().is_editor() ? SLIC3R_APP_FULL_NAME : GCODEVIEWER_APP_NAME;
// dynamically get the version to display
version = GUI_App::format_display_version();
// credits infornation
credits = "";
//title_font = Label::Head_16;
version_font = Label::Body_13;
based_on_font = Label::Body_8;
credits_font = Label::Body_8;
}
}
m_constant_text;
};
#ifdef __linux__
bool static check_old_linux_datadir(const wxString& app_name) {
// If we are on Linux and the datadir does not exist yet, look into the old
// location where the datadir was before version 2.3. If we find it there,
// tell the user that he might wanna migrate to the new location.
// (https://github.com/prusa3d/PrusaSlicer/issues/2911)
// To be precise, the datadir should exist, it is created when single instance
// lock happens. Instead of checking for existence, check the contents.
namespace fs = boost::filesystem;
std::string new_path = Slic3r::data_dir();
wxString dir;
if (! wxGetEnv(wxS("XDG_CONFIG_HOME"), &dir) || dir.empty() )
dir = wxFileName::GetHomeDir() + wxS("/.config");
std::string default_path = (dir + "/" + app_name).ToUTF8().data();
if (new_path != default_path) {
// This happens when the user specifies a custom --datadir.
// Do not show anything in that case.
return true;
}
fs::path data_dir = fs::path(new_path);
if (! fs::is_directory(data_dir))
return true; // This should not happen.
int file_count = std::distance(fs::directory_iterator(data_dir), fs::directory_iterator());
if (file_count <= 1) { // just cache dir with an instance lock
// BBS
} else {
// If the new directory exists, be silent. The user likely already saw the message.
}
return true;
}
#endif
struct FileWildcards {
std::string_view title;
std::vector<std::string_view> file_extensions;
};
static const FileWildcards file_wildcards_by_type[FT_SIZE] = {
/* FT_STEP */ { "STEP files"sv, { ".stp"sv, ".step"sv } },
/* FT_STL */ { "STL files"sv, { ".stl"sv } },
/* FT_OBJ */ { "OBJ files"sv, { ".obj"sv } },
/* FT_AMF */ { "AMF files"sv, { ".amf"sv, ".zip.amf"sv, ".xml"sv } },
/* FT_3MF */ { "3MF files"sv, { ".3mf"sv } },
/* FT_GCODE */ { "G-code files"sv, { ".gcode"sv, ".3mf"sv } },
/* FT_GCODE_3MF */ { "G-code 3MF files"sv, { ".3mf"sv } },
#ifdef __APPLE__
/* FT_MODEL */
{"All"sv, {".3mf"sv, ".stl"sv, ".oltp"sv, ".stp"sv, ".step"sv, ".svg"sv, ".amf"sv, ".obj"sv, ".usd"sv, ".usda"sv, ".usdc"sv, ".usdz"sv, ".abc"sv, ".ply"sv,".dae"sv,".3ds"sv,".off"sv}},
#else
/* FT_MODEL */
{"All"sv, {".3mf"sv, ".stl"sv, ".oltp"sv, ".stp"sv, ".step"sv, ".svg"sv, ".amf"sv, ".obj"sv, ".dae"sv, ".3ds"sv, ".ply"sv, ".off"sv}},
#endif
/* FT_ZIP */ { "ZIP files"sv, { ".zip"sv } },
/* FT_PROJECT */ { "Project files"sv, { ".3mf"sv, ".cxprj"sv } },
/* FT_GALLERY */ { "Known files"sv, { ".stl"sv, ".obj"sv } },
/* FT_INI */ { "INI files"sv, { ".ini"sv } },
/* FT_SVG */ { "SVG files"sv, { ".svg"sv } },
/* FT_TEX */ { "Texture"sv, { ".png"sv, ".svg"sv } },
/* FT_SL1 */ { "Masked SLA files"sv, { ".sl1"sv, ".sl1s"sv } },
/* FT_CXPRJ */ { "Cxprj files"sv, { ".cxprj"sv } },
/* FT_ONLY_GCODE */ { "G-code files"sv, { ".gcode"sv } },
/* FT_MESH_FILE */ { "Mesh files"sv, { ".stl"sv, ".obj"sv, ".dae"sv, ".3ds"sv, ".off"sv, ".ply"sv, ".3mf"sv } },
/* FT_CAD_FILE */ { "CAD files"sv, { ".stp"sv, ".step"sv } },
};
// This function produces a Win32 file dialog file template mask to be consumed by wxWidgets on all platforms.
// The function accepts a custom extension parameter. If the parameter is provided, the custom extension
// will be added as a fist to the list. This is important for a "file save" dialog on OSX, which strips
// an extension from the provided initial file name and substitutes it with the default extension (the first one in the template).
wxString file_wildcards(FileType file_type, const std::string &custom_extension)
{
const FileWildcards& data = file_wildcards_by_type[file_type];
std::string title;
std::string mask;
std::string custom_ext_lower;
if (! custom_extension.empty()) {
// Generate an extension into the title mask and into the list of extensions.
custom_ext_lower = boost::to_lower_copy(custom_extension);
const std::string custom_ext_upper = boost::to_upper_copy(custom_extension);
if (custom_ext_lower == custom_extension) {
// Add a lower case version.
title = std::string("*") + custom_ext_lower;
mask = title;
// Add an upper case version.
mask += ";*";
mask += custom_ext_upper;
} else if (custom_ext_upper == custom_extension) {
// Add an upper case version.
title = std::string("*") + custom_ext_upper;
mask = title;
// Add a lower case version.
mask += ";*";
mask += custom_ext_lower;
} else {
// Add the mixed case version only.
title = std::string("*") + custom_extension;
mask = title;
}
}
for (const std::string_view &ext : data.file_extensions)
// Only add an extension if it was not added first as the custom extension.
if (ext != custom_ext_lower) {
if (title.empty()) {
title = "*";
title += ext;
mask = title;
} else {
title += ", *";
title += ext;
mask += ";*";
mask += ext;
}
mask += ";*";
mask += boost::to_upper_copy(std::string(ext));
}
return GUI::format_wxstr("%s (%s)|%s", data.title, title, mask);
}
static std::string libslic3r_translate_callback(const char *s) { return wxGetTranslation(wxString(s, wxConvUTF8)).utf8_str().data(); }
#ifdef WIN32
#if !wxVERSION_EQUAL_OR_GREATER_THAN(3,1,3)
static void register_win32_dpi_event()
{
enum { WM_DPICHANGED_ = 0x02e0 };
wxWindow::MSWRegisterMessageHandler(WM_DPICHANGED_, [](wxWindow *win, WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) {
const int dpi = wParam & 0xffff;
const auto rect = reinterpret_cast<PRECT>(lParam);
const wxRect wxrect(wxPoint(rect->top, rect->left), wxPoint(rect->bottom, rect->right));
DpiChangedEvent evt(EVT_DPI_CHANGED_SLICER, dpi, wxrect);
win->GetEventHandler()->AddPendingEvent(evt);
return true;
});
}
#endif // !wxVERSION_EQUAL_OR_GREATER_THAN
static GUID GUID_DEVINTERFACE_HID = { 0x4D1E55B2, 0xF16F, 0x11CF, 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 };
static void register_win32_device_notification_event()
{
wxWindow::MSWRegisterMessageHandler(WM_DEVICECHANGE, [](wxWindow *win, WXUINT /* nMsg */, WXWPARAM wParam, WXLPARAM lParam) {
// 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.
auto main_frame = dynamic_cast<MainFrame*>(win);
auto plater = (main_frame == nullptr) ? nullptr : main_frame->plater();
if (plater == nullptr)
// Maybe some other top level window like a dialog or maybe a pop-up menu?
return true;
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
switch (wParam) {
case DBT_DEVICEARRIVAL:
if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
plater->GetEventHandler()->AddPendingEvent(VolumeAttachedEvent(EVT_VOLUME_ATTACHED));
else if (lpdb->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
PDEV_BROADCAST_DEVICEINTERFACE lpdbi = (PDEV_BROADCAST_DEVICEINTERFACE)lpdb;
// if (lpdbi->dbcc_classguid == GUID_DEVINTERFACE_VOLUME) {
// printf("DBT_DEVICEARRIVAL %d - Media has arrived: %ws\n", msg_count, lpdbi->dbcc_name);
if (lpdbi->dbcc_classguid == GUID_DEVINTERFACE_HID)
plater->GetEventHandler()->AddPendingEvent(HIDDeviceAttachedEvent(EVT_HID_DEVICE_ATTACHED, boost::nowide::narrow(lpdbi->dbcc_name)));
}
break;
case DBT_DEVICEREMOVECOMPLETE:
if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
plater->GetEventHandler()->AddPendingEvent(VolumeDetachedEvent(EVT_VOLUME_DETACHED));
else if (lpdb->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
PDEV_BROADCAST_DEVICEINTERFACE lpdbi = (PDEV_BROADCAST_DEVICEINTERFACE)lpdb;
// if (lpdbi->dbcc_classguid == GUID_DEVINTERFACE_VOLUME)
// printf("DBT_DEVICEARRIVAL %d - Media was removed: %ws\n", msg_count, lpdbi->dbcc_name);
if (lpdbi->dbcc_classguid == GUID_DEVINTERFACE_HID)
plater->GetEventHandler()->AddPendingEvent(HIDDeviceDetachedEvent(EVT_HID_DEVICE_DETACHED, boost::nowide::narrow(lpdbi->dbcc_name)));
}
break;
default:
break;
}
return true;
});
wxWindow::MSWRegisterMessageHandler(MainFrame::WM_USER_MEDIACHANGED, [](wxWindow *win, WXUINT /* nMsg */, WXWPARAM wParam, WXLPARAM lParam) {
// 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.
auto main_frame = dynamic_cast<MainFrame*>(win);
auto plater = (main_frame == nullptr) ? nullptr : main_frame->plater();
if (plater == nullptr)
// Maybe some other top level window like a dialog or maybe a pop-up menu?
return true;
wchar_t sPath[MAX_PATH];
if (lParam == SHCNE_MEDIAINSERTED || lParam == SHCNE_MEDIAREMOVED) {
struct _ITEMIDLIST* pidl = *reinterpret_cast<struct _ITEMIDLIST**>(wParam);
if (! SHGetPathFromIDList(pidl, sPath)) {
BOOST_LOG_TRIVIAL(error) << "MediaInserted: SHGetPathFromIDList failed";
return false;
}
}
switch (lParam) {
case SHCNE_MEDIAINSERTED:
{
//printf("SHCNE_MEDIAINSERTED %S\n", sPath);
plater->GetEventHandler()->AddPendingEvent(VolumeAttachedEvent(EVT_VOLUME_ATTACHED));
break;
}
case SHCNE_MEDIAREMOVED:
{
//printf("SHCNE_MEDIAREMOVED %S\n", sPath);
plater->GetEventHandler()->AddPendingEvent(VolumeDetachedEvent(EVT_VOLUME_DETACHED));
break;
}
default:
// printf("Unknown\n");
break;
}
return true;
});
wxWindow::MSWRegisterMessageHandler(WM_INPUT, [](wxWindow *win, WXUINT /* nMsg */, WXWPARAM wParam, WXLPARAM lParam) {
auto main_frame = dynamic_cast<MainFrame*>(Slic3r::GUI::find_toplevel_parent(win));
auto plater = (main_frame == nullptr) ? nullptr : main_frame->plater();
// if (wParam == RIM_INPUTSINK && plater != nullptr && main_frame->IsActive()) {
if (wParam == RIM_INPUT && plater != nullptr && main_frame->IsActive()) {
RAWINPUT raw;
UINT rawSize = sizeof(RAWINPUT);
::GetRawInputData((HRAWINPUT)lParam, RID_INPUT, &raw, &rawSize, sizeof(RAWINPUTHEADER));
if (raw.header.dwType == RIM_TYPEHID && plater->get_mouse3d_controller().handle_raw_input_win32(raw.data.hid.bRawData, raw.data.hid.dwSizeHid))
return true;
}
return false;
});
wxWindow::MSWRegisterMessageHandler(WM_COPYDATA, [](wxWindow* win, WXUINT /* nMsg */, WXWPARAM wParam, WXLPARAM lParam) {
COPYDATASTRUCT* copy_data_structure = { 0 };
copy_data_structure = (COPYDATASTRUCT*)lParam;
if (copy_data_structure->dwData == 1) {
LPCWSTR arguments = (LPCWSTR)copy_data_structure->lpData;
Slic3r::GUI::wxGetApp().other_instance_message_handler()->handle_message(boost::nowide::narrow(arguments));
}
return true;
});
}
#endif // WIN32
static void generic_exception_handle()
{
// Note: Some wxWidgets APIs use wxLogError() to report errors, eg. wxImage
// - see https://docs.wxwidgets.org/3.1/classwx_image.html#aa249e657259fe6518d68a5208b9043d0
//
// wxLogError typically goes around exception handling and display an error dialog some time
// after an error is logged even if exception handling and OnExceptionInMainLoop() take place.
// This is why we use wxLogError() here as well instead of a custom dialog, because it accumulates
// errors if multiple have been collected and displays just one error message for all of them.
// Otherwise we would get multiple error messages for one missing png, for example.
//
// If a custom error message window (or some other solution) were to be used, it would be necessary
// to turn off wxLogError() usage in wx APIs, most notably in wxImage
// - see https://docs.wxwidgets.org/trunk/classwx_image.html#aa32e5d3507cc0f8c3330135bc0befc6a
/*#ifdef WIN32
//LPEXCEPTION_POINTERS exception_pointers = nullptr;
__try {
throw;
}
__except (CBaseException::UnhandledExceptionFilter2(GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER) {
//__except (exception_pointers = GetExceptionInformation(), EXCEPTION_EXECUTE_HANDLER) {
// if (exception_pointers) {
// CBaseException::UnhandledExceptionFilter(exception_pointers);
// }
// else
throw;
}
#else*/
try {
throw;
} catch (const std::bad_alloc& ex) {
// bad_alloc in main thread is most likely fatal. Report immediately to the user (wxLogError would be delayed)
// and terminate the app so it is at least certain to happen now.
BOOST_LOG_TRIVIAL(error) << boost::format("std::bad_alloc exception: %1%") % ex.what();
flush_logs();
wxString errmsg = wxString::Format(_L("Creality3DSlicer will terminate because of running out of memory."
"It may be a bug. It will be appreciated if you report the issue to our team."));
wxMessageBox(errmsg + "\n\n" + wxString(ex.what()), _L("Fatal error"), wxOK | wxICON_ERROR);
std::terminate();
//throw;
} catch (const boost::io::bad_format_string& ex) {
BOOST_LOG_TRIVIAL(error) << boost::format("Uncaught exception: %1%") % ex.what();
flush_logs();
wxString errmsg = _L("Creality3DSlicer will terminate because of a localization error. "
"It will be appreciated if you report the specific scenario this issue happened.");
wxMessageBox(errmsg + "\n\n" + wxString(ex.what()), _L("Critical error"), wxOK | wxICON_ERROR);
std::terminate();
//throw;
} catch (const std::exception& ex) {
wxLogError(format_wxstr(_L("Creality3DSlicer got an unhandled exception: %1%"), ex.what()));
BOOST_LOG_TRIVIAL(error) << boost::format("Uncaught exception: %1%") % ex.what();
flush_logs();
throw;
}
//#endif
}
bool GUI_App::getExtraHeader(std::map<std::string, std::string>& mapHeader)
{
mapHeader = this->get_extra_header();
return true;
}
void GUI_App::toggle_show_gcode_window()
{
m_show_gcode_window = !m_show_gcode_window;
app_config->set_bool("show_gcode_window", m_show_gcode_window);
}
#ifdef _WIN32
static std::list<HWND> l_creality_print_hwnds;
static BOOL CALLBACK EnumWindowsProc(_In_ HWND hwnd, _In_ LPARAM lParam)
{
//checks for other instances of prusaslicer, if found brings it to front and return false to stop enumeration and quit this instance
//search is done by classname(wxWindowNR is wxwidgets thing, so probably not unique) and name in window upper panel
//other option would be do a mutex and check for its existence
//BOOST_LOG_TRIVIAL(error) << "ewp: version: " << l_version_wstring;
TCHAR wndText[1000];
TCHAR className[1000];
int err;
err = GetClassName(hwnd, className, 1000);
if (err == 0)
return true;
err = GetWindowText(hwnd, wndText, 1000);
if (err == 0)
return true;
std::wstring classNameString(className);
std::wstring wndTextString(wndText);
if (wndTextString.find(L"CrealityPrint") != std::wstring::npos && classNameString == L"wxWindowNR") {
//check if other instances has same instance hash
//if not it is not same version(binary) as this version
HANDLE handle = GetProp(hwnd, L"Instance_Hash_Minor");
uint64_t other_instance_hash = PtrToUint(handle);
uint64_t other_instance_hash_major;
uint64_t my_instance_hash = GUI::wxGetApp().get_instance_hash_int();
handle = GetProp(hwnd, L"Instance_Hash_Major");