PopupWindow & PopupMenu powered by CanvasGadget
Posted: Sun Sep 01, 2013 9:22 pm
- This library provides functions to create custom popup menu and custom popup window.
- popup window style and behavior:
- popup window style and behavior:
- Auto close
- Drop shadow
- Open animation

Code: Select all
;:=============================================================================
;:- CustomPopupWindow.pbi
;:- Author : Eddy
;:- Date : September 1, 2013
;:- Compiler : PureBasic 5.20 beta 15
;:- Target OS : Windows
;:- Source --------------------------------------------------------------------
;:- http://www.purebasic.fr/english/viewtopic.php?f=40&t=56381
;:=============================================================================
EnableExplicit
Procedure.i PopupWindowCustomCallback(hWnd, message, wParam, lParam)
If GetProp_(hWnd, "PurebasicPopupWindowCustomCallback")
Protected PopupWindow=GetProp_(hWnd, "PB_WindowID")-1
Protected *PopupWindowCallback=GetProp_(hWnd, "PurebasicPopupWindowCustomCallback")
Select message
Case #WM_NCACTIVATE
;detect if focus will be lost
If wparam=0 : PostMessage_(hWnd, #WM_SHOWWINDOW, 0, 0) : EndIf
Case #WM_SHOWWINDOW
If wparam=0
;auto close popup window
SetWindowLongPtr_(hWnd, #GWL_HWNDPARENT, 0)
ShowWindow_(hWnd, #SW_HIDE)
;retore parent window behavior
Protected *ParentWindow=GetProp_(hWnd, "ParentWindow")
If *ParentWindow
SetWindowLongPtr_(*ParentWindow, #GWL_WNDPROC, GetProp_(*ParentWindow, "PurebasicPopupWindowParentCallback"))
RemoveProp_(*ParentWindow, "PurebasicPopupWindowParentCallback")
EndIf
EndIf
EndSelect
ProcedureReturn CallWindowProc_(*PopupWindowCallback, hWnd, message, wParam, lParam)
EndIf
If GetProp_(hWnd, "PurebasicPopupWindowParentCallback")
Protected *ParentWindowCallback=GetProp_(hWnd, "PurebasicPopupWindowParentCallback")
Select message
Case #WM_NCACTIVATE
;disallow inactive titlebar drawing
If wparam=0 : ProcedureReturn 1 : EndIf
EndSelect
ProcedureReturn CallWindowProc_(*ParentWindowCallback, hWnd, message, wParam, lParam)
EndIf
EndProcedure
Procedure.i CreatePopupWindow(PopupWindow, Animated=#True, GrabFocus=#False)
Protected parent=OpenWindow(#PB_Any, 0, 0, 0, 0, "", #PB_Window_Invisible)
Protected result=OpenWindow(PopupWindow, 0, 0, 200, 200, "Purebasic Custom Popup Window",
#PB_Window_BorderLess|#PB_Window_ScreenCentered|#PB_Window_NoActivate|#PB_Window_Invisible,
WindowID(parent))
If PopupWindow=#PB_Any : PopupWindow=result : EndIf
;enable drop shadow style
Protected *PopupWindow=WindowID(PopupWindow)
SetClassLongPtr_(*PopupWindow, #GCL_STYLE, GetClassLongPtr_(*PopupWindow, #GCL_STYLE)|#CS_DROPSHADOW)
;enable popup behavior
StickyWindow(PopupWindow, 1)
SetProp_(*PopupWindow, "PurebasicPopupWindowCustomCallback", SetWindowLongPtr_(*PopupWindow, #GWL_WNDPROC, @PopupWindowCustomCallback()))
SetProp_(*PopupWindow, "Animated", Animated)
SetProp_(*PopupWindow, "GrabFocus", GrabFocus)
ProcedureReturn result
EndProcedure
Procedure DisplayPopupWindow(PopupWindow, *WindowID, x=#PB_Ignore, y=#PB_Ignore, Width=#PB_Ignore, Height=#PB_Ignore)
Protected *PopupWindow=WindowID(PopupWindow)
Protected Animated=GetProp_(*PopupWindow, "Animated")
Protected GrabFocus=GetProp_(*PopupWindow, "GrabFocus")
If x=#PB_Ignore : x=DesktopMouseX() : EndIf
If y=#PB_Ignore : y=DesktopMouseY() : EndIf
;control parent window behavior
If GetProp_(*WindowID, "PurebasicPopupWindowParentCallback")=0 ;And GrabFocus=#False
SetProp_(*PopupWindow, "ParentWindow", *WindowID)
SetProp_(*WindowID, "PurebasicPopupWindowParentCallback", SetWindowLongPtr_(*WindowID, #GWL_WNDPROC, @PopupWindowCustomCallback()))
EndIf
;reset popup window position and visibility
HideWindow(PopupWindow, 1)
SetWindowLongPtr_(*PopupWindow, #GWL_HWNDPARENT, *PopupWindow)
ResizeWindow(PopupWindow, x, y, Width, Height)
If Animated
;show inactive popup window with "fade-in" animation
AnimateWindow_(*PopupWindow, 0, #AW_HIDE)
AnimateWindow_(*PopupWindow, 500, #AW_BLEND|#AW_ACTIVATE)
Else
;show inactive popup window
HideWindow(PopupWindow, 0)
EndIf
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
DisableExplicit
; ***************************************
; EXAMPLE 1 - custom popup menu
; ***************************************
PopupWindow=CreatePopupWindow(#PB_Any)
If IsWindow(PopupWindow)
StringGadget(222, 5, 5, 150, 20, "Test...")
ButtonGadget(111, 5, 30, 150, 20, "Ok")
CanvasGadget(1, 5, 55, 150, 120)
EndIf
SecondaryWindow=OpenWindow(#PB_Any, 440, 400, 500, 200, "Main Window", #PB_Window_SystemMenu)
MainWindow=OpenWindow(#PB_Any, 0, 0, 300, 400, "Main Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If IsWindow(MainWindow)
SetWindowColor(MainWindow, RGB(131, 131, 182))
txt=TextGadget(#PB_Any, 5, 5, WindowWidth(MainWindow), 100, "Right-click here to display popup window...")
SetGadgetFont(txt, LoadFont(0, "Arial", 24, #PB_Font_Bold|#PB_Font_Italic|#PB_Font_HighQuality))
SetGadgetColor(txt, #PB_Gadget_BackColor, GetWindowColor(MainWindow))
SetGadgetColor(txt, #PB_Gadget_FrontColor, RGB(214, 189, 212))
EndIf
Repeat
Select WaitWindowEvent()
Case #PB_Event_ActivateWindow
Case #PB_Event_DeactivateWindow
Case #PB_Event_Gadget
If EventGadget()=1
StartDrawing(CanvasOutput(1))
Plot(Random(100), Random(100), #Red)
StopDrawing()
EndIf
Case #PB_Event_CloseWindow
If EventWindow()=MainWindow
Break
ElseIf EventWindow()=SecondaryWindow
CloseWindow(EventWindow())
EndIf
Case #PB_Event_RightClick
If EventWindow()=MainWindow
DisplayPopupWindow(PopupWindow, WindowID(MainWindow))
EndIf
EndSelect
ForEver
CompilerEndIf