2014-01-29 19:09:30 -08:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.IO ;
using MatterHackers.Agg.Image ;
using MatterHackers.Agg ;
using MatterHackers.Agg.UI ;
using MatterHackers.VectorMath ;
using MatterHackers.MatterControl ;
using MatterHackers.MatterControl.PrintQueue ;
using MatterHackers.MatterControl.DataStorage ;
using MatterHackers.MatterControl.PrinterControls.PrinterConnections ;
using MatterHackers.Localizations ;
namespace MatterHackers.MatterControl.ActionBar
{
class PrinterActionRow : ActionRowBase
{
TextImageButtonFactory actionBarButtonFactory = new TextImageButtonFactory ( ) ;
Button connectPrinterButton ;
Button disconnectPrinterButton ;
Button selectActivePrinterButton ;
ConnectionWindow connectionWindow ;
bool connectionWindowIsOpen = false ;
protected override void Initialize ( )
{
2014-03-21 16:18:47 -07:00
actionBarButtonFactory . normalTextColor = ActiveTheme . Instance . PrimaryTextColor ;
actionBarButtonFactory . hoverTextColor = ActiveTheme . Instance . PrimaryTextColor ;
actionBarButtonFactory . pressedTextColor = ActiveTheme . Instance . PrimaryTextColor ;
2014-01-29 19:09:30 -08:00
2014-03-21 16:18:47 -07:00
actionBarButtonFactory . disabledTextColor = ActiveTheme . Instance . TabLabelUnselected ;
2014-01-29 19:09:30 -08:00
actionBarButtonFactory . disabledFillColor = ActiveTheme . Instance . PrimaryBackgroundColor ;
actionBarButtonFactory . disabledBorderColor = ActiveTheme . Instance . PrimaryBackgroundColor ;
actionBarButtonFactory . invertImageLocation = true ;
actionBarButtonFactory . borderWidth = 0 ;
this . BackgroundColor = ActiveTheme . Instance . PrimaryBackgroundColor ;
}
protected override void AddChildElements ( )
{
actionBarButtonFactory . invertImageLocation = false ;
2014-04-10 11:00:26 -07:00
string connectString = "Connect" . Localize ( ) . ToUpper ( ) ;
2014-01-29 19:09:30 -08:00
connectPrinterButton = actionBarButtonFactory . Generate ( connectString , "icon_power_32x32.png" ) ;
2014-04-27 20:49:29 -07:00
connectPrinterButton . Margin = new BorderDouble ( 0 , 0 , 3 , 3 ) ;
2014-04-27 20:24:58 -07:00
connectPrinterButton . VAnchor = VAnchor . ParentTop ;
2014-01-29 19:09:30 -08:00
connectPrinterButton . Cursor = Cursors . Hand ;
2014-04-10 11:00:26 -07:00
string disconnectString = "Disconnect" . Localize ( ) . ToUpper ( ) ;
2014-01-29 19:09:30 -08:00
disconnectPrinterButton = actionBarButtonFactory . Generate ( disconnectString , "icon_power_32x32.png" ) ;
2014-04-27 20:49:29 -07:00
disconnectPrinterButton . Margin = new BorderDouble ( 0 , 0 , 3 , 3 ) ;
2014-04-27 20:24:58 -07:00
disconnectPrinterButton . VAnchor = VAnchor . ParentTop ;
2014-01-29 19:09:30 -08:00
disconnectPrinterButton . Visible = false ;
disconnectPrinterButton . Cursor = Cursors . Hand ;
selectActivePrinterButton = new PrinterSelectButton ( ) ;
selectActivePrinterButton . HAnchor = HAnchor . ParentLeftRight ;
selectActivePrinterButton . Cursor = Cursors . Hand ;
2014-03-21 18:44:01 -07:00
if ( ApplicationWidget . Instance . WidescreenMode )
{
2014-04-27 20:49:29 -07:00
selectActivePrinterButton . Margin = new BorderDouble ( 0 , 6 , 0 , 3 ) ;
2014-03-21 18:44:01 -07:00
}
else
{
2014-04-27 20:49:29 -07:00
selectActivePrinterButton . Margin = new BorderDouble ( 0 , 6 , 6 , 3 ) ;
2014-03-21 18:44:01 -07:00
}
2014-03-21 16:18:47 -07:00
2014-01-29 19:09:30 -08:00
actionBarButtonFactory . invertImageLocation = true ;
this . AddChild ( connectPrinterButton ) ;
this . AddChild ( disconnectPrinterButton ) ;
this . AddChild ( selectActivePrinterButton ) ;
2014-03-21 16:18:47 -07:00
//this.AddChild(CreateOptionsMenu());
2014-01-29 19:09:30 -08:00
}
event EventHandler unregisterEvents ;
protected override void AddHandlers ( )
{
2014-02-14 12:06:44 -08:00
ActivePrinterProfile . Instance . ActivePrinterChanged . RegisterEvent ( ReloadPrinterSelectionWidget , ref unregisterEvents ) ;
ActivePrinterProfile . Instance . ActivePrinterChanged . RegisterEvent ( onActivePrinterChanged , ref unregisterEvents ) ;
2014-01-29 19:09:30 -08:00
PrinterCommunication . Instance . EnableChanged . RegisterEvent ( onPrinterStatusChanged , ref unregisterEvents ) ;
PrinterCommunication . Instance . ConnectionStateChanged . RegisterEvent ( onPrinterStatusChanged , ref unregisterEvents ) ;
selectActivePrinterButton . Click + = new ButtonBase . ButtonEventHandler ( onSelectActivePrinterButton_Click ) ;
connectPrinterButton . Click + = new ButtonBase . ButtonEventHandler ( onConnectButton_Click ) ;
disconnectPrinterButton . Click + = new ButtonBase . ButtonEventHandler ( onDisconnectButtonClick ) ;
base . AddHandlers ( ) ;
}
public override void OnClosed ( EventArgs e )
{
if ( unregisterEvents ! = null )
{
unregisterEvents ( this , null ) ;
}
base . OnClosed ( e ) ;
}
void onConnectButton_Click ( object sender , MouseEventArgs mouseEvent )
{
Button buttonClicked = ( ( Button ) sender ) ;
if ( buttonClicked . Enabled )
{
2014-02-14 12:06:44 -08:00
if ( ActivePrinterProfile . Instance . ActivePrinter = = null )
2014-01-29 19:09:30 -08:00
{
2014-04-10 16:38:22 -07:00
OpenConnectionWindow ( ConnectToActivePrinter ) ;
2014-01-29 19:09:30 -08:00
}
else
{
ConnectToActivePrinter ( ) ;
}
}
}
void ConnectToActivePrinter ( )
{
PrinterCommunication . Instance . HaltConnectionThread ( ) ;
PrinterCommunication . Instance . ConnectToActivePrinter ( ) ;
}
void onSelectActivePrinterButton_Click ( object sender , MouseEventArgs mouseEvent )
{
OpenConnectionWindow ( ) ;
}
2014-04-10 16:38:22 -07:00
public delegate void ConnectOnSelectFunction ( ) ;
ConnectOnSelectFunction functionToCallOnSelect ;
void OpenConnectionWindow ( ConnectOnSelectFunction functionToCallOnSelect = null )
2014-01-29 19:09:30 -08:00
{
if ( this . connectionWindowIsOpen = = false )
{
connectionWindow = new ConnectionWindow ( ) ;
this . connectionWindowIsOpen = true ;
2014-04-10 16:38:22 -07:00
this . functionToCallOnSelect = functionToCallOnSelect ;
2014-01-29 19:09:30 -08:00
connectionWindow . Closed + = new EventHandler ( ConnectionWindow_Closed ) ;
}
else
{
if ( connectionWindow ! = null )
{
connectionWindow . BringToFront ( ) ;
}
}
}
void ConnectionWindow_Closed ( object sender , EventArgs e )
{
this . connectionWindowIsOpen = false ;
}
void ReloadPrinterSelectionWidget ( object sender , EventArgs e )
{
//selectActivePrinterButton.Invalidate();
}
void onActivePrinterChanged ( object sender , EventArgs e )
{
2014-04-10 16:38:22 -07:00
connectPrinterButton . Enabled = true ;
if ( functionToCallOnSelect ! = null )
{
functionToCallOnSelect ( ) ;
functionToCallOnSelect = null ;
}
2014-01-29 19:09:30 -08:00
}
void onDisconnectButtonClick ( object sender , MouseEventArgs e )
{
UiThread . RunOnIdle ( OnIdleDisconnect ) ;
}
void OnIdleDisconnect ( object state )
{
bool doCancel = true ;
if ( PrinterCommunication . Instance . PrinterIsPrinting )
{
2014-05-04 14:49:31 -07:00
if ( StyledMessageBox . ShowMessageBox ( "Disconnect and cancel the current print?" , "WARNING: Disconnecting will cancel the current print.\n\nDo you want to disconnect?" , StyledMessageBox . MessageType . YES_NO ) )
2014-01-29 19:09:30 -08:00
{
PrinterCommunication . Instance . Stop ( ) ;
}
else
{
doCancel = false ;
}
}
if ( doCancel )
{
PrinterCommunication . Instance . Disable ( ) ;
disconnectPrinterButton . Visible = false ;
connectPrinterButton . Visible = true ;
connectPrinterButton . Enabled = true ;
selectActivePrinterButton . Invalidate ( ) ;
}
}
void onPrinterStatusChanged ( object sender , EventArgs e )
{
if ( PrinterCommunication . Instance . PrinterIsConnected )
{
onConnectionSuccess ( ) ;
}
else
{
onConnectionFailed ( ) ;
}
}
void onConnectionFailed ( )
{
disconnectPrinterButton . Visible = false ;
connectPrinterButton . Visible = true ;
connectPrinterButton . Enabled = true ;
}
void onConnectionSuccess ( )
{
disconnectPrinterButton . Visible = true ;
connectPrinterButton . Visible = false ;
}
}
}