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