]> git.cworth.org Git - akamaru/blobdiff - dock.c
Get rid of 1024x768 screen size assumption.
[akamaru] / dock.c
diff --git a/dock.c b/dock.c
index 54e4746774bffa9f92cd58471f57c1aece50c6d2..f46240686a3f77be586f5923d4a5251f3e6b4a93 100644 (file)
--- a/dock.c
+++ b/dock.c
@@ -20,8 +20,8 @@ struct Closure {
   int num_icons;
   GdkWindow **windows;
   int drag_offset_x, drag_offset_y;
-  int anchor_x, anchor_y;
   int spacing;
+  int height;
 };
 
 static gint
@@ -36,7 +36,8 @@ timeout_callback (gpointer data)
                     closure->model.objects[i + 1].position.y + 0.5);
   }
 
-  model_step (&closure->model, 0.1);
+  for (i = 0; i < 4; i++)
+    model_step (&closure->model, 0.03);
 
   return TRUE;
 }
@@ -56,12 +57,13 @@ create_window (GdkScreen *screen, int x, int y, int width, int height)
   attributes.y = y;
   attributes.width = width;
   attributes.height = height;
-  attributes.event_mask |=
+  attributes.event_mask =
     GDK_EXPOSURE_MASK |
     GDK_BUTTON_PRESS_MASK |
     GDK_BUTTON_RELEASE_MASK |
     GDK_ENTER_NOTIFY_MASK |
     GDK_LEAVE_NOTIFY_MASK |
+    GDK_POINTER_MOTION_MASK |
     GDK_POINTER_MOTION_HINT_MASK;
 
   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
@@ -71,12 +73,14 @@ create_window (GdkScreen *screen, int x, int y, int width, int height)
 }
 
 static void
-model_init_dock (Model *model, int num_items, int x, int y, int spacing)
+model_init_dock (Model *model, int num_items,
+                int width, int height, int spacing)
 {
   const int num_objects = num_items + 1;
   const int num_spacers = (num_objects - 1) * (num_objects - 2) / 2;
   const int num_springs = num_objects - 1;
-  int i, j;
+  const int spread = spacing + 20;
+  int i, j, left_edge;
   Object *object;
   Spring *spring;
   Spacer *spacer;
@@ -94,20 +98,24 @@ model_init_dock (Model *model, int num_items, int x, int y, int spacing)
 
   model->polygons = g_new (Polygon, 1);
   model->num_polygons = 1;
-  polygon_init_enclosing_rectangle (&model->polygons[0], 0, 0, 1024 - 50, y);
+  polygon_init_enclosing_rectangle (&model->polygons[0],
+                                   0, 0, width - 50, height - 50);
 
-  model->anchors[0].x = x;
-  model->anchors[0].y = y;
+  model->anchors[0].x = width / 2;
+  model->anchors[0].y = height - 50;
   model->anchors[0].object = &model->objects[0];
 
-  object_init (&model->objects[0], x, y, 20);
+  object_init (&model->objects[0],
+              model->anchors[0].x, model->anchors[0].y, 0);
 
   object = &model->objects[1];
   spring = model->springs;
   spacer = model->spacers;
+  left_edge = (width - (num_items - 1) * spread) / 2;
 
   for (i = 1; i < num_objects; i++, object++) {
-    object_init (&model->objects[i], 200 + i * spacing / 2, 300, 1);
+    object_init (&model->objects[i],
+                left_edge + (i - 1) * spread, height - 100, 1);
     spring_init (spring++, &model->objects[0], object, spacing);
     for (j = 1; j < num_objects - i; j++) {
       spacer_init (spacer++, object, object + j, spacing);
@@ -144,10 +152,10 @@ window_event (GdkXEvent *xevent, GdkEvent *event, gpointer data)
 
   case MotionNotify:
     gdk_window_get_pointer (gdk_get_default_root_window(), &x, &y, &state);
-    closure->model.mouse_anchor.x = x + 0.5 - closure->drag_offset_x;
-    closure->model.mouse_anchor.y = y + 0.5 - closure->drag_offset_y;
-    if (closure->model.mouse_anchor.y > closure->anchor_y)
-      closure->model.mouse_anchor.y = closure->anchor_y;
+    closure->model.mouse_anchor.x = x - closure->drag_offset_x;
+    closure->model.mouse_anchor.y = y - closure->drag_offset_y;
+    if (closure->model.mouse_anchor.y > closure->height)
+      closure->model.mouse_anchor.y = closure->height;
     break;
 
   default:
@@ -177,29 +185,32 @@ int main (int argc, char *argv[])
     RsvgHandle *handle;
     RsvgDimensionData dimension;
     cairo_t *cr;
-    const int screen_width = 1024, screen_height = 768, spacing = 50;
+    const int spacing = 50;
 
     gtk_init (&argc, &argv);
 
     rsvg_init ();
 
     screen = gdk_screen_get_default ();
+    width = gdk_screen_get_width (screen);
+    height = gdk_screen_get_height (screen);
 
-    closure.anchor_x = screen_width / 2;
-    closure.anchor_y = screen_height - 50;
     closure.spacing = spacing;
+    closure.height = height - 50;
     closure.num_icons = num_icons;
     closure.windows = g_new (GdkWindow *, num_icons);
+
+    model_init_dock (&closure.model, num_icons, width, height, spacing);
+
     for (i = 0; i < num_icons; i++) {
 
       handle = rsvg_handle_new_from_file (icons[i], NULL);
       rsvg_handle_get_dimensions (handle, &dimension);
 
-      x = (screen_width - spacing * num_icons) / 2 + i * spacing;
-      y = closure.anchor_y;
-      width = dimension.width;
-      height = dimension.height;
-      closure.windows[i] = create_window (screen, x, y, width, height);
+      x = closure.model.objects[i + 1].position.x;
+      y = closure.model.objects[i + 1].position.y;
+      closure.windows[i] =
+       create_window (screen, x, y, dimension.width, dimension.height);
 
       gdk_window_show (closure.windows[i]);
 
@@ -214,8 +225,6 @@ int main (int argc, char *argv[])
       gdk_window_add_filter (closure.windows[i], window_event, &closure);
     }
 
-    model_init_dock (&closure.model, num_icons,
-                    closure.anchor_x, closure.anchor_y, spacing);
     g_timeout_add (20, timeout_callback, &closure);
 
     gtk_main ();