carbon_main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. ppedit - A pattern plate editor for Spiro splines.
  3. Copyright (C) 2007 Raph Levien
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  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. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301, USA.
  16. */
  17. #include <Carbon/Carbon.h>
  18. #include "bezctx.h"
  19. #include "bezctx_quartz.h"
  20. #include "plate.h"
  21. #include "pe_view.h"
  22. #define kCommandToggleCorner 'togC'
  23. typedef struct {
  24. WindowRef window_ref;
  25. plate *p;
  26. HIViewRef view;
  27. } plate_edit;
  28. int n_iter = 10;
  29. pascal OSStatus my_handler(EventHandlerCallRef nextHandler, EventRef theEvent, void *data)
  30. {
  31. plate_edit *pe = (plate_edit *)data;
  32. WindowRef window = pe->window_ref;
  33. UInt32 klass = GetEventClass(theEvent);
  34. UInt32 kind = GetEventKind(theEvent);
  35. OSStatus result = eventNotHandledErr;
  36. OSStatus err;
  37. Point where;
  38. Rect bounds;
  39. switch (klass) {
  40. case kEventClassMouse:
  41. switch (kind) {
  42. case kEventMouseDown:
  43. err = GetEventParameter(theEvent, kEventParamMouseLocation,
  44. typeQDPoint, NULL, sizeof(where), NULL, &where);
  45. printf("mouse down %d %d\n", where.h, where.v);
  46. break;
  47. }
  48. break;
  49. case kEventClassWindow:
  50. switch (kind) {
  51. case kEventWindowDrawContent:
  52. printf("draw content\n");
  53. result = noErr;
  54. break;
  55. case kEventWindowClickContentRgn:
  56. printf("click_content region\n");
  57. break;
  58. case kEventWindowHandleContentClick:
  59. err = GetEventParameter(theEvent, kEventParamMouseLocation,
  60. typeQDPoint, NULL, sizeof(where), NULL, &where);
  61. GetWindowBounds(window, kWindowContentRgn, &bounds);
  62. printf("content click %d, %d; %d, %d\n", where.h, where.v,
  63. where.h - bounds.left, where.v - bounds.top);
  64. break;
  65. }
  66. }
  67. return result;
  68. }
  69. pascal OSStatus app_handler(EventHandlerCallRef nextHandler, EventRef theEvent, void *data)
  70. {
  71. plate_edit *pe = (plate_edit *)data;
  72. HICommand hiCommand;
  73. OSStatus result = eventNotHandledErr;
  74. GetEventParameter(theEvent, kEventParamDirectObject, typeHICommand,NULL,
  75. sizeof(HICommand),NULL,&hiCommand);
  76. unsigned int c = hiCommand.commandID;
  77. MenuRef menu;
  78. MenuItemIndex ix;
  79. printf("app_handler %c%c%c%c\n",
  80. (c >> 24) & 255, (c >> 16) & 255, (c >> 8) & 255, c & 255);
  81. switch (c) {
  82. case kHICommandUndo:
  83. GetIndMenuItemWithCommandID(NULL, kHICommandUndo, 1, &menu, &ix);
  84. SetMenuItemTextWithCFString(menu, ix, CFSTR("Undo disabled"));
  85. DisableMenuItem(menu, ix);
  86. break;
  87. case kCommandToggleCorner:
  88. pe_view_toggle_corner(pe->view);
  89. break;
  90. }
  91. return result;
  92. }
  93. void
  94. add_pe_view(WindowRef window, plate_edit *pe, plate *p)
  95. {
  96. HIRect rect = {{0, 0}, {32767, 32767}};
  97. HIViewRef view;
  98. pe_view_create(window, &rect, &view);
  99. pe_view_set_plate(view, p);
  100. HIViewSetVisible(view, true);
  101. pe->view = view;
  102. }
  103. void
  104. init_window(WindowRef window, plate_edit *pe)
  105. {
  106. EventTypeSpec app_event_types[] = {
  107. { kEventClassCommand, kEventProcessCommand }
  108. };
  109. EventTypeSpec event_types[] = {
  110. { kEventClassWindow, kEventWindowDrawContent },
  111. { kEventClassWindow, kEventWindowHandleContentClick },
  112. { kEventClassMouse, kEventMouseDown }
  113. };
  114. InstallApplicationEventHandler(NewEventHandlerUPP(app_handler),
  115. GetEventTypeCount(app_event_types),
  116. app_event_types, (void *)pe, NULL);
  117. InstallWindowEventHandler(window, NewEventHandlerUPP(my_handler),
  118. GetEventTypeCount(event_types),
  119. event_types, (void *)pe, NULL);
  120. add_pe_view(window, pe, pe->p);
  121. }
  122. int main(int argc, char* argv[])
  123. {
  124. IBNibRef nibRef;
  125. WindowRef window;
  126. plate_edit pe;
  127. OSStatus err;
  128. // Create a Nib reference passing the name of the nib file (without the .nib extension)
  129. // CreateNibReference only searches into the application bundle.
  130. err = CreateNibReference(CFSTR("main"), &nibRef);
  131. require_noerr( err, CantGetNibRef );
  132. // Once the nib reference is created, set the menu bar. "MainMenu" is the name of the menu bar
  133. // object. This name is set in InterfaceBuilder when the nib is created.
  134. err = SetMenuBarFromNib(nibRef, CFSTR("MenuBar"));
  135. require_noerr( err, CantSetMenuBar );
  136. // Then create a window. "MainWindow" is the name of the window object. This name is set in
  137. // InterfaceBuilder when the nib is created.
  138. err = CreateWindowFromNib(nibRef, CFSTR("MainWindow"), &window);
  139. require_noerr( err, CantCreateWindow );
  140. // We don't need the nib reference anymore.
  141. DisposeNibReference(nibRef);
  142. pe.window_ref = window;
  143. pe.p = file_read_plate("/Users/raph/golf/ppedit/g.plate");
  144. if (pe.p == NULL)
  145. pe.p = new_plate();
  146. init_window(window, &pe);
  147. // The window was created hidden so show it.
  148. ShowWindow( window );
  149. // Call the event loop
  150. RunApplicationEventLoop();
  151. CantCreateWindow:
  152. CantSetMenuBar:
  153. CantGetNibRef:
  154. return err;
  155. }