git.fiddlerwoaroof.com
Browse code

demo app works

Ed L authored on 06/01/2018 10:07:36
Showing 4 changed files
... ...
@@ -12,3 +12,9 @@ GTAGS
12 12
 .*.dSYM
13 13
 *.dSYM
14 14
 *~
15
+*.o
16
+.[#]*[#]
17
+[#]*
18
+*.old
19
+nsrect-expose.c
20
+test.c
... ...
@@ -12,11 +12,15 @@
12 12
 (cffi:defcfun (init-window "initWindow")
13 13
     :pointer
14 14
   (window :pointer)
15
-  (rect (:pointer (:struct objc-runtime:ns-rect)))
15
+  (rect :pointer)
16 16
   (a :char)
17 17
   (b :char)
18 18
   (c :boolean))
19 19
 
20
+(cffi:defcfun (print-rect "printRect")
21
+    :void
22
+  (rect (:struct objc-runtime::ns-rect)))
23
+
20 24
 #+null
21 25
 (cffi:defcfun (set-uncaught-exception-handler "set_uncaught_exception_handler"
22 26
                                               :library objc-runtime::expose-stuff)
... ...
@@ -28,9 +32,9 @@
28 32
   (check-type y real)
29 33
   (check-type w real)
30 34
   (check-type h real)
31
-  (cffi:with-foreign-object (rect '(:struct ns-rect))
35
+  (cffi:with-foreign-object (rect '(:struct objc-runtime::ns-rect))
32 36
     (cffi:with-foreign-slots (((:pointer ns-rect-origin) (:pointer ns-rect-size))
33
-                              rect (:struct ns-rect))
37
+                              rect (:struct objc-runtime::ns-rect))
34 38
       (cffi:with-foreign-slots ((ns-point-x ns-point-y) ns-rect-origin (:struct ns-point))
35 39
         (setf ns-point-x (coerce x 'double-float)
36 40
               ns-point-y (coerce y 'double-float)))
... ...
@@ -77,31 +81,53 @@
77 81
                            '(:struct objc-runtime:ns-rect)))
78 82
 
79 83
 (defun main ()
84
+  (break)
80 85
   (trivial-main-thread:with-body-in-main-thread ()
81 86
     (with-selectors ((shared-application "sharedApplication")
82 87
                      (process-info "processInfo")
83 88
                      (process-name "processName")
84 89
                      (set-activation-policy "setActivationPolicy:")
85
-                     (init-with-content-rect "initWithContentRect:styleMask:backing:defer:")
90
+                     ;; (init-with-content-rect "initWithContentRect:styleMask:backing:defer:")
86 91
                      (set-title "setTitle:")
87 92
                      (run "run")
88 93
                      (activate-ignoring-other-apps "activateIgnoringOtherApps:")
89
-                     alloc
90 94
                      (make-key-and-order-front "makeKeyAndOrderFront:")
91 95
                      (cascade-top-left-from-point "cascadeTopLeftFromPoint:")
96
+                     (add-item "addItem:")
97
+                     (set-main-menu "setMainMenu:")
98
+                     (init-with-title "initWithTitle:action:keyEquivalent:")
99
+                     (set-submenu "setSubmenu:")
100
+                     (init-with-encoding "initWithCString:length:")
101
+                     terminate?
92 102
                      ;; (application-should-terminate "applicationShouldTerminate:")
93 103
                      ;; (set-delegate "setDelegate:")
94 104
                      ;; (finish-launching "finishLaunching")
105
+                     alloc new autorelease
95 106
                      )
107
+      [#@NSAutoReleasePool new]
96 108
       [#@NSApplication shared-application]
97 109
       [objc-runtime::ns-app set-activation-policy :int 0]
98 110
 
99 111
       ;; (break)
100 112
       (let* ((application-name [[#@NSProcessInfo process-info] process-name]))
113
+        (let* ((menubar [[#@NSMenu new] autorelease])
114
+               (app-menu-item [[#@NSMenuItem new] autorelease])
115
+               (app-menu [[#@NSMenu new] autorelease])
116
+               (quit-name [[#@NSString alloc] init-with-encoding :string "Quit" :uint 4])
117
+               (key [[#@NSString alloc] init-with-encoding :string "q" :uint 1])
118
+               (quit-menu-item
119
+                [[[#@NSMenuItem alloc] init-with-title :pointer quit-name :pointer terminate? :string key] autorelease]))
120
+          [menubar add-item :pointer app-menu-item]
121
+          [app-menu add-item :pointer quit-menu-item]
122
+          [app-menu-item set-submenu :pointer app-menu]
123
+          [objc-runtime::ns-app set-main-menu :pointer menubar] )
124
+
101 125
         (with-point (p (20 20))
102 126
           (let* ((the-window [#@NSWindow alloc])
103 127
                  (foreign-rect (make-rect 10 10 120 120)))
104
-            (format t "~&My rect: ~s~%" (cffi:convert-from-foreign foreign-rect '(:struct objc-runtime::ns-rect)))
128
+            (format t "~&My rect: ~s~%"
129
+                    (cffi:convert-from-foreign foreign-rect
130
+                                               '(:struct objc-runtime::ns-rect)))
105 131
             (init-window the-window foreign-rect 1 2 nil)
106 132
             [the-window cascade-top-left-from-point :pointer p]
107 133
             [the-window set-title :pointer application-name]
... ...
@@ -6,13 +6,18 @@
6 6
 #define EXPORT __attribute__((visibility("default")))
7 7
 
8 8
 EXPORT void set_uncaught_exception_handler(NSUncaughtExceptionHandler * _Nullable handler) {
9
-  NSSetUncaughtExceptionHandler(handler);
10
-};
9
+  NSSetUncaughtExceptionHandler(handler);};
10
+
11
+EXPORT void printRect(NSRect rect) {
12
+  printf("Got a rect: (%f %f), (%f %f)\n",
13
+         rect.size.width,
14
+         rect.size.height,
15
+         rect.origin.x,
16
+         rect.origin.y);}
11 17
 
12 18
 EXPORT id initWindow(NSWindow *window, NSRect *rect, char a, char b, Boolean c) {
13 19
   printf("Got a rect: (%f %f), (%f %f)\n", rect->size.width, rect->size.height, rect->origin.x, rect->origin.y);
14 20
   return [window initWithContentRect: *rect
15 21
                            styleMask: a
16 22
                              backing: b
17
-                               defer: c];
18
-}
23
+                               defer: c];}
... ...
@@ -86,6 +86,16 @@
86 86
 (defgeneric get-methods (class)
87 87
   (:method ((class string))
88 88
     (get-methods (objc-look-up-class class)))
89
+
90
+  #+ccl
91
+  (:method ((class ccl:macptr))
92
+    (with-foreign-object (num-methods :int)
93
+      (let ((methods (class-copy-method-list class num-methods)))
94
+        (let ((result (list)))
95
+          (dotimes (n (mem-aref num-methods :int) (nreverse result))
96
+            (push (mem-aref methods :pointer n)
97
+                  result))))))
98
+
89 99
   #+sbcl
90 100
   (:method ((class sb-sys:system-area-pointer))
91 101
     (with-foreign-object (num-methods :int)
... ...
@@ -111,8 +121,13 @@
111 121
 
112 122
 (defparameter *selector-cache* (make-hash-table :test 'equal))
113 123
 
124
+(serapeum:eval-always
125
+  (defun normalize-selector-name (sel-name)
126
+    (substitute #\: #\? sel-name)))
127
+
114 128
 (defun ensure-selector (name)
115
-  (alexandria:ensure-gethash name *selector-cache*
129
+  (alexandria:ensure-gethash name
130
+                             *selector-cache*
116 131
                              (sel-register-name name)))
117 132
 
118 133
 (defmacro with-selectors ((&rest selector-specs) &body body)
... ...
@@ -120,7 +135,8 @@
120 135
                      `(,sym (ensure-selector ,foreign-selector)))
121 136
                    (mapcar (fw.lu:glambda (spec)
122 137
                              (:method ((spec symbol))
123
-                               (list spec (string-downcase spec)))
138
+                               (list spec (normalize-selector-name
139
+                                           (string-downcase spec))))
124 140
                              (:method ((spec cons))
125 141
                                (list (car spec) (cadr spec))))
126 142
                            selector-specs)))