/// <summary> /// Refresh the dialog /// </summary> private void Refresh() { // Reset the controls if (controlFocus != null) controlFocus.OnFocusOut(); if (controlMouseOver != null) controlMouseOver.OnMouseExit(); controlFocus = null; controlMouseDown = null; controlMouseOver = null; // Refresh any controls foreach(Control c in controlList) { c.Refresh(); } if (usingKeyboardInput) FocusDefaultControl(); }
/// <summary> /// Cycles focus to the next available control /// </summary> private void OnCycleFocus(bool forward) { // This should only be handled by the dialog which owns the focused control, and // only if a control currently has focus if (controlFocus == null || controlFocus.Parent != this ) return; Control control = controlFocus; // Go through a bunch of controls for (int i = 0; i < 0xffff; i++) { control = (forward) ? GetNextControl(control) : GetPreviousControl(control); // If we've gone in a full circle, focus won't change if (control == controlFocus) return; // If the dialog accepts keybord input and the control can have focus then // move focus if (control.Parent.IsUsingKeyboardInput && control.CanHaveFocus) { controlFocus.OnFocusOut(); controlFocus = control; controlFocus.OnFocusIn(); return; } } throw new InvalidOperationException("Multiple dialogs are improperly chained together."); }
/// <summary> /// Handle mouse moves /// </summary> private void OnMouseMove(System.Drawing.Point pt) { // If the mouse was previously hovering over a control, it's either // still over the control or has left if (controlMouseDown != null) { // If another dialog owns this control then let that dialog handle it if (controlMouseDown.Parent != this ) return; // If the same control is still under the mouse, nothing needs to be done if (controlMouseDown.ContainsPoint(pt)) return; // Mouse has moved outside the control, notify the control and continue controlMouseDown.OnMouseExit(); controlMouseDown = null; } // Figure out which control the mouse is over now Control control = GetControlAtPoint(pt); if (control != null) { controlMouseDown = control; controlMouseDown.OnMouseEnter(); } }
/// <summary> /// Gets the previous control /// </summary> private static Control GetPreviousControl(Control control) { int index = (int)control.index - 1; Dialog dialog = control.Parent; // Cycle through dialogs in the loop to find the next control. Note // that if only one control exists in all looped dialogs it will // be the returned 'previous' control. while (index < 0) { dialog = dialog.prevDialog; if (dialog == null) dialog = control.Parent; index = dialog.controlList.Count - 1; } return dialog.controlList[index] as Control; }
/// <summary> /// Sets focus to the default control of a dialog /// </summary> private void FocusDefaultControl() { // Check for a default control in this dialog foreach(Control c in controlList) { if (c.isDefault) { // Remove focus from the current control ClearFocus(); // Give focus to the default control controlFocus = c; controlFocus.OnFocusIn(); return; } } }
/// <summary>Removes all controls from this dialog</summary> public void RemoveAllControls() { controlList.Clear(); if ( (controlFocus != null) && (controlFocus.Parent == this) ) controlFocus = null; controlMouseOver = null; }
/// <summary> /// Gets the next control /// </summary> private static Control GetNextControl(Control control) { int index = (int)control.index + 1; Dialog dialog = control.Parent; // Cycle through dialogs in the loop to find the next control. Note // that if only one control exists in all looped dialogs it will // be the returned 'next' control. while (index >= (int)dialog.controlList.Count) { dialog = dialog.nextDialog; index = 0; } return dialog.controlList[index] as Control; }
/// <summary> /// Handle messages for this dialog /// </summary> public bool MessageProc(IntPtr hWnd, NativeMethods.WindowMessage msg, IntPtr wParam, IntPtr lParam) { // If caption is enable, check for clicks in the caption area. if (hasCaption) { if (msg == NativeMethods.WindowMessage.LeftButtonDown || msg == NativeMethods.WindowMessage.LeftButtonDoubleClick) { // Current mouse position short mouseX = NativeMethods.LoWord((uint)lParam.ToInt32()); short mouseY = NativeMethods.HiWord((uint)lParam.ToInt32()); if (mouseX >= dialogX && mouseX < dialogX + width && mouseY >= dialogY && mouseY < dialogY + captionHeight) { isDragging = true; NativeMethods.SetCapture(hWnd); return true; } } else if ( (msg == NativeMethods.WindowMessage.LeftButtonUp) && isDragging) { // Current mouse position short mouseX = NativeMethods.LoWord((uint)lParam.ToInt32()); short mouseY = NativeMethods.HiWord((uint)lParam.ToInt32()); if (mouseX >= dialogX && mouseX < dialogX + width && mouseY >= dialogY && mouseY < dialogY + captionHeight) { NativeMethods.ReleaseCapture(); isDragging = false; return true; } } } // If the dialog is minimized, don't send any messages to controls. if (isDialogMinimized) return false; // If a control is in focus, it belongs to this dialog, and it's enabled, then give // it the first chance at handling the message. if (controlFocus != null && controlFocus.Parent == this && controlFocus.IsEnabled) { // If the control MsgProc handles it, then we don't. if (controlFocus.MsgProc(hWnd, msg, wParam, lParam)) return true; } switch(msg) { // Call OnFocusIn()/OnFocusOut() of the control that currently has the focus // as the application is activated/deactivated. This matches the Windows // behavior. case NativeMethods.WindowMessage.ActivateApplication: { if (controlFocus != null && controlFocus.Parent == this && controlFocus.IsEnabled) { if (wParam != IntPtr.Zero) controlFocus.OnFocusIn(); else controlFocus.OnFocusOut(); } } break; // Keyboard messages case NativeMethods.WindowMessage.KeyDown: case NativeMethods.WindowMessage.SystemKeyDown: case NativeMethods.WindowMessage.KeyUp: case NativeMethods.WindowMessage.SystemKeyUp: { // If a control is in focus, it belongs to this dialog, and it's enabled, then give // it the first chance at handling the message. if (controlFocus != null && controlFocus.Parent == this && controlFocus.IsEnabled) { // If the control MsgProc handles it, then we don't. if (controlFocus.HandleKeyboard(msg, wParam, lParam)) return true; } // Not yet handled, see if this matches a control's hotkey if (msg == NativeMethods.WindowMessage.KeyUp) { foreach(Control c in controlList) { // Was the hotkey hit? if (c.Hotkey == (System.Windows.Forms.Keys)wParam.ToInt32()) { // Yup! c.OnHotKey(); return true; } } } if (msg == NativeMethods.WindowMessage.KeyDown) { // If keyboard input is not enabled, this message should be ignored if (!usingKeyboardInput) return false; System.Windows.Forms.Keys key = (System.Windows.Forms.Keys)wParam.ToInt32(); switch(key) { case System.Windows.Forms.Keys.Right: case System.Windows.Forms.Keys.Down: if (controlFocus != null) { OnCycleFocus(true); return true; } break; case System.Windows.Forms.Keys.Left: case System.Windows.Forms.Keys.Up: if (controlFocus != null) { OnCycleFocus(false); return true; } break; case System.Windows.Forms.Keys.Tab: if (controlFocus == null) { FocusDefaultControl(); } else { bool shiftDown = NativeMethods.IsKeyDown(System.Windows.Forms.Keys.ShiftKey); OnCycleFocus(!shiftDown); } return true; } } } break; // Mouse messages case NativeMethods.WindowMessage.MouseMove: case NativeMethods.WindowMessage.MouseWheel: case NativeMethods.WindowMessage.LeftButtonUp: case NativeMethods.WindowMessage.LeftButtonDown: case NativeMethods.WindowMessage.LeftButtonDoubleClick: case NativeMethods.WindowMessage.RightButtonUp: case NativeMethods.WindowMessage.RightButtonDown: case NativeMethods.WindowMessage.RightButtonDoubleClick: case NativeMethods.WindowMessage.MiddleButtonUp: case NativeMethods.WindowMessage.MiddleButtonDown: case NativeMethods.WindowMessage.MiddleButtonDoubleClick: case NativeMethods.WindowMessage.XButtonUp: case NativeMethods.WindowMessage.XButtonDown: case NativeMethods.WindowMessage.XButtonDoubleClick: { // If not accepting mouse input, return false to indicate the message should still // be handled by the application (usually to move the camera). if (!usingMouseInput) return false; // Current mouse position short mouseX = NativeMethods.LoWord((uint)lParam.ToInt32()); short mouseY = NativeMethods.HiWord((uint)lParam.ToInt32()); System.Drawing.Point mousePoint = new System.Drawing.Point(mouseX, mouseY); // Offset mouse point mousePoint.X -= dialogX; mousePoint.Y -= dialogY; // If caption is enabled, offset the Y coordinate by the negative of its height. if (hasCaption) mousePoint.Y -= captionHeight; // If a control is in focus, it belongs to this dialog, and it's enabled, then give // it the first chance at handling the message. if (controlFocus != null && controlFocus.Parent == this && controlFocus.IsEnabled) { // If the control MsgProc handles it, then we don't. if (controlFocus.HandleMouse(msg, mousePoint, wParam, lParam)) return true; } // Not yet handled, see if the mouse is over any controls Control control = GetControlAtPoint(mousePoint); if ((control != null) && (control.IsEnabled)) { // Let the control handle the mouse if it wants (and return true if it handles it) if (control.HandleMouse(msg, mousePoint, wParam, lParam)) return true; } else { // Mouse not over any controls in this dialog, if there was a control // which had focus it just lost it if (msg == NativeMethods.WindowMessage.LeftButtonDown && controlFocus != null && controlFocus.Parent == this) { controlFocus.OnFocusOut(); controlFocus = null; } } // Still not handled, hand this off to the dialog. Return false to indicate the // message should still be handled by the application (usually to move the camera). switch(msg) { case NativeMethods.WindowMessage.MouseMove: OnMouseMove(mousePoint); return false; } } break; } // Didn't handle this message return false; }
/// <summary> /// Initializes a control /// </summary> public void InitializeControl(Control control) { if (control == null) throw new ArgumentNullException("control", "You cannot pass in a null control to initialize"); // Set the index control.index = (uint)controlList.Count; // Look for a default element entires for (int i = 0; i < defaultElementList.Count; i++) { // Find any elements for this control ElementHolder holder = (ElementHolder)defaultElementList[i]; if (holder.ControlType == control.ControlType) control[holder.ElementIndex] = holder.Element; } // Initialize the control control.OnInitialize(); }
/// <summary> /// Adds a control to the dialog /// </summary> public void AddControl(Control control) { // Initialize the control first InitializeControl(control); // Add this to the control list controlList.Add(control); }
/// <summary> /// Request that this control has focus /// </summary> public static void RequestFocus(Control control) { if (controlFocus == control) return; // Already does if (!control.CanHaveFocus) return; // Can't have focus if (controlFocus != null) controlFocus.OnFocusOut(); // Set the control focus now control.OnFocusIn(); controlFocus = control; }
/// <summary> /// Clears focus of the dialog /// </summary> public static void ClearFocus() { if (controlFocus != null) { controlFocus.OnFocusOut(); controlFocus = null; } }