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