2008-10-06 13:26:48 +00:00
|
|
|
{
|
|
|
|
view.pas
|
|
|
|
|
|
|
|
View class of the texteditor example. Creates the user interface.
|
|
|
|
|
|
|
|
This example project is released under public domain
|
|
|
|
|
|
|
|
AUTHORS: Felipe Monteiro de Carvalho
|
|
|
|
}
|
2008-09-29 00:47:50 +00:00
|
|
|
unit view;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils,
|
|
|
|
MacOSAll, appkit, foundation, objc,
|
|
|
|
mytoolbar;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
{ TMyView }
|
|
|
|
|
|
|
|
TMyView = class
|
|
|
|
private
|
|
|
|
{ Other helper functions }
|
2008-10-06 02:46:47 +00:00
|
|
|
procedure AddToMenubar(menu: NSMenu);
|
|
|
|
|
|
|
|
function CreateAppleMenu(): NSMenu;
|
|
|
|
function CreateFileMenu(): NSMenu;
|
|
|
|
procedure CreateMainMenu();
|
|
|
|
function CreateMenuItem(ATitle: shortstring): NSMenuItem;
|
|
|
|
|
2008-09-29 00:47:50 +00:00
|
|
|
function CreateToolbar(AOwnerView: NSView; AX, AY, AWidth, AHeight: Double): NSToolbar;
|
|
|
|
function CreateTextField(): NSTextField;
|
|
|
|
public
|
|
|
|
{ classes }
|
|
|
|
MainWindow: NSWindow;
|
|
|
|
MainWindowView: NSView;
|
|
|
|
Toolbar: NSToolbar;
|
|
|
|
TextField: NSTextField;
|
2008-10-06 02:46:47 +00:00
|
|
|
MainMenu, AppleMenu, ServicesMenu, FileMenu: NSMenu;
|
|
|
|
OpenItem, SaveItem, SaveAsItem, ExitItem: NSMenuItem;
|
2008-09-29 00:47:50 +00:00
|
|
|
{ strings and sizes}
|
|
|
|
CFWindowTitle, CFEmptyString: CFStringRef;
|
|
|
|
MainWindowRect: NSRect;
|
|
|
|
{ methods }
|
|
|
|
procedure CreateUserInterface();
|
2008-10-06 02:46:47 +00:00
|
|
|
procedure AttachEventHandlers();
|
2008-09-29 00:47:50 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
|
|
|
myView: TMyView;
|
|
|
|
|
|
|
|
const
|
|
|
|
Str_Window_Title = 'Text Editor';
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses controller, model;
|
|
|
|
|
|
|
|
{@@
|
|
|
|
}
|
|
|
|
procedure TMyView.CreateUserInterface();
|
|
|
|
begin
|
|
|
|
CFEmptyString := CFStringCreateWithPascalString(nil, '', kCFStringEncodingUTF8);
|
|
|
|
|
|
|
|
{ Creates the main window }
|
|
|
|
|
|
|
|
MainWindowRect.origin.x := 300.0;
|
|
|
|
MainWindowRect.origin.y := 300.0;
|
|
|
|
MainWindowRect.size.width := 300.0;
|
|
|
|
MainWindowRect.size.height := 500.0;
|
|
|
|
|
|
|
|
MainWindow := NSWindow.initWithContentRect_styleMask_backing_defer(MainWindowRect,
|
|
|
|
NSTitledWindowMask or NSClosableWindowMask or NSMiniaturizableWindowMask or NSResizableWindowMask,
|
|
|
|
NSBackingStoreBuffered, LongBool(NO));
|
|
|
|
MainWindowView := NSView.CreateWithHandle(MainWindow.contentView);
|
|
|
|
|
|
|
|
CFWindowTitle := CFStringCreateWithPascalString(nil, Str_Window_Title, kCFStringEncodingUTF8);
|
|
|
|
MainWindow.setTitle(CFWindowTitle);
|
|
|
|
|
|
|
|
{ Adds the toolbar and it's buttons }
|
|
|
|
|
|
|
|
Toolbar := CreateToolbar(MainWindowView, 0, MainWindowRect.size.height - 50, MainWindowRect.size.width, 50);
|
|
|
|
|
|
|
|
MainWindow.setToolbar(Toolbar.Handle);
|
|
|
|
|
|
|
|
{ Add the text area }
|
|
|
|
|
|
|
|
TextField := CreateTextField();
|
|
|
|
|
|
|
|
{ Add the main menu }
|
|
|
|
|
2008-10-06 02:46:47 +00:00
|
|
|
CreateMainMenu();
|
|
|
|
end;
|
2008-09-29 00:47:50 +00:00
|
|
|
|
2008-10-06 13:26:48 +00:00
|
|
|
{@@
|
|
|
|
}
|
2008-10-06 02:46:47 +00:00
|
|
|
procedure TMyView.AttachEventHandlers();
|
|
|
|
begin
|
|
|
|
OpenItem.setTarget(myController.Handle);
|
|
|
|
OpenItem.setAction(sel_registerName(PChar('doOpenFile:')));
|
|
|
|
|
|
|
|
SaveItem.setTarget(myController.Handle);
|
|
|
|
SaveItem.setAction(sel_registerName(PChar('doSaveFile:')));
|
|
|
|
|
|
|
|
ExitItem.setTarget(myController.Handle);
|
|
|
|
ExitItem.setAction(sel_registerName(PChar('doClose:')));
|
|
|
|
end;
|
|
|
|
|
2008-10-06 13:26:48 +00:00
|
|
|
{@@
|
|
|
|
}
|
2008-10-06 02:46:47 +00:00
|
|
|
function TMyView.CreateAppleMenu(): NSMenu;
|
|
|
|
var
|
|
|
|
AppleMenuTitle, ServicesMenuTitle: CFStringRef;
|
|
|
|
begin
|
|
|
|
AppleMenuTitle := CFStringCreateWithPascalString(nil, 'Apple Menu', kCFStringEncodingUTF8);
|
|
|
|
ServicesMenuTitle := CFStringCreateWithPascalString(nil, 'Services', kCFStringEncodingUTF8);
|
|
|
|
|
|
|
|
{ Creates the Apple menu }
|
|
|
|
|
|
|
|
Result := NSMenu.initWithTitle(AppleMenuTitle);
|
|
|
|
|
|
|
|
{ Add the services submenu }
|
|
|
|
|
|
|
|
ServicesMenu := NSMenu.initWithTitle(ServicesMenuTitle);
|
|
|
|
NSApp.setServicesMenu(ServicesMenu.Handle);
|
|
|
|
end;
|
|
|
|
|
2008-10-06 13:26:48 +00:00
|
|
|
{@@
|
|
|
|
}
|
2008-10-06 02:46:47 +00:00
|
|
|
function TMyView.CreateFileMenu(): NSMenu;
|
|
|
|
var
|
|
|
|
MenuTitle: CFStringRef;
|
|
|
|
begin
|
|
|
|
MenuTitle := CFStringCreateWithPascalString(nil, 'File', kCFStringEncodingUTF8);
|
|
|
|
|
|
|
|
{ Creates the file menu }
|
|
|
|
|
|
|
|
Result := NSMenu.initWithTitle(MenuTitle);
|
|
|
|
|
|
|
|
{ Adds items to it }
|
|
|
|
|
|
|
|
OpenItem := CreateMenuItem('Open');
|
|
|
|
Result.addItem(OpenItem.Handle);
|
|
|
|
SaveItem := CreateMenuItem('Save');
|
|
|
|
Result.addItem(SaveItem.Handle);
|
|
|
|
ExitItem := CreateMenuItem('Exit');
|
|
|
|
Result.addItem(ExitItem.Handle);
|
|
|
|
end;
|
|
|
|
|
2008-10-06 13:26:48 +00:00
|
|
|
{@@
|
|
|
|
}
|
2008-10-06 02:46:47 +00:00
|
|
|
procedure TMyView.AddToMenubar(menu: NSMenu);
|
|
|
|
var
|
|
|
|
dummyItem: NSMenuItem;
|
|
|
|
begin
|
|
|
|
dummyItem := NSMenuItem.initWithTitle_action_keyEquivalent(CFEmptyString, nil, CFEmptyString);
|
|
|
|
dummyItem.setSubmenu(menu.Handle);
|
|
|
|
MainMenu.addItem(dummyItem.Handle);
|
|
|
|
dummyItem.Free;
|
2008-09-29 00:47:50 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
{@@
|
|
|
|
}
|
|
|
|
function TMyView.CreateToolbar(AOwnerView: NSView; AX, AY, AWidth,
|
|
|
|
AHeight: Double): NSToolbar;
|
|
|
|
begin
|
|
|
|
Result := NSToolbar.initWithIdentifier(CFEmptyString);
|
2008-10-06 02:46:47 +00:00
|
|
|
myToolbarController := TMyToolbarController.Create;
|
|
|
|
Result.setDelegate(myToolbarController.Handle);
|
2008-09-29 00:47:50 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
{@@
|
|
|
|
}
|
2008-10-06 02:46:47 +00:00
|
|
|
procedure TMyView.CreateMainMenu();
|
2008-09-29 00:47:50 +00:00
|
|
|
begin
|
2008-10-06 02:46:47 +00:00
|
|
|
MainMenu := NSMenu.initWithTitle(CFEmptyString);
|
2008-09-29 00:47:50 +00:00
|
|
|
|
2008-10-06 02:46:47 +00:00
|
|
|
NSApp.setMainMenu(MainMenu.Handle);
|
2008-09-29 00:47:50 +00:00
|
|
|
|
2008-10-06 02:46:47 +00:00
|
|
|
AppleMenu := CreateAppleMenu();
|
|
|
|
NSApp.setAppleMenu(AppleMenu.Handle);
|
|
|
|
AddToMenubar(AppleMenu);
|
2008-09-29 00:47:50 +00:00
|
|
|
|
2008-10-06 02:46:47 +00:00
|
|
|
FileMenu := CreateFileMenu();
|
|
|
|
AddToMenubar(FileMenu);
|
2008-09-29 00:47:50 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
{@@
|
|
|
|
}
|
2008-10-06 02:46:47 +00:00
|
|
|
function TMyView.CreateMenuItem(ATitle: shortstring): NSMenuItem;
|
2008-09-29 00:47:50 +00:00
|
|
|
var
|
|
|
|
ItemText: CFStringRef;
|
|
|
|
KeyText: CFStringRef;
|
|
|
|
begin
|
|
|
|
KeyText := CFStringCreateWithPascalString(nil, '', kCFStringEncodingUTF8);
|
|
|
|
ItemText := CFStringCreateWithPascalString(nil, ATitle, kCFStringEncodingUTF8);
|
|
|
|
WriteLn(' ItemText: ', IntToHex(Int64(ItemText), 8), ' ATitle: ', ATitle);
|
|
|
|
|
|
|
|
Result := NSMenuItem.initWithTitle_action_keyEquivalent(ItemText, nil, KeyText);
|
|
|
|
end;
|
|
|
|
|
2008-10-06 13:26:48 +00:00
|
|
|
{@@
|
|
|
|
Creates an autosized NSTextField
|
|
|
|
}
|
2008-09-29 00:47:50 +00:00
|
|
|
function TMyView.CreateTextField(): NSTextField;
|
|
|
|
var
|
|
|
|
ClientRect: NSRect;
|
|
|
|
begin
|
|
|
|
ClientRect.origin.x := 0.0;
|
|
|
|
ClientRect.origin.y := 0.0;
|
|
|
|
ClientRect.size.width := 300.0;
|
|
|
|
ClientRect.size.height := 500.0;
|
|
|
|
|
|
|
|
Result := NSTextField.initWithFrame(ClientRect);
|
2008-10-06 13:26:48 +00:00
|
|
|
Result.setAutoresizingMask(NSViewWidthSizable or NSViewHeightSizable);
|
2008-09-29 00:47:50 +00:00
|
|
|
MainWindowView.addSubview(Result.Handle);
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|
|
|
|
|