     }
   }
 
+  let CLICK_RADIUS = 5
+  let CLICK_TIMESPAN = 150 // ms
+
+  func checkForClick(mouseDownEv: SDL_Event, mouseUpEv: SDL_Event) {
+    let d = mouseDownEv.button
+    let u = mouseUpEv.button
+    if (Int(u.timestamp) - Int(d.timestamp) <= CLICK_TIMESPAN) &&
+        u.clicks == 1 &&
+        (u.x >= d.x - CLICK_RADIUS) && (u.x <= d.x + CLICK_RADIUS) &&
+        (u.y >= d.y - CLICK_RADIUS) && (u.y <= d.y + CLICK_RADIUS) {
+      signal(.MouseClick, ev: mouseDownEv)
+      p("accepted the click", mouseDownEv.button)
+    }
+  }
+
   // Returns the id that can be used for removing the handler.
   public func on(eventType: SEventType, fn: SEventHandler) -> Int {
     var a = customEvents[eventType]

  case MouseButtonDown
  case MouseButtonUp
  case MouseWheel
}



            }
          case MOUSEBUTTONDOWN:
            p("mouse button down \(ev.button.x) \(ev.button.y) \(ev.button.clicks)")
            lastMouseMouseDown = ev
          case MOUSEBUTTONUP:
            checkForClick(lastMouseMouseDown, mouseUpEv: ev)
            p("mouse button up \(ev.button.x) \(ev.button.y) \(ev.button.clicks)")
            p("mainDiv \(mainDiv.lastx) \(mainDiv.lasty) \(mainDiv.lastSize.width) \(mainDiv.lastSize.height)")
            if let cp = textGrid.pointToCell(ev.button.x, y: ev.button.y) {
