From 0d67f969dbf2f1330b4b1509a83542501be021fc Mon Sep 17 00:00:00 2001 From: Armando Rivera Date: Sun, 18 Jul 2021 03:10:52 -0400 Subject: [PATCH] Initial Commit --- .gitignore | 4 +- Cocoa/NSButton.nim | 5 + Cocoa/NSCheckbox.nim | 5 + Cocoa/NSColordialog.nim | 0 Cocoa/NSCombobox.nim | 5 + Cocoa/NSDialog.nim | 5 + Cocoa/NSFunctions.nim | 56 ++++ Cocoa/NSLabel.nim | 5 + Cocoa/NSLine.nim | 5 + Cocoa/NSListbox.nim | 5 + Cocoa/NSMenu.nim | 7 + Cocoa/NSMessagebox.nim | 5 + Cocoa/NSOpendialog.nim | 5 + Cocoa/NSSavedialog.nim | 5 + Cocoa/NSSlider.nim | 5 + Cocoa/NSTextedit.nim | 5 + Cocoa/NSTextfield.nim | 5 + Cocoa/NSWindow.nim | 8 + Cocoa/widgets/button.m | 33 +++ Cocoa/widgets/checkbox.m | 29 ++ Cocoa/widgets/col.h | 562 ++++++++++++++++++++++++++++++++++++ Cocoa/widgets/colordialog.m | 98 +++++++ Cocoa/widgets/combobox.m | 43 +++ Cocoa/widgets/dialog.m | 15 + Cocoa/widgets/functions.m | 154 ++++++++++ Cocoa/widgets/label.m | 14 + Cocoa/widgets/line.m | 24 ++ Cocoa/widgets/listbox.m | 85 ++++++ Cocoa/widgets/menu.m | 64 ++++ Cocoa/widgets/messagebox.m | 14 + Cocoa/widgets/nsslider.m | 30 ++ Cocoa/widgets/opendialog.m | 43 +++ Cocoa/widgets/saneview.m | 11 + Cocoa/widgets/savedialog.m | 48 +++ Cocoa/widgets/slider.m | 38 +++ Cocoa/widgets/textedit.m | 83 ++++++ Cocoa/widgets/textfield.m | 11 + Cocoa/widgets/window.m | 190 ++++++++++++ slider.nim | 26 ++ test1.nim | 41 +++ 40 files changed, 1794 insertions(+), 2 deletions(-) create mode 100644 Cocoa/NSButton.nim create mode 100644 Cocoa/NSCheckbox.nim create mode 100644 Cocoa/NSColordialog.nim create mode 100644 Cocoa/NSCombobox.nim create mode 100644 Cocoa/NSDialog.nim create mode 100644 Cocoa/NSFunctions.nim create mode 100644 Cocoa/NSLabel.nim create mode 100644 Cocoa/NSLine.nim create mode 100644 Cocoa/NSListbox.nim create mode 100644 Cocoa/NSMenu.nim create mode 100644 Cocoa/NSMessagebox.nim create mode 100644 Cocoa/NSOpendialog.nim create mode 100644 Cocoa/NSSavedialog.nim create mode 100644 Cocoa/NSSlider.nim create mode 100644 Cocoa/NSTextedit.nim create mode 100644 Cocoa/NSTextfield.nim create mode 100644 Cocoa/NSWindow.nim create mode 100644 Cocoa/widgets/button.m create mode 100644 Cocoa/widgets/checkbox.m create mode 100644 Cocoa/widgets/col.h create mode 100644 Cocoa/widgets/colordialog.m create mode 100644 Cocoa/widgets/combobox.m create mode 100644 Cocoa/widgets/dialog.m create mode 100644 Cocoa/widgets/functions.m create mode 100644 Cocoa/widgets/label.m create mode 100644 Cocoa/widgets/line.m create mode 100644 Cocoa/widgets/listbox.m create mode 100644 Cocoa/widgets/menu.m create mode 100644 Cocoa/widgets/messagebox.m create mode 100644 Cocoa/widgets/nsslider.m create mode 100644 Cocoa/widgets/opendialog.m create mode 100644 Cocoa/widgets/saneview.m create mode 100644 Cocoa/widgets/savedialog.m create mode 100644 Cocoa/widgets/slider.m create mode 100644 Cocoa/widgets/textedit.m create mode 100644 Cocoa/widgets/textfield.m create mode 100644 Cocoa/widgets/window.m create mode 100644 slider.nim create mode 100644 test1.nim diff --git a/.gitignore b/.gitignore index bc0077f..5ec0864 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -# ---> Nim -nimcache/ +.DS_Store +*.app diff --git a/Cocoa/NSButton.nim b/Cocoa/NSButton.nim new file mode 100644 index 0000000..9518ef9 --- /dev/null +++ b/Cocoa/NSButton.nim @@ -0,0 +1,5 @@ +{.compile: "widgets/button.m".} + +import NSFunctions + +proc newButton*(parent: ID; caption: cstring; left: cint; top: cint; width: cint; height: cint; `func`: ACTION): ID {.cdecl, importc: "createButton".} \ No newline at end of file diff --git a/Cocoa/NSCheckbox.nim b/Cocoa/NSCheckbox.nim new file mode 100644 index 0000000..7eb01de --- /dev/null +++ b/Cocoa/NSCheckbox.nim @@ -0,0 +1,5 @@ +{.compile: "widgets/checkbox.m".} + +import NSFunctions + +proc newCheckBox*(parent: ID, caption: cstring, left, top, width, height:int): ID {.cdecl, importc: "createCheckBox".} \ No newline at end of file diff --git a/Cocoa/NSColordialog.nim b/Cocoa/NSColordialog.nim new file mode 100644 index 0000000..e69de29 diff --git a/Cocoa/NSCombobox.nim b/Cocoa/NSCombobox.nim new file mode 100644 index 0000000..91d8c8d --- /dev/null +++ b/Cocoa/NSCombobox.nim @@ -0,0 +1,5 @@ +{.compile: "widgets/combobox.m".} + +import NSFunctions + +proc newComboBox*(parent: ID; left: cint; top: cint; width: cint; height: cint; `callback`: ACTION): ID {.cdecl, importc: "createComboBox".} \ No newline at end of file diff --git a/Cocoa/NSDialog.nim b/Cocoa/NSDialog.nim new file mode 100644 index 0000000..b0610a0 --- /dev/null +++ b/Cocoa/NSDialog.nim @@ -0,0 +1,5 @@ +{.compile: "widgets/dialog.m".} + +import NSFunctions + +proc newDialog*(title, message: cstring, height: cint): cint {.cdecl, importc: "createDialog".} \ No newline at end of file diff --git a/Cocoa/NSFunctions.nim b/Cocoa/NSFunctions.nim new file mode 100644 index 0000000..b5a343d --- /dev/null +++ b/Cocoa/NSFunctions.nim @@ -0,0 +1,56 @@ +{.compile: "widgets/functions.m".} + +type + ID* = pointer + ACTION* = proc (a2: ID) + +# GUI ANCHOR SYSTEM +const + akNone* = 0 + akRight* = 1 + akWidth* = 2 + akLeft* = 4 + akBottom* = 8 + akHeight* = 16 + akFull* = 18 + akTop* = 32 + + # Flags for GUI Anchor System + + # akNone No Anchor + # akRight Anchor to Right of Window + # akWidth Anchor to Left and Right (width) + # akLeft Anchor to Left + # akBottom Anchor to Bottom + # akHeight Anchor to Top and Bottom + # akFull Anchor to Left/Right/Top/Bottom + # akTop Anchor to Top + +proc `anchor=`*(widget: ID; value: cint) {.cdecl, importc: "Pin".} +proc Cocoa_Init*() {.cdecl, importc: "Cocoa_Init".} +proc Cocoa_Run*() {.cdecl, importc: "Cocoa_Run".} +proc `text=`*(widget: ID; txt: cstring) {.cdecl, importc: "SetText".} +proc `text`*(widget: ID): cstring {.cdecl, importc: "GetText".} +proc `item=`*(widget: ID; txt: cstring) {.cdecl, importc: "AddItem".} +proc `item`*(widget: ID) {.cdecl, importc: "GetItem".} +proc `value=`*(widget: ID, value: cint) {.cdecl, importc: "SetValue".} +proc `value`*(widget: ID): cint {.cdecl, importc: "GetValue".} + + +# proc `text=`*(widget: ID; txt: cstring) = +# widget.SetText(txt) + +# proc `text`*(widget: ID): cstring = +# GetText(widget) + +# proc `item`*(widget: ID): cstring = +# GetItem(widget) + +# proc `anchor=`*(widget: ID; value: cint) = +# widget.AutoSizeMask(value) + +# proc `value=`*(widget: ID, value: cint) = +# widget.SetValue(value) + +# proc `value`*(widget: ID): cint = +# result = widget.GetValue() \ No newline at end of file diff --git a/Cocoa/NSLabel.nim b/Cocoa/NSLabel.nim new file mode 100644 index 0000000..0b8564b --- /dev/null +++ b/Cocoa/NSLabel.nim @@ -0,0 +1,5 @@ +{.compile: "widgets/label.m".} + +import NSFunctions + +proc newLabel*(parent: ID; txt: cstring, left, top, width, height: int): ID {.cdecl, importc: "createLabel".} \ No newline at end of file diff --git a/Cocoa/NSLine.nim b/Cocoa/NSLine.nim new file mode 100644 index 0000000..0562a61 --- /dev/null +++ b/Cocoa/NSLine.nim @@ -0,0 +1,5 @@ +{.compile: "widgets/line.m".} + +import NSFunctions + +proc newLine*(parent: ID, left, top, width: cint): ID {.cdecl, importc: "createLine".} \ No newline at end of file diff --git a/Cocoa/NSListbox.nim b/Cocoa/NSListbox.nim new file mode 100644 index 0000000..4b25d52 --- /dev/null +++ b/Cocoa/NSListbox.nim @@ -0,0 +1,5 @@ +{.compile: "widgets/listbox.m".} + +import NSFunctions + +proc newListBox*(parent: ID, left, top, width, height: cint): ID {.cdecl, importc: "createListBox".} \ No newline at end of file diff --git a/Cocoa/NSMenu.nim b/Cocoa/NSMenu.nim new file mode 100644 index 0000000..aeaaa31 --- /dev/null +++ b/Cocoa/NSMenu.nim @@ -0,0 +1,7 @@ +{.compile: "widgets/menu.m".} + +import NSFunctions + +proc newMenu*(title: cstring): ID {.cdecl, importc: "createMenu".} +proc newMenuItem*(parent: ID, caption, key: cstring, `callback`:ACTION) {.cdecl, importc: "createMenuItem".} +proc newMenuSeparator*(parent: ID) {.cdecl, importc: "createMenuSeparator".} \ No newline at end of file diff --git a/Cocoa/NSMessagebox.nim b/Cocoa/NSMessagebox.nim new file mode 100644 index 0000000..a2df48a --- /dev/null +++ b/Cocoa/NSMessagebox.nim @@ -0,0 +1,5 @@ +{.compile: "widgets/messagebox.m".} + +import NSFunctions + +proc newMessageBox*(title, message: cstring, height: cint): cint {.cdecl, importc: "createMessageBox".} diff --git a/Cocoa/NSOpendialog.nim b/Cocoa/NSOpendialog.nim new file mode 100644 index 0000000..9ee9791 --- /dev/null +++ b/Cocoa/NSOpendialog.nim @@ -0,0 +1,5 @@ +{.compile: "widgets/opendialog.m".} + +import NSFunctions + +proc newOpenDialog*(parent: ID, types: cstring): cstring {.cdecl, importc: "createOpenDialog".} \ No newline at end of file diff --git a/Cocoa/NSSavedialog.nim b/Cocoa/NSSavedialog.nim new file mode 100644 index 0000000..f2cf98d --- /dev/null +++ b/Cocoa/NSSavedialog.nim @@ -0,0 +1,5 @@ +{.compile: "widgets/savedialog.m".} + +import NSFunctions + +proc newSaveDialog*(parent: ID, types: cstring): cstring {.cdecl, importc: "createSaveDialog".} \ No newline at end of file diff --git a/Cocoa/NSSlider.nim b/Cocoa/NSSlider.nim new file mode 100644 index 0000000..d6137ac --- /dev/null +++ b/Cocoa/NSSlider.nim @@ -0,0 +1,5 @@ +{.compile: "widgets/slider.m".} + +import NSFunctions + +proc newSlider*(parent: ID; left, top, width, height: int, `func`: ACTION): ID {.cdecl, importc: "createSlider".} \ No newline at end of file diff --git a/Cocoa/NSTextedit.nim b/Cocoa/NSTextedit.nim new file mode 100644 index 0000000..0de4deb --- /dev/null +++ b/Cocoa/NSTextedit.nim @@ -0,0 +1,5 @@ +{.compile: "widgets/textedit.m".} + +import NSFunctions + +proc newTextEdit*(parent: ID; txt: cstring, left, top, width, height: int): ID {.cdecl, importc: "createTextEdit".} \ No newline at end of file diff --git a/Cocoa/NSTextfield.nim b/Cocoa/NSTextfield.nim new file mode 100644 index 0000000..f60a29a --- /dev/null +++ b/Cocoa/NSTextfield.nim @@ -0,0 +1,5 @@ +{.compile: "widgets/textfield.m".} + +import NSFunctions + +proc newTextField*(parent: ID; txt: cstring; left: cint; top: cint; width: cint; height: cint): ID {.cdecl, importc: "createTextField".} \ No newline at end of file diff --git a/Cocoa/NSWindow.nim b/Cocoa/NSWindow.nim new file mode 100644 index 0000000..b637577 --- /dev/null +++ b/Cocoa/NSWindow.nim @@ -0,0 +1,8 @@ +{.compile: "widgets/window.m".} +{.compile: "widgets/saneview.m".} + +import NSFunctions + +{.passL: "-fobjc-arc -framework Cocoa"} + +proc newWindow*(title: cstring, width, height: int): ID {.importc: "createWindow".} diff --git a/Cocoa/widgets/button.m b/Cocoa/widgets/button.m new file mode 100644 index 0000000..6c4ee95 --- /dev/null +++ b/Cocoa/widgets/button.m @@ -0,0 +1,33 @@ +#import + +#import "col.h" + + + + +@implementation CocoaButton + + @synthesize buttonAction; + + + - (void) click:(id)sender { + buttonAction(sender); +} +@end + +id createButton(id parent, const char* caption, int l, int t, int w, int h, ACTION func){ + CocoaButton *widget = [[[CocoaButton alloc] initWithFrame:NSMakeRect( l, t, w, h )] autorelease]; + [widget setButtonType:NSMomentaryPushInButton]; + [widget setBezelStyle: NSRoundedBezelStyle]; + [widget setTitle: [NSString stringWithUTF8String:caption]]; + [widget setTarget: widget]; +// [widget setAutoresizingMask: NSViewMinXMargin ]; + + if (func) { + [widget setButtonAction:func]; + [widget setAction: @selector(click:)]; + } + [[parent contentView] addSubview:widget]; + return widget; +} + diff --git a/Cocoa/widgets/checkbox.m b/Cocoa/widgets/checkbox.m new file mode 100644 index 0000000..6a97a3b --- /dev/null +++ b/Cocoa/widgets/checkbox.m @@ -0,0 +1,29 @@ +#import + +#import "col.h" + + + + +id createCheckBox(id parent, const char* caption, int l, int t, int w, int h){ + NSButton *self = [[[NSButton alloc] initWithFrame:NSMakeRect( l, t, w, h )] autorelease]; + [self setButtonType:NSSwitchButton]; + [self setBezelStyle: 0]; + [self setTitle: [NSString stringWithUTF8String:caption]]; + [self setTarget: self]; + + [[parent contentView] addSubview:self]; + return self; +} + +void SetState(id widget, bool state) { + if (state == TRUE) { + [widget setState: NSOnState]; + }else{ + [widget setState: NSOffState]; + } +} + +int State(id widget) { + return [widget state]; +} diff --git a/Cocoa/widgets/col.h b/Cocoa/widgets/col.h new file mode 100644 index 0000000..432da4b --- /dev/null +++ b/Cocoa/widgets/col.h @@ -0,0 +1,562 @@ +#import + +#ifdef __cplusplus + extern "C" { +#endif + + typedef void* id; + #define NSSTR(txt) [NSString stringWithUTF8String:txt] + #define BeginMenu dispatch_async(dispatch_get_main_queue(), ^{ + #define EndMenu }); + + + #ifndef NSWindowStyleMaskTitled + #define NSWindowStyleMaskTitled NSTitledWindowMask + #endif + + #ifndef NSWindowStyleMaskClosable + #define NSWindowStyleMaskClosable NSClosableWindowMask + #endif + + #ifndef NSWindowStyleMaskMiniaturizable + #define NSWindowStyleMaskMiniaturizable NSMiniaturizableWindowMask + #endif + + #ifndef NSWindowStyleMaskResizable + #define NSWindowStyleMaskResizable NSResizableWindowMask + #endif + + #ifndef NSAlertStyleWarning + #define NSAlertStyleWarning NSWarningAlertStyle + #endif + + #ifndef NSTextAlignmentLeft + #define NSTextAlignmentLeft NSLeftTextAlignment + #endif + + #ifndef NSAlertStyleWarning + #define NSAlertStyleWarning NSWarningAlertStyle + #endif + + #ifndef NSEventTypeKeyDown + #define NSEventTypeKeyDown NSKeyDown + #endif + + #ifndef NSEventModifierFlagCommand + #define NSEventModifierFlagCommand NSCommandKeyMask + #endif + + #ifndef NSEventModifierFlagOption + #define NSEventModifierFlagOption NSAlternateKeyMask + #endif + + + + typedef void(*ACTION)(id); + /** + Used by Internal Callback System + to route callback to user provided + subroutine + + Subroutine must not return value + + Ex: + void Example(void); + */ + + enum { + akNone, + akRight, + akWidth, + akLeft = 4, + akBottom = 8, + akHeight = 16, + akFull = 18, + akTop = 32 + + }; + /** + Flags for Widget Anchor System + + akNone No Anchor + akRight Anchor to Right of Window + akWidth Anchor to Left and Right (width) + akLeft Anchor to Left + akBottom Anchor to Bottom + akHeight Anchor to Top and Bottom + akTop Anchor to Top + */ + + @interface SaneView : NSClipView + - (BOOL)isFlipped; + @end + + @interface CocoaWindow : NSWindow + { + // NOT USED + } + + - (void)applicationDidFinishLaunching: (NSNotification *)notification; + - (BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSNotification *)notification; + - (id) initFormWithTitle:(NSString*)title width:(NSInteger)width height:(NSInteger)height; + - (void)createApplicationMenu; + @end + + @interface awColorPanel: NSColorPanel { + NSColor* theColor; + NSModalSession modalSession; + } + @property (copy) NSColor* theColor; + + - (void)colorUpdate:(NSColorPanel*)colorPanel; + + - (const char *) hexColor; + @end + + @interface CocoaListBox : NSScrollView + { + NSTableView *tbl; + NSTableColumn *tblc; + NSMutableArray *db; + + + int cnt; + } + @property (retain) NSTableView *tbl; + @property (retain) NSMutableArray *db; + + + - (id)initWithFrame:(NSRect)frame; + - (void)doubleclicked:(id)sender; + - (void)selected:(id)sender; + @end + + @interface CocoaButton: NSButton{ + ACTION buttonAction; + } + @property ACTION buttonAction; + + + - (void) click:(id)sender; + @end + + @interface CocoaComboBox : NSComboBox { + // NSInteger currentItem; + ACTION comboCallBack; + } + + @property ACTION comboCallBack; + @property NSInteger currentItem; + + - (id)initWithFrame:(NSRect)frame; + + /* notification responders */ + - (void)comboBoxSelectionDidChange:(NSNotification *)notification; + + @end + + @interface CocoaTextEdit : NSScrollView + { + NSTextView *widget; + } + @property (retain) NSTextView *widget; + - (id)initWithFrame:(NSRect)frame; + @end + + @interface CocoaMenu : NSMenu + { + ACTION menuAction; + } + + @property (retain) NSMenu *menu; + @property ACTION menuAction; + + + - (void) CreateMenu: (NSString*) Title; + - (void) AddItem: (NSString*)Title withShortcut: (NSString*)key andCallback: (ACTION) callback; + // - (void) click:(id)sender; + - (void) SetAction: (id) widget Callback: (ACTION) func; + - (void)addSeparator; + //- (void) click:(id)sender; + + @end + + @interface CocoaMenuItem : NSMenuItem + { + ACTION menuItemAction; + } + @property ACTION menuItemAction; + + - (void) click:(id)sender; + @end + + @interface CocoaSlider: NSSlider + { + int Value; + ACTION sliderAction; + } + @property int Value; + @property ACTION sliderAction; + + - (IBAction)onChanged:(id)sender; + - (id) initWithFrame:(NSRect)rect callBack:(ACTION)func; +@end + + id createWindow(const char* title, int width, int height); + /** + \brief Creates Main Window + + parameters: + String: Title of Window, + INT: Width of Window, + INT: Height of Window + + Returns: + Pointer: Created Window + */ + + void Pin(id widget, NSInteger value); + /** + \brief Specifies how child widget responds to resize of Parent + + parameters: + Pointer: Widget + INT: Anchor point (see Enum above) + + Returns: + None + */ + + void Cocoa_Init(); + /** + \brief Initializes Cocoa GUI + + parameters: + None + + Returns: + None + */ + + void Cocoa_Run(); + /** + \brief Passes control over to Cocoa Runtime + + parameters: + None + + Returns: + None + */ + + void Cocoa_Quit(); + /** + \brief Terminates Cocoa Runtime + + parameters: + None + + Returns: + None + */ + + void Cocoa_About(); + /** + \brief Displays Cocoa About Dialog + + parameters: + None + + Returns: + None + + Note: + Utilizes information contained in Application Bundle's Info.plist + */ + + void SetText(id widget, const char* txt); + /** + \brief Sets Widget Text + + parameters: + Pointer: Target Widget + Text to set + + Returns: + None + */ + + const char* GetText(id widget); + /** + \brief Retrives Text from Widget + + parameters: + Pointer: Target Widget + + Returns: + String: from Widget + */ + + int GetValue(id widget); + /** + \brief Retrieves INT value from Widget + parameters: + Pointer: Target Widget + + Returns: + integer: from Widget + */ + + void SetValue(id widget, int value); + /** + \brief Sets INT value for Widget + parameters: + Pointer: Target Widget + INT: Value to set + + */ + + void AddItem(id widget, const char* txt ); + /** + \brief Adds TEXT Item to ComboBox/ListBox + + parameters: + Pointer: Target Widget + String: Text to Add + + Returns: + None + */ + + // char* GetItem(id widget); + /** + \brief Retrieves Selected Text from ComboBox/ListBox + + parameters: + Pointer: Target Widget + + Returns: + String: Text of Selected Item + */ + + id createTextField(id parent,const char* txt,int l, int t, int w, int h); + /** + \brief Creates TextField (TextBox/TextEntry) Widget + + parameters: + Pointer: Parent Widget, + String: Default Text to Display, + INT: Left Position in Parent, + INT: Top Position in Parent, + INT: Width of Widget, + INT: Height of Widget + + Returns: + Pointer: Created TextField + */ + + id createComboBox(id parent,int l, int t, int w, int h, ACTION callback); + /** + \brief Creates ComboBox Widget + + parameters: + Pointer: Parent Widget, + INT: Left Position in Parent, + INT: Top Position in Parent, + INT: Width of Widget, + INT: Height of Widget, + ACTION: Callback Function or NULL + + Returns: + Pointer: Created ComboBox + */ + + id createButton(id parent, const char* caption, int l, int t, int w, int h, ACTION func); + /** + \brief Creates Button Widget + + parameters: + Pointer: Parent Widget, + String: Button Caption, + INT: Left Position in Parent, + INT: Top Position in Parent, + INT: Width of Widget, + INT: Height of Widget, + ACTION: Callback Function or NULL + + Returns: + Pointer to Created Button + */ + + void SetAction(id widget, SEL func); + + id createCheckBox(id parent, const char* caption, int l, int t, int w, int h); + /** + \brief Creates CheckBox Widget + + parameters: + Pointer: Parent Widget, + String: CheckBox Caption, + INT: Left Position in Parent, + INT: Top Position in Parent, + INT: Width of Widget, + INT: Height of Widget, + ACTION: Callback Function or NULL + + Returns: + Pointer to Created TextField + */ + + void SetState(id widget, bool state); + /** + \brief Sets State of CheckBox Widget (Checked/Unchecked) + + parameters: + Pointer: Parent Widget, + BOOL: State (True for on, False for off) + + Returns: + None + */ + + int State(id widget); + /** + \brief Retrieves State of Checkbox (On[True], Off[False]) + + parameters: + Pointer: Checkbox Widget, + + Returns: + BOOL: State [True/False] + */ + + id createListBox(id parent,int l, int t, int w, int h); + /** + \brief Creates ListBox Widget + + parameters: + Pointer: Parent Widget, + INT: Left Position in Parent, + INT: Top Position in Parent, + INT: Width of Widget, + INT: Height of Widget + + Returns: + Pointer: Created ListBox Widget + */ + + void LB_Clear(id widget); + void SetItem(id widget, int pos); + void RemoveItem(id widget, int pos); + + + char* createOpenDialog( id parent, const char *types); + /** + \brief Displays OpenFile Dialog Widget + + parameters: + Pointer: Parent Widget, + String: File Types Filter, separated by ":" + + Returns: + String: Path to Selected File, or Blank String + + Example: + char *fName = OpenDialog(mywin,"txt:c:h"); + */ + + char* createSaveDialog(id parent, const char* title, const char *types); + /** + \brief Displays SaveFile Dialog Widget + + parameters: + Pointer: Parent Widget, + String: Dialog Title, + String: File Types Filter, separated by ":" + + Returns: + String: Path to Selected File, or Blank String + + Example: + char *fName = SaveDialog(mywin,"txt:c:h"); + */ + + int createDialog(const char *title, const char *message, int type); + int createMessageBox(const char *title, const char *message, int type); + /** + \brief Displays MessageBox Dialog Widget + + parameters: + String: Dialog Title, + String: Message to Display, + INT: Type of Dialog(?) + + Returns: + INT: Value corresponding to OK or Cancel + + Example: + char *fName = SaveDialog(mywin,"txt:c:h"); + */ + + id createColorDialog(); + const char *getColor(id panel); + const char* hexColor(id cPanel); + + id createLabel(id parent, const char* txt, int l, int t, int w, int h); + /** + \brief Creates Label Widget + + parameters: + Pointer: Parent Widget, + String: Caption of Label, + INT: Left Position in Parent, + INT: Top Position in Parent, + INT: Width of Widget, + INT: Height of Widget + + Returns: + Pointer: Label Widget + */ + + id createTextEdit(id parent, const char* txt, int l, int t, int w, int h); + /** + \brief Creates TextEdit Widget + + parameters: + Pointer: Parent Widget, + String: Default Text, + INT: Left Position in Parent, + INT: Top Position in Parent, + INT: Width of Widget, + INT: Height of Widget + + Returns: + Pointer: Label Widget + */ + + void Notify(const char *title, const char * subtitle, const char *text); + /** + \brief Sends Notification to macOS Notification System + + parameters: + String: Title of Notification, + String: Subtitle of Notification, + String: Message to Display + + Returns: + None + */ + + id createLine(id parent, int x, int y, int width); + + id createMenu(const char * Title); + void createMenuItem(id parent, const char *caption, const char *key, ACTION callback); + void createMenuSeparator(id parent); + + id createSlider(id parent, int left, int top, int width, int height, ACTION callback); + +#ifdef __cplusplus + } +#endif + +// #endif diff --git a/Cocoa/widgets/colordialog.m b/Cocoa/widgets/colordialog.m new file mode 100644 index 0000000..a1776dd --- /dev/null +++ b/Cocoa/widgets/colordialog.m @@ -0,0 +1,98 @@ +#import + +#include "col.h" + + + + + +@implementation awColorPanel + @synthesize theColor; + + - (void)colorUpdate:(awColorPanel*)colorPanel{ + theColor = colorPanel.color; +// printf("%s\n",[self hexColor]); + } + + - (const char *) hexColor { + NSString* hexString = [NSString stringWithFormat:@"%02X%02X%02X", + (int) (theColor.redComponent * 0xFF), + (int) (theColor.greenComponent * 0xFF), + (int) (theColor.blueComponent * 0xFF)]; + return [hexString UTF8String]; + } +@end + +id createColorDialog() { + NSColorPanel *cp = [awColorPanel sharedColorPanel]; + [cp setTarget:cp]; + [cp setAction:@selector(colorUpdate:)]; + // [NSApp runModalForWindow: cp]; + [cp makeKeyAndOrderFront:nil]; + + return cp; + +} + +const char* hexColor(awColorPanel* cPanel) { + return [cPanel hexColor]; +} +// @interface NSColorPanel (CPL) +// +// - (void)disablePanel; +// - (void)enablePanel; +// + (NSString *)strColor; +// +// @end +// +// #include +// +// static BOOL colorPanelEnabled = YES; +// +// +// @implementation NSColorPanel (CPL) +// +// - (void)disablePanel { +// colorPanelEnabled = NO; +// } +// +// - (void)enablePanel { +// colorPanelEnabled = YES; +// } + +// - (void)orderFront:(id)sender { +// if (colorPanelEnabled) { +// NSColorPanel *panel = [BFColorPickerPopover sharedPopover].colorPanel; +// if (panel) { +// self.contentView = panel.contentView; +// } +// [super orderFront:sender]; +// } else { +// // Don't do anything. +// } +// } + +// + (NSString *)strColor { +// NSColorPanel *panel = [NSColorPanel sharedColorPanel]; +// [panel orderFront:nil]; +// NSColor *color = [panel.color colorUsingColorSpaceName: NSDeviceRGBColorSpace]; +// return [NSString stringWithFormat:@"r: %d, g: %d, b: %d, a: %d", +// (int)round([color redComponent]*255), +// (int)round([color greenComponent]*255), +// (int)round([color blueComponent]*255), +// (int)round([color alphaComponent]*255)]; +// // return @""; +// } +// @end + +// const char* ColorDialog() { +// NSColorPanel *panel = [NSColorPanel sharedColorPanel]; +// [panel orderFront:nil]; +// NSString *aColor = [NSColorPanel strColor]; +// return [[NSString stringWithFormat:@"r: %d, g: %d, b: %d, a: %d", +// (int)round([color redComponent]*255), +// (int)round([color greenComponent]*255), +// (int)round([color blueComponent]*255), +// (int)round([color alphaComponent]*255)] UTF8String]; +// return [aColor UTF8String]; +// } diff --git a/Cocoa/widgets/combobox.m b/Cocoa/widgets/combobox.m new file mode 100644 index 0000000..c4ea334 --- /dev/null +++ b/Cocoa/widgets/combobox.m @@ -0,0 +1,43 @@ +#import + +#import "col.h" + + + +@implementation CocoaComboBox + +@synthesize currentItem; +@synthesize comboCallBack; + +- (id)initWithFrame:(NSRect)frame +{ + self = [super initWithFrame:frame]; + if (self) { + [self setEditable: NO]; + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(comboBoxSelectionDidChange:) + name:NSComboBoxSelectionDidChangeNotification + object:self]; + } + return self; +} + +- (void)comboBoxSelectionDidChange:(NSNotification *)notification { + // [self setCurrentItem: [self indexOfSelectedItem]]; + self.currentItem = [self indexOfSelectedItem]; + if (self.comboCallBack != NULL) comboCallBack(self); +} +@end + +id createComboBox(id parent,int l, int t, int w, int h, ACTION callback){ + id widget = [[[CocoaComboBox alloc] initWithFrame:NSMakeRect( l, t, w, h )] autorelease]; + if (callback) { + [widget setComboCallBack:callback]; + // [widget setAction: @selector(click:)]; + } + + [[parent contentView] addSubview:widget]; + return widget; +} + diff --git a/Cocoa/widgets/dialog.m b/Cocoa/widgets/dialog.m new file mode 100644 index 0000000..39af961 --- /dev/null +++ b/Cocoa/widgets/dialog.m @@ -0,0 +1,15 @@ +#import + +#include "col.h" + + +int createDialog(const char *title, const char *message, int type) { + NSAlert *alert = [[NSAlert new] autorelease]; + [alert addButtonWithTitle:@"Continue"]; + [alert addButtonWithTitle:@"Cancel"]; + [alert setMessageText:[NSString stringWithUTF8String:title] ]; + [alert setInformativeText:[NSString stringWithUTF8String:message] ]; + [alert setAlertStyle:NSAlertStyleWarning]; + return [alert runModal]; +} + diff --git a/Cocoa/widgets/functions.m b/Cocoa/widgets/functions.m new file mode 100644 index 0000000..ec20253 --- /dev/null +++ b/Cocoa/widgets/functions.m @@ -0,0 +1,154 @@ +#import + +#import "col.h" + +void Pin(id widget, NSInteger value) { + [(NSView*)widget setAutoresizingMask: value]; +} + +void SetText(id widget, const char* txt){ + NSString *widgetClass = [[widget class] description]; + + if ([widgetClass isEqualToString:@"CocoaTextEdit"]) + { + [[widget documentView] setString: [NSString stringWithUTF8String: txt]]; + [widget setNeedsDisplay: YES]; + }else if ([widgetClass isEqualToString:@"CocoaWindow"]) { + [widget setTitle: [NSString stringWithUTF8String: txt]]; + }else{ + [widget setStringValue:[NSString stringWithUTF8String:txt]]; + } +} + +void AddItem(id widget, const char* txt ){ + NSString *widgetClass = [[widget class] description]; + + /* ListBox */ + if ([widgetClass isEqualToString:@"CocoaListBox"]) + { + [[widget db] addObject: [NSString stringWithUTF8String: txt]]; + [[widget tbl] reloadData]; + }else{ + /* ComboBox */ + [widget addItemWithObjectValue:[NSString stringWithUTF8String:txt]]; + } +} + +const char* GetText(id widget) { + NSString *widgetClass = [[widget class] description]; + + /* ListBox */ + if ([widgetClass isEqualToString:@"CocoaListBox"]) { + NSInteger row = [[widget tbl] selectedRow]; + // NSString *dbValue = [[widget db]objectAtIndex:row]; + // return strdup([dbValue UTF8String]); + return [[[widget db]objectAtIndex:row] UTF8String]; + + /* TextEdit */ + }else if ([widgetClass isEqualToString:@"CocoaTextEdit"]) { + if ( [[[widget textStorage] string] length] > 0) { + return [[[widget textStorage] string] UTF8String]; + }else{ + return @"".UTF8String; + } + // const char* str = [[[widget textStorage] string] UTF8String]; + // if (strlen(str)>0) { + // return strdup(str); + // }else{ + // return strdup(""); + // } + /* ComboBox */ + }else if ([widgetClass isEqualToString: @"CocoaComboBox"]) { + return [[widget objectValueOfSelectedItem] UTF8String]; + + }else{ + /* Everything Else */ + return [[widget stringValue] UTF8String]; + } +} + +int GetValue(id widget) { + NSString *widgetClass = [[widget class] description]; + + /* Slider */ + if ([widgetClass isEqualToString: @"CocoaSlider"]) { + return [widget Value]; + } + return 0; +} + +void SetValue(id widget, int value) { + NSString *widgetClass = [[widget class] description]; + + /* Slider */ + if ([widgetClass isEqualToString: @"CocoaSlider"]) { + [widget setIntValue:value]; + } +} + +void SetItem(id widget, int pos) { + NSString *widgetClass = [[widget class] description]; + if ([widgetClass isEqualToString:@"CocoaListbox"]) { + NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:pos]; + [widget selectRowIndexes: indexSet byExtendingSelection:NO]; + }else if ([widgetClass isEqualToString:@"CocoaComboBox"]) { + [widget selectItemAtIndex:pos]; + [widget setObjectValue:[widget objectValueOfSelectedItem]]; + } +} + +void RemoveItem(id widget, int index) { + NSString *widgetClass = [[widget class] description]; + + if ([widgetClass isEqualToString:@"CocoaListBox"]) { + [[widget db]removeObjectAtIndex: index]; + [[widget tbl] reloadData]; + }else if ([widgetClass isEqualToString:@"CocoaComboBox"]) { + [widget removeItemAtIndex: index]; + } +} + +void LB_Clear(id widget) { + [[widget db] removeAllObjects]; + [[widget tbl] reloadData]; +} + +void Cocoa_Init() { + [NSApplication sharedApplication]; + [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; +} + +void Cocoa_Run() { + // NSApplicationMain(0,0); + // [NSApp setDelegate:mainWin]; + [NSApp activateIgnoringOtherApps:YES]; + [NSApp run]; +} + +void Cocoa_Quit() { + [NSApp performSelector:@selector(terminate:) withObject:nil afterDelay:0.0]; +} + +void Cocoa_About() { + [NSApp performSelector:@selector(orderFrontStandardAboutPanel:) withObject:nil afterDelay:0.0]; +} + +void Notify(const char *title, const char * subtitle, const char *text) { + if (strlen(title)>0 && strlen(text)>0) { + + NSUserNotificationCenter *nc = NSUserNotificationCenter.defaultUserNotificationCenter; + + NSUserNotification *note = NSUserNotification.new; + note.title = [NSString stringWithUTF8String:title]; + note.subtitle = [NSString stringWithUTF8String:subtitle]; + note.informativeText = [NSString stringWithUTF8String:text]; + note.soundName = NSUserNotificationDefaultSoundName; + + [nc deliverNotification:note]; + + [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; + + } + +} + diff --git a/Cocoa/widgets/label.m b/Cocoa/widgets/label.m new file mode 100644 index 0000000..4ef41f7 --- /dev/null +++ b/Cocoa/widgets/label.m @@ -0,0 +1,14 @@ +#import + +#import "col.h" + +id createLabel(id parent, const char* txt, int l, int t, int w, int h){ + NSTextField *widget = [[[NSTextField alloc] initWithFrame:NSMakeRect( l, t, w, h )] autorelease]; + [widget setStringValue:[NSString stringWithUTF8String:txt]]; + [widget setEditable: NO]; + [widget setAlignment: NSTextAlignmentLeft]; + [widget setDrawsBackground: NO]; + [widget setBordered: NO]; + [[parent contentView] addSubview:widget]; + return widget; +} diff --git a/Cocoa/widgets/line.m b/Cocoa/widgets/line.m new file mode 100644 index 0000000..3f94f09 --- /dev/null +++ b/Cocoa/widgets/line.m @@ -0,0 +1,24 @@ +#import + +#import "col.h" + +id createLine(id parent, int x, int y, int width) { + NSBox *widget = [[[NSBox alloc] initWithFrame:NSMakeRect(x,y,width,1)] autorelease]; + widget.boxType = NSBoxSeparator; + [[parent contentView] addSubview:widget]; + return widget; +} + +// create a horizontally oriented separator + +// NSBox *horizontalSeparator=[[NSBox alloc] initWithFrame:NSMakeRect(15.0,250.0,250.0,1.0)]; + +// [horizontalSeparator setBoxType:NSBoxSeparator]; + + + +// create a vertically oriented separator + +// NSBox *verticalSeparator=[[NSBox alloc] initWithFrame:NSMakeRect(250.0,15.0,1.0,250.0)]; + +// [verticalSeparator setBoxType:NSBoxSeparator]; diff --git a/Cocoa/widgets/listbox.m b/Cocoa/widgets/listbox.m new file mode 100644 index 0000000..a914db4 --- /dev/null +++ b/Cocoa/widgets/listbox.m @@ -0,0 +1,85 @@ +#import + +#import "col.h" + + +@implementation CocoaListBox + +@synthesize tbl; +@synthesize db; + +- (id)initWithFrame:(NSRect)frame { + self = [super initWithFrame:frame]; + db = [NSMutableArray new]; + + if (self) { + tblc = [[NSTableColumn alloc] initWithIdentifier: @"Name"]; + [tblc setEditable: NO]; + + tbl = [[NSTableView alloc] initWithFrame: NSMakeRect(5,40,335,185)]; + [tbl setGridStyleMask: NSTableViewGridNone]; + [tbl setAllowsColumnSelection: NO]; + [tbl setAllowsColumnReordering: NO]; + [tbl setAllowsEmptySelection: NO]; + [tbl setAllowsMultipleSelection: NO]; + [tbl setColumnAutoresizingStyle: NSTableViewUniformColumnAutoresizingStyle]; + [tbl addTableColumn: tblc]; + [tbl setDataSource: self]; + [tbl setDelegate: self]; + [tbl setTarget:self]; + [tbl setDoubleAction:@selector(doubleclicked:)]; + // [tbl setAction:@selector(selected:)]; + + [tbl setHeaderView:nil]; + + [self setHasHorizontalScroller: NO]; + [self setHasVerticalScroller: YES]; + [self setDocumentView: tbl]; + // [self setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; + + [tblc setResizingMask: NSTableColumnAutoresizingMask]; + [tblc setWidth:10000]; + [tbl reloadData]; + + cnt = 0; + } + return self; +} + +- (void)doubleclicked:(id)sender +{ + // int rowIndex = [sender selectedRow]; +} + +- (void)selected:(id)sender +{ + // [tbl selectRowIndexes:[NSIndexSet indexSetWithIndex:item->row] byExtendingSelection:NO]; +} + +-(void)tableViewSelectionDidChange:(NSNotification *)notification +{ + NSInteger row = [tbl selectedRow]; + +} + +- (long)numberOfRowsInTableView:(NSTableView *)aTableView +{ + return db.count; +} + +-(id)tableView:(NSTableView *)tableView + objectValueForTableColumn:(NSTableColumn *)tableColumn + row:(NSInteger)row +{ + return self.db[row]; +} + + +@end + + +id createListBox(id parent,int l, int t, int w, int h){ + id self = [[[CocoaListBox alloc] initWithFrame:NSMakeRect( l, t, w, h )] autorelease]; + [[parent contentView] addSubview:self]; + return self; +} diff --git a/Cocoa/widgets/menu.m b/Cocoa/widgets/menu.m new file mode 100644 index 0000000..9568580 --- /dev/null +++ b/Cocoa/widgets/menu.m @@ -0,0 +1,64 @@ +#import "col.h" + +@implementation CocoaMenuItem + @synthesize menuItemAction; + +- (void) click:(id)sender { + menuItemAction(sender); +} +@end + +@implementation CocoaMenu + +@synthesize menu; +@synthesize menuAction; + + +- (void) CreateMenu: (NSString*) Title { + NSMenuItem *newMenuItem = [[NSMenuItem alloc] initWithTitle:Title action:NULL keyEquivalent:@""]; + + self.menu = [[NSMenu alloc] initWithTitle:Title]; + [newMenuItem setSubmenu:self.menu]; + [[[NSApplication sharedApplication] mainMenu] addItem: newMenuItem]; +} + +- (void) AddItem: (NSString*)Title withShortcut: (NSString*)key andCallback: (ACTION) callback { + CocoaMenuItem *item = [[[CocoaMenuItem alloc] init] autorelease]; + [item setTitle:Title]; + [item setTarget:item]; + [item setKeyEquivalent: key]; + + if (callback != NULL) { + [item setMenuItemAction: callback]; + [item setAction: @selector(click:)]; + } + + [self.menu addItem: item]; +} + +- (void)addSeparator { + [self.menu addItem: [NSMenuItem separatorItem]]; +} + +- (void) SetAction: (id) widget Callback: (ACTION) func { + [widget setMenuAction:func]; +} + +@end + + +id createMenu(const char * Title) { + id widget = [CocoaMenu new]; + [widget CreateMenu: NSSTR(Title)]; + return widget; + +} + +void createMenuItem(id parent, const char *caption, const char *key, ACTION callback) { + + [parent AddItem: NSSTR(caption) withShortcut: NSSTR(key) andCallback: callback]; + +} +void createMenuSeparator(id parent){ + [parent addSeparator]; +} diff --git a/Cocoa/widgets/messagebox.m b/Cocoa/widgets/messagebox.m new file mode 100644 index 0000000..e3ea07f --- /dev/null +++ b/Cocoa/widgets/messagebox.m @@ -0,0 +1,14 @@ +#import + +#import "col.h" + + + +int createMessageBox(const char *title, const char *message, int type) { + NSAlert *alert = [[NSAlert new] autorelease]; + [alert addButtonWithTitle:@"Ok"]; + [alert setMessageText:[NSString stringWithUTF8String:title] ]; + [alert setInformativeText:[NSString stringWithUTF8String:message] ]; + [alert setAlertStyle:NSAlertStyleWarning]; + return [alert runModal]; +} diff --git a/Cocoa/widgets/nsslider.m b/Cocoa/widgets/nsslider.m new file mode 100644 index 0000000..22d1de4 --- /dev/null +++ b/Cocoa/widgets/nsslider.m @@ -0,0 +1,30 @@ +#import +#import "col.h" + +@implementation CocoaSlider +{ + +} +@synthesize Value; +@synthesize sliderAction; + +- (IBAction)onChanged:(id)sender{ + self.Value = [sender intValue]; + sliderAction(sender); +} + +- (id) initWithFrame:(NSRect)rect callBack:(ACTION)func { + self = [super initWithFrame: rect]; + [self setMinValue:0]; + [self setMaxValue:100]; + [self setAllowsTickMarkValuesOnly:NO]; + [self setNumberOfTickMarks:0]; + [self setTickMarkPosition:1]; + [self setTarget:self]; + if (func != NULL) { + [self setSliderAction: func]; + [self setAction: @selector(onChanged:)]; + } + return self; +} +@end diff --git a/Cocoa/widgets/opendialog.m b/Cocoa/widgets/opendialog.m new file mode 100644 index 0000000..11a944a --- /dev/null +++ b/Cocoa/widgets/opendialog.m @@ -0,0 +1,43 @@ +#import + +#import "col.h" + + +char* createOpenDialog( id parent, const char *types) { + NSOpenPanel* widget = [NSOpenPanel openPanel]; + + // Enable the selection of files in the dialog. + [widget setCanChooseFiles:YES]; + + widget.showsResizeIndicator = YES; + widget.showsHiddenFiles = NO; + widget.canCreateDirectories = YES; + + // Change "Open" dialog button to "Select" + [widget setPrompt:@"Select"]; + + // Set FileTypes + if ( strlen(types) > 0 ) { + NSString *fileTypes = [NSString stringWithUTF8String:types]; + NSArray *fTypes = [fileTypes componentsSeparatedByString:@":"]; + + [widget setAllowedFileTypes:fTypes]; + } + + // if ( [widget runModal] == NSModalResponseOK ) { + // NSString* path = [[widget URL] path]; + // return strdup([path UTF8String]); + // } + [widget beginSheetModalForWindow:parent completionHandler:^(NSInteger result) { + [NSApp stopModalWithCode:result]; + }]; + if ([NSApp runModalForWindow:parent] == NSFileHandlingPanelOKButton) { + NSString* path = [[widget URL] path]; + return strdup([path UTF8String]); + } + + return strdup(""); +} + + + diff --git a/Cocoa/widgets/saneview.m b/Cocoa/widgets/saneview.m new file mode 100644 index 0000000..a6efea5 --- /dev/null +++ b/Cocoa/widgets/saneview.m @@ -0,0 +1,11 @@ +#import + +#import "col.h" + + +@implementation SaneView +- (BOOL)isFlipped +{ + return YES; +} +@end diff --git a/Cocoa/widgets/savedialog.m b/Cocoa/widgets/savedialog.m new file mode 100644 index 0000000..589e82a --- /dev/null +++ b/Cocoa/widgets/savedialog.m @@ -0,0 +1,48 @@ +#include + +#import "col.h" + + +char* createSaveDialog(id parent, const char* title, const char* types) { + NSString* path = @""; + NSSavePanel *widget = [NSSavePanel savePanel]; + + if (strlen(title)>0) { + widget.title = [NSString stringWithUTF8String: title]; + }else{ + widget.title = @"Save File"; + } + + + // Set FileTypes + if ( strlen(types) > 0 ) { + NSString *fileTypes = [NSString stringWithUTF8String:types]; + NSArray *fTypes = [fileTypes componentsSeparatedByString:@":"]; + + [widget setAllowedFileTypes:fTypes]; + } + + widget.showsResizeIndicator = YES; + widget.showsHiddenFiles = NO; + widget.canCreateDirectories = YES; + widget.allowsOtherFileTypes = YES; + + + // if ( [widget runModal] == NSModalResponseOK ) { + // path = [[widget URL] path]; + // } + + [widget beginSheetModalForWindow:parent completionHandler:^(NSInteger result) { + [NSApp stopModalWithCode:result]; + }]; + + if ([NSApp runModalForWindow:parent] == NSFileHandlingPanelOKButton) { + NSString* path = [[widget URL] path]; + // return strdup([path UTF8String]); + } + return strdup([path UTF8String]); + +} + + + diff --git a/Cocoa/widgets/slider.m b/Cocoa/widgets/slider.m new file mode 100644 index 0000000..945a86d --- /dev/null +++ b/Cocoa/widgets/slider.m @@ -0,0 +1,38 @@ +#import +#import "col.h" + +@implementation CocoaSlider +{ + +} +@synthesize Value; +@synthesize sliderAction; + +- (IBAction)onChanged:(id)sender{ + self.Value = [sender intValue]; + sliderAction(sender); +} + +- (id) initWithFrame:(NSRect)rect callBack:(ACTION)callback { + self = [super initWithFrame: rect]; + [self setMinValue:0]; + [self setMaxValue:100]; + [self setAllowsTickMarkValuesOnly:NO]; + [self setNumberOfTickMarks:0]; + [self setTickMarkPosition:1]; + [self setTarget:self]; + [self setContinuous: YES]; + if (callback != NULL) { + [self setSliderAction: callback]; + [self setAction: @selector(onChanged:)]; + } + return self; +} +@end + +id createSlider(id parent, int left, int top, int width, int height, ACTION callback) { + id widget = [[CocoaSlider alloc] initWithFrame:NSMakeRect(left, top, width, height) callBack:callback]; + + [[parent contentView] addSubview:widget]; + return widget; +} diff --git a/Cocoa/widgets/textedit.m b/Cocoa/widgets/textedit.m new file mode 100644 index 0000000..4a5fdd0 --- /dev/null +++ b/Cocoa/widgets/textedit.m @@ -0,0 +1,83 @@ +#import + +#import "col.h" + +@implementation CocoaTextEdit + @synthesize widget; + +- (id)initWithFrame:(NSRect)frame { + self = [super initWithFrame:frame]; + if (self) { + self.widget = [[[NSTextView alloc] initWithFrame:frame] autorelease]; + [[self.widget textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)]; + [[self.widget textContainer] setWidthTracksTextView:NO]; + [self.widget setHorizontallyResizable:YES]; + [self.widget setTextContainerInset:NSMakeSize(2.0,2.0)]; + [self setHasHorizontalScroller: YES]; + [self setHasVerticalScroller: YES]; + [self.widget setFont: [NSFont userFontOfSize:13.0]]; + [self.widget setDelegate:self]; + [self.widget becomeFirstResponder]; +// [self.widget setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; + [self setDocumentView: widget]; + } + return self; +} + +- (BOOL)performKeyEquivalent:(NSEvent *)theEvent +{ + if (([theEvent type] == NSEventTypeKeyDown) && ([theEvent modifierFlags] & NSEventModifierFlagCommand)) { + NSResponder * responder = [[self window] firstResponder]; + + if ((responder != nil) && [responder isKindOfClass:[NSTextView class]]) { + NSTextView * textView = (NSTextView *)responder; + NSRange range = [textView selectedRange]; + bool bHasSelectedTexts = (range.length > 0); + + unsigned short keyCode = [theEvent keyCode]; + + bool bHandled = false; + + //6 Z, 7 X, 8 C, 9 V + if (keyCode == 6) { + if ([[textView undoManager] canUndo]) + { + [[textView undoManager] undo]; + bHandled = true; + } + } + else if (keyCode == 7 && bHasSelectedTexts) { + [textView cut:self]; + bHandled = true; + } + else if (keyCode== 8 && bHasSelectedTexts) { + [textView copy:self]; + bHandled = true; + } + else if (keyCode == 9) { + [textView paste:self]; + bHandled = true; + } + + if (bHandled) + return YES; + } + } + + return NO; +} + + +@end + +id createTextEdit(id parent, const char* txt, int l, int t, int w, int h) { + CocoaTextEdit *widget = [[[CocoaTextEdit alloc] initWithFrame:NSMakeRect( l, t, w, h )] autorelease]; + if (strlen(txt)>0) { + NSString *str = [NSString stringWithUTF8String:txt]; + [[widget documentView] setString:str]; + } + + [[parent contentView] addSubview:widget]; + return widget; +} + diff --git a/Cocoa/widgets/textfield.m b/Cocoa/widgets/textfield.m new file mode 100644 index 0000000..ef46902 --- /dev/null +++ b/Cocoa/widgets/textfield.m @@ -0,0 +1,11 @@ +#import + +#import "col.h" + +id createTextField(id parent, const char* txt, int l, int t, int w, int h){ + NSTextField *widget = [[[NSTextField alloc] initWithFrame:NSMakeRect( l, t, w, h )] autorelease]; + [widget setStringValue:[NSString stringWithUTF8String:txt]]; +// [widget setAutoresizingMask: NSViewWidthSizable]; + [[parent contentView] addSubview:widget]; + return widget; +} diff --git a/Cocoa/widgets/window.m b/Cocoa/widgets/window.m new file mode 100644 index 0000000..304fdab --- /dev/null +++ b/Cocoa/widgets/window.m @@ -0,0 +1,190 @@ +#import + +#import "col.h" + + + + +@implementation CocoaWindow : NSWindow + + NSMenuItem* item; + NSMenu* subMenu; + +-(id) initFormWithTitle:(NSString*)title width:(NSInteger)width height:(NSInteger)height +{ + self = [super initWithContentRect: NSMakeRect(0, 0, width, height) + styleMask: (NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable) + // styleMask: (NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask) + backing: NSBackingStoreBuffered + defer: NO]; + if (self) { + SaneView *sv; + sv = [[SaneView alloc] initWithFrame: [[self contentView] frame]]; + [self setContentView:sv]; + [sv setBackgroundColor: [NSColor windowBackgroundColor]]; + [self setTitle: title]; + [self center]; + [self setMinSize: NSMakeSize(width,height)]; + [self makeKeyAndOrderFront:nil]; + [self createApplicationMenu]; + + + } + return self; +} + +- (void)createApplicationMenu { + // Create MenuBar + id menubar = [[NSMenu new] autorelease]; + id appMenuItem = [[NSMenuItem new] autorelease]; + [menubar addItem:appMenuItem]; + [NSApp setMainMenu:menubar]; + + // Create Application Menu + id appMenu = [[NSMenu new] autorelease]; + id appName = [[NSProcessInfo processInfo] processName]; + + // About Menu + id aboutMenu =[[[NSMenuItem alloc] initWithTitle:[@"About " stringByAppendingString:appName] + action:@selector(orderFrontStandardAboutPanel:) + keyEquivalent:@""] + autorelease]; + [appMenu addItem:aboutMenu]; + + // Preferences Menu + id prefMenu = [[[NSMenuItem alloc] initWithTitle:@"Preferences…" + action:NULL keyEquivalent:@","] + autorelease]; + [prefMenu setTarget:self]; + [appMenu addItem:prefMenu]; + [appMenu addItem:[NSMenuItem separatorItem]]; + + // Show/Hide Menu + id hideMenuItem = [[[NSMenuItem alloc] initWithTitle:[@"Hide " stringByAppendingString:appName] + action:@selector(hide:) + keyEquivalent:@"h"] + autorelease]; + [appMenu addItem:hideMenuItem]; + + id hideOthersMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Hide Others" + action:@selector(hideOtherApplications:) + keyEquivalent:@"h"] + autorelease]; + [hideOthersMenuItem setKeyEquivalentModifierMask:(NSEventModifierFlagOption | NSEventModifierFlagCommand)]; + [appMenu addItem:hideOthersMenuItem]; + + id showMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Show All" + action:@selector(unhideAllApplications:) + keyEquivalent:@""] + autorelease]; + [appMenu addItem:showMenuItem]; + [appMenu addItem:[NSMenuItem separatorItem]]; + + // Quit Menu + id quitMenuItem=[[[NSMenuItem alloc] initWithTitle:@"Quit" + action:@selector(terminate:) + keyEquivalent:@"q"] + autorelease]; + [appMenu addItem:quitMenuItem]; + [appMenuItem setSubmenu:appMenu]; + +} +- (void)applicationDidFinishLaunching: (NSNotification *)notification +{ + [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self]; + // NSString *appName = [[NSProcessInfo processInfo] processName]; + + // NSMenuItem* item; + // NSMenu *subMenu; + // NSMenu *servicesMenu; + // self.menu=[[NSMenu alloc] initWithTitle:@"AMainMenu"]; + + + // //Create the application menu. + // item=[[NSMenuItem alloc] initWithTitle:@"Apple" + // action:NULL + // keyEquivalent:@""]; + // [self.menu addItem:item]; + // subMenu=[[NSMenu alloc] initWithTitle:@"Apple"]; + // [self.menu setSubmenu:subMenu forItem:item]; + // [item release]; + + // // About Menu + // item=[[[NSMenuItem alloc] initWithTitle:[@"About " stringByAppendingString:appName] + // action:@selector(orderFrontStandardAboutPanel:) + // keyEquivalent:@""] + // autorelease]; + // [subMenu addItem:item]; + // [subMenu addItem:[NSMenuItem separatorItem]]; + + // // Preferences Menu + // item = [[[NSMenuItem alloc] initWithTitle:@"Preferences…" + // action:NULL keyEquivalent:@","] + // autorelease]; + // [item setTarget:self]; + // [subMenu addItem:item]; + // [subMenu addItem:[NSMenuItem separatorItem]]; + + // // Services Menu + // // item = [[[NSMenuItem alloc] initWithTitle:@"Services" + // // action:NULL + // // keyEquivalent:@""] + // // autorelease]; + // // servicesMenu = [[[NSMenu alloc] initWithTitle:@"Services"] autorelease]; + // // [item setSubmenu:servicesMenu]; + // // [NSApp setServicesMenu:servicesMenu]; + // // [subMenu addItem:item]; + // // [subMenu addItem:[NSMenuItem separatorItem]]; + + // // Show/Hide Menu + // item = [[[NSMenuItem alloc] initWithTitle:[@"Hide " stringByAppendingString:appName] + // action:@selector(hide:) + // keyEquivalent:@"h"] + // autorelease]; + // [subMenu addItem:item]; + // item = [[[NSMenuItem alloc] initWithTitle:@"Hide Others" + // action:@selector(hideOtherApplications:) + // keyEquivalent:@"h"] + // autorelease]; + // [item setKeyEquivalentModifierMask:(NSEventModifierFlagOption | NSEventModifierFlagCommand)]; + // [subMenu addItem:item]; + // item = [[[NSMenuItem alloc] initWithTitle:@"Show All" + // action:@selector(unhideAllApplications:) + // keyEquivalent:@""] + // autorelease]; + // [subMenu addItem:item]; + // [subMenu addItem:[NSMenuItem separatorItem]]; + + + // // Quit Menu + // item=[[[NSMenuItem alloc] initWithTitle:@"Quit" + // action:@selector(terminate:) + // keyEquivalent:@"q"] + // autorelease]; + // [subMenu addItem:item]; + // [subMenu release]; + // [NSApp setMenu: self.menu]; + // [self.menu release]; + // [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; + + // [self orderFront: self]; +} + +- (BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSNotification *)notification +{ + return YES; +} + +- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification +{ + return YES; +} + +@end + + + + +id createWindow(const char* title, int width, int height) { + return [[[CocoaWindow alloc] initFormWithTitle:[NSString stringWithUTF8String:title] width: width height: height] autorelease]; +} diff --git a/slider.nim b/slider.nim new file mode 100644 index 0000000..df81938 --- /dev/null +++ b/slider.nim @@ -0,0 +1,26 @@ +import Cocoa / [NSWindow, NSFunctions, NSLabel, NSSlider] + +const + width = 320 + height = 120 + +var mainWindow, label, slider: ID + +proc sliderCB(sender: ID) = + label.text = "Slider Value: " & $slider.value + +proc main() = + + Cocoa_Init() + + mainWindow = newWindow("Nim Mac GUI Slider Demo", width,height) + label = newLabel(mainWindow,"Slider Value: 0", 16,20,120,24) + label.anchor = akWidth + slider = newSlider(mainWindow, 16,40,width-40,24, sliderCB) + slider.anchor = akWidth + + + Cocoa_Run() + +when isMainModule: + main() \ No newline at end of file diff --git a/test1.nim b/test1.nim new file mode 100644 index 0000000..7b4a889 --- /dev/null +++ b/test1.nim @@ -0,0 +1,41 @@ +import Cocoa / [NSWindow, NSTextfield, NSButton, NSFunctions, NSCombobox, NSCheckbox, NSLabel, NSSlider, NSTextedit, NSOpenDialog] + +const + width = 800 + height = 600 + +var mainWindow, txt1, btn1, combo, chkbox, label1, slider1, editor: ID + +proc btnClicked(sender:ID) = + let fName = newOpenDialog(mainWindow, "nim") + if fName.len > 0: + txt1.text = fName + let fText = readFile($fName) + editor.text = fText +proc sliderCallback(sender: ID) = + echo label1.text + +proc main() = + Cocoa_Init() + + mainWindow = createWindow("Nim Cocoa Module GUI Test", width, height) + txt1 = newTextField(mainWindow,"Welcome to AIR's Cocoa Demo", 16, 20, width-122, 22) + btn1 = newButton(mainWindow, "Open", width-100, 20, 90, 24, btnClicked) + combo = newComboBox(mainWindow, 16,54,210,26, nil) + for item in ["One", "Two", "Three"]: combo.AddItem(item) + combo.text = "Two" + + chkbox = newCheckBox(mainWindow,"Check Box 1", 240, 54, 100, 24) + label1 = newLabel(mainWindow,"This is a Label", 360, 57, 100, 24) + slider1 = newSlider(mainWindow, 680, 57, 100, 24, sliderCallback) + slider1.value = 1 + editor = newTextEdit(mainWindow, "", 16, 96, width-32, height-120) + + txt1.anchor = akWidth + btn1.anchor = akRight + editor.anchor = akFull + + Cocoa_Run() + +when isMainModule: + main() \ No newline at end of file