[fpc-pascal]About "callbacks method" of a class
Mattias Gaertner
nc-gaertnma at netcologne.de
Wed Nov 13 16:43:04 CET 2002
On Wed, 13 Nov 2002 22:37:55 +0800
mili <milimeter at 163.com> wrote:
> Hello, everybody
>
> I am using gtk2forpascal. I want to program as follows:
> >>>>>
> TButton = class(Tobject)
> private
> FButton:PGtkWidget;
> public
> constructor Create;
> destructor Destroy;
> procedure Callback_OnClick(widget:PGtkWidget; data:Object); cdecl;
> end;//TButton
>
> constructor TButton.Create;
> begin
> inherited Create;
> FButton:=gtk_button_new_with_label('Click Me!');
> gtk_widget_show(FButton);
> g_signal_connect(G_OBJECT(FButton),'clicked',
> TGCALLBACK(@Callback_OnClick),self);
> ...
> end;//TButton.Create
>
> procedure TButton.Callback_OnClick(widget:PGtkWidget; data:Object); cdecl;
> begin
> ......
> end;//TButton.Callback_OnClick
> <<<<<
>
> But I can't comiler this program. When I connect the signal with a
> subroutine which is not a method, the compilation works. Can anybody
> confirm me that: In class definition, We can't use "@MethodName" and in
> one method there's no way to directly connect a signal with another method
> in the same class? In short, is there "callbacks method"?
Right, procedures and methods (= procedures of object) are not compatible.
You have to write your own mapper. For example:
type
TButton = class
procedure OnClickCallback(Widget: PGtkWidget);
end;
procedure TButton.OnClickCallback(Widget: PGtkWidget);
begin
end;
procedure Callback_TButton_OnClick(widget:PGtkWidget; data:Object); cdecl;
begin
TButton(Data).OnClickCallback(Widget);
end;
begin
g_signal_connect(G_OBJECT(FButton),'clicked',
TGCALLBACK(@Callback_TButton_OnClick),FButton);
end.
> And another problem, I found the following code style in Lazarus project
> sources:
> >>>>>
> procedure TSomeClass.FuncName(...);
> procedure FuncNameEmbeded(...);
> begin
> .....
> end;//FuncNameEmbeded
> begin
> end;//TSomeClass.FuncName
> <<<<<
>
> I know it's "nested procedure". But the information from fpc
> documentations is too little. can you tell me more about it? Why the
> following code can compile but will run with "Access violation"?
> >>>>>
> procedure TSomeClass.FuncName(...);
> procedure FuncNameEmbeded(...);
> begin
> .....
> writeln(FValue);//Here FValue is a member (Integer) of TSomecalss
> .....
> end;//FuncNameEmbeded
> begin
> end;//TSomeClass.FuncName
> <<<<<
Looks like good code.
Can you send more (class definition and code, that calls FuncName)?
Mattias Gaertner
More information about the fpc-pascal
mailing list