2

Ok. here is the situation, I am an average c++ developer. I do not have any prior experience of working with Objective C or Cocoa for that matter.

I am working on a project currently in OSX with Carbon [I am NOVICE in carbon as well], and been coding in pure C++ for 3 months.

Now I am facing an issue where I have to display an input box to the user, and get some input from him, say USERNAME, having literally no knowledge of how input boxes are displayed in OSX, what are my options. I have done programming in win32, so, a couple of hours of reading and 1 hour of programming should do the job for me. I just need a little help pointing me in right direction.

Here is what I have got from Googling a little bit--

There are 3 ways which can be used to implement an input box in OSX

1- Use carbon, I have been able to display a simple dialog box using it. I don't know how I can use input boxes there.. Here is the code I tried for Input Box..

   DialogRef    dialog = GetNewDialog (128, NULL, (WindowRef)-1);
   WindowRef lay;
   ControlRef   outControl;

   Rect boundsRect;

   boundsRect.top = 0;
   boundsRect.left = 0;
   boundsRect.right = 200;
   boundsRect.bottom = 99;


   lay = GetDialogWindow(dialog);

   CreateEditTextControl (lay, &boundsRect, NULL, false, true, NULL, &outControl);

   InstallStandardEventHandler(GetWindowEventTarget (lay));

   ShowWindow (lay);

I was not able to see anything when I ran the program, and Xcode showed a warning on CreateEditTextControl saying it is deprecated.

Option 2 which I have is to combine Objective C and C++, but I do not know how objective C works, here is a little lead I got in doing this. I only have a few hours to accomplish this.

Option 3 I found this here.

//
// test1.cpp
// This program shows how to access Cocoa GUI from pure C/C++
// and build a truly functional GUI application (although very simple).

// Compile using:
//   g++ -framework Cocoa -o test1 test1.cpp
//
// that will output 'test1' binary.
//


#include <CoreFoundation/CoreFoundation.h>
#include <objc/objc.h>
#include <objc/objc-runtime.h>
#include <iostream>

extern "C" int NSRunAlertPanel(CFStringRef strTitle, CFStringRef strMsg,
                           CFStringRef strButton1, CFStringRef strButton2, 
                           CFStringRef strButton3, ...);


int main(int argc, char** argv)
{
id app = NULL;
id pool = objc_getClass("NSAutoreleasePool");
if (!pool)
{
    std::cerr << "Unable to get NSAutoreleasePool!\nAborting\n";
    return -1;
}
pool = objc_msgSend(pool, sel_registerName("alloc"));
if (!pool)
{
    std::cerr << "Unable to create NSAutoreleasePool...\nAborting...\n";
    return -1;
}
pool = objc_msgSend(pool, sel_registerName("init"));

app = objc_msgSend(objc_getClass("NSApplication"),
                   sel_registerName("sharedApplication"));

NSRunAlertPanel(CFSTR("Testing"),
                CFSTR("This is a simple test to display NSAlertPanel."),
                CFSTR("OK"), NULL, NULL);

objc_msgSend(pool, sel_registerName("release"));
return 0;
}
Community
  • 1
  • 1
2am
  • 378
  • 1
  • 6
  • 18

2 Answers2

3

All of Carbon UI is deprecated, and cannot be used for 64-bit development.

A 3rd alternative would be CFUserNotification.

JWWalker
  • 22,385
  • 6
  • 55
  • 76
  • I am not going for Carbon either. I found another alternative, using NSRunAlertPanel from Cocoa, I am able to display a dialog with buttons using the same, I am checking out how I can combine it with input boxes now. – 2am Dec 05 '13 at 07:33
  • I will look up CFUserNotification as well, that seems interesting too ;) Cheers. – 2am Dec 05 '13 at 07:35
3

Using option2 : Cocoa framework provides you all sets of simple controls, messagbox and others. But there are no inputBox. :(

However you can create your custom inputBox or modify the alertpanel as I did here:

- (NSString *)inputBox: (NSString *)prompt{
    NSAlert *alert = [NSAlert alertWithMessageText: prompt
                                     defaultButton:@"OK"
                                   alternateButton:@"Cancel"
                                       otherButton:nil
                         informativeTextWithFormat:@""];

    NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
    [alert setAccessoryView:input];
    NSInteger button = [alert runModal];
    if (button == NSAlertDefaultReturn) {
        [input validateEditing];
        return [input stringValue];
    }
    else if (button == NSAlertAlternateReturn) {
        return nil;
    }
    else {
        return nil;
    }
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • How can this be converted into C++? – 2am Dec 05 '13 at 08:12
  • i dont think you can convert it into c++. But you can use obj-c and c++ files in your project. – Anoop Vaidya Dec 05 '13 at 08:15
  • Have a look at the changes I made to the question :) I was able to display at least a dialog box using that, Now all I wanna do is take input from the user – 2am Dec 05 '13 at 08:21
  • Samething you used `NSAlertPanel` now you need to customize as I show in answer. But I think you need to add cocoa-framework to your project. – Anoop Vaidya Dec 05 '13 at 08:23
  • Yes, I added Cocoa framework in build phrased in Xcode. I am trying to customize the code mixing it with your answer. I am not quite sure how can i use NSTextField in C++, along with initWithFrame and NSMakeRect, as I said my knowledge with Obj C is very little, could you point me to the right resources regarding that? Thank you & Regards, Ashutosh. – 2am Dec 05 '13 at 08:26
  • my knowledge is on obj-c not in C++ :( – Anoop Vaidya Dec 05 '13 at 08:35