2015-07-30 17:42:58 -07:00
/ *
2017-06-04 16:16:07 -07:00
Copyright ( c ) 2017 , 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 .
* /
2017-06-04 16:16:07 -07:00
using System ;
using System.Linq ;
2017-06-16 18:04:47 -07:00
using MatterHackers.Agg ;
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-18 11:31:31 -07:00
using MatterHackers.MatterControl.SlicerConfiguration ;
namespace MatterHackers.MatterControl
2015-07-30 17:42:58 -07:00
{
2017-09-02 08:36:54 -07:00
public class PrinterSelector : DropDownList , IIgnoredPopupChild
2015-07-30 17:42:58 -07:00
{
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 ;
2016-12-10 14:39:15 -08:00
if ( printerID = = "new"
| | string . IsNullOrEmpty ( printerID )
| | printerID = = ActiveSliceSettings . Instance . ID )
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
{
2017-06-13 17:22:49 -07:00
if ( PrinterConnection . Instance . PrinterIsPrinting
| | PrinterConnection . Instance . PrinterIsPaused )
2016-07-06 16:40:23 -07:00
{
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 ;
2016-12-09 12:09:28 -08:00
UiThread . RunOnIdle ( ( ) = > ActiveSliceSettings . SwitchToProfile ( printerID ) ) ;
2016-07-06 16:40:23 -07:00
}
2016-05-16 16:21:42 -07:00
}
2016-04-18 11:31:31 -07:00
} ;
2016-06-09 09:23:23 -07:00
2016-12-06 16:22:28 -08:00
ActiveSliceSettings . SettingChanged . RegisterEvent ( SettingChanged , ref unregisterEvents ) ;
2016-08-19 09:27:57 -07:00
// Rebuild the droplist any time the Profiles list changes
ProfileManager . ProfilesListChanged . RegisterEvent ( ( s , e ) = > Rebuild ( ) , ref unregisterEvents ) ;
2017-09-02 08:36:54 -07:00
HAnchor = HAnchor . Fit ;
Cursor = Cursors . Hand ;
Margin = 0 ;
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
}
2017-02-09 14:33:27 -08:00
if ( ActiveSliceSettings . Instance . PrinterSelected )
2016-06-09 09:23:23 -07:00
{
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
}
}
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
}
}
}
2017-02-03 13:06:08 -08:00
public override void OnClosed ( ClosedEventArgs e )
2016-06-09 09:23:23 -07:00
{
unregisterEvents ? . Invoke ( this , null ) ;
base . OnClosed ( e ) ;
2015-07-30 17:42:58 -07:00
}
}
}