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