-
Notifications
You must be signed in to change notification settings - Fork 1
/
startpage.cpp
944 lines (890 loc) · 38.9 KB
/
startpage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
///////////////////////////////////////////////////////////////////////////////
// Name: startpage.cpp
// Author: Blake Madden
// Copyright: (c) 2005-2023 Blake Madden
// License: 3-Clause BSD license
// SPDX-License-Identifier: BSD-3-Clause
///////////////////////////////////////////////////////////////////////////////
#include "startpage.h"
wxDEFINE_EVENT(wxEVT_STARTPAGE_CLICKED, wxCommandEvent);
//-------------------------------------------
wxStartPage::wxStartPage(wxWindow* parent, wxWindowID id /*= wxID_ANY*/,
const wxArrayString& mruFiles /*= wxArrayString{}*/,
const wxBitmapBundle& logo /*= wxBitmapBundle{}*/,
const wxString& productDescription /*= wxString{}*/)
: wxWindow(parent, id, wxDefaultPosition, wxDefaultSize,
wxFULL_REPAINT_ON_RESIZE, L"wxStartPage"),
m_logoFont(wxFontInfo(
wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT).
GetFractionalPointSize() * 1.5)),
m_logo(logo),
m_productDescription(productDescription)
{
// Size of an icon scaled to 32x32, with label padding above and below it.
// Note that Realise will adjust this later more intelligently.
m_buttonHeight = GetButtonSize().GetHeight() + (2 * GetLabelPaddingHeight());
m_buttonWidth = FromDIP(wxSize(200, 200)).GetWidth();
SetBackgroundStyle(wxBG_STYLE_CUSTOM);
SetMRUList(mruFiles);
if (wxSystemSettings::GetAppearance().IsDark())
{
SetButtonAreaBackgroundColor(wxColour(46, 46, 46));
SetMRUBackgroundColor(wxColour(31, 31, 31));
}
Bind(wxEVT_PAINT, &wxStartPage::OnPaintWindow, this);
Bind(wxEVT_MOTION, &wxStartPage::OnMouseChange, this);
Bind(wxEVT_LEFT_DOWN, &wxStartPage::OnMouseClick, this);
Bind(wxEVT_LEAVE_WINDOW, &wxStartPage::OnMouseLeave, this);
Bind(wxEVT_SIZE, &wxStartPage::OnResize, this);
}
//---------------------------------------------------
void wxStartPage::DrawHighlight(wxDC& dc, const wxRect rect,
const wxColour color) const
{
wxDCPenChanger pc(dc, *wxLIGHT_GREY_PEN);
if (m_style == wxStartPageStyle::wxStartPage3D)
{
// fill with the color
dc.GradientFillLinear(rect, color, color.ChangeLightness(140), wxSOUTH);
// create a shiny overlay
dc.GradientFillLinear(wxRect(rect.GetX(), rect.GetY(),
rect.GetWidth(), rect.GetHeight() * 0.25),
wxColour(255, 255, 255, 25),
wxColour(255, 255, 255, 125), wxSOUTH);
dc.DrawLine(rect.GetLeftTop(), rect.GetRightTop());
dc.DrawLine(rect.GetRightTop(), rect.GetRightBottom());
dc.DrawLine(rect.GetRightBottom(), rect.GetLeftBottom());
dc.DrawLine(rect.GetLeftBottom(), rect.GetLeftTop());
}
else
{
wxDCBrushChanger bc(dc, color);
dc.DrawRectangle(rect);
}
}
//---------------------------------------------------
void wxStartPage::SetMRUList(const wxArrayString& mruFiles)
{
m_fileButtons.clear();
m_fileButtons.resize(mruFiles.GetCount() + 1);
if (mruFiles.GetCount() == 0)
{ return; }
const auto simplifyFilePath = [](auto path)
{
path = wxFileName(path).GetPath();
// shorten standard user paths
path.Replace(
wxStandardPaths::Get().GetUserDir(wxStandardPathsBase::Dir::Dir_Documents),
_(L"Documents"));
path.Replace(
wxStandardPaths::Get().GetUserDir(wxStandardPathsBase::Dir::Dir_Desktop),
_(L"Desktop"));
path.Replace(
wxStandardPaths::Get().GetUserDir(wxStandardPathsBase::Dir::Dir_Pictures),
_(L"Pictures"));
path.Replace(
wxStandardPaths::Get().GetUserDir(wxStandardPathsBase::Dir::Dir_Videos),
_(L"Videos"));
path.Replace(
wxStandardPaths::Get().GetUserDir(wxStandardPathsBase::Dir::Dir_Music),
_(L"Music"));
path.Replace(
wxStandardPaths::Get().GetUserDir(wxStandardPathsBase::Dir::Dir_Downloads),
_(L"Downloads"));
// replace slashes with guillemets (makes it look fancier)
path.Replace(wxFileName::GetPathSeparator(), L" \x00BB ", true);
return path;
};
// load files that can be found
size_t buttonCount{ 0 };
std::vector<wxString> files;
for (const auto& file: mruFiles)
{
if (wxFileName::FileExists(file))
{ files.push_back(file); }
// no more than 9 items here, not enough real estate
if (buttonCount == MAX_FILE_BUTTONS)
{ break; }
}
// no files, so no need for file buttons or the clear all button
if (files.empty())
{
return;
}
std::sort(files.begin(), files.end(),
[](const auto& lhv, const auto& rhv)
{
const wxFileName fn1(lhv), fn2(rhv);
wxDateTime accessTime1, modTime1, createTime1,
accessTime2, modTime2, createTime2;
fn1.GetTimes(&accessTime1, &modTime1, &createTime1);
fn2.GetTimes(&accessTime2, &modTime2, &createTime2);
// going in reverse, most recently modified goes to the front
return modTime1 > modTime2;
});
// connect the file paths to the buttons in the MRU list
for (buttonCount = 0; buttonCount < files.size(); ++buttonCount)
{
m_fileButtons[buttonCount].m_id = ID_FILE_ID_START + buttonCount;
m_fileButtons[buttonCount].m_fullFilePath = files[buttonCount];
m_fileButtons[buttonCount].m_label = simplifyFilePath(files[buttonCount]);
}
m_fileButtons[buttonCount].m_id = START_PAGE_FILE_LIST_CLEAR;
m_fileButtons[buttonCount++].m_label = GetClearFileListLabel();
m_fileButtons.resize(buttonCount);
}
//---------------------------------------------------
wxString wxStartPage::FormatGreeting() const
{
if (m_greetingStyle == wxStartPageGreetingStyle::wxNoGreeting)
{ return wxString{}; }
else if (m_greetingStyle == wxStartPageGreetingStyle::wxCustomGreeting)
{ return m_customGreeting; }
else if (m_greetingStyle == wxStartPageGreetingStyle::wxDynamicGreetingWithUserName &&
m_userName.length())
{
const auto currentHour{ wxDateTime::Now().GetHour() };
return currentHour < 12 ?
wxString::Format(_(L"Good morning, %s"), m_userName) :
currentHour < 17 ?
wxString::Format(_(L"Good afternoon, %s"), m_userName) :
wxString::Format(_(L"Good evening, %s"), m_userName);
}
else
{
const auto currentHour{ wxDateTime::Now().GetHour() };
return currentHour < 12 ?
_(L"Good morning") :
currentHour < 17 ?
_(L"Good afternoon") :
_(L"Good evening");
}
}
//---------------------------------------------------
void wxStartPage::CalcMRUColumnHeaderHeight(wxDC& dc)
{
wxDCFontChanger fc(dc, dc.GetFont().Larger().Larger().Bold());
m_fileColumnHeaderHeight = dc.GetTextExtent(GetRecentLabel()).GetHeight() +
(2 * GetLabelPaddingHeight());
m_fileColumnHeaderHeight = dc.GetTextExtent(GetRecentLabel()).GetHeight() +
(2 * GetLabelPaddingHeight());
const auto greeting{ FormatGreeting() };
if (greeting.length())
{
m_fileColumnHeaderHeight += dc.GetTextExtent(greeting).GetHeight() +
(2 * GetLabelPaddingHeight());
}
}
//---------------------------------------------------
void wxStartPage::CalcButtonStart(wxDC& dc)
{
wxCoord appNameWidth{ 0 }, appNameHeight{ 0 },
appDescWidth{ 0 }, appDescHeight{ 0 };
{
wxDCFontChanger fc(dc, m_logoFont);
dc.GetTextExtent(wxTheApp->GetAppName(), &appNameWidth, &appNameHeight);
}
if (m_appHeaderStyle == wxStartPageAppHeaderStyle::wxStartPageNoHeader)
{ m_buttonsStart = GetTopBorder(); }
else
{
m_buttonsStart =
(m_appHeaderStyle == wxStartPageAppHeaderStyle::wxStartPageAppNameAndLogo) &&
m_logo.IsOk() ?
GetTopBorder() + (2 * GetLabelPaddingHeight()) +
std::max(appNameHeight, GetAppLogoSize().GetHeight()) :
GetTopBorder() + (2 * GetLabelPaddingHeight()) + appNameHeight;
if (m_productDescription.length())
{
dc.GetTextExtent(m_productDescription, &appDescWidth, &appDescHeight);
m_buttonsStart += appDescHeight + (2 * GetLabelPaddingHeight());
}
}
}
//---------------------------------------------------
void wxStartPage::OnResize(wxSizeEvent& WXUNUSED(event))
{
wxClientDC dc(this);
wxCoord appNameWidth{ 0 }, appNameHeight{ 0 },
appDescWidth{ 0 }, appDescHeight{ 0 };
{
wxDCFontChanger fc(dc, m_logoFont);
dc.GetTextExtent(wxTheApp->GetAppName(), &appNameWidth, &appNameHeight);
}
CalcButtonStart(dc);
CalcMRUColumnHeaderHeight(dc);
if (m_productDescription.length())
{
dc.GetTextExtent(m_productDescription, &appDescWidth, &appDescHeight);
m_buttonsStart += appDescHeight + (2 * GetLabelPaddingHeight());
}
// calculate how wide the buttons/top label need to be fit their content
const auto buttonIconSize = GetButtonSize();
m_buttonHeight = buttonIconSize.GetHeight() + (2 * GetLabelPaddingHeight());
{
m_buttonWidth = std::max(m_buttonWidth,
(m_logo.IsOk() ?
(appNameWidth + GetAppLogoSize().GetWidth()+(2*GetLabelPaddingWidth())) :
appNameWidth + (2*GetLabelPaddingWidth())));
wxDCFontChanger fc(dc, m_buttons.size() > MAX_BUTTONS_SMALL_SIZE ?
dc.GetFont() :
dc.GetFont().Larger());
wxCoord textWidth{ 0 }, textHeight{ 0 };
for (const auto& button : m_buttons)
{
dc.GetTextExtent(button.m_label, &textWidth, &textHeight);
m_buttonWidth =
std::max(m_buttonWidth,
textWidth +
(4 * GetLabelPaddingWidth()) + buttonIconSize.GetWidth());
m_buttonHeight = std::max(m_buttonHeight,
textHeight + (2 * GetLabelPaddingWidth()));
if (button.m_icon.IsOk())
{
m_buttonHeight =
std::max(m_buttonHeight,
buttonIconSize.GetHeight() +
(2 * GetLabelPaddingHeight()));
}
}
}
// initialize the buttons
for (size_t i = 0; i < m_buttons.size(); ++i)
{
m_buttons[i].m_id = ID_BUTTON_ID_START + i;
m_buttons[i].m_rect = wxRect(GetLeftBorder(),
m_buttonsStart + (i * m_buttonHeight),
m_buttonWidth,
m_buttonHeight);
}
// calculate MRU info
{
wxDCFontChanger fc(dc, dc.GetFont().Larger());
if (m_fileButtons.size())
{
auto line2TextSz = dc.GetTextExtent(m_fileButtons[0].m_label);
wxDCFontChanger fc2(dc, wxFont(dc.GetFont()).MakeLarger());
auto line1TextSz = dc.GetTextExtent(m_fileButtons[0].m_label);
// enough space for the text (label and path) height
// (or icon, whichever is larger) and some padding around it
m_mruButtonHeight =
std::max(line1TextSz.GetHeight() + line2TextSz.GetHeight(),
buttonIconSize.GetHeight()) +
(2*GetLabelPaddingHeight()) +
// line space between file name and path
(GetLabelPaddingHeight()/2);
}
}
}
//---------------------------------------------------
void wxStartPage::OnPaintWindow(wxPaintEvent& WXUNUSED(event))
{
wxAutoBufferedPaintDC adc(this);
adc.Clear();
wxGCDC dc(adc);
CalcButtonStart(dc);
CalcMRUColumnHeaderHeight(dc);
const wxColour buttonAreaFontColor =
BlackOrWhiteContrast(GetButtonAreaBackgroundColor());
const wxColour mruFontColor =
BlackOrWhiteContrast(GetMRUBackgroundColor());
const wxColour buttonAreaHoverColor =
ShadeOrTint(GetButtonAreaBackgroundColor());
const wxColour mruHoverColor =
ShadeOrTint(GetMRUBackgroundColor());
const wxColour buttonAreaHoverFontColor =
BlackOrWhiteContrast(buttonAreaHoverColor);
const wxColour mruFontHoverColor =
BlackOrWhiteContrast(mruHoverColor);
const wxColour mruSeparatorLineColor =
ShadeOrTint(GetMRUBackgroundColor());
// calculate the positions of the buttons in the files area
const wxRect filesArea = wxRect(m_buttonWidth + (GetLeftBorder() * 2),
0,
GetClientSize().GetWidth() - (m_buttonWidth + (GetLeftBorder() * 2)),
GetClientSize().GetHeight());
const wxRect buttonsArea =
wxRect(wxSize(GetClientSize().GetWidth() - filesArea.GetWidth(),
GetClientSize().GetHeight()));
const wxRect fileColumnHeader =
wxRect(filesArea.GetLeft(), 0,
filesArea.GetWidth(), m_fileColumnHeaderHeight);
wxRect greetingRect{ fileColumnHeader }, recentRect{ fileColumnHeader };
const auto greeting{ FormatGreeting() };
if (greeting.length())
{
greetingRect.SetHeight(fileColumnHeader.GetHeight()/2);
recentRect.SetTop(greetingRect.GetBottom());
recentRect.SetHeight(fileColumnHeader.GetHeight()/2);
}
else
{ greetingRect.SetSize(wxSize(0, 0)); }
if (GetMRUFileCount() > 0)
{
// update the rects for the file buttons
for (size_t i = 0; i < GetMRUFileCount(); ++i)
{
m_fileButtons[i].m_rect =
wxRect(filesArea.GetLeft() + FromDIP(1),
m_fileColumnHeaderHeight + (i * GetMRUButtonHeight()),
filesArea.GetWidth() - FromDIP(2),
GetMRUButtonHeight());
}
// the "clear file list" button
{
wxDCFontChanger fc(dc, wxFont(dc.GetFont()).MakeLarger());
const auto clearButtonSize = dc.GetTextExtent(GetClearFileListLabel());
m_fileButtons[GetMRUFileAndClearButtonCount() - 1].m_rect =
wxRect(filesArea.GetLeft() + FromDIP(1),
m_fileColumnHeaderHeight +
((GetMRUFileAndClearButtonCount() - 1) * GetMRUButtonHeight()),
clearButtonSize.GetWidth() + (GetLabelPaddingHeight() * 2),
clearButtonSize.GetHeight() + (GetLabelPaddingHeight() * 2));
}
}
// update the custom buttons' rects
for (size_t i = 0; i < m_buttons.size(); ++i)
{
m_buttons[i].m_rect = wxRect(GetLeftBorder(),
m_buttonsStart + (i * m_buttonHeight),
m_buttonWidth,
m_buttonHeight);
}
// fill the background
dc.SetBackground(GetButtonAreaBackgroundColor());
dc.Clear();
// draw the program logo
if (m_appHeaderStyle != wxStartPageAppHeaderStyle::wxStartPageNoHeader)
{
wxCoord appDescWidth{ 0 }, appDescHeight{ 0 };
if (m_productDescription.length())
{
dc.GetTextExtent(m_productDescription, &appDescWidth, &appDescHeight);
appDescHeight += (2 * GetLabelPaddingHeight());
}
wxDCTextColourChanger cc(dc, buttonAreaFontColor);
wxDCPenChanger pc(dc, buttonAreaFontColor);
wxCoord textWidth{ 0 }, textHeight{ 0 };
wxBitmap appLogo = m_logo.GetBitmap(GetAppLogoSize()).ConvertToImage();
if (m_appHeaderStyle == wxStartPageAppHeaderStyle::wxStartPageAppNameAndLogo &&
appLogo.IsOk())
{
dc.DrawBitmap(appLogo, GetLeftBorder(), GetTopBorder());
// draw with larger font
{
wxDCFontChanger fc(dc, m_logoFont);
dc.GetTextExtent(wxTheApp->GetAppName(), &textWidth, &textHeight);
dc.DrawText(wxTheApp->GetAppName(),
GetLeftBorder()+appLogo.GetWidth()+GetLabelPaddingWidth(),
GetTopBorder()+((appLogo.GetHeight()/2)-(textHeight/2)));
}
if (m_productDescription.length())
{
dc.DrawText(m_productDescription,
(GetLeftBorder()) + ((m_buttonWidth/2) - (appDescWidth/2)),
GetTopBorder() + std::max(appLogo.GetHeight(),textHeight) +
GetLabelPaddingHeight());
}
const auto lineY = GetTopBorder() +
std::max(appLogo.GetHeight(), textHeight) +
appDescHeight + GetLabelPaddingWidth();
dc.DrawLine(wxPoint((2*GetLeftBorder()),lineY),
wxPoint(m_buttonWidth, lineY));
}
else
{
// draw with larger font
{
wxDCFontChanger fc(dc, m_logoFont);
dc.GetTextExtent(wxTheApp->GetAppName(), &textWidth, &textHeight);
// centering looks better when there is no logo
dc.DrawText(wxTheApp->GetAppName(),
(buttonsArea.GetWidth() - textWidth) * 0.5,
GetTopBorder() + GetLabelPaddingHeight());
}
if (m_productDescription.length())
{
dc.DrawText(m_productDescription,
(GetLeftBorder())+((m_buttonWidth/2) - (appDescWidth/2)),
GetTopBorder() + textHeight + GetLabelPaddingHeight());
}
dc.DrawLine(wxPoint((2*GetLeftBorder()),
GetTopBorder() + textHeight +
GetLabelPaddingHeight() + appDescHeight),
wxPoint(m_buttonWidth,
GetTopBorder() + textHeight +
GetLabelPaddingHeight() + appDescHeight));
}
}
// draw the MRU files area
{
wxDCPenChanger pc(dc, *wxTRANSPARENT_PEN);
wxDCBrushChanger bc(dc, GetMRUBackgroundColor());
dc.DrawRectangle(filesArea);
// if areas have the same color, then draw a contrasting line between them
if (GetMRUBackgroundColor() == GetButtonAreaBackgroundColor())
{
wxDCPenChanger pc2(dc, ShadeOrTint(GetMRUBackgroundColor()));
dc.DrawLine(filesArea.GetTopLeft(), filesArea.GetBottomLeft());
}
}
// draw the greeting
{
wxDCFontChanger fc(dc, dc.GetFont().Larger().Larger().Bold());
wxDCTextColourChanger tcc(dc, mruFontColor);
wxDCPenChanger pc(dc, mruSeparatorLineColor);
dc.SetClippingRegion(greetingRect);
dc.DrawLabel(greeting,
wxRect(greetingRect).Deflate(GetLabelPaddingWidth()),
wxALIGN_LEFT);
dc.DestroyClippingRegion();
dc.DrawLine(greetingRect.GetLeftBottom(),
greetingRect.GetRightBottom());
}
// draw MRU column header
{
wxDCFontChanger fc(dc, dc.GetFont().Larger().Larger());
wxDCTextColourChanger tcc(dc, mruFontColor);
wxDCPenChanger pc(dc,
wxPen(wxPenInfo(mruSeparatorLineColor,
FromDIP(2)).Cap(wxPenCap::wxCAP_BUTT)));
dc.SetClippingRegion(recentRect);
dc.DrawLabel(GetRecentLabel(),
wxRect(recentRect).Deflate(GetLabelPaddingWidth()),
wxALIGN_CENTRE);
dc.DestroyClippingRegion();
auto midPoint = recentRect.GetLeftBottom();
midPoint.x += (recentRect.GetRightBottom().x -
recentRect.GetLeftBottom().x) / 2;
const wxSize recentTextSz{ dc.GetTextExtent(GetRecentLabel()) };
dc.DrawLine(midPoint -
wxSize((recentTextSz.GetWidth()/2), 0),
midPoint +
wxSize((recentTextSz.GetWidth()/2), 0));
}
const wxString currentToolTip = m_toolTip;
// highlight the active MRU file or custom button
if (m_activeButton != wxNOT_FOUND)
{
wxRect buttonBorderRect;
ActiveButtonType activeButton{ ActiveButtonType::CustomButton };
for (size_t i = 0; i < GetMRUFileAndClearButtonCount(); ++i)
{
if (m_fileButtons[i].IsOk() &&
m_activeButton == m_fileButtons[i].m_id)
{
// show either full path, the label, or nothing
// (if a button under the MRU list)
m_toolTip = (i == (GetMRUFileAndClearButtonCount() -1 )) ?
wxString{} :
m_fileButtons[i].m_fullFilePath.length() ?
m_fileButtons[i].m_fullFilePath : m_fileButtons[i].m_label;
buttonBorderRect = m_fileButtons[i].m_rect;
activeButton = (i == (GetMRUFileAndClearButtonCount() - 1)) ?
ActiveButtonType::FileActionButton :
ActiveButtonType::FileButton;
break;
}
}
for (const auto& button : m_buttons)
{
if (m_activeButton == button.m_id)
{
buttonBorderRect = button.m_rect;
m_toolTip.clear();
activeButton = ActiveButtonType::CustomButton;
break;
}
}
if (!buttonBorderRect.IsEmpty())
{
if (activeButton == ActiveButtonType::FileActionButton)
{
// highlight just the border so that it looks like a UI button
wxDCBrushChanger bdc(dc, *wxTRANSPARENT_BRUSH);
wxDCPenChanger pdc(dc,
wxPen(ShadeOrTint(GetMRUBackgroundColor(), 0.4), FromDIP(2)));
dc.DrawRectangle(buttonBorderRect);
}
else
{
DrawHighlight(dc, buttonBorderRect,
(activeButton == ActiveButtonType::CustomButton) ?
buttonAreaHoverColor : mruHoverColor);
}
}
}
else
{ m_toolTip.clear(); }
// don't use tooltips with GTK, they only appear randomly and cause painting issues
#ifndef __WXGTK__
if (currentToolTip != m_toolTip)
{ SetToolTip(m_toolTip); }
#endif
const auto formatFileDateTime = [](const auto& dt)
{
wxString dateStr, timeStr;
const auto currentTime{ wxDateTime::Now() };
const wxTimeSpan timeDiff = wxDateTime::Now().Subtract(dt);
if (timeDiff.GetHours() < 1)
{
if (timeDiff.GetMinutes() < 10)
{ dateStr = _(L"Just now"); }
else
{
dateStr = wxString::Format(_(L"%d minutes ago"),
timeDiff.GetMinutes());
}
}
else if (timeDiff.GetMinutes() < 90)
{ dateStr = _(L"1 hour ago"); }
else if (timeDiff.GetHours() <= 8)
{
dateStr = wxString::Format(_(L"%d hours ago"),
timeDiff.GetHours() +
// round the minutes
((timeDiff.GetMinutes() - (60 * timeDiff.GetHours())) < 30 ? 0 : 1));
}
// named day if modified sometime this week
else if (currentTime.GetYear() == dt.GetYear() &&
currentTime.GetMonth() == dt.GetMonth() &&
currentTime.GetWeekOfMonth() == dt.GetWeekOfMonth())
{
if (currentTime.GetDay() == dt.GetDay())
{ dateStr = _(L"Today at "); }
else if (currentTime.GetDay() - 1 == dt.GetDay())
{ dateStr = _(L"Yesterday at "); }
else
{
dateStr = wxString::Format(_(L"%s at "),
wxDateTime::GetWeekDayName(dt.GetWeekDay(),
wxDateTime::NameFlags::Name_Abbr));
}
// include time also, in the local clock format
wxString am, pm;
wxDateTime::GetAmPmStrings(&am, &pm);
if (am.length() && pm.length())
{ timeStr = dt.Format(L"%I:%M %p").MakeUpper(); }
else
{ timeStr = dt.Format(L"%H:%M"); }
}
else
{
// only show year if modified last year
const wxString dateFormatStr =
(wxDateTime::Now().GetYear() == dt.GetYear()) ?
L"%B %d " :
L"%B %d, %Y";
dateStr = dt.Format(dateFormatStr);
}
return dateStr + timeStr;
};
// file labels
{
// Get the widest file modified time label so that we can draw
// them ragged right. Also, get the longest file path to make
// sure the time and path don't overlap.
decltype(wxSize::x) filePathLabelWidth{ 0 };
decltype(wxSize::x) timeLabelWidth{ 0 };
{
wxDCFontChanger fc(dc, wxFont(dc.GetFont()));
for (size_t i = 0; i < GetMRUFileCount(); ++i)
{
if (m_fileButtons[i].IsOk())
{
const wxFileName fn(m_fileButtons[i].m_fullFilePath);
wxDateTime accessTime, modTime, createTime;
if (fn.FileExists() && fn.GetTimes(&accessTime, &modTime, &createTime))
{
const wxString modTimeStr =
formatFileDateTime(modTime);
const wxSize timeStringSize =
dc.GetTextExtent(modTimeStr);
if (timeLabelWidth < timeStringSize.GetWidth())
{ timeLabelWidth = timeStringSize.GetWidth(); }
}
const wxSize filePathStringSize =
dc.GetTextExtent(fn.GetPath());
if (filePathLabelWidth < filePathStringSize.GetWidth())
{ filePathLabelWidth = filePathStringSize.GetWidth(); }
}
}
}
// begin drawing them
wxBitmap fileIcon = m_logo.GetBitmap(FromDIP(wxSize(32, 32))).ConvertToImage();
for (size_t i = 0; i < GetMRUFileAndClearButtonCount(); ++i)
{
if (m_fileButtons[i].IsOk())
{
wxDCTextColourChanger tcc(dc,
m_activeButton == m_fileButtons[i].m_id ?
mruFontHoverColor : mruFontColor);
const wxRect fileLabelRect =
wxRect{ m_fileButtons[i].m_rect }.Deflate(GetLabelPaddingHeight());
dc.SetClippingRegion(m_fileButtons[i].m_rect);
// if the "clear file list" button
if (i == GetMRUFileAndClearButtonCount() - 1)
{
wxDCFontChanger fc(dc, wxFont(dc.GetFont()).MakeLarger());
dc.DrawLabel(m_fileButtons[i].m_label, fileLabelRect,
wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL);
}
else
{
// show the files
const wxFileName fn(m_fileButtons[i].m_fullFilePath);
if (fileIcon.IsOk())
{
dc.DrawBitmap(fileIcon,
wxPoint(fileLabelRect.GetLeft(),
fileLabelRect.GetTop() +
((fileLabelRect.GetHeight() - fileIcon.GetHeight()) / 2)
));
int nameHeight{ 0 };
// draw the filename
{
wxDCFontChanger fc(dc, wxFont(dc.GetFont()).MakeLarger());
nameHeight =
dc.GetTextExtent(fn.GetFullName()).GetHeight();
dc.DrawText(fn.GetFullName(),
wxPoint(fileLabelRect.GetLeft() +
GetLabelPaddingWidth() + fileIcon.GetWidth(),
fileLabelRect.GetTop()));
}
// draw the filepath
{
wxDCTextColourChanger cc(dc, mruFontColor);
dc.DrawText(
// truncate the path is necessary
(m_fileButtons[i].m_label.length() <= 75) ?
m_fileButtons[i].m_label :
(m_fileButtons[i].m_label.substr(0, 75) + L"..."),
wxPoint(fileLabelRect.GetLeft() +
GetLabelPaddingWidth() +
fileIcon.GetWidth(),
fileLabelRect.GetTop() + nameHeight +
(GetLabelPaddingHeight()/2)));
}
// draw the modified time off to the side
if ((fileIcon.GetWidth() +
GetLabelPaddingWidth() +
filePathLabelWidth + timeLabelWidth) <
fileLabelRect.GetWidth())
{
wxDateTime accessTime, modTime, createTime;
if (fn.FileExists() && fn.GetTimes(&accessTime, &modTime, &createTime))
{
const wxString modTimeStr =
formatFileDateTime(modTime);
const wxSize timeStringSize =
dc.GetTextExtent(modTimeStr);
dc.DrawText(modTimeStr,
fileLabelRect.GetRight() -
(timeLabelWidth+GetLabelPaddingHeight()),
fileLabelRect.GetTop() +
((fileLabelRect.GetHeight()/2) -
(timeStringSize.GetHeight()/2)));
}
}
}
// if not using an icon, then just keep it
// simple and draw the filename
else
{
dc.DrawLabel(fn.GetFullName(), fileLabelRect,
wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL);
}
// draw separator line, unless this button is highlighted
if (m_activeButton != m_fileButtons[i].m_id)
{
wxDCPenChanger pc(dc, mruSeparatorLineColor);
dc.DrawLine(m_fileButtons[i].m_rect.GetLeftBottom(),
m_fileButtons[i].m_rect.GetRightBottom());
}
}
dc.DestroyClippingRegion();
}
}
}
// draw the custom button labels
{
const auto buttonIconSize = GetButtonSize();
m_buttonHeight = buttonIconSize.GetHeight() + (2 * GetLabelPaddingHeight());
wxDCFontChanger fc(dc, m_buttons.size() > MAX_BUTTONS_SMALL_SIZE ?
dc.GetFont() :
dc.GetFont().Larger());
for (const auto& button : m_buttons)
{
if (button.IsOk())
{
wxDCTextColourChanger cc(dc,
m_activeButton == button.m_id ?
buttonAreaHoverFontColor : buttonAreaFontColor);
// draw it
dc.SetClippingRegion(button.m_rect);
dc.DrawLabel(button.m_label, button.m_icon.GetBitmap(buttonIconSize).ConvertToImage(),
wxRect(button.m_rect).Deflate(GetLabelPaddingWidth()));
dc.DestroyClippingRegion();
}
}
}
}
//---------------------------------------------------
void wxStartPage::OnMouseChange(wxMouseEvent& event)
{
// see which (if any) button was previously highlighted
wxRect previousRect, currentRect;
if (m_activeButton != wxNOT_FOUND)
{
for (const auto& button : m_buttons)
{
if (m_activeButton == button.m_id)
{
previousRect = button.m_rect;
break;
}
}
for (size_t i = 0; i < GetMRUFileAndClearButtonCount(); ++i)
{
if (m_activeButton == m_fileButtons[i].m_id)
{
previousRect = m_fileButtons[i].m_rect;
break;
}
}
}
const auto previouslyActiveButton{ m_activeButton };
m_activeButton = wxNOT_FOUND;
for (const auto& button : m_buttons)
{
if (button.IsOk() &&
button.m_rect.Contains(event.GetX(), event.GetY()) )
{
m_activeButton = button.m_id;
// if its the same active button from before, then don't bother refreshing
if (previouslyActiveButton == m_activeButton)
{ return; }
// ...otherwise, just refresh the current and previous (if applicable) highlighted areas
currentRect = button.m_rect;
wxRect refreshRect = previousRect.IsEmpty() ?
currentRect :
previousRect.Union(currentRect);
refreshRect.Inflate(GetLabelPaddingHeight());
Refresh(true, &refreshRect);
Update();
return;
}
}
for (size_t i = 0; i < GetMRUFileAndClearButtonCount(); ++i)
{
if (m_fileButtons[i].IsOk() &&
m_fileButtons[i].m_rect.Contains(event.GetX(), event.GetY()) )
{
m_activeButton = m_fileButtons[i].m_id;
if (previouslyActiveButton == m_activeButton)
{ return; }
currentRect = m_fileButtons[i].m_rect;
wxRect refreshRect = previousRect.IsEmpty() ?
currentRect :
previousRect.Union(currentRect);
refreshRect.Inflate(GetLabelPaddingHeight());
Refresh(true, &refreshRect);
Update();
return;
}
}
if (previousRect.IsEmpty())
{ previousRect = GetClientRect(); }
else
{ previousRect.Inflate(GetLabelPaddingHeight()); }
Refresh(true, &previousRect);
Update();
}
//---------------------------------------------------
void wxStartPage::OnMouseLeave(wxMouseEvent& WXUNUSED(event))
{
// see which (if any) button was previously highlighted
wxRect refreshRect;
if (m_activeButton != wxNOT_FOUND)
{
for (const auto& button : m_buttons)
{
if (m_activeButton == button.m_id)
{
refreshRect = button.m_rect;
break;
}
}
for (size_t i = 0; i < GetMRUFileAndClearButtonCount(); ++i)
{
if (m_activeButton == m_fileButtons[i].m_id)
{
refreshRect = m_fileButtons[i].m_rect;
break;
}
}
}
else
{ return; }
m_activeButton = wxNOT_FOUND;
refreshRect.Inflate(GetLabelPaddingHeight());
Refresh(true, &refreshRect);
Update();
}
//---------------------------------------------------
void wxStartPage::OnMouseClick(wxMouseEvent& event)
{
for (const auto& button : m_buttons)
{
if (button.IsOk() &&
button.m_rect.Contains(event.GetX(), event.GetY()) )
{
wxCommandEvent cevent(wxEVT_STARTPAGE_CLICKED, GetId());
cevent.SetId(button.m_id);
cevent.SetInt(button.m_id);
cevent.SetEventObject(this);
GetEventHandler()->ProcessEvent(cevent);
break;
}
}
for (size_t i = 0; i < GetMRUFileAndClearButtonCount(); ++i)
{
if (m_fileButtons[i].IsOk() &&
m_fileButtons[i].m_rect.Contains(event.GetX(), event.GetY()) )
{
// if clicking the clear all button
if (i == GetMRUFileAndClearButtonCount() - 1)
{
if (wxMessageBox(
_(L"Do you wish to clear the list of recent files?"),
_(L"Clear File List"), wxYES_NO | wxICON_QUESTION) == wxYES)
{
SetMRUList(wxArrayString{});
Refresh();
Update();
// give the caller a change to clear the file history
// from their doc manager and whatnot
wxCommandEvent cevent(wxEVT_STARTPAGE_CLICKED, GetId());
cevent.SetId(START_PAGE_FILE_LIST_CLEAR);
cevent.SetInt(START_PAGE_FILE_LIST_CLEAR);
cevent.SetEventObject(this);
GetEventHandler()->ProcessEvent(cevent);
break;
}
}
else
{
wxCommandEvent cevent(wxEVT_STARTPAGE_CLICKED, GetId());
// client calls IsFileId() on this ID to see
// if a file button was clicked
cevent.SetId(m_fileButtons[i].m_id);
cevent.SetInt(m_fileButtons[i].m_id);
// selected file path
cevent.SetString(m_fileButtons[i].m_fullFilePath);
cevent.SetEventObject(this);
GetEventHandler()->ProcessEvent(cevent);
break;
}
}
}
}