2020-05-03 08:39:35 +00:00
using System ;
using System.IO ;
using System.Collections.Generic ;
using System.Linq ;
using System.Threading.Tasks ;
using System.Windows.Forms ;
using HeliosPlus.Resources ;
using HeliosPlus.Shared ;
2020-05-10 10:47:18 +00:00
using Manina.Windows.Forms ;
using System.Text.RegularExpressions ;
2020-10-09 03:27:59 +00:00
using System.Diagnostics ;
2020-05-03 08:39:35 +00:00
namespace HeliosPlus.UIForms
{
internal partial class DisplayProfileForm : Form
{
2020-06-07 08:48:45 +00:00
private ProfileItem _selectedProfile ;
2020-06-15 09:57:46 +00:00
//private List<ProfileItem> _savedProfiles = new List<ProfileItem>();
2020-05-09 13:02:07 +00:00
private string _saveOrRenameMode = "save" ;
2020-06-15 09:57:46 +00:00
//private static bool _inDialog = false;
2020-06-07 08:48:45 +00:00
private static ProfileItem _profileToLoad = null ;
2020-07-15 08:11:38 +00:00
private ProfileAdaptor _profileAdaptor = new ProfileAdaptor ( ) ;
2020-05-15 11:08:44 +00:00
2020-05-03 08:39:35 +00:00
public DisplayProfileForm ( )
{
InitializeComponent ( ) ;
2020-05-11 11:11:26 +00:00
this . AcceptButton = this . btn_save_or_rename ;
2020-05-03 08:39:35 +00:00
}
2020-06-07 08:48:45 +00:00
public DisplayProfileForm ( ProfileItem profileToLoad ) : this ( )
2020-05-15 11:08:44 +00:00
{
_profileToLoad = profileToLoad ;
}
2020-05-03 08:39:35 +00:00
private void Apply_Click ( object sender , EventArgs e )
{
2020-07-24 01:11:42 +00:00
if ( ! ( _selectedProfile is ProfileItem ) )
return ;
2020-05-13 11:04:18 +00:00
if ( ! _selectedProfile . IsPossible )
2020-05-03 08:39:35 +00:00
{
2020-05-13 11:04:18 +00:00
MessageBox . Show ( this , Language . This_profile_is_currently_impossible_to_apply ,
Language . Apply_Profile ,
MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
2020-05-03 08:39:35 +00:00
2020-05-13 11:04:18 +00:00
return ;
}
2020-05-03 08:39:35 +00:00
2020-07-24 01:11:42 +00:00
// Apply the Profile
2020-10-04 10:01:03 +00:00
//ProfileRepository.ApplyProfile(_selectedProfile);
Program . ApplyProfile ( _selectedProfile ) ;
2020-07-23 06:31:00 +00:00
2020-05-03 08:39:35 +00:00
}
2020-07-23 23:06:33 +00:00
2020-05-03 08:39:35 +00:00
private void Exit_Click ( object sender , EventArgs e )
{
2020-05-17 09:19:55 +00:00
DialogResult = DialogResult . Cancel ;
2020-05-03 08:39:35 +00:00
this . Close ( ) ;
}
private void Delete_Click ( object sender , EventArgs e )
{
2020-07-24 01:11:42 +00:00
if ( ! ( _selectedProfile is ProfileItem ) )
return ;
2020-05-15 03:41:01 +00:00
if ( MessageBox . Show ( $"Are you sure you want to delete the '{_selectedProfile.Name}' Display Profile?" , $"Delete '{_selectedProfile.Name}' Display Profile?" , MessageBoxButtons . YesNo , MessageBoxIcon . Error ) = = DialogResult . No )
return ;
2020-05-12 10:46:23 +00:00
// remove the profile from the imagelistview
int currentIlvIndex = ilv_saved_profiles . SelectedItems [ 0 ] . Index ;
ilv_saved_profiles . Items . RemoveAt ( currentIlvIndex ) ;
2020-06-14 04:20:52 +00:00
// Remove the Profile
ProfileRepository . RemoveProfile ( _selectedProfile ) ;
_selectedProfile = null ;
2020-05-03 08:39:35 +00:00
2020-05-12 10:46:23 +00:00
// If the imageview isn't empty
if ( ilv_saved_profiles . Items . Count > 0 )
{
// set the new selected profile as the next one in the imagelistview
// or the new end one if we deleted the last one before
int ilvItemToSelect = currentIlvIndex ;
if ( ilv_saved_profiles . Items . Count < currentIlvIndex + 1 )
ilvItemToSelect = ilv_saved_profiles . Items . Count - 1 ;
// Set the nearest profile image as selected
ilv_saved_profiles . Items [ ilvItemToSelect ] . Selected = true ;
// select the
2020-06-15 09:57:46 +00:00
foreach ( ProfileItem newSelectedProfile in ProfileRepository . AllProfiles )
2020-05-03 08:39:35 +00:00
{
2020-05-12 10:46:23 +00:00
if ( newSelectedProfile . Name . Equals ( ilv_saved_profiles . Items [ ilvItemToSelect ] . Text ) )
{
ChangeSelectedProfile ( newSelectedProfile ) ;
}
2020-05-03 08:39:35 +00:00
}
2020-05-12 10:46:23 +00:00
}
else
{
// We now only have an unsaved current profile, and no saved ones
// So we need to change the mode
2020-06-14 04:20:52 +00:00
ChangeSelectedProfile ( ProfileRepository . CurrentProfile ) ;
2020-05-03 08:39:35 +00:00
2020-05-12 10:46:23 +00:00
}
2020-05-03 08:39:35 +00:00
}
2020-05-15 03:41:01 +00:00
private void RefreshDisplayProfileUI ( )
2020-05-03 08:39:35 +00:00
{
2020-05-15 03:41:01 +00:00
2020-07-24 01:11:42 +00:00
ImageListViewItem newItem = null ;
2020-06-15 09:57:46 +00:00
// Temporarily stop updating the saved_profiles listview
2020-07-24 01:11:42 +00:00
// To stop the display showing all sorts of changes happening
2020-06-15 09:57:46 +00:00
ilv_saved_profiles . SuspendLayout ( ) ;
2020-05-03 08:39:35 +00:00
2020-07-24 01:11:42 +00:00
// Empty the imageListView
ilv_saved_profiles . Items . Clear ( ) ;
2020-05-15 03:41:01 +00:00
2020-07-24 01:11:42 +00:00
// Fill it back up with the Profiles we have
foreach ( ProfileItem profile in ProfileRepository . AllProfiles )
{
// Create a new ImageListViewItem from the profile
newItem = new ImageListViewItem ( profile , profile . Name ) ;
2020-06-15 09:57:46 +00:00
2020-07-24 01:11:42 +00:00
// Select it if its the selectedProfile
if ( _selectedProfile is ProfileItem & & _selectedProfile . Equals ( profile ) )
newItem . Selected = true ;
2020-05-03 08:39:35 +00:00
2020-07-24 01:11:42 +00:00
// Add it to the list!
ilv_saved_profiles . Items . Add ( newItem , _profileAdaptor ) ;
2020-06-15 09:57:46 +00:00
2020-05-15 03:41:01 +00:00
}
2020-06-15 09:57:46 +00:00
// Restart updating the saved_profiles listview
ilv_saved_profiles . ResumeLayout ( ) ;
2020-05-03 08:39:35 +00:00
}
2020-05-15 03:41:01 +00:00
private void DisplayProfileForm_Activated ( object sender , EventArgs e )
2020-05-03 08:39:35 +00:00
{
2020-05-15 03:41:01 +00:00
// We handle the UI updating in DisplayProfileForm_Activated so that
// the app will check for changes to the current profile when the
// user clicks back to this app. This is designed to allow people to
// alter their Windows Display settings then come back to our app
// and the app will automatically recognise that things have changed.
2020-10-09 03:27:59 +00:00
// Reload the profiles in case we swapped to another program to change it
//ChangeSelectedProfile(ProfileRepository.CurrentProfile);
2020-05-15 03:41:01 +00:00
// Refresh the Profile UI
2020-10-09 03:27:59 +00:00
//RefreshDisplayProfileUI();
2020-05-03 08:39:35 +00:00
}
2020-05-15 03:41:01 +00:00
private void DisplayProfileForm_Load ( object sender , EventArgs e )
2020-05-03 08:39:35 +00:00
{
2020-10-09 03:27:59 +00:00
2020-05-15 03:41:01 +00:00
// Update the Current Profile
2020-10-09 03:27:59 +00:00
ProfileRepository . UpdateActiveProfile ( ) ;
ChangeSelectedProfile ( ProfileRepository . CurrentProfile ) ;
2020-09-18 10:52:18 +00:00
2020-05-15 03:41:01 +00:00
// Refresh the Profile UI
RefreshDisplayProfileUI ( ) ;
2020-05-03 08:39:35 +00:00
}
2020-05-10 10:47:18 +00:00
2020-06-07 08:48:45 +00:00
private void ChangeSelectedProfile ( ProfileItem profile )
2020-05-09 13:02:07 +00:00
{
2020-05-10 10:47:18 +00:00
// And we need to update the actual selected profile too!
_selectedProfile = profile ;
2020-05-11 11:11:26 +00:00
// We also need to load the saved profile name to show the user
lbl_profile_shown . Text = _selectedProfile . Name ;
// And update the save/rename textbox
txt_profile_save_name . Text = _selectedProfile . Name ;
2020-10-09 03:27:59 +00:00
if ( ProfileRepository . ContainsProfile ( profile ) )
2020-05-11 11:11:26 +00:00
{
2020-06-15 09:57:46 +00:00
// we already have the profile stored
_saveOrRenameMode = "rename" ;
btn_save_or_rename . Text = "Rename To" ;
if ( ! _selectedProfile . IsPossible )
{
lbl_profile_shown_subtitle . Text = "(Display Profile is not valid so cannot be used)" ;
btn_apply . Visible = false ;
}
else
{
lbl_profile_shown_subtitle . Text = "" ;
2020-07-24 01:11:42 +00:00
if ( ProfileRepository . IsActiveProfile ( _selectedProfile ) )
btn_apply . Visible = false ;
else
btn_apply . Visible = true ;
2020-06-15 09:57:46 +00:00
}
}
else
{
// we don't have the profile stored yet
_saveOrRenameMode = "save" ;
btn_save_or_rename . Text = "Save As" ;
lbl_profile_shown_subtitle . Text = "(Current Display Profile in use - UNSAVED)" ;
btn_apply . Visible = false ;
}
2020-05-15 03:41:01 +00:00
// Refresh the image list view
2020-07-24 01:11:42 +00:00
//RefreshImageListView(profile);
2020-05-15 03:41:01 +00:00
2020-05-10 10:47:18 +00:00
// And finally show the profile in the display view
dv_profile . Profile = profile ;
dv_profile . Refresh ( ) ;
2020-05-15 03:41:01 +00:00
2020-05-10 10:47:18 +00:00
}
2020-05-09 13:02:07 +00:00
2020-05-10 10:47:18 +00:00
private void btn_save_as_Click ( object sender , EventArgs e )
2020-05-09 13:02:07 +00:00
{
2020-05-10 10:47:18 +00:00
// Check the name is valid
2020-05-17 09:19:55 +00:00
if ( ! Program . IsValidFilename ( txt_profile_save_name . Text ) )
2020-05-10 10:47:18 +00:00
{
MessageBox . Show ( "The profile name cannot contain the following characters:" + Path . GetInvalidFileNameChars ( ) , "Invalid characters in profile name" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
return ;
}
2020-05-11 11:11:26 +00:00
// Check we're not already using the name
2020-06-14 04:20:52 +00:00
foreach ( ProfileItem savedProfile in ProfileRepository . AllProfiles )
2020-05-10 10:47:18 +00:00
{
2020-05-11 11:11:26 +00:00
//if (String.Equals(txt_profile_save_name.Text, savedProfile.Name, StringComparison.InvariantCultureIgnoreCase))
if ( savedProfile . Name . Equals ( txt_profile_save_name . Text ) )
2020-05-10 10:47:18 +00:00
{
2020-05-11 11:11:26 +00:00
MessageBox . Show ( "Sorry, each saved display profile needs a unique name." , "Profile name already exists" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
return ;
2020-05-10 10:47:18 +00:00
}
2020-05-11 11:11:26 +00:00
}
// If we're saving the current profile as a new item
// then we'll be in "save" mode
if ( _saveOrRenameMode = = "save" )
{
2020-05-10 10:47:18 +00:00
// We're in 'save' mode!
2020-05-12 10:46:23 +00:00
// Check we're not already saving this profile
2020-06-14 04:20:52 +00:00
foreach ( ProfileItem savedProfile in ProfileRepository . AllProfiles )
2020-05-12 10:46:23 +00:00
{
//if (String.Equals(txt_profile_save_name.Text, savedProfile.Name, StringComparison.InvariantCultureIgnoreCase))
if ( savedProfile . Equals ( _selectedProfile ) )
{
MessageBox . Show ( $"Sorry, this display profile was already saved as '{savedProfile.Name}'." , "Profile already saved" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
return ;
}
}
2020-05-11 11:11:26 +00:00
// So we've already passed the check that says this profile is unique
2020-05-12 10:46:23 +00:00
// Update the name just to make sure we record it if the user changed it
_selectedProfile . Name = txt_profile_save_name . Text ;
2020-06-14 04:20:52 +00:00
// Add the current profile to the list of profiles so it gets saved
ProfileRepository . AddProfile ( _selectedProfile ) ;
2020-05-11 11:11:26 +00:00
2020-05-12 10:46:23 +00:00
// Also update the imagelistview so that we can see the new profile we just saved
2020-05-10 10:47:18 +00:00
// Load the currentProfile image into the imagelistview
2020-05-16 03:58:59 +00:00
//ImageListViewItem newItem = new ImageListViewItem(_selectedProfile.SavedProfileCacheFilename, _selectedProfile.Name);
ImageListViewItem newItem = new ImageListViewItem ( _selectedProfile , _selectedProfile . Name ) ;
2020-05-10 10:47:18 +00:00
newItem . Selected = true ;
2020-05-16 03:58:59 +00:00
//ilv_saved_profiles.Items.Add(newItem);
ilv_saved_profiles . Items . Add ( newItem , _profileAdaptor ) ;
2020-05-10 10:47:18 +00:00
}
else
{
// We're in 'rename' mode!
2020-05-12 10:46:23 +00:00
// Check the name is the same, and if so do nothing
if ( _selectedProfile . Name . Equals ( txt_profile_save_name . Text ) )
{
return ;
}
2020-05-11 11:11:26 +00:00
// Lets save the old names for usage next
2020-05-10 10:47:18 +00:00
string oldProfileName = _selectedProfile . Name ;
2020-05-11 11:11:26 +00:00
// Lets rename the entry in the imagelistview to the new name
2020-05-10 10:47:18 +00:00
foreach ( ImageListViewItem myItem in ilv_saved_profiles . Items )
{
if ( myItem . Text = = oldProfileName )
{
myItem . Text = txt_profile_save_name . Text ;
}
}
2020-05-11 11:11:26 +00:00
// Lets rename the selectedProfile to the new name
2020-06-14 04:20:52 +00:00
ProfileRepository . RenameProfile ( _selectedProfile , txt_profile_save_name . Text ) ;
2020-06-15 09:57:46 +00:00
2020-05-11 11:11:26 +00:00
// Lets update the rest of the profile screen too
lbl_profile_shown . Text = txt_profile_save_name . Text ;
2020-06-15 09:57:46 +00:00
// And we also need to go through the Shortcuts in the library and rename them!
ShortcutRepository . RenameShortcutProfile ( _selectedProfile ) ;
2020-06-14 04:20:52 +00:00
2020-06-15 09:57:46 +00:00
2020-05-10 10:47:18 +00:00
}
2020-05-12 10:46:23 +00:00
ChangeSelectedProfile ( _selectedProfile ) ;
2020-05-10 10:47:18 +00:00
// now update the profiles image listview
//ilv_saved_profiles.Refresh();
2020-05-09 13:02:07 +00:00
}
2020-05-10 10:47:18 +00:00
private void ilv_saved_profiles_ItemClick ( object sender , ItemClickEventArgs e )
2020-05-09 13:02:07 +00:00
{
2020-06-15 09:57:46 +00:00
foreach ( ProfileItem savedProfile in ProfileRepository . AllProfiles )
2020-05-09 13:02:07 +00:00
{
2020-05-10 10:47:18 +00:00
if ( savedProfile . Name = = e . Item . Text )
2020-05-09 13:02:07 +00:00
{
2020-05-10 10:47:18 +00:00
ChangeSelectedProfile ( savedProfile ) ;
2020-05-09 13:02:07 +00:00
}
}
2020-05-10 10:47:18 +00:00
}
2020-05-09 13:02:07 +00:00
2020-05-10 10:47:18 +00:00
private void btn_view_current_Click ( object sender , EventArgs e )
{
2020-05-15 03:41:01 +00:00
// Reload the profiles in case we swapped to another program to change it
2020-08-19 06:55:50 +00:00
//ProfileRepository.UpdateCurrentProfile();
ProfileRepository . GetActiveProfile ( ) ;
2020-07-24 01:11:42 +00:00
// Change to the current selected Profile
ChangeSelectedProfile ( ProfileRepository . GetActiveProfile ( ) ) ;
2020-05-15 03:41:01 +00:00
// Refresh the Profile UI
RefreshDisplayProfileUI ( ) ;
2020-05-09 13:02:07 +00:00
}
2020-05-11 11:11:26 +00:00
private void txt_profile_save_name_KeyDown ( object sender , KeyEventArgs e )
{
if ( e . KeyCode . Equals ( Keys . Enter ) )
{
MessageBox . Show ( "Click works!" , "Click works" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
}
2020-10-09 03:27:59 +00:00
protected override void WndProc ( ref Message m )
{
const int WM_DISPLAYCHANGE = 0x007E ;
const int x_bitshift = 0 ;
const int y_bitshift = 16 ;
const int xy_mask = 0xFFFF ;
bool displayChange = false ;
switch ( m . Msg )
{
case WM_DISPLAYCHANGE :
ProfileRepository . UpdateActiveProfile ( ) ;
break ;
}
base . WndProc ( ref m ) ;
}
/ * private static void MainWindow_Closed ( object sender , EventArgs e )
{
DeviceNotification . UnRegisterUsbDeviceNotification ( ) ;
DeviceNotification . UnRegisterMonitorDeviceNotification ( ) ;
}
protected override void OnSourceInitialized ( EventArgs e )
{
base . OnSourceInitialized ( e ) ;
if ( ! ( PresentationSource . FromVisual ( this ) is HwndSource source ) )
return ;
source . AddHook ( this . WndProc ) ;
DeviceNotification . RegisterUsbDeviceNotification ( source . Handle ) ;
DeviceNotification . RegisterMonitorDeviceNotification ( source . Handle ) ;
} * /
// Notification registeration for display detection from here http://codetips.nl/detectmonitor.html
/ * protected override void WndProc ( ref Message m )
{
const int WM_SETTINGCHANGE = 0x001A ;
const int SPI_SETWORKAREA = 0x02F ;
const int WM_DISPLAYCHANGE = 0x007E ;
const int WM_DEVICECHANGE = 0x0219 ; // WM Device change message ID
const int DBT_DEVICEARRIVAL = 0x8000 ; // WM Device Change Event: System detected a new device
const int DBT_DEVICEREMOVECOMPLETE = 0x8004 ; // WM Device Change Event: Device is gone
const int x_bitshift = 0 ;
const int y_bitshift = 16 ;
const int xy_mask = 0xFFFF ;
bool displayChange = false ;
switch ( m . Msg )
{
case WM_DISPLAYCHANGE :
case DeviceNotification . DbtDeviceRemoveComplete :
case DeviceNotification . DbtDeviceArrival :
{
if ( DeviceNotification . IsMonitor ( lParam ) )
Debug . WriteLine ( $"Monitor {((int)wParam == DeviceNotification.DbtDeviceArrival ? " arrived " : " removed ")}" ) ;
if ( DeviceNotification . IsUsbDevice ( lParam ) )
Debug . WriteLine ( $"Usb device {((int)wParam == DeviceNotification.DbtDeviceArrival ? " arrived " : " removed ")}" ) ;
}
break ;
}
base . WndProc ( ref m ) ;
} * /
2020-05-03 08:39:35 +00:00
}
}