[Windows, Linux] - Additional Window & Gadget Functions

Share your advanced PureBasic knowledge/code with the community.
Env
Enthusiast
Enthusiast
Posts: 151
Joined: Tue Apr 27, 2010 3:20 pm
Location: Wales, United Kingdom

[Windows, Linux] - Additional Window & Gadget Functions

Post by Env »

Hey All,

Find below source code that extends what can be done with Gadgets & Windows.

So far it works (tested) on Windows and Linux. I have yet to pick apart the MacOS API documentation... But if you know the solutions for MacOS, please do share them.

Change Log
  • Rev 2: Added optional 'State' argument to FreezeGadget & FreezeWindow as suggested by Kiffi.
  • Rev 4: Utilised SetWindowLongPtr for x64 compatiability on Windows operating systems.
Functions
  • FreezeGadget(Gadget [,State]) - Stops this gadget from being drawn. Useful for updating items.
  • ThawGadget(Gadget) - Re-Enables drawing of this gadget.
  • FreezeWindow(Window [,State]) - Stops this window from being drawn.
  • ThawWindow(Window) - Re-Enables drawing of this window.
  • SetGadgetProperty(Gadget, Key$, *Data) - Set a property of this gadget.
  • GetGadgetProperty(Gadget, Key$) - Returns the associated data with the property.
  • RemoveGadgetProperty(Gadget, Key$) - Removes a property from the gadget.
  • AddWindowProperty(Window, Property$, *Data) - Same as the Gadget implementation.
  • GetWindowProperty(Window, Property$) - Same as the Gadget implementation.
  • RemoveWindowProperty(Window, Property$) - Same as the Gadget implementation.
  • SetWindowOpacity(Window, Opacity) - Change the Opacity of the window. Opacity: 0.0 - 1.0
Code (Contains Usage Example)

Code: Select all

; ----------------------------------------------------------------------------------------------------
; Title:        Utility Functions for Windows & Gadgets.
; Description:  Functions to extend the control over Windows & Gadgets within PureBasic.
; Author(s):    Michael R. King (mrking2910@gmail.com)
; Revision:     4
; Support:      Windows (Tested), Linux (Tested), MacOS (Partial)
;
; Notes:
;               [Linux] SetWindowOpacity() requires 'gtk2-engines-pixbuf' to be installed.
; ----------------------------------------------------------------------------------------------------

; - Change Log -
; Rev 1: Bug Fixes
; Rev 2: Added optional 'State' argument to FreezeGadget & FreezeWindow as suggested by Kiffi.
; Rev 3: Bug Fixes
; Rev 4: Utilised SetWindowLongPtr for x64 compatiability on Windows operating systems.

EnableExplicit

CompilerIf Defined(_PBI_CONTROLUTILITIES_, #PB_Constant) = #False
  #_PBI_CONTROLUTILITIES_ = #True
   
  ; - Freeze Gadget -
  Procedure FreezeGadget(Gadget, State = #True)
    Protected *hCtrl
    If IsGadget(Gadget)
      *hCtrl = GadgetID(Gadget)
      CompilerSelect #PB_Compiler_OS
        CompilerCase #PB_OS_Windows
          If State
            SendMessage_(*hCtrl, #WM_SETREDRAW, #False, 0)
          Else
            SendMessage_(*hCtrl, #WM_SETREDRAW, #True, 0)
            RedrawWindow_(*hCtrl, 0, 0, #RDW_INTERNALPAINT | #RDW_INVALIDATE)
          EndIf
        CompilerCase #PB_OS_Linux
          If State
            gdk_window_freeze_updates_(__GdkWindow(*hCtrl))
          Else
            gdk_window_thaw_updates_(__GdkWindow(*hCtrl))
          EndIf
        CompilerCase #PB_OS_MacOS
          
          ;- TODO: Add MacOS Code Here
          
      CompilerEndSelect
    EndIf
  EndProcedure
  
  ; - Thaw Gadget -
  Macro ThawGadget(Gadget)
    FreezeGadget(Gadget, #False)
  EndMacro
  
  ; - Freeze Window -
  Procedure FreezeWindow(Window, State = #True)
    Protected *hCtrl
    If IsWindow(Window)
      *hCtrl = WindowID(Window)
      CompilerSelect #PB_Compiler_OS
        CompilerCase #PB_OS_Windows
          If State
            SendMessage_(*hCtrl, #WM_SETREDRAW, #False, 0)
          Else
            SendMessage_(*hCtrl, #WM_SETREDRAW, #True, 0)
            RedrawWindow_(*hCtrl, 0, 0, #RDW_INTERNALPAINT | #RDW_INVALIDATE)
          EndIf
        CompilerCase #PB_OS_Linux
          If State
            gdk_window_freeze_updates_(__GdkWindow(*hCtrl))
          Else
            gdk_window_thaw_updates_(__GdkWindow(*hCtrl))
          EndIf
        CompilerCase #PB_OS_MacOS
          
          ;- TODO: Add MacOS Code Here
          
      CompilerEndSelect
    EndIf
  EndProcedure
  
  ; - Thaw Window -  
  Macro ThawWindow(Window)
    FreezeWindow(Window, #False)
  EndMacro
  
  ; - Set Gadget Property -
  Procedure SetGadgetProperty(Gadget, PropertyKey$, *PropertyData)
    Protected *hCtrl
    If IsGadget(Gadget) And Len(PropertyKey$) > 0
      *hCtrl = GadgetID(Gadget)
      CompilerSelect #PB_Compiler_OS
        CompilerCase #PB_OS_Windows
          ProcedureReturn SetProp_(*hCtrl, PropertyKey$, *PropertyData)
        CompilerCase #PB_OS_Linux
          ProcedureReturn g_object_set_data_(*hCtrl, PropertyKey$, *PropertyData)
        CompilerCase #PB_OS_MacOS
          
          ;- TODO: Add MacOS Code Here
          
      CompilerEndSelect
    EndIf
  EndProcedure
  
  ; - Get Gadget Property -
  Procedure.i GetGadgetProperty(Gadget, PropertyKey$)
    Protected *hCtrl
    If IsGadget(Gadget) And Len(PropertyKey$) > 0
      *hCtrl = GadgetID(Gadget)
      CompilerSelect #PB_Compiler_OS
        CompilerCase #PB_OS_Windows
          ProcedureReturn GetProp_(*hCtrl, PropertyKey$)
        CompilerCase #PB_OS_Linux
          ProcedureReturn g_object_get_data_(*hCtrl, PropertyKey$)
        CompilerCase #PB_OS_MacOS
          
          ;- TODO: Add MacOS Code Here
          
      CompilerEndSelect
    EndIf
  EndProcedure
  
  ; - Remove Gadget Property -
  Procedure RemoveGadgetProperty(Gadget, PropertyKey$)
    Protected *hCtrl
    If IsGadget(Gadget) And Len(PropertyKey$) > 0
      *hCtrl = GadgetID(Gadget)
      CompilerSelect #PB_Compiler_OS
        CompilerCase #PB_OS_Windows
          ProcedureReturn RemoveProp_(*hCtrl, PropertyKey$)
        CompilerCase #PB_OS_Linux
          ProcedureReturn g_object_steal_data_(*hCtrl, PropertyKey$)
        CompilerCase #PB_OS_MacOS
          
          ;- TODO: Add MacOS Code Here
          
      CompilerEndSelect
    EndIf
  EndProcedure
  
  ; - Set Window Property -
  Procedure SetWindowProperty(Window, PropertyKey$, *PropertyData)
    Protected *hCtrl
    If IsWindow(Window) And Len(PropertyKey$) > 0
      *hCtrl = WindowID(Window)
      CompilerSelect #PB_Compiler_OS
        CompilerCase #PB_OS_Windows
          ProcedureReturn SetProp_(*hCtrl, PropertyKey$, *PropertyData)
        CompilerCase #PB_OS_Linux
          ProcedureReturn g_object_set_data_(*hCtrl, PropertyKey$, *PropertyData)
        CompilerCase #PB_OS_MacOS
          
          ;- TODO: Add MacOS Code Here
          
      CompilerEndSelect
    EndIf
  EndProcedure
  
  ; - Get Window Property -
  Procedure.i GetWindowProperty(Window, PropertyKey$)
    Protected *hCtrl
    If IsWindow(Window) And Len(PropertyKey$) > 0
      *hCtrl = WindowID(Window)
      CompilerSelect #PB_Compiler_OS
        CompilerCase #PB_OS_Windows
          ProcedureReturn GetProp_(*hCtrl, PropertyKey$)
        CompilerCase #PB_OS_Linux
          ProcedureReturn g_object_get_data_(*hCtrl, PropertyKey$)
        CompilerCase #PB_OS_MacOS
          
          ;- TODO: Add MacOS Code Here
          
      CompilerEndSelect
    EndIf
  EndProcedure
  
  ; - Remove Window Property -
  Procedure RemoveWindowProperty(Window, PropertyKey$)
    Protected *hCtrl
    If IsWindow(Window) And Len(PropertyKey$) > 0
      *hCtrl = WindowID(Window)
      CompilerSelect #PB_Compiler_OS
        CompilerCase #PB_OS_Windows
          ProcedureReturn RemoveProp_(*hCtrl, PropertyKey$)
        CompilerCase #PB_OS_Linux
          ProcedureReturn g_object_steal_data_(*hCtrl, PropertyKey$)
        CompilerCase #PB_OS_MacOS
          
          ;- TODO: Add MacOS Code Here
          
      CompilerEndSelect
    EndIf
  EndProcedure
  
  ; - Set Window Opacity -
  Procedure SetWindowOpacity(Window, Opacity.d = 1.0)
    Protected *hCtrl
    If IsWindow(Window)
      *hCtrl = WindowID(Window)
      If Opacity < 0 : Opacity = 0 : EndIf
      If Opacity > 255 : Opacity = 255 : EndIf
      CompilerSelect #PB_Compiler_OS
        CompilerCase #PB_OS_Windows
          SetWindowLongPtr_(*hCtrl, #GWL_EXSTYLE, GetWindowLongPtr_(*hCtrl, #GWL_EXSTYLE) | #WS_EX_LAYERED)
          SetLayeredWindowAttributes_(*hCtrl, 0, 255 * Opacity / 1.0, 2)        
        CompilerCase #PB_OS_Linux
          If gtk_widget_is_composited(*hCtrl)
            gtk_window_set_opacity(*hCtrl, Opacity)
          EndIf          
        CompilerCase #PB_OS_MacOS     
          SetWindowAlpha(*hCtrl, Opacity)
      CompilerEndSelect
    EndIf
  EndProcedure
  
CompilerEndIf ;_PBI_CONTROLUTILITIES_

; -------------------------------------------------------------------------------------
; - DEMONSTRATION CODE - DEMONSTRATION CODE - DEMONSTRATION CODE - DEMONSTRATION CODE -
; -------------------------------------------------------------------------------------

; - Open Window -
OpenWindow(0, 0, 0, 400, 300, "Test", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)

; - Set Window Opacity -
SetWindowOpacity(0, 0.8)

; - Window Property -
SetWindowProperty(0, "TestProperty", 123)
Debug "Window Property Value: " + Str(GetWindowProperty(0, "TestProperty"))
RemoveWindowProperty(0, "TestProperty")

; - Main Loop -
Repeat
  Select WindowEvent()
    Case #PB_Event_CloseWindow
      End
  EndSelect
ForEver
Feel free to add suggestions, and offer solutions for MacOS, and I will be slowly updating this code in the future, so keep eyes on this space!

Thanks :D
Last edited by Env on Sun Dec 11, 2011 10:00 pm, edited 3 times in total.
Thanks!
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: [Windows, Linux] - Additional Window & Gadget Functions

Post by ts-soft »

Thanks, nice code, works fine here (windows 7 and ubuntu 11.10)
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
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: [Windows, Linux] - Additional Window & Gadget Functions

Post by Kiffi »

Thanks for sharing!

one little suggestion: How about keeping the PB-Syntax (similar to DisableGadget() e.g.)?

For example:

FreezeGadget(Gadget) -> FreezeGadget(Gadget, #True)
ThawGadget(Gadget) -> FreezeGadget(Gadget, #False)

Greetings ... Kiffi
Hygge
Env
Enthusiast
Enthusiast
Posts: 151
Joined: Tue Apr 27, 2010 3:20 pm
Location: Wales, United Kingdom

Re: [Windows, Linux] - Additional Window & Gadget Functions

Post by Env »

Thanks ts-soft and Kiffi :)

Kiffi, I agree with your suggestion and have implemented it into the code (revision 2) - However I replaced ThawGadget & ThawWindow with Macros so both ways of doing it work.



Thanks :D
Thanks!
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: [Windows, Linux] - Additional Window & Gadget Functions

Post by electrochrisso »

Some useful stuff here, thanks. :)
PureBasic! Purely the best 8)
Env
Enthusiast
Enthusiast
Posts: 151
Joined: Tue Apr 27, 2010 3:20 pm
Location: Wales, United Kingdom

Re: [Windows, Linux] - Additional Window & Gadget Functions

Post by Env »

Thanks :)


I am looking to fill in the blanks with the MacOSX version of the code (already have a couple of routines already sorted) - Just having to re-install Mac on a VM to gain access to XCode and the Carbon documentation... Never been a huge fan of Mac OSX's lack of API reference...

Watch this space!



Thanks :D
Thanks!
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Re: [Windows, Linux] - Additional Window & Gadget Functions

Post by Poshu »

Most useful piece of code I've seen in ages :shock: thanks a lot!
Env
Enthusiast
Enthusiast
Posts: 151
Joined: Tue Apr 27, 2010 3:20 pm
Location: Wales, United Kingdom

Re: [Windows, Linux] - Additional Window & Gadget Functions

Post by Env »

Thanks, and no problem :)


Currently working on something a bit meaty in terms of the amount of time I am having to commit, but I will update this code with other little functions when I write them for the main project I'm working on :)



Thanks :D
Thanks!
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: [Windows, Linux] - Additional Window & Gadget Functions

Post by DoubleDutch »

I am looking to fill in the blanks with the MacOSX version of the code (already have a couple of routines already sorted) - Just having to re-install Mac on a VM to gain access to XCode and the Carbon documentation... Never been a huge fan of Mac OSX's lack of API reference...
This would be great - if Fred/Freak spots this then maybe they will make it native.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Env
Enthusiast
Enthusiast
Posts: 151
Joined: Tue Apr 27, 2010 3:20 pm
Location: Wales, United Kingdom

Re: [Windows, Linux] - Additional Window & Gadget Functions

Post by Env »

Going to have to cancel plans to install Mac OS as a VM... For whatever reason it won't work on my processor (regardless of emulation) and the solution is to patch/hack/change the virtualisation software, which i'm not going to do...

If people know the Mac OS solutions to the procedures, please do share.

Thanks :)
Thanks!
Env
Enthusiast
Enthusiast
Posts: 151
Joined: Tue Apr 27, 2010 3:20 pm
Location: Wales, United Kingdom

Re: [Windows, Linux] - Additional Window & Gadget Functions

Post by Env »

New revision submitted.

Revision 4: Utilised SetWindowLongPtr for x64 compatiability on Windows operating systems.

Code updated on initial post.
Thanks!
Post Reply