1 /*
2 Copyright (C) 2009 Facundo Domínguez
3
4 This file is part of Spacejunk.
5
6 Spacejunk is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Foobar is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Foobar. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "gcnlayouts.h"
21
Layout()22 Layout::Layout() {
23 setOpaque(false);
24 }
25
calculateBB()26 void Layout::calculateBB() {
27 int w=0,h=0,x=0,y=0;
28 if (!mWidgets.empty()) {
29 WidgetList::iterator i=mWidgets.begin();
30 x=(*i)->getX();
31 y=(*i)->getY();
32 w=(*i)->getWidth();
33 h=(*i)->getHeight();
34 i++;
35 for (;i!=mWidgets.end();i++) {
36 if ((*i)->getX()<x) {
37 w+=x-(*i)->getX();
38 x=(*i)->getX();
39 }
40 if ((*i)->getY()<y) {
41 h+=y-(*i)->getY();
42 y=(*i)->getY();
43 }
44 if ((*i)->getX()+(*i)->getWidth()>x+w) w=(*i)->getX()+(*i)->getWidth()-x;
45 if ((*i)->getY()+(*i)->getHeight()>y+h) h=(*i)->getY()+(*i)->getHeight()-y;
46 }
47
48 for (i=mWidgets.begin();i!=mWidgets.end();i++)
49 (*i)->setPosition((*i)->getX()-x,(*i)->getY()-y);
50
51 }
52 setX(x);
53 setY(y);
54 setWidth(w);
55 setHeight(h);
56 }
fitWidgets()57 void Layout::fitWidgets() {
58 calculateBB();
59 }
60
CenterVerticalLayout(int spacing)61 CenterVerticalLayout::CenterVerticalLayout(int spacing)
62 : spacing(spacing) {};
63
calculateBB()64 void CenterVerticalLayout::calculateBB() {
65 int w=0,h=0;
66 if (!mWidgets.empty()) {
67 WidgetList::iterator i=mWidgets.begin();
68 w=(*i)->getWidth();
69 h=(*i)->getHeight();
70 i++;
71 for (;i!=mWidgets.end();i++) {
72 if (w<(*i)->getWidth()) w=(*i)->getWidth();
73 h+=(*i)->getHeight()+spacing;
74 }
75 }
76 setWidth(w);
77 setHeight(h);
78 }
fitWidgets()79 void CenterVerticalLayout::fitWidgets() {
80 calculateBB();
81 int y=0;
82 for (WidgetList::iterator i=mWidgets.begin();i!=mWidgets.end();i++) {
83 (*i)->setX(getX()+(getWidth()-(*i)->getWidth())/2);
84 (*i)->setY(y);
85 y+=spacing+(*i)->getHeight();
86 }
87 }
88
89
LeftVerticalLayout(int spacing)90 LeftVerticalLayout::LeftVerticalLayout(int spacing)
91 : CenterVerticalLayout(spacing) {};
92
fitWidgets()93 void LeftVerticalLayout::fitWidgets() {
94 calculateBB();
95 int y=0;
96 for (WidgetList::iterator i=mWidgets.begin();i!=mWidgets.end();i++) {
97 (*i)->setX(0);
98 (*i)->setY(y);
99 y+=spacing+(*i)->getHeight();
100 }
101 };
102
SpacingLayout(int u,int d,int l,int r)103 SpacingLayout::SpacingLayout(int u,int d,int l,int r):c(NULL),u(u),d(d),l(l),r(r) {};
104
setContainer(Layout * cl)105 void SpacingLayout::setContainer(Layout * cl) {
106 c=cl;
107 add(cl);
108 }
109
fitWidgets()110 void SpacingLayout::fitWidgets() {
111 if (c) {
112 c->fitWidgets();
113 setHeight(u+d+c->getHeight());
114 setWidth(l+r+c->getWidth());
115 c->setPosition(l,u);
116 }
117 };
118
calculateBB()119 void CenterHorizontalLayout::calculateBB() {
120 int w=0,h=0;
121 if (!mWidgets.empty()) {
122 WidgetList::iterator i=mWidgets.begin();
123 w=(*i)->getWidth();
124 h=(*i)->getHeight();
125 i++;
126 for (;i!=mWidgets.end();i++) {
127 if (h<(*i)->getHeight()) h=(*i)->getHeight();
128 w+=(*i)->getWidth()+spacing;
129 }
130 }
131 setWidth(w);
132 setHeight(h);
133 }
134
CenterHorizontalLayout(int spacing)135 CenterHorizontalLayout::CenterHorizontalLayout(int spacing): spacing(spacing) {};
fitWidgets()136 void CenterHorizontalLayout::fitWidgets() {
137 calculateBB();
138 int x=0;
139 for (WidgetList::iterator i=mWidgets.begin();i!=mWidgets.end();i++) {
140 (*i)->setY(getY()+(getHeight()-(*i)->getHeight())/2);
141 (*i)->setX(x);
142 x+=spacing+(*i)->getWidth();
143 }
144 };
145
keyPressed(gcn::KeyEvent & e)146 void UCSTextBox::keyPressed(gcn::KeyEvent& e) {
147 gcn::Key key = e.getKey();
148 if (key.getValue()==161 && mEditable) {
149 mTextRows[mCaretRow].insert(mCaretColumn,std::string(1,(char)key.getValue()));
150 ++mCaretColumn;
151 }
152 gcn::TextBox::keyPressed(e);
153 }
154