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