From e4ea123c998f88d86619e0133fe132e7fd240aa2 Mon Sep 17 00:00:00 2001 From: cxl Date: Sat, 6 Oct 2018 12:15:50 +0000 Subject: [PATCH] CtrlCore: Developing Cocoa git-svn-id: svn://ultimatepp.org/upp/trunk@12335 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/CtrlCore/CocoApp.mm | 7 ++-- uppsrc/CtrlCore/CocoImage.mm | 70 +++++++++++++++++++++++++++--------- uppsrc/CtrlCore/CocoMM.h | 2 ++ uppsrc/CtrlCore/CocoProc.mm | 3 ++ uppsrc/CtrlCore/cocotodo.txt | 4 +++ 5 files changed, 68 insertions(+), 18 deletions(-) diff --git a/uppsrc/CtrlCore/CocoApp.mm b/uppsrc/CtrlCore/CocoApp.mm index c97d6ad56..e8d39f3c5 100644 --- a/uppsrc/CtrlCore/CocoApp.mm +++ b/uppsrc/CtrlCore/CocoApp.mm @@ -25,6 +25,8 @@ void SyncPopupFocus(NSWindow *win) extern const char *sClipFmtsRTF; +id menubar; + void CocoInit(int argc, const char **argv, const char **envptr) { Ctrl::GlobalBackBuffer(); @@ -33,10 +35,9 @@ void CocoInit(int argc, const char **argv, const char **envptr) [NSApplication sharedApplication]; [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; - id menubar = [[NSMenu new] autorelease]; + menubar = [[NSMenu new] autorelease]; id appMenuItem = [[NSMenuItem new] autorelease]; [menubar addItem:appMenuItem]; - [NSApp setMainMenu:menubar]; id appMenu = [[NSMenu new] autorelease]; id appName = [[NSProcessInfo processInfo] processName]; id quitTitle = [@"Quit " stringByAppendingString:appName]; @@ -45,6 +46,8 @@ void CocoInit(int argc, const char **argv, const char **envptr) keyEquivalent:@"q"] autorelease]; [appMenu addItem:quitMenuItem]; [appMenuItem setSubmenu:appMenu]; + + [NSApp setMainMenu:menubar]; NSFont *sysfont = [NSFont systemFontOfSize:0]; Font::SetFace(0, ToString((CFStringRef)[sysfont familyName]), Font::TTF); diff --git a/uppsrc/CtrlCore/CocoImage.mm b/uppsrc/CtrlCore/CocoImage.mm index d91269b62..ac13d977a 100644 --- a/uppsrc/CtrlCore/CocoImage.mm +++ b/uppsrc/CtrlCore/CocoImage.mm @@ -167,6 +167,57 @@ NSCursor *GetNSCursor(int kind) #pragma clang diagnostic pop +NSImage *CreateNSImage(const Image& img, CGImageRef cgimg) +{ + Size isz = img.GetSize(); + double scale = 1.0 / DPI(1); + NSSize size; + size.width = scale * isz.cx; + size.height = scale * isz.cy; + NSImage *nsimage = [[NSImage alloc] initWithCGImage:cgimg size:size]; + cg_image_cache.Shrink(4 * 1024 * 768, 1000); // Cache must be after Paint because of PaintOnly! + return nsimage; +} + +struct NSImageSysData { + Image img; + NSImage *nsimage = NULL; + + void Init(const Image& img); + ~NSImageSysData(); +}; + +void NSImageSysData::Init(const Image& img) +{ + ImageSysDataMaker m; + m.img = img; + nsimage = CreateNSImage(img, cg_image_cache.Get(m).cgimg); +} + +NSImageSysData::~NSImageSysData() +{ + if(nsimage) + [nsimage release]; +} + +struct NSImageSysDataMaker : LRUCache::Maker { + Image img; + + virtual int64 Key() const { return img.GetSerialId(); } + virtual int Make(NSImageSysData& object) const { object.Init(img); return img.GetLength(); } +}; + +static LRUCache nsimage_cache; + +NSImage *GetNSImage(const Image& img) +{ + NSImageSysDataMaker m; + m.img = img; + NSImage *nsimage = nsimage_cache.Get(m).nsimage; + nsimage_cache.Shrink(4 * 1024*768, 1000); + return nsimage; +} + void Ctrl::SetNSAppImage(const Image& img) { ImageSysDataMaker m; @@ -179,16 +230,7 @@ void Ctrl::SetNSAppImage(const Image& img) cgimg = sd.cgimg; if(nsimg) [nsimg release]; - Point p = img.GetHotSpot(); - Size isz = img.GetSize(); - double scale = 1.0 / DPI(1); - NSSize size; - size.width = scale * isz.cx; - size.height = scale * isz.cy; - NSPoint hot; - hot.x = scale * p.x; - hot.y = scale * p.y; - nsimg = [[NSImage alloc] initWithCGImage:cgimg size:size]; + nsimg = CreateNSImage(img, cgimg); } [NSApp setApplicationIconImage:nsimg]; } @@ -212,16 +254,12 @@ void Ctrl::SetMouseCursor(const Image& img) cgimg = sd.cgimg; if(cursor) [cursor release]; - Point p = img.GetHotSpot(); - Size isz = img.GetSize(); + NSImage *nsimg = CreateNSImage(img, cgimg); double scale = 1.0 / DPI(1); - NSSize size; - size.width = scale * isz.cx; - size.height = scale * isz.cy; + Point p = img.GetHotSpot(); NSPoint hot; hot.x = scale * p.x; hot.y = scale * p.y; - NSImage *nsimg = [[NSImage alloc] initWithCGImage:cgimg size:size]; cursor = [[NSCursor alloc] initWithImage:nsimg hotSpot:hot]; [nsimg release]; } diff --git a/uppsrc/CtrlCore/CocoMM.h b/uppsrc/CtrlCore/CocoMM.h index 7cde2d6a8..0ed494876 100644 --- a/uppsrc/CtrlCore/CocoMM.h +++ b/uppsrc/CtrlCore/CocoMM.h @@ -41,6 +41,8 @@ struct AutoreleasePool { CGImageRef createCGImage(const Image& img); +NSImage *GetNSImage(const Image& img); + WString ToWString(CFStringRef s); String ToString(CFStringRef s); diff --git a/uppsrc/CtrlCore/CocoProc.mm b/uppsrc/CtrlCore/CocoProc.mm index c94223b3a..9b6292e8e 100644 --- a/uppsrc/CtrlCore/CocoProc.mm +++ b/uppsrc/CtrlCore/CocoProc.mm @@ -13,6 +13,9 @@ static int coco_flags; static Upp::Ptr coco_capture; namespace Upp { + +extern id menubar; + bool GetShift() { return coco_flags & NSEventModifierFlagShift; } bool GetCtrl() { return coco_flags & NSEventModifierFlagCommand; } diff --git a/uppsrc/CtrlCore/cocotodo.txt b/uppsrc/CtrlCore/cocotodo.txt index 36695328e..7daa5fdd0 100644 --- a/uppsrc/CtrlCore/cocotodo.txt +++ b/uppsrc/CtrlCore/cocotodo.txt @@ -1,5 +1,9 @@ sooner: +- drag&drop in theide does not work + +- ctrl tab does not work + - modal disabled window can be closed - Uword cursor is blinking even in background