Merge pull request #829 from matthid/pullVisualStudioDebuggerProblem

Fix for really annoying debugger exception + fixed whitespace
This commit is contained in:
Hylke Bons 2012-07-02 13:31:31 -07:00
commit 2a58657d6e

View file

@ -30,9 +30,9 @@ using Forms = System.Windows.Forms;
namespace SparkleShare { namespace SparkleShare {
[ContentProperty("Text")] [ContentProperty("Text")]
[DefaultEvent("MouseDoubleClick")] [DefaultEvent("MouseDoubleClick")]
public class SparkleNotifyIcon : FrameworkElement, IAddChild { public class SparkleNotifyIcon : FrameworkElement, IAddChild {
[DllImport("kernel32.dll", CharSet = CharSet.Auto)] [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr GetModuleHandle(string module_name); private static extern IntPtr GetModuleHandle(string module_name);
@ -45,7 +45,7 @@ namespace SparkleShare {
[DllImport("user32.dll", EntryPoint = "DestroyIcon")] [DllImport("user32.dll", EntryPoint = "DestroyIcon")]
static extern bool DestroyIcon (IntPtr hIcon); static extern bool DestroyIcon (IntPtr hIcon);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)] [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern int UnhookWindowsHookEx (int hook_id); private static extern int UnhookWindowsHookEx (int hook_id);
@ -60,12 +60,12 @@ namespace SparkleShare {
internal int Info; internal int Info;
} }
public Drawing.Bitmap Icon { public Drawing.Bitmap Icon {
set { set {
this.notify_icon.Icon = GetIconFromBitmap (value); this.notify_icon.Icon = GetIconFromBitmap (value);
} }
} }
public string HeaderText { public string HeaderText {
get { get {
@ -73,21 +73,21 @@ namespace SparkleShare {
} }
set { set {
header_text = value; header_text = value;
} }
} }
public string Text { public string Text {
get { get {
return (string) GetValue (TextProperty); return (string) GetValue (TextProperty);
} }
set { set {
string text = value; string text = value;
if (!string.IsNullOrEmpty (header_text)) if (!string.IsNullOrEmpty (header_text))
text = header_text + "\n" + text; text = header_text + "\n" + text;
SetValue (TextProperty, text); SetValue (TextProperty, text);
} }
} }
@ -114,27 +114,27 @@ namespace SparkleShare {
public readonly RoutedEvent MouseClickEvent = EventManager.RegisterRoutedEvent ( public readonly RoutedEvent MouseClickEvent = EventManager.RegisterRoutedEvent (
"MouseClick", RoutingStrategy.Bubble, typeof (MouseButtonEventHandler), typeof (SparkleNotifyIcon)); "MouseClick", RoutingStrategy.Bubble, typeof (MouseButtonEventHandler), typeof (SparkleNotifyIcon));
public readonly RoutedEvent MouseDoubleClickEvent = EventManager.RegisterRoutedEvent( public readonly RoutedEvent MouseDoubleClickEvent = EventManager.RegisterRoutedEvent(
"MouseDoubleClick", RoutingStrategy.Bubble, typeof (MouseButtonEventHandler), typeof (SparkleNotifyIcon)); "MouseDoubleClick", RoutingStrategy.Bubble, typeof (MouseButtonEventHandler), typeof (SparkleNotifyIcon));
public readonly DependencyProperty TextProperty = DependencyProperty.Register( public readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text", typeof(string), typeof (SparkleNotifyIcon), new PropertyMetadata (OnTextChanged)); "Text", typeof(string), typeof (SparkleNotifyIcon), new PropertyMetadata (OnTextChanged));
private string header_text; private string header_text;
private Forms.NotifyIcon notify_icon; private Forms.NotifyIcon notify_icon;
private HookProc hook_proc_ref; private HookProc hook_proc_ref;
private int mouse_hook_handle; private int mouse_hook_handle;
private delegate int HookProc (int code, int param, IntPtr struct_pointer); private delegate int HookProc (int code, int param, IntPtr struct_pointer);
public SparkleNotifyIcon () public SparkleNotifyIcon ()
{ {
VisibilityProperty.OverrideMetadata (typeof (SparkleNotifyIcon), VisibilityProperty.OverrideMetadata (typeof (SparkleNotifyIcon),
new PropertyMetadata (OnVisibilityChanged)); new PropertyMetadata (OnVisibilityChanged));
this.notify_icon = new Forms.NotifyIcon () { this.notify_icon = new Forms.NotifyIcon () {
Text = Text, Text = Text,
Visible = true Visible = true
@ -146,17 +146,17 @@ namespace SparkleShare {
this.notify_icon.MouseDoubleClick += OnMouseDoubleClick; this.notify_icon.MouseDoubleClick += OnMouseDoubleClick;
this.hook_proc_ref = OnMouseEventProc; this.hook_proc_ref = OnMouseEventProc;
} }
public void ShowBalloonTip (string title, string subtext, string image_path) public void ShowBalloonTip (string title, string subtext, string image_path)
{ {
// TODO: // TODO:
// - Use the image pointed to by image_path // - Use the image pointed to by image_path
// - Find a way to use the prettier (Win7?) balloons // - Find a way to use the prettier (Win7?) balloons
this.notify_icon.ShowBalloonTip (5 * 1000, title, subtext, Forms.ToolTipIcon.Info); this.notify_icon.ShowBalloonTip (5 * 1000, title, subtext, Forms.ToolTipIcon.Info);
} }
public void Dispose() public void Dispose()
{ {
@ -164,49 +164,49 @@ namespace SparkleShare {
} }
void IAddChild.AddChild (object value) void IAddChild.AddChild (object value)
{ {
throw new InvalidOperationException (); throw new InvalidOperationException ();
} }
void IAddChild.AddText (string text) void IAddChild.AddText (string text)
{ {
if (text == null) if (text == null)
throw new ArgumentNullException (); throw new ArgumentNullException ();
Text = text; Text = text;
} }
private static MouseButtonEventArgs CreateMouseButtonEventArgs( private static MouseButtonEventArgs CreateMouseButtonEventArgs(
RoutedEvent handler, Forms.MouseButtons button) RoutedEvent handler, Forms.MouseButtons button)
{ {
MouseButton mouse_button; MouseButton mouse_button;
if (button == Forms.MouseButtons.Left) { if (button == Forms.MouseButtons.Left) {
mouse_button = MouseButton.Left; mouse_button = MouseButton.Left;
} else if (button == Forms.MouseButtons.Right) { } else if (button == Forms.MouseButtons.Right) {
mouse_button = MouseButton.Right; mouse_button = MouseButton.Right;
} else if (button == Forms.MouseButtons.Middle) { } else if (button == Forms.MouseButtons.Middle) {
mouse_button = MouseButton.Middle; mouse_button = MouseButton.Middle;
} else if (button == Forms.MouseButtons.XButton1) { } else if (button == Forms.MouseButtons.XButton1) {
mouse_button = MouseButton.XButton1; mouse_button = MouseButton.XButton1;
} else if (button == Forms.MouseButtons.XButton2) { } else if (button == Forms.MouseButtons.XButton2) {
mouse_button = MouseButton.XButton2; mouse_button = MouseButton.XButton2;
} else { } else {
throw new InvalidOperationException (); throw new InvalidOperationException ();
} }
return new MouseButtonEventArgs (InputManager.Current.PrimaryMouseDevice, 0, mouse_button) { return new MouseButtonEventArgs (InputManager.Current.PrimaryMouseDevice, 0, mouse_button) {
RoutedEvent = handler RoutedEvent = handler
}; };
} }
private void ShowContextMenu () private void ShowContextMenu ()
@ -214,60 +214,72 @@ namespace SparkleShare {
if (ContextMenu != null) { if (ContextMenu != null) {
ContextMenu.Opened += OnContextMenuOpened; ContextMenu.Opened += OnContextMenuOpened;
ContextMenu.Closed += OnContextMenuClosed; ContextMenu.Closed += OnContextMenuClosed;
ContextMenu.Placement = PlacementMode.Mouse; ContextMenu.Placement = PlacementMode.Mouse;
ContextMenu.IsOpen = true; ContextMenu.IsOpen = true;
} }
} }
private Rect GetContextMenuRect (ContextMenu menu) private Rect GetContextMenuRect (ContextMenu menu)
{ {
Point start_point = menu.PointToScreen (new Point (0, 0)); var source = PresentationSource.FromVisual (menu);
Point end_point = menu.PointToScreen (new Point (menu.ActualWidth, menu.ActualHeight)); if (source != null) {
Point start_point = menu.PointToScreen(new Point(0, 0));
Point end_point = menu.PointToScreen(new Point(menu.ActualWidth, menu.ActualHeight));
return new Rect (start_point, end_point); return new Rect(start_point, end_point);
}
return new Rect();
} }
private Point GetHitPoint (IntPtr struct_pointer) private Point GetHitPoint (IntPtr struct_pointer)
{ {
MouseLLHook mouse_hook = (MouseLLHook) Marshal.PtrToStructure ( MouseLLHook mouse_hook = (MouseLLHook) Marshal.PtrToStructure (
struct_pointer, typeof (MouseLLHook)); struct_pointer, typeof (MouseLLHook));
return new Point (mouse_hook.X, mouse_hook.Y); return new Point (mouse_hook.X, mouse_hook.Y);
} }
private int OnMouseEventProc (int code, int button, IntPtr data_pointer) private int OnMouseEventProc (int code, int button, IntPtr data_pointer)
{ {
int left_button_down = 0x201; int left_button_down = 0x201;
int right_button_down = 0x204; int right_button_down = 0x204;
if (button == left_button_down || button == right_button_down) { int ret;
Rect context_menu_rect = GetContextMenuRect (ContextMenu); try {
Point hit_point = GetHitPoint (data_pointer); if (button == left_button_down || button == right_button_down)
{
Rect context_menu_rect = GetContextMenuRect(ContextMenu);
Point hit_point = GetHitPoint(data_pointer);
if (!context_menu_rect.Contains (hit_point)) if (!context_menu_rect.Contains(hit_point))
ContextMenu.IsOpen = false; ContextMenu.IsOpen = false;
} }
}
finally {
ret = CallNextHookEx(this.mouse_hook_handle, code, button, data_pointer);
}
return CallNextHookEx (this.mouse_hook_handle, code, button, data_pointer); return ret;
} }
private void OnContextMenuOpened (object sender, RoutedEventArgs args) private void OnContextMenuOpened (object sender, RoutedEventArgs args)
{ {
using (Process process = Process.GetCurrentProcess ()) using (Process process = Process.GetCurrentProcess ())
using (ProcessModule module = process.MainModule) using (ProcessModule module = process.MainModule)
{ {
this.mouse_hook_handle = SetWindowsHookEx (14, this.hook_proc_ref, this.mouse_hook_handle = SetWindowsHookEx (14, this.hook_proc_ref,
GetModuleHandle (module.ModuleName), 0); GetModuleHandle (module.ModuleName), 0);
} }
if (this.mouse_hook_handle == 0) if (this.mouse_hook_handle == 0)
throw new Win32Exception (Marshal.GetLastWin32Error ()); throw new Win32Exception (Marshal.GetLastWin32Error ());
} }
private void OnContextMenuClosed (object sender, RoutedEventArgs args) private void OnContextMenuClosed (object sender, RoutedEventArgs args)
@ -277,44 +289,44 @@ namespace SparkleShare {
ContextMenu.Opened -= OnContextMenuOpened; ContextMenu.Opened -= OnContextMenuOpened;
ContextMenu.Closed -= OnContextMenuClosed; ContextMenu.Closed -= OnContextMenuClosed;
} }
private void OnVisibilityChanged (DependencyObject target, private void OnVisibilityChanged (DependencyObject target,
DependencyPropertyChangedEventArgs args) DependencyPropertyChangedEventArgs args)
{ {
SparkleNotifyIcon control = (SparkleNotifyIcon) target; SparkleNotifyIcon control = (SparkleNotifyIcon) target;
control.notify_icon.Visible = (control.Visibility == Visibility.Visible); control.notify_icon.Visible = (control.Visibility == Visibility.Visible);
} }
private void OnMouseDown(object sender, Forms.MouseEventArgs args) private void OnMouseDown(object sender, Forms.MouseEventArgs args)
{ {
RaiseEvent (CreateMouseButtonEventArgs (MouseDownEvent, args.Button)); RaiseEvent (CreateMouseButtonEventArgs (MouseDownEvent, args.Button));
} }
private void OnMouseClick (object sender, Forms.MouseEventArgs args) private void OnMouseClick (object sender, Forms.MouseEventArgs args)
{ {
RaiseEvent (CreateMouseButtonEventArgs (MouseClickEvent, args.Button)); RaiseEvent (CreateMouseButtonEventArgs (MouseClickEvent, args.Button));
} }
private void OnMouseDoubleClick (object sender, Forms.MouseEventArgs args) private void OnMouseDoubleClick (object sender, Forms.MouseEventArgs args)
{ {
RaiseEvent (CreateMouseButtonEventArgs (MouseDoubleClickEvent, args.Button)); RaiseEvent (CreateMouseButtonEventArgs (MouseDoubleClickEvent, args.Button));
} }
private void OnMouseUp (object sender, Forms.MouseEventArgs args) private void OnMouseUp (object sender, Forms.MouseEventArgs args)
{ {
if (args.Button == Forms.MouseButtons.Left || if (args.Button == Forms.MouseButtons.Left ||
args.Button == Forms.MouseButtons.Right) { args.Button == Forms.MouseButtons.Right) {
ShowContextMenu (); ShowContextMenu ();
} }
RaiseEvent (CreateMouseButtonEventArgs (MouseUpEvent, args.Button)); RaiseEvent (CreateMouseButtonEventArgs (MouseUpEvent, args.Button));
} }
protected override void OnVisualParentChanged (DependencyObject parent) protected override void OnVisualParentChanged (DependencyObject parent)
@ -324,12 +336,12 @@ namespace SparkleShare {
private static void OnTextChanged (DependencyObject target, private static void OnTextChanged (DependencyObject target,
DependencyPropertyChangedEventArgs args) DependencyPropertyChangedEventArgs args)
{ {
SparkleNotifyIcon control = (SparkleNotifyIcon) target; SparkleNotifyIcon control = (SparkleNotifyIcon) target;
control.notify_icon.Text = control.Text; control.notify_icon.Text = control.Text;
} }
private Drawing.Icon GetIconFromBitmap (Drawing.Bitmap bitmap) private Drawing.Icon GetIconFromBitmap (Drawing.Bitmap bitmap)
{ {
@ -339,5 +351,5 @@ namespace SparkleShare {
return icon; return icon;
} }
} }
} }