1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /***************************************************************************
3 * combobox.cc
4 *
5 * Sun Mar 10 19:04:50 CET 2013
6 * Copyright 2013 Bent Bisballe Nyeng
7 * deva@aasimon.org
8 ****************************************************************************/
9
10 /*
11 * This file is part of DrumGizmo.
12 *
13 * DrumGizmo is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * DrumGizmo is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with DrumGizmo; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 */
27 #include "combobox.h"
28
29 #include "painter.h"
30 #include "font.h"
31
32 #include <stdio.h>
33
34 #define BORDER 10
35
36 namespace GUI
37 {
38
listboxSelectHandler()39 void ComboBox::listboxSelectHandler()
40 {
41 ButtonEvent buttonEvent;
42 buttonEvent.direction = Direction::down;
43 this->buttonEvent(&buttonEvent);
44 }
45
ComboBox(Widget * parent)46 ComboBox::ComboBox(Widget* parent)
47 : Widget(parent)
48 , listbox(parent)
49 {
50 CONNECT(&listbox, selectionNotifier, this, &ComboBox::listboxSelectHandler);
51 CONNECT(&listbox, clickNotifier, this, &ComboBox::listboxSelectHandler);
52
53 listbox.hide();
54 }
55
~ComboBox()56 ComboBox::~ComboBox()
57 {
58 }
59
addItem(std::string name,std::string value)60 void ComboBox::addItem(std::string name, std::string value)
61 {
62 listbox.addItem(name, value);
63 }
64
clear()65 void ComboBox::clear()
66 {
67 listbox.clear();
68 redraw();
69 }
70
selectItem(int index)71 bool ComboBox::selectItem(int index)
72 {
73 listbox.selectItem(index);
74 redraw();
75 return true;
76 }
77
selectedName()78 std::string ComboBox::selectedName()
79 {
80 return listbox.selectedName();
81 }
82
selectedValue()83 std::string ComboBox::selectedValue()
84 {
85 return listbox.selectedValue();
86 }
87
drawArrow(Painter & p,int x,int y,int w,int h)88 static void drawArrow(Painter &p, int x, int y, int w, int h)
89 {
90 p.drawLine(x, y, x+(w/2), y+h);
91 p.drawLine(x+(w/2), y+h, x+w, y);
92
93 y++;
94 p.drawLine(x, y, x+(w/2), y+h);
95 p.drawLine(x+(w/2), y+h, x+w, y);
96 }
97
repaintEvent(RepaintEvent * repaintEvent)98 void ComboBox::repaintEvent(RepaintEvent* repaintEvent)
99 {
100 Painter p(*this);
101
102 std::string _text = selectedName();
103
104 int w = width();
105 int h = height();
106 if(w == 0 || h == 0)
107 {
108 return;
109 }
110
111 box.setSize(w, h);
112 p.drawImage(0, 0, box);
113
114 p.setColour(Colour(183.0f/255.0f, 219.0f/255.0f, 255.0/255.0f, 1.0f));
115 p.drawText(BORDER - 4 + 3, height()/2+5 + 1 + 1, font, _text);
116
117 // p.setColour(Colour(1, 1, 1));
118 // p.drawText(BORDER - 4, (height()+font.textHeight()) / 2 + 1, font, _text);
119
120 //int n = height() / 2;
121
122 // p.drawLine(width() - n - 6, 1 + 6, width() - 1 - 6, 1 + 6);
123 {
124 int w = 10;
125 int h = 6;
126 drawArrow(p, width() - 6 - 4 - w, (height() - h) / 2, w, h);
127 p.drawLine(width() - 6 - 4 - w - 4, 7,
128 width() - 6 - 4 - w - 4, height() - 8);
129 }
130 }
131
scrollEvent(ScrollEvent * scrollEvent)132 void ComboBox::scrollEvent(ScrollEvent* scrollEvent)
133 {
134 /*
135 scroll_offset += e->delta;
136 if(scroll_offset < 0)
137 {
138 scroll_offset = 0;
139 }
140 if(scroll_offset > (items.size() - 1))
141 {
142 scroll_offset = (items.size() - 1);
143 }
144 redraw();
145 */
146 }
147
keyEvent(KeyEvent * keyEvent)148 void ComboBox::keyEvent(KeyEvent* keyEvent)
149 {
150 if(keyEvent->direction != Direction::up)
151 {
152 return;
153 }
154
155 /*
156 switch(keyEvent->keycode) {
157 case Key::up:
158 {
159 selected--;
160 if(selected < 0)
161 {
162 selected = 0;
163 }
164 if(selected < scroll_offset)
165 {
166 scroll_offset = selected;
167 if(scroll_offset < 0)
168 {
169 scroll_offset = 0;
170 }
171 }
172 }
173 break;
174 case Key::down:
175 {
176 // Number of items that can be displayed at a time.
177 int numitems = height() / (font.textHeight() + padding);
178
179 selected++;
180 if(selected > (items.size() - 1))
181 {
182 selected = (items.size() - 1);
183 }
184 if(selected > (scroll_offset + numitems - 1))
185 {
186 scroll_offset = selected - numitems + 1;
187 if(scroll_offset > (items.size() - 1))
188 {
189 scroll_offset = (items.size() - 1);
190 }
191 }
192 }
193 break;
194 case Key::home:
195 selected = 0;
196 break;
197 case Key::end:
198 selected = items.size() - 1;
199 break;
200 default:
201 break;
202 }
203
204 redraw();
205 */
206 }
207
buttonEvent(ButtonEvent * buttonEvent)208 void ComboBox::buttonEvent(ButtonEvent* buttonEvent)
209 {
210 // Ignore everything except left clicks.
211 if(buttonEvent->button != MouseButton::left)
212 {
213 return;
214 }
215
216 if(buttonEvent->direction != Direction::down)
217 {
218 return;
219 }
220
221 if(!listbox.visible())
222 {
223 listbox.resize(width() - 10, 100);
224 listbox.move(x() + 5, y() + height() - 7);
225 }
226 else
227 {
228 valueChangedNotifier(listbox.selectedName(), listbox.selectedValue());
229 }
230
231 listbox.setVisible(!listbox.visible());
232 }
233
234 } // GUI::
235