2015-07-30 17:42:58 -07:00
/ *
2016-04-18 11:31:31 -07:00
Copyright ( c ) 2016 , John Lewin
2015-07-30 17:42:58 -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 .
* /
2016-05-07 17:18:05 -07:00
using MatterHackers.Agg ;
using MatterHackers.Agg.Image ;
2016-04-29 13:55:29 -07:00
using MatterHackers.Agg.ImageProcessing ;
using MatterHackers.Agg.PlatformAbstract ;
2016-04-29 15:29:31 -07:00
using MatterHackers.Agg.UI ;
2016-04-18 11:31:31 -07:00
using MatterHackers.Localizations ;
2016-07-06 16:40:23 -07:00
using MatterHackers.MatterControl.PrinterCommunication ;
2016-04-29 15:29:31 -07:00
using MatterHackers.MatterControl.PrinterControls.PrinterConnections ;
2016-04-18 11:31:31 -07:00
using MatterHackers.MatterControl.SlicerConfiguration ;
2016-05-07 17:18:05 -07:00
using MatterHackers.VectorMath ;
2016-05-03 18:22:59 -07:00
using System ;
2016-06-09 09:23:23 -07:00
using System.Linq ;
2016-04-18 11:31:31 -07:00
namespace MatterHackers.MatterControl
2015-07-30 17:42:58 -07:00
{
2016-06-03 18:11:51 -07:00
public class PrinterSelector : DropDownList
2015-07-30 17:42:58 -07:00
{
2016-05-03 18:22:59 -07:00
public event EventHandler AddPrinter ;
2016-06-09 09:23:23 -07:00
private EventHandler unregisterEvents ;
2016-07-06 16:40:23 -07:00
int lastSelectedIndex = - 1 ;
2016-06-09 09:23:23 -07:00
2016-07-15 11:52:06 -07:00
public PrinterSelector ( ) : base ( "Printers" . Localize ( ) + "... " )
2015-07-30 17:42:58 -07:00
{
2016-06-09 09:23:23 -07:00
Rebuild ( ) ;
2016-04-18 11:31:31 -07:00
2016-08-01 17:21:31 -07:00
this . Name = "Printers... Menu" ;
2016-04-18 11:31:31 -07:00
this . SelectionChanged + = ( s , e ) = >
{
2016-05-16 16:21:42 -07:00
string printerID = this . SelectedValue ;
if ( printerID = = "new" )
2016-04-18 11:31:31 -07:00
{
2016-06-21 19:02:04 -07:00
// do nothing
2016-04-29 13:55:29 -07:00
}
2016-05-16 16:21:42 -07:00
else
{
2016-07-06 16:40:23 -07:00
if ( PrinterConnectionAndCommunication . Instance . PrinterIsPrinting
| | PrinterConnectionAndCommunication . Instance . PrinterIsPaused )
{
if ( this . SelectedIndex ! = lastSelectedIndex )
{
UiThread . RunOnIdle ( ( ) = >
StyledMessageBox . ShowMessageBox ( null , "Please wait until the print has finished and try again." . Localize ( ) , "Can't switch printers while printing" . Localize ( ) )
) ;
this . SelectedIndex = lastSelectedIndex ;
}
}
else
{
lastSelectedIndex = this . SelectedIndex ;
ActiveSliceSettings . SwitchToProfile ( printerID ) ;
}
2016-05-16 16:21:42 -07:00
}
2016-04-18 11:31:31 -07:00
} ;
2016-06-09 09:23:23 -07:00
SliceSettingsWidget . SettingChanged . RegisterEvent ( SettingChanged , ref unregisterEvents ) ;
2016-06-16 13:23:44 -07:00
ProfileManager . ProfilesListChanged . RegisterEvent ( SettingChanged , ref unregisterEvents ) ;
2016-06-09 09:23:23 -07:00
}
public void Rebuild ( )
{
this . MenuItems . Clear ( ) ;
//Add the menu items to the menu itself
2016-07-29 11:16:59 -07:00
foreach ( var printer in ProfileManager . Instance . ActiveProfiles . OrderBy ( p = > p . Name ) )
2016-06-09 09:23:23 -07:00
{
2016-06-15 12:58:36 -07:00
this . AddItem ( printer . Name , printer . ID . ToString ( ) ) ;
2016-06-09 09:23:23 -07:00
}
if ( ActiveSliceSettings . Instance ! = null )
{
this . SelectedValue = ActiveSliceSettings . Instance . ID ;
2016-07-06 16:40:23 -07:00
lastSelectedIndex = this . SelectedIndex ;
2016-06-16 16:35:10 -07:00
this . mainControlText . Text = ActiveSliceSettings . Instance . GetValue ( SettingsKey . printer_name ) ;
2016-06-09 09:23:23 -07:00
}
2016-06-16 13:23:44 -07:00
this . AddItem (
StaticData . Instance . LoadIcon ( "icon_plus.png" , 32 , 32 ) ,
"Add New Printer..." ,
2016-06-21 19:02:04 -07:00
"new" ) . Click + = ( s , e ) = >
{
if ( AddPrinter ! = null )
{
UiThread . RunOnIdle ( ( ) = > AddPrinter ( this , null ) ) ;
}
} ;
2016-06-09 09:23:23 -07:00
}
private void SettingChanged ( object sender , EventArgs e )
{
string settingsName = ( e as StringEventArgs ) ? . Data ;
2016-07-11 15:30:12 -07:00
if ( settingsName ! = null & & settingsName = = SettingsKey . printer_name )
2016-06-09 09:23:23 -07:00
{
2016-06-15 12:58:36 -07:00
if ( ProfileManager . Instance . ActiveProfile ! = null )
2016-06-09 09:23:23 -07:00
{
2016-06-16 16:35:10 -07:00
ProfileManager . Instance . ActiveProfile . Name = ActiveSliceSettings . Instance . GetValue ( SettingsKey . printer_name ) ;
2016-07-29 11:18:37 -07:00
Rebuild ( ) ;
2016-06-09 09:23:23 -07:00
}
}
}
public override void OnClosed ( EventArgs e )
{
unregisterEvents ? . Invoke ( this , null ) ;
base . OnClosed ( e ) ;
2015-07-30 17:42:58 -07:00
}
}
}