11 ListBox::ListBox(uint32 x, uint32 y, uint32 w, uint32 h): Element(x, y, w, h),
12 thumbClicked(false), windowPtr(0), cursor(0), limit(0), charWidth((w / FONT_WIDTH) - 1),
13 charHeight(h / FONT_HEIGHT), elementToTell(NULL), upArrow(w - 8, 0, upArrowBox),
14 downArrow(w - 8, h - 8, downArrowBox), upArrow2(w - 8, h - 16, upArrowBox)
16 upArrow.SetNotificationElement(this);
17 downArrow.SetNotificationElement(this);
18 upArrow2.SetNotificationElement(this);
19 extents.w -= 8; // Make room for scrollbar...
22 void ListBox::HandleKey(SDLKey key)
26 if (cursor != limit - 1) // Cursor is within its window
28 else // Otherwise, scroll the window...
30 if (cursor + windowPtr != item.size() - 1)
34 else if (key == SDLK_UP)
44 else if (key == SDLK_PAGEDOWN)
46 if (cursor != limit - 1)
51 if (windowPtr > item.size() - limit)
52 windowPtr = item.size() - limit;
55 else if (key == SDLK_PAGEUP)
61 if (windowPtr < limit)
67 else if (key >= SDLK_a && key <= SDLK_z)
69 // Advance cursor to filename with first letter pressed...
70 uint8 which = (key - SDLK_a) + 65; // Convert key to A-Z char
72 for(uint32 i=0; i<item.size(); i++)
74 if ((item[i][0] & 0xDF) == which)
76 cursor = i - windowPtr;
77 if (i > windowPtr + limit - 1)
78 windowPtr = i - limit + 1, cursor = limit - 1;
80 windowPtr = i, cursor = 0;
87 void ListBox::HandleMouseMove(uint32 x, uint32 y)
89 upArrow.HandleMouseMove(x - extents.x, y - extents.y);
90 downArrow.HandleMouseMove(x - extents.x, y - extents.y);
91 upArrow2.HandleMouseMove(x - extents.x, y - extents.y);
95 uint32 sbHeight = extents.h - 24,
96 thumb = (uint32)(((float)limit / (float)item.size()) * (float)sbHeight);
98 //yRelativePoint is the spot on the thumb where we clicked...
99 int32 newThumbStart = y - yRelativePoint;
101 if (newThumbStart < 0)
104 if ((uint32)newThumbStart > sbHeight - thumb)
105 newThumbStart = sbHeight - thumb;
107 windowPtr = (uint32)(((float)newThumbStart / (float)sbHeight) * (float)item.size());
108 //Check for cursor bounds as well... Or do we need to???
109 //Actually, we don't...!
113 void ListBox::HandleMouseButton(uint32 x, uint32 y, bool mouseDown)
115 if (Inside(x, y) && mouseDown)
117 // Why do we have to do this??? (- extents.y?)
118 // I guess it's because only the Window class has offsetting implemented... !!! FIX !!!
119 // cursor = (y - extents.y) / 8;
120 cursor = (y - extents.y) / FONT_HEIGHT;
123 // Check for a hit on the scrollbar...
124 if (x > (uint32)(extents.x + extents.w) && x <= (uint32)(extents.x + extents.w + 8)
125 && y > (uint32)(extents.y + 8) && y <= (uint32)(extents.y + extents.h - 16))
129 // This shiaut should be calculated in AddItem(), not here... (or in Draw() for that matter)
130 uint32 sbHeight = extents.h - 24,
131 thumb = (uint32)(((float)limit / (float)item.size()) * (float)sbHeight),
132 thumbStart = (uint32)(((float)windowPtr / (float)item.size()) * (float)sbHeight);
134 // Did we hit the thumb?
135 if (y >= (extents.y + 8 + thumbStart) && y < (extents.y + 8 + thumbStart + thumb))
136 thumbClicked = true, yRelativePoint = y - thumbStart;
138 //Seems that this is useless--never reached except in rare cases and that the code outside is
141 // thumbClicked = false;
145 thumbClicked = false;
147 upArrow.HandleMouseButton(x - extents.x, y - extents.y, mouseDown);
148 downArrow.HandleMouseButton(x - extents.x, y - extents.y, mouseDown);
149 upArrow2.HandleMouseButton(x - extents.x, y - extents.y, mouseDown);
152 void ListBox::Draw(uint32 offsetX/*= 0*/, uint32 offsetY/*= 0*/)
154 for(uint32 i=0; i<limit; i++)
156 // Strip off the extension
157 // (extension stripping should be an option, not default!)
158 std::string s(item[windowPtr + i], 0, item[windowPtr + i].length() - 4);
159 // DrawString(screenBuffer, extents.x + offsetX, extents.y + offsetY + i*8,
160 DrawString(screenBuffer, extents.x + offsetX, extents.y + offsetY + i*FONT_HEIGHT,
161 (cursor == i ? true : false), "%-*.*s", charWidth, charWidth, s.c_str());
164 upArrow.Draw(extents.x + offsetX, extents.y + offsetY);
165 downArrow.Draw(extents.x + offsetX, extents.y + offsetY);
166 upArrow2.Draw(extents.x + offsetX, extents.y + offsetY);
168 uint32 sbHeight = extents.h - 24,
169 thumb = (uint32)(((float)limit / (float)item.size()) * (float)sbHeight),
170 thumbStart = (uint32)(((float)windowPtr / (float)item.size()) * (float)sbHeight);
172 for(uint32 y=extents.y+offsetY+8; y<extents.y+offsetY+extents.h-16; y++)
174 // for(uint32 x=extents.x+offsetX+extents.w-8; x<extents.x+offsetX+extents.w; x++)
175 for(uint32 x=extents.x+offsetX+extents.w; x<extents.x+offsetX+extents.w+8; x++)
177 if (y >= thumbStart + (extents.y+offsetY+8) && y < thumbStart + thumb + (extents.y+offsetY+8))
178 // screenBuffer[x + (y * pitch)] = (thumbClicked ? 0x458E : 0xFFFF);
179 //458E -> 01 0001 0 1100 0 1110 -> 0100 0101 0110 0011 0111 0011 -> 45 63 73
180 screenBuffer[x + (y * pitch)] = (thumbClicked ? 0xFF736345 : 0xFFFFFFFF);
182 // screenBuffer[x + (y * pitch)] = 0x0200;
183 //0200 -> 000000 10000 00000 -> 00 1000 0100 00
184 screenBuffer[x + (y * pitch)] = 0xFF008400;
189 void ListBox::Notify(Element * e)
191 if (e == &upArrow || e == &upArrow2)
197 if (cursor < limit - 1)
201 else if (e == &downArrow)
203 if (windowPtr < item.size() - limit)
213 void ListBox::SetNotificationElement(Element * e)
218 void ListBox::AddItem(std::string s)
220 // Do a simple insertion sort
221 bool inserted = false;
223 for(std::vector<std::string>::iterator i=item.begin(); i<item.end(); i++)
225 if (stringCmpi(s, *i) == -1)
236 limit = (item.size() > charHeight ? charHeight : item.size());
239 std::string ListBox::GetSelectedItem(void)
241 return item[windowPtr + cursor];