Gdk.Rectangle GetUsableMonitorGeometry(Gdk.Rectangle caret) { Screen screen = null; if (parent != null) { screen = parent.Screen; } else if (xwtParent != null) { screen = Gdk.Screen.Default; // FIXME: should we try to get the Screen from the backend? } if (screen != null) { return(GtkWorkarounds.GetUsableMonitorGeometry(screen, screen.GetMonitorAtPoint(caret.X, caret.Y))); } return(Gdk.Rectangle.Zero); }
void PositionWidget(Gtk.Widget widget) { if (!(widget is Gtk.Window)) { return; } int ox, oy; ParentWindow.GetOrigin(out ox, out oy); int w; int itemXPosition = GetHoverXPosition(out w); int dx = ox + this.Allocation.X + itemXPosition; int dy = oy + this.Allocation.Bottom; var req = widget.SizeRequest(); Gdk.Rectangle geometry = GtkWorkarounds.GetUsableMonitorGeometry(Screen, Screen.GetMonitorAtPoint(dx, dy)); int width = System.Math.Max(req.Width, w); if (width >= geometry.Width - spacing * 2) { width = geometry.Width - spacing * 2; dx = geometry.Left + spacing; } widget.WidthRequest = width; if (dy + req.Height > geometry.Bottom) { dy = oy + this.Allocation.Y - req.Height; } if (dx + width > geometry.Right) { dx = geometry.Right - width; } (widget as Gtk.Window).Move(dx, dy); (widget as Gtk.Window).Resize(width, req.Height); widget.GrabFocus(); }
public virtual void RepositionWindow(Gdk.Rectangle?newCaret = null) { if (parent == null) { return; } int x, y; if (newCaret.HasValue) //Update caret if parameter is given { currentCaret = newCaret.Value; } Gdk.Rectangle caret = currentCaret; Gdk.Window window = targetWindow; if (targetWindow == null) { return; } PopupPosition position = Theme.TargetPosition; this.position = Theme.TargetPosition; UpdatePadding(); window.GetOrigin(out x, out y); var alloc = parent.Allocation; if (eventProvided) { caret.X = x; caret.Y = y; caret.Width = caret.Height = 1; } else { if (caret.Equals(Gdk.Rectangle.Zero)) { caret = new Gdk.Rectangle(0, 0, alloc.Width, alloc.Height); } caret = GtkUtil.ToScreenCoordinates(parent, parent.GdkWindow, caret); } Gtk.Requisition request = SizeRequest(); var screen = parent.Screen; Gdk.Rectangle geometry = GtkWorkarounds.GetUsableMonitorGeometry(screen, screen.GetMonitorAtPoint(caret.X, caret.Y)); // Add some spacing between the screen border and the popover window geometry.Inflate(-5, -5); // Flip the orientation if the window doesn't fit the screen. int intPos = (int)position; switch ((PopupPosition)(intPos & 0x0f)) { case PopupPosition.Top: if (caret.Bottom + request.Height > geometry.Bottom) { intPos = (intPos & 0xf0) | (int)PopupPosition.Bottom; } break; case PopupPosition.Bottom: if (caret.Top - request.Height < geometry.X) { intPos = (intPos & 0xf0) | (int)PopupPosition.Top; } break; case PopupPosition.Right: if (caret.X - request.Width < geometry.X) { intPos = (intPos & 0xf0) | (int)PopupPosition.Left; } break; case PopupPosition.Left: if (caret.Right + request.Width > geometry.Right) { intPos = (intPos & 0xf0) | (int)PopupPosition.Right; } break; } position = (PopupPosition)intPos; UpdatePadding(); // Calculate base coordinate switch ((PopupPosition)((int)position & 0x0f)) { case PopupPosition.Top: y = caret.Bottom; break; case PopupPosition.Bottom: y = caret.Y - request.Height; break; case PopupPosition.Right: x = caret.X - request.Width; break; case PopupPosition.Left: x = caret.Right; break; } int offset; if ((position & PopupPosition.Top) != 0 || (position & PopupPosition.Bottom) != 0) { if (((int)position & 0x10) != 0) { x = caret.X - MinArrowSpacing - Theme.ArrowWidth / 2; } else if (((int)position & 0x20) != 0) { x = caret.Right - request.Width + MinArrowSpacing + Theme.ArrowWidth / 2; } else { x = caret.X + (caret.Width - request.Width) / 2; } if (x < geometry.Left) { x = geometry.Left; } else if (x + request.Width > geometry.Right) { x = geometry.Right - request.Width; } offset = caret.X + caret.Width / 2 - x; if (offset - Theme.ArrowWidth / 2 < MinArrowSpacing) { offset = MinArrowSpacing + Theme.ArrowWidth / 2; } if (offset > request.Width - MinArrowSpacing - Theme.ArrowWidth / 2) { offset = request.Width - MinArrowSpacing - Theme.ArrowWidth / 2; } } else { if (((int)position & 0x10) != 0) { y = caret.Y - MinArrowSpacing - Theme.ArrowWidth / 2; } else if (((int)position & 0x20) != 0) { y = caret.Bottom - request.Height + MinArrowSpacing + Theme.ArrowWidth / 2; } else { y = caret.Y + (caret.Height - request.Height) / 2; } if (y < geometry.Top) { y = geometry.Top; } else if (y + request.Height > geometry.Bottom) { y = geometry.Bottom - request.Height; } if (MaximumYTopBound > 0) { y = Math.Max(MaximumYTopBound, y); } offset = caret.Y + caret.Height / 2 - y; if (offset - Theme.ArrowWidth / 2 < MinArrowSpacing) { offset = MinArrowSpacing + Theme.ArrowWidth / 2; } if (offset > request.Height - MinArrowSpacing - Theme.ArrowWidth / 2) { offset = request.Height - MinArrowSpacing - Theme.ArrowWidth / 2; } } Theme.ArrowOffset = offset; this.position = position; UpdatePadding(); Move(x, y); Show(); if (!ShowWindowShadow) { DesktopService.RemoveWindowShadow(this); } }