Access gadgets from library (All OS)

Share your advanced PureBasic knowledge/code with the community.
User avatar
mk-soft
Always Here
Always Here
Posts: 5397
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Access gadgets from library (All OS)

Post by mk-soft »

One possibility is to pass the main objects to the DLL. To do this, you must first save the library objects when loading the library
and restore them when exiting.
With a function "InitLib(...) the objects are passed from the main program to the library.
The application and library must be compile with the same purebasic version.

But it seems to work without guarantee (Window, Linux and macOS)

Update v1.03
- Linux has not AttachProcess and DetachProcess
- Change InitLib
- Added ReleaseLib
- Optimize code

Update v1.04
- Optimize force compiler use fonts

TestLib.pb

Code: Select all

;-TOP DLL

; Comment : Library
; Author  : mk-soft
; Version : v1.04

EnableExplicit

; ----

If 0 : LoadFont(0, "", 9) : EndIf
Import ""
  PB_Window_Objects.i
  PB_Gadget_Objects.i
  PB_Image_Objects.i
  PB_Font_Objects.i
EndImport

Global IsInit
Global save_window_objects
Global save_gadget_objects
Global save_image_objects
Global save_font_objects

; ----

ProcedureDLL InitLib(window_objects, gadget_objects, image_objects, font_objects)
  If Not IsInit
    IsInit = #True
    ; Save DLL Objects
    save_window_objects = PB_Window_Objects
    save_gadget_objects = PB_Gadget_Objects
    save_image_objects  = PB_Image_Objects
    save_font_objects   = PB_Font_Objects
    ; Set Main Objects
    PB_Window_Objects = window_objects
    PB_Gadget_Objects = gadget_objects
    PB_Image_Objects  = image_objects
    PB_Font_Objects   = font_objects
  EndIf
  ProcedureReturn IsInit
EndProcedure

ProcedureDLL ReleaseLib()
  If IsInit
    IsInit = #False
    ; Restore DLL Objects
    PB_Window_Objects = save_window_objects
    PB_Gadget_Objects = save_gadget_objects
    PB_Image_Objects  = save_image_objects
    PB_Font_Objects   = save_font_objects
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
EndProcedure

; ++++

ProcedureDLL GadgetResize(Gadget, X.i, Y.i, W.i, H.i)
  ResizeGadget(Gadget, X.i, Y.i, W.i, H.i)
  ProcedureReturn 1
EndProcedure

; ----
TestApp.pb

Code: Select all

;-TOP Main

; Comment : Application
; Author  : mk-soft
; Version : v1.04


;- Purebasic SDK Objects
If 0 : LoadFont(0, "", 9) : EndIf
Import ""
  PB_Window_Objects.i
  PB_Gadget_Objects.i
  PB_Image_Objects.i
  PB_Font_Objects.i
EndImport

;- LibFunctions
Prototype GadgetResize(Gadget, X.i, Y.i, W.i, H.i)

; ----

Procedure OpenTestLibrary(ID)
  Protected r1, LibName.s
  
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
      LibName = "TestLib.dll"
    CompilerCase #PB_OS_Linux
      LibName = "TestLib.so"
    CompilerCase #PB_OS_MacOS
      LibName = "TestLib.dylib"
  CompilerEndSelect
  
  If OpenLibrary(ID, LibName)
    ; Function
    Global GadgetResize.GadgetResize = GetFunction(ID, "GadgetResize")
    
    ; Init Library
    r1 = CallFunction(ID, "InitLib", PB_Window_Objects, PB_Gadget_Objects, PB_Image_Objects, PB_Font_Objects)
    If Not r1
      CloseLibrary(ID)
    EndIf
    ProcedureReturn r1
  Else
    ProcedureReturn #False
  EndIf
  
EndProcedure

Procedure CloseTestLibrary(ID)
  Protected r1
  If IsLibrary(ID)
    r1 = CallFunction(ID, "ReleaseLib")
    If r1
      CloseLibrary(ID)
    EndIf
  EndIf
  ProcedureReturn r1
EndProcedure

; ****

;-Window
Enumeration
  #Window
EndEnumeration


If OpenTestLibrary(0) 
  Debug "InitLib Ok"
Else
  Debug "InitLib faild"
  End
EndIf

If OpenWindow(#Window, 40, 40, 220, 100, "Window", #PB_Window_SystemMenu)
  ButtonGadget(1, 10, 60, 20, 30, "Exit")
  
  If GadgetResize(1, 10, 40, 200, 50)
    MessageRequester("Result", "GadgetResize Ok")
  EndIf
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break ; Exit mainloop
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            CloseWindow(#Window)
            Break ; Exit mainloop
        EndSelect
        
    EndSelect
  ForEver
  
  If CloseTestLibrary(0)
    Debug "ReleaseLib Ok"
  EndIf
  
EndIf
Last edited by mk-soft on Sun Aug 16, 2020 9:33 am, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Access gadgets from library (All OS)

Post by Kwai chang caine »

Waooouuuh !!! very nice and useful code :shock:
Works nice here with W10
Thanks a lot MkSoft for sharing 8)
ImageThe happiness is a road...
Not a destination
ShadowStorm
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Feb 14, 2017 12:07 pm

Re: Access gadgets from library (All OS)

Post by ShadowStorm »

Yes, Thanks MkSoft :)
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
BarryG
Addict
Addict
Posts: 3322
Joined: Thu Apr 18, 2019 8:17 am

Re: Access gadgets from library (All OS)

Post by BarryG »

Code: Select all

Global __Dummy = LoadFont(#PB_Any, "", 9) : FreeFont(__Dummy)
Why load a font just to free it immediately?
User avatar
mk-soft
Always Here
Always Here
Posts: 5397
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Access gadgets from library (All OS)

Post by mk-soft »

Update v1.04
- Optimize force compiler use fonts

If no fonts will be used, there will be no 'PB_Font_Objects' (Linux, macOS)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply