Menu ▾ ▴

#141 Input validator: error color is not visible on a themed csDropDownList combobox

open
nobody
None
2 days ago
2 days ago
No

Symptom

With visual themes enabled (Common Controls 6 manifest), ControlValidator(cmb, iv).Validate(False, ...) on a TComboBox with Style = csDropDownList sets the combobox's Color to the error color, but the control still looks exactly like a valid one. The user gets no visual hint which combobox is wrong; only the status bar message shows the error. Comboboxes with Style = csDropDown and edits are fine.

Cause

TComboboxValidator does not override doSetColor, so TControlValidator.doSetColor only sets Control.Color. A themed drop down list is drawn like a button by the theme engine and ignores its Color.

Suggested fix

Override doSetColor in TComboboxValidator and turn off theming for the control (SetWindowTheme(Handle, '', '')) while it shows a color other than the OK color; turn it back on (SetWindowTheme(Handle, nil, nil)) when the input is valid again. GetWindowTheme tells whether theming is currently on, so the theme is only changed when necessary. Patch against u_dzInputValidator.pas:

@@ -531,6 +531,7 @@

 uses
   Math,

+  UxTheme,
   u_dzVclUtils,
   u_dzFileUtils,
   u_dzDateUtils,
@@ -772,6 +773,12 @@
     function ValidateAnyItemSelected(out _Text: string): Boolean; overload;
     function ValidateAnyItemSelected(out _Idx: Integer): Boolean; overload;
     function ValidateAnyItemSelected(out _Obj: Pointer): Boolean; overload;
+  protected
+    ///<summary>
+    /// Sets the color and, for a csDropDownList combobox with visual themes enabled, turns
+    /// off theming while the color is not the OK color, because a themed drop down list
+    /// is drawn like a button and ignores its Color. </summary>
+    procedure doSetColor(_Color: TColor); override;
   public
     constructor Create(_cmb: TCustomComboBox; _Validator: IdzInputValidator;
       _Container: IdzControlTextSource = nil);
@@ -1930,6 +1937,27 @@
   Result := TComboBoxHack(FControl as TCustomComboBox);
 end;

+procedure TComboboxValidator.doSetColor(_Color: TColor);
+var

+  cmb: TComboBoxHack;
+  ShouldBeThemed: Boolean;
+  IsThemed: Boolean;
+begin
+  inherited;
+  cmb := Combo;
+  if (cmb.Style = csDropDownList) and cmb.HandleAllocated and IsThemesEnabled then begin
+    ShouldBeThemed := (_Color = FValidator.OkColor);
+    // GetWindowTheme returns 0 after SetWindowTheme(Handle, '', '') turned theming off
+    IsThemed := (GetWindowTheme(cmb.Handle) <> 0);
+    if ShouldBeThemed <> IsThemed then begin
+      if ShouldBeThemed then
+        SetWindowTheme(cmb.Handle, nil, nil)
+      else
+        SetWindowTheme(cmb.Handle, '', '');
+    end;
+  end;
+end;
+
 function TComboboxValidator.AsIpv4: IdzTextControlIpV4Validator;
 begin
   Result := TTextControlIpV4Validator.Create(FControl, FValidator, FContainer);

Tested with Delphi 12 (Win64) and runtime themes: two comboboxes with a duplicate value are drawn unthemed with the error color, and both return to the themed look once the duplicate is resolved. Not yet tested with Delphi 2007, where GetWindowTheme/SetWindowTheme are function variables in UxTheme; they are loaded when themes are enabled, and the code only calls them in that case.

Discussion


Log in to post a comment.