From abbd39ddec2ab53ee8c4ad8f3ffde6b8eb8a0489 Mon Sep 17 00:00:00 2001 From: zeljko Date: Fri, 3 Aug 2007 17:49:21 +0000 Subject: [PATCH] (Qt): Fixed all mem leaks with SynEdit, Bitmaps by paul, with small fix against his patch from me. git-svn-id: trunk@11730 - --- lcl/interfaces/qt/qtcaret.pas | 2 +- lcl/interfaces/qt/qtobject.inc | 10 +++--- lcl/interfaces/qt/qtobjects.pas | 51 ++++++++++++++++++--------- lcl/interfaces/qt/qtwidgets.pas | 8 ++--- lcl/interfaces/qt/qtwinapi.inc | 62 ++++++++------------------------- lcl/interfaces/qt/qtwinapih.inc | 1 + 6 files changed, 61 insertions(+), 73 deletions(-) diff --git a/lcl/interfaces/qt/qtcaret.pas b/lcl/interfaces/qt/qtcaret.pas index 4079ad4995..337842af88 100644 --- a/lcl/interfaces/qt/qtcaret.pas +++ b/lcl/interfaces/qt/qtcaret.pas @@ -412,7 +412,7 @@ procedure TEmulatedCaret.UpdateCaret; var R: TRect; begin - if (FWidget <> nil) and not FWidget.InPaint then + if (FWidget <> nil) and not (FWidget.Context <> 0) then begin R.Left := FPos.x; R.Top := FPos.y; diff --git a/lcl/interfaces/qt/qtobject.inc b/lcl/interfaces/qt/qtobject.inc index 44b8629f36..46a1549cd7 100644 --- a/lcl/interfaces/qt/qtobject.inc +++ b/lcl/interfaces/qt/qtobject.inc @@ -26,7 +26,7 @@ constructor TQtWidgetSet.Create; begin inherited Create; - InitStockItems; + InitStockItems; QtWidgetSet := Self; end; @@ -42,14 +42,16 @@ destructor TQtWidgetSet.Destroy; begin DestroyGlobalCaret; Clipboard.Free; - - FreeStockItems; + FreeStockItems; QtWidgetSet := nil; if SavedDCList<>nil then SavedDCList.Free; + QtDefaultContext.Free; + QtScreenContext.Free; + inherited Destroy; end; @@ -234,7 +236,7 @@ begin Painter :=TQtDeviceContext(CanvasHandle).Widget; Pen := QPainter_pen(Painter); QPen_color(Pen, @ASavedColor); - QColor_setRgb(QColorH(@Color),Red(AColor),Green(AColor),Blue(AColor)); + QColor_setRgb(QColorH(@Color),Red(AColor),Green(AColor),Blue(AColor)); QPainter_setPen(Painter, @Color); QPainter_drawPoint(Painter, X,Y); QPainter_setPen(Painter, @ASavedColor); diff --git a/lcl/interfaces/qt/qtobjects.pas b/lcl/interfaces/qt/qtobjects.pas index cf9b8d405f..bceae9675f 100644 --- a/lcl/interfaces/qt/qtobjects.pas +++ b/lcl/interfaces/qt/qtobjects.pas @@ -391,6 +391,9 @@ type procedure DebugRegion(const msg: string; Rgn: QRegionH); function Clipboard: TQtClipboard; + function QtDefaultContext: TQtDeviceContext; + function QtScreenContext: TQtDeviceContext; + implementation const ClipbBoardTypeToQtClipboard: array[TClipboardType] of QClipboardMode = @@ -402,6 +405,22 @@ const var FClipboard: TQtClipboard = nil; + FDefaultContext: TQtDeviceContext = nil; + FScreenContext: TQtDeviceContext = nil; + +function QtDefaultContext: TQtDeviceContext; +begin + if FDefaultContext = nil then + FDefaultContext := TQtDeviceContext.Create(nil); + Result := FDefaultContext; +end; + +function QtScreenContext: TQtDeviceContext; +begin + if FScreenContext = nil then + FScreenContext := TQtDeviceContext.Create(QApplication_desktop(), True); + Result := FScreenContext; +end; { TQtObject } @@ -709,7 +728,8 @@ end; procedure TQtFont.setPointSize(p1: Integer); begin - QFont_setPointSize(Widget, p1); + if p1 > 0 then + QFont_setPointSize(Widget, p1); end; function TQtFont.pixelSize: Integer; @@ -719,7 +739,8 @@ end; procedure TQtFont.setPixelSize(p1: Integer); begin - QFont_setPixelSize(Widget, p1); + if p1 > 0 then + QFont_setPixelSize(Widget, p1); end; function TQtFont.weight: Integer; @@ -1105,8 +1126,8 @@ begin if Parent <> nil then begin - vFont := TQtFont.Create(False); - vFont.Owner := Self; + vFont := TQtFont.Create(False); + vFont.Owner := Self; vBrush := TQtBrush.Create(False); vBrush.Owner := Self; @@ -1117,8 +1138,8 @@ begin vRegion := TQtRegion.Create(False); vRegion.Owner := Self; - vBackgroundBrush := TQtBrush.Create(False); - vBackgroundBrush.Owner := Self; + vBackgroundBrush := TQtBrush.Create(False); + vBackgroundBrush.Owner := Self; end else begin @@ -1150,26 +1171,23 @@ begin if Parent <> nil then - begin - - vFont.Widget := nil; - vFont.Free; + begin + vFont.Widget := nil; + vFont.Free; vBrush.Widget := nil; vBrush.Free; vPen.Widget := nil; vPen.Free; vRegion.Widget := nil; vRegion.Free; - vBackgroundBrush.Widget := nil; - vBackgroundBrush.Free; - + vBackgroundBrush.Widget := nil; + vBackgroundBrush.Free; end; - if vImage <> nil then - QImage_destroy(vImage); - if Widget <> nil then + begin QPainter_destroy(Widget); + end; if ParentPixmap <> nil then QPixmap_destroy(ParentPixmap); @@ -2189,3 +2207,4 @@ end; end. + diff --git a/lcl/interfaces/qt/qtwidgets.pas b/lcl/interfaces/qt/qtwidgets.pas index e182df3886..42f30afde4 100644 --- a/lcl/interfaces/qt/qtwidgets.pas +++ b/lcl/interfaces/qt/qtwidgets.pas @@ -53,6 +53,7 @@ type FPaintData: TPaintData; FCentralWidget: QWidgetH; FStopMouseEventsProcessing: Boolean; // widget stop processing of mouse events + FContext: HDC; function GetProps(const AnIndex: String): pointer; function GetWidget: QWidgetH; @@ -70,7 +71,6 @@ type AVariant: QVariantH; LCLObject: TWinControl; HasCaret: Boolean; - InPaint: Boolean; public constructor Create(const AWinControl: TWinControl; const AParams: TCreateParams); virtual; destructor Destroy; override; @@ -133,6 +133,7 @@ type function windowFlags: QtWindowFlags; function windowModality: QtWindowModality; + property Context: HDC read FContext; property Props[AnIndex:String]:pointer read GetProps write SetProps; property PaintData: TPaintData read FPaintData write FPaintData; property Widget: QWidgetH read GetWidget write SetWidget; @@ -213,7 +214,6 @@ type {$endif} MenuBar: TQtMenuBar; ToolBar: TQtToolBar; - Canvas: TQtDeviceContext; destructor Destroy; override; function getClientBounds: TRect; override; procedure setTabOrders; @@ -1579,7 +1579,6 @@ begin {$ifdef VerboseQt} WriteLn('TQtWidget.SlotPaint'); {$endif} - InPaint := True; if (LCLObject is TWinControl) then begin FillChar(Msg, SizeOf(Msg), #0); @@ -1598,6 +1597,7 @@ begin end; Msg.DC := BeginPaint(THandle(Self), AStruct^); + FContext := Msg.DC; with getClientBounds do SetWindowOrgEx(Msg.DC, -Left, -Top, nil); @@ -1611,6 +1611,7 @@ begin finally Dispose(PaintData.ClipRect); Fillchar(FPaintData, SizeOf(FPaintData), 0); + FContext := 0; EndPaint(THandle(Self), AStruct^); Dispose(AStruct); end; @@ -1618,7 +1619,6 @@ begin Application.HandleException(nil); end; end; - InPaint := False; end; {------------------------------------------------------------------------------ diff --git a/lcl/interfaces/qt/qtwinapi.inc b/lcl/interfaces/qt/qtwinapi.inc index 0e8f039823..96edcedce7 100644 --- a/lcl/interfaces/qt/qtwinapi.inc +++ b/lcl/interfaces/qt/qtwinapi.inc @@ -105,10 +105,6 @@ begin New(DC.vClipRect); DC.vClipRect^ := Widget.PaintData.ClipRect^; end; - //DC.DebugClipRect('BeginPaint: After'); - // TODO: ask what is this good for - if Widget is TQtMainWindow then - TQtMainWindow(Widget).Canvas := DC; end; Result := PS.hdc; @@ -498,12 +494,7 @@ begin {$ifdef VerboseQtWinAPI} WriteLn('[WinAPI CreateCompatibleDC] DC: ', dbghex(DC)); {$endif} - {TODO: TEST IT - if (GetQtDefaultDC <> 0) - and (DC = 0) then - DeleteDefaultDC; - } - Result := GetDC(DC); + Result := HDC(TQtDeviceContext.Create(nil, True)); end; {------------------------------------------------------------------------------ @@ -754,21 +745,7 @@ begin Result := False; if not IsValidDC(hDC) then exit; - - Painter := TQtDeviceContext(hDC).Widget; - Result := not QPainter_isActive(Painter); - - if not Result then - QPainter_end(Painter); - - if Result then - begin - {we should never come here ... endPaint() do the job} - {$ifdef VerboseQtWinAPI} - WriteLn('[WinAPI DeleteDC] freeing resources '); - {$endif} - TQtDeviceContext(hDC).Free; - end; + TQtDeviceContext(hDC).Free; end; {------------------------------------------------------------------------------ @@ -854,8 +831,7 @@ begin and QPaintEngine_isActive(APaintEngine) then begin APainter := QPaintEngine_painter(APaintEngine); - if APainter <> NiL - then + if APainter <> nil then QPainter_end(APainter); end ; @@ -1492,6 +1468,8 @@ begin if Str <> nil then begin WideStr := UTF8Decode(Str); + if WideStr = '' then + WideStr := Str; QtDC.drawText(X, Y, @WideStr); end; @@ -1918,22 +1896,16 @@ begin Widget := TQtWidget(hWnd); if Widget <> nil then - Result := HDC(TQtDeviceContext.Create(Widget.Widget)) + begin + Result := Widget.Context; + if Result = 0 then + Result := HDC(QtDefaultContext); + end else begin - { TODO: Test this ! - if GetQtDefaultDC = 0 then - begin - Result := HDC(TQtDeviceContext.Create(nil)); - SetQtDefaultDC(Result); - end else - Result := GetQtDefaultDC; - } - Result := HDC(TQtDeviceContext.Create(nil)); + Result := HDC(QtScreenContext); end; -// if hWnd <> 0 then TQtCustomForm(hWnd).Canvas := TQtDeviceContext(Result); - {$ifdef VerboseQtWinAPI} WriteLn('Trace:< [WinAPI GetDC] Result: ', dbghex(Result)); {$endif} @@ -1948,14 +1920,8 @@ begin end; function TQtWidgetSet.GetDesignerDC(WindowHandle: HWND): HDC; -var - Widget: TQtWidget; begin - Widget := TQtWidget(WindowHandle); - if Widget <> nil then - Result := HDC(TQtDeviceContext.Create(Widget.Widget, False)) - else - Result := HDC(TQtDeviceContext.Create(nil, False)); + Result := GetDC(WindowHandle); {$ifdef VerboseQtWinAPI_MISSING_IMPLEMENTATION} WriteLn('***** [WinAPI TQtWidgetSet.GetDesignerDC] missing implementation '); {$endif} @@ -3618,7 +3584,7 @@ begin Result := 0; - if IsValidDC(DC) then TQtDeviceContext(DC).Free; + if IsValidDC(DC) then Exit; Result := 1; end; @@ -3778,7 +3744,7 @@ begin if RGN <> 0 then QPainter_setClipRegion(Painter, TQtRegion(Rgn).Widget) else - QPainter_setClipping(Painter, False); + QPainter_setClipRegion(Painter, nil, QtNoClip); if QPainter_hasClipping(Painter) then begin ARegion := QRegion_Create; diff --git a/lcl/interfaces/qt/qtwinapih.inc b/lcl/interfaces/qt/qtwinapih.inc index ebf237e9a7..cbb18e8324 100644 --- a/lcl/interfaces/qt/qtwinapih.inc +++ b/lcl/interfaces/qt/qtwinapih.inc @@ -195,3 +195,4 @@ function WindowFromPoint(Point: TPoint): HWND; override; //##apiwiz##eps## // Do not remove, no wizard declaration after this line +