Skip to content

Commit

Permalink
Fixes #65 by properly using parameters provided to CustomPopupPlaceme…
Browse files Browse the repository at this point in the history
…ntMethod
  • Loading branch information
batzen committed Feb 20, 2015
1 parent dd77c5f commit e3b324f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 57 deletions.
85 changes: 41 additions & 44 deletions Fluent/Controls/RibbonTabControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,6 @@ public override void OnApplyTemplate()
this.DropDownPopup = this.Template.FindName("PART_Popup", this) as Popup;
if (this.DropDownPopup != null)
{
/*Binding binding = new Binding("IsOpen");
binding.Mode = BindingMode.TwoWay;
binding.Source = this;
popup.SetBinding(Popup.IsOpenProperty, binding);
*/
this.DropDownPopup.CustomPopupPlacementCallback = this.CustomPopupPlacementMethod;
}

Expand Down Expand Up @@ -731,54 +726,56 @@ private void OnRibbonTabPopupOpening()
/// <returns></returns>
private CustomPopupPlacement[] CustomPopupPlacementMethod(Size popupsize, Size targetsize, Point offset)
{
if (this.DropDownPopup != null
&& this.SelectedTabItem != null)
if (this.DropDownPopup == null
|| this.SelectedTabItem == null)
{
// Get current workarea
var tabItemPos = this.SelectedTabItem.PointToScreen(new Point(0, 0));
var tabItemRect = new RECT
{
left = (int)tabItemPos.X,
top = (int)tabItemPos.Y,
right = (int)tabItemPos.X + (int)this.SelectedTabItem.ActualWidth,
bottom = (int)tabItemPos.Y + (int)this.SelectedTabItem.ActualHeight
};
return null;
}

const uint MONITOR_DEFAULTTONEAREST = 0x00000002;
// Get current workarea
var tabItemPos = this.SelectedTabItem.PointToScreen(new Point(0, 0));
var tabItemRect = new RECT
{
left = (int)tabItemPos.X,
top = (int)tabItemPos.Y,
right = (int)tabItemPos.X + (int)this.SelectedTabItem.ActualWidth,
bottom = (int)tabItemPos.Y + (int)this.SelectedTabItem.ActualHeight
};

var monitor = NativeMethods.MonitorFromRect(ref tabItemRect, MONITOR_DEFAULTTONEAREST);
if (monitor != IntPtr.Zero)
{
var monitorInfo = new MONITORINFO();
monitorInfo.cbSize = Marshal.SizeOf(monitorInfo);
UnsafeNativeMethods.GetMonitorInfo(monitor, monitorInfo);
const uint MONITOR_DEFAULTTONEAREST = 0x00000002;

var startPoint = this.PointToScreen(new Point(0, 0));
if (this.FlowDirection == FlowDirection.RightToLeft)
{
startPoint.X -= this.ActualWidth;
}
var monitor = NativeMethods.MonitorFromRect(ref tabItemRect, MONITOR_DEFAULTTONEAREST);
if (monitor == IntPtr.Zero)
{
return null;
}

var inWindowRibbonWidth = monitorInfo.rcWork.right - Math.Max(monitorInfo.rcWork.left, startPoint.X);
var monitorInfo = new MONITORINFO();
monitorInfo.cbSize = Marshal.SizeOf(monitorInfo);
UnsafeNativeMethods.GetMonitorInfo(monitor, monitorInfo);

var actualWidth = this.ActualWidth;
if (startPoint.X < monitorInfo.rcWork.left)
{
actualWidth -= monitorInfo.rcWork.left - startPoint.X;
startPoint.X = monitorInfo.rcWork.left;
}
var startPoint = this.PointToScreen(new Point(0, 0));
if (this.FlowDirection == FlowDirection.RightToLeft)
{
startPoint.X -= this.ActualWidth;
}

// Set width and prevent negative values
this.DropDownPopup.Width = Math.Max(0, Math.Min(actualWidth, inWindowRibbonWidth));
return new[]
{
new CustomPopupPlacement(new Point(startPoint.X - tabItemPos.X, this.SelectedTabItem.ActualHeight - ((FrameworkElement)this.DropDownPopup.Child).Margin.Top), PopupPrimaryAxis.None),
new CustomPopupPlacement(new Point(startPoint.X - tabItemPos.X, -((ScrollViewer)this.SelectedContent).ActualHeight - ((FrameworkElement)this.DropDownPopup.Child).Margin.Bottom), PopupPrimaryAxis.None)
};
}
var inWindowRibbonWidth = monitorInfo.rcWork.right - Math.Max(monitorInfo.rcWork.left, startPoint.X);

var actualWidth = this.ActualWidth;
if (startPoint.X < monitorInfo.rcWork.left)
{
actualWidth -= monitorInfo.rcWork.left - startPoint.X;
startPoint.X = monitorInfo.rcWork.left;
}

return null;
// Set width and prevent negative values
this.DropDownPopup.Width = Math.Max(0, Math.Min(actualWidth, inWindowRibbonWidth));
return new[]
{
new CustomPopupPlacement(new Point(startPoint.X - tabItemPos.X + offset.X, targetsize.Height + offset.Y), PopupPrimaryAxis.Vertical),
new CustomPopupPlacement(new Point(startPoint.X - tabItemPos.X + offset.X, -1 * (targetsize.Height + offset.Y + ((ScrollViewer)this.SelectedContent).ActualHeight)), PopupPrimaryAxis.Vertical)
};
}

// Handles IsDropDownOpen property changed
Expand Down
7 changes: 3 additions & 4 deletions Fluent/Themes/Office2010/Controls/RibbonTabControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -501,16 +501,15 @@
</Grid>
<Popup x:Name="PART_Popup"
PlacementTarget="{Binding Path=SelectedTabItem,RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
Placement="Custom"
StaysOpen="True"
AllowsTransparency="True"
PopupAnimation="Fade"
VerticalOffset="-1"
IsOpen="{TemplateBinding IsDropDownOpen}"
HorizontalOffset="0"
Placement="Custom">
HorizontalOffset="0">
<Grid Width="Auto"
Height="Auto"
Margin="0,20">
Height="Auto">
<Grid.RowDefinitions>
<RowDefinition Height="94" />
<RowDefinition Height="5" />
Expand Down
8 changes: 3 additions & 5 deletions Fluent/Themes/Office2013/Controls/RibbonTabControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -344,17 +344,15 @@
</Grid>
<Popup x:Name="PART_Popup"
PlacementTarget="{Binding Path=SelectedTabItem,RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
Placement="Custom"
StaysOpen="True"
AllowsTransparency="True"
PopupAnimation="Slide"
VerticalOffset="-1"
IsOpen="{TemplateBinding IsDropDownOpen}"
HorizontalOffset="0"
Placement="Custom">

HorizontalOffset="0">
<Grid Width="Auto"
Height="Auto"
Margin="0,20">
Height="Auto">
<Grid.RowDefinitions>
<RowDefinition Height="94" />
<RowDefinition Height="1" />
Expand Down
7 changes: 3 additions & 4 deletions Fluent/Themes/Windows8/Controls/RibbonTabControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,15 @@
</Grid>
<Popup x:Name="PART_Popup"
PlacementTarget="{Binding Path=SelectedTabItem,RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
Placement="Custom"
StaysOpen="True"
AllowsTransparency="True"
PopupAnimation="Fade"
VerticalOffset="-1"
IsOpen="{TemplateBinding IsDropDownOpen}"
HorizontalOffset="0"
Placement="Custom">
HorizontalOffset="0">
<Grid Width="Auto"
Height="Auto"
Margin="0,20">
Height="Auto">
<Grid.RowDefinitions>
<RowDefinition Height="94" />
<RowDefinition Height="5" />
Expand Down

0 comments on commit e3b324f

Please sign in to comment.