Index: branches/simulator/projects/simulator/simulator/gui/gtk_environment_view.c
===================================================================
--- branches/simulator/projects/simulator/simulator/gui/gtk_environment_view.c	(revision 1085)
+++ branches/simulator/projects/simulator/simulator/gui/gtk_environment_view.c	(revision 1086)
@@ -38,10 +38,15 @@
 				GdkEventMotion* event);
 
 
-static void draw_world(GdkDrawable* drawable, GdkGC* gc, world_t* world);
-static void draw_robot(GdkDrawable* drawable, GdkGC* gc, 
+static void draw_world(GtkEnvironmentView* view, GdkDrawable* drawable, GdkGC* gc, world_t* world);
+static void draw_robot(GtkEnvironmentView* view, GdkDrawable* drawable, GdkGC* gc, 
                         float x, float y, float angle);
 
+static void to_world_coordinates(GtkEnvironmentView* view, int x, int y, float* world_x, float* world_y);
+static void to_window_coordinates(GtkEnvironmentView* view, float x, float y, int* win_x, int* win_y);
+static void to_window_scale(GtkEnvironmentView* view, float v, int* win_v);
+
+
 GtkType gtk_environment_view_get_type(void)
 {
 	static GtkType environment_view_type = 0;
@@ -193,12 +198,12 @@
 	
 	view = GTK_ENVIRONMENT_VIEW(widget);
 
-	draw_world(widget->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)], &world);
+	draw_world(view, widget->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)], &world);
 	
 	robot_iterator_reset();
 	Robot* r;
 	while ((r = robot_iterator_next()) != NULL) {
-		draw_robot(widget->window, 
+		draw_robot(view, widget->window, 
 			widget->style->fg_gc[GTK_WIDGET_STATE(widget)], 
             r->pose.x, r->pose.y, r->pose.theta);
     }
@@ -210,6 +215,9 @@
 				GdkEventButton* event)
 {
 	GtkEnvironmentView* view;
+	Robot* r;
+	float closestdist;
+	int closest;
 
 	if (widget == NULL || !GTK_IS_ENVIRONMENT_VIEW(widget) || event == NULL)
 		return FALSE;
@@ -224,6 +232,13 @@
 	view->mouseX = event->x;
 	view->mouseY = event->y;
 	view->mouseDown = 1;
+	
+	/*closestdist = 10e8;
+	robot_iterator_reset();
+	while ((r = robot_iterator_next()) != NULL) {
+	{
+		float dist = r->pose.x - 
+	}*/
 
 	return FALSE;
 }
@@ -320,7 +335,7 @@
 		return FALSE;
 
 	// we need to make sure the new view is to scale
-	int w = abs(view->mouseX - view->mouseDownX);
+	/*int w = abs(view->mouseX - view->mouseDownX);
 	int h = abs(view->mouseY - view->mouseDownY);
 	// scale to height
 	if (fabs((double)w / h) <= fabs((double)view->width / view->height))
@@ -339,7 +354,7 @@
 			view->mouseY = view->mouseDownY - h;
 		else
 			view->mouseY = view->mouseDownY + h;
-	}
+	}*/
 
 	gtk_widget_queue_draw_area(widget, 0, 0, view->width, view->height);
 
@@ -353,8 +368,55 @@
 	gdk_threads_leave();
 }
 
-static void draw_world(GdkDrawable* drawable, GdkGC* gc, world_t* world)
+void to_world_coordinates(GtkEnvironmentView* view, int x, int y, float* world_x, float* world_y)
 {
+	double leftx = world.win.p1.x;
+	double upy = world.win.p1.x;
+	double xwidth = world.win.p2.x - leftx;
+	double yheight = world.win.p2.y - upy;
+	int width = view->width, height = view->height;
+	if (xwidth / width < yheight / height)
+		width = (int)(height / yheight * xwidth);
+	else
+		height = (int)(width / xwidth * yheight);
+
+	*world_x = (float)x / width * xwidth + leftx;
+	*world_y = (float)y / height * yheight + upy;
+}
+
+void to_window_coordinates(GtkEnvironmentView* view, float x, float y, int* win_x, int* win_y)
+{
+	double leftx = world.win.p1.x;
+	double upy = world.win.p1.x;
+	double xwidth = world.win.p2.x - leftx;
+	double yheight = world.win.p2.y - upy;
+	int width = view->width, height = view->height;
+	if (xwidth / width < yheight / height)
+		width = (int)(height / yheight * xwidth);
+	else
+		height = (int)(width / xwidth * yheight);
+
+	*win_x = (int)((x - leftx) / xwidth * width);
+	*win_y = (int)((y - upy) / yheight * height);
+}
+
+void to_window_scale(GtkEnvironmentView* view, float v, int* win_v)
+{
+	double leftx = world.win.p1.x;
+	double upy = world.win.p1.x;
+	double xwidth = world.win.p2.x - leftx;
+	double yheight = world.win.p2.y - upy;
+	int width = view->width, height = view->height;
+	if (xwidth / width < yheight / height)
+		width = (int)(height / yheight * xwidth);
+	else
+		height = (int)(width / xwidth * yheight);
+	
+	*win_v = (int)(v / xwidth * width);
+}
+
+static void draw_world(GtkEnvironmentView* view, GdkDrawable* drawable, GdkGC* gc, world_t* world)
+{
 	int i, j;
 	GdkPoint* points;
 	poly_t* p;
@@ -362,17 +424,6 @@
 	if (!drawable || !gc || !world)
 		return;
 
-	double leftx = world->win.p1.x;
-	double upy = world->win.p1.x;
-	double xwidth = world->win.p2.x - leftx;
-	double yheight = world->win.p2.y - upy;
-	int width, height;
-	gdk_drawable_get_size(drawable, &width, &height);
-	if (xwidth / width < yheight / height)
-		width = (int)(height / yheight * xwidth);
-	else
-		height = (int)(width / xwidth * yheight);
-
 	for (i = 0 ; i < world->max_objs; i++)
 	{
 		object_t* o = &(world->objs[i]);
@@ -382,10 +433,7 @@
 				p = (poly_t*)o->props;
 				points = (GdkPoint*)malloc(sizeof(GdkPoint) * p->num_pts);
 				for (j = 0; j < p->num_pts; j++)
-				{
-					points[j].x = (int)((p->pts[j].x - leftx) / xwidth * width);
-					points[j].y = (int)((p->pts[j].y - upy) / yheight * height);
-				}
+					to_window_coordinates(view, p->pts[j].x, p->pts[j].y, &(points[j].x), &(points[j].y));
 				switch(p->type)
 				{
 					case POLY_CONNECTED:
@@ -407,35 +455,23 @@
 	}
 }
 
-static void draw_robot(GdkDrawable* drawable, GdkGC* gc, 
+static void draw_robot(GtkEnvironmentView* view, GdkDrawable* drawable, GdkGC* gc, 
                         float x, float y, float angle)
 {
-	double leftx = world.win.p1.x;
-	double upy = world.win.p1.x;
-	double xwidth = world.win.p2.x - leftx;
-	double yheight = world.win.p2.y - upy;
-	double rx, ry;
-	int width, height;
-	
+	int rx, ry;
+	int rox, roy;
 	if (!drawable || !gc)
 		return;
-	gdk_drawable_get_size(drawable, &width, &height);
-	if (xwidth / width < yheight / height)
-		width = (int)(height / yheight * xwidth);
-	else
-		height = (int)(width / xwidth * yheight);
 	
-	rx = (double)ROBOT_DIAMETER / xwidth * width;
-	ry = (double)ROBOT_DIAMETER / yheight * height;
+	to_window_scale(view, ROBOT_DIAMETER, &rx);
+	to_window_scale(view, ROBOT_DIAMETER, &ry);
+	to_window_coordinates(view, x, y, &rox, &roy);
 
-	x = ((x - leftx) / xwidth * width);
-	y = ((y - upy) / yheight * height);
-
-	gdk_draw_arc(drawable, gc, FALSE, (int)(x - rx / 2), (int)(y - ry / 2), 
+	gdk_draw_arc(drawable, gc, FALSE, (int)(rox - (float)rx / 2), (int)(roy - (float)ry / 2), 
 		rx, ry, 0, 360*64);
 
-	gdk_draw_line(drawable, gc, x, y, 
-		x + (rx / 1.8) * cos(-angle),
-		y - (ry / 1.8) * sin(-angle));
+	gdk_draw_line(drawable, gc, rox, roy, 
+		rox + (rx / 1.8) * cos(-angle),
+		roy - (ry / 1.8) * sin(-angle));
 }
 
Index: branches/simulator/projects/simulator/simulator/gui/gtk_environment_view.h
===================================================================
--- branches/simulator/projects/simulator/simulator/gui/gtk_environment_view.h	(revision 1085)
+++ branches/simulator/projects/simulator/simulator/gui/gtk_environment_view.h	(revision 1086)
@@ -32,6 +32,8 @@
 	int mouseX;
 	int mouseY;
 	int mouseDown;
+
+	int selectedRobot;
 };
 
 struct _GtkEnvironmentViewClass
