Gadget Z-Order Problem (Possible PB Bug)

Just starting out? Need help? Post your questions and find answers here.
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Gadget Z-Order Problem (Possible PB Bug)

Post by akj »

In the program below when I click the centre of any one of the 4 coloured rectanges I expect it to be displayed on top of the other 3, but instead it goes beneath the others. I want it to be on top. What am I doing wrong?

Code: Select all

; ZOrder Demo  AKJ  27-Jul-08
; Program to illustrate image gadget Z-ordering
#Program$ = "ZOrder Demo"
#Version$ = "1.0"
EnableExplicit

; Constants (GUI objects)
Enumeration
  #winMain
  ; Four image gadgets, but I choose to call them canvas gadgets
  #canvas
  ; Four image objects used to fill the canvasses
  #image = #canvas+4
EndEnumeration

; Globals
Global cnvw, cnvh
Declare ColouredCanvas(canvas, image, colour, x, y)

; Gui Metrics
Define gap, winw, winh
gap = 30 ; Inter-object spacing
cnvw = 100: cnvh = 80 ; Canvas (image) gadgets
winw = cnvw*2+gap*3: winh = cnvh*2+gap*3 ; Window

; Create GUI
Define title$, flags, hwin, x, y
title$ = " "+#Program$+"  v"+#Version$
flags = #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered
hwin = OpenWindow(#winMain, 0, 0, winw, winh, title$, flags)
CreateGadgetList(hwin)
; Create 4 image gadgets, with 4 different images
x = (winw-cnvw)/2: y = gap
ColouredCanvas(#canvas+0, #image+0, #Red, x, y)
x = gap: y + (cnvh+gap)/2
ColouredCanvas(#canvas+1, #image+1, #Yellow, x, y)
x + cnvw+gap
ColouredCanvas(#canvas+2, #image+2, #Green, x, y)
x = (winw-cnvw)/2: y + (cnvh+gap)/2
ColouredCanvas(#canvas+3, #image+3, #Blue, x, y)

; Event loop
Define done=#False, canvas, cnv
Repeat
  Select WaitWindowEvent()
  Case #PB_Event_Gadget
    Select EventGadget()
    Case #canvas+0 To #canvas+3 ; If a canvas was clicked
      canvas = EventGadget()
      Debug "Canvas "+Str(canvas-#canvas)+" clicked"
      BringWindowToTop_(GadgetID(canvas))
      RedrawWindow_(hwin, 0, 0, #RDW_INVALIDATE)
    EndSelect
  Case #PB_Event_CloseWindow
    done=#True
  EndSelect
Until done
End

Procedure ColouredCanvas(canvas, image, colour, x, y)
; Create a coloured canvas, with the colour provided by an image object
Protected himg
himg = CreateImage(image, cnvw, cnvh)
StartDrawing(ImageOutput(image))
  Box(0, 0, cnvw, cnvh, colour)
StopDrawing()
ImageGadget(canvas, x, y, cnvw, cnvh, himg)
EndProcedure
Last edited by akj on Sun Jul 27, 2008 8:04 pm, edited 1 time in total.
Anthony Jordan
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Change

Code: Select all

BringWindowToTop_(GadgetID(canvas)) 
RedrawWindow_(hwin, 0, 0, #RDW_INVALIDATE)
to

Code: Select all

;BringWindowToTop_(GadgetID(canvas)) 
SetWindowPos_(GadgetID(canvas),#HWND_BOTTOM,GadgetX(canvas),GadgetY(canvas),cnvw,cnvh,0)
RedrawWindow_(hwin, 0, 0, #RDW_INVALIDATE)
but you can end up with two boxes being shown as the topmost.
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

Further investigation has convinced me there is a bug in Pure Basic for image gadgets (and perhaps others) in the handling of their z-ordering. This is further borne out by the topic:
Gadget z-depth reversed?http://www.purebasic.fr/english/viewtopic.php?t=28802
That topic defines a function that completely solves the problem in my program:

Code: Select all

Procedure ForceGadgetZOrder(gadget) 
; Flip the gadget draw order and force the topmost gadget to receive focus 
; first for overlapping gadgets
SetWindowLong_(GadgetID(gadget),#GWL_STYLE,GetWindowLong_(GadgetID(gadget),#GWL_STYLE)|#WS_CLIPSIBLINGS) 
SetWindowPos_(GadgetID(gadget),#HWND_TOP,0,0,0,0,#SWP_NOSIZE|#SWP_NOMOVE) 
EndProcedure
By calling this function immediately after creating a gadget, everything then behaves correctly and in particular

Code: Select all

BringWindowToTop_(GadgetID(canvas))
works as it should.
Anthony Jordan
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Definitely a problem but at least there is a way round it.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

PB has no support for overlapping gadgets (and z-order) at all.
Search the forum for some discussions on it.
quidquid Latine dictum sit altum videtur
Post Reply