| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <Carbon/Carbon.h> |
| 22 |
|
| 23 |
#include "bezctx.h" |
| 24 |
#include "bezctx_quartz.h" |
| 25 |
#include "plate.h" |
| 26 |
#include "pe_view.h" |
| 27 |
|
| 28 |
#define kCommandToggleCorner 'togC' |
| 29 |
|
| 30 |
typedef struct { |
| 31 |
WindowRef window_ref; |
| 32 |
plate *p; |
| 33 |
|
| 34 |
HIViewRef view; |
| 35 |
} plate_edit; |
| 36 |
|
| 37 |
int n_iter = 10; |
| 38 |
|
| 39 |
pascal OSStatus my_handler(EventHandlerCallRef nextHandler, EventRef theEvent, void *data) |
| 40 |
{ |
| 41 |
plate_edit *pe = (plate_edit *)data; |
| 42 |
WindowRef window = pe->window_ref; |
| 43 |
UInt32 klass = GetEventClass(theEvent); |
| 44 |
UInt32 kind = GetEventKind(theEvent); |
| 45 |
OSStatus result = eventNotHandledErr; |
| 46 |
OSStatus err; |
| 47 |
Point where; |
| 48 |
Rect bounds; |
| 49 |
|
| 50 |
switch (klass) { |
| 51 |
case kEventClassMouse: |
| 52 |
switch (kind) { |
| 53 |
case kEventMouseDown: |
| 54 |
err = GetEventParameter(theEvent, kEventParamMouseLocation, |
| 55 |
typeQDPoint, NULL, sizeof(where), NULL, &where); |
| 56 |
printf("mouse down %d %d\n", where.h, where.v); |
| 57 |
break; |
| 58 |
} |
| 59 |
break; |
| 60 |
case kEventClassWindow: |
| 61 |
switch (kind) { |
| 62 |
case kEventWindowDrawContent: |
| 63 |
printf("draw content\n"); |
| 64 |
result = noErr; |
| 65 |
break; |
| 66 |
case kEventWindowClickContentRgn: |
| 67 |
printf("click_content region\n"); |
| 68 |
break; |
| 69 |
case kEventWindowHandleContentClick: |
| 70 |
err = GetEventParameter(theEvent, kEventParamMouseLocation, |
| 71 |
typeQDPoint, NULL, sizeof(where), NULL, &where); |
| 72 |
GetWindowBounds(window, kWindowContentRgn, &bounds); |
| 73 |
printf("content click %d, %d; %d, %d\n", where.h, where.v, |
| 74 |
where.h - bounds.left, where.v - bounds.top); |
| 75 |
break; |
| 76 |
} |
| 77 |
} |
| 78 |
return result; |
| 79 |
} |
| 80 |
|
| 81 |
pascal OSStatus app_handler(EventHandlerCallRef nextHandler, EventRef theEvent, void *data) |
| 82 |
{ |
| 83 |
plate_edit *pe = (plate_edit *)data; |
| 84 |
HICommand hiCommand; |
| 85 |
OSStatus result = eventNotHandledErr; |
| 86 |
GetEventParameter(theEvent, kEventParamDirectObject, typeHICommand,NULL, |
| 87 |
sizeof(HICommand),NULL,&hiCommand); |
| 88 |
unsigned int c = hiCommand.commandID; |
| 89 |
MenuRef menu; |
| 90 |
MenuItemIndex ix; |
| 91 |
|
| 92 |
printf("app_handler %c%c%c%c\n", |
| 93 |
(c >> 24) & 255, (c >> 16) & 255, (c >> 8) & 255, c & 255); |
| 94 |
switch (c) { |
| 95 |
case kHICommandUndo: |
| 96 |
GetIndMenuItemWithCommandID(NULL, kHICommandUndo, 1, &menu, &ix); |
| 97 |
SetMenuItemTextWithCFString(menu, ix, CFSTR("Undo disabled")); |
| 98 |
DisableMenuItem(menu, ix); |
| 99 |
break; |
| 100 |
case kCommandToggleCorner: |
| 101 |
pe_view_toggle_corner(pe->view); |
| 102 |
break; |
| 103 |
} |
| 104 |
return result; |
| 105 |
} |
| 106 |
|
| 107 |
void |
| 108 |
add_pe_view(WindowRef window, plate_edit *pe, plate *p) |
| 109 |
{ |
| 110 |
HIRect rect = {{0, 0}, {32767, 32767}}; |
| 111 |
HIViewRef view; |
| 112 |
|
| 113 |
pe_view_create(window, &rect, &view); |
| 114 |
pe_view_set_plate(view, p); |
| 115 |
HIViewSetVisible(view, true); |
| 116 |
pe->view = view; |
| 117 |
} |
| 118 |
|
| 119 |
void |
| 120 |
init_window(WindowRef window, plate_edit *pe) |
| 121 |
{ |
| 122 |
EventTypeSpec app_event_types[] = { |
| 123 |
{ kEventClassCommand, kEventProcessCommand } |
| 124 |
}; |
| 125 |
EventTypeSpec event_types[] = { |
| 126 |
{ kEventClassWindow, kEventWindowDrawContent }, |
| 127 |
{ kEventClassWindow, kEventWindowHandleContentClick }, |
| 128 |
{ kEventClassMouse, kEventMouseDown } |
| 129 |
}; |
| 130 |
|
| 131 |
InstallApplicationEventHandler(NewEventHandlerUPP(app_handler), |
| 132 |
GetEventTypeCount(app_event_types), |
| 133 |
app_event_types, (void *)pe, NULL); |
| 134 |
InstallWindowEventHandler(window, NewEventHandlerUPP(my_handler), |
| 135 |
GetEventTypeCount(event_types), |
| 136 |
event_types, (void *)pe, NULL); |
| 137 |
|
| 138 |
add_pe_view(window, pe, pe->p); |
| 139 |
} |
| 140 |
|
| 141 |
int main(int argc, char* argv[]) |
| 142 |
{ |
| 143 |
IBNibRef nibRef; |
| 144 |
WindowRef window; |
| 145 |
plate_edit pe; |
| 146 |
|
| 147 |
OSStatus err; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
err = CreateNibReference(CFSTR("main"), &nibRef); |
| 152 |
require_noerr( err, CantGetNibRef ); |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
err = SetMenuBarFromNib(nibRef, CFSTR("MenuBar")); |
| 157 |
require_noerr( err, CantSetMenuBar ); |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
err = CreateWindowFromNib(nibRef, CFSTR("MainWindow"), &window); |
| 162 |
require_noerr( err, CantCreateWindow ); |
| 163 |
|
| 164 |
|
| 165 |
DisposeNibReference(nibRef); |
| 166 |
|
| 167 |
pe.window_ref = window; |
| 168 |
pe.p = file_read_plate("/Users/raph/golf/ppedit/g.plate"); |
| 169 |
if (pe.p == NULL) |
| 170 |
pe.p = new_plate(); |
| 171 |
init_window(window, &pe); |
| 172 |
|
| 173 |
ShowWindow( window ); |
| 174 |
|
| 175 |
|
| 176 |
RunApplicationEventLoop(); |
| 177 |
|
| 178 |
CantCreateWindow: |
| 179 |
CantSetMenuBar: |
| 180 |
CantGetNibRef: |
| 181 |
return err; |
| 182 |
} |