Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-11

About "callbacks method" of a class

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

mili

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"?

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
<<<<<

Thanks,
Yours,
mili

Mattias Gaertner
to mili
Show quoted text 34 lines
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.
Show quoted text 26 lines
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

mili

Hi,

Actually it's the same problem, I tried to connect TSomeClass.FuncName with a signal, which caused problem. Yesterday I thought perhaps I can make a callbacks function to be a nested function, then it will be invisible to other places. Now I know I was wrong. It seems there's no good way to encapsulate a callback completely into a class.

Now I have write a

Show quoted text 18 lines
> 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