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