BOOL COptionsGrid::OnApply() 
{
	ShowGrid  = IsDlgButtonChecked(GRIDBOX_SHOW);
	SnapGrid  = IsDlgButtonChecked(GRIDBOX_SNAP);

	int hold = GetCheckedRadioButton(GRIDBOX_MM,GRIDBOX_INCHES);
	if (hold == GRIDBOX_MM)
		Units = 0;
	else
		Units = 1;


	hold = GetCheckedRadioButton(GRIDBOX_NORMAL,GRIDBOX_USER);
	switch (hold)
	{
		case GRIDBOX_NORMAL:
			Grid = NormalGrid;
			break;
		case GRIDBOX_FINE:
			Grid = FineGrid;
			break;
		case GRIDBOX_USER:
			{
				CString s;
				GetDlgItemText(GRIDBOX_SPACING,s);
				Grid = _tstof( s );
				if (Units == 0)
					Grid = (Grid * static_cast<double>(PIXELSPERMM));
				else
					Grid = (Grid * 254.0 * PIXELSPERMM) / 1000.0;
				break;
			}
	}

	if (Grid <=0 )
	{
		Grid = FineGrid;
	}

	// Write these settings back
	TransformSnap* pSnap = &(GetDocument()->m_snap);

	pSnap->SetAccurateGrid(Grid);
	pSnap->SetGridSnap( SnapGrid );

	GetDocument()->GetOptions()->SetGridShow( ShowGrid == TRUE );
	GetDocument()->GetOptions()->SetUnits( Units == TRUE );

	// Write the settings to the registry
	CRegistry::Set("ShowGrid",ShowGrid);
	CRegistry::Set("Units",Units);
	CRegistry::Set("Grid",Grid);

	return CPropertyPage::OnApply();
}
void CLibraryView::OnDraw(CDC* pDC)
{
	CLibraryDoc * pDoc = GetDocument();

	CLibraryDoc::idCollection::iterator it = pDoc->m_SymbolMap.begin();
	Transform	view_transform;

	if (pDC->IsPrinting())
	{
		view_transform.SetZoomFactor( 5.0 );
	}

	// The font for naming with
	CFont theFont;
	theFont.CreateFont(static_cast<int>(view_transform.doubleScale( 16 )),0,0,0,400,FALSE,FALSE,FALSE,
		ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS | CLIP_LH_ANGLES,
		DEFAULT_QUALITY,DEFAULT_PITCH | FF_MODERN,_T("Courier New"));

	CFont theFont2;
	theFont2.CreateFont(static_cast<int>(view_transform.doubleScale( 14 )),0,0,0,400,FALSE,FALSE,FALSE,
		ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS | CLIP_LH_ANGLES,
		DEFAULT_QUALITY,DEFAULT_PITCH | FF_MODERN,_T("Courier New"));

	CFont theFont3;
	theFont3.CreateFont(static_cast<int>(view_transform.doubleScale( 16 )),0,0,0,400,FALSE,FALSE,FALSE,
		ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS | CLIP_LH_ANGLES,
		DEFAULT_QUALITY,DEFAULT_PITCH |  FF_SWISS, _T("Arial"));

	CFont *oldFont = pDC->SelectObject( &theFont );

	TransformSnap s;
	s.SetGridSnap( FALSE );
	s.SetAccurateGrid(FineGrid);
	CTinyCadDoc doc;
	
	const int inter_spacing = 20;
	const int text_spacing = 25;
	int current_index = 0;
	int current_y = 0;
	int start_y = 0;
	
	double height = view_transform.doubleDeScale( pDC->GetDeviceCaps( VERTRES ) );
	int advance = 0;

	double max_height;
	double max_width;

	if (!pDC->IsPrinting())
	{
		CRect r;
		GetClientRect( r );
		max_width = r.Width();
		max_height = height / m_symbols_per_screen_page;

		// Establish where to start drawing from...
		advance = static_cast<int>(GetScrollPosition().y / max_height);
		current_y = static_cast<int>(advance * max_height);
		start_y = current_y;
	}
	else
	{
		max_width = view_transform.doubleDeScale( pDC->GetDeviceCaps( HORZRES ) );
		max_height = (height - text_spacing*4) / m_symbols_per_print_page;

		CString s = GetDocument()->m_pLibrary->m_name;
		CContext q( pDC, view_transform );
		pDC->SelectObject( &theFont3 );
		q.TextOut( 5, 0, s );
		s.Format(_T("Page %d of %d"), m_printing_page + 1, (GetDocument()->m_SymbolMap.size() / m_symbols_per_print_page) + 1 );
		q.TextOut( 5, height - text_spacing, s );

		// Establish where to start drawing from...
		current_y = text_spacing * 2;
		advance = (m_printing_page * m_symbols_per_print_page);
	}

	while ( advance > 0 && it != pDoc->m_SymbolMap.end() )
	{
		++ it;
		++ current_index;
		-- advance;
	}

	for (;it != pDoc->m_SymbolMap.end(); ++it )
	{
		CContext q( pDC, view_transform );
		
		// Display the name of this symbol
		pDC->SelectObject( &theFont );
			
		// Now access the symbol and draw it next to the name
		CDPoint p;
		CLibraryStoreNameSet &s = GetDocument()->getNameSet(it);
		CDesignFileSymbol *pSymbol = s.GetDesignSymbol( &doc, 0 );

		// Determine the rotation
		int orientation = s.orientation;


		drawingCollection method;
		if (pSymbol)
		{
			pSymbol->GetMethod( 0, false, method );
			p = pSymbol->GetTr( 0, false );
		}
		else
		{
			p = CDPoint(-75,-15);
		}

		if (orientation >= 2)
		{
			CDPoint q = p;
			p.x = q.y;
			p.y = q.x;
		}

		int block_height = 0;
		double zoom = min( (max_width*75) / fabs(p.x), (max_height*75)/fabs(p.y) );
		zoom = min( 100, zoom );
		zoom = max( 2, zoom );
		block_height = static_cast<int>(max_height);

		// Fill in the background rectangle
		q.SetOrigin( CDPoint( 0, -current_y) );
		if (!pDC->IsPrinting())
		{
			if (current_index == pDoc->getSelected())
			{
				q.SelectBrush( cOFFPAGE );
			}
			else
			{
				q.SelectBrush( cWHITE );
			}
			q.SelectPen( cBLACK,0,0);
			q.Rectangle( CDRect(0,0,max_width, block_height ));
		}
		else
		{
			q.SelectPen( cOFFPAGE,0,0 );
			q.MoveTo( CDPoint(0,0) );
			q.LineTo( CDPoint(max_width, 0) );
		}

		// Draw the name
		q.SetTextAlign(TA_LEFT | TA_BOTTOM | TA_NOUPDATECP);
		q.SetBkMode( TRANSPARENT );
		q.SetTextColor( RGB(0,0,0) );

		// Do this for each of the names in the symbol set
		int text_pos_y = 20;
		int text_pos_x = 5;
		int text_col_width = 0;
		for (int i =0; i < s.GetNumRecords(); i++)
		{
			CSymbolRecord &r = s.GetRecord( i );

			pDC->SelectObject( &theFont );
			q.TextOut( text_pos_x, text_pos_y, r.name );

			pDC->SelectObject( &theFont2 );
			q.TextOut( text_pos_x, text_pos_y + 16, r.description );
			text_pos_y += 35;

			text_col_width = max( text_col_width, static_cast<int>(view_transform.DeScale( pDC->GetTextExtent( r.description ).cx )) );
			text_col_width = max( text_col_width, static_cast<int>(view_transform.DeScale( pDC->GetTextExtent( r.name ).cx ) ) );

			if (text_pos_y > max_height - 40)
			{
				// Move across to the other side...
				text_pos_x += text_col_width + 30;
				text_col_width = 0;
				text_pos_y = 20;
			}
		}

		int width_offset = max( 250, text_col_width + text_pos_x + 30 );
		int width_to_fill = static_cast<int>(max_width) - width_offset;

		// Now display the symbol
		CDPoint old;

		q.SetZoomFactor( zoom/100.0 *  q.GetTransform().GetZoomFactor() );
		int centering_height = static_cast<int>((max_height * 100) / zoom);
		int centering_width = static_cast<int>((width_to_fill * 100) / zoom);
		q.SetOrigin( CDPoint( -(width_offset*100)/zoom, ((-current_y)*100)/zoom) );
		switch (orientation)
		{
		case 2:
			old = q.SetTRM( CDPoint((centering_width-p.x)/2, (centering_height - p.y)/2) , CDPoint(0,0), orientation );
			break;
		case 3:
			old = q.SetTRM( CDPoint(centering_width / 2 + p.x/2, (centering_height-p.y)/2) , CDPoint(0,0), orientation );
			break;
		default:
			old = q.SetTRM( CDPoint((centering_width-p.x)/2, (centering_height-p.y)/2) , CDPoint(0,0), orientation );
			break;
		}

		 
		if (pSymbol)
		{
			drawingIterator paint_it = method.begin();
			while (paint_it != method.end())
			{
				(*paint_it)->Paint( q, draw_normal );
				++ paint_it;
			}

			delete pSymbol;
		}

		q.EndTRM( old );

		current_index ++;
		current_y += block_height;

		if (current_y - start_y + block_height > height)
		{
			break;
		}
	}

	pDC->SelectObject( oldFont );

	SetScrollSizes( MM_TEXT, GetDocSize() );
}