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