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