Exemple #1
0
void cListBox::SetSelected(int ID, bool ev, bool deselect_others)
{
	cListItem* current = m_Items;
	m_LastSelected = 0;
	m_HeaderClicked = "";

	if (!current) return;

	// special case if ID "-2" is sent, select first actual list item (not based on ID)
	if (ID == -2)
	{
		current->m_Selected = true;
		m_LastSelected = current;
		m_Position = 0;
		m_ScrollBar->SetTopValue(m_Position);
		if (ev) g_InterfaceEvents.AddEvent(EVENT_SELECTIONCHANGE, m_ID);
		return;
	}

	int count = 0; int posit = 0;
	while (current)
	{
		if (current->m_ID == ID)
		{
			if (ev) g_InterfaceEvents.AddEvent(EVENT_SELECTIONCHANGE, m_ID);
			current->m_Selected = true;
			m_LastSelected = current;
			posit = count;
			if (!deselect_others) break;
		}
		else
		{
			if (deselect_others) current->m_Selected = false;
		}
		count++;
		current = current->m_Next;
	}

	if (count <= m_NumDrawnElements)
	{
		m_Position = 0;
	}
	else
	{
		if (m_Position >= posit)
			m_Position = posit - 1;
		else if (m_Position + m_NumDrawnElements - 1 <= posit)
			m_Position = posit - m_NumDrawnElements + 2;
	}

	if (m_Position > count - m_NumDrawnElements) m_Position = count - m_NumDrawnElements;
	if (m_Position < 0) m_Position = 0;

	m_ScrollBar->SetTopValue(m_Position);
}
Exemple #2
0
bool cButton::ButtonClicked(int x, int y)
{
	if (m_Disabled || m_Hidden) return false;
	if (IsOver(x, y))
	{
		g_InterfaceEvents.AddEvent(EVENT_BUTTONCLICKED, m_ID);
		return true;
	}
	return false;
}
Exemple #3
0
void cListBox::OnClicked(int x, int y, bool mouseWheelDown, bool mouseWheelUp)
{
	cListItem* current = 0;
	if (m_NumElements == 0)	// it doesn't matter if there are no items in the list
		return;

	// if user clicked on "un-sort" header, do that
	if (m_ShowHeaders && m_HeaderClicksSort
		&& x > m_XPos + m_eWidth - 16
		&& x <= m_XPos + m_eWidth
		&& y > m_YPos + m_BorderSize
		&& y <= m_YPos + m_BorderSize + LISTBOX_ITEMHEIGHT
		)
	{
		UnSortList();
		return;
	}

	if (IsOver(x, y))
	{
		if (mouseWheelDown)
		{
			ScrollDown();
			return;
		}

		if (mouseWheelUp)
		{
			ScrollUp();
			return;
		}

		if (m_EnableEvents)
			g_InterfaceEvents.AddEvent(EVENT_SELECTIONCHANGE, m_ID);

		// See if a header was clicked
		m_HeaderClicked = "";
		if (m_ShowHeaders && y > m_YPos + m_BorderSize && y <= m_YPos + m_BorderSize + LISTBOX_ITEMHEIGHT)
		{
			int x_start = 0, x_end = 0;
			for (int i = 0; i < m_ColumnCount; i++)
			{
				if (m_SkipColumn[i]) continue;

				x_start = m_ColumnOffset[i] - 3;

				if (i < m_ColumnCount - 1)
					x_end = m_ColumnOffset[i + 1] - 3;
				else
					x_end = m_eWidth;

				if (x >= m_XPos + x_start && x <= m_XPos + x_end)
				{
					// then set it as clicked
					m_HeaderClicked = m_ColumnName[i];

					// should we re-sort list based on header?
					if (m_HeaderClicksSort)
					{
						if (m_SortedColumn == m_ColumnName[i])
							m_SortedDescending = !m_SortedDescending;
						else
						{
							m_SortedColumn = m_ColumnName[i];
							m_SortedDescending = false;
						}
						ReSortList();
					}
					return;
				}
			}
		}

		bool deselect = false;
		if (m_MultiSelect == true)
		{
			if (g_ShiftDown == false && g_CTRLDown == false)
			{
				m_HasMultiSelect = false;
				deselect = true;
			}
			else m_HasMultiSelect = true;
		}
		else deselect = true;

		// first unselect any currently selected items
		if (deselect)
		{
			m_LastSelected = 0;
			current = m_Items;
			while (current)
			{
				current->m_Selected = false;
				current = current->m_Next;
			}
		}

		bool singleSelect = true;
		if (m_MultiSelect == true)
		{
			if (g_ShiftDown == true)
				singleSelect = false;
		}
		else singleSelect = true;

		if (singleSelect)	// find the first element displayed
		{
			current = m_Items;
			int count = 0;
			while (current)
			{
				if (count == m_Position) break;
				count++;
				current = current->m_Next;
			}

			// Check each element to see if mouse is over it
			int temp = 0;
			while (current)
			{
				if ((count - m_Position) >= m_NumDrawnElements)	// stop if running past the visible list
					break;

				int cX = m_XPos + m_BorderSize;
				int cY = (m_YPos + m_BorderSize) + (LISTBOX_ITEMHEIGHT*temp);
				if (m_ShowHeaders) // Account for headers if shown
					cY += LISTBOX_ITEMHEIGHT;

				// Check if over the item
				if (x > cX && y > cY && x < cX + m_eWidth && y <= cY + m_eHeight)
				{
					// then select it
					current->m_Selected = !current->m_Selected;
					m_LastSelected = current;

					// update info for tracking double-clicks
					m_LastClickTime = m_CurrentClickTime;
					m_LastClickX = m_CurrentClickX;
					m_LastClickY = m_CurrentClickY;
					m_CurrentClickTime = SDL_GetTicks();
					m_CurrentClickX = x;
					m_CurrentClickY = y;

					break;
				}

				count++; temp++;
				current = current->m_Next;
			}
		}
		else
		{
			if (g_ShiftDown == true)	// select from first to last
			{
				current = m_Items;
				cListItem* select_first = 0;
				cListItem* select_last = 0;
				int count = 0;
				// scan through to find both last-clicked-on item and currently-clicked-on item
				while (current)
				{
					if (select_first && select_last)  // if we have both now, we're done here
						break;

					// was this the last item the user clicked on earlier?
					if (current == m_LastSelected)
					{
						if (!select_first)
							select_first = current;
						else
							select_last = current;
					}

					// is this the current item the user just clicked on?
					if (count >= m_Position && count - m_Position < m_NumDrawnElements)
					{
						int cX = m_XPos + m_BorderSize;
						int cY = (m_YPos + m_BorderSize) + (LISTBOX_ITEMHEIGHT*(count - m_Position));
						if (m_ShowHeaders) // Account for headers if shown
							cY += LISTBOX_ITEMHEIGHT;

						// Check if over the item
						if (x > cX && y > cY && x < cX + m_eWidth && y <= cY + m_eHeight)
						{
							if (!select_first)
								select_first = current;
							else
								select_last = current;
						}
					}

					count++;
					current = current->m_Next;
				}

				current = select_first;
				while (current)	// now simply select each one ranging from first to last
				{
					current->m_Selected = true;	// select the item
					if (current == select_last)
					{
						m_LastSelected = current;
						break;
					}
					current = current->m_Next;
				}
			}
		}
	}
}