Skip to content

Instantly share code, notes, and snippets.

@nariakiiwatani
Created November 11, 2014 14:07
Show Gist options
  • Save nariakiiwatani/ecffbac4bd21417784c3 to your computer and use it in GitHub Desktop.
Save nariakiiwatani/ecffbac4bd21417784c3 to your computer and use it in GitHub Desktop.
Combobox dialog for Cocoa
#include "Dialog.h"
#include <Cocoa/Cocoa.h>
std::string systemComboBoxSelector(const std::string &question, const std::vector<std::string> &list)
{
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:@"OK"];
// [alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:[NSString stringWithCString:question.c_str()
encoding:NSUTF8StringEncoding]];
// create combo box
NSComboBox* box = [[NSComboBox alloc] initWithFrame:NSRectFromCGRect(CGRectMake(0,0,300,25))];
for(auto name : list) {
[box addItemWithObjectValue:[NSString stringWithCString:name.c_str() encoding:NSUTF8StringEncoding]];
}
if(!list.empty()) {
[box selectItemAtIndex:0];
}
[alert setAccessoryView:box];
NSInteger returnCode = [alert runModal];
std::string text = "";
if ( returnCode == NSAlertFirstButtonReturn )
text = [[box stringValue] UTF8String];
return text;
}
#pragma once
#include <string>
#include <vector>
std::string systemComboBoxSelector(const std::string &question, const std::vector<std::string> &list);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment