Many of us see large difference between Win32 and Objective-C. Developers who are migrated from Win32 to Cocoa or some other form of Objective-C generally find Win32 a lot easier. Reverse is also true in the most cases.
However, there are few similarities between Win32 and Objective-C.
Communication to object via message passing
Consider the case of Objective-C first. You have an object of NSWindow. It is a window on the screen and you want to change its title.
Following code snippets does this:
/*Create or get a window*/
NSWindow * win = ...;
/*Change the title*/
[win setTitle:@"New title"]; /*Line 4*/
Whereas on Win32
/*Create or get a window*/
HWND win = ...;
/*Change the title*/
TCHAR * title = TEXT("New title");
SendMessage (win, WM_SETTEXT, 0, (LPARAM)title);
Wait a minute. At one place, you are calling method on the object (at Line 4) and at another place, you are calling a function (SendMessage)! Is it a similarity? Let me explain.
Objective-C adds message passing capability to C. You are sending a message -setTitle to NSWindow object so that it can change the title of window in line 5. On Win32 front, you too are sending a message to the window object by calling SendMessage so that it can change the title.
In Win32, you generally send a message to a Win32 control to change its property or get its property. On Objective-C you call the method on object which actually sends the message to the object.
Can you see the similarity of sending the message to the object?
In objective-C, a class is defined as an interface which declares what are the messages to which this class will respond.
For example:
@interface MyClass: NSObject
{
}
-(id)init;
-(void)dealloc;
-(void)doSomething:(int)number;
@end
It shows that MyClass will respond init, dealloc and doSomething: messages (however, more methods can be added into runtime to MyClass).
In case of NSWindow, a method of NSWindow (a message to NSWindow object) is called by main event loop (say -keyDown: on key down) or explicitly (setTitle in this case).
On Win32, for simplicity, consider a window object only. There is a message proc of a window object which is registered before its creation. This message proc processes the various messages of window object which may be dispatched by message pump or can be send manually by SendMessage. This message proc is similar to this list in case of Win32.
Can I change the way a particular message is handled or customise it?
This leads to next point.
Overriding methods by subclassing
In Win32, we can change the message proc of a window object by changing the value of GWL_WNDPROC of the window. It will change the default message proc and call our message proc. It is subclassing of windows control. In our message proc, we can override an existing message handling as we want. Messages we do not process can be sent to default message proc or old message proc in this case. However, this step is optional but required in most of the cases. This way, we can add as per our requirement into default behaviour (or can abuse it). For more on windows subclassing in Win32, see this.
In this case, let us take an example of textbox or edit control.
HWND editbox = ...;
WNDPROC oldproc;
oldproc = (WNDPROC)SetWindowLongPtr (editbox, GWLP_WNDPROC, MyEditProc);
Inside MyEditProc
LRESULT MyEditProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
//Subclass the control by overriding message handling.
switch(uMsg) {
//If i can handle it, handle it.
case WM_CHAR:
Capitalize (hWnd, wParam,lParam);
return 0;
}
//If do not handle itself, let the old message proc to handle.
return CallWindowProc(oldproc, hWnd, uMsg, wParam, lParam);
}
On Objective-C, a class can inherit another class (like we do in object oriented language C++ or Java) and overrides the methods as per the needs. While overriding a method, subclass can let superclass to handle it if it does not.
For example: Say MyWindow inherits NSWindow and overrides -keyDown to handle the key in its own manner.
@interface MyWindow:NSWindow
{
}
- (void)keyDown: (NSEvent *)event;
@end
In the implementation part
@implementation MyWindow
- (void)keyDown: (NSEvent *)event
{
//Handle Cmd-Q and for other, send to superclass.
if(KeyIsCmdQ(event))
[NSApplication terminate:nil];
else
//let superclass to handle it.
[super keyDown:event];
}
@end
You can see the similarity here too but with a difference. In Win32, you can abuse the subclassing by not letting old proc or default proc to handle the message which you are not handling.
Anyway, are you able to see the similarity between Objective-C and Win32?
Did Win32 copy Objective-C or Objective-C copy Win32 or were they developed independently?
Do you feel anything wrong or requires any correction? Please feel free to comment on it.
Also, if you have to add more, add it by comment.
Recent Comments