c078ec
diff --git a/gtk/gtktooltip.c b/gtk/gtktooltip.c
c078ec
index 5cc2334..204a2b6 100644
c078ec
--- a/gtk/gtktooltip.c
c078ec
+++ b/gtk/gtktooltip.c
c078ec
@@ -903,53 +903,128 @@ gtk_tooltip_position (GtkTooltip *tooltip,
c078ec
 {
c078ec
   gint x, y;
c078ec
   GdkScreen *screen;
c078ec
+  gint monitor_num;
c078ec
+  GdkRectangle monitor;
c078ec
+  GtkRequisition requisition;
c078ec
+  guint cursor_size;
c078ec
+  GdkRectangle bounds;
c078ec
+
c078ec
+#define MAX_DISTANCE 32
c078ec
 
c078ec
   tooltip->tooltip_widget = new_tooltip_widget;
c078ec
 
c078ec
+  screen = gtk_widget_get_screen (new_tooltip_widget);
c078ec
+
c078ec
+  gtk_widget_size_request (GTK_WIDGET (tooltip->current_window), &requisition);
c078ec
+
c078ec
+  monitor_num = gdk_screen_get_monitor_at_point (screen,
c078ec
+                                                 tooltip->last_x,
c078ec
+                                                 tooltip->last_y);
c078ec
+  gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
c078ec
+
c078ec
+  get_bounding_box (new_tooltip_widget, &bounds);
c078ec
+
c078ec
   /* Position the tooltip */
c078ec
-  /* FIXME: should we swap this when RTL is enabled? */
c078ec
-  if (tooltip->keyboard_mode_enabled)
c078ec
+
c078ec
+  cursor_size = gdk_display_get_default_cursor_size (display);
c078ec
+
c078ec
+  /* Try below */
c078ec
+  x = bounds.x + bounds.width / 2 - requisition.width / 2;
c078ec
+  y = bounds.y + bounds.height + 4;
c078ec
+
c078ec
+  if (y + requisition.height <= monitor.y + monitor.height)
c078ec
     {
c078ec
-      GdkRectangle bounds;
c078ec
+      if (tooltip->keyboard_mode_enabled)
c078ec
+        goto found;
c078ec
 
c078ec
-      get_bounding_box (new_tooltip_widget, &bounds);
c078ec
+      if (y <= tooltip->last_y + cursor_size + MAX_DISTANCE)
c078ec
+        {
c078ec
+          if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
c078ec
+            x = tooltip->last_x + cursor_size + MAX_DISTANCE;
c078ec
+          else if (x + requisition.width < tooltip->last_x - MAX_DISTANCE)
c078ec
+            x = tooltip->last_x - MAX_DISTANCE - requisition.width;
c078ec
 
c078ec
-      /* For keyboard mode we position the tooltip below the widget,
c078ec
-       * right of the center of the widget.
c078ec
-       */
c078ec
-      x = bounds.x + bounds.width / 2;
c078ec
-      y = bounds.y + bounds.height + 4;
c078ec
+          goto found;
c078ec
+        }
c078ec
+   }
c078ec
+
c078ec
+  /* Try above */
c078ec
+  x = bounds.x + bounds.width / 2 - requisition.width / 2;
c078ec
+  y = bounds.y - requisition.height - 4;
c078ec
+
c078ec
+  if (y >= monitor.y)
c078ec
+    {
c078ec
+      if (tooltip->keyboard_mode_enabled)
c078ec
+        goto found;
c078ec
+
c078ec
+      if (y + requisition.height >= tooltip->last_y - MAX_DISTANCE)
c078ec
+        {
c078ec
+          if (tooltip->last_x + cursor_size + MAX_DISTANCE < x)
c078ec
+            x = tooltip->last_x + cursor_size + MAX_DISTANCE;
c078ec
+          else if (x + requisition.width < tooltip->last_x - MAX_DISTANCE)
c078ec
+            x = tooltip->last_x - MAX_DISTANCE - requisition.width;
c078ec
+
c078ec
+          goto found;
c078ec
+        }
c078ec
     }
c078ec
-  else
c078ec
+
c078ec
+  /* Try right FIXME: flip on rtl ? */
c078ec
+  x = bounds.x + bounds.width + 4;
c078ec
+  y = bounds.y + bounds.height / 2 - requisition.height / 2;
c078ec
+
c078ec
+  if (x + requisition.width <= monitor.x + monitor.width)
c078ec
     {
c078ec
-      guint cursor_size;
c078ec
+      if (tooltip->keyboard_mode_enabled)
c078ec
+        goto found;
c078ec
 
c078ec
-      x = tooltip->last_x;
c078ec
-      y = tooltip->last_y;
c078ec
+      if (x <= tooltip->last_x + cursor_size + MAX_DISTANCE)
c078ec
+        {
c078ec
+          if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
c078ec
+            y = tooltip->last_y + cursor_size + MAX_DISTANCE;
c078ec
+          else if (y + requisition.height < tooltip->last_y - MAX_DISTANCE)
c078ec
+            y = tooltip->last_y - MAX_DISTANCE - requisition.height;
c078ec
 
c078ec
-      /* For mouse mode, we position the tooltip right of the cursor,
c078ec
-       * a little below the cursor's center.
c078ec
-       */
c078ec
-      cursor_size = gdk_display_get_default_cursor_size (display);
c078ec
-      x += cursor_size / 2;
c078ec
-      y += cursor_size / 2;
c078ec
+          goto found;
c078ec
+        }
c078ec
     }
c078ec
 
c078ec
-  screen = gtk_widget_get_screen (new_tooltip_widget);
c078ec
+  /* Try left FIXME: flip on rtl ? */
c078ec
+  x = bounds.x - requisition.width - 4;
c078ec
+  y = bounds.y + bounds.height / 2 - requisition.height / 2;
c078ec
 
c078ec
-  /* Show it */
c078ec
-  if (tooltip->current_window)
c078ec
+  if (x >= monitor.x)
c078ec
     {
c078ec
-      gint monitor_num;
c078ec
-      GdkRectangle monitor;
c078ec
-      GtkRequisition requisition;
c078ec
+      if (tooltip->keyboard_mode_enabled)
c078ec
+        goto found;
c078ec
 
c078ec
-      gtk_widget_size_request (GTK_WIDGET (tooltip->current_window),
c078ec
-                               &requisition);
c078ec
+      if (x + requisition.width >= tooltip->last_x - MAX_DISTANCE)
c078ec
+        {
c078ec
+          if (tooltip->last_y + cursor_size + MAX_DISTANCE < y)
c078ec
+            y = tooltip->last_y + cursor_size + MAX_DISTANCE;
c078ec
+          else if (y + requisition.height < tooltip->last_y - MAX_DISTANCE)
c078ec
+            y = tooltip->last_y - MAX_DISTANCE - requisition.height;
c078ec
 
c078ec
-      monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
c078ec
-      gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
c078ec
+          goto found;
c078ec
+        }
c078ec
+    }
c078ec
 
c078ec
+   /* Fallback */
c078ec
+  if (tooltip->keyboard_mode_enabled)
c078ec
+    {
c078ec
+      x = bounds.x + bounds.width / 2 - requisition.width / 2;
c078ec
+      y = bounds.y + bounds.height + 4;
c078ec
+    }
c078ec
+  else
c078ec
+    {
c078ec
+      /* At cursor */
c078ec
+      x = tooltip->last_x + cursor_size * 3 / 4;
c078ec
+      y = tooltip->last_y + cursor_size * 3 / 4;
c078ec
+    }
c078ec
+
c078ec
+found:
c078ec
+  /* Show it */
c078ec
+  if (tooltip->current_window)
c078ec
+    {
c078ec
       if (x + requisition.width > monitor.x + monitor.width)
c078ec
         x -= x - (monitor.x + monitor.width) + requisition.width;
c078ec
       else if (x < monitor.x)
c078ec
@@ -957,7 +1032,9 @@ gtk_tooltip_position (GtkTooltip *tooltip,
c078ec
 
c078ec
       if (y + requisition.height > monitor.y + monitor.height)
c078ec
         y -= y - (monitor.y + monitor.height) + requisition.height;
c078ec
-  
c078ec
+      else if (y < monitor.y)
c078ec
+        y = monitor.y;
c078ec
+
c078ec
       if (!tooltip->keyboard_mode_enabled)
c078ec
         {
c078ec
           /* don't pop up under the pointer */
c078ec
@@ -965,7 +1042,7 @@ gtk_tooltip_position (GtkTooltip *tooltip,
c078ec
               y <= tooltip->last_y && tooltip->last_y < y + requisition.height)
c078ec
             y = tooltip->last_y - requisition.height - 2;
c078ec
         }
c078ec
-  
c078ec
+
c078ec
       gtk_window_move (GTK_WINDOW (tooltip->current_window), x, y);
c078ec
       gtk_widget_show (GTK_WIDGET (tooltip->current_window));
c078ec
     }