diff --git a/uppsrc/CtrlCore/Coco.h b/uppsrc/CtrlCore/Coco.h index d20cb3f8b..63a3ef50a 100644 --- a/uppsrc/CtrlCore/Coco.h +++ b/uppsrc/CtrlCore/Coco.h @@ -137,6 +137,8 @@ void DrawDragRect(SystemDraw& w, const Rect& rect1, const Rect& rect2, const Rec #define GUIPLATFORM_KEYCODES_INCLUDE struct CocoTop; +struct MMCtrl; +struct MMImp; #define GUIPLATFORM_CTRL_TOP_DECLS CocoTop *coco = NULL; diff --git a/uppsrc/CtrlCore/CocoCreate.mm b/uppsrc/CtrlCore/CocoCreate.mm index d7a032a97..ab658f8d0 100644 --- a/uppsrc/CtrlCore/CocoCreate.mm +++ b/uppsrc/CtrlCore/CocoCreate.mm @@ -4,6 +4,12 @@ #define LLOG(x) +void Upp::MMCtrl::SyncRect(CocoView *view) +{ + NSWindow *win = [view window]; + view->ctrl->SetWndRect(MakeRect([win contentRectForFrameRect: [win frame]])); +} + void Upp::Ctrl::Create(const Upp::Rect& r, const char *title) { NSRect frame = NSMakeRect(r.left, r.top, r.GetWidth(), r.GetHeight()); @@ -31,7 +37,8 @@ void Upp::Ctrl::Create(const Upp::Rect& r, const char *title) top->coco = new CocoTop; top->coco->window = window; top->coco->view = view; - SyncRect(view); + MMCtrl::SyncRect(view); + isopen = true; } void Upp::Ctrl::WndDestroy() @@ -42,26 +49,20 @@ void Upp::Ctrl::WndDestroy() delete top->coco; delete top; top = NULL; + isopen = false; } void Upp::Ctrl::WndInvalidateRect(const Rect& r) { GuiLock __; + DLOG("InvalidateRect " << r); [top->coco->view setNeedsDisplay:YES]; // - (BOOL)needsToDrawRect:(NSRect)rect; } -void Upp::Ctrl::NewRect__(const Rect& r) -{ - DLOG("NewRect " << r); - SetWndRect(r); +bool Upp::Ctrl::IsWndOpen() const { + GuiLock __; + return top; } -/* -Upp::Size CocoWindow::GetSize() const -{ - return top ? top->ns_size : Size(0, 0); -} -*/ - #endif diff --git a/uppsrc/CtrlCore/CocoCtrl.cpp b/uppsrc/CtrlCore/CocoCtrl.cpp index 3f7c57a10..58f8c13ec 100644 --- a/uppsrc/CtrlCore/CocoCtrl.cpp +++ b/uppsrc/CtrlCore/CocoCtrl.cpp @@ -6,15 +6,6 @@ NAMESPACE_UPP -bool GetShift() { return false; } -bool GetCtrl() { return false; } -bool GetAlt() { return false; } -bool GetCapsLock() { return false; } -bool GetMouseLeft() { return false; } -bool GetMouseRight() { return false; } -bool GetMouseMiddle() { return false; } -Point GetMousePos() { return Point(0, 0); } - void ChHostSkin(void) { } diff --git a/uppsrc/CtrlCore/CocoCtrl.h b/uppsrc/CtrlCore/CocoCtrl.h index e25eddfc0..de6cd68f8 100644 --- a/uppsrc/CtrlCore/CocoCtrl.h +++ b/uppsrc/CtrlCore/CocoCtrl.h @@ -1,12 +1,11 @@ //$ class Ctrl { private: void SetImageCursor(const Image& img); + + friend struct MMCtrl; + friend struct MMImp; public: -// defeat private: to connect ObjectiveC with U++ - void CocoDispatchMouse__(int event, Point p, int zdelta) { DispatchMouse(event, p, zdelta); } - void NewRect__(const Rect& r); - // TODO: Temp only, remove! void Create(const Upp::Rect& r, const char *title); diff --git a/uppsrc/CtrlCore/CocoMM.h b/uppsrc/CtrlCore/CocoMM.h index 5c2bc4cff..36d1b52cf 100644 --- a/uppsrc/CtrlCore/CocoMM.h +++ b/uppsrc/CtrlCore/CocoMM.h @@ -60,6 +60,10 @@ struct RectCG { } @end +struct Upp::MMCtrl { + static void SyncRect(CocoView *view); +}; + struct Upp::CocoTop { NSWindow *window = NULL; CocoView *view = NULL; diff --git a/uppsrc/CtrlCore/CocoProc.mm b/uppsrc/CtrlCore/CocoProc.mm index c2fdcef61..1d76ab470 100644 --- a/uppsrc/CtrlCore/CocoProc.mm +++ b/uppsrc/CtrlCore/CocoProc.mm @@ -4,33 +4,69 @@ #define LLOG(x) -void DoMouseEvent(CocoView *view, NSEvent *e, int event) -{ - DLOG("MOUSE"); - NSPoint p = [view convertPoint:[e locationInWindow] fromView:nil]; - int zd = 0; // TODO: MouseWheel - view->ctrl->CocoDispatchMouse__(event, Upp::Point(p.x, p.y), zd); -} +static Upp::Point coco_mouse_pos; +static bool coco_mouse_left; +static bool coco_mouse_right; +static int coco_flags; -void SyncRect(CocoView *view) -{ - NSWindow *win = [view window]; - view->ctrl->NewRect__(MakeRect([win contentRectForFrameRect: [win frame]])); -} +namespace Upp { +bool GetShift() { return coco_flags & NSEventModifierFlagShift; } +bool GetCtrl() { return coco_flags & NSEventModifierFlagCommand; } +bool GetAlt() { return coco_flags & NSEventModifierFlagControl; } +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); + } +}; + +}; @implementation CocoView -(void)drawRect:(NSRect)rect { Upp::SystemDraw w([[NSGraphicsContext currentContext] CGContext], [self bounds].size.height); - DLOG("CocoDraw"); - ctrl->Paint(w); // TODO!!! + Upp::MMImp::Paint(ctrl, w, MakeRect(rect)); } -- (void)mouseDown:(NSEvent *)e { DoMouseEvent(self, e, Upp::Ctrl::LEFTDOWN); } -- (void)mouseUp:(NSEvent *)e { DoMouseEvent(self, e, Upp::Ctrl::LEFTUP); } -- (void)mouseMoved:(NSEvent *)e { DoMouseEvent(self, e, Upp::Ctrl::MOUSEMOVE); } -- (void)mouseDragged:(NSEvent *)e { DoMouseEvent(self, e, Upp::Ctrl::MOUSEMOVE); } // TODO? -- (void)rightMouseDown:(NSEvent*)e { DoMouseEvent(self, e, Upp::Ctrl::RIGHTDOWN); } -- (void)rightMouseUp:(NSEvent*)e { DoMouseEvent(self, e, Upp::Ctrl::RIGHTUP); } +- (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 { // ctrl->Text("keyDown flag: " + AsString(e.modifierFlags) + ", characters: " + @@ -41,8 +77,8 @@ void SyncRect(CocoView *view) // ctrl->Text("keyUp "); } -- (void)windowDidResize:(NSNotification *)notification { SyncRect(self); } -- (void)windowDidMove:(NSNotification *)notification { SyncRect(self); } +- (void)windowDidResize:(NSNotification *)notification { Upp::MMCtrl::SyncRect(self); } +- (void)windowDidMove:(NSNotification *)notification { Upp::MMCtrl::SyncRect(self); } //TODO: more layout changes - (void)windowDidBecomeKey:(NSNotification *)notification diff --git a/uppsrc/CtrlCore/CocoWnd.cpp b/uppsrc/CtrlCore/CocoWnd.cpp index 3b4e6c8fc..a540f8581 100644 --- a/uppsrc/CtrlCore/CocoWnd.cpp +++ b/uppsrc/CtrlCore/CocoWnd.cpp @@ -182,11 +182,6 @@ void Ctrl::WndUpdate() GuiLock __; } -bool Ctrl::IsWndOpen() const { - GuiLock __; - return false; -} - void Ctrl::SetAlpha(byte alpha) { GuiLock __;