1 //About.cpp
2 
3 #include "About.h"
4 
5 #include "ClientUI.h"
6 #include "CUIControls.h"
7 #include "../util/Directories.h"
8 #include "../util/i18n.h"
9 
10 #include <GG/GUI.h>
11 
12 #include <boost/optional.hpp>
13 #include <boost/filesystem/path.hpp>
14 #include <boost/filesystem/fstream.hpp>
15 
16 
17 namespace {
18 
ReadFile(const boost::filesystem::path & file_path)19 boost::optional<std::string> ReadFile(const boost::filesystem::path& file_path) {
20     boost::filesystem::ifstream fin(file_path);
21     if (!fin.is_open())
22         return boost::none;
23 
24     std::stringstream buffer;
25     buffer << fin.rdbuf();
26 
27     return buffer.str();
28 }
29 
30 }
31 
32 ////////////////////////////////////////////
33 //   About
34 ////////////////////////////////////////////
About()35 About::About():
36     CUIWnd(UserString("ABOUT_WINDOW_TITLE"), GG::X(80), GG::Y(130), GG::X(600), GG::Y(500),
37            GG::INTERACTIVE | GG::DRAGABLE | GG::MODAL)
38 {}
39 
CompleteConstruction()40 void About::CompleteConstruction() {
41     CUIWnd::CompleteConstruction();
42 
43     m_done = Wnd::Create<CUIButton>(UserString("DONE"));
44     m_license = Wnd::Create<CUIButton>(UserString("LICENSE"));
45     m_vision = Wnd::Create<CUIButton>(UserString("VISION"));
46     m_info = GG::Wnd::Create<CUIMultiEdit>(UserString("FREEORION_VISION"), GG::MULTI_WORDBREAK | GG::MULTI_READ_ONLY);
47     AttachChild(m_info);
48     AttachChild(m_vision);
49     AttachChild(m_license);
50     AttachChild(m_done);
51 
52     DoLayout();
53 
54     // Read in the copyright info from a file
55     // this is not GetResourceDir() / "COPYING" because if a mod or scenario is loaded
56     // that changes the settings directory, the copyright notice should be unchanged
57     m_license_str = ReadFile(GetRootDataDir() / "default" / "COPYING").value_or("");
58 
59     m_done->LeftClickedSignal.connect(boost::bind(&GG::Wnd::EndRun, this));
60     m_license->LeftClickedSignal.connect(boost::bind(&About::ShowLicense, this));
61     m_vision->LeftClickedSignal.connect(boost::bind(&About::ShowVision, this));
62 }
63 
KeyPress(GG::Key key,std::uint32_t key_code_point,GG::Flags<GG::ModKey> mod_keys)64 void About::KeyPress(GG::Key key, std::uint32_t key_code_point,
65                      GG::Flags<GG::ModKey> mod_keys)
66 {
67     if ((key == GG::GGK_RETURN) || (key == GG::GGK_ESCAPE))
68         EndRun();
69 }
70 
ShowLicense()71 void About::ShowLicense()
72 { m_info->SetText(m_license_str); }
73 
ShowVision()74 void About::ShowVision()
75 { m_info->SetText(UserString("FREEORION_VISION")); }
76 
DoLayout()77 void About::DoLayout() {
78     const GG::X BUTTONS_HORIZONTAL_SPACING(5);
79     const GG::Y CONTENT_GROUPS_VERTICAL_SPACING(5);
80     const GG::Pt BORDERS_SIZE { GG::X {5}, GG::Y {5} };
81     const GG::Pt BUTTON_SIZE {
82         std::max({ m_vision->MinUsableSize().x, m_license->MinUsableSize().x, m_done->MinUsableSize().x }),
83         std::max({ m_vision->MinUsableSize().y, m_license->MinUsableSize().y, m_done->MinUsableSize().y }),
84     };
85 
86     auto const window_lr = ScreenToClient(ClientLowerRight());
87     auto const content_lr = window_lr - BORDERS_SIZE;
88     auto const content_ul = BORDERS_SIZE;
89 
90     GG::Pt draw_point = content_lr;
91 
92     for (auto& button : { m_done, m_vision, m_license }) {
93         GG::Pt button_ul = draw_point - BUTTON_SIZE;
94         button->SizeMove(button_ul, draw_point);
95 
96         draw_point.x -= BUTTON_SIZE.x + BUTTONS_HORIZONTAL_SPACING;
97     }
98 
99     draw_point.x = content_lr.x;
100     draw_point.y -= BUTTON_SIZE.y + CONTENT_GROUPS_VERTICAL_SPACING;
101 
102     m_info->SizeMove(content_ul, draw_point);
103 }
104