controldemo.pas 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. {
  2. controldemo.pas
  3. *****************************************************************************
  4. * *
  5. * This demonstration program is public domain, which means no copyright, *
  6. * but also no warranty! *
  7. * *
  8. * This program is distributed in the hope that it will be useful, *
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
  11. * *
  12. *****************************************************************************
  13. This application will create a window with two buttons in it
  14. When you click the button 'Hello Button',
  15. it will show or hide (alternating with each click) a text on the window
  16. When you click the button 'Show Dialog', it will show a modal message dialog
  17. Author: Felipe Monteiro de Carvalho
  18. Contributors: Ingemar Ragnemalm
  19. }
  20. program controldemo;
  21. {$mode delphi}
  22. uses
  23. SysUtils, FPCMacOSAll, MacPas;
  24. var
  25. mainWindow: WindowRef;
  26. contentView: HIViewRef;
  27. button1, button2: ControlRef;
  28. staticText: ControlRef;
  29. showTextFlag: Boolean = false;
  30. const
  31. kButtonHello = 'HELO';
  32. kButtonMessage = 'MSGE';
  33. { implementation of the functions }
  34. { Functions to easely generate carbon structures }
  35. function GetQDRect(Left, Top, Width, Height: Integer): FPCMacOSAll.Rect;
  36. begin
  37. result.Left := Left;
  38. result.Top := Top;
  39. result.Right := Left + Width;
  40. result.Bottom := Top + Height;
  41. end;
  42. { Shows a message box }
  43. procedure DoShowMessage(ATitle, AMsg: string);
  44. var
  45. outItemHit: SInt16;
  46. err: OSErr;
  47. begin
  48. err := StandardAlert(kAlertNoteAlert, ATitle, AMsg, nil, outItemHit);
  49. end;
  50. { Event handling routines }
  51. { Here we alternate the visibility status of the static text
  52. with each button click }
  53. function ButtonHelloPressed: OSStatus;
  54. begin
  55. result := 0;
  56. showTextFlag := not showTextFlag;
  57. if showTextFlag then HIViewSetVisible(staticText, True)
  58. else HIViewSetVisible(staticText, False);
  59. end;
  60. function ButtonMessagePressed: OSStatus;
  61. begin
  62. result := 0;
  63. DoShowMessage('Standard message dialog', 'This dialog is modal');
  64. end;
  65. { Message handling function }
  66. function WindowCommandHandler(nextHandler: EventHandlerCallRef; theEvent: EventRef; userDataPtr: UnivPtr): OSStatus; cdecl;
  67. var
  68. status: OSStatus;
  69. ignoreresult: OSStatus;
  70. aCommand: HICommand;
  71. begin
  72. status := eventNotHandledErr;
  73. ignoreresult := GetEventParameter(theEvent, kEventParamDirectObject,
  74. typeHICommand, nil, sizeof(aCommand), nil, @aCommand);
  75. if aCommand.commandID = FOUR_CHAR_CODE(kButtonHello) then status := ButtonHelloPressed()
  76. else if aCommand.commandID = FOUR_CHAR_CODE(kButtonMessage) then status := ButtonMessagePressed();
  77. result := status;
  78. end;
  79. { Initialization and finalization routines }
  80. procedure Initialize;
  81. var
  82. status, ignoreResult: OSStatus;
  83. cmdEvent: EventTypeSpec;
  84. eventHandler: EventHandlerUPP;
  85. fontStyle: ControlFontStyleRec;
  86. begin
  87. status := CreateNewWindow(kDocumentWindowClass,
  88. (kWindowStandardDocumentAttributes or kWindowStandardHandlerAttribute
  89. or kWindowCompositingAttribute),
  90. GetQDRect(100, 100, 350, 350), mainWindow);
  91. if (status <> noErr) or (mainWindow = nil) then
  92. begin
  93. DoShowMessage('Error', 'CreateNewWindow failed');
  94. Exit;
  95. end;
  96. ignoreResult := SetWindowTitleWithCFString(mainWindow, CFSTRP('Carbon FPC Controls Demo'));
  97. ignoreResult := HIViewFindByID(HIViewGetRoot(mainWindow), kHIViewWindowContentID, contentView);
  98. { Add events }
  99. cmdEvent.eventClass := kEventClassCommand;
  100. cmdEvent.eventKind := kEventCommandProcess;
  101. eventHandler := NewEventHandlerUPP(@WindowCommandHandler);
  102. ignoreResult := InstallEventHandler(GetWindowEventTarget(mainWindow),
  103. eventHandler, 1, @cmdEvent, nil, nil);
  104. { Creates the hello button }
  105. ignoreResult := CreatePushButtonControl(nil, GetQDRect(50, 200, 100, 50),
  106. CFSTRP('Hello Button'), button1);
  107. ignoreResult := HIViewAddSubview(contentView, button1);
  108. ignoreResult := SetControlCommandID(button1, FOUR_CHAR_CODE(kButtonHello));
  109. ignoreResult := HIViewSetVisible(button1, TRUE);
  110. { Creates the message button }
  111. ignoreResult := CreatePushButtonControl(nil, GetQDRect(200, 200, 100, 50),
  112. CFSTRP('Show Dialog'), button2);
  113. ignoreResult := HIViewAddSubview(contentView, button2);
  114. ignoreResult := SetControlCommandID(button2, FOUR_CHAR_CODE(kButtonMessage));
  115. ignoreResult := HIViewSetVisible(button2, TRUE);
  116. { Creates the text control }
  117. fontStyle.flags := kControlUseJustMask or kControlUseSizeMask;
  118. fontStyle.just := teCenter;
  119. fontStyle.size := 30;
  120. ignoreResult := CreateStaticTextControl(mainWindow,
  121. GetQDRect(0, 50, 350, 50), nil, @fontStyle, staticText);
  122. ignoreResult := HIViewAddSubview(contentView, staticText);
  123. ignoreResult := HIViewSetVisible(staticText, FALSE);
  124. HIViewSetText(staticText, CFSTRP('Hello Controls!'));
  125. { Shows the window }
  126. ShowWindow(mainWindow);
  127. end;
  128. procedure DoCloseWindow(theWind: WindowRef);
  129. var
  130. theEvent: EventRef;
  131. begin
  132. CreateEvent(nil, kEventClassWindow, kEventWindowClose, GetCurrentEventTime, kEventAttributeNone, theEvent);
  133. SetEventParameter(theEvent, kEventParamDirectObject, typeWindowRef, sizeof(WindowRef), theWind);
  134. SendEventToEventTarget(theEvent, GetWindowEventTarget(theWind));
  135. end;
  136. { Closes all windows, so they have time to save any user data (none in this case) }
  137. procedure Finalize;
  138. begin
  139. DoCloseWindow(mainWindow);
  140. end;
  141. { Main program section }
  142. begin
  143. Initialize();
  144. RunApplicationEventLoop();
  145. Finalize();
  146. end.