1 /***************************************************************************
2                           listbox.cpp  -  A List Box
3                              -------------------
4     begin                : za mrt 8 2008
5     copyright            : (C) 2008 by CJP
6     email                : cornware-cjp@users.sourceforge.net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include <cstdio>
19 
20 #include "SDL.h"
21 #include <GL/gl.h>
22 
23 #include <libintl.h>
24 #define _(String) gettext (String)
25 
26 #include "console.h"
27 #include "listbox.h"
28 
CListBox()29 CListBox::CListBox()
30 {
31 	loadConsoleFont();
32 
33 	m_Title = "Title";
34 	m_Cancelled = false;
35 }
36 
37 
~CListBox()38 CListBox::~CListBox()
39 {
40 }
41 
setTitle(const CString & title)42 void CListBox::setTitle(const CString &title)
43 {
44 	m_Title = title;
45 	updateLines();
46 }
47 
setOptions(const vector<CString> & options)48 void CListBox::setOptions(const vector<CString> &options)
49 {
50 	m_Menu.m_Lines = options;
51 }
52 
setSelected(unsigned int sel)53 void CListBox::setSelected(unsigned int sel)
54 {
55 	m_Menu.m_Selected = sel;
56 }
57 
getSelected() const58 unsigned int CListBox::getSelected() const
59 {
60 	return m_Menu.m_Selected;
61 }
62 
updateLines()63 void CListBox::updateLines()
64 {
65 	int maxChars = int(m_W / theConsoleFont->getFontW());
66 	if(maxChars == 0) maxChars = 1;
67 
68 	//Splitting the title up into separate lines
69 	m_Lines.clear();
70 	CString theRest = m_Title;
71 	while(true)
72 	{
73 		//quit when there is no need to break
74 		if(int(theRest.length()) <= maxChars)
75 		{
76 			m_Lines.push_back(theRest);
77 			break;
78 		}
79 
80 		//find the last space before the break pos
81 		int breakpos = -1;
82 		for(int i=0; i < maxChars; i++)
83 		{
84 			if(theRest[i] == ' ') breakpos = i;
85 		}
86 
87 		CString lhs;
88 		if(breakpos < 0) //if there was no space
89 		{
90 			lhs = theRest.mid(0, maxChars);
91 			theRest = theRest.mid(maxChars);
92 		}
93 		else //we found a space
94 		{
95 			lhs = theRest.mid(0, breakpos);
96 			theRest = theRest.mid(breakpos+1); //skip the space character
97 		}
98 
99 		//printf("%d: \"%s\"\n", m_Lines.size(), lhs.c_str());
100 		m_Lines.push_back(lhs);
101 	}
102 }
103 
onResize(int x,int y,int w,int h)104 int CListBox::onResize(int x, int y, int w, int h)
105 {
106 	int ret = CWidget::onResize(x, y, w, h);
107 	updateLines();
108 
109 	ret = ret | m_Menu.onResize(int(x + 0.1*w), int(y + 0.2*h), int(0.8*w), int(0.5*h));
110 
111 	return ret;
112 }
113 
onKeyPress(int key)114 int CListBox::onKeyPress(int key)
115 {
116 	int ret = m_Menu.onKeyPress(key);
117 
118 	if(ret & WIDGET_CANCELLED) m_Cancelled = true;
119 
120 	return ret;
121 }
122 
onMouseClick(int x,int y,unsigned int buttons)123 int CListBox::onMouseClick(int x, int y, unsigned int buttons)
124 {
125 	if(m_Menu.isInWidget(x, y))
126 	{
127 		int ret = m_Menu.onMouseClick(x, y, buttons);
128 		if(ret & WIDGET_CANCELLED) m_Cancelled = true;
129 		return ret;
130 	}
131 
132 	if(y < m_Menu.getY())
133 	{
134 		m_Cancelled = true;
135 		return WIDGET_CANCELLED | WIDGET_QUIT;
136 	}
137 
138 	return 0;
139 }
140 
onMouseMove(int x,int y,unsigned int buttons)141 int CListBox::onMouseMove(int x, int y, unsigned int buttons)
142 {
143 	if(m_Menu.isInWidget(x, y))
144 		return m_Menu.onMouseMove(x, y, buttons);
145 
146 	return 0;
147 }
148 
onRedraw()149 int CListBox::onRedraw()
150 {
151 	CWidget::onRedraw();
152 
153 	glColor4f(0, 0, 0, 0.8);
154 	drawBackground();
155 	glColor4f(1,1,1, 1);
156 
157 	//centered
158 	glTranslatef(m_W/2, m_H/2, 0);
159 
160 	glPushMatrix();
161 
162 	glTranslatef(-3*theConsoleFont->getFontW(), -0.4*m_H, 0);
163 
164 	glColor3f(1, 0.9, 0);
165 	theConsoleFont->enable();
166 	theConsoleFont->drawString(_("Cancel"));
167 	theConsoleFont->disable();
168 
169 	glPopMatrix();
170 
171 	drawTitle();
172 
173 	return m_Menu.onRedraw();
174 }
175 
drawTitle()176 void CListBox::drawTitle()
177 {
178 	glPushMatrix();
179 
180 	glTranslatef(0.0, 0.3*m_H, 0);
181 	theConsoleFont->enable();
182 	for(unsigned int i=0; i < m_Lines.size(); i++)
183 	{
184 		glPushMatrix();
185 		glTranslatef(-0.5*theConsoleFont->getFontW()*m_Lines[i].size(), -theConsoleFont->getFontH()*i, 0);
186 
187 		//the title
188 		glColor3f(1,1,1);
189 		theConsoleFont->drawString(m_Lines[i]);
190 		glPopMatrix();
191 	}
192 	theConsoleFont->disable();
193 
194 	glPopMatrix();
195 }
196 
197