Recent

Author Topic: Message methods [SOLVED]  (Read 10803 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Message methods [SOLVED]
« on: May 14, 2021, 08:11:38 pm »
كُنتُ أقرأ في وثائق الـ free Pascal وصادفتني الـ Message methods كما في هذا الرابط الرسمي https://www.freepascal.org/docs-html/current/ref/refsu31.html#x83-1050006.6.7 غير أنّي لم أستوعب الفكرة ولم أعرف فائدة أو جدوى الـ Message methods ولماذا تمّت إضافتها للأصناف فعلى ما أتذكّر لم يُوجد هذا النّوع من الـ methods في نسخ الـ OOP القياسيّة للغات التي كانت تعمل في النّظام DOS.

google translate:

"I was reading the free Pascal documents and came across the Message methods as well as in this official link https://www.freepascal.org/docs-html/current/ref/refsu31.html#x83-1050006.6.7 However, I did not understand the idea and did not know the usefulness or usefulness of Message methods and why they were added to classes. As far as I can remember, this type of method was not found in the standard OOP versions of languages ​​that were running in DOS."
« Last Edit: May 15, 2021, 09:44:58 pm by pascal111 »
La chose par la chose est rappelé.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Message methods
« Reply #1 on: May 14, 2021, 09:03:14 pm »
Assuming that we're thinking about the same thing (and if not, I apologise) it's for hanging methods onto externally-generated events:

Code: Pascal  [Select][+][-]
  1. ...
  2.   private
  3.     { Private declarations }
  4.     fastTerminate, trimWorkingSetAsap: BOOLEAN;
  5.     procedure WMQueryEndSession(var Message: TWMQueryEndSession); message WM_QUERYENDSESSION;
  6.     procedure WMSysColorChange(var Message: TWMSysColorChange); message WM_SYSCOLORCHANGE;
  7.   public
  8.     { Public declarations }
  9.   end;
  10.  

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Message methods
« Reply #2 on: May 14, 2021, 09:13:28 pm »
You can also add custom messages. For example, I have several applications that draw several plots of data. If the data changes, the data Form sends a message, and the plot Forms update when they receive the message.
« Last Edit: May 14, 2021, 09:22:36 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Message methods
« Reply #3 on: May 14, 2021, 11:28:29 pm »
يُمكننا أن قرع كأسينا أيها النبيل @MarkMLl نخب هذه المعلومة الرائعة ولكن بذكر مُساعدة النبيل @VTwin فلقد أوضح كلامكـ المُوجز "it's for hanging methods onto externally-generated events" وإنّها لعبارة إنجليزيّة أنيقة وبليغة وقياسيّة وبلغة علميّة للتعبير عن المعنى المنشود (أعذراني فأنا أُحب أسلوب الروايات الإنجليزيّة).

إستفساري التالي هو كيف يكونُ هناكـ مُتغيّر اسمه Message وأنا على ما أظنّ أنّها كلمة من كلمات لغة البرمجة نفسها كما أوردت في الكود الذي زوّدتنا به؟!

google translate:

"We can knock our glasses, noble @MarkMLl, toast "in honor of" this wonderful information, but by mentioning the help of the noble @VTwin, "he made it clear" your brief statement made it clear: "it's for hanging methods onto externally-generated events" and it is an elegant, eloquent, standard English phrase and scientific language to express the desired meaning (excuse me, I love the style English novels).

My next inquiry is: How can there be a variable named Message, and I think it is one of the words of the programming language itself, as it was mentioned in the code that you provided us with ?!"

Code: Pascal  [Select][+][-]
  1.     procedure WMQueryEndSession(var Message: TWMQueryEndSession); message WM_QUERYENDSESSION;
  2.  
La chose par la chose est rappelé.

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Message methods
« Reply #4 on: May 15, 2021, 12:58:52 am »
My understanding is that the message methods take untyped parameters, so can refer to any data such as a record. The final "message" indicates an integer (dword) value that identifies the method to be called.

So a message call includes an identifier, along with data to be used by the receiving method. The identifier tells which method to be called, which then uses the data.
« Last Edit: May 15, 2021, 01:03:11 am by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Message methods
« Reply #5 on: May 15, 2021, 01:13:02 am »
This is a simple example that I use:

Code: Pascal  [Select][+][-]
  1.  
  2. type
  3.   TVMessage = packed record
  4.     Id    : longint;
  5.     Param : word;
  6.     Flag  : word;
  7.     Text  : shortstring;
  8.   end;
  9.  
  10. ...
  11.  
  12.  
  13. { DispatchMessage
  14.   Dispatches a message to all forms. This adds a message to the message queue,
  15.   which will be processsed by the handler after the current process finishes.
  16.   Define a message id such as:
  17.     LM_Msg = LM_USER + 1;
  18.   where LM_USER is defined in LMessages, and a handler such as:
  19.     procedure OnMessage(var msg: TLMessage); message LM_Msg; }
  20. procedure DispatchMessage(Id: dword; Param, Flag: word; Txt: string);
  21. var
  22.   i: integer;
  23.   f: TForm;
  24.   m: TVMessage;
  25. begin
  26.   m.Id := Id;
  27.   m.Param := Param;
  28.   m.Flag := Flag;
  29.   m.Text := Txt;
  30.   for i := 0 to Screen.FormCount - 1 do begin
  31.     f := Screen.Forms[i];
  32.     if (f <> nil) then
  33.       f.Dispatch(m);
  34.   end;
  35. end;
  36.  

Then a method in a form:

Code: Pascal  [Select][+][-]
  1.     procedure OnDataChange(var Msg: TVMessage); message OM_DataChange;
  2.  
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Message methods
« Reply #6 on: May 15, 2021, 01:28:30 am »
The dual use of "message" is curious, however according to this, "message" is not actually a "reserved" word, just "dangerous":

http://www.festra.com/eng/ref-reserved.htm
« Last Edit: May 15, 2021, 01:30:29 am by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Message methods
« Reply #7 on: May 15, 2021, 08:55:41 am »
As far as I can remember, this type of method was not found in the standard OOP versions of languages ​​that were running in DOS."
Yes, message methods were first introduced only in Delphi. As noted in message methods docs, they are primarily intended to ease programming of callback functions in several GUI toolkits, such as Win32 or GTK.
Every time you declare a method this way:
Code: Pascal  [Select][+][-]
  1. procedure SomeMethod(var R); message NNNN;
the compiler adds an entry with the index NNNN to a separate table of virtual methods (message methods).
In the process of working, a certain class (TForm, TApplication, etc), when processing external messages, uses this mechanism through the method TObject.Dispatch(var R), which scans the class hierarchy through the method table to find a suitable index whose value is the first 32-bit number from the R record (in Delphi - 16-bit).
« Last Edit: May 15, 2021, 08:58:44 am by ASerge »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Message methods
« Reply #8 on: May 15, 2021, 09:10:57 am »
"We can knock our glasses, noble @MarkMLl, toast "in honor of" this wonderful information, but by mentioning the help of the noble @VTwin, "he made it clear" your brief statement made it clear: "it's for hanging methods onto externally-generated events" and it is an elegant, eloquent, standard English phrase and scientific language to express the desired meaning (excuse me, I love the style English novels).

My next inquiry is: How can there be a variable named Message, and I think it is one of the words of the programming language itself, as it was mentioned in the code that you provided us with ?!"

Google has a surprisingly florid turn of phrase. As far as your final question goes, it's important to appreciate that when a compiler parses (qv) the syntax (qv) of a language, it's not simply saying "what patterns can I match on this line". Focussing on the current example, it's saying "OK, I've got the arbitrarily-named variables out of the way, so I must be looking at one of function procedure property end (plus visibility keywords etc.).

So it starts parsing a procedure declaration and gets to the ; terminator. It then looks ahead one token and finds message, but the only thing this can be in this context is a number (etc.) which tells it what actually to do with the declaration it's just read.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Message methods
« Reply #9 on: May 15, 2021, 05:59:25 pm »
As far as I can remember, this type of method was not found in the standard OOP versions of languages ​​that were running in DOS."
Yes, message methods were first introduced only in Delphi. As noted in message methods docs, they are primarily intended to ease programming of callback functions in several GUI toolkits, such as Win32 or GTK.
Every time you declare a method this way:
Code: Pascal  [Select][+][-]
  1. procedure SomeMethod(var R); message NNNN;
the compiler adds an entry with the index NNNN to a separate table of virtual methods (message methods).
In the process of working, a certain class (TForm, TApplication, etc), when processing external messages, uses this mechanism through the method TObject.Dispatch(var R), which scans the class hierarchy through the method table to find a suitable index whose value is the first 32-bit number from the R record (in Delphi - 16-bit).

لحظة من فضلكـ فهنا يكمن واحدٌ من أسئلتي ،كُنتُ أتعجب أنّهُ يُمكن تمرير سجل لـ Dispatch وكُنتُ أتساءل كيف سيتم معرفة رقم الرسالة أو الـ ID وهنا أنت قلت أنّهُ يتم البحث في أول 32 بت من السجل أي أنّ - إن كان ما فهمته صحيحاً - Dispatch سيتعرّف على رقم الرسالة من خلال أوّل 32 بت من السجل والتي عادةً هي من النّوع Cardinal ويُسمح كذلكـ بالنّوع الحرفي string مع DispatchStr. هل ما فهمته صحيح؟

google translate:

"A moment please, here lies one of my questions, I was wondering that a record could be passed to Dispatch and I was wondering how the message number or ID would be known, and here you said that the first 32 bits of the record are being searched, meaning that - if what I understood is correct - Dispatch will recognize On the message number through the first 32 bits of the register which is usually of type Cardinal, the literal string type is also allowed with DispatchStr. Is what I understood correct?"
La chose par la chose est rappelé.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Message methods
« Reply #10 on: May 15, 2021, 06:07:38 pm »
This is a simple example that I use:

Code: Pascal  [Select][+][-]
  1.  
  2. type
  3.   TVMessage = packed record
  4.     Id    : longint;
  5.     Param : word;
  6.     Flag  : word;
  7.     Text  : shortstring;
  8.   end;
  9.  
  10. ...
  11.  
  12.  
  13. { DispatchMessage
  14.   Dispatches a message to all forms. This adds a message to the message queue,
  15.   which will be processsed by the handler after the current process finishes.
  16.   Define a message id such as:
  17.     LM_Msg = LM_USER + 1;
  18.   where LM_USER is defined in LMessages, and a handler such as:
  19.     procedure OnMessage(var msg: TLMessage); message LM_Msg; }
  20. procedure DispatchMessage(Id: dword; Param, Flag: word; Txt: string);
  21. var
  22.   i: integer;
  23.   f: TForm;
  24.   m: TVMessage;
  25. begin
  26.   m.Id := Id;
  27.   m.Param := Param;
  28.   m.Flag := Flag;
  29.   m.Text := Txt;
  30.   for i := 0 to Screen.FormCount - 1 do begin
  31.     f := Screen.Forms[i];
  32.     if (f <> nil) then
  33.       f.Dispatch(m);
  34.   end;
  35. end;
  36.  

Then a method in a form:

Code: Pascal  [Select][+][-]
  1.     procedure OnDataChange(var Msg: TVMessage); message OM_DataChange;
  2.  

هل يجب أن يكون "m.Id" يساوي "OM_DataChange" ليتم إستدعاء "OnDataChange" ؟

google translate:

"Does "m.Id" need to be equal to "OM_DataChange" to call "OnDataChange"?"

Code: Pascal  [Select][+][-]
  1. m.Id:=OM_DataChange;
  2.  
La chose par la chose est rappelé.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Message methods
« Reply #11 on: May 15, 2021, 06:20:19 pm »
"We can knock our glasses, noble @MarkMLl, toast "in honor of" this wonderful information, but by mentioning the help of the noble @VTwin, "he made it clear" your brief statement made it clear: "it's for hanging methods onto externally-generated events" and it is an elegant, eloquent, standard English phrase and scientific language to express the desired meaning (excuse me, I love the style English novels).

My next inquiry is: How can there be a variable named Message, and I think it is one of the words of the programming language itself, as it was mentioned in the code that you provided us with ?!"

Google has a surprisingly florid turn of phrase. As far as your final question goes, it's important to appreciate that when a compiler parses (qv) the syntax (qv) of a language, it's not simply saying "what patterns can I match on this line". Focussing on the current example, it's saying "OK, I've got the arbitrarily-named variables out of the way, so I must be looking at one of function procedure property end (plus visibility keywords etc.).

So it starts parsing a procedure declaration and gets to the ; terminator. It then looks ahead one token and finds message, but the only thing this can be in this context is a number (etc.) which tells it what actually to do with the declaration it's just read.

MarkMLl

ما فهمتُه أن تسمية المُتغير باسم "message" لا يُشكل مُشكلة لدى الـ Compiler ﻷنّهُ إنّما يبحث عن قيمة صحيحة غالباً وليس المُعرّف ذاته.

google translate:

"What I understood is that naming the variable as "message" is not a problem for the Compiler because it is looking for a correct "an integer" value often and not the identifier itself."
La chose par la chose est rappelé.

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Message methods
« Reply #12 on: May 15, 2021, 08:33:08 pm »
google translate:

"Does "m.Id" need to be equal to "OM_DataChange" to call "OnDataChange"?"

Yes.

OM_DataChange is just a number such as LM_USER + 1. For this program I name the message ids and methods using this convention which is helpful for me. The leading "O" designates the application, just my convention. 
« Last Edit: May 15, 2021, 08:42:19 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Message methods
« Reply #13 on: May 15, 2021, 09:44:41 pm »
google translate:

"Does "m.Id" need to be equal to "OM_DataChange" to call "OnDataChange"?"

Yes.

OM_DataChange is just a number such as LM_USER + 1. For this program I name the message ids and methods using this convention which is helpful for me. The leading "O" designates the application, just my convention.

عظيم!

وكذلكـ لديكـ أُسلوب برمجة جيّد.

أظنّني يُمكنني أنْ أقول تقريباً أنّ هذا الموضوع قد حُل إلّا أنّني أرغب في سماع إضافاتٍ وأقوالٍ أخرى في هذه القضيّة (الموضوع ،فأنا أتخيّل هذه المرّة الموضوع كقضيّة قانونيّة).

google translate:

"Great!

Also, you have a good programming style.

I think I can almost say that this issue "topic" has been resolved, but I would like to hear additional additions and other statements in this case (the topic, I am this time imagining the subject as a "case of law" legal issue)."
La chose par la chose est rappelé.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Message methods
« Reply #14 on: May 15, 2021, 09:57:08 pm »
Dispatch will recognize On the message number through the first 32 bits of the register which is usually of type Cardinal, the literal string type is also allowed with DispatchStr. Is what I understood correct?"
TObject.Dispatch will interpret the structure of the record as if it starts with a 32-bit number. For TObject.DispatchStr - as if there is a ShortString at the beginning.
See objpas.inc:
Code: Pascal  [Select][+][-]
  1. procedure TObject.Dispatch(var message);
  2. ...
  3. begin
  4.   index:=dword(message);
  5. ...
Code: Pascal  [Select][+][-]
  1. procedure TObject.DispatchStr(var message);
  2. ...
  3. begin
  4.   name:=pshortstring(@message)^;
  5. ...

 

TinyPortal © 2005-2018