[fpc-devel] Carbon demonstration app

Felipe Monteiro de Carvalho felipemonteiro.carvalho at gmail.com
Mon Jun 11 23:48:26 CEST 2007


Hello,

I would like to contribute a carbon demonstration application.

A new folder should be created here: fpc/packages/extra/univint/examples

Then, I guess that I will need a makefile as well. I don't fully
understand exactly why, since I would say that this example won't be
compiled by default, but all other folders have makefiles.

I took a look at the Makefile.fpc for the gtk 1.2 examples and there
are some odd things:

[target]
dirs=3Dtutorial >>> Which tutorial dirs is this? There is no tutorial direc=
tory
programs=3Dentry notebook scribble clist ttt_test pixmap list
progressbar filesel \
         statusbar toolbar rulers spinbutton
 >> Any use in listing the examples here?

[require]
packages=3Dgtk >> for carbon it should be univint ?

[clean]
units=3Dtictactoe  >> So, it will only clean one example, and exactly
the one which isn't compiled???

[install]
fpcpackage=3Dy >> What does that mean?

[default]
fpcdir=3D../../../..

[rules]
.NOTPARALLEL:

Further, do I also need to change the makefile from the univint directory?

Further, should I also build the makefile for the new directory? And
in this case, do I need to regenerate the entire makefile tree? umm
... maybe I should let that to someone with more experience, I will
probably break something if I have to do this myself =3D)

I attached the app in case anyone wants to review it before I commit.

thanks,
-- =

Felipe Monteiro de Carvalho
-------------- next part --------------
{
 controldemo.pas

 **************************************************************************=
***
 *                                                                         =
  *
 *  This demonstration program is public domain, which means no copyright, =
  *
 * but also no warranty!                                                   =
  *
 *                                                                         =
  *
 *  This program is distributed in the hope that it will be useful,        =
  *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         =
  *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                   =
  *
 *                                                                         =
  *
 **************************************************************************=
***

 This application will create a window with two buttons in it

 When you click the button 'Hello Button',
  it will show or hide (alternating with each click) a text on the window

 When you click the button 'Show Dialog', it will show a modal message dial=
og

 Author: Felipe Monteiro de Carvalho

 Contributors: Ingemar Ragnemalm
}
program controldemo;

{$mode delphi}

uses
 SysUtils, FPCMacOSAll, MacPas;

var
  mainWindow: WindowRef;
  contentView: HIViewRef;
  button1, button2: ControlRef;
  staticText: ControlRef;
  showTextFlag: Boolean =3D false;

const
  kButtonHello =3D 'HELO';
  kButtonMessage =3D 'MSGE';

{ implementation of the functions }

{ Functions to easely generate carbon structures }

function GetQDRect(Left, Top, Width, Height: Integer): FPCMacOSAll.Rect;
begin
  result.Left :=3D Left;
  result.Top :=3D Top;
  result.Right :=3D Left + Width;
  result.Bottom :=3D Top + Height;
end;

{ Shows a message box }

procedure DoShowMessage(ATitle, AMsg: string);
var
  outItemHit: SInt16;
  err: OSErr;
begin
  err :=3D StandardAlert(kAlertNoteAlert, ATitle, AMsg, nil, outItemHit);
end;

{ Event handling routines }

{  Here we alternate the visibility status of the static text
  with each button click }
function ButtonHelloPressed: OSStatus;
begin
  result :=3D 0;

  showTextFlag :=3D not showTextFlag;

  if showTextFlag then HIViewSetVisible(staticText, True)
  else HIViewSetVisible(staticText, False);
end;

function ButtonMessagePressed: OSStatus;
begin
  result :=3D 0;

  DoShowMessage('Standard message dialog', 'This dialog is modal');
end;

{ Message handling function }

function WindowCommandHandler(nextHandler: EventHandlerCallRef; theEvent: E=
ventRef; userDataPtr: UnivPtr): OSStatus;
var
  status: OSStatus;
  ignoreresult: OSStatus;
  aCommand: HICommand;
begin
  status :=3D eventNotHandledErr;

  ignoreresult :=3D GetEventParameter(theEvent, kEventParamDirectObject,
   typeHICommand, nil, sizeof(aCommand), nil, @aCommand);

  if aCommand.commandID =3D FOUR_CHAR_CODE(kButtonHello) then status :=3D B=
uttonHelloPressed()
  else if aCommand.commandID =3D FOUR_CHAR_CODE(kButtonMessage) then status=
 :=3D ButtonMessagePressed();

  result :=3D status;
end;

{ Initialization and finalization routines }

procedure Initialize;
var
  status, ignoreResult: OSStatus;
  cmdEvent: EventTypeSpec;
  eventHandler: EventHandlerUPP;
  fontStyle: ControlFontStyleRec;
begin
  status :=3D CreateNewWindow(kDocumentWindowClass,
   (kWindowStandardDocumentAttributes or kWindowStandardHandlerAttribute
    or kWindowCompositingAttribute),
   GetQDRect(100, 100, 350, 350), mainWindow);

  if (status <> noErr) or (mainWindow =3D nil) then
  begin
    DoShowMessage('Error', 'CreateNewWindow failed');
    Exit;
  end;

  ignoreResult :=3D SetWindowTitleWithCFString(mainWindow, CFSTRP('Carbon F=
PC Controls Demo'));

  ignoreResult :=3D HIViewFindByID(HIViewGetRoot(mainWindow), kHIViewWindow=
ContentID, contentView);

  { Add events }

  cmdEvent.eventClass :=3D kEventClassCommand;
  cmdEvent.eventKind :=3D kEventCommandProcess;
  eventHandler :=3D NewEventHandlerUPP(@WindowCommandHandler);
  ignoreResult :=3D InstallEventHandler(GetWindowEventTarget(mainWindow),
   eventHandler, 1, @cmdEvent, nil, nil);

  { Creates the hello button }

  ignoreResult :=3D CreatePushButtonControl(nil, GetQDRect(50, 200, 100, 50=
),
   CFSTRP('Hello Button'), button1);

  ignoreResult :=3D HIViewAddSubview(contentView, button1);
  ignoreResult :=3D SetControlCommandID(button1, FOUR_CHAR_CODE(kButtonHell=
o));
  ignoreResult :=3D HIViewSetVisible(button1, TRUE);

  { Creates the message button }

  ignoreResult :=3D CreatePushButtonControl(nil, GetQDRect(200, 200, 100, 5=
0),
   CFSTRP('Show Dialog'), button2);

  ignoreResult :=3D HIViewAddSubview(contentView, button2);
  ignoreResult :=3D SetControlCommandID(button2, FOUR_CHAR_CODE(kButtonMess=
age));
  ignoreResult :=3D HIViewSetVisible(button2, TRUE);

  { Creates the text control }

  fontStyle.flags :=3D kControlUseJustMask or kControlUseSizeMask;
  fontStyle.just :=3D teCenter;
  fontStyle.size :=3D 30;

  ignoreResult :=3D CreateStaticTextControl(mainWindow,
   GetQDRect(0, 50, 350, 50), nil, @fontStyle, staticText);

  ignoreResult :=3D HIViewAddSubview(contentView, staticText);
  ignoreResult :=3D HIViewSetVisible(staticText, FALSE);

  HIViewSetText(staticText, CFSTRP('Hello Controls!'));

  { Shows the window }

  ShowWindow(mainWindow);
end;

procedure DoCloseWindow(theWind: WindowRef);
var
  theEvent: EventRef;
begin
  CreateEvent(nil, kEventClassWindow, kEventWindowClose, GetCurrentEventTim=
e, kEventAttributeNone, theEvent);
  SetEventParameter(theEvent, kEventParamDirectObject, typeWindowRef, sizeo=
f(WindowRef), theWind);
  SendEventToEventTarget(theEvent, GetWindowEventTarget(theWind));
end;

{ Closes all windows, so they have time to save any user data (none in this=
 case) }

procedure Finalize;
begin
  DoCloseWindow(mainWindow);
end;

{ Main program section }

begin
  Initialize();

  RunApplicationEventLoop();

  Finalize();
end.


More information about the fpc-devel mailing list