Commits

Douglas Mendizabal  committed 2bb17f8

Added support to handle MessageBoxes.

  • Participants
  • Parent commits f991741

Comments (0)

Files changed (6)

File src/Setup/Resources/Strings.Designer.cs

+//------------------------------------------------------------------------------
+// <auto-generated>
+//     This code was generated by a tool.
+//     Runtime Version:4.0.30319.225
+//
+//     Changes to this file may cause incorrect behavior and will be lost if
+//     the code is regenerated.
+// </auto-generated>
+//------------------------------------------------------------------------------
+
+namespace wpfsetup.Setup.Resources {
+    using System;
+    
+    
+    /// <summary>
+    ///   A strongly-typed resource class, for looking up localized strings, etc.
+    /// </summary>
+    // This class was auto-generated by the StronglyTypedResourceBuilder
+    // class via a tool like ResGen or Visual Studio.
+    // To add or remove a member, edit your .ResX file then rerun ResGen
+    // with the /str option, or rebuild your VS project.
+    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
+    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
+    internal class Strings {
+        
+        private static global::System.Resources.ResourceManager resourceMan;
+        
+        private static global::System.Globalization.CultureInfo resourceCulture;
+        
+        [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
+        internal Strings() {
+        }
+        
+        /// <summary>
+        ///   Returns the cached ResourceManager instance used by this class.
+        /// </summary>
+        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+        internal static global::System.Resources.ResourceManager ResourceManager {
+            get {
+                if (object.ReferenceEquals(resourceMan, null)) {
+                    global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("wpfsetup.Setup.Resources.Strings", typeof(Strings).Assembly);
+                    resourceMan = temp;
+                }
+                return resourceMan;
+            }
+        }
+        
+        /// <summary>
+        ///   Overrides the current thread's CurrentUICulture property for all
+        ///   resource lookups using this strongly typed resource class.
+        /// </summary>
+        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+        internal static global::System.Globalization.CultureInfo Culture {
+            get {
+                return resourceCulture;
+            }
+            set {
+                resourceCulture = value;
+            }
+        }
+        
+        /// <summary>
+        ///   Looks up a localized string similar to Error.
+        /// </summary>
+        internal static string MessageBox_Error {
+            get {
+                return ResourceManager.GetString("MessageBox_Error", resourceCulture);
+            }
+        }
+        
+        /// <summary>
+        ///   Looks up a localized string similar to Fatal Error.
+        /// </summary>
+        internal static string MessageBox_FatalError {
+            get {
+                return ResourceManager.GetString("MessageBox_FatalError", resourceCulture);
+            }
+        }
+        
+        /// <summary>
+        ///   Looks up a localized string similar to Warning.
+        /// </summary>
+        internal static string MessageBox_Warning {
+            get {
+                return ResourceManager.GetString("MessageBox_Warning", resourceCulture);
+            }
+        }
+    }
+}

File src/Setup/Resources/Strings.resx

+<?xml version="1.0" encoding="utf-8"?>
+<root>
+  <!-- 
+    Microsoft ResX Schema 
+    
+    Version 2.0
+    
+    The primary goals of this format is to allow a simple XML format 
+    that is mostly human readable. The generation and parsing of the 
+    various data types are done through the TypeConverter classes 
+    associated with the data types.
+    
+    Example:
+    
+    ... ado.net/XML headers & schema ...
+    <resheader name="resmimetype">text/microsoft-resx</resheader>
+    <resheader name="version">2.0</resheader>
+    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
+    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
+    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
+    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
+    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
+        <value>[base64 mime encoded serialized .NET Framework object]</value>
+    </data>
+    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
+        <comment>This is a comment</comment>
+    </data>
+                
+    There are any number of "resheader" rows that contain simple 
+    name/value pairs.
+    
+    Each data row contains a name, and value. The row also contains a 
+    type or mimetype. Type corresponds to a .NET class that support 
+    text/value conversion through the TypeConverter architecture. 
+    Classes that don't support this are serialized and stored with the 
+    mimetype set.
+    
+    The mimetype is used for serialized objects, and tells the 
+    ResXResourceReader how to depersist the object. This is currently not 
+    extensible. For a given mimetype the value must be set accordingly:
+    
+    Note - application/x-microsoft.net.object.binary.base64 is the format 
+    that the ResXResourceWriter will generate, however the reader can 
+    read any of the formats listed below.
+    
+    mimetype: application/x-microsoft.net.object.binary.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
+            : and then encoded with base64 encoding.
+    
+    mimetype: application/x-microsoft.net.object.soap.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
+            : and then encoded with base64 encoding.
+
+    mimetype: application/x-microsoft.net.object.bytearray.base64
+    value   : The object must be serialized into a byte array 
+            : using a System.ComponentModel.TypeConverter
+            : and then encoded with base64 encoding.
+    -->
+  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
+    <xsd:element name="root" msdata:IsDataSet="true">
+      <xsd:complexType>
+        <xsd:choice maxOccurs="unbounded">
+          <xsd:element name="metadata">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" />
+              </xsd:sequence>
+              <xsd:attribute name="name" use="required" type="xsd:string" />
+              <xsd:attribute name="type" type="xsd:string" />
+              <xsd:attribute name="mimetype" type="xsd:string" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="assembly">
+            <xsd:complexType>
+              <xsd:attribute name="alias" type="xsd:string" />
+              <xsd:attribute name="name" type="xsd:string" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="data">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
+              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
+              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="resheader">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" />
+            </xsd:complexType>
+          </xsd:element>
+        </xsd:choice>
+      </xsd:complexType>
+    </xsd:element>
+  </xsd:schema>
+  <resheader name="resmimetype">
+    <value>text/microsoft-resx</value>
+  </resheader>
+  <resheader name="version">
+    <value>2.0</value>
+  </resheader>
+  <resheader name="reader">
+    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <resheader name="writer">
+    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <data name="MessageBox_Error" xml:space="preserve">
+    <value>Error</value>
+  </data>
+  <data name="MessageBox_FatalError" xml:space="preserve">
+    <value>Fatal Error</value>
+  </data>
+  <data name="MessageBox_Warning" xml:space="preserve">
+    <value>Warning</value>
+  </data>
+</root>

File src/Setup/Setup.csproj

       <SubType>Designer</SubType>
     </ApplicationDefinition>
     <Compile Include="Model\DefaultInstallerAdapter.cs" />
+    <Compile Include="ViewModel\User32.cs" />
     <Compile Include="RelayCommand.cs" />
+    <Compile Include="Resources\Strings.Designer.cs">
+      <AutoGen>True</AutoGen>
+      <DesignTime>True</DesignTime>
+      <DependentUpon>Strings.resx</DependentUpon>
+    </Compile>
     <Compile Include="ViewModel\ExternalUIHandlerViewModel.cs" />
     <Compile Include="Model\IInstallerAdapter.cs" />
     <Page Include="MainWindow.xaml">
     <AppDesigner Include="Properties\" />
   </ItemGroup>
   <ItemGroup>
-    <Folder Include="Resources\" />
     <Folder Include="View\" />
   </ItemGroup>
+  <ItemGroup>
+    <EmbeddedResource Include="Resources\Strings.resx">
+      <Generator>ResXFileCodeGenerator</Generator>
+      <LastGenOutput>Strings.Designer.cs</LastGenOutput>
+    </EmbeddedResource>
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.

File src/Setup/ViewModel/ExternalUIHandlerViewModel.cs

 using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
 using Microsoft.Deployment.WindowsInstaller;
 using wpfsetup.Setup.Model;
+using wpfsetup.Setup.Resources;
 
 namespace wpfsetup.Setup.ViewModel
 {
 
         #endregion // Constructor
 
+        #region Methods
+
         public MessageResult BasicUIHandler(InstallMessage messagetype, string message, MessageButtons buttons, MessageIcon icon, MessageDefaultButton defaultbutton)
         {
             if (this._firstTime)
                 this._firstTime = false;
             }
 
+            if (message.Length == 0)
+                return MessageResult.None;
+
+            switch (messagetype)
+            {
+                case InstallMessage.FatalExit:
+                    return User32.MessageBox(IntPtr.Zero, message, Strings.MessageBox_FatalError,
+                                             User32.Options(buttons, icon, defaultbutton));
+                    break;
+                case InstallMessage.Error:
+                    return User32.MessageBox(IntPtr.Zero, message, Strings.MessageBox_Error,
+                                             User32.Options(buttons, icon, defaultbutton));
+                    break;
+                case InstallMessage.Warning:
+                    return User32.MessageBox(IntPtr.Zero, message, Strings.MessageBox_Warning,
+                                             User32.Options(buttons, icon, defaultbutton));
+                    break;
+                case InstallMessage.User:
+                    break;
+                case InstallMessage.Info:
+                    break;
+                case InstallMessage.FilesInUse:
+                    break;
+                case InstallMessage.ResolveSource:
+                    break;
+                case InstallMessage.OutOfDiskSpace:
+                    break;
+                case InstallMessage.ActionStart:
+                    break;
+                case InstallMessage.ActionData:
+                    break;
+                case InstallMessage.Progress:
+                    break;
+                case InstallMessage.CommonData:
+                    break;
+                case InstallMessage.Initialize:
+                    break;
+                case InstallMessage.Terminate:
+                    break;
+                case InstallMessage.ShowDialog:
+                    break;
+                case InstallMessage.RMFilesInUse:
+                    break;
+                case InstallMessage.InstallStart:
+                    break;
+                case InstallMessage.InstallEnd:
+                    break;
+                default:
+                    throw new ArgumentOutOfRangeException("messagetype");
+            }
+
             return MessageResult.None;
         }
+
+        #endregion // Methods
     }
 }

File src/Setup/ViewModel/User32.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+using Microsoft.Deployment.WindowsInstaller;
+
+namespace wpfsetup.Setup.Model
+{
+    /// <summary>
+    /// Interop class for showing message boxes using Win32 MessageBox.
+    /// </summary>
+    public static class User32
+    {
+        [DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)]
+        public static extern MessageResult MessageBox(IntPtr hWnd, String text, String caption, uint options);
+
+        [Flags]
+        public enum MessageBoxOptions : uint
+        {
+            OK = 0x000000,
+            OKCancel = 0x000001,
+            AbortRetryIgnore = 0x000002,
+            YesNoCancel = 0x000003,
+            YesNo = 0x000004,
+            RetryCancel = 0x000005,
+            CancelTryContinue = 0x000006,
+
+            IconHand = 0x000010,
+            IconQuestion = 0x000020,
+            IconExclamation = 0x000030,
+            IconAsterisk = 0x000040,
+            UserIcon = 0x000080,
+
+            IconWarning = IconExclamation,
+            IconError = IconHand,
+            IconInformation = IconAsterisk,
+            IconStop = IconHand,
+
+            DefButton1 = 0x000000,
+            DefButton2 = 0x000100,
+            DefButton3 = 0x000200,
+            DefButton4 = 0x000300,
+
+            ApplicationModal = 0x000000,
+            SystemModal = 0x001000,
+            TaskModal = 0x002000,
+
+            Help = 0x004000, //Help Button
+            NoFocus = 0x008000,
+
+            SetForeground = 0x010000,
+            DefaultDesktopOnly = 0x020000,
+            Topmost = 0x040000,
+            Right = 0x080000,
+            RTLReading = 0x100000,
+        }
+
+        public enum MessageBoxResult : uint
+        {
+            OK = 1,
+            Cancel,
+            Abort,
+            Retry,
+            Ignore,
+            Yes,
+            No,
+            Close,
+            Help,
+            TryAgain,
+            Continue,
+            Timeout = 32000
+        }
+
+        /// <summary>
+        /// Returns a uint representation of the MessageBox options passed in the parameters.
+        /// </summary>
+        public static uint Options(MessageButtons buttons, MessageIcon icon, MessageDefaultButton defaultbutton)
+        {
+            MessageBoxOptions result;
+
+            switch (buttons)
+            {
+                case MessageButtons.OK:
+                    result = MessageBoxOptions.OK;
+                    break;
+                case MessageButtons.OKCancel:
+                    result = MessageBoxOptions.OKCancel;
+                    break;
+                case MessageButtons.AbortRetryIgnore:
+                    result = MessageBoxOptions.AbortRetryIgnore;
+                    break;
+                case MessageButtons.YesNoCancel:
+                    result = MessageBoxOptions.YesNoCancel;
+                    break;
+                case MessageButtons.YesNo:
+                    result = MessageBoxOptions.YesNo;
+                    break;
+                case MessageButtons.RetryCancel:
+                    result = MessageBoxOptions.RetryCancel;
+                    break;
+                default:
+                    throw new ArgumentOutOfRangeException("buttons");
+            }
+
+            return (uint) (result | (MessageBoxOptions) icon | (MessageBoxOptions) defaultbutton);
+        }
+    }
+}

File src/SetupTests/ViewModel/ExternalUIHandlerViewModelTests.cs

 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.Linq;
 using System.Text;
 using Microsoft.Deployment.WindowsInstaller;
         {
             var mockInstallerAdapter = new Mock<IInstallerAdapter>();
             var externalUIHandlerViewModel = new ExternalUIHandlerViewModel(mockInstallerAdapter.Object);
-            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Error, "MessageText", MessageButtons.OK,
+            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Error, "", MessageButtons.OK,
                                                       MessageIcon.Error, MessageDefaultButton.Button1);
-            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Error, "MessageText", MessageButtons.OK,
+            externalUIHandlerViewModel.BasicUIHandler(InstallMessage.Error, "", MessageButtons.OK,
                                                       MessageIcon.Error, MessageDefaultButton.Button1);
             mockInstallerAdapter.Verify(m => m.SetInternalUI(InstallUIOptions.Basic), Times.Exactly(1));
         }
                                                                                   MessageDefaultButton.Button1);
             Assert.Equal(MessageResult.None, returnedMessageResult);
         }
+
+        [Fact (Skip = "Actually shows the window, so we want to skip this for automated testing.")]
+        public void BasicUIHandler_shows_User32MessageBox()
+        {
+            var ia = new Mock<IInstallerAdapter>();
+            var vm = new ExternalUIHandlerViewModel(ia.Object);
+            var message = "Error message text here.";
+            var returnedMessageResult = vm.BasicUIHandler(InstallMessage.FatalExit,
+                                                          message,
+                                                          MessageButtons.RetryCancel,
+                                                          MessageIcon.Information,
+                                                          MessageDefaultButton.Button3);
+            switch (returnedMessageResult)
+            {
+                case MessageResult.Error:
+                    Debug.WriteLine("Error");
+                    break;
+                case MessageResult.None:
+                    Debug.WriteLine("None");
+                    break;
+                case MessageResult.OK:
+                    Debug.WriteLine("OK");
+                    break;
+                case MessageResult.Cancel:
+                    Debug.WriteLine("Cancel");
+                    break;
+                case MessageResult.Abort:
+                    Debug.WriteLine("Abort");
+                    break;
+                case MessageResult.Retry:
+                    Debug.WriteLine("Retry");
+                    break;
+                case MessageResult.Ignore:
+                    Debug.WriteLine("Ignore");
+                    break;
+                case MessageResult.Yes:
+                    Debug.WriteLine("Yes");
+                    break;
+                case MessageResult.No:
+                    Debug.WriteLine("No");
+                    break;
+                default:
+                    throw new ArgumentOutOfRangeException();
+            }
+        }
     }
 }