3434    nativeView: NimxView 
3535    renderingContext: GraphicsContext 
3636    displayLink: CVDisplayLink 
37+     textInputActive: bool 
3738
3839const 
3940  AppDelegateClass  =  " NimxAppDelegate" 
@@ -458,13 +459,13 @@ proc nextEvent(evt: var sdl2.Event) =
458459          break 
459460
460461  animateAndDraw() 
462+ ]# 
461463
462- method startTextInput*(w: EmscriptenWindow , r: Rect) = 
463-   startTextInput()  
464+ method  startTextInput * (w: AppkitWindow , r: Rect ) = 
465+   w.textInputActive  =   true 
464466
465- method stopTextInput*(w: EmscriptenWindow) = 
466-   stopTextInput() 
467- ]# 
467+ method  stopTextInput * (w: AppkitWindow ) = 
468+   w.textInputActive =  false 
468469
469470proc  runUntilQuit (cb: proc ()) = 
470471  let  pool =  NSAutoreleasePool .alloc ().init ()
@@ -519,6 +520,8 @@ proc appkitModFlags(f: uint): ModifiersSet =
519520    result .incl (LeftControl )
520521
521522proc  eventWithNSEvent (w: AppkitWindow , e: NSEvent ): Event  = 
523+   let  mods =  appkitModFlags (cast [uint ](e.modifierFlags))
524+ 
522525  case  e.kind
523526  of  NSLeftMouseDown :
524527    result  =  newMouseButtonEvent (w.pointFromNSEvent (e), VirtualKey .MouseButtonPrimary , bsDown)
@@ -532,14 +535,25 @@ proc eventWithNSEvent(w: AppkitWindow, e: NSEvent): Event =
532535    result  =  newMouseButtonEvent (w.pointFromNSEvent (e), VirtualKey .MouseButtonSecondary , bsUp)
533536  of  NSRightMouseDragged :
534537    result  =  newMouseButtonEvent (w.pointFromNSEvent (e), VirtualKey .MouseButtonSecondary , bsUnknown)
538+   of  NSScrollWheel :
539+     result  =  newEvent (etScroll, w.pointFromNSEvent (e))
540+     result .offset.x =  e.scrollingDeltaX
541+     result .offset.y =  e.scrollingDeltaY
535542  of  NSKeyDown :
536-     result  =  newKeyboardEvent (virtualKeyFromNative (e.keyCode.int ), bsDown, e.isARepeat)
543+     let  vk =  virtualKeyFromNative (e.keyCode.int )
544+     if  w.textInputActive and 
545+        vk.toAscii !=  char 0 ) and 
546+        not  mods.anyCtrl and  not  mods.anyGui:
547+         result  =  newEvent (etTextInput)
548+         result .text =  $ e.characters
549+     else :
550+       result  =  newKeyboardEvent (vk, bsDown, e.isARepeat)
537551  of  NSKeyUp :
538552    result  =  newKeyboardEvent (virtualKeyFromNative (e.keyCode.int ), bsUp, e.isARepeat)
539553  else :
540554    discard 
541555
542-   result .modifiers =  appkitModFlags ( cast [ uint ](e.modifierFlags)) 
556+   result .modifiers =  mods 
543557  result .window =  w
544558
545559proc  getNimxWindow (v: NimxView ): AppkitWindow  = 
@@ -569,6 +583,14 @@ proc reshape(self: NimxView, sel: SEL) =
569583proc  onDisplayLinkMainThread (self: NimxView , s: SEL , r: NSObject ) = 
570584  self.setNeedsDisplay (true )
571585
586+ proc  handleEvent (self: NimxView , cmd: SEL , e: NSEvent ) = 
587+   let  w =  self.getNimxWindow ()
588+   var  evt =  w.eventWithNSEvent (e)
589+   if  not  mainApplication ().handleEvent (evt):
590+     var  s =  ObjcSuper getSuperclass (viewClass ()))
591+     let  superImp =  cast [proc (s: var  ObjcSuper , c: SEL , e: NSEvent ) {.cdecl .}](objc_msgSendSuper)
592+     superImp (s, cmd, e)
593+ 
572594proc  createViewClass (): ObjcClass  = 
573595  result  =  allocateClassPair (getClass (" NSOpenGLView" " NimxView" 0 )
574596
@@ -577,16 +599,23 @@ proc createViewClass(): ObjcClass =
577599  discard  addMethod (result , sel_registerName (" reshape" 
578600  discard  addMethod (result , sel_registerName (" onDisplayLinkMainThread:" 
579601
580-   registerClassPair (result )
581- 
602+   template  ev (e: cstring ) = 
603+     discard  addMethod (result , sel_registerName (e), handleEvent)
604+   ev (" keyDown:" 
605+   ev (" keyUp:" 
606+   ev (" mouseDown:" 
607+   ev (" mouseUp:" 
608+   ev (" mouseDragged:" 
609+   ev (" rightMouseDown:" 
610+   ev (" rightMouseUp:" 
611+   ev (" rightMouseDragged:" 
612+   ev (" otherMouseDown:" 
613+   ev (" otherMouseUp:" 
614+   ev (" otherMouseDragged:" 
615+   ev (" scrollWheel:" 
582616
583- #  - (void)keyDown: (NSEvent*) e {
584- #    [super keyDown: e];
585- #  }
617+   registerClassPair (result )
586618
587- #  - (void)keyUp: (NSEvent*) e {
588- #    [super keyUp: e];
589- #  }
590619
591620#  - (void)insertText:(id)string replacementRange:(NSRange)replacementRange {
592621#    NSLog(@"text: %@", string);
@@ -637,17 +666,6 @@ proc createViewClass(): ObjcClass =
637666#  """.}
638667
639668
640- proc  sendEvent (self: NimxWindow , cmd: SEL , e: NSEvent ) {.cdecl .} = 
641-   var  s =  ObjcSuper getSuperclass (windowClass ()))
642-   let  superImp =  cast [proc (s: var  ObjcSuper , c: SEL , e: NSEvent ) {.cdecl .}](objc_msgSendSuper)
643-   superImp (s, cmd, e)
644- 
645-   let  w =  self.getNimxWindow ()
646-   var  evt =  w.eventWithNSEvent (e)
647-   discard  mainApplication ().handleEvent (evt)
648- 
649669proc  createWindowClass (): ObjcClass  = 
650670  result  =  allocateClassPair (getClass (" NSWindow" " NimxWindow" 0 )
651-   discard  addMethod (result , sel_registerName (" sendEvent:" 
652- 
653671  registerClassPair (result )
0 commit comments