2017-08-23 23:59:45 -07:00
/ *
2019-05-06 12:53:06 -07:00
Copyright ( c ) 2019 , Lars Brubaker , John Lewin
2017-08-23 23:59:45 -07:00
All rights reserved .
Redistribution and use in source and binary forms , with or without
modification , are permitted provided that the following conditions are met :
1. Redistributions of source code must retain the above copyright notice , this
list of conditions and the following disclaimer .
2. Redistributions in binary form must reproduce the above copyright notice ,
this list of conditions and the following disclaimer in the documentation
and / or other materials provided with the distribution .
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES
( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ;
LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies ,
either expressed or implied , of the FreeBSD Project .
* /
using System ;
using System.Collections.Generic ;
using MatterHackers.Agg ;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.UI ;
using MatterHackers.Localizations ;
2014-06-11 14:52:58 -07:00
using MatterHackers.MatterControl.PrinterCommunication ;
2016-06-02 16:21:32 -07:00
using MatterHackers.SerialPortCommunication.FrostedSerial ;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections
{
2017-11-08 15:56:37 -08:00
public class SetupStepComPortManual : DialogPage
2015-04-08 15:20:10 -07:00
{
2018-04-14 20:51:01 -07:00
private GuiWidget nextButton ;
private GuiWidget connectButton ;
private GuiWidget refreshButton ;
private GuiWidget printerComPortHelpLink ;
2016-04-18 11:31:31 -07:00
2016-06-02 16:21:32 -07:00
private bool printerComPortIsAvailable = false ;
2015-04-08 15:20:10 -07:00
private TextWidget printerComPortHelpMessage ;
2016-04-18 11:31:31 -07:00
private TextWidget printerComPortError ;
2015-04-08 15:20:10 -07:00
2019-05-06 12:53:06 -07:00
private List < SerialPortIndexRadioButton > serialPortButtonsList = new List < SerialPortIndexRadioButton > ( ) ;
2017-09-17 21:08:16 -07:00
private PrinterConfig printer ;
2016-04-18 11:31:31 -07:00
2017-09-17 21:08:16 -07:00
public SetupStepComPortManual ( PrinterConfig printer )
2015-04-08 15:20:10 -07:00
{
2017-09-17 21:08:16 -07:00
this . printer = printer ;
2019-05-06 12:53:06 -07:00
FlowLayoutWidget printerComPortContainer = CreateComPortContainer ( ) ;
2018-11-03 10:12:27 -07:00
contentRow . AddChild ( printerComPortContainer ) ;
2014-01-29 19:09:30 -08:00
2019-05-06 12:53:06 -07:00
// Construct buttons
2018-04-14 20:51:01 -07:00
nextButton = theme . CreateDialogButton ( "Done" . Localize ( ) ) ;
2019-06-28 22:22:58 -07:00
nextButton . Click + = ( s , e ) = > Parent . Close ( ) ;
2016-04-18 11:31:31 -07:00
nextButton . Visible = false ;
2014-01-29 19:09:30 -08:00
2018-04-14 20:51:01 -07:00
connectButton = theme . CreateDialogButton ( "Connect" . Localize ( ) ) ;
2017-08-23 23:54:31 -07:00
connectButton . Click + = ( s , e ) = >
{
try
{
printerComPortHelpLink . Visible = false ;
2018-11-03 09:13:07 -07:00
printerComPortError . TextColor = theme . TextColor ;
2017-08-23 23:54:31 -07:00
printerComPortError . Text = "Attempting to connect" . Localize ( ) + "..." ;
2018-11-03 09:13:07 -07:00
printerComPortError . TextColor = theme . TextColor ;
2017-08-23 23:54:31 -07:00
2019-05-06 15:11:05 -07:00
printer . Connection . ConnectionFailed + = Connection_CommunicationStateChanged ;
printer . Connection . ConnectionSucceeded + = Connection_CommunicationStateChanged ;
2018-10-05 09:24:57 -07:00
printer . Settings . Helpers . SetComPort ( GetSelectedSerialPort ( ) ) ;
2017-09-18 07:20:06 -07:00
printer . Connection . Connect ( ) ;
2017-08-23 23:54:31 -07:00
connectButton . Visible = false ;
refreshButton . Visible = false ;
}
catch
{
printerComPortHelpLink . Visible = false ;
2017-10-31 11:43:25 -07:00
printerComPortError . TextColor = Color . Red ;
2017-08-23 23:54:31 -07:00
printerComPortError . Text = "Oops! Please select a serial port." . Localize ( ) ;
}
} ;
2014-01-29 19:09:30 -08:00
2018-04-14 20:51:01 -07:00
refreshButton = theme . CreateDialogButton ( "Refresh" . Localize ( ) ) ;
2017-08-23 15:51:29 -07:00
refreshButton . Click + = ( s , e ) = > UiThread . RunOnIdle ( ( ) = >
{
2018-06-19 15:02:25 -07:00
DialogWindow . ChangeToPage ( new SetupStepComPortManual ( printer ) ) ;
2017-08-23 15:51:29 -07:00
} ) ;
2015-04-08 15:20:10 -07:00
2017-08-23 17:27:30 -07:00
this . AddPageAction ( nextButton ) ;
this . AddPageAction ( connectButton ) ;
this . AddPageAction ( refreshButton ) ;
2015-04-08 15:20:10 -07:00
2018-11-16 08:44:56 -08:00
// Register listeners
printer . Connection . CommunicationStateChanged + = Connection_CommunicationStateChanged ;
2016-04-18 11:31:31 -07:00
}
2015-04-08 15:20:10 -07:00
2017-10-18 09:24:29 -07:00
protected override void OnCancel ( out bool abortCancel )
{
printer . Connection . HaltConnectionThread ( ) ;
abortCancel = false ;
}
2018-08-23 16:44:11 -07:00
public override void OnClosed ( EventArgs e )
2015-04-08 15:20:10 -07:00
{
2018-11-16 08:44:56 -08:00
// Unregister listeners
printer . Connection . CommunicationStateChanged - = Connection_CommunicationStateChanged ;
2019-05-06 15:11:05 -07:00
printer . Connection . ConnectionFailed - = Connection_CommunicationStateChanged ;
printer . Connection . ConnectionSucceeded - = Connection_CommunicationStateChanged ;
2018-11-16 08:44:56 -08:00
2015-04-08 15:20:10 -07:00
base . OnClosed ( e ) ;
}
2019-05-06 12:53:06 -07:00
private FlowLayoutWidget CreateComPortContainer ( )
2015-04-08 15:20:10 -07:00
{
2018-05-14 11:50:27 -07:00
var container = new FlowLayoutWidget ( FlowDirection . TopToBottom )
{
Margin = new BorderDouble ( 0 ) ,
VAnchor = VAnchor . Stretch
} ;
2014-01-29 19:09:30 -08:00
2018-05-14 11:50:27 -07:00
BorderDouble elementMargin = new BorderDouble ( top : 3 ) ;
2014-01-29 19:09:30 -08:00
2018-05-14 11:50:27 -07:00
var comPortLabel = new TextWidget ( "Serial Port" . Localize ( ) + ":" , 0 , 0 , 12 )
{
2018-11-03 09:13:07 -07:00
TextColor = theme . TextColor ,
2018-05-14 11:50:27 -07:00
Margin = new BorderDouble ( 0 , 0 , 0 , 10 ) ,
HAnchor = HAnchor . Stretch
} ;
2014-01-29 19:09:30 -08:00
2018-05-14 11:50:27 -07:00
var serialPortContainer = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
2015-05-01 13:28:31 -07:00
CreateSerialPortControls ( serialPortContainer , null ) ;
2014-01-29 19:09:30 -08:00
2018-05-14 11:50:27 -07:00
var comPortMessageContainer = new FlowLayoutWidget
{
Margin = elementMargin ,
HAnchor = HAnchor . Stretch
} ;
2014-01-29 19:09:30 -08:00
2018-05-14 11:50:27 -07:00
printerComPortError = new TextWidget ( "Currently available serial ports." . Localize ( ) , 0 , 0 , 10 )
{
2018-11-03 09:13:07 -07:00
TextColor = theme . TextColor ,
2018-05-14 11:50:27 -07:00
AutoExpandBoundsToText = true
} ;
2014-01-29 19:09:30 -08:00
2018-07-11 09:26:54 -07:00
printerComPortHelpLink = new LinkLabel ( "What's this?" . Localize ( ) , theme )
{
Margin = new BorderDouble ( left : 5 ) ,
VAnchor = VAnchor . Bottom
} ;
2016-04-18 11:31:31 -07:00
printerComPortHelpLink . Click + = ( s , e ) = > printerComPortHelpMessage . Visible = ! printerComPortHelpMessage . Visible ;
2014-01-29 19:09:30 -08:00
2018-05-14 11:50:27 -07:00
printerComPortHelpMessage = new TextWidget ( "The 'Serial Port' section lists all available serial\nports on your device. Changing which USB port the printer\nis connected to may change the associated serial port.\n\nTip: If you are uncertain, unplug/plug in your printer\nand hit refresh. The new port that appears should be\nyour printer." . Localize ( ) , 0 , 0 , 10 )
{
2018-11-03 09:13:07 -07:00
TextColor = theme . TextColor ,
2018-05-14 11:50:27 -07:00
Margin = new BorderDouble ( top : 10 ) ,
Visible = false
} ;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
comPortMessageContainer . AddChild ( printerComPortError ) ;
comPortMessageContainer . AddChild ( printerComPortHelpLink ) ;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
container . AddChild ( comPortLabel ) ;
2015-05-01 13:28:31 -07:00
container . AddChild ( serialPortContainer ) ;
2015-04-08 15:20:10 -07:00
container . AddChild ( comPortMessageContainer ) ;
container . AddChild ( printerComPortHelpMessage ) ;
2014-01-29 19:09:30 -08:00
2017-08-07 15:47:27 -07:00
container . HAnchor = HAnchor . Stretch ;
2017-09-17 21:08:16 -07:00
2015-04-08 15:20:10 -07:00
return container ;
}
2014-01-29 19:09:30 -08:00
2018-11-16 08:44:56 -08:00
private void Connection_CommunicationStateChanged ( object sender , EventArgs e )
2015-04-08 15:20:10 -07:00
{
2018-02-01 14:51:44 -08:00
if ( printer . Connection . IsConnected )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
printerComPortHelpLink . Visible = false ;
2018-11-03 09:13:07 -07:00
printerComPortError . TextColor = theme . TextColor ;
2016-04-18 11:31:31 -07:00
printerComPortError . Text = "Connection succeeded" . Localize ( ) + "!" ;
nextButton . Visible = true ;
connectButton . Visible = false ;
2019-07-06 13:24:57 -07:00
this ? . Parent ? . CloseOnIdle ( ) ;
2015-04-08 15:20:10 -07:00
}
2017-09-17 21:08:16 -07:00
else if ( printer . Connection . CommunicationState ! = CommunicationStates . AttemptingToConnect )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
printerComPortHelpLink . Visible = false ;
2017-10-31 11:43:25 -07:00
printerComPortError . TextColor = Color . Red ;
2016-04-18 11:31:31 -07:00
printerComPortError . Text = "Uh-oh! Could not connect to printer." . Localize ( ) ;
connectButton . Visible = true ;
nextButton . Visible = false ;
2015-04-08 15:20:10 -07:00
}
}
2016-06-02 16:21:32 -07:00
protected void CreateSerialPortControls ( FlowLayoutWidget comPortContainer , string activePrinterSerialPort )
{
int portIndex = 0 ;
// Add a radio button for each filtered port
2018-05-14 11:50:27 -07:00
foreach ( string portName in FrostedSerialPort . GetPortNames ( ) )
2016-06-02 16:21:32 -07:00
{
2019-05-06 12:53:06 -07:00
SerialPortIndexRadioButton comPortOption = CreateComPortOption ( portName , activePrinterSerialPort = = portName ) ;
2016-06-02 16:21:32 -07:00
if ( comPortOption . Checked )
{
printerComPortIsAvailable = true ;
}
2019-05-06 12:53:06 -07:00
serialPortButtonsList . Add ( comPortOption ) ;
2016-06-02 16:21:32 -07:00
comPortContainer . AddChild ( comPortOption ) ;
portIndex + + ;
}
// Add a virtual entry for serial ports that were previously configured but are not currently connected
if ( ! printerComPortIsAvailable & & activePrinterSerialPort ! = null )
{
2019-05-06 12:53:06 -07:00
SerialPortIndexRadioButton comPortOption = CreateComPortOption ( activePrinterSerialPort , true ) ;
2016-06-02 16:21:32 -07:00
comPortOption . Enabled = false ;
comPortContainer . AddChild ( comPortOption ) ;
2019-05-06 12:53:06 -07:00
serialPortButtonsList . Add ( comPortOption ) ;
2016-06-02 16:21:32 -07:00
portIndex + + ;
}
2019-05-06 12:53:06 -07:00
// If there are still no com ports show a message to that effect
2016-06-02 16:21:32 -07:00
if ( portIndex = = 0 )
{
2018-05-14 11:50:27 -07:00
var comPortOption = new TextWidget ( "No COM ports available" . Localize ( ) )
{
Margin = new BorderDouble ( 3 , 6 , 5 , 6 ) ,
2018-11-03 09:13:07 -07:00
TextColor = theme . TextColor
2018-05-14 11:50:27 -07:00
} ;
2016-06-02 16:21:32 -07:00
comPortContainer . AddChild ( comPortOption ) ;
}
}
2019-05-06 12:53:06 -07:00
private SerialPortIndexRadioButton CreateComPortOption ( string portName , bool isActivePrinterPort )
2016-06-02 16:21:32 -07:00
{
2018-05-14 11:50:27 -07:00
return new SerialPortIndexRadioButton ( portName , portName )
2016-06-02 16:21:32 -07:00
{
2017-08-07 15:47:27 -07:00
HAnchor = HAnchor . Left ,
2016-06-02 16:21:32 -07:00
Margin = new BorderDouble ( 3 , 3 , 5 , 3 ) ,
2018-11-03 09:13:07 -07:00
TextColor = theme . TextColor ,
2016-06-02 16:21:32 -07:00
Checked = isActivePrinterPort
} ;
}
private string GetSelectedSerialPort ( )
{
2019-05-06 12:53:06 -07:00
foreach ( SerialPortIndexRadioButton button in serialPortButtonsList )
2016-06-02 16:21:32 -07:00
{
if ( button . Checked )
{
return button . PortValue ;
}
}
2017-01-04 07:23:30 -08:00
throw new Exception ( "Could not find a selected button." . Localize ( ) ) ;
2016-06-02 16:21:32 -07:00
}
2015-04-08 15:20:10 -07:00
}
}