Sign in Join
Wiki copytemporary

Accessing the Interfaces directly: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Accessing the Interfaces directly

OldNewLine
1+This page describes how to write access the underlying [[Widgetset|widgetset]] used by lazarus directly (for example: The Windows API, or Gtk, or Qt, etc). One should always try to avoid relying on widgetset specials. This document is here for both people improving the widgetsets and for people writing components that really need to access the widgets directly.
2+
3+## Overview
4+
5+Every TWinControl has a handle, and the LCL does not need to know, what a Handle is. The meaning of the Handle is totally up to the interface:
6+
7+- under [GTK](/wiki-category/GTK%20Widgetsets) a handle is often a PGtkWidget
8+- under Windows a handle is often a HWnd.
9+- under Carbon a handle is often a object descendant of TCarbonWidget.
10+- under Qt, a handle is often a pointer to a object descendent of TQtWidget.
11+
12+## Per-Widgetset Details
13+
14+### Qt
15+
16+Handles on [[Qt Interface|Qt]] are mostly pointers to objects. Every time a control is created, a helper object is also created and both are connected. The helper object is necessary to receive events from Qt. After receiving an event it will send it to the LCL.
17+
18+Most of those objects are descendents from TQtWidget as declared below:
19+
20+``` pascal
21+{ TQtWidget }
22+
23+TQtWidget = class(TObject)
24+private
25+ ...
26+public
27+ Widget: QWidgetH;
28+ LCLObject: TWinControl;
29+public
30+ function GetContainerWidget: QWidgetH; virtual;
31+ ...
32+end;
33+```
34+
35+Notice the two variables they all contain:
36+
37+- Widget - This is a connect to the Qt object for this Widget
38+- LCLObject - Is a bridge with the LCL object it is linked to.
39+
40+And also this method:
41+
42+- GetContrainerWidget() - When you wish to create a new widget and set it's parent, don't set it to Widget. Set it to GetContrainerWidget() instead. This happens because forms have 2 widgets. The main form widget, and also a container widget, where all child controls should be put to ensure that the size of the menu is handled automatically. Most other controls will have just one widget, but by using this function you avoid surprises in case the implementation of a control changes.
43+
44+### Carbon
45+
46+Handles on [[Carbon Interface|Carbon]] are very similar to Qt ones. The helper classes are descended from abstract class TCarbonWidget.
47+
48+``` pascal
49+{ TCarbonWidget }
50+
51+TCarbonWidget = class(TObject)
52+private
53+public
54+ Widget: Pointer;
55+ LCLObject: TWinControl;
56+end;
57+```
58+
59+Notice the two variables they all contain:
60+
61+- Widget - This is a connect to the Carbon object for this Widget: ControlRef (TCarbonControl descendants) or WindowRef (TCarbonWindow descendants).
62+- LCLObject - Is a bridge with the LCL object it is linked to.
63+
64+### Windows
65+
66+Handles on [[Win32/64 Interface|Win32]] or [[Windows CE Interface|WinCE]] are almost always the same as their corresponding Win api handles. For example the handle for a TMenu is an HMENU, the handle for a TBrush is an HBRUSH. You simply pass one such handle as a parameter in a Windows api call.
67+
68+## List of Handles on various LCL classes
69+
70+### TFont
71+
72+- Win32, WinCE: HFONT
73+- Qt: A TQtFont object from qtprivate.pas
74+- Carbon: A TCarbonFont object from carbongdiobjects.pp
75+
76+### TCanvas
77+
78+- Win32, WinCE: HDC
79+- Gtk: A TDeviceContext object from gtkdef.pp
80+ - You can get a drawable, for example with: TDeviceContext(MyCanvas.Handle).Drawable
81+- Qt: A TQtDeviceContext object from qtprivate.pas
82+- Carbon: A TCarbonDeviceContext object from carboncanvas.pp
83+ - The native handle can be accessed with TCarbonDeviceContext(MyCanvas.Handle).CGContext of type CGContextRef
84+
85+### TBrush
86+
87+- Win32, WinCE: HBRUSH
88+- Qt: A TQtBrush object from qtprivate.pas
89+- Carbon: A TCarbonBrush object from carbongdiobjects.pp
90+
91+### TBitmap
92+
93+- Win32, WinCE: HBITMAP
94+- Gtk: PGDIObject (defined on unit GtkDef)
95+- Qt: A TQtImage object from qtobjects.pas unit
96+- Carbon: A TCarbonBitmap object from carbongdiobjects.pp
97+
98+You can access this on Gtk using:
99+
100+``` pascal
101+var
102+ GDIObject: PGDIObject;
103+ Bitmap: TBitmap;
104+ AImage: PGtkWidget;
105+begin
106+ ...
107+
108+ GDIObject := PgdiObject(Bitmap.Handle);
109+
110+ AImage := gtk_image_new_from_pixmap(GDIObject^.GDIPixmapObject,
111+ GDIObject^.GDIBitmapMaskObject);
112+
113+ gtk_widget_show(AImage);
114+
115+ gtk_container_add(GTK_CONTAINER(MyForm.Handle), AImage);
116+end;
117+```
118+
119+### TCustomForm
120+
121+- Win32, WinCE: it´s a HWND, a native window handle
122+- Gtk: It´s a pointer to a structure.
123+- Qt: The Handle is a pointer to a object from the TQtMainWindow class declared at qtwidgets.pas.
124+ - To have access to the QMainWindow Qt object it represents do: TQtMainWindow(Handle).Widget
125+ - To have access to the container widget of the form, do: TQtMainWindow(Handle).GetContrainerWidget
126+- Carbon: TCarbonWindow from carbonprivate.pp
127+
128+### TMenu, TMainMenu, TMenuItem and TPopUpMenu
129+
130+- Win32, WinCE : HMENU
131+- Qt: TMainMenu is a pointer to a TQtMenuBar object. TPopUpMenu is a TQtMenu object. TMenuItem can either be a TQtAction if it represents a clickable item or a separator or a TQtMenu if it is an item that has submenus.
132+- Carbon: TMenuItem is a TCarbonMenu object, TMenu (TMainMenu and TPopupMenu) is represented by its root menu item
133+
134+## See also
135+
136+- [[Interfaces]]
137+- [[How To Use Interfaces]]
138+- [[Understanding Interfaces]]
139+
140+---
141+*From the Free Pascal wiki, page "Accessing the Interfaces directly", as it stood on 2020-03-09 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history