22using System ;
33using System . IO ;
44using System . Text ;
5+ using System . Collections . Generic ;
56using System . Drawing ;
7+ using System . Reflection ;
68using System . Threading ;
79using System . Windows . Forms ;
810using System . Drawing . Imaging ;
@@ -38,7 +40,13 @@ public static void OnNotification(ScNotification notification)
3840 {
3941 if ( notification . Header . Code == ( uint ) SciMsg . SCN_CHARADDED )
4042 {
41- Kbg . Demo . Namespace . Main . doInsertHtmlCloseTag ( ( char ) notification . Character ) ;
43+ Demo . Namespace . Main . doInsertHtmlCloseTag ( ( char ) notification . Character ) ;
44+ }
45+ // dark mode (de-)activated
46+ if ( notification . Header . Code == ( uint ) NppMsg . NPPN_DARKMODECHANGED )
47+ {
48+ INotepadPPGateway notepad = new NotepadPPGateway ( ) ;
49+ Demo . Namespace . Main . ToggleDarkMode ( Demo . Namespace . Main . frmGoToLine , notepad . IsDarkModeEnabled ( ) ) ;
4250 }
4351 }
4452
@@ -57,7 +65,7 @@ class Main
5765 static string keyName = "doCloseTag" ;
5866 static bool doCloseTag = false ;
5967 static string sessionFilePath = @"C:\text.session" ;
60- static frmGoToLine frmGoToLine = null ;
68+ static internal frmGoToLine frmGoToLine = null ;
6169 static internal int idFrmGotToLine = - 1 ;
6270
6371 // toolbar icons
@@ -177,6 +185,104 @@ static internal void PluginCleanUp()
177185 {
178186 Win32 . WritePrivateProfileString ( sectionName , keyName , doCloseTag ? "1" : "0" , iniFilePath ) ;
179187 }
188+
189+ /// <summary>
190+ /// Apply dark mode (or re-apply light mode) to the controls of any form.<br></br>
191+ /// This method currently supports colorizing the following types of controls:<br></br>
192+ /// - Buttons<br></br>
193+ /// - Labels<br></br>
194+ /// - LinkLabels<br></br>
195+ /// - ComboBoxes<br></br>
196+ /// - CheckBoxes<br></br>
197+ /// - ListBoxes<br></br>
198+ /// - TreeViews<br></br>
199+ /// Feel free to add more as needed.<br></br>
200+ /// TODO: Figure out best way to customize border colors of controls.
201+ /// https://stackoverflow.com/questions/1445472/how-to-change-the-form-border-color-c
202+ /// may be a lead.
203+ /// </summary>
204+ /// <param name="form">a Windows Form</param>
205+ /// <param name="isDark">is Notepad++ dark mode on?</param>
206+ static internal void ToggleDarkMode ( Form form , bool isDark )
207+ {
208+ if ( form == null )
209+ return ;
210+ IntPtr themePtr = notepad . GetDarkModeColors ( ) ;
211+ if ( isDark && themePtr == IntPtr . Zero )
212+ return ;
213+ var theme = ( DarkModeColors ) Marshal . PtrToStructure ( themePtr , typeof ( DarkModeColors ) ) ;
214+ foreach ( Form childForm in form . OwnedForms )
215+ {
216+ // allow possibility that some forms will have other child forms
217+ // JsonTools does this in a couple of places
218+ ToggleDarkMode ( childForm , isDark ) ;
219+ }
220+ if ( isDark )
221+ {
222+ form . BackColor = NppDarkMode . BGRToColor ( theme . Background ) ;
223+ form . ForeColor = NppDarkMode . BGRToColor ( theme . Text ) ;
224+ }
225+ else
226+ {
227+ form . ResetForeColor ( ) ;
228+ form . ResetBackColor ( ) ;
229+ }
230+ foreach ( Control ctrl in form . Controls )
231+ {
232+ if ( isDark )
233+ {
234+ // this doesn't actually make disabled controls have different colors
235+ // windows forms don't make it easy for the user to choose the
236+ // color of a disabled control. See https://stackoverflow.com/questions/136129/windows-forms-how-do-you-change-the-font-color-for-a-disabled-label
237+ var textTheme = ctrl . Enabled ? theme . Text : theme . DisabledText ;
238+ if ( ctrl is Button btn )
239+ {
240+ btn . BackColor = NppDarkMode . BGRToColor ( theme . SofterBackground ) ;
241+ btn . ForeColor = NppDarkMode . BGRToColor ( textTheme ) ;
242+ }
243+ else if ( ctrl is LinkLabel llbl )
244+ {
245+ llbl . BackColor = NppDarkMode . BGRToColor ( theme . ErrorBackground ) ;
246+ llbl . ForeColor = NppDarkMode . BGRToColor ( theme . DarkerText ) ;
247+ llbl . LinkColor = NppDarkMode . BGRToColor ( theme . LinkText ) ;
248+ llbl . ActiveLinkColor = NppDarkMode . BGRToColor ( theme . Text ) ;
249+ llbl . VisitedLinkColor = NppDarkMode . BGRToColor ( theme . DarkerText ) ;
250+ }
251+ // other common text-based controls
252+ else if ( ctrl is TextBox
253+ || ctrl is Label
254+ || ctrl is ListBox
255+ || ctrl is ComboBox )
256+ {
257+ ctrl . BackColor = NppDarkMode . BGRToColor ( theme . PureBackground ) ;
258+ ctrl . ForeColor = NppDarkMode . BGRToColor ( textTheme ) ;
259+ }
260+ else if ( ctrl is TreeView tv )
261+ {
262+ tv . BackColor = NppDarkMode . BGRToColor ( theme . HotBackground ) ;
263+ tv . ForeColor = NppDarkMode . BGRToColor ( textTheme ) ;
264+ }
265+ else
266+ {
267+ // other controls I haven't thought of yet
268+ ctrl . BackColor = NppDarkMode . BGRToColor ( theme . SofterBackground ) ;
269+ ctrl . ForeColor = NppDarkMode . BGRToColor ( textTheme ) ;
270+ }
271+ }
272+ else // normal light mode
273+ {
274+ ctrl . ResetForeColor ( ) ;
275+ ctrl . ResetBackColor ( ) ;
276+ if ( ctrl is LinkLabel llbl )
277+ {
278+ llbl . LinkColor = Color . Blue ;
279+ llbl . ActiveLinkColor = Color . Red ;
280+ llbl . VisitedLinkColor = Color . Purple ;
281+ }
282+ }
283+ }
284+ Marshal . FreeHGlobal ( themePtr ) ;
285+ }
180286 #endregion
181287
182288 #region " Menu functions "
@@ -441,6 +547,7 @@ static void DockableDlgDemo()
441547 Win32 . SendMessage ( PluginBase . nppData . _nppHandle , ( uint ) NppMsg . NPPM_SETMENUITEMCHECK , PluginBase . _funcItems . Items [ idFrmGotToLine ] . _cmdID , 0 ) ;
442548 }
443549 }
550+ ToggleDarkMode ( frmGoToLine , notepad . IsDarkModeEnabled ( ) ) ;
444551 frmGoToLine . textBox1 . Focus ( ) ;
445552 }
446553 #endregion
0 commit comments