-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcombobox.c
70 lines (55 loc) · 2.39 KB
/
combobox.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Based on: https://github.com/gammasoft71/Examples_Cocoa/blob/master/src/CommonControls/ComboBox/README.md
*/
#define GL_SILENCE_DEPRECATION
#define SILICON_IMPLEMENTATION
#include <silicon.h>
// The main variables for our example.
NSComboBox* comboBox1;
NSComboBox* comboBox2;
// Call this function each time the comboxBox's text field gets changed.
void OnComboBox1SelectedItemChange(id sender) {
NSComboBox_selectItem(comboBox2, NSComboBox_indexOfSelectedItem(comboBox1));
}
NSApplication* NSApp;
// Standard close function.
bool windowShouldClose(id sender) {
NSApplication_terminate(NSApp, sender);
return true;
}
int main(int argc, char* argv[]) {
// Convert C functions to Objective-C methods (refer to the 'si_func_to_SEL' comment from 'examples/menu.c' for more).
si_func_to_SEL(SI_DEFAULT, windowShouldClose);
si_func_to_SEL(SI_DEFAULT, OnComboBox1SelectedItemChange);
NSApp = NSApplication_sharedApplication();
NSApplication_setActivationPolicy(NSApp, NSApplicationActivationPolicyRegular);
// Init the window beforehand as we'll have to reference for later.
NSWindow* window = NSWindow_init(NSMakeRect(100, 100, 300, 300), NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable, NSBackingStoreBuffered, false);
NSWindow_setTitle(window, "ComboBox Example");
// Init our comboBoxes.
comboBox1 = NSComboBox_initWithFrame(NSMakeRect(10, 260, 121, 26));
NSComboBox_addItem(comboBox1, "item1");
NSComboBox_addItem(comboBox1, "item2");
NSComboBox_addItem(comboBox1, "item3");
NSComboBox_setTarget(comboBox1, (id)window);
NSComboBox_setAction(comboBox1, selector(OnComboBox1SelectedItemChange));
comboBox2 = NSComboBox_initWithFrame(NSMakeRect(10, 220, 121, 26));
NSComboBox_setEditable(comboBox2, false);
NSComboBox_addItem(comboBox2, "item1");
NSComboBox_addItem(comboBox2, "item2");
NSComboBox_addItem(comboBox2, "item3");
NSComboBox_setTarget(comboBox2, (id)window);
// Select which item index to show for each comboBox.
NSComboBox_selectItem(comboBox1, 1);
NSComboBox_selectItem(comboBox2, 1);
// Add the comboBoxes to the view.
NSView* view = NSWindow_contentView(window);
NSView_addSubview(view, (NSView*)comboBox1);
NSView_addSubview(view, (NSView*)comboBox2);
// Set the window visible and main.
NSWindow_setIsVisible(window, true);
NSWindow_makeMainWindow(window);
// Run it.
NSApplication_run(NSApp);
return 0;
}