ultimatepp/uppsrc/CtrlCore/CocoProc.mm
cxl f10f1521b8 CtrlCore: Developing Cocoa
git-svn-id: svn://ultimatepp.org/upp/trunk@12088 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2018-07-21 17:14:40 +00:00

121 lines
No EOL
3.5 KiB
Text

#include "CocoMM.h"
#ifdef GUI_COCO
#define LLOG(x)
static Upp::Point coco_mouse_pos;
static bool coco_mouse_left;
static bool coco_mouse_right;
static int coco_flags;
namespace Upp {
bool GetShift() { return coco_flags & NSEventModifierFlagShift; }
bool GetCtrl() { return coco_flags & NSEventModifierFlagCommand; }
bool GetAlt() { return coco_flags & NSEventModifierFlagControl; }
bool GetOption() { return coco_flags & NSEventModifierFlagOption; }
bool GetCapsLock() { return coco_flags & NSEventModifierFlagCapsLock; }
/*
NSEventModifierFlagOption
NSEventModifierFlagNumericPad
NSEventModifierFlagHelp
NSEventModifierFlagFunction
NSEventModifierFlagDeviceIndependentFlagsMask
*/
bool GetMouseLeft() { return coco_mouse_left; }
bool GetMouseRight() { return coco_mouse_right; }
bool GetMouseMiddle() { return false; } // TODO
Point GetMousePos() { return coco_mouse_pos; } // TODO: read it if no mouse events
struct MMImp {
static void MouseEvent(CocoView *view, NSEvent *e, int event)
{
NSPoint np = [view convertPoint:[e locationInWindow] fromView:nil];
Upp::Point p(np.x, np.y);
int zd = 0; // TODO: MouseWheel
coco_mouse_pos = p + view->ctrl->GetRect().TopLeft();
view->ctrl->DispatchMouse(event, p, zd);
DLOG("MOUSE " << Upp::Point(p.x, p.y));
}
static void Flags(NSEvent *e)
{
coco_flags = [e modifierFlags];
}
static void Paint(Upp::Ctrl *ctrl, Upp::SystemDraw& w, const Rect& r)
{
DLOG("MMPaint " << r);
ctrl->fullrefresh = false;
ctrl->UpdateArea(w, r);
}
static void KeyEvent(Upp::Ctrl *ctrl, NSEvent *e, int up) {
Flags(e);
Upp::dword k = e.keyCode|K_DELTA|up;
if(GetCtrl())
k |= K_CTRL;
if(GetShift())
k |= K_SHIFT;
if(GetAlt())
k |= K_ALT;
if(GetOption()) // TODO
k |= K_OPTION;
ctrl->DispatchKey(k, 1);
if(!up) {
WString x = ToWString((CFStringRef)(e.characters));
for(wchar c : x)
if(c < 0xF700)
ctrl->DispatchKey(c, 1);
}
}
static void GotFocus(Upp::Ctrl *ctrl)
{
ctrl->ActivateWnd();
}
};
};
@implementation CocoView
-(void)drawRect:(NSRect)rect {
Upp::SystemDraw w([[NSGraphicsContext currentContext] CGContext], [self bounds].size.height);
Upp::MMImp::Paint(ctrl, w, MakeRect(rect));
}
- (void)mouseDown:(NSEvent *)e { Upp::MMImp::MouseEvent(self, e, Upp::Ctrl::LEFTDOWN); coco_mouse_left = true; }
- (void)mouseUp:(NSEvent *)e { Upp::MMImp::MouseEvent(self, e, Upp::Ctrl::LEFTUP); coco_mouse_left = false; }
- (void)mouseMoved:(NSEvent *)e { Upp::MMImp::MouseEvent(self, e, Upp::Ctrl::MOUSEMOVE); }
- (void)mouseDragged:(NSEvent *)e { Upp::MMImp::MouseEvent(self, e, Upp::Ctrl::MOUSEMOVE); } // TODO?
- (void)rightMouseDown:(NSEvent*)e { Upp::MMImp::MouseEvent(self, e, Upp::Ctrl::RIGHTDOWN); coco_mouse_right = true; }
- (void)rightMouseUp:(NSEvent*)e { Upp::MMImp::MouseEvent(self, e, Upp::Ctrl::RIGHTUP); coco_mouse_right = false; }
- (void)keyDown:(NSEvent *)e {
Upp::MMImp::KeyEvent(ctrl, e, 0);
}
- (void)keyUp:(NSEvent *)e {
Upp::MMImp::KeyEvent(ctrl, e, Upp::K_KEYUP);
}
- (void)windowDidResize:(NSNotification *)notification { Upp::MMCtrl::SyncRect(self); }
- (void)windowDidMove:(NSNotification *)notification { Upp::MMCtrl::SyncRect(self); }
//TODO: more layout changes
- (void)windowDidBecomeKey:(NSNotification *)notification
{
DLOG("DidBecomeKey");
Upp::MMImp::GotFocus(ctrl);
}
- (BOOL)acceptsFirstResponder { return YES; }
- (BOOL)canBecomeKeyView { return YES; }
@end
#endif