Author Topic: Apple Swift on Ubuntu  (Read 48333 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3493
    • ScriptBasic Open Source Project
Re: Apple Swift on Ubuntu
« Reply #15 on: December 09, 2015, 10:41:06 AM »
I got it to work.

Code: Text
  1. import Foundation
  2. import Glibc
  3.  
  4. IupOpen(nil, nil)
  5.  
  6. var dlg = IupCreate("dialog")
  7. var vbx = IupCreate("vbox")
  8. var lbl = IupCreate("label")
  9.  
  10. IupStoreAttribute(dlg, "TITLE", "Swift IUP")
  11. IupStoreAttribute(lbl, "TITLE", "Hello world from Swift IUP.")
  12.  
  13. IupAppend(vbx, lbl)
  14. IupAppend(dlg, vbx)
  15.  
  16. IupShowXY(dlg, IUP_CENTER, IUP_CENTER)
  17.  
  18. IupMainLoop()
  19.  
  20. IupClose()
  21.  

Compiles, no errors.
« Last Edit: December 09, 2015, 10:48:50 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3493
    • ScriptBasic Open Source Project
Re: Apple Swift on Ubuntu
« Reply #16 on: December 09, 2015, 02:48:28 PM »
AIR,

I'm trying to get a callback function pointer but it seems elusive.

Code: Text
  1. import Foundation
  2. import Glibc
  3.  
  4. func btn_exit_cb(ih: Int32) -> Int32 {
  5.   return IUP_CLOSE
  6. }
  7.  
  8. IupOpen(nil, nil)
  9.  
  10. var dlg = IupCreate("dialog")
  11. var vbx = IupCreate("vbox")
  12. var lbl = IupCreate("label")
  13. var but = IupCreate("button")
  14.  
  15. IupStoreAttribute(dlg, "TITLE", "Swift IUP")
  16. IupStoreAttribute(lbl, "TITLE", "Hello world from Swift IUP.")
  17. IupStoreAttribute(but, "TITLE", "Quit")
  18.  
  19. IupAppend(vbx, lbl)
  20. IupAppend(vbx, but)
  21. IupAppend(dlg, vbx)
  22.  
  23. IupSetCallback(but, "ACTION", CUseCallback(btn_exit_cb, 1))
  24.  
  25. IupShowXY(dlg, IUP_CENTER, IUP_CENTER)
  26.  
  27. IupMainLoop()
  28.  
  29. IupClose()
  30.  


jrs@laptop:~/Swift/usr/share/examples$ swiftc iuphw2.swift -import-objc-header /usr/include/iup/iup.h -liup -o iuphw2
iuphw2.swift:23:31: error: use of unresolved identifier 'CUseCallback'
IupSetCallback(but, "ACTION", CUseCallback(btn_exit_cb, 1))
                              ^~~~~~~~~~~~
jrs@laptop:~/Swift/usr/share/examples$


Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Apple Swift on Ubuntu
« Reply #17 on: December 09, 2015, 06:25:48 PM »
Code: Text
  1. import Foundation
  2. import Glibc
  3.  
  4. func btn_exit_cb(ih: COpaquePointer) -> Int32 {
  5.   return IUP_CLOSE
  6. }
  7.  
  8. IupOpen(nil, nil)
  9.  
  10. var dlg = IupCreate("dialog")
  11. var vbx = IupCreate("vbox")
  12. var lbl = IupCreate("label")
  13. var but = IupCreate("button")
  14.  
  15. IupStoreAttribute(dlg, "TITLE", "Swift IUP")
  16. IupStoreAttribute(lbl, "TITLE", "Hello world from Swift IUP.")
  17. IupStoreAttribute(but, "TITLE", "Quit")
  18.  
  19. IupAppend(vbx, lbl)
  20. IupAppend(vbx, but)
  21. IupAppend(dlg, vbx)
  22.  
  23. IupSetCallback(but, "ACTION", btn_exit_cb)
  24.  
  25. IupShowXY(dlg, IUP_CENTER, IUP_CENTER)
  26.  
  27. IupMainLoop()
  28.  
  29. IupClose()
  30.  

Offline John

  • Forum Support / SB Dev
  • Posts: 3493
    • ScriptBasic Open Source Project
Re: Apple Swift on Ubuntu
« Reply #18 on: December 09, 2015, 06:38:39 PM »
Thanks AIR!

It closes the window when the quit button is clicked. This shows the core IUP functionality works in Swift. I'll try building on this.

Swift sure makes the syntax simple to use.



Code: Text
  1. /*
  2. Linux Swift IUP Hello World Example
  3. Screen Shot: http://www.johnspikowski.com/Swift-C/swiftiuphello.png
  4. Compile: swiftc iuphw.swift -import-objc-header /usr/include/iup/iup.h -liup -o iuphw
  5. Contributors: John Spikowski, Armando I. Rivera (AIR) 2015-12-09
  6. */
  7.  
  8. import Foundation
  9. import Glibc
  10.  
  11. func btn_exit_cb(ih: COpaquePointer) -> Int32 {
  12.   return IUP_CLOSE
  13. }
  14.  
  15. IupOpen(nil, nil)
  16.  
  17. var dlg = IupCreate("dialog")
  18. var vbx = IupCreate("vbox")
  19. var lbl = IupCreate("label")
  20. var but = IupCreate("button")
  21.  
  22. IupStoreAttribute(dlg, "TITLE", "Swift IUP")
  23. IupSetAttributes(vbx, "MARGIN=10x10, ALIGNMENT=ACENTER")
  24. IupSetAttributes(lbl, "FONT=\"Arial, 24\", TITLE=\"Hello World\"")
  25. IupStoreAttribute(but, "TITLE", " Quit ")
  26.  
  27. IupAppend(vbx, lbl)
  28. IupAppend(vbx, but)
  29. IupAppend(dlg, vbx)
  30.  
  31. IupSetCallback(but, "ACTION", btn_exit_cb)
  32.  
  33. IupShowXY(dlg, IUP_CENTER, IUP_CENTER)
  34.  
  35. IupMainLoop()
  36.  
  37. IupClose()
  38.  


jrs@laptop:~/Swift/usr/share/examples$ swiftc iuphw.swift -import-objc-header /usr/include/iup/iup.h -liup -o iuphw
jrs@laptop:~/Swift/usr/share/examples$ ./iuphw


Attached is a screenshot of editing a Swift programs in UltraEdit and gedit.
« Last Edit: December 10, 2015, 02:31:26 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3493
    • ScriptBasic Open Source Project
Re: Apple Swift on Ubuntu
« Reply #19 on: December 11, 2015, 02:07:54 PM »
AIR,

Here is an example of using a IUP LED screen definition to define the UI. The questions I have is will Swift support a multi-line string and how does LED process the defined callback function references? What would be cool is a string array of screen UI definitions and concatenated (assembled) just before the display.

Update
Quote
Unfortunately, swift doesn't appear to allow you to have a single literal over multiple lines but you can add + literals together over multiple lines.



Code: ScriptBasic
  1. ' Script BASIC IUP/LED
  2.  
  3. IMPORT iup.bas
  4. sample_led = """
  5.  
  6. img1 = IMAGE[0="0 0 0",1="BGCOLOR",2="255 0 0"]
  7. (32,32
  8. ,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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,2,2,2
  71. ,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2
  72. ,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
  73. ,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
  74. ,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
  75. ,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
  76. )
  77.  
  78. mnu = MENU
  79. (
  80.  SUBMENU("IupSubMenu 1",
  81.    MENU
  82.    (
  83.      ITEM[VALUE=ON]("IupItem 1 Checked",myaction),
  84.      SEPARATOR(),
  85.      ITEM[ACTIVE="NO"]("IupItem 2 Disabled",myaction)
  86.    )
  87.  ),
  88.  ITEM("IupItem 3",myaction),
  89.  ITEM("IupItem 4",myaction)
  90. )
  91.  
  92. dlg = DIALOG[TITLE="IupDialog Title",MENU="mnu"]
  93. (
  94.  VBOX[GAP="5",ALIGNMENT="ARIGHT",MARGIN="5x5"]
  95.  (
  96.    HBOX
  97.    (
  98.      FRAME[TITLE="IupButton"]
  99.      (
  100.        VBOX
  101.        (
  102.          BUTTON("Button Text",myaction),
  103.          BUTTON[IMAGE=img1]("",myaction),
  104.          BUTTON[IMAGE=img1,IMPRESS=img2]("",myaction)
  105.        )
  106.      ),
  107.      FRAME[TITLE="IupLabel"]
  108.      (
  109.        VBOX
  110.        (
  111.          LABEL("Label Text"),
  112.          LABEL[SEPARATOR=HORIZONTAL](""),
  113.          LABEL[IMAGE=img1]("")
  114.        )
  115.      ),
  116.      FRAME[TITLE="IupToggle"]
  117.      (
  118.        VBOX
  119.        (
  120.          TOGGLE[VALUE="ON"]("Toggle Text",myaction),
  121.          TOGGLE[IMAGE=img1,IMPRESS=img2]("",myaction),
  122.          FRAME[TITLE="IupRadio"]
  123.          (
  124.            RADIO
  125.            (
  126.              VBOX
  127.              (
  128.                TOGGLE("Toggle Text",myaction),
  129.                TOGGLE("Toggle Text",myaction)
  130.              )
  131.            )
  132.          )
  133.        )
  134.      ),
  135.      FRAME[TITLE="IupText/IupMultiline"]
  136.      (
  137.        VBOX
  138.        (
  139.          TEXT[SIZE="80x",VALUE="IupText Text"](myaction),
  140.          MULTILINE[SIZE="80x60",EXPAND="YES",VALUE="IupMultiline Text\nSecond Line\nThird Line"](myaction)
  141.        )
  142.      ),
  143.      FRAME[TITLE="IupList"]
  144.      (
  145.        VBOX
  146.        (
  147.          LIST[EXPAND="YES",VALUE="1",1="Item 1 Text",2="Item 2 Text",3="Item 3 Text"](myaction),
  148.          LIST[DROPDOWN="YES",EXPAND="YES",VALUE="2",1="Item 1 Text",2="Item 2 Text",3="Item 3 Text"](myaction),
  149.          LIST[EDITBOX="YES",EXPAND="YES",VALUE="3",1="Item 1 Text",2="Item 2 Text",3="Item 3 Text"](myaction)
  150.        )
  151.      )
  152.    ),
  153.    CANVAS[BGCOLOR="128 255 0"](myaction)
  154.  )
  155. )
  156. """
  157.  
  158. Iup::Open()
  159. Iup::LoadBuffer(sample_led)
  160. Iup::Show(Iup::GetHandle("dlg"))
  161. Iup::MLoop()
  162. IUP::Close()
  163.  
« Last Edit: December 11, 2015, 05:05:47 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3493
    • ScriptBasic Open Source Project
Re: Apple Swift on Ubuntu
« Reply #20 on: December 11, 2015, 05:24:12 PM »
This works. I was able to locate more info on LED. (see attached)

This call associates the LED (expression/callback ID) to the actual function name being used.

Code: [Select]
IupSetFunction("LED_reference", callback_function_name)



Code: Text
  1. import Foundation
  2. import Glibc
  3.  
  4. func btn_exit_cb(ih: COpaquePointer) -> Int32 {
  5.   return IUP_CLOSE
  6. }
  7.  
  8. IupOpen(nil, nil)
  9.  
  10. IupLoad("sample.led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,2,2,2,2,2
,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
)

mnu = MENU
(
  SUBMENU("IupSubMenu 1",
    MENU
    (
      ITEM[VALUE=ON]("IupItem 1 Checked",myaction),
      SEPARATOR(),
      ITEM[ACTIVE="NO"]("IupItem 2 Disabled",myaction)
    )
  ),
  ITEM("IupItem 3",myaction),
  ITEM("IupItem 4",myaction)
)

dlg = DIALOG[TITLE="Swift IUP LED",MENU="mnu"]
(
  VBOX[GAP="5",ALIGNMENT="ARIGHT",MARGIN="5x5"]
  (
    HBOX
    (
      FRAME[TITLE="IupButton"]
      (
        VBOX
        (
          BUTTON("Quit Dialog",exit_dlg),
          BUTTON[IMAGE=img1]("",myaction),
          BUTTON[IMAGE=img1,IMPRESS=img2]("",myaction)
        )
      ),
      FRAME[TITLE="IupLabel"]
      (
        VBOX
        (
          LABEL("Label Text"),
          LABEL[SEPARATOR=HORIZONTAL](""),
          LABEL[IMAGE=img1]("")
        )
      ),
      FRAME[TITLE="IupToggle"]
      (
        VBOX
        (
          TOGGLE[VALUE="ON"]("Toggle Text",myaction),
          TOGGLE[IMAGE=img1,IMPRESS=img2]("",myaction),
          FRAME[TITLE="IupRadio"]
          (
            RADIO
            (
              VBOX
              (
                TOGGLE("Toggle Text",myaction),
                TOGGLE("Toggle Text",myaction)
              )
            )
          )
        )
      ),
      FRAME[TITLE="IupText/IupMultiline"]
      (
        VBOX
        (
          TEXT[SIZE="80x",VALUE="IupText Text"](myaction),
          MULTILINE[SIZE="80x60",EXPAND="YES",VALUE="IupMultiline Text\nSecond Line\nThird Line"](myaction)
        )
      ),
      FRAME[TITLE="IupList"]
      (
        VBOX
        (
          LIST[EXPAND="YES",VALUE="1",1="Item 1 Text",2="Item 2 Text",3="Item 3 Text"](myaction),
          LIST[DROPDOWN="YES",EXPAND="YES",VALUE="2",1="Item 1 Text",2="Item 2 Text",3="Item 3 Text"](myaction),
          LIST[EDITBOX="YES",EXPAND="YES",VALUE="3",1="Item 1 Text",2="Item 2 Text",3="Item 3 Text"](myaction)
        )
      )
    ),
    CANVAS[BGCOLOR="128 255 0"](myaction)
  )
)


jrs@laptop:~/Swift/usr/share/examples$ swiftc iupled.swift -import-objc-header /usr/include/iup/iup.h -liup -o iupled
jrs@laptop:~/Swift/usr/share/examples$ ./iupled

« Last Edit: December 11, 2015, 06:53:29 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3493
    • ScriptBasic Open Source Project
Re: Apple Swift on Ubuntu
« Reply #21 on: December 11, 2015, 10:55:15 PM »
AIR,

I found a Swift based SQLite3 wrapper for the Apple version. I tried to compile it but had some errors. I think this would be a good SQLite3 library interface if it wouldn't take a lot of work to get running.

SwiftData is a simple and effective wrapper around the SQLite3 C API written completely in Swift

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Apple Swift on Ubuntu
« Reply #22 on: December 12, 2015, 09:09:09 AM »
Of course it has errors, it's using Apple-specific API calls.

To do this right, you should make use of the PackageManager support that Swift-Linux has to incorporate the dependencies you'll need.

IOW, you're probably gonna have to roll your own Sqlite3 implementation unless someone else has already done it.

Offline John

  • Forum Support / SB Dev
  • Posts: 3493
    • ScriptBasic Open Source Project
Re: Apple Swift on Ubuntu
« Reply #23 on: December 12, 2015, 10:46:54 AM »
Quote
Of course it has errors, it's using Apple-specific API calls.

I was fooled by the claim of being written in 100% Swift. I have never used Object-C or the Swift Package Manager for that matter. If you feel that this package is out of the Swift generic ballpark, then maybe we need to look elsewhere.

We could be traditional and you be the SQLite king as your were for Script BASIC.  8)

I'll leave it in your hands what we should do for a SQLite Swift solution.


Offline John

  • Forum Support / SB Dev
  • Posts: 3493
    • ScriptBasic Open Source Project
Re: Apple Swift on Ubuntu
« Reply #24 on: December 12, 2015, 03:36:44 PM »
IUP includes a Dialog Layout tool that lets you interactively modify your live dialog. In this example I modified the background and forground colors of the Quit Dialog button. Changes can be made without changing code and once you decide on your design you can export your new LED screen definition to a file. No recompiling needed.  :)

Code: Text
  1. /*
  2. Linux Swift IUP LED Example
  3. Screen Shot: http://www.johnspikowski.com/Swift-C/swift_iupled.png
  4. Compile: swiftc iupled.swift -import-objc-header /usr/include/iup/iup.h -liup -o iupled
  5. Contributors: John Spikowski 2015-12-11
  6. */
  7.  
  8.  
  9. import Foundation
  10. import Glibc
  11.  
  12. func btn_exit_cb(ih: COpaquePointer) -> Int32 {
  13.   return IUP_CLOSE
  14. }
  15.  
  16. IupOpen(nil, nil)
  17.  
  18. IupLoad("sample.led")
  19.  
  20. IupSetFunction("exit_dlg", btn_exit_cb)
  21.  
  22. IupShow(IupLayoutDialog(IupGetHandle("dlg")))
  23.  
  24. IupMainLoop()
  25.  
  26. IupClose()
  27.  

Dialog Layout Editor




   

Here is the untouched export of sample.led via the Dialog Layout editor.

Code: Text
  1. #   Generated by IupLayoutDialog export to LED.
  2.  
  3. dlg = DIALOG[
  4.   MENU = mnu,
  5.   USERSIZE = 0x0,
  6.   TITLE = "Swift IUP LED"](
  7.     VBOX[
  8.       USERSIZE = 0x0,
  9.       MARGIN = 5x5,
  10.       GAP = 5,
  11.       ALIGNMENT = ARIGHT](
  12.         HBOX[
  13.           USERSIZE = 0x0](
  14.             FRAME[
  15.               USERSIZE = 0x0,
  16.               TITLE = IupButton](
  17.                 VBOX[
  18.                   USERSIZE = 0x0](
  19.                     BUTTON[
  20.                       USERSIZE = 0x0,
  21.                       TITLE = "Quit Dialog",
  22.                       ACTION = exit_dlg]("", exit_dlg),
  23.                     BUTTON[
  24.                       IMAGE = img1,
  25.                       USERSIZE = 0x0]("", do_nothing),
  26.                     BUTTON[
  27.                       IMPRESS = img2,
  28.                       IMAGE = img1,
  29.                       USERSIZE = 0x0]("", do_nothing))),
  30.             FRAME[
  31.               USERSIZE = 0x0,
  32.               TITLE = IupLabel](
  33.                 VBOX[
  34.                   USERSIZE = 0x0](
  35.                     LABEL[
  36.                       USERSIZE = 0x0,
  37.                       TITLE = "Label Text"](""),
  38.                     LABEL[
  39.                       SEPARATOR = HORIZONTAL,
  40.                       EXPAND = HORIZONTALFREE,
  41.                       USERSIZE = 0x0](""),
  42.                     LABEL[
  43.                       IMAGE = img1,
  44.                       USERSIZE = 0x0](""))),
  45.             FRAME[
  46.               USERSIZE = 0x0,
  47.               TITLE = IupToggle](
  48.                 VBOX[
  49.                   USERSIZE = 0x0](
  50.                     TOGGLE[
  51.                       USERSIZE = 0x0,
  52.                       TITLE = "Toggle Text",
  53.                       VALUE = ON]("", do_nothing),
  54.                     TOGGLE[
  55.                       IMPRESS = img2,
  56.                       IMAGE = img1,
  57.                       USERSIZE = 0x0]("", do_nothing),
  58.                     FRAME[
  59.                       USERSIZE = 0x0,
  60.                       TITLE = IupRadio](
  61.                         RADIO[
  62.                           USERSIZE = 0x0](
  63.                             VBOX[
  64.                               USERSIZE = 0x0](
  65.                                 TOGGLE[
  66.                                   USERSIZE = 0x0,
  67.                                   TITLE = "Toggle Text"]("", do_nothing),
  68.                                 TOGGLE[
  69.                                   USERSIZE = 0x0,
  70.                                   TITLE = "Toggle Text"]("", do_nothing)))))),
  71.             FRAME[
  72.               USERSIZE = 0x0,
  73.               TITLE = IupText/IupMultiline](
  74.                 VBOX[
  75.                   USERSIZE = 0x0](
  76.                     TEXT[
  77.                       SIZE = 80x,
  78.                       USERSIZE = 140x0,
  79.                       VALUE = "IupText Text"](do_nothing),
  80.                     MULTILINE[
  81.                       EXPAND = YES,
  82.                       SIZE = 80x60,
  83.                       USERSIZE = 140x128,
  84.                       VALUE = "IupMultiline Text\nSecond Line\nThird Line",
  85.                       MULTILINE = YES](do_nothing))),
  86.             FRAME[
  87.               USERSIZE = 0x0,
  88.               TITLE = IupList](
  89.                 VBOX[
  90.                   USERSIZE = 0x0](
  91.                     LIST[
  92.                       EXPAND = YES,
  93.                       USERSIZE = 0x0,
  94.                       VALUE = 1,
  95.                       1 = "Item 1 Text",
  96.                       2 = "Item 2 Text",
  97.                       3 = "Item 3 Text"](do_nothing),
  98.                     LIST[
  99.                       EXPAND = YES,
  100.                       USERSIZE = 0x0,
  101.                       VALUE = 2,
  102.                       1 = "Item 1 Text",
  103.                       2 = "Item 2 Text",
  104.                       3 = "Item 3 Text",
  105.                       DROPDOWN = YES](do_nothing),
  106.                     LIST[
  107.                       EXPAND = YES,
  108.                       USERSIZE = 0x0,
  109.                       EDITBOX = YES,
  110.                       VALUE = 3,
  111.                       1 = "Item 1 Text",
  112.                       2 = "Item 2 Text",
  113.                       3 = "Item 3 Text"](do_nothing)))),
  114.         CANVAS[
  115.           USERSIZE = 0x0,
  116.           BGCOLOR = "128 255 0"](do_nothing)))
  117.  

Quote
When the application defines the SIZE or RASTERSIZE attributes, it changes the User size in IUP. The initial internal value is "0x0". When set to NULL the User size is internally set to "0x0". If the element is not mapped then the returned value by SIZE or RASTERSIZE is the User size, if the element is mapped then the returned value is the Current size. To obtain the User size after the element is mapped use the USERSIZE attribute (since 3.12).

By default the layout computation uses the Natural size of the element to compose the layout of the dialog, but if the User size is defined then it is used instead of the Natural size. In this case the Natural size is not even computed. But there are two exceptions.

If the element is a container (not including the dialog) the User size will be used instead of the Natural size only if bigger than the Natural size. So for containers the User size will also act as a minimum value for Natural size.

For the dialog, if the User size is defined then it is used instead of the Natural size, but the Natural size of the dialog is always computed. And if the User size is not defined, the Natural size is used only if bigger than the Current size, so in this case the dialog will always increase its size to fit all its contents. In other words, in this case the dialog will not shrink its Current size unless the User size is defined. See the SHRINK attribute guide bellow for an alternative.

When the user is interactively changing the dialog size the Current size is updated. But the dialog contents will always occupy the Natural size available, being smaller or bigger than the dialog Current size.

When SIZE or RASTERSIZE attributes are set for the dialog (changing the User size) the Current size is also reset to "0x0". Allowing the application to force an update of its Window size. To only change the User size in pixels, without resetting the Current size, set the USERSIZE attribute (since 3.12).

After the Natural size is calculated for all the elements in the dialog, the the Current size is set based on the available space in the dialog. So the Current size is set from the outer element (the dialog) to the inner element, in opposite of what it is done for the Natural size.

After all the elements have their Current size updated, the elements positions are calculated, and finally, after the element is mapped, the Window size and position are set for the native elements. The Window size is set exactly to the Current size.

After the element is mapped the returned value for SIZE or RASTERSIZE is the Current size. It actually returns the native Window size of the element. Before mapping, the returned value is the User size.

ALIGNMENT (non inheritable): horizontal and vertical alignment. Possible values: "ALEFT", "ACENTER" and "ARIGHT",  combined to "ATOP", "ACENTER" and "ABOTTOM". Default: "ACENTER:ACENTER". Partial values are also accepted, like "ARIGHT" or ":ATOP", the other value will be used from the current alignment.

MARGIN, CMARGIN: Defines a margin in pixels, CMARGIN is in the same units of the SIZE attribute. Its value has the format "widthxheight", where width and height are integer values corresponding to the horizontal and vertical margins, respectively. Default: "0x0" (no margin).

GAP, CGAP: Defines a vertical space in pixels between the children, CGAP is in the same units of the SIZE attribute for the height. Default: "0".

To reduce the size of the dialog and its containers to a size smaller than the Natural size the SHRINK attribute of the dialog can be used. If set to YES all the containers of the dialog will be able to reduce its size. But be aware that elements may overlap and the layout result could be visually bad if the dialog size is smaller than its Natural size.

Another way to increase the size of elements is to use the EXPAND attribute. When there is room in the container to expand an element, the container layout will expand the elements that have the EXPAND attribute set to YES, HORIZONTAL or VERTICAL accordingly, even if they have the User size defined. The default is EXPAND=NO, but for containers is EXPAND=YES.



« Last Edit: December 13, 2015, 01:47:35 AM by John »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Apple Swift on Ubuntu
« Reply #25 on: December 13, 2015, 12:34:10 PM »
I'll leave it in your hands what we should do for a SQLite Swift solution.

This is the most fucking retarded way of implementing an interface to a C library I've ever encountered.  I've stated that I didn't care for Swift before, now I can say that I absolutely HATE it.

It definitely DOES NOT HELP that there is almost NO documentation that's worth anything at this point on the PackageManager under Linux.

I've attached what I managed to create as far as a Sqlite3 interface, but this is as far as I go.  After unpacking, cd into the directory and do:  swift build.  The binary will be in the .build/debug folder

If I ever use Swift again, it will be exclusively on my Mac.

Have fun repeatedly banging your head against a wall.....

Offline John

  • Forum Support / SB Dev
  • Posts: 3493
    • ScriptBasic Open Source Project
Re: Apple Swift on Ubuntu
« Reply #26 on: December 13, 2015, 02:08:53 PM »
That is the first time I've been kissed and slapped at the same time.  ;D

Didn't I mention that the Swift-C effort here was experimental?

Thanks again AIR for being a good friend and allowing me to continue being optimistic towards Swift's future. I have to laugh hearing you bad mouthing an Apple offering.  :-X

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Apple Swift on Ubuntu
« Reply #27 on: December 13, 2015, 02:58:39 PM »
That is the first time I've been kissed and slapped at the same time.  ;D

Didn't I mention that the Swift-C effort here was experimental?

Thanks again AIR for being a good friend and allowing me to continue being optimistic towards Swift's future. I have to laugh hearing you bad mouthing an Apple offering.  :-X

Apple's not stupid:  They're selling the pipe dream that you'll be able to create apps for their store on non-Apple hardware/OS.  It might be possible at some point, but you won't be using the Cocoa and other Frameworks to do it anytime soon, if at all.

People use Swift because a huge (<--cue Donald Trump Accent) company is behind it.  Kind of like how Objective C became popular.  At least with Objective C, I can drop straight C into it without having to jump through crazy hoops to get shit to work.

I asked myself the question, is Swift worth all the effort to get something as simple as SQlite going?  After taking the same program and re doing it in NIM (5min, but it already has sqlite support), the answer is a resounding NO.

I'm really surprised that you dropped NIM, btw.  It already has an IUP module that works in Linux and Windows, and already has support for a large amount of 3rd party libraries.

Here's the same simple sqlite program in NIM:

Code: Text
  1. import db_sqlite
  2.  
  3.  
  4. let db = open("test.db", nil, nil, nil)
  5.  
  6. db.exec(sql("Drop table if exists cars"))
  7.  
  8. db.exec(sql("CREATE TABLE Cars(Id INT, Name TEXT, Price INT)"))
  9.  
  10. for x in items(["1, 'Audi', 52642",
  11.                 "2, 'Mercedes', 57127",
  12.                 "3, 'Skoda', 9000",
  13.                 "4, 'Volvo', 29000",
  14.                 "5, 'Bentley', 350000",
  15.                 "6, 'Citroen', 21000",
  16.                 "7, 'Hummer', 41400",
  17.                 "8, 'Volkswagen', 21600"]):
  18.  
  19.     db.exec(sql("INSERT INTO Cars VALUES(" & x & ")"))
  20.  
  21. for x in db.fastRows(sql("select Name,Price from cars Order by Price DESC")):
  22.   echo x[0], " ", x[1]
  23.  
  24. db.close()
  25.  

Same thing in Swift:
Code: Text
  1. import Foundation
  2. import Glibc
  3. import CSqlite3
  4.  
  5.  
  6.  
  7. func sqlColumnText(statement: COpaquePointer, column: Int32) -> String {
  8.         return String.fromCString(UnsafePointer<CChar>(sqlite3_column_text(statement,column)))!
  9. }
  10.  
  11. func sqlVersion() -> String {
  12.         let s = String.fromCString(sqlite3_libversion())!
  13.         return "Sqlite3 Version: \(s)"
  14. }
  15.  
  16. func sqlOpen(filename: String, database: UnsafeMutablePointer<COpaquePointer>) {
  17.         let rc = sqlite3_open(filename, database)
  18.  
  19.         if (rc != SQLITE_OK) {
  20.                 print("Cannot open database " + String.fromCString(sqlite3_errmsg(database.memory))!)
  21.                 exit(1)
  22.         }
  23. }
  24.  
  25. func sqlExec(database: COpaquePointer, query: String) {
  26.         var err_msg: UnsafeMutablePointer<CChar> = nil
  27.         let rc = sqlite3_exec(database, query, nil, nil, &err_msg)
  28.  
  29.         if (rc != SQLITE_OK ) {
  30.  
  31.             print("SQL error: " + String.fromCString( err_msg)!)
  32.  
  33.             sqlite3_free(err_msg)
  34.             sqlite3_close(database)
  35.  
  36.             exit(1)
  37.         }
  38. }
  39.  
  40. func sqlPrepare(database: COpaquePointer, query: String, statement: UnsafeMutablePointer<COpaquePointer>) {
  41.         let rc = sqlite3_prepare_v2(database,query,-1,statement,nil)
  42.  
  43.         if (rc != SQLITE_OK ) {
  44.  
  45.             print("Failed to select data")
  46.             print("SQL error: " + String.fromCString( sqlite3_errmsg(database))!)
  47.  
  48.  
  49.             sqlite3_close(database)
  50.  
  51.             exit(1)
  52.         }
  53. }
  54.  
  55. func sqlStep(statement: COpaquePointer) -> Int32 {
  56.         return sqlite3_step(statement)
  57. }
  58.  
  59. func sqlClose(database: COpaquePointer){
  60.         sqlite3_close(database)
  61. }
  62.  
  63.  
  64.  
  65.  
  66.  
  67. var db: COpaquePointer = nil
  68. var stmt: COpaquePointer = nil
  69.  
  70. print(sqlVersion(),"\n")
  71.  
  72. sqlOpen("test.db", database: &db)
  73.  
  74. var sql =       "DROP TABLE IF EXISTS Cars;" +
  75.             "CREATE TABLE Cars(Id INT, Name TEXT, Price INT);" +
  76.             "INSERT INTO Cars VALUES(1, 'Audi', 52642);" +
  77.             "INSERT INTO Cars VALUES(2, 'Mercedes', 57127);" +
  78.             "INSERT INTO Cars VALUES(3, 'Skoda', 9000);" +
  79.             "INSERT INTO Cars VALUES(4, 'Volvo', 29000);" +
  80.             "INSERT INTO Cars VALUES(5, 'Bentley', 350000);" +
  81.             "INSERT INTO Cars VALUES(6, 'Citroen', 21000);" +
  82.             "INSERT INTO Cars VALUES(7, 'Hummer', 41400);" +
  83.             "INSERT INTO Cars VALUES(8, 'Volkswagen', 21600);"
  84.  
  85. sqlExec(db, query: sql)
  86.  
  87.  
  88. sql = "SELECT Name,Price FROM Cars Order By Price DESC"
  89.  
  90. sqlPrepare(db, query: sql, statement: &stmt)
  91.  
  92. while sqlStep(stmt) == SQLITE_ROW {
  93.         var make  = sqlColumnText(stmt, column: 0)
  94.         var price = sqlColumnText(stmt, column: 1)
  95.         print(make,price)
  96.  
  97. }
  98.  
  99.  
  100. sqlClose(db)
  101.  

That's a ridiculous amount of casting just to get Swift to work with SQlite3.

One other thing, and this applies to MacOS as well:  If you use Swift, you also need to make the libraries available.  On the Mac, they're added to the application bundle, bloating the overall App size.  It also won't work on systems running os versions less than 10.9.  On Linux, you'll need to distribute the libraries in order for apps to work.  Reminds me of Mono on Mac and Linux.

Offline John

  • Forum Support / SB Dev
  • Posts: 3493
    • ScriptBasic Open Source Project
Re: Apple Swift on Ubuntu
« Reply #28 on: December 13, 2015, 03:27:12 PM »
I haven't given up on Script BASIC for the same reason. It might be slower than Swift but I can do anything I want with it and on multiple platforms with a common scripting code base. C BASIC for the extension module API even makes it easier to extend the language seamlessly.

To me, your Swift SQLite interface doesn't look bad and is very informative how one must interface with standard C libraries.

Thanks again for this effort and it gives me something to build on. (try to plug in my SDL_gfx generic library I initially did for BaCon)


jrs@laptop:~/Swift/sqldemo$ ls -l
total 12
-rw-rw-r-- 1 jrs jrs  151 Dec 13 12:17 Package.swift
drwxrwxr-x 2 jrs jrs 4096 Dec 13 15:57 sources
drwxrwxr-x 4 jrs jrs 4096 Dec 13 15:57 Sqlite3
jrs@laptop:~/Swift/sqldemo$ swift build
Cloning Packages/Sqlite3
Compiling Swift Module 'sqldemo' (1 sources)
Linking Executable:  .build/debug/sqldemo
jrs@laptop:~/Swift/sqldemo$ ls -la
total 28
drwxrwxr-x 6 jrs jrs 4096 Dec 13 16:05 .
drwxrwxr-x 7 jrs jrs 4096 Dec 13 15:57 ..
drwxrwxr-x 3 jrs jrs 4096 Dec 13 16:05 .build
drwxrwxr-x 3 jrs jrs 4096 Dec 13 16:05 Packages
-rw-rw-r-- 1 jrs jrs  151 Dec 13 12:17 Package.swift
drwxrwxr-x 2 jrs jrs 4096 Dec 13 15:57 sources
drwxrwxr-x 4 jrs jrs 4096 Dec 13 15:57 Sqlite3
jrs@laptop:~/Swift/sqldemo$ cd .build
jrs@laptop:~/Swift/sqldemo/.build$ ls -l
total 4
drwxrwxr-x 4 jrs jrs 4096 Dec 13 16:05 debug
jrs@laptop:~/Swift/sqldemo/.build$ cd debug
jrs@laptop:~/Swift/sqldemo/.build/debug$ ls -l
total 80
-rwxrwxr-x 1 jrs jrs 56275 Dec 13 16:05 sqldemo
drwxrwxr-x 5 jrs jrs  4096 Dec 13 16:05 sqldemo.o
-rw-rw-r-- 1 jrs jrs   540 Dec 13 16:05 sqldemo.swiftdoc
-rw-rw-r-- 1 jrs jrs 11148 Dec 13 16:05 sqldemo.swiftmodule
drwxrwxr-x 3 jrs jrs  4096 Dec 13 16:05 Sqlite3.o
jrs@laptop:~/Swift/sqldemo/.build/debug$ ./sqldemo
Sqlite3 Version: 3.8.2

Bentley 350000
Mercedes 57127
Audi 52642
Hummer 41400
Volvo 29000
Volkswagen 21600
Citroen 21000
Skoda 9000
jrs@laptop:~/Swift/sqldemo/.build/debug$
« Last Edit: December 13, 2015, 04:06:47 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3493
    • ScriptBasic Open Source Project
Re: Apple Swift on Ubuntu
« Reply #29 on: December 13, 2015, 04:47:33 PM »
This works for me. How can I make your SQLite3 Swift code an include or import?

Code: Text
  1. import Foundation
  2. import Glibc
  3.  
  4.  
  5. func sqlColumnText(statement: COpaquePointer, column: Int32) -> String {
  6.         return String.fromCString(UnsafePointer<CChar>(sqlite3_column_text(statement,column)))!
  7. }
  8.  
  9. func sqlVersion() -> String {
  10.         let s = String.fromCString(sqlite3_libversion())!
  11.         return "Sqlite3 Version: \(s)"
  12. }
  13.  
  14. func sqlOpen(filename: String, database: UnsafeMutablePointer<COpaquePointer>) {
  15.         let rc = sqlite3_open(filename, database)
  16.  
  17.         if (rc != SQLITE_OK) {
  18.                 print("Cannot open database " + String.fromCString(sqlite3_errmsg(database.memory))!)
  19.                 exit(1)
  20.         }
  21. }
  22.  
  23. func sqlExec(database: COpaquePointer, query: String) {
  24.         var err_msg: UnsafeMutablePointer<CChar> = nil
  25.         let rc = sqlite3_exec(database, query, nil, nil, &err_msg)
  26.  
  27.         if (rc != SQLITE_OK ) {
  28.                
  29.             print("SQL error: " + String.fromCString( err_msg)!)
  30.            
  31.             sqlite3_free(err_msg)      
  32.             sqlite3_close(database)
  33.            
  34.             exit(1)
  35.         }
  36. }
  37.  
  38. func sqlPrepare(database: COpaquePointer, query: String, statement: UnsafeMutablePointer<COpaquePointer>) {
  39.         let rc = sqlite3_prepare_v2(database,query,-1,statement,nil)
  40.  
  41.         if (rc != SQLITE_OK ) {
  42.            
  43.             print("Failed to select data")
  44.             print("SQL error: " + String.fromCString( sqlite3_errmsg(database))!)
  45.  
  46.  
  47.             sqlite3_close(database)
  48.            
  49.             exit(1)
  50.         }
  51. }
  52.  
  53. func sqlStep(statement: COpaquePointer) -> Int32 {
  54.         return sqlite3_step(statement)
  55. }
  56.  
  57. func sqlClose(database: COpaquePointer){
  58.         sqlite3_close(database)
  59. }
  60.  
  61.  
  62. var db: COpaquePointer = nil
  63. var stmt: COpaquePointer = nil
  64.  
  65. print(sqlVersion(),"\n")
  66.  
  67. sqlOpen("test.db", database: &db)
  68.  
  69. var sql =       "DROP TABLE IF EXISTS Cars;" +
  70.             "CREATE TABLE Cars(Id INT, Name TEXT, Price INT);" +
  71.             "INSERT INTO Cars VALUES(1, 'Audi', 52642);" +
  72.             "INSERT INTO Cars VALUES(2, 'Mercedes', 57127);" +
  73.             "INSERT INTO Cars VALUES(3, 'Skoda', 9000);" +
  74.             "INSERT INTO Cars VALUES(4, 'Volvo', 29000);" +
  75.             "INSERT INTO Cars VALUES(5, 'Bentley', 350000);" +
  76.             "INSERT INTO Cars VALUES(6, 'Citroen', 21000);" +
  77.             "INSERT INTO Cars VALUES(7, 'Hummer', 41400);" +
  78.             "INSERT INTO Cars VALUES(8, 'Volkswagen', 21600);"
  79.  
  80. sqlExec(db, query: sql)
  81.  
  82. sql = "SELECT Name,Price FROM Cars Order By Price DESC"
  83.  
  84. sqlPrepare(db, query: sql, statement: &stmt)
  85.  
  86. while sqlStep(stmt) == SQLITE_ROW {
  87.         var make  = sqlColumnText(stmt, column: 0)
  88.         var price = sqlColumnText(stmt, column: 1)
  89.         print(make,price)
  90.  
  91. }    
  92.  
  93. sqlClose(db)
  94.  


jrs@laptop:~/Swift/usr/share/examples$ swiftc airlite.swift -import-objc-header /usr/include/sqlite3.h -lsqlite3 -o airlite
jrs@laptop:~/Swift/usr/share/examples$ time ./airlite
Sqlite3 Version: 3.8.2

Bentley 350000
Mercedes 57127
Audi 52642
Hummer 41400
Volvo 29000
Volkswagen 21600
Citroen 21000
Skoda 9000

real   0m1.226s
user   0m0.022s
sys   0m0.000s
jrs@laptop:~/Swift/usr/share/examples$
« Last Edit: December 13, 2015, 04:53:15 PM by John »