@kepu: modifying the version I posted earlier, this one breaks the doLine() procedure into stages. It executes a portion of the stage it is on then returns to the eventLoop. If a stage completes it sets the drawingLineStatus to advance to the next stage. These modifications are based on your description of the problem (that it isn't updating graphic elements until the line drawing completes.).
Code: Select all
;code modified by Demivec (8/3/2009)
; additional changes to make doLine() procedure to work in stages so that events can be updated in a more timely manner
Declare crosshair()
Declare doLine()
Procedure handleError(p_result.l, p_text.s) ;display error message if result=0 and then END program.
;sample use HandleError(OpenWindow(#WINDOW_ROOT,0,0,120,120,#APP_NAME,#PB_Window_SystemMenu),"Main window could not be created.")
;Procedure borrowed from "Kale" in PureBasic forum
If p_result = 0
MessageRequester("Error", p_text, #PB_MessageRequester_Ok)
End
EndIf
EndProcedure
Procedure WindowMouseButton(Wnd, ButtonNr)
CompilerIf #PB_Compiler_OS = #PB_OS_Linux
;Linux Version
Protected gdkWnd.l, x.l, y.l, mask.l
If Wnd
*Window.GTKWindow = Wnd
gdkWnd = *Window\bin\child\Window
gdk_window_get_pointer_(gdkWnd, @x, @y, @mask)
Select ButtonNr
Case 0
If (mask & #GDK_BUTTON1_MASK)
ProcedureReturn 1
EndIf
Case 1
If (mask & #GDK_BUTTON3_MASK)
ProcedureReturn 1
EndIf
Case 2
If (mask & #GDK_BUTTON2_MASK)
ProcedureReturn 1
EndIf
EndSelect
EndIf
CompilerElse
;Windows Version
If Wnd And GetForegroundWindow_() = Wnd
Select ButtonNr
Case 0
If GetAsyncKeyState_(#VK_LBUTTON) > 0
ProcedureReturn 1
EndIf
Case 1
If GetAsyncKeyState_(#VK_RBUTTON) > 0
ProcedureReturn 1
EndIf
Case 2
If GetAsyncKeyState_(#VK_MBUTTON) > 0
ProcedureReturn 1
EndIf
EndSelect
EndIf
CompilerEndIf
ProcedureReturn 0
EndProcedure
;********************************************************************
Define WindowWidth = 1000
Define WindowHeight = 700
handleError(OpenWindow(0, 10, 10, WindowWidth, WindowHeight, "PureQCad.018 "),"Couldn't setup main Window")
Define hwnd = WindowID(0)
handleError(hwnd,"Couldn't obtain window handle")
If CreateMenu(0, WindowID(0))
MenuTitle("File")
MenuItem( 1, "&Load...")
MenuItem( 2, "Save")
MenuBar()
MenuItem( 5, "Exit")
MenuTitle("Edit")
MenuTitle("Draw")
MenuItem( 18, "Line")
MenuTitle("Mode")
MenuTitle("Help")
MenuItem( 44, "Help")
MenuItem( 45, "About")
EndIf
If CreateStatusBar(0, WindowID(0))
AddStatusBarField(100)
AddStatusBarField(100)
AddStatusBarField(100)
AddStatusBarField(100)
AddStatusBarField(100)
AddStatusBarField(100)
AddStatusBarField(100)
AddStatusBarField(100)
AddStatusBarField(100)
AddStatusBarField(100)
StatusBarText(0, 0, "prog_name")
StatusBarText(0, 1, " xx= ")
StatusBarText(0, 2, " yy= ")
StatusBarText(0, 3, " orx= ")
StatusBarText(0, 4, " ory= ")
StatusBarText(0, 5, " DrawC ")
StatusBarText(0, 6, " x= " )
StatusBarText(0, 7, " y= " )
StatusBarText(0, 8, " ")
StatusBarText(0, 9, " ")
StatusBarText(0, 10, " ")
Define statusbarCreated = #True
EndIf
;******** init imagegadget ***********************************************
Define bitmap1_n = 1 ; image number 1
Define bitmap1_h = CreateImage(bitmap1_n,800,550) ; create the image and store the handle
Define gadget1_n = 1
ImageGadget(gadget1_n,0,0,800,500,bitmap1_h)
; ******** init cross hair ***************************************************
StartDrawing(ImageOutput(bitmap1_n))
FillArea(10,10,-1,$FFFFFF)
DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_XOr)
Define ox=10
Define oy=10
Define oox=ox
Define ooy=oy
Define hwidth=WindowWidth-40
Define hheight=WindowHeight-45
Line(ox,10, 0, hheight , RGB(55,55,55))
Line(10,oy, hwidth , 0, RGB(55,55,55))
StopDrawing()
SetGadgetState(gadget1_n,bitmap1_h)
Delay(1)
;********* menuloop ******************************************************************************
Define drawingLineStatus = 0
Repeat
Repeat
Define event= WaitWindowEvent(5)
Select event
Case #PB_Event_Menu
Select EventMenu()
Case 18 ;Line
doLine()
Case 45 ; About
MessageRequester("About", "image_gadget_test", 0)
Case 5 ; exit
Define Quit=#True ; End
Default
MessageRequester("Info", "MenuItem: "+Str(EventMenu()), 0)
EndSelect
Case #PB_Event_CloseWindow
Quit=#True
EndSelect
If drawingLineStatus
doLine()
EndIf
Until event = 0 Or Quit = #True
crosshair() ;this does not work properly in Linux if :SetGadgetState).... is used incrosshair sub
Until Quit = #True
End
;'****** end of menuloop ****************************************
Procedure crosshair()
Shared statusbarCreated, bitmap1_h, bitmap1_n, gadget1_n, hheight, hwidth, ox, oy, oox, ooy
StartDrawing(ImageOutput(bitmap1_n))
DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_XOr)
ox=WindowMouseX(0)
oy=WindowMouseY(0)
Line ( oox, 10, 0, hheight, RGB(55,55,55))
Line (10, ooy, hwidth, 0, RGB(55,55,55))
Line ( ox, 10, 0, hheight, RGB(55,55,55))
Line ( 10, oy, hwidth,0 , RGB(55,55,55))
StopDrawing()
SetGadgetState(gadget1_n,bitmap1_h)
oox=ox
ooy=oy
If statusbarCreated
StatusBarText(0, 1, " xx= "+Str(ox))
StatusBarText(0, 2, " yy= "+Str(oy))
StatusBarText(0, 3, " orx= "+Str(oox))
StatusBarText(0, 4, " ory= "+Str(ooy))
EndIf
EndProcedure
Procedure doLine()
Shared statusbarCreated, bitmap1_h, bitmap1_n, gadget1_n, hwnd, ox, oy, drawingLineStatus
Static startX, startY, omousX, omousY
Select drawingLineStatus
Case 0
drawingLineStatus = 1
Case 1 ;setLine
crosshair()
If statusbarCreated
StatusBarText(0, 0, "Line")
EndIf
WindowMouseButton(hwnd, 0)
Delay(1)
If WindowMouseButton(hwnd, 0)=1
drawingLineStatus = 2
startX = ox
startY = oy
EndIf
Case 2 ;startLine
crosshair()
StartDrawing(ImageOutput(bitmap1_n))
DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_XOr)
Protected mousX = WindowMouseX(0)
Protected mousY = WindowMouseY(0)
Line(startX,startY,mousX-startX,mousY-startY,RGB(5,5,200))
omousX = mousX
omousY = mousY
If statusbarCreated
StatusBarText(0, 6, Str(startX) )
StatusBarText(0, 7, Str(startY))
EndIf
StopDrawing()
SetGadgetState(gadget1_n,bitmap1_h)
Delay(1)
drawingLineStatus = 3
Case 3 ;drawLine
If WindowMouseButton(hwnd, 0) <> 0
crosshair()
mousX =ox ; WindowMouseX(0)
mousY = oy ;WindowMouseY(0)
StartDrawing(ImageOutput(bitmap1_n))
DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_XOr)
Line(startX,startY,omousX-startX, omousY-startY,RGB(5,5,200))
Line(startX,startY,mousX-startX,mousY-startY,RGB(5,5,200))
omousX=mousX
omousY=mousY
If statusbarCreated
StatusBarText(0, 8, Str(mousX-startX))
StatusBarText(0, 9, Str(mousY-startY))
EndIf
StopDrawing()
SetGadgetState(gadget1_n,bitmap1_h)
Else
drawingLineStatus = 4
EndIf
Case 4 ;finishLine
mousX =ox ; WindowMouseX(0)
mousY = oy ;WindowMouseY(0)
StartDrawing(ImageOutput(bitmap1_n))
DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_XOr)
Line(startX,startY,omousX-startX, omousY-startY,RGB(5,5,200))
Line(startX,startY,mousX-startX,mousY-startY,RGB(5,5,200))
If statusbarCreated
StatusBarText(0, 8, Str(mousX-startX))
StatusBarText(0, 9, Str(mousY-startY))
StatusBarText(0, 0, " ")
EndIf
StopDrawing()
SetGadgetState(gadget1_n,bitmap1_h)
drawingLineStatus = 0
EndSelect
EndProcedure