reapack

Package manager for REAPER
Log | Files | Refs | Submodules | README | LICENSE

richedit.mm (2479B)


      1 /* ReaPack: Package manager for REAPER
      2  * Copyright (C) 2015-2025  Christian Fillion
      3  *
      4  * This program is free software: you can redistribute it and/or modify
      5  * it under the terms of the GNU Lesser General Public License as published by
      6  * the Free Software Foundation, either version 3 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  * GNU Lesser General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Lesser General Public License
     15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16  */
     17 
     18 #include "richedit.hpp"
     19 
     20 #include <AppKit/NSAttributedString.h>
     21 #include <AppKit/NSColor.h>
     22 #include <AppKit/NSTextView.h>
     23 
     24 void RichEdit::Init()
     25 {
     26 }
     27 
     28 RichEdit::RichEdit(HWND handle)
     29   : Control(handle)
     30 {
     31 }
     32 
     33 RichEdit::~RichEdit() = default;
     34 
     35 void RichEdit::onNotify(LPNMHDR, LPARAM)
     36 {
     37 }
     38 
     39 void RichEdit::setPlainText(const std::string &text)
     40 {
     41   NSString *str = [NSString
     42     stringWithCString: text.c_str()
     43     encoding: NSUTF8StringEncoding
     44   ];
     45 
     46   NSDictionary *darkMode = [NSDictionary
     47     dictionaryWithObject: NSColor.textColor
     48                   forKey: NSForegroundColorAttributeName];
     49 
     50   NSAttributedString *attrStr = [[NSAttributedString alloc]
     51    initWithString: str attributes: darkMode];
     52 
     53   auto textView = static_cast<NSTextView *>(handle());
     54   [[textView textStorage] setAttributedString: attrStr];
     55   [attrStr release];
     56 }
     57 
     58 bool RichEdit::setRichText(const std::string &rtf)
     59 {
     60   NSString *str = [NSString
     61     stringWithCString: rtf.c_str()
     62     encoding: NSUTF8StringEncoding
     63   ];
     64 
     65   NSAttributedString *attrStr = [[NSAttributedString alloc]
     66     initWithRTF: [str dataUsingEncoding: NSUTF8StringEncoding]
     67     documentAttributes: nullptr];
     68 
     69   if(!attrStr)
     70     return false;
     71 
     72   auto textView = static_cast<NSTextView *>(handle());
     73   [[textView textStorage] setAttributedString: attrStr];
     74   [textView setTextColor: NSColor.textColor]; // dark mode support
     75   [attrStr release];
     76 
     77   // auto-detect links, equivalent to Windows' EM_AUTOURLDETECT message
     78   const BOOL isEditable = textView.isEditable;
     79   [textView setEditable: YES];
     80   [textView setEnabledTextCheckingTypes: NSTextCheckingTypeLink];
     81   [textView checkTextInDocument: nil];
     82   [textView setEditable: isEditable];
     83 
     84   return [[textView string] length];
     85 }