void ElementDataGridRow::RemoveChildren(int first_row_removed, int num_rows_removed)
{
	if (num_rows_removed == -1)
	{
		num_rows_removed = (int)children.size() - first_row_removed;
	}

	// prevent relayout of the document while removing rows
	Core::ElementDocument* document = parent_grid->GetOwnerDocument();
	document->LockLayout(true);

	for (int i = num_rows_removed - 1; i >= 0; i--)
	{
		children[first_row_removed + i]->RemoveChildren();
		parent_grid->RemoveRows(children[first_row_removed + i]->GetTableRelativeIndex());
	}

	children.erase(children.begin() + first_row_removed, children.begin() + (first_row_removed + num_rows_removed));
    for (int i = first_row_removed; i < (int) children.size(); i++)
	{
		children[i]->SetChildIndex(i);
		children[i]->DirtyTableRelativeIndex();
	}

	document->LockLayout(false);

	Rocket::Core::Dictionary parameters;
	parameters.Set("first_row_removed", GetChildTableRelativeIndex(first_row_removed));
	parameters.Set("num_rows_removed", num_rows_removed);
	parent_grid->DispatchEvent("rowremove", parameters);
}
void ElementDataGrid::OnUpdate()
{
	Core::ElementDocument* document = GetOwnerDocument();
	document->LockLayout(true);
	
	if (!new_data_source.Empty())
	{
		root->SetDataSource(new_data_source);
		new_data_source = "";
	}

	bool any_new_children = root->UpdateChildren();
	if (any_new_children)
	{
		DispatchEvent("rowupdate", Rocket::Core::Dictionary());
	}
	
	if (!body_visible && (!any_new_children || root->GetNumLoadedChildren() >= Rocket::Core::Math::RealToInteger(ResolveProperty("min-rows", 0))))
	{
		body->SetProperty("display", "block");
		body_visible = true;
	}
	
	document->LockLayout(false);
}
// Adds children underneath this row, and fetches their contents (and possible
// children) from the row's data source.
void ElementDataGridRow::AddChildren(int first_row_added, int num_rows_added)
{
	if (first_row_added == -1)
	{
		first_row_added = (int)children.size();
	}

	// prevent relayout of the document while adding rows
	Core::ElementDocument* document = parent_grid->GetOwnerDocument();
	document->LockLayout(true);

	// We need to make a row for each new child, then pass through the cell
	// information and the child's data source (if one exists.)
	if (data_source)
	{
		for (int i = 0; i < num_rows_added; i++)
		{
			int row_index = first_row_added + i;

			// Make a new row:
			ElementDataGridRow* new_row = parent_grid->AddRow(this, row_index);
			children.insert(children.begin() + row_index, new_row);

			if (!row_expanded)
			{
				new_row->SetProperty("display", "none");
			}
		}

		for (int i = first_row_added + num_rows_added; i < (int)children.size(); i++)
		{
			children[i]->SetChildIndex(i);
			children[i]->DirtyTableRelativeIndex();
		}

		if (parent_row)
		{
			parent_row->ChildChanged(child_index);
		}
	}

	document->LockLayout(false);

	RefreshChildDependentCells();
	DirtyRow();

	Rocket::Core::Dictionary parameters;
	parameters.Set("first_row_added", GetChildTableRelativeIndex(first_row_added));
	parameters.Set("num_rows_added", num_rows_added);
	parent_grid->DispatchEvent("rowadd", parameters);
}