From a2db9d45791f57d4dffdb37867523ba52d5ba431 Mon Sep 17 00:00:00 2001 From: cxl Date: Mon, 21 Jan 2019 18:43:50 +0000 Subject: [PATCH] CtrlCore, CtrlLib: Cocoa FileSel icons git-svn-id: svn://ultimatepp.org/upp/trunk@12694 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/CtrlCore/Coco.h | 2 ++ uppsrc/CtrlCore/CocoImage.mm | 21 +++++++++++++++++ uppsrc/CtrlLib/ChCocoMM.mm | 2 +- uppsrc/CtrlLib/FileSel.cpp | 44 ++++++++++++++++++++++++++++++++++++ uppsrc/Draw/ImageOp.h | 7 +++--- 5 files changed, 72 insertions(+), 4 deletions(-) diff --git a/uppsrc/CtrlCore/Coco.h b/uppsrc/CtrlCore/Coco.h index c58dae1f5..12ad1d8dd 100644 --- a/uppsrc/CtrlCore/Coco.h +++ b/uppsrc/CtrlCore/Coco.h @@ -145,6 +145,8 @@ struct DrawDragRectInfo { void DrawDragRect(SystemDraw& w, const Rect& rect1, const Rect& rect2, const Rect& clip, int n, Color color, uint64 pattern); +Image GetIconForFileExt(const char *ext); + #ifndef PLATFORM_WIN32 #include #endif diff --git a/uppsrc/CtrlCore/CocoImage.mm b/uppsrc/CtrlCore/CocoImage.mm index 98847e9c2..fbe1aa1de 100644 --- a/uppsrc/CtrlCore/CocoImage.mm +++ b/uppsrc/CtrlCore/CocoImage.mm @@ -330,6 +330,27 @@ ImageDraw::operator Image() return Image(ib); } +Image GetIconForFileExt(const char *ext) +{ + ImageDraw iw(DPI(16, 16)); + + CGContextRef cg = (CGContextRef) iw.GetCGHandle(); + + CFRef fext = CFStringCreateWithCString(NULL, ext, kCFStringEncodingUTF8); + CFRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fext, NULL); + + NSImage *image = *ext == '*' ? [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericFolderIcon)] + : [[NSWorkspace sharedWorkspace]iconForFileType:(__bridge NSString *)~fileUTI]; + + NSGraphicsContext *gc = [NSGraphicsContext graphicsContextWithCGContext:cg flipped:YES]; + NSGraphicsContext* cgc = [NSGraphicsContext currentContext]; + [NSGraphicsContext setCurrentContext:gc]; + [image drawInRect:NSMakeRect(0, 0, DPI(16), DPI(16))]; + [NSGraphicsContext setCurrentContext:cgc]; + + return iw; +} + }; #endif diff --git a/uppsrc/CtrlLib/ChCocoMM.mm b/uppsrc/CtrlLib/ChCocoMM.mm index 4b009e549..1a7e3ea8e 100644 --- a/uppsrc/CtrlLib/ChCocoMM.mm +++ b/uppsrc/CtrlLib/ChCocoMM.mm @@ -9,7 +9,7 @@ void Coco_ThemePaint(void *cgcontext, const Upp::Rect& r, int type, int value, int state, bool focus) { - auto cg = (CGContextRef) cgcontext; + auto cg = (CGContextRef) cgcontext ; if(Upp::IsUHDMode()) CGContextScaleCTM(cg, 2, 2); CGRect cr = CGRectMake(r.left, r.top, r.Width(), r.Height()); diff --git a/uppsrc/CtrlLib/FileSel.cpp b/uppsrc/CtrlLib/FileSel.cpp index b6de02b68..1c0911b38 100644 --- a/uppsrc/CtrlLib/FileSel.cpp +++ b/uppsrc/CtrlLib/FileSel.cpp @@ -394,6 +394,42 @@ Image GetFileIcon(const String& folder, const String& filename, bool isdir, bool #endif +#ifdef PLATFORM_COCOA +struct FileIconMaker : ImageMaker { + String file; + bool exe; + bool dir; + bool large; + + virtual String Key() const { + return file + (exe ? "1" : "0") + (dir ? "1" : "0"); + } + + virtual Image Make() const { + String ext = GetFileExt(file); + if(*ext == '.') + ext = ext.Mid(1); + if(exe) + ext = "app"; + if(dir && ext != "app") + return GetIconForFileExt("*"); + return GetIconForFileExt(ext); + } +}; + +#define GETFILEICON_DEFINED + +Image GetFileIcon(const char *path, bool dir, bool exe, bool large, bool quick = false) +{ + FileIconMaker m; + m.exe = exe; + m.dir = dir; + m.file = path; + m.large = large; + return MakeImage(m); +} +#endif + #ifndef GETFILEICON_DEFINED Image PosixGetDriveImage(String dir, bool) { @@ -432,9 +468,13 @@ Image NativePathIcon0(const char *path, bool folder, bool large) String p = path; bool isdrive = folder && ((p == "/media") || (p == "/mnt")); FindFile ff(path); +#ifdef PLATFORM_COCOA + return GetFileIcon(path, folder, ff.GetMode() & 0111, large); +#else return isdrive ? PosixGetDriveImage(GetFileName(path), large) : GetFileIcon(path, GetFileName(path), folder, ff.GetMode() & 0111, large); #endif +#endif } Image NativePathIcon(const char *path, bool folder) @@ -508,9 +548,13 @@ bool Load(FileList& list, const String& dir, const char *patterns, bool dirs, MatchSearch(fi.filename, search) && show) { Image img; #ifdef PLATFORM_POSIX + #ifdef PLATFORM_COCOA + img = GetFileIcon(AppendFileName(dir, fi.filename), fi.is_directory, fi.unix_mode & 0111, false, lazyicons); + #else img = isdrive ? PosixGetDriveImage(fi.filename, false) : GetFileIcon(dir, fi.filename, fi.is_directory, fi.unix_mode & 0111, false); #endif + #endif #ifdef GUI_WIN img = GetFileIcon(AppendFileName(dir, fi.filename), fi.is_directory, false, false, lazyicons); #endif diff --git a/uppsrc/Draw/ImageOp.h b/uppsrc/Draw/ImageOp.h index f7f7e43be..fd4296fff 100644 --- a/uppsrc/Draw/ImageOp.h +++ b/uppsrc/Draw/ImageOp.h @@ -224,9 +224,10 @@ void SyncUHDMode(); Image DPI(const Image& m); Image DPI(const Image& img, int expected); -inline int DPI(int a) { return IsUHDMode() ? 2 * a : a; } -inline double DPI(double a) { return IsUHDMode() ? 2 * a : a; } -inline Size DPI(Size sz) { return IsUHDMode() ? 2 * sz : sz; } +inline int DPI(int a) { return IsUHDMode() ? 2 * a : a; } +inline double DPI(double a) { return IsUHDMode() ? 2 * a : a; } +inline Size DPI(Size sz) { return IsUHDMode() ? 2 * sz : sz; } +inline Size DPI(int cx, int cy) { return Size(DPI(cx), DPI(cy)); } inline Image DPI(const Image& a, const Image& b) { return IsUHDMode() ? b : a; }