From 6269810b98cbe5005643be25c9cb66d9aab4cc47 Mon Sep 17 00:00:00 2001 From: Armando Rivera Date: Sun, 8 Aug 2021 15:33:13 -0400 Subject: [PATCH] Rearraigned to address issue with Nimble --- bundle.nim | 55 ++++ cocoa.nim | 44 +++ cocoa/NSButton.nim | 20 ++ cocoa/NSCheckbox.nim | 5 + cocoa/NSColordialog.nim | 0 cocoa/NSCombobox.nim | 5 + cocoa/NSContainer.nim | 5 + cocoa/NSDialog.nim | 5 + cocoa/NSFunctions.nim | 82 +++++ cocoa/NSLabel.nim | 5 + cocoa/NSLine.nim | 5 + cocoa/NSListbox.nim | 5 + cocoa/NSMenu.nim | 11 + cocoa/NSMessagebox.nim | 5 + cocoa/NSOpendialog.nim | 5 + cocoa/NSRadioButton.nim | 5 + cocoa/NSSavedialog.nim | 5 + cocoa/NSSlider.nim | 5 + cocoa/NSTabView.nim | 8 + cocoa/NSTableview.nim | 9 + cocoa/NSTextedit.nim | 5 + cocoa/NSTextfield.nim | 5 + cocoa/NSWindow.nim | 8 + cocoa/col.nim | 119 +++++++ cocoa/widgets/bind.m | 6 + cocoa/widgets/button.m | 34 ++ cocoa/widgets/checkbox.m | 53 +++ cocoa/widgets/col.h | 630 ++++++++++++++++++++++++++++++++++++ cocoa/widgets/colordialog.m | 47 +++ cocoa/widgets/combobox.m | 44 +++ cocoa/widgets/container.m | 17 + cocoa/widgets/dialog.m | 15 + cocoa/widgets/functions.m | 183 +++++++++++ cocoa/widgets/label.m | 14 + cocoa/widgets/line.m | 24 ++ cocoa/widgets/listbox.m | 85 +++++ cocoa/widgets/menu.m | 176 ++++++++++ cocoa/widgets/messagebox.m | 14 + cocoa/widgets/nsslider.m | 30 ++ cocoa/widgets/opendialog.m | 43 +++ cocoa/widgets/radiobutton.m | 30 ++ cocoa/widgets/saneview.m | 11 + cocoa/widgets/savedialog.m | 48 +++ cocoa/widgets/slider.m | 38 +++ cocoa/widgets/tableview.m | 131 ++++++++ cocoa/widgets/tabview.m | 42 +++ cocoa/widgets/textedit.m | 110 +++++++ cocoa/widgets/textfield.m | 11 + cocoa/widgets/window.m | 117 +++++++ 49 files changed, 2379 insertions(+) create mode 100644 bundle.nim create mode 100644 cocoa.nim 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/NSContainer.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/NSRadioButton.nim create mode 100644 cocoa/NSSavedialog.nim create mode 100644 cocoa/NSSlider.nim create mode 100644 cocoa/NSTabView.nim create mode 100644 cocoa/NSTableview.nim create mode 100644 cocoa/NSTextedit.nim create mode 100644 cocoa/NSTextfield.nim create mode 100644 cocoa/NSWindow.nim create mode 100644 cocoa/col.nim create mode 100644 cocoa/widgets/bind.m 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/container.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/radiobutton.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/tableview.m create mode 100644 cocoa/widgets/tabview.m create mode 100644 cocoa/widgets/textedit.m create mode 100644 cocoa/widgets/textfield.m create mode 100644 cocoa/widgets/window.m diff --git a/bundle.nim b/bundle.nim new file mode 100644 index 0000000..54487b2 --- /dev/null +++ b/bundle.nim @@ -0,0 +1,55 @@ +import json, os, plists, times + +if paramCount() >= 1: + let fname = paramStr(1) + + let dt = now() + + let appAuthor = "Armando I. Rivera" + let appName = fname + let iconFile = "appIcon.icns" + let bundleIdentifier = "net.binarymagic." & appName + let appVersion = "0.1" + let appInfo = appVersion & " Created by " & appAuthor & " on " & dt.format("MM-dd-yyyy") + let appCopyRight = "Copyright" & dt.format(" yyyy ") & appAuthor & ". All rights reserved." + let appBundle = appName & ".app" + + if fname.len > 0: + var pl = %* + + { "CFBundlePackageType" : "APPL", + "CFBundleInfoDictionaryVersion" : "6.0", + "CFBundleName" : appName, + "CFBundleExecutable" : appName, + "CFBundleIconFile" : iconFile , + "CFBundleIdentifier" : bundleIdentifier , + "CFBundleVersion" : appVersion , + "CFBundleGetInfoString" : appInfo, + "CFBundleShortVersionString" : appVersion , + "NSHumanReadableCopyright" : appCopyRight , + "NSPrincipalClass" : "NSApplication" , + "NSMainNibFile" : "MainMenu" + } + + + createDir(appBundle & "/Contents/MacOS") + createDir(appBundle & "/Contents/Resources") + createDir(appBundle & "/Contents/Frameworks") + + if appName.fileExists: + appName.copyFileWithPermissions(appBundle & "/Contents/MacOS/" & appName) + + if iconFile.fileExists: + iconFile.copyFileWithPermissions(appBundle & "/Contents/Resources/" & iconFile) + + if "Credits.rtf".fileExists: + "Credits.rtf".copyFileWithPermissions(appBundle & "/Contents/Resources/Credits.rtf") + + + pl.writePlist(appBundle & "/Contents/Info.plist") + + discard execShellCmd("touch " & appBundle) + discard execShellCmd("open " & appBundle) +else: + echo "Usage: bundle " + diff --git a/cocoa.nim b/cocoa.nim new file mode 100644 index 0000000..1e3ece4 --- /dev/null +++ b/cocoa.nim @@ -0,0 +1,44 @@ +import Cocoa / [NSButton, + NSCheckbox, + NSColordialog, + NSCombobox, + NSContainer, + NSDialog, + NSFunctions, + NSLabel, + NSLine, + NSListbox, + NSMenu, + NSMessagebox, + NSOpendialog, + NSRadioButton, + NSSavedialog, + NSSlider, + NSTabView, + NSTableview, + NSTextedit, + NSTextfield, + NSWindow +] + +export NSButton, + NSCheckbox, + NSColordialog, + NSCombobox, + NSContainer, + NSDialog, + NSFunctions, + NSLabel, + NSLine, + NSListbox, + NSMenu, + NSMessagebox, + NSOpendialog, + NSRadioButton, + NSSavedialog, + NSSlider, + NSTabView, + NSTableview, + NSTextedit, + NSTextfield, + NSWindow diff --git a/cocoa/NSButton.nim b/cocoa/NSButton.nim new file mode 100644 index 0000000..452e81d --- /dev/null +++ b/cocoa/NSButton.nim @@ -0,0 +1,20 @@ +{.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".} +## Creates new Button object +## +## Example: +## +## .. code-block::nim +## btn1 = newButton(mainWindow, "Open", width-100, 20, 90, 24, btnClicked) +## +## +## Callback must be in the form: +## +## .. code-block::nim +## proc buttonCB(sender: ID) {.cdecl.} = +## <..code...> +## + 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/NSContainer.nim b/cocoa/NSContainer.nim new file mode 100644 index 0000000..71c9ec8 --- /dev/null +++ b/cocoa/NSContainer.nim @@ -0,0 +1,5 @@ +{.compile: "widgets/container.m".} + +import NSFunctions + +proc newContainer*(parent: ID, title: cstring; left: cint; top: cint; width: cint; height: cint): ID {.cdecl, importc: "createBox".} \ No newline at end of file diff --git a/cocoa/NSDialog.nim b/cocoa/NSDialog.nim new file mode 100644 index 0000000..cde1073 --- /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..7579286 --- /dev/null +++ b/cocoa/NSFunctions.nim @@ -0,0 +1,82 @@ +{.compile: "widgets/functions.m".} +{.compile: "widgets/bind.m".} + +type + ID* = pointer + ACTION* = proc (a2: ID) {.cdecl.} + + +# WINDOW STYLES +const + NSWindowStyleMaskBorderless* = 0 + NSWindowStyleMaskTitled* = 1 shl 0 + NSWindowStyleMaskClosable* = 1 shl 1 + NSWindowStyleMaskMiniaturizable* = 1 shl 2 + NSWindowStyleMaskResizable* = 1 shl 3 + NSWindowStyleMaskUtilityWindow* = 1 shl 4 + NSWindowStyleMaskDocModalWindow* = 1 shl 6 + NSWindowStyleMaskNonactivatingPanel* = 1 shl 7 + NSWindowStyleMaskUnifiedTitleAndToolbar* = 1 shl 12 + NSWindowStyleMaskHUDWindow* = 1 shl 13 + NSWindowStyleMaskFullScreen* = 1 shl 14 + NSWindowStyleMaskFullSizeContentView* = 1 shl 15 + +# 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 Cocoa_Init*() {.cdecl, importc: "Cocoa_Init".} + +proc Cocoa_Run*(a2: ID) {.cdecl, importc: "Cocoa_Run".} + +proc Cocoa_Quit*(a2: ID) {.cdecl, importc: "Cocoa_Quit".} + +proc Cocoa_About*() {.cdecl, importc: "Cocoa_About".} +proc `anchor=`*(widget: ID; value: cint) {.cdecl, importc: "Pin".} +proc `text=`*(widget: ID; txt: cstring) {.cdecl, importc: "SetText".} + +proc text*(widget: ID): cstring {.cdecl, importc: "GetText".} + +proc value*(widget: ID): cint {.cdecl, importc: "GetValue".} + +proc `value=`*(widget: ID; value: cint) {.cdecl, importc: "SetValue".} + +proc add*(widget: ID; txt: cstring) {.cdecl, importc: "AddItem".} + +proc `action=`*(widget: ID; `func`: ACTION) {.cdecl, importc: "SetAction".} + +proc `state=`*(widget: ID; state: bool) {.cdecl, importc: "SetState".} + +proc state*(widget: ID): cint {.cdecl, importc: "State".} + +proc clear*(widget: ID) {.cdecl, importc: "LB_Clear".} +proc `item=`(widget: ID; pos: cint) {.cdecl, importc: "SetItem".} +proc remove*(widget: ID; pos: cint) {.cdecl, importc: "RemoveItem".} +proc getColor*(panel: ID): cstring {.cdecl, importc: "getColor".} +proc hexColor*(cPanel: ID): cstring {.cdecl, importc: "hexColor".} + +proc Notify*(title: cstring; subtitle: cstring; text: cstring) {.cdecl, importc: "Notify".} + +proc newSeparator*(parent: ID; x: cint; y: cint; width: cint): ID {.cdecl, importc: "createLine".} + +proc link*( widget: ID, widgetIvar: cstring, target: ID, targetIvar: cstring) {.cdecl, importc:"bind".} + +proc `tooltip=`*(widget: ID, tip: cstring) {.cdecl, importc: "setToolTip".} \ 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..f37adf2 --- /dev/null +++ b/cocoa/NSMenu.nim @@ -0,0 +1,11 @@ +{.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".} +proc setSystemAction*(widget: ID, title, actionName: cstring) {.cdecl, importc: "setSystemAction".} +proc newFileMenu*() {.cdecl, importc: "createFileMenu".} +proc newEditMenu*() {.cdecl, importc: "createEditMenu".} +proc newFormatMenu*() {.cdecl, importc: "createFormatMenu".} \ No newline at end of file diff --git a/cocoa/NSMessagebox.nim b/cocoa/NSMessagebox.nim new file mode 100644 index 0000000..1576c47 --- /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/NSRadioButton.nim b/cocoa/NSRadioButton.nim new file mode 100644 index 0000000..f68c583 --- /dev/null +++ b/cocoa/NSRadioButton.nim @@ -0,0 +1,5 @@ +{.compile: "widgets/radiobutton.m".} + +import NSFunctions + +proc newRadioButton*(parent: ID, caption: cstring; left: cint; top: cint; width: cint; height: cint; callback: ACTION): ID {.cdecl, importc: "createRadioButton".} \ No newline at end of file diff --git a/cocoa/NSSavedialog.nim b/cocoa/NSSavedialog.nim new file mode 100644 index 0000000..b579975 --- /dev/null +++ b/cocoa/NSSavedialog.nim @@ -0,0 +1,5 @@ +{.compile: "widgets/savedialog.m".} + +import NSFunctions + +proc newSaveDialog*(parent: ID, title: cstring, 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/NSTabView.nim b/cocoa/NSTabView.nim new file mode 100644 index 0000000..1905984 --- /dev/null +++ b/cocoa/NSTabView.nim @@ -0,0 +1,8 @@ +{.compile: "widgets/tabview.m".} + +import NSFunctions + +proc newTabBox*(parent: ID, label: cstring; left: cint; top: cint; width: cint; height: cint): ID {.cdecl, importc: "createTabView".} + +proc addTab*(parent: ID, label: cstring) {.cdecl, importc: "addTab".} +proc getTab*(parent: ID, label: cstring): ID {.cdecl, importc: "getTab".} \ No newline at end of file diff --git a/cocoa/NSTableview.nim b/cocoa/NSTableview.nim new file mode 100644 index 0000000..7d6f985 --- /dev/null +++ b/cocoa/NSTableview.nim @@ -0,0 +1,9 @@ +{.compile: "widgets/tableview.m".} + +import NSFunctions + +proc newTableview*(parent: ID, left, top, width, height: cint): ID {.cdecl, importc: "createTableView".} +proc addColumn*(parent: ID, name: cstring) {.cdecl, importc: "newTableColumn".} +proc addRow*(parent: ID, person: cstring) {.cdecl, importc: "addRow".} +proc saveTableView*(tview: ID, path: cstring) {.cdecl, importc: "tableviewSaveToFile".} +proc loadTableView*(tview: ID, path: cstring) {.cdecl, importc: "tableviewLoadFromFile".} \ 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..14a1524 --- /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, style: int): ID {.importc: "createWindow".} diff --git a/cocoa/col.nim b/cocoa/col.nim new file mode 100644 index 0000000..f6ee1ae --- /dev/null +++ b/cocoa/col.nim @@ -0,0 +1,119 @@ +{.pragma: libcol_s, cdecl.} +{.passL: "-fobjc-arc -framework Cocoa -lcol_s"} + + +type + id* = pointer + ACTION* = proc (a1: id) {.cdecl.} + + +const + akNone* = 0 + akRight* = 1 + akWidth* = 2 + akLeft* = 4 + akBottom* = 8 + akHeight* = 16 + akFull* = 18 + akTop* = 32 + + +proc newWindow*(title: cstring; width: cint; height: cint): id {.cdecl, importc: "createWindow".} + +proc Pin(widget: id; value: cint) {.cdecl, importc: "Pin".} + +proc Cocoa_Init*() {.cdecl, importc: "Cocoa_Init".} + +proc Cocoa_Run*(a2: id) {.cdecl, importc: "Cocoa_Run".} + +proc Cocoa_Quit*(a2: id) {.cdecl, importc: "Cocoa_Quit".} + +proc Cocoa_About*() {.cdecl, importc: "Cocoa_About".} + +proc SetText(widget: id; txt: cstring) {.cdecl, importc: "SetText".} + +proc GetText(widget: id): cstring {.cdecl, importc: "GetText".} + +proc GetValue(widget: id): cint {.cdecl, importc: "GetValue".} + +proc SetValue(widget: id; value: cint) {.cdecl, importc: "SetValue".} + +proc AddItem(widget: id; txt: cstring) {.cdecl, importc: "AddItem".} + +proc newTextField*(parent: id; txt: cstring; l: cint; t: cint; w: cint; h: cint): id {.cdecl, importc: "createTextField".} + +proc newComboBox*(parent: id; l: cint; t: cint; w: cint; h: cint; callback: ACTION): id {.cdecl, importc: "createComboBox".} + +proc newButton*(parent: id; caption: cstring; l: cint; t: cint; w: cint; h: cint; `func`: ACTION): id {.cdecl, importc: "createButton".} + +proc SetAction(widget: id; `func`: ACTION) {.cdecl, importc: "SetAction".} +proc newCheckBox*(parent: id; caption: cstring; l: cint; t: cint; w: cint; h: cint): id {.cdecl, importc: "createCheckBox".} + +proc SetState(widget: id; state: bool) {.cdecl, importc: "SetState".} + +proc State(widget: id): cint {.cdecl, importc: "State".} + +proc newListBox*(parent: id; l: cint; t: cint; w: cint; h: cint): id {.cdecl, importc: "createListBox".} + +proc Clearitems*(widget: id) {.cdecl, importc: "LB_Clear".} +proc SetItem*(widget: id; pos: cint) {.cdecl, importc: "SetItem".} +proc RemoveItem*(widget: id; pos: cint) {.cdecl, importc: "RemoveItem".} +proc newOpenDialog*(parent: id; types: cstring): cstring {.cdecl, importc: "createOpenDialog".} + +proc newSaveDialog*(parent: id; title: cstring; types: cstring): cstring {.cdecl, importc: "createSaveDialog".} + +proc newDialog*(title: cstring; message: cstring; `type`: cint): cint {.cdecl, importc: "createDialog".} +proc newMessageBox*(title: cstring; message: cstring; `type`: cint): cint {.cdecl, importc: "createMessageBox".} + +proc newColorDialog*(): id {.cdecl, importc: "createColorDialog".} +proc getColor*(panel: id): cstring {.cdecl, importc: "getColor".} +proc hexColor*(cPanel: id): cstring {.cdecl, importc: "hexColor".} +proc newLabel*(parent: id; txt: cstring; l: cint; t: cint; w: cint; h: cint): id {.cdecl, importc: "createLabel".} + +proc newTextEdit*(parent: id; txt: cstring; l: cint; t: cint; w: cint; h: cint): id {.cdecl, importc: "createTextEdit".} + +proc Notify*(title: cstring; subtitle: cstring; text: cstring) {.cdecl, importc: "Notify".} + +proc newSeparator*(parent: id; x: cint; y: cint; width: cint): id {.cdecl, importc: "createLine".} +proc newMenu*(Title: cstring): id {.cdecl, importc: "createMenu".} +proc newMenuItem*(parent: id; caption: cstring; key: cstring; callback: ACTION) {.cdecl, importc: "createMenuItem".} +proc newMenuSeparator*(parent: id) {.cdecl, importc: "createMenuSeparator".} +proc newSlider*(parent: id; left: cint; top: cint; width: cint; height: cint; callback: ACTION): id {.cdecl, importc: "createSlider".} + +proc `text=`*(widget: id; txt: cstring) = + widget.SetText(txt) + +proc `reset`*(widget: id) = + widget.SetText("") + +proc `text`*(widget: id): string = + $GetText(widget) + +# proc `item`*(widget: id): cstring = +# GetItem(widget) + +proc `anchor=`*(widget: id; value: cint) = + widget.Pin(value) + +proc `state`*(widget: id): cint = + widget.State() + +proc `state=`*(widget: id; val: bool) = + widget.SetState(val) + +proc `item=`*(widget: id, val: cstring) = + widget.AddItem(val) + +proc `item`*(widget: id): string = + $GetText(widget) + +proc action*(widget: id, action:ACTION) = + SetAction(widget, action) + +proc value*(widget: id, value: cint) = + SetValue(widget, value) + +proc value*(widget: id): cint = + GetValue(widget) + +# #endif \ No newline at end of file diff --git a/cocoa/widgets/bind.m b/cocoa/widgets/bind.m new file mode 100644 index 0000000..3fc2279 --- /dev/null +++ b/cocoa/widgets/bind.m @@ -0,0 +1,6 @@ +#import + + +void bind( id widget, const char * widgetIvar, id target, const char *targetIvar) { + [widget bind: [NSString stringWithUTF8String: widgetIvar] toObject: target withKeyPath:[NSString stringWithUTF8String: targetIvar] options:nil]; +} \ No newline at end of file diff --git a/cocoa/widgets/button.m b/cocoa/widgets/button.m new file mode 100644 index 0000000..3392883 --- /dev/null +++ b/cocoa/widgets/button.m @@ -0,0 +1,34 @@ +#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:)]; + } + addToParent(parent, widget); + + return widget; +} + diff --git a/cocoa/widgets/checkbox.m b/cocoa/widgets/checkbox.m new file mode 100644 index 0000000..7cdf252 --- /dev/null +++ b/cocoa/widgets/checkbox.m @@ -0,0 +1,53 @@ +#import + +#import "col.h" + +@interface CocoaCheckbox : NSButton + { + int state; + } + @property int state; + - (void) click:(id)sender; +@end + +@implementation CocoaCheckbox + + @synthesize state; + + - (id)initWithFrame:(NSRect)frame { + self = [super initWithFrame:frame]; + if (self) { + [self setButtonType:NSSwitchButton]; + [self setBezelStyle: 0]; + + [self setTarget: self]; + [self setAction: @selector(click:)]; + } + return self; + } + + - (void) click:(id)sender { + self.state = [self intValue]; + } +@end + +id createCheckBox(id parent, const char* caption, int l, int t, int w, int h){ + id self = [[[CocoaCheckbox alloc] initWithFrame:NSMakeRect( l, t, w, h )] autorelease]; + [self setTitle: [NSString stringWithUTF8String:caption]]; + + addToParent(parent, 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..9a729c0 --- /dev/null +++ b/cocoa/widgets/col.h @@ -0,0 +1,630 @@ +#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); + */ + + typedef void(*EVENT_ACTION)(id, const char*); + + 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 windowStyle: (NSWindowStyleMask) style; + - (void)createApplicationMenu; + @end + + @interface awColorPanel: NSColorPanel { + NSColor* theColor; + NSModalSession modalSession; + } + @property (copy) NSColor* theColor; + + - (void)colorUpdate:(NSColorPanel*)colorPanel; + + -(void)changeColor:(id)sender; + + - (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 CocoaTableView : NSScrollView + { + NSTableView *tbl; + NSTableColumn *tblc; + NSTableColumn *tbld; + NSMutableArray *db; + NSMutableDictionary *dict; + + + int cnt; + } + @property (retain) NSTableView *tbl; + @property (retain) NSMutableArray *db; + @property (retain) NSMutableDictionary *dict; + + + - (id)initWithFrame:(NSRect)frame; + - (void)doubleclicked:(id)sender; + - (void)selected:(id)sender; + @end + + @interface CocoaButton: NSButton{ + ACTION buttonAction; + EVENT_ACTION eventAction; + } + @property ACTION buttonAction; + @property EVENT_ACTION eventAction; + + + - (void) click:(id)sender; + - (void)mouseEntered:(NSEvent *)theEvent; + - (void)mouseExited:(NSEvent *)theEvent; + @end + + @interface RadioButton: 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, NSWindowStyleMask style); + /** + \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 createRadioButton(id parent, const char* caption, int l, int t, int w, int h, ACTION func); + /** + \brief Creates RadioButton 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 RadioButton + */ + + 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(); + id getColor(id panel); + void setColor(id target, id source); + + 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); + + void addToParent(id parent, id child); + + id createBox(id parent, const char* title, int x, int y, int width, int height); + + id createRadioButton(id parent, const char* caption, int l, int t, int w, int h, ACTION func); + + id createTabView(id parent, const char *label, int x, int y, int width, int height); + void addTab(id parent, const char *name); + id getTab(id parent, const char *name); + + void eventAction(id widget, EVENT_ACTION callback); + +#ifdef __cplusplus + } +#endif + +// #endif diff --git a/cocoa/widgets/colordialog.m b/cocoa/widgets/colordialog.m new file mode 100644 index 0000000..c85847d --- /dev/null +++ b/cocoa/widgets/colordialog.m @@ -0,0 +1,47 @@ +#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]; +} + +id getColor(id panel) { + return theColor; +} + +void setColor(id target, id source) { + [target setBackgroundColor: [source color]]; +} \ No newline at end of file diff --git a/cocoa/widgets/combobox.m b/cocoa/widgets/combobox.m new file mode 100644 index 0000000..fe81f48 --- /dev/null +++ b/cocoa/widgets/combobox.m @@ -0,0 +1,44 @@ +#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:)]; + } + + addToParent(parent, widget); + + return widget; +} + diff --git a/cocoa/widgets/container.m b/cocoa/widgets/container.m new file mode 100644 index 0000000..47ec76a --- /dev/null +++ b/cocoa/widgets/container.m @@ -0,0 +1,17 @@ +#import + +#import "col.h" + + +id createBox(id parent, const char* title, int x, int y, int width, int height) { + id widget = [[[NSBox alloc] initWithFrame: NSMakeRect(x, y, width, height)] autorelease]; + [widget setTitle: [NSString stringWithUTF8String: title]]; + + // Do we need to create a contentView??? + id sv = [[[SaneView alloc] initWithFrame: NSZeroRect] autorelease]; + [sv setBackgroundColor: [NSColor windowBackgroundColor]]; + [widget setContentView: sv]; + [widget setBorderType: NSLineBorder]; + addToParent(parent, widget); + return widget; +} \ No newline at end of file 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..e734204 --- /dev/null +++ b/cocoa/widgets/functions.m @@ -0,0 +1,183 @@ +#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"] || [widgetClass isEqualToString:@"CocoaTableView"]) + { + [[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 documentView] string] length] > 0) { + return [[[widget documentView] 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]; + + /* RadioButton, Box */ + }else if ([widgetClass isEqualToString: @"RadioButton"] || [widgetClass isEqualToString: @"NSBox"]) { + return [[widget title] 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]]; + + } + +} + +void addToParent(id parent, id child) { + NSString *widgetClass = [[parent class] description]; + if ([widgetClass isEqualToString:@"NSTabViewItem"]) { + [[parent view] addSubview: child]; + } else { + [[parent contentView] addSubview: child]; + } +} + +void setToolTip(id widget, const char *tooltip) { + [widget setToolTip: [NSString stringWithUTF8String: tooltip]]; +} + +void eventAction(id widget, EVENT_ACTION callback) { + [widget setEventAction: callback]; +} + +void setSystemAction(id widget, const char *Title, const char *actionName) { + SEL selector = NSSelectorFromString(NSSTR(actionName)); + id menuItem = [widget itemWithTitle: NSSTR(Title)]; + NSLog(@"%d",[menuItem respondsToSelector: selector]); + if (selector) { + [menuItem setAction: @selector(selector)]; + } +} diff --git a/cocoa/widgets/label.m b/cocoa/widgets/label.m new file mode 100644 index 0000000..4bceefc --- /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]; + addToParent(parent, widget); + return widget; +} diff --git a/cocoa/widgets/line.m b/cocoa/widgets/line.m new file mode 100644 index 0000000..a32d381 --- /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; + addToParent(parent, 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..6921b05 --- /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]; + addToParent(parent, self); + return self; +} diff --git a/cocoa/widgets/menu.m b/cocoa/widgets/menu.m new file mode 100644 index 0000000..57e7396 --- /dev/null +++ b/cocoa/widgets/menu.m @@ -0,0 +1,176 @@ +#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]; + [item setIdentifier: Title]; + + 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]; +} + +- (id) createFileMenu { + NSMenuItem *newMenuItem = [[NSMenuItem alloc] initWithTitle:@"File" action:NULL keyEquivalent:@""]; + + NSMenu *fileMenu = [[NSMenu alloc] initWithTitle:@"File"]; + + id newMenu =[[[NSMenuItem alloc] initWithTitle:@"New" + action:@selector(newDocument:) + keyEquivalent:@"n"] + autorelease]; + [fileMenu addItem:newMenu]; + + [newMenuItem setSubmenu: fileMenu]; + [[[NSApplication sharedApplication] mainMenu] addItem: newMenuItem]; + + +} + +- (id) createEditMenu { + NSString *appName = [[NSProcessInfo processInfo] processName]; + NSMenuItem *newMenuItem = [[NSMenuItem alloc] initWithTitle:@"Edit" action:NULL keyEquivalent:@""]; + + NSMenu *editMenu = [[NSMenu alloc] initWithTitle:@"Edit"]; + + id undoMenu =[[[NSMenuItem alloc] initWithTitle:@"Undo" + action:@selector(undo:) + keyEquivalent:@"z"] + autorelease]; + [editMenu addItem:undoMenu]; + + // ; + + id redoMenu =[[[NSMenuItem alloc] initWithTitle:@"Redo" + action:@selector(redo:) + keyEquivalent:@"Z"] + autorelease]; + [editMenu addItem:redoMenu]; + + [editMenu addItem: [NSMenuItem separatorItem]]; + + id cutMenu =[[[NSMenuItem alloc] initWithTitle:@"Cut" + action:@selector(cut:) + keyEquivalent:@"x"] + autorelease]; + [editMenu addItem:cutMenu]; + + + id copyMenu =[[[NSMenuItem alloc] initWithTitle:@"Copy" + action:@selector(copy:) + keyEquivalent:@"c"] + autorelease]; + [editMenu addItem:copyMenu]; + + + + id pasteMenu =[[[NSMenuItem alloc] initWithTitle:@"Paste" + action:@selector(paste:) + keyEquivalent:@"v"] + autorelease]; + [editMenu addItem:pasteMenu]; + + id pasteAndMatchMenu =[[[NSMenuItem alloc] initWithTitle:@"Paste and Match Style" + action:@selector(pastAsPlainText:) + keyEquivalent:@""] + autorelease]; + [editMenu addItem:pasteAndMatchMenu]; + + id selectAllMenu =[[[NSMenuItem alloc] initWithTitle:@"Select All" + action:@selector(selectAll:) + keyEquivalent:@"a"] + autorelease]; + [editMenu addItem:selectAllMenu]; + + // [newMenuItem setSubmenu: selectAllMenu]; + + // [editMenu addItem: [NSMenuItem separatorItem]]; + [newMenuItem setSubmenu: editMenu]; + [[[NSApplication sharedApplication] mainMenu] addItem: newMenuItem]; +} + +- (id) createFormatMenu{ + + NSFontManager *fontManager = [NSFontManager sharedFontManager]; + [fontManager setDelegate: self]; + NSMenu *fontMenu = [fontManager fontMenu:YES]; + // [[[NSApplication sharedApplication] mainMenu] addItem: fontManager]; + + NSMenuItem *newMenuItem = [[NSMenuItem alloc] initWithTitle:@"Format" action:NULL keyEquivalent:@""]; + + NSMenu *blah = [[NSMenu alloc] initWithTitle: @"Format"]; + [newMenuItem setSubmenu: blah]; + + [newMenuItem setSubmenu: fontMenu]; + [[[NSApplication sharedApplication] mainMenu] addItem: newMenuItem]; +} +@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]; +} + +id createFileMenu() { + id widget = [CocoaMenu new]; + [widget createFileMenu]; + return widget; +} + +id createFormatMenu() { + id widget = [CocoaMenu new]; + [widget createFormatMenu]; + return widget; +} +id createEditMenu() { + id widget = [CocoaMenu new]; + [widget createEditMenu]; + return widget; +} \ No newline at end of file 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/radiobutton.m b/cocoa/widgets/radiobutton.m new file mode 100644 index 0000000..3b3a879 --- /dev/null +++ b/cocoa/widgets/radiobutton.m @@ -0,0 +1,30 @@ +#import + +#import "col.h" + +@implementation RadioButton + + @synthesize buttonAction; + + + - (void) click:(id)sender { + buttonAction(sender); + } +@end + +id createRadioButton(id parent, const char* caption, int l, int t, int w, int h, ACTION func){ + NSButton *self = [[[RadioButton alloc] initWithFrame:NSMakeRect( l, t, w, h )] autorelease]; + [self setButtonType: NSButtonTypeRadio]; + [self setBezelStyle: 0]; + [self setTitle: [NSString stringWithUTF8String:caption]]; + [self setTarget: self]; + + if (func) { + [self setButtonAction:func]; + [self setAction: @selector(click:)]; + } + + addToParent(parent, self); + + return self; +} \ No newline at end of file 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..4f6a4a0 --- /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..4691f98 --- /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]; + + addToParent(parent, widget); + return widget; +} diff --git a/cocoa/widgets/tableview.m b/cocoa/widgets/tableview.m new file mode 100644 index 0000000..5d60312 --- /dev/null +++ b/cocoa/widgets/tableview.m @@ -0,0 +1,131 @@ +#import + +#import "col.h" + +#ifndef NSSTR + #define NSSTR(txt) [NSString stringWithUTF8String:txt] +#endif + + + +@implementation CocoaTableView + +@synthesize tbl; +@synthesize db; +@synthesize dict; + +- (id)initWithFrame:(NSRect)frame { + self = [super initWithFrame:frame]; + db = [NSMutableArray new]; + dict = [NSMutableDictionary new]; + + if (self) { + + tbl = [[NSTableView alloc] initWithFrame: NSMakeRect(5,40,335,185)]; + [tbl setGridStyleMask: NSTableViewGridNone]; + [tbl setUsesAlternatingRowBackgroundColors: YES]; + [tbl setAllowsColumnSelection: YES]; + [tbl setAllowsColumnReordering: YES]; + [tbl setAllowsEmptySelection: NO]; + [tbl setAllowsMultipleSelection: NO]; + [tbl setColumnAutoresizingStyle: NSTableViewUniformColumnAutoresizingStyle]; + + [tbl setDataSource: self]; + [tbl setDelegate: self]; + [tbl setTarget:self]; + [tbl setDoubleAction:@selector(doubleclicked:)]; + + [self setHasHorizontalScroller: YES]; + [self setHasVerticalScroller: YES]; + [self setDocumentView: tbl]; + [self setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; + + + [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; + // return dict.count; +} + +-(id)tableView:(NSTableView *)tableView + objectValueForTableColumn:(NSTableColumn *)tableColumn + row:(NSInteger)row +{ + // NSString *columnIdentifier = [tableColumn identifier]; + + return [self.db[row] valueForKey: [tableColumn identifier]]; + // return [self.dict valueForKey: [tableColumn identifier]]; + +} + + +@end + + +id createTableView(id parent,int l, int t, int w, int h){ + id self = [[[CocoaTableView alloc] initWithFrame:NSMakeRect( l, t, w, h )] autorelease]; + addToParent(parent, self); + return self; +} + +void addRow(id parent, const char *person) { + NSArray *tmpArray = [NSSTR(person) componentsSeparatedByString: @"|"]; + NSMutableDictionary *myDic = [NSMutableDictionary new]; + NSArray *columns = [[parent tbl] tableColumns]; + + for (int i = 0; i < tmpArray.count; i++) { + NSString *ident = [[parent tbl] tableColumns][i].identifier; + [myDic setObject: tmpArray[i] forKey: ident]; + } + + [[parent db] addObject: myDic]; + + [[parent tbl] reloadData]; +} + +void newTableColumn(id parent, const char *name) { + NSString *title = [NSString stringWithUTF8String: name]; + id tCol = [[NSTableColumn alloc] initWithIdentifier: [title lowercaseString]]; + [tCol setMinWidth: 200]; + [tCol setTitle: title]; + + [[parent tbl] addTableColumn: tCol]; +} + +void tableviewSaveToFile(id tview, const char *path) { + + [[tview db] writeToFile: NSSTR(path) atomically:YES]; +} + +void tableviewLoadFromFile(id tview, const char *path) { + NSArray *fileArrary = [NSArray arrayWithContentsOfFile: NSSTR(path)]; + [[tview db] removeAllObjects]; + for (NSMutableDictionary *x in fileArrary) { + [[tview db] addObject: x]; + } + + [[tview tbl] reloadData]; +} \ No newline at end of file diff --git a/cocoa/widgets/tabview.m b/cocoa/widgets/tabview.m new file mode 100644 index 0000000..3e0291b --- /dev/null +++ b/cocoa/widgets/tabview.m @@ -0,0 +1,42 @@ +#import + +#import "col.h" + + +id createTabView(id parent, const char *label, int x, int y, int width, int height) { + NSString *str = [NSString stringWithUTF8String:label]; + NSArray *items = [str componentsSeparatedByString: @"|"]; + + id *widget = [[[NSTabView alloc] initWithFrame:NSMakeRect(x,y,width,height)] autorelease]; + + for(NSString *x in items) { + id sv = [[[SaneView alloc] initWithFrame: NSZeroRect] autorelease]; + [sv setBackgroundColor: [NSColor windowBackgroundColor]]; + + id *tabwidget = [[[NSTabViewItem alloc] initWithIdentifier: x ] autorelease]; + [tabwidget setLabel: x]; + [tabwidget setView: sv]; + [widget addTabViewItem: tabwidget]; + } + + [[parent contentView] addSubview:widget]; + return widget; +} + +void addTab(id parent, const char *name) { + NSString *str = [NSString stringWithUTF8String:name]; + NSArray *items = [str componentsSeparatedByString: @"|"]; + + for(NSString *x in items){ + id *widget = [[[NSTabViewItem alloc] initWithIdentifier: x ] autorelease]; + [widget setLabel: x]; + [parent addTabViewItem: widget]; + } +} + +id getTab(id parent, const char *name) { + NSString *str = [NSString stringWithUTF8String:name]; + NSInteger index = [parent indexOfTabViewItemWithIdentifier: str]; + // NSLog(@"%@", [[parent tabViewItemAtIndex: index] view]); + return [parent tabViewItemAtIndex: index]; +} diff --git a/cocoa/widgets/textedit.m b/cocoa/widgets/textedit.m new file mode 100644 index 0000000..1076452 --- /dev/null +++ b/cocoa/widgets/textedit.m @@ -0,0 +1,110 @@ +#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.widget setAllowsUndo: YES]; + + [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); + + // NSLog(@"Keycod: %hu", [theEvent keyCode]); + 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; + } + } + // }else if (([theEvent type] == NSEventTypeKeyDown) && ([theEvent modifierFlags] & NSEventModifierFlagCommand & NSEventModifierFlagShift)) { + // 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; + + // if (keyCode == 6) { + // if ([[textView undoManager] canRedo]) + // { + // [[textView undoManager] redo]; + // 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]; + } + + addToParent(parent, widget); + return widget; +} + diff --git a/cocoa/widgets/textfield.m b/cocoa/widgets/textfield.m new file mode 100644 index 0000000..f51a9c0 --- /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]; + addToParent(parent, widget); + return widget; +} diff --git a/cocoa/widgets/window.m b/cocoa/widgets/window.m new file mode 100644 index 0000000..e539884 --- /dev/null +++ b/cocoa/widgets/window.m @@ -0,0 +1,117 @@ +#import + +#import "col.h" + + + + +@implementation CocoaWindow : NSWindow + + NSMenuItem* item; + NSMenu* subMenu; + +-(id) initFormWithTitle:(NSString*)title width:(NSInteger)width height:(NSInteger)height windowStyle: (NSWindowStyleMask) style +{ + self = [super initWithContentRect: NSMakeRect(0, 0, width, height) + styleMask: style //(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 setInitialFirstResponder: nil]; + [self setAutorecalculatesKeyViewLoop: YES]; + [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]; + +} + +- (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, NSWindowStyleMask style) { + return [[[CocoaWindow alloc] initFormWithTitle:[NSString stringWithUTF8String:title] width: width height: height windowStyle: style] autorelease]; +}