1 /* GG is a GUI for OpenGL.
2    Copyright (C) 2003-2008 T. Zachary Laine
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public License
6    as published by the Free Software Foundation; either version 2.1
7    of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with this library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA
18 
19    If you do not wish to comply with the terms of the LGPL please
20    contact the author as other terms are available for a fee.
21 
22    Zach Laine
23    whatwasthataddress@gmail.com */
24 
25 #include <GG/StyleFactory.h>
26 
27 #include <GG/Button.h>
28 #include <GG/dialogs/ColorDlg.h>
29 #include <GG/dialogs/FileDlg.h>
30 #include <GG/dialogs/ThreeButtonDlg.h>
31 #include <GG/DropDownList.h>
32 #include <GG/DynamicGraphic.h>
33 #include <GG/Edit.h>
34 #include <GG/GroupBox.h>
35 #include <GG/ListBox.h>
36 #include <GG/Menu.h>
37 #include <GG/MultiEdit.h>
38 #include <GG/Scroll.h>
39 #include <GG/Slider.h>
40 #include <GG/Spin.h>
41 #include <GG/StaticGraphic.h>
42 #include <GG/TabWnd.h>
43 #include <GG/TextControl.h>
44 
45 #include "DefaultFont.h"
46 
47 #include <memory>
48 
49 
50 using namespace GG;
51 
StyleFactory()52 StyleFactory::StyleFactory()
53 {}
54 
~StyleFactory()55 StyleFactory::~StyleFactory()
56 {}
57 
DefaultFont(unsigned int pts) const58 std::shared_ptr<Font> StyleFactory::DefaultFont(unsigned int pts/* = 12*/) const
59 {
60     if (GetFontManager().HasFont(DefaultFontName(), pts)) {
61         return GUI::GetGUI()->GetFont(DefaultFontName(), pts, std::vector<unsigned char>());
62     } else {
63         std::vector<unsigned char> bytes;
64         VeraTTFBytes(bytes);
65         return GUI::GetGUI()->GetFont(DefaultFontName(), pts, bytes);
66     }
67 }
68 
DefaultFont(unsigned int pts,const UnicodeCharset * first,const UnicodeCharset * last) const69 std::shared_ptr<Font> StyleFactory::DefaultFont(unsigned int pts,
70                                                 const UnicodeCharset* first,
71                                                 const UnicodeCharset* last) const
72 {
73     if (GetFontManager().HasFont(DefaultFontName(), pts, first, last)) {
74         return GUI::GetGUI()->GetFont(DefaultFontName(), pts, std::vector<unsigned char>(), first, last);
75     } else {
76         std::vector<unsigned char> bytes;
77         VeraTTFBytes(bytes);
78         return GUI::GetGUI()->GetFont(DefaultFontName(), pts, bytes, first, last);
79     }
80 }
81 
82 // Don't translate, just pass the key.
Translate(const std::string & key) const83 std::string StyleFactory::Translate(const std::string& key) const
84 { return key; }
85 
NewButton(const std::string & str,const std::shared_ptr<Font> & font,Clr color,Clr text_color,Flags<WndFlag> flags) const86 std::shared_ptr<Button> StyleFactory::NewButton(const std::string& str, const std::shared_ptr<Font>& font,
87                                                 Clr color, Clr text_color/* = CLR_BLACK*/,
88                                                 Flags<WndFlag> flags/* = INTERACTIVE*/) const
89 { return Wnd::Create<Button>(str, font, color, text_color, flags); }
90 
NewRadioButtonGroup(Orientation orientation) const91 std::shared_ptr<RadioButtonGroup> StyleFactory::NewRadioButtonGroup(Orientation orientation) const
92 { return Wnd::Create<RadioButtonGroup>(orientation); }
93 
NewDropDownList(size_t num_shown_elements,Clr color) const94 std::shared_ptr<DropDownList> StyleFactory::NewDropDownList(size_t num_shown_elements, Clr color) const
95 { return Wnd::Create<DropDownList>(num_shown_elements, color); }
96 
NewEdit(const std::string & str,const std::shared_ptr<Font> & font,Clr color,Clr text_color,Clr interior) const97 std::shared_ptr<Edit> StyleFactory::NewEdit(const std::string& str, const std::shared_ptr<Font>& font,
98                             Clr color, Clr text_color/* = CLR_BLACK*/, Clr interior/* = CLR_ZERO*/) const
99 { return Wnd::Create<Edit>(str, font, color, text_color, interior); }
100 
NewListBox(Clr color,Clr interior) const101 std::shared_ptr<ListBox> StyleFactory::NewListBox(Clr color, Clr interior/* = CLR_ZERO*/) const
102 { return Wnd::Create<ListBox>(color, interior); }
103 
NewScroll(Orientation orientation,Clr color,Clr interior) const104 std::shared_ptr<Scroll> StyleFactory::NewScroll(Orientation orientation, Clr color, Clr interior) const
105 { return Wnd::Create<Scroll>(orientation, color, interior); }
106 
NewIntSlider(int min,int max,Orientation orientation,Clr color,int tab_width,int line_width) const107 std::shared_ptr<Slider<int>> StyleFactory::NewIntSlider(int min, int max, Orientation orientation,
108                                         Clr color, int tab_width, int line_width/* = 5*/) const
109 { return Wnd::Create<Slider<int>>(min, max, orientation, color, tab_width, line_width, INTERACTIVE); }
110 
NewTextControl(const std::string & str,const std::shared_ptr<Font> & font,Clr color,Flags<TextFormat> format) const111 std::shared_ptr<TextControl> StyleFactory::NewTextControl(const std::string& str, const std::shared_ptr<Font>& font,
112                                           Clr color/* = CLR_BLACK*/, Flags<TextFormat> format/* = FORMAT_NONE*/) const
113 { return Wnd::Create<TextControl>(X0, Y0, X1, Y1, str, font, color, format, NO_WND_FLAGS); }
114 
NewTabBar(const std::shared_ptr<Font> & font,Clr color,Clr text_color) const115 std::shared_ptr<TabBar> StyleFactory::NewTabBar(const std::shared_ptr<Font>& font, Clr color, Clr text_color/* = CLR_BLACK*/) const
116 { return Wnd::Create<TabBar>(font, color, text_color, INTERACTIVE); }
117 
NewDropDownListListBox(Clr color,Clr interior) const118 std::shared_ptr<ListBox> StyleFactory::NewDropDownListListBox(Clr color, Clr interior/* = CLR_ZERO*/) const
119 {
120     auto lb = NewListBox(color, interior);
121     // Because the rows of DropDownLists must be the same size, there's
122     // no need to worry that the bottom entry will get cut off if the
123     // scrollbar ends exactly at the list's end.
124     lb->AddPaddingAtEnd(false);
125     return lb;
126 }
127 
NewListBoxVScroll(Clr color,Clr interior) const128 std::shared_ptr<Scroll> StyleFactory::NewListBoxVScroll(Clr color, Clr interior) const
129 { return NewScroll(VERTICAL, color, interior); }
130 
NewListBoxHScroll(Clr color,Clr interior) const131 std::shared_ptr<Scroll> StyleFactory::NewListBoxHScroll(Clr color, Clr interior) const
132 { return NewScroll(HORIZONTAL, color, interior); }
133 
NewMultiEditVScroll(Clr color,Clr interior) const134 std::shared_ptr<Scroll> StyleFactory::NewMultiEditVScroll(Clr color, Clr interior) const
135 { return NewScroll(VERTICAL, color, interior); }
136 
NewMultiEditHScroll(Clr color,Clr interior) const137 std::shared_ptr<Scroll> StyleFactory::NewMultiEditHScroll(Clr color, Clr interior) const
138 { return NewScroll(HORIZONTAL, color, interior); }
139 
NewScrollUpButton(Clr color) const140 std::shared_ptr<Button> StyleFactory::NewScrollUpButton(Clr color) const
141 { return NewButton("", nullptr, color, CLR_BLACK, INTERACTIVE | REPEAT_BUTTON_DOWN); }
142 
NewScrollDownButton(Clr color) const143 std::shared_ptr<Button> StyleFactory::NewScrollDownButton(Clr color) const
144 { return NewButton("", nullptr, color, CLR_BLACK, INTERACTIVE | REPEAT_BUTTON_DOWN); }
145 
NewVScrollTabButton(Clr color) const146 std::shared_ptr<Button> StyleFactory::NewVScrollTabButton(Clr color) const
147 { return NewButton("", nullptr, color, CLR_BLACK, INTERACTIVE); }
148 
NewScrollLeftButton(Clr color) const149 std::shared_ptr<Button> StyleFactory::NewScrollLeftButton(Clr color) const
150 { return NewButton("", nullptr, color, CLR_BLACK, INTERACTIVE | REPEAT_BUTTON_DOWN); }
151 
NewScrollRightButton(Clr color) const152 std::shared_ptr<Button> StyleFactory::NewScrollRightButton(Clr color) const
153 { return NewButton("", nullptr, color, CLR_BLACK, INTERACTIVE | REPEAT_BUTTON_DOWN); }
154 
NewHScrollTabButton(Clr color) const155 std::shared_ptr<Button> StyleFactory::NewHScrollTabButton(Clr color) const
156 { return NewButton("", nullptr, color, CLR_BLACK, INTERACTIVE); }
157 
NewVSliderTabButton(Clr color) const158 std::shared_ptr<Button> StyleFactory::NewVSliderTabButton(Clr color) const
159 { return NewButton("", nullptr, color, CLR_BLACK, INTERACTIVE); }
160 
NewHSliderTabButton(Clr color) const161 std::shared_ptr<Button> StyleFactory::NewHSliderTabButton(Clr color) const
162 { return NewButton("", nullptr, color, CLR_BLACK, INTERACTIVE); }
163 
NewSpinIncrButton(const std::shared_ptr<Font> & font,Clr color) const164 std::shared_ptr<Button> StyleFactory::NewSpinIncrButton(const std::shared_ptr<Font>& font, Clr color) const
165 { return NewButton("+", font, color, CLR_BLACK, INTERACTIVE | REPEAT_BUTTON_DOWN); }
166 
NewSpinDecrButton(const std::shared_ptr<Font> & font,Clr color) const167 std::shared_ptr<Button> StyleFactory::NewSpinDecrButton(const std::shared_ptr<Font>& font, Clr color) const
168 { return NewButton("-", font, color, CLR_BLACK, INTERACTIVE | REPEAT_BUTTON_DOWN); }
169 
NewSpinEdit(const std::string & str,const std::shared_ptr<Font> & font,Clr color,Clr text_color,Clr interior) const170 std::shared_ptr<Edit> StyleFactory::NewSpinEdit(const std::string& str, const std::shared_ptr<Font>& font,
171                                 Clr color, Clr text_color/* = CLR_BLACK*/, Clr interior/* = CLR_ZERO*/) const
172 { return NewEdit(str, font, color, text_color, interior); }
173 
NewTabBarTab(const std::string & str,const std::shared_ptr<Font> & font,Flags<TextFormat> format,Clr color,Clr text_color) const174 std::shared_ptr<StateButton> StyleFactory::NewTabBarTab(const std::string& str,
175                                         const std::shared_ptr<Font>& font, Flags<TextFormat> format, Clr color,
176                                         Clr text_color/* = CLR_BLACK*/) const
177 {
178     auto retval = Wnd::Create<StateButton>(
179         str, font, format, color, std::make_shared<BeveledTabRepresenter>(), text_color);
180     retval->Resize(retval->MinUsableSize() + Pt(X(12), Y0));
181     return retval;
182 }
183 
NewTabBarLeftButton(const std::shared_ptr<Font> & font,Clr color,Clr text_color) const184 std::shared_ptr<Button> StyleFactory::NewTabBarLeftButton(
185     const std::shared_ptr<Font>& font, Clr color, Clr text_color/* = CLR_BLACK*/) const
186 { return NewButton("<", font, color, text_color, INTERACTIVE); }
187 
NewTabBarRightButton(const std::shared_ptr<Font> & font,Clr color,Clr text_color) const188 std::shared_ptr<Button> StyleFactory::NewTabBarRightButton(
189     const std::shared_ptr<Font>& font, Clr color, Clr text_color/* = CLR_BLACK*/) const
190 { return NewButton(">", font, color, text_color, INTERACTIVE); }
191 
NewThreeButtonDlg(X w,Y h,const std::string & msg,const std::shared_ptr<Font> & font,Clr color,Clr border_color,Clr button_color,Clr text_color,int buttons,const std::string & zero,const std::string & one,const std::string & two) const192 std::shared_ptr<ThreeButtonDlg> StyleFactory::NewThreeButtonDlg(
193     X w, Y h, const std::string& msg, const std::shared_ptr<Font>& font,
194     Clr color, Clr border_color, Clr button_color, Clr text_color,
195     int buttons, const std::string& zero/* = ""*/,
196     const std::string& one/* = ""*/, const std::string& two/* = ""*/) const
197 {
198     return Wnd::Create<ThreeButtonDlg>(w, h, msg, font, color, border_color, button_color, text_color,
199                                        buttons, zero, one, two);
200 }
201 
DefaultFontName()202 const std::string& StyleFactory::DefaultFontName()
203 {
204     static std::string retval = DEFAULT_FONT_NAME;
205     return retval;
206 }
207