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 ;
2018-04-18 11:50:21 -07:00
using MatterHackers.Agg.Image ;
2016-04-29 15:29:31 -07:00
using MatterHackers.Agg.UI ;
2016-04-18 11:31:31 -07:00
using MatterHackers.Localizations ;
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
2018-04-17 17:13:18 -07:00
public PrinterSelector ( ThemeConfig theme )
2018-01-12 12:57:53 -08:00
: base ( "Printers" . Localize ( ) + "... " , theme . Colors . PrimaryTextColor , pointSize : theme . DefaultFontSize )
2015-07-30 17:42:58 -07:00
{
2016-08-01 17:21:31 -07:00
this . Name = "Printers... Menu" ;
2018-04-17 17:13:18 -07:00
this . BorderColor = Color . Transparent ;
2018-04-18 11:50:21 -07:00
this . AutoScaleIcons = false ;
2018-04-17 17:13:18 -07:00
this . BackgroundColor = theme . MinimalShade ;
2018-04-19 18:12:14 -07:00
this . GutterWidth = 30 ;
this . MenuItemsTextHoverColor = new Color ( "#ddd" ) ;
this . Rebuild ( ) ;
2016-04-18 11:31:31 -07:00
this . SelectionChanged + = ( s , e ) = >
{
2016-05-16 16:21:42 -07:00
string printerID = this . SelectedValue ;
2018-04-17 17:13:18 -07:00
if ( printerID = = "new"
| | string . IsNullOrEmpty ( printerID )
2016-12-10 14:39:15 -08:00
| | 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
{
2018-04-01 09:26:47 -07:00
// TODO: when this opens a new tab we will not need to check any printer
2017-09-17 21:08:16 -07:00
if ( ApplicationController . Instance . ActivePrinter . Connection . PrinterIsPrinting
| | ApplicationController . Instance . ActivePrinter . Connection . PrinterIsPaused )
2016-07-06 16:40:23 -07:00
{
if ( this . SelectedIndex ! = lastSelectedIndex )
{
UiThread . RunOnIdle ( ( ) = >
2017-10-18 19:54:06 -07:00
StyledMessageBox . ShowMessageBox ( "Please wait until the print has finished and try again." . Localize ( ) , "Can't switch printers while printing" . Localize ( ) )
2016-07-06 16:40:23 -07:00
) ;
this . SelectedIndex = lastSelectedIndex ;
}
}
else
{
lastSelectedIndex = this . SelectedIndex ;
2018-04-17 17:13:18 -07:00
ProfileManager . Instance . LastProfileID = this . SelectedValue ;
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
2018-04-17 17:13:18 -07:00
ActiveSliceSettings . SettingChanged . RegisterEvent ( ( s , e ) = >
{
string settingsName = ( e as StringEventArgs ) ? . Data ;
if ( settingsName ! = null & & settingsName = = SettingsKey . printer_name )
{
if ( ProfileManager . Instance . ActiveProfile ! = null )
{
ProfileManager . Instance . ActiveProfile . Name = ActiveSliceSettings . Instance . GetValue ( SettingsKey . printer_name ) ;
Rebuild ( ) ;
}
}
} , ref unregisterEvents ) ;
2016-08-19 09:27:57 -07:00
2018-08-13 13:50:35 -07:00
// Rebuild the droplist any time the ActivePrinter changes
ActiveSliceSettings . ActivePrinterChanged . RegisterEvent ( ( s , e ) = >
{
this . Rebuild ( ) ;
} , ref unregisterEvents ) ;
2016-08-19 09:27:57 -07:00
// Rebuild the droplist any time the Profiles list changes
2018-04-17 17:13:18 -07:00
ProfileManager . ProfilesListChanged . RegisterEvent ( ( s , e ) = >
{
this . 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 ( ) ;
2018-08-06 13:17:29 -07:00
// Always reset to -1, then search for match below
this . SelectedIndex = - 1 ;
2016-06-09 09:23:23 -07:00
//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
{
2018-04-18 11:50:21 -07:00
this . AddItem ( this . GetOemIcon ( printer . Make ) , printer . Name , printer . ID ) ;
2016-06-09 09:23:23 -07:00
}
2018-04-17 17:13:18 -07:00
string lastProfileID = ProfileManager . Instance . LastProfileID ;
if ( ! string . IsNullOrEmpty ( lastProfileID ) )
2016-06-09 09:23:23 -07:00
{
2018-04-17 17:13:18 -07:00
this . SelectedValue = lastProfileID ;
2016-07-06 16:40:23 -07:00
lastSelectedIndex = this . SelectedIndex ;
2016-06-09 09:23:23 -07:00
}
2018-06-05 18:18:47 -07:00
else
{
this . SelectedIndex = - 1 ;
}
2016-06-09 09:23:23 -07:00
}
2018-04-18 11:50:21 -07:00
private ImageBuffer GetOemIcon ( string oemName )
{
var imageBuffer = new ImageBuffer ( 16 , 16 ) ;
2018-04-18 12:56:12 -07:00
ApplicationController . Instance . DownloadToImageAsync (
imageBuffer ,
ApplicationController . Instance . GetFavIconUrl ( oemName ) ,
scaleToImageX : false ) ;
2018-04-18 11:50:21 -07:00
return imageBuffer ;
}
2018-08-23 16:44:11 -07:00
public override void OnClosed ( EventArgs e )
2016-06-09 09:23:23 -07:00
{
unregisterEvents ? . Invoke ( this , null ) ;
base . OnClosed ( e ) ;
2015-07-30 17:42:58 -07:00
}
2018-09-11 10:57:53 -07:00
public bool KeepMenuOpen ( )
{
return false ;
}
2015-07-30 17:42:58 -07:00
}
}