// The object selection functions
// This selects objects in a box
// (We don't select construction objects)
void CTinyCadDoc::Select(CDPoint p1,CDPoint p2)
{
  double left=min(p1.x,p2.x);
  double right=max(p1.x,p2.x);
  double top=min(p1.y,p2.y);
  double bottom=max(p1.y,p2.y);

  UnSelect();

	drawingIterator it = GetDrawingBegin();
	while (it != GetDrawingEnd()) 
	{
		CDrawingObject *obj = *it;

		if (    obj->IsInside(left,right,top,bottom) 
			&& !obj->IsConstruction()
			&& 
			(obj->GetType() != xJunction || !GetOption().GetAutoJunc() ) )
		{
			obj->Display();
			Select( obj );
		} 
  		
		++ it;
	}
}
// Remove an item from the drawing...
void CTinyCadDoc::Delete( drawingIterator it)
{
	CDrawingObject *pointer = *it;

	pointer->Display();
	if (pointer == GetSelectable())
	{
		SetSelectable( NULL );
	}
	UnSelect(pointer);

	MarkDeleteForUndo( *it );
	delete *it;
	m_drawing.erase( it );

	SetModifiedFlag( TRUE );
}
// Delete the selected objects!
void CTinyCadDoc::SelectDelete()
{
  if (!IsSelected())
	return;

    CJunctionUtils j( this );

	drawingIterator it = GetDrawingBegin();
	while (it != GetDrawingEnd()) 
	{
		drawingIterator current = it;
		++ it;

		CDrawingObject *pointer = *current;
		if (IsSelected( pointer ))
		{
			if (pointer == GetSelectable())
			{
				SetSelectable( NULL );
			}

			j.AddObjectToTodo( pointer );
			MarkDeleteForUndo( pointer );
			m_drawing.erase( current );
			pointer->Display();
			delete pointer;
		}
	}

	m_selected.erase( m_selected.begin(), m_selected.end() );

  // ... and perform the junction requirements...
  j.CheckTodoList( true );

  SetModifiedFlag( TRUE );
}
// Redo the last action
void CTinyCadDoc::Redo()
{
	SetSelectable( NULL );
	BOOL action_taken = FALSE;

	// Is this possible?
	while (CanRedo() && !action_taken)
	{


		m_undo_level ++;

		// Re-apply all of the changes we have done at this level
		CDocUndoSet &s = m_undo[ m_undo_level ];


		// Go through the list of action and redo each one 
		//
		CDocUndoSet::actionCollection::iterator act_it = s.m_actions.begin();

		while (act_it != s.m_actions.end())
		{
			CDocUndoSet::CDocUndoAction &act = *act_it;

			// Look up this item from the index...
			drawingCollection::iterator it = m_drawing.begin();
			int index = act.m_index;
			while (index > 0 && it != m_drawing.end())
			{
				++ it;
				-- index;
			}

			if (it != m_drawing.end())
			{
				(*it)->Display();
			}
			act.m_object->Display();

			switch (act.m_action)
			{
			case CDocUndoSet::Deletion:
				// We must re-delete the deleted objects
				delete *it;
				m_drawing.erase( it );
				action_taken = TRUE;
				break;
			case CDocUndoSet::Addition:
				// We must re-insert the additions
				m_drawing.insert( it, Dup(act.m_object) );
				action_taken = TRUE;
				break;
			case CDocUndoSet::Change:
				// We convert the old objects into the new objects...
				{
					// Keep a copy for the redo...
					CDrawingObject *copy = Dup(*it);
					delete *it;

					copy->Display();
					*it = act.m_object;
					act.m_object = copy;
				}
				action_taken = TRUE;
				break;
			}

			++ act_it;
		}
	}
}