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