4

I have an objc program and i would like to use a widget that is written in objc++ (namely https://launchpad.net/scintilla-cocoa). How do i go about this? Basically i want a new window controller object to interface with this objc++ library to define a scintilla text editor widget. Simply creating a new 'objc class' and accessing the library from there generates a bunch of errors related to the C++ class keyword and so on.

Thanks in advance

Mat
  • 202,337
  • 40
  • 393
  • 406
horseyguy
  • 29,455
  • 20
  • 103
  • 145

2 Answers2

3

Since I'm the one who put you into the (hopefully rewarding :-)) trouble of using Scintilla, here I am.


Let's say we create a ScintillaView subclass, named ppScintillaEditor. The file should have an .mm extension (e.g. ppScintillaEditor.mm)

The code would be roughly like this...

Interface

#import "Scintilla/ScintillaView.h"

@interface ppScintillaEditor : ScintillaView
{
    // your iVars
}

// your properties / methods / whatever

Now, as for the implementation part, remember to put some initialization method to set up the view properly (as in the example accompanying Scintilla-cocoa; I mean the Test project)


Sidenote : Of course, you can create subclasses, categories or whatever on top the ScintillaView class, pretty much based on what you need - I, for example, have create a separate Category just in order to group there some ScintillaView specific commands (sooner or later, you'll notice that for some more advanced Scintilla manipulations, although it's there, it may need some polishing to be a bit more cocoa-friendly, so here you go...)


Now, last but not least...

To resolve the "bunch of errors related to the C++ class keyword and so on", as I've shown in my other video-response to your comment, all you have to do is :

  • Go to your project's Build Settings
  • Under Apple LLVM Compiler 3.0 - Preprocessing
  • Option Preprocessor Macros
  • Add to both Debug and Release :

    SCI_NAMESPACE SCI_LEXER
    

And that's it. :-)


Hint : The above are defined by Scintilla to avoid clashes between C and non-C elements, like above... so, all it takes is to notify the preprocessor and the rest is taken care of....

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
1

you would create an objc class which has the interface your app needs, then implement and add the ivars and implement -- all behind a compilation firewall so the objc++ sources are not included in the header. your implementation would provide any necessary conversions.

it is like you have already done, but you remove the scintilla headers from the header for your wrapper -- they are visible only to your wrapper's implementation.

Update

To illustrate one possible approach:

MONScintillaWrapper.h

// no c++/scintilla sources should be included in this header
#import <Foundation/Foundation.h>

@interface MONScintillaWrapper : NSObject

- (void)setBackgroundColor:(NSColor *)pColor;    

@end

MONScintillaWrapper.mm

#import "MONScintillaWrapper.h"

@implementation MONScintillaWrapper
{
  scintilla::t_thing scintillaThing;
}

- (void)setBackgroundColor:(NSColor *)pColor
{
...convert pColor to a scintilla color and pass that to scintillaThing...
}

@end
justin
  • 104,054
  • 14
  • 179
  • 226
  • sorry, im still a bit green to follow this. My implementation is a `.mm` file or a `.m` file? currently it's just a `.m` file, i would have to rename it to `.mm` for this to work? – horseyguy Apr 22 '12 at 09:19
  • yes - `.mm` for this wrapper object. then declare your ivars (scintilla objects) here: `@implementation MONScintillaWrapper { scintilla::t_thingy scintillaThingy; }`. this way, the rest of your program does not need to see your scintilla ivars or the c++ sources. – justin Apr 22 '12 at 09:39
  • expanded answer to illustrate this approach. – justin Apr 22 '12 at 09:46
  • thanks, so i import the scintilla c++ headers in `MONScintillaWrapper.mm` ? – horseyguy Apr 22 '12 at 10:08
  • @anonymous_downvoter that's not very helpful unless you explain *why* – justin Apr 23 '12 at 06:47
  • 1
    @Justin The -1 was by me. But with NO malicious intentions whatsoever. And the reason is pretty simple : I had had some **great** trouble in the past dealing with the exact same issue mentioned by the OP (`errors related to the C++ class keyword` when integrating the Scintilla component), and your answer - although generally correct - definitely does **not** solve it. *(As I mention in my answer, what it takes - at least, the best and most universal take on the subject, AFAIK - is a Scintilla-specific workaround)*. – Dr.Kameleon Apr 23 '12 at 09:05
  • 1
    @Dr.Kameleon explanation appreciated +1 – justin Apr 23 '12 at 09:36