Qt: QtThemes - added DrawText(), teach treeview to draw items text, added DTFlagsToQtFlags() for reuse

git-svn-id: trunk@24890 -
This commit is contained in:
zeljko 2010-04-24 18:28:29 +00:00
parent 0f7ab11664
commit 638f1c3e07
3 changed files with 125 additions and 47 deletions

View File

@ -162,6 +162,7 @@ type
procedure EventTrace(message : string; data : pointer);
function HwndFromWidgetH(const WidgetH: QWidgetH): HWND;
function DTFlagsToQtFlags(const Flags: Cardinal): Integer;
const
@ -197,6 +198,54 @@ const
KEYMAP_TOGGLE = $20000;
KEYMAP_EXTENDED = $40000;
function DTFlagsToQtFlags(const Flags: Cardinal): Integer;
const
QtTextSingleLine = $0100;
QtTextDontClip = $0200;
QtTextExpandTabs = $0400;
QtTextShowMnemonic = $0800;
QtTextWordWrap = $1000;
QtTextWrapAnywhere = $2000;
QtTextHideMnemonic = $8000;
QtTextDontPrint = $4000;
QtTextIncludeTrailingSpaces = $08000000;
QtTextJustificationForced = $10000;
begin
Result := 0;
// horizontal alignment
if Flags and DT_CENTER <> 0 then
Result := Result or QtAlignHCenter
else
if Flags and DT_RIGHT <> 0 then
Result := Result or QtAlignRight
else
Result := Result or QtAlignLeft;
// vertical alignment
if Flags and DT_VCENTER <> 0 then
Result := Result or QtAlignVCenter
else
if Flags and DT_BOTTOM <> 0 then
Result := Result or QtAlignBottom
else
Result := Result or QtAlignTop;
// mutually exclusive wordbreak and singleline
if Flags and DT_WORDBREAK <> 0 then
Result := Result or QtTextWordWrap
else
if Flags and DT_SINGLELINE <> 0 then
Result := Result or QtTextSingleLine;
if Flags and DT_NOPREFIX = 0 then
Result := Result or QtTextShowMnemonic;
if Flags and DT_NOCLIP <> 0 then
Result := Result or QtTextDontClip;
if Flags and DT_EXPANDTABS <> 0 then
Result := Result or QtTextExpandTabs;
end;
procedure EventTrace(message: string; data: pointer);
begin

View File

@ -59,6 +59,7 @@ type
procedure DrawElement(DC: HDC; Details: TThemedElementDetails; const R: TRect; ClipRect: PRect); override;
procedure DrawEdge(DC: HDC; Details: TThemedElementDetails; const R: TRect; Edge, Flags: Cardinal; AContentRect: PRect); override;
procedure DrawIcon(DC: HDC; Details: TThemedElementDetails; const R: TRect; himl: HIMAGELIST; Index: Integer); override;
procedure DrawText(ACanvas: TPersistent; Details: TThemedElementDetails; const S: String; R: TRect; Flags, Flags2: Cardinal); override;
function GetDetailSize(Details: TThemedElementDetails): TSize; override;
function GetStockImage(StockID: LongInt; out Image, Mask: HBitmap): Boolean; override;
@ -67,6 +68,7 @@ type
end;
implementation
uses qtint, qtproc;
{ TQtThemeServices }
@ -121,6 +123,7 @@ var
ABrush: QBrushH;
Widget: QWidgetH;
AViewportPaint: Boolean;
StyleState: QStyleState;
begin
if (Context <> nil) then
begin
@ -190,7 +193,14 @@ begin
QStyleOption_initFrom(opt, Context.Parent);
Palette := QPalette_create();
QStyleOption_palette(opt, Palette);
AColor := QPalette_color(Palette, QPaletteHighlight)^;
StyleState := GetControlState(Details);
if (StyleState and QStyleState_HasFocus) <> 0 then
AColor := QPalette_color(Palette, QPaletteActive, QPaletteHighLight)^
else
if (StyleState and QStyleState_Selected) <> 0 then
AColor := QPalette_color(Palette, QPaletteInActive, QPaletteHighLight)^
else
AColor := QPalette_color(Palette, QPaletteHighLight)^;
ABrush := QBrush_create(QPainter_brush(Context.Widget));
QBrush_setColor(ABrush, @AColor);
QBrush_setStyle(ABrush, QtSolidPattern);
@ -311,6 +321,69 @@ begin
end;
procedure TQtThemeServices.DrawText(ACanvas: TPersistent;
Details: TThemedElementDetails; const S: String; R: TRect; Flags,
Flags2: Cardinal);
var
Palette: QPaletteH;
Brush: QBrushH;
Context: TQtDeviceContext;
Widget: QWidgetH;
W: WideString;
begin
case Details.Element of
teTreeView:
begin
Context := TQtDeviceContext(TCanvas(ACanvas).Handle);
if Details.Part = TVP_TREEITEM then
begin
Palette := nil;
if Context.Parent <> nil then
begin
Widget := QWidget_parentWidget(Context.Parent);
if (Widget <> nil) and QObject_inherits(Widget,'QAbstractScrollArea') then
Palette := QPalette_create(QWidget_palette(Widget))
else
Palette := QPalette_create(QWidget_palette(Context.Parent));
end;
if Palette = nil then
begin
inherited;
exit;
end;
W := GetUTF8String(S);
Context.save;
try
Context.SetBkMode(TRANSPARENT);
if Details.State = TREIS_SELECTEDNOTFOCUS then
QPalette_setCurrentColorGroup(Palette, QPaletteInactive)
else
QPalette_setCurrentColorGroup(Palette, QPaletteActive);
if Details.State in
[TREIS_SELECTED, TREIS_HOTSELECTED, TREIS_SELECTEDNOTFOCUS] then
QStyle_drawItemText(Style, Context.Widget, @R,
DTFlagsToQtFlags(Flags), Palette,
not IsDisabled(Details), @W, QPaletteHighlightedText)
else
QStyle_drawItemText(Style, Context.Widget, @R,
DTFlagsToQtFlags(Flags), Palette,
not IsDisabled(Details), @W, QPaletteText);
finally
Context.restore;
end;
QPalette_destroy(Palette);
end else
inherited;
end;
else
inherited;
end;
end;
function TQtThemeServices.HasTransparentParts(Details: TThemedElementDetails): Boolean;
begin
Result := True;

View File

@ -1206,22 +1206,11 @@ end;
------------------------------------------------------------------------------}
function TQtWidgetSet.DrawText(DC: HDC; Str: PChar; Count: Integer;
var ARect: TRect; Flags: Cardinal): Integer;
const
QtTextSingleLine = $0100;
QtTextDontClip = $0200;
QtTextExpandTabs = $0400;
QtTextShowMnemonic = $0800;
QtTextWordWrap = $1000;
QtTextWrapAnywhere = $2000;
QtTextHideMnemonic = $8000;
QtTextDontPrint = $4000;
QtTextIncludeTrailingSpaces = $08000000;
QtTextJustificationForced = $10000;
var
WideStr: WideString;
R: TRect;
F: Integer;
QtDC: TQtDeviceContext;
F: Integer;
begin
{$ifdef VerboseQtWinAPI}
WriteLn('[WinAPI DrawText] DC: ', dbghex(DC), ' Str: ', string(Str),
@ -1240,40 +1229,7 @@ begin
else
WideStr := GetUtf8String(Str);
// convert DT flags to QT Flags
F := 0;
// horizontal alignment
if Flags and DT_CENTER <> 0 then
F := F or QtAlignHCenter
else
if Flags and DT_RIGHT <> 0 then
F := F or QtAlignRight
else
F := F or QtAlignLeft;
// vertical alignment
if Flags and DT_VCENTER <> 0 then
F := F or QtAlignVCenter
else
if Flags and DT_BOTTOM <> 0 then
F := F or QtAlignBottom
else
F := F or QtAlignTop;
// mutually exclusive wordbreak and singleline
if Flags and DT_WORDBREAK <> 0 then
F := F or QtTextWordWrap
else
if Flags and DT_SINGLELINE <> 0 then
F := F or QtTextSingleLine;
if Flags and DT_NOPREFIX = 0 then
F := F or QtTextShowMnemonic;
if Flags and DT_NOCLIP <> 0 then
F := F or QtTextDontClip;
if Flags and DT_EXPANDTABS <> 0 then
F := F or QtTextExpandTabs;
F := DTFlagsToQtFlags(Flags);
QtDC.font.Metrics.BoundingRect(@R, @ARect, F, @WideStr);