@@ -205,44 +205,56 @@ static internal void PluginCleanUp()
205
205
/// </summary>
206
206
/// <param name="form">a Windows Form</param>
207
207
/// <param name="isDark">is Notepad++ dark mode on?</param>
208
- static internal void ToggleDarkMode ( Form form , bool isDark )
208
+ static internal void ToggleDarkMode ( Control ctrl , bool isDark )
209
209
{
210
- if ( form == null )
210
+ if ( ctrl == null )
211
211
return ;
212
212
IntPtr themePtr = notepad . GetDarkModeColors ( ) ;
213
213
if ( isDark && themePtr == IntPtr . Zero )
214
214
return ;
215
215
var theme = ( DarkModeColors ) Marshal . PtrToStructure ( themePtr , typeof ( DarkModeColors ) ) ;
216
- foreach ( Form childForm in form . OwnedForms )
216
+ if ( ctrl is Form form )
217
217
{
218
- // allow possibility that some forms will have other child forms
219
- // JsonTools does this in a couple of places
220
- ToggleDarkMode ( childForm , isDark ) ;
218
+ foreach ( Form childForm in form . OwnedForms )
219
+ {
220
+ // allow possibility that some forms will have other child forms
221
+ // JsonTools does this in a couple of places
222
+ ToggleDarkMode ( childForm , isDark ) ;
223
+ }
221
224
}
222
225
if ( isDark )
223
226
{
224
- form . BackColor = NppDarkMode . BGRToColor ( theme . Background ) ;
225
- form . ForeColor = NppDarkMode . BGRToColor ( theme . Text ) ;
227
+ ctrl . BackColor = NppDarkMode . BGRToColor ( theme . Background ) ;
228
+ ctrl . ForeColor = NppDarkMode . BGRToColor ( theme . Text ) ;
226
229
}
227
230
else
228
231
{
229
- form . ResetForeColor ( ) ;
230
- form . ResetBackColor ( ) ;
232
+ ctrl . ResetForeColor ( ) ;
233
+ ctrl . ResetBackColor ( ) ;
231
234
}
232
- foreach ( Control ctrl in form . Controls )
235
+ foreach ( Control child in ctrl . Controls )
233
236
{
234
237
if ( isDark )
235
238
{
236
239
// this doesn't actually make disabled controls have different colors
237
240
// windows forms don't make it easy for the user to choose the
238
241
// color of a disabled control. See https://stackoverflow.com/questions/136129/windows-forms-how-do-you-change-the-font-color-for-a-disabled-label
239
- var textTheme = ctrl . Enabled ? theme . Text : theme . DisabledText ;
240
- if ( ctrl is Button btn )
242
+ var textTheme = child . Enabled ? theme . Text : theme . DisabledText ;
243
+ Color foreColor = NppDarkMode . BGRToColor ( textTheme ) ;
244
+ Color backColor = NppDarkMode . BGRToColor ( theme . PureBackground ) ;
245
+ Color InBetween = Color . FromArgb (
246
+ foreColor . R / 4 + 3 * backColor . R / 4 ,
247
+ foreColor . G / 4 + 3 * backColor . G / 4 ,
248
+ foreColor . B / 4 + 3 * backColor . B / 4
249
+ ) ;
250
+ if ( child is GroupBox )
251
+ ToggleDarkMode ( child , isDark ) ;
252
+ else if ( child is Button btn )
241
253
{
242
254
btn . BackColor = NppDarkMode . BGRToColor ( theme . SofterBackground ) ;
243
- btn . ForeColor = NppDarkMode . BGRToColor ( textTheme ) ;
255
+ btn . ForeColor = foreColor ;
244
256
}
245
- else if ( ctrl is LinkLabel llbl )
257
+ else if ( child is LinkLabel llbl )
246
258
{
247
259
llbl . BackColor = NppDarkMode . BGRToColor ( theme . ErrorBackground ) ;
248
260
llbl . ForeColor = NppDarkMode . BGRToColor ( theme . DarkerText ) ;
@@ -251,36 +263,60 @@ static internal void ToggleDarkMode(Form form, bool isDark)
251
263
llbl . VisitedLinkColor = NppDarkMode . BGRToColor ( theme . DarkerText ) ;
252
264
}
253
265
// other common text-based controls
254
- else if ( ctrl is TextBox
255
- || ctrl is Label
256
- || ctrl is ListBox
257
- || ctrl is ComboBox )
266
+ else if ( child is TextBox
267
+ || child is Label
268
+ || child is ListBox
269
+ || child is ComboBox )
258
270
{
259
- ctrl . BackColor = NppDarkMode . BGRToColor ( theme . PureBackground ) ;
260
- ctrl . ForeColor = NppDarkMode . BGRToColor ( textTheme ) ;
271
+ child . BackColor = backColor ;
272
+ child . ForeColor = foreColor ;
261
273
}
262
- else if ( ctrl is TreeView tv )
274
+ else if ( child is TreeView tv )
263
275
{
264
276
tv . BackColor = NppDarkMode . BGRToColor ( theme . HotBackground ) ;
265
- tv . ForeColor = NppDarkMode . BGRToColor ( textTheme ) ;
277
+ tv . ForeColor = foreColor ;
278
+ }
279
+ else if ( child is DataGridView dgv )
280
+ {
281
+ dgv . EnableHeadersVisualStyles = false ;
282
+ dgv . BackgroundColor = InBetween ;
283
+ dgv . ForeColor = foreColor ;
284
+ dgv . GridColor = foreColor ;
285
+ dgv . ColumnHeadersDefaultCellStyle . ForeColor = foreColor ;
286
+ dgv . ColumnHeadersDefaultCellStyle . BackColor = backColor ;
287
+ dgv . RowHeadersDefaultCellStyle . ForeColor = foreColor ;
288
+ dgv . RowHeadersDefaultCellStyle . BackColor = backColor ;
289
+ dgv . RowsDefaultCellStyle . ForeColor = foreColor ;
290
+ dgv . RowsDefaultCellStyle . BackColor = backColor ;
266
291
}
267
292
else
268
293
{
269
294
// other controls I haven't thought of yet
270
- ctrl . BackColor = NppDarkMode . BGRToColor ( theme . SofterBackground ) ;
271
- ctrl . ForeColor = NppDarkMode . BGRToColor ( textTheme ) ;
295
+ child . BackColor = NppDarkMode . BGRToColor ( theme . SofterBackground ) ;
296
+ child . ForeColor = foreColor ;
272
297
}
273
298
}
274
299
else // normal light mode
275
300
{
276
- ctrl . ResetForeColor ( ) ;
277
- ctrl . ResetBackColor ( ) ;
278
- if ( ctrl is LinkLabel llbl )
301
+ child . ResetForeColor ( ) ;
302
+ child . ResetBackColor ( ) ;
303
+ if ( child is GroupBox )
304
+ ToggleDarkMode ( child , isDark ) ;
305
+ if ( child is LinkLabel llbl )
279
306
{
280
307
llbl . LinkColor = Color . Blue ;
281
308
llbl . ActiveLinkColor = Color . Red ;
282
309
llbl . VisitedLinkColor = Color . Purple ;
283
310
}
311
+ else if ( child is DataGridView dgv )
312
+ {
313
+ dgv . EnableHeadersVisualStyles = true ;
314
+ dgv . BackgroundColor = SystemColors . ControlDark ;
315
+ dgv . ForeColor = SystemColors . ControlText ;
316
+ dgv . GridColor = SystemColors . ControlLight ;
317
+ dgv . RowsDefaultCellStyle . ForeColor = SystemColors . ControlText ;
318
+ dgv . RowsDefaultCellStyle . BackColor = SystemColors . Window ;
319
+ }
284
320
}
285
321
}
286
322
Marshal . FreeHGlobal ( themePtr ) ;
0 commit comments