CtrlCore: Developing Cocoa

git-svn-id: svn://ultimatepp.org/upp/trunk@12335 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2018-10-06 12:15:50 +00:00
parent fd9c0682c7
commit e4ea123c99
5 changed files with 68 additions and 18 deletions

View file

@ -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);

View file

@ -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<NSImageSysData, int64>::Maker {
Image img;
virtual int64 Key() const { return img.GetSerialId(); }
virtual int Make(NSImageSysData& object) const { object.Init(img); return img.GetLength(); }
};
static LRUCache<NSImageSysData, int64> 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];
}

View file

@ -41,6 +41,8 @@ struct AutoreleasePool {
CGImageRef createCGImage(const Image& img);
NSImage *GetNSImage(const Image& img);
WString ToWString(CFStringRef s);
String ToString(CFStringRef s);

View file

@ -13,6 +13,9 @@ static int coco_flags;
static Upp::Ptr<Upp::Ctrl> coco_capture;
namespace Upp {
extern id menubar;
bool GetShift() { return coco_flags & NSEventModifierFlagShift; }
bool GetCtrl() { return coco_flags & NSEventModifierFlagCommand; }

View file

@ -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