Modul OwnGadget (PB5.2x)

Share your advanced PureBasic knowledge/code with the community.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Modul OwnGadget (PB5.2x)

Post by ts-soft »

This Modul based of: http://www.purebasic.fr/english/viewtopic.php?p=140940

You can register your own gadget, for example API-Controls, Controls from ext. Libraries and so on.

The source uses the infos from the actual SDK.

In the moment only for windows!

Code: Select all

;======================================================================
; Module:          OwnGadget.pbi
;
; Author:          Thomas (ts-soft) Schulz
; based on Code by edel (Hallodri)
; Date:            Jul 08, 2013
; Version:         0.8
; Target Compiler: PureBasic 5.2+
; Target OS:       in the moment only windows
; License:         Free, unrestricted, no warranty whatsoever
;                  Use at your own risk
;======================================================================

; History:
; 0.7
; removed GetGadgetParent() (UseGadgetList(0) do the same)
; put RegisterGadget() to public

; 0.8
; fixed RegisterGadget

DeclareModule OwnGadget
  Structure GadgetVT
    GadgetType.l   
    SizeOf.l       
    GadgetCallback.i
    FreeGadget.i
    GetGadgetState.i
    SetGadgetState.i
    GetGadgetText.i
    SetGadgetText.i
    AddGadgetItem2.i
    AddGadgetItem3.i
    RemoveGadgetItem.i
    ClearGadgetItemList.i
    ResizeGadget.i
    CountGadgetItems.i
    GetGadgetItemState.i
    SetGadgetItemState.i
    GetGadgetItemText.i
    SetGadgetItemText.i
    OpenGadgetList2.i
    GadgetX.i
    GadgetY.i
    GadgetWidth.i
    GadgetHeight.i
    HideGadget.i
    AddGadgetColumn.i
    RemoveGadgetColumn.i
    GetGadgetAttribute.i
    SetGadgetAttribute.i
    GetGadgetItemAttribute2.i
    SetGadgetItemAttribute2.i
    SetGadgetColor.i
    GetGadgetColor.i
    SetGadgetItemColor2.i
    GetGadgetItemColor2.i
    SetGadgetItemData.i
    GetGadgetItemData.i
    GetRequiredSize.i
    SetActiveGadget.i
    GetGadgetFont.i
    SetGadgetFont.i
    SetGadgetItemImage.i
  EndStructure

  Declare RegisterGadget(ID.i, hWnd.i, *vt.GadgetVT = 0)
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
      Declare CreateGadget(ID.i, ClassName.s, Text.s, Style.l, x.i, y.i, w.i, h.i, ExStyle.l = 0, *vt.GadgetVT = 0)
  CompilerEndSelect
EndDeclareModule

Module OwnGadget
  EnableExplicit
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  Import ""
  CompilerElse
  ImportC ""
  CompilerEndIf
    PB_Object_GetOrAllocateID(Objects.i, ID.i)
    PB_Gadget_RegisterGadget(ID.i, *Gadget, hWnd.i, *GadgetVT)
    PB_Gadget_Objects.i
  EndImport
  
  Structure Gadget
    Gadget.i
    *vt.GadgetVT
    UserData.i
    OldCallback.i
    Daten.i[4]
  EndStructure
  
  Procedure RegisterGadget(ID.i, hWnd.i, *vt.GadgetVT = 0)
    Protected *Gadget.Gadget
    Protected *vttmp.GadgetVT = AllocateMemory(SizeOf(GadgetVT))

    If Not *vttmp : ProcedureReturn #False : EndIf
          
    If Not hWnd Or ID < -1
      ProcedureReturn #False
    EndIf

    If *vt
      CopyMemory(*vt, *vttmp, SizeOf(GadgetVT))
      *vttmp\SizeOf = SizeOf(GadgetVT)
    EndIf
          
    *Gadget = PB_Object_GetOrAllocateID(PB_Gadget_Objects, ID)
    ProcedureReturn PB_Gadget_RegisterGadget(ID, *Gadget, hWnd, *vttmp)
    
  EndProcedure
  
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
    Procedure CreateGadget(ID.i, ClassName.s, Text.s, Style.l, x.i, y.i, w.i, h.i, ExStyle.l = 0, *vt.GadgetVT = 0)
      Protected hWnd.i, Parent.i = UseGadgetList(0), hInstance.i = GetModuleHandle_(0) 
      
      hWnd  = CreateWindowEx_(ExStyle, ClassName, Text, Style, x, y, w, h, Parent, 0, hInstance, 0)
      SendMessage_(hWnd, #WM_SETFONT, GetGadgetFont(#PB_Default), 1)
      
      If Not hWnd : ProcedureReturn #False : EndIf
      
      ProcedureReturn RegisterGadget(ID, hWnd, *vt)
    EndProcedure
  CompilerEndSelect
EndModule

CompilerIf #PB_Compiler_IsMainFile
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
    
      Enumeration
        #btn1
        #btn2
      EndEnumeration
      
      Procedure MyButton(Gadget, x, y, width, height, Text.s)
        ProcedureReturn OwnGadget::CreateGadget(Gadget, "Button", Text, #WS_CHILD | #WS_VISIBLE, x, y, width, height)
      EndProcedure
      
      Procedure btn1_event()
        Debug "Okay pressed"
      EndProcedure
      
      Procedure Btn3_event()
        End
      EndProcedure
      
      Define btn3
      OpenWindow(0, #PB_Ignore, #PB_Ignore, 190, 100, "example")
      MyButton(#btn1, 10, 10, 80, 25, "Okay")
      BindGadgetEvent(#btn1, @btn1_event())
      MyButton(#btn2, 100, 10, 80, 25, "")
      SetGadgetText(#btn2, "Cancel")
      btn3 = MyButton(#PB_Any, 60, 60, 80, 25, "End")
      GadgetToolTip(btn3, "Close Example!")
      BindGadgetEvent(btn3, @Btn3_event())
      
      Repeat
        Select WaitWindowEvent()
          Case #PB_Event_CloseWindow : Break
          Case #PB_Event_Gadget
            Select EventGadget()
              Case #btn2
                Debug "Cancel pressed"
            EndSelect
        EndSelect
      ForEver
  CompilerEndSelect
CompilerEndIf
Greetings - Thomas
Last edited by ts-soft on Tue Jul 09, 2013 7:50 pm, edited 3 times in total.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Modul OwnGadget (PB5.2x)

Post by luis »

Pretty nice template :)

Thanks for sharing.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
idle
Always Here
Always Here
Posts: 5093
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Modul OwnGadget (PB5.2x)

Post by idle »

Interesting as always, thanks.
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Modul OwnGadget (PB5.2x)

Post by Kwai chang caine »

Works great, thanks TsSoft 8)
ImageThe happiness is a road...
Not a destination
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: Modul OwnGadget (PB5.2x)

Post by eddy »

@TSoft
Hi,
First, thanks for your GadgetVT structure. It's the most recent one that I've found.

Is it possible to define a custom DisableGadget(...) method ?
Last edited by eddy on Wed Jul 24, 2013 5:44 pm, edited 1 time in total.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Modul OwnGadget (PB5.2x)

Post by ts-soft »

I can't see a entry in the vtTable, so i don't know!
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Bisonte
Addict
Addict
Posts: 1232
Joined: Tue Oct 09, 2007 2:15 am

Re: Modul OwnGadget (PB5.2x)

Post by Bisonte »

eddy wrote:@TSoft
Hi,
First, thanks for your GadgetVT structure. It's the most recent one that I've found.

Is it possible to define a custom DisableGadget(...) method ?
You can do it in the callback proc.

Like

Code: Select all

  Case #WM_ENABLE
    If wParam = #True
      ; DisableGadget(Gadget, #True) was used
    Else
      ; DisableGadget(Gadget, #False) was used
    EndIf
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: Modul OwnGadget (PB5.2x)

Post by eddy »

Is there SDK for linux or mac in PB 5.30 ?
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
mk-soft
Always Here
Always Here
Posts: 5393
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Modul OwnGadget (PB5.2x)

Post by mk-soft »

No way.

Only an old Linux. For Mac no SDK for occupancy and construction of GadgetVT.
Already have all searched and inquired.
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
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: Modul OwnGadget (PB5.2x)

Post by eddy »

mk-soft wrote:No way.

Only an old Linux. For Mac no SDK for occupancy and construction of GadgetVT.
Already have all searched and inquired.
Such a pity. It would be cool to fully integrate custom gadgets into PB.
PB has now many canvas-based custom gadgets (tabbar, gauge, button, checkbox, splitter, color picker, etc...).
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
mk-soft
Always Here
Always Here
Posts: 5393
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Modul OwnGadget (PB5.2x)

Post by mk-soft »

I had also thought so that you can use the standard functions.
http://www.purebasic.fr/english/viewtop ... 12&t=59057

Have it but not get MacOS to run.

Therefore, I now go the way to modules and interfaces
http://www.purebasic.fr/german/viewtopi ... =8&t=28903
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
Fred
Administrator
Administrator
Posts: 16681
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Modul OwnGadget (PB5.2x)

Post by Fred »

I will add SDK stuff for OS X and Linux in the next release.
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: Modul OwnGadget (PB5.2x)

Post by eddy »

Fred wrote:I will add SDK stuff for OS X and Linux in the next release.
Great! :D
mk-soft wrote:I had also thought so that you can use the standard functions.
http://www.purebasic.fr/english/viewtop ... 12&t=59057

Have it but not get MacOS to run.

Therefore, I now go the way to modules and interfaces
http://www.purebasic.fr/german/viewtopi ... =8&t=28903
Thanks.
I'll study your code.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
mk-soft
Always Here
Always Here
Posts: 5393
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Modul OwnGadget (PB5.2x)

Post by mk-soft »

Very Great :D
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
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: Modul OwnGadget (PB5.2x)

Post by eddy »

Hi,
I took a quick look at PB SDK. There's no API to retrieve PB Window number of current parent window.

I tried UseGadgetList(0) but it returns PB WindowID value of current parent window.

Code: Select all

PostEvent(#PB_Event_Gadget, Window, Gadget, #PB_EventType_Change)
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
Post Reply