Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .github/CODE_OF_CONDUCT.md

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Copyright (C) 2023 ColleagueRiley ColleagueRiley@gmail.com
2022-2023 EimaMei/Sacode
Copyright (C) 2023 EimaMei
2023 ColleagueRiley ColleagueRiley@gmail.com

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
Expand Down
1 change: 0 additions & 1 deletion TODO

This file was deleted.

33 changes: 17 additions & 16 deletions examples/controls/button.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int button1Clicked;
int button2Clicked;

// Buffer for our text.
static char buffer[30];
char buffer[30];

NSApplication* NSApp;

Expand All @@ -23,25 +23,27 @@ bool windowShouldClose(id sender) {
}

void OnButton1Click(id sender) {
sprintf(buffer, "button1 clicked %d times", ++button1Clicked);
button1Clicked += 1;
sprintf(buffer, "button1 clicked %d times", button1Clicked);
NSTextField_setStringValue(label1, buffer);
}

void OnButton2Click(id sender) {
sprintf(buffer, "button2 clicked %d times", ++button2Clicked);
button2Clicked += 1;
sprintf(buffer, "button2 clicked %d times", button2Clicked);
NSTextField_setStringValue(label2, buffer);
}

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, OnButton1Click);
si_func_to_SEL(SI_DEFAULT, OnButton2Click);
si_func_to_SEL("NSObject", windowShouldClose);
si_func_to_SEL("NSObject", OnButton1Click);
si_func_to_SEL("NSObject", OnButton2Click);

NSApp = NSApplication_sharedApplication();
NSApplication_setActivationPolicy(NSApp, NSApplicationActivationPolicyRegular);

NSWindow* window = NSWindow_init(NSMakeRect(100, 100, 300, 300), NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable, NSBackingStoreBuffered, false);
NSWindow* window = NSWindow_init(NSAlloc(NSClass(NSWindow)), NSMakeRect(100, 100, 300, 300), NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable, NSBackingStoreBuffered, false);

button1Clicked = 0;
button2Clicked = 0;
Expand All @@ -50,14 +52,14 @@ int main(int argc, char* argv[]) {
button1 = NSButton_initWithFrame(NSMakeRect(50, 225, 90, 25)); // [[[NSButton alloc] initWithFrame:NSMakeRect(50, 225, 90, 25)] autorelease];
NSButton_setTitle(button1, "button1");
NSButton_setBezelStyle(button1, NSBezelStyleRounded);
NSButton_setTarget(button1, (id)window);
NSButton_setTarget(button1, window);
NSButton_setAction(button1, selector(OnButton1Click));
NSButton_setAutoresizingMask(button1, NSViewMaxXMargin | NSViewMinYMargin);

button2 = NSButton_initWithFrame(NSMakeRect(50, 125, 200, 75));
NSButton_setTitle(button2, "button2");
NSButton_setBezelStyle(button2, NSBezelStyleRegularSquare);
NSButton_setTarget(button2, (id)window);
NSButton_setTarget(button2, window);
NSButton_setAction(button2, selector(OnButton2Click));
NSButton_setAutoresizingMask(button2, NSViewMaxXMargin | NSViewMinYMargin);

Expand All @@ -76,14 +78,13 @@ int main(int argc, char* argv[]) {

NSWindow_setTitle(window, "Button example");
NSView* view = NSWindow_contentView(window);
NSView_addSubview(view, (NSView*)button1);
NSView_addSubview(view, (NSView*)button2);
NSView_addSubview(view, (NSView*)label1);
NSView_addSubview(view, (NSView*)label2);
NSWindow_setIsVisible(window, true);
NSWindow_makeMainWindow(window);
NSView_addSubview(view, button1);
NSView_addSubview(view, button2);
NSView_addSubview(view, label1);
NSView_addSubview(view, label2);
NSWindow_makeKeyAndOrderFront(window, nil);

NSApplication_run(NSApp);

return 0;
}
}
24 changes: 11 additions & 13 deletions examples/controls/checkbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void OnCheckBox5Click(id sender) {


NSButton* create_checkbox(checkBox checkbox) {
NSButton* result = NSAutoRelease(NSButton_initWithFrame(checkbox.rect));
NSButton* result = NSAutorelease(NSButton_initWithFrame(checkbox.rect));
NSButton_setTitle(result, checkbox.title);
NSButton_setAllowsMixedState(result, checkbox.allowMixedState);
NSButton_setButtonType(result, checkbox.type);
Expand All @@ -85,14 +85,14 @@ int main(int argc, char* argv[]) {
NSApplication_setActivationPolicy(NSApp, NSApplicationActivationPolicyRegular);

// 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, OnCheckBox1Click);
si_func_to_SEL(SI_DEFAULT, OnCheckBox2Click);
si_func_to_SEL(SI_DEFAULT, OnCheckBox3Click);
si_func_to_SEL(SI_DEFAULT, OnCheckBox4Click);
si_func_to_SEL(SI_DEFAULT, OnCheckBox5Click);

NSWindow* window = NSWindow_init(NSMakeRect(100, 100, 300, 300), NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable, NSBackingStoreBuffered, false);
si_func_to_SEL("NSObject", windowShouldClose);
si_func_to_SEL("NSObject", OnCheckBox1Click);
si_func_to_SEL("NSObject", OnCheckBox2Click);
si_func_to_SEL("NSObject", OnCheckBox3Click);
si_func_to_SEL("NSObject", OnCheckBox4Click);
si_func_to_SEL("NSObject", OnCheckBox5Click);

NSWindow* window = NSWindow_init(NSAlloc(NSClass(NSWindow)), NSMakeRect(100, 100, 300, 300), NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable, NSBackingStoreBuffered, false);
NSWindow_setTitle(window, "CheckBox example");

checkBox array_of_checkboxes[5] = {
Expand All @@ -108,11 +108,9 @@ int main(int argc, char* argv[]) {
for (size_t i = 0; i < arr_countof(array_of_checkboxes); i++) {
// We create the checkboxes, set them as the pointers to the selector functions, and also add them to the NSView as subviews to make them visible.
created_checkboxes[i] = create_checkbox(array_of_checkboxes[i]);
NSView_addSubview(view, (NSView*)created_checkboxes[i]);
NSView_addSubview(view, created_checkboxes[i]);
}

NSWindow_setIsVisible(window, true);
NSWindow_makeMainWindow(window);
NSWindow_makeKeyAndOrderFront(window, nil);

NSApplication_run(NSApp);
}
21 changes: 10 additions & 11 deletions examples/controls/combobox.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,45 @@ bool windowShouldClose(id sender) {

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);
si_func_to_SEL("NSObject", windowShouldClose);
si_func_to_SEL("NSObject", 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* window = NSWindow_init(NSAlloc(NSClass(NSWindow)), 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_setTarget(comboBox1, 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);
NSComboBox_setTarget(comboBox2, 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);
NSView_addSubview(view, comboBox1);
NSView_addSubview(view, comboBox2);

// Set the window visible and main.
NSWindow_setIsVisible(window, true);
NSWindow_makeMainWindow(window);
// Set the window as key and put it in the front.
NSWindow_makeKeyAndOrderFront(window, nil);

// Run it.
NSApplication_run(NSApp);

return 0;
}
}
86 changes: 38 additions & 48 deletions examples/controls/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <silicon.h>

#include <stdarg.h>
#define local_array_size(array) sizeof(array) / sizeof(*array) // For convenience sake.
#define arr_size(array) sizeof(array) / sizeof(*array) // For convenience sake.

// Define all of the functions that'll be activated when the menu item gets clicked.
void fileNew() { printf("MainMenu/File/New\n"); }
Expand All @@ -21,73 +21,62 @@ void editDelete() { printf("MainMenu/Edit/Delete\n"); }
void editSelectAll() { printf("MainMenu/Edit/SelectAll\n"); }

NSApplication* NSApp;
char* allocator;
size_t allocator_index = 0;

bool windowShouldClose(id sender) {
NSApplication_terminate(NSApp, sender);
return YES;
}

/*
This is just a quick 'n dirty way to achieve the same result as [NSString stringWithFormat:(...)]
Note that after each usage of 'sprintf_output' in this file, the returned string does not get freed, which in production code should obviously
*/
const char* sprintf_output(const char* text, ...) {
va_list args;

// Get the length of the args (important for the malloc size).
va_start(args, text);
int len = vsnprintf(0, 0, text, args);
va_end(args);
char* output = &allocator[allocator_index];

char* output = malloc(len + 1);

// Do sprintf.
va_start(args, text);
vsnprintf(output, len + 1, text, args);
va_end(args);
va_list va;
va_start(va, text);
size_t len = vsnprintf(output, 1024 - allocator_index, text, va);
va_end(va);

allocator_index += len;
return output;
}


// Creates a submenu for the main menu.
NSMenu* create_submenu(NSMenu* main_menu, const char* title, NSMenuItem** items, size_t sizeof_items) {
// First, create a submenu for the app's menu bar.
NSMenuItem* menuItem = NSAutoRelease(NSAlloc(SI_NS_CLASSES[NS_MENUITEM_CODE]));
NSMenuItem* menu_item = NSAutorelease(NSAlloc(NSClass(NSMenuItem)));

// Add the NSMenuItem to the main menu
NSMenu_addItem(main_menu, menuItem);
NSMenu_addItem(main_menu, menu_item);

// Create a menu list for our submenu.
NSMenu* new_menu = NSMenu_init(title);
NSMenuItem_setSubmenu(menuItem, new_menu);
NSMenuItem_setSubmenu(menu_item, new_menu);

// Add the items to the new menu list.
for (size_t i = 0; i < sizeof_items; i++)
NSMenu_addItem(new_menu, items[i]);

return new_menu;
}

int main() {
/*
C's function pointers and Objective-C's class methods (SELs) are different. While they are 'pointers' at their core, SELs are class functions and as such, sending a regular function pointer will not work.
The way to fix this is to use the 'si_func_to_SEL' function, which takes a function and adds it as a method to the provided class (default being SI_DEFAULT). With this you can now do selector(<function name>),
which finds the SEL pointer to the provided function and use it as a valid argument.
*/
si_func_to_SEL(SI_DEFAULT, windowShouldClose);

si_func_to_SEL(SI_DEFAULT, fileNew);
si_func_to_SEL(SI_DEFAULT, fileOpen);
si_func_to_SEL(SI_DEFAULT, fileClose);

si_func_to_SEL(SI_DEFAULT, editUndo);
si_func_to_SEL(SI_DEFAULT, editRedo);
si_func_to_SEL(SI_DEFAULT, editCut);
si_func_to_SEL(SI_DEFAULT, editCopy);
si_func_to_SEL(SI_DEFAULT, editPaste);
si_func_to_SEL(SI_DEFAULT, editDelete);
si_func_to_SEL(SI_DEFAULT, editSelectAll);
allocator = malloc(1024);

si_func_to_SEL("NSObject", windowShouldClose);

si_func_to_SEL("NSObject", fileNew);
si_func_to_SEL("NSObject", fileOpen);
si_func_to_SEL("NSObject", fileClose);

si_func_to_SEL("NSObject", editUndo);
si_func_to_SEL("NSObject", editRedo);
si_func_to_SEL("NSObject", editCut);
si_func_to_SEL("NSObject", editCopy);
si_func_to_SEL("NSObject", editPaste);
si_func_to_SEL("NSObject", editDelete);
si_func_to_SEL("NSObject", editSelectAll);

NSApp = NSApplication_sharedApplication();
// Ever since MacOS 10.6, Mac applications require a 'NSApplicationActivationPolicyRegular' type policy to show menubars.
Expand All @@ -97,7 +86,7 @@ int main() {
const char* process_name = NSProcessInfo_processName(NSProcessInfo_processInfo());

// Create and set the main menubar
NSMenu* main_menu = NSAutoRelease(NSAlloc(SI_NS_CLASSES[NS_MENU_CODE]));
NSMenu* main_menu = NSAutorelease(NSAlloc(SI_NS_CLASSES[NS_MENU_CODE]));
NSApplication_setMainMenu(NSApp, main_menu);

// The items for each of our menus ('<Executable name>', 'File', 'Edit', 'View', 'Windows' and 'Help')
Expand Down Expand Up @@ -140,18 +129,18 @@ int main() {

// Now we create the menus themselves.
// '<Process name>' menu
create_submenu(main_menu, process_name, process_items, local_array_size(process_items));
NSMenu* process_services = NSAutoRelease(NSAlloc(SI_NS_CLASSES[NS_MENU_CODE])); // We initialize a new menu.
create_submenu(main_menu, process_name, process_items, arr_size(process_items));
NSMenu* process_services = NSAutorelease(NSAlloc(NSClass(NSMenu))); // We initialize a new menu.

NSMenuItem_setSubmenu(process_items[2], process_services); // 'process_items[2]' is 'Services' (refer to 'process_items' variable).

NSApplication_setServicesMenu(NSApp, process_services); // Now 'Services' becomes a Services menu.

// 'File' menu
create_submenu(main_menu, "File", file_items, local_array_size(file_items));
create_submenu(main_menu, "File", file_items, arr_size(file_items));

// 'File' menu
create_submenu(main_menu, "Edit", edit_items, local_array_size(edit_items));
create_submenu(main_menu, "Edit", edit_items, arr_size(edit_items));

// 'View' menu
create_submenu(main_menu, "View", nil, 0); // For whatever reason, MacOS turns a "View" titled menu automatically into a View menu.
Expand All @@ -165,13 +154,14 @@ int main() {
NSApplication_setHelpMenu(NSApp, help_menu); // Set our menu into a Help menu.

// Create our window.
NSWindow* win = NSWindow_init(NSMakeRect(100, 100, 300, 300), NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable, NSBackingStoreBuffered, false);
NSWindow* win = NSWindow_init(NSAlloc(NSClass(NSWindow)), NSMakeRect(100, 100, 300, 300), NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable, NSBackingStoreBuffered, false);
NSWindow_setTitle(win, "MainMenu example");
NSWindow_setIsVisible(win, true);
NSWindow_makeMainWindow(win);
NSWindow_makeKeyAndOrderFront(win, nil);

// Run it.
NSApplication_run(NSApp);

free(allocator);

return 0;
}
}
Loading