PureBasic Forum
http://forums.purebasic.com/english/

Module ThreadToGUI-Update windows,gadgets,... from threads
http://forums.purebasic.com/english/viewtopic.php?f=12&t=66180
Page 2 of 2

Author:  IdeasVacuum [ Fri Feb 17, 2017 1:59 am ]
Post subject:  Re: Module ThreadToGUI-Update windows,gadgets,... from threa

This is very fine work. It would be good to see these functions incorporated into PB. What do you think Fred?

Author:  mk-soft [ Wed Jun 28, 2017 10:52 am ]
Post subject:  Re: Module ThreadToGUI-Update windows,gadgets,... from threa

Update v1.14
- Added Clipboard functions. On Linux crashed clipboard functions over threads
- Added Requester
- Added check AllocateStructure() and CreateSemaphore()

The functions without return values give a success (1) if the event is successfully entered. Not that the event was processed in the Mainscope.
Do not want the thread blocked ...

Author:  mk-soft [ Sat Jul 08, 2017 7:07 pm ]
Post subject:  Re: Module ThreadToGUI-Update windows,gadgets,... from threa

Update v1.15
- Added more Requester
- Update OpenFileRequester
- Optimize code

Added Input, Color and Font Requester.

Example Requester
Code:
;-TOP

; Example ThreadToGUI Requester

IncludeFile "Modul_ThreadToGUI.pb"

Enumeration
  #Window
EndEnumeration

Enumeration
  #List
EndEnumeration

;- Constants
Enumeration #PB_Event_FirstCustomValue
  #My_DoEvent
EndEnumeration

Procedure Test(Null)
 
  Protected result, path.s
 
  UseModule ThreadToGUI
 
  DoAddGadgetItem(#List, -1, "Start Thread")
  Delay(500)
  result = DoMessageRequester("MessageRequester", "Question", #PB_MessageRequester_YesNoCancel | #PB_MessageRequester_Info)
  Select result
    Case #PB_MessageRequester_Yes
      DoAddGadgetItem(#List, -1, "Message result yes")
    Case #PB_MessageRequester_No
      DoAddGadgetItem(#List, -1, "Message result no")
    Case #PB_MessageRequester_Cancel
      DoAddGadgetItem(#List, -1, "Message result cancel")
  EndSelect
  path = DoOpenFileRequester("OpenFileRequester", "/", "", 0, #PB_Requester_MultiSelection)
  DoAddGadgetItem(#List, -1, "OpenFile: " + path)
  While path
    path = DoNextSelectedFileName()
    If path
      DoAddGadgetItem(#List, -1, "OpenFile: " + path)
    EndIf
  Wend
  path = DoSaveFileRequester("SaveFileRequester", "Test.txt", "", 0)
  DoAddGadgetItem(#List, -1, "SaveFile: " + path)
  path = DoPathRequester("PathRequester", "/")
  DoAddGadgetItem(#List, -1, "Path: " + path)
  path = DoInputRequester("InputRequester", "Password", "******", #PB_InputRequester_Password)
  DoAddGadgetItem(#List, -1, "Input: " + path)
  result = DoColorRequester()
  DoAddGadgetItem(#List, -1, "Color: " + Hex(Result))
  DoAddGadgetItem(#List, -1, "Exit Thread")
 
EndProcedure

If OpenWindow(#Window, 0, 0, 600, 400, "Example Requester", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
  ListViewGadget(#List, 0, 0, 600, 400)
 
  UseModule ThreadToGUI
 
  BindEventGUI(#My_DoEvent)
  hThread = CreateThread(@Test(), #Null)
 
  Repeat
   
    Select WaitWindowEvent()
       
      Case #PB_Event_CloseWindow
        exit = 1
       
    EndSelect
   
  Until exit
  If IsThread(hThread)
    Debug "Thread läuft"
    KillThread(hThread)
  EndIf
 
EndIf

FontRequester
Code:
;-TOP

; Example ThreadToGUI Requester

IncludeFile "Modul_ThreadToGUI.pb"

Enumeration
  #Window
EndEnumeration

Enumeration
  #List
EndEnumeration

;- Constants
Enumeration #PB_Event_FirstCustomValue
  #My_DoEvent
EndEnumeration

Procedure Test(Null)
 
  Protected result, path.s, message$
 
  UseModule ThreadToGUI
 
  DoAddGadgetItem(#List, -1, "Start Thread")
  Delay(500)
  path = DoOpenFileRequester("OpenFileRequester", "/", "", 0, #PB_Requester_MultiSelection)
  DoAddGadgetItem(#List, -1, "OpenFile: " + path)
  While path
    path = DoNextSelectedFileName()
    If path
      DoAddGadgetItem(#List, -1, "OpenFile: " + path)
    EndIf
  Wend
  result = DoFontRequester("Arial", 12, 0, #Red, #PB_Font_Bold)
  If Result
    Message$ = "Selected Font:"
    DoAddGadgetItem(#List, -1, message$)
    Message$ = "Name:  " + DoSelectedFontName()
    DoAddGadgetItem(#List, -1, message$)
    Message$ = "Size: " + Str(DoSelectedFontSize())
    DoAddGadgetItem(#List, -1, message$)
    Message$ = "Color: " + Str(DoSelectedFontColor())
    DoAddGadgetItem(#List, -1, message$)
    message$ = "Style "
    If DoSelectedFontStyle() & #PB_Font_Italic
      Message$ + "/ Italic"
    EndIf
    If DoSelectedFontStyle() & #PB_Font_Bold
      Message$ + " / Bold"
    EndIf
    If DoSelectedFontStyle() & #PB_Font_StrikeOut
      Message$ + " / StrikeOut"
    EndIf
    If DoSelectedFontStyle() & #PB_Font_Underline
      Message$ + " / Underline"
    EndIf
    DoAddGadgetItem(#List, -1, message$)
  Else
    Message$ = "Cancel."
    DoAddGadgetItem(#List, -1, message$)
  EndIf
  DoAddGadgetItem(#List, -1, "Exit Thread")
 
EndProcedure

If OpenWindow(#Window, 0, 0, 600, 400, "Example Requester", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
  ListViewGadget(#List, 0, 0, 600, 400)
 
  UseModule ThreadToGUI
 
  BindEventGUI(#My_DoEvent)
  hThread = CreateThread(@Test(), #Null)
 
  Repeat
   
    Select WaitWindowEvent()
       
      Case #PB_Event_CloseWindow
        exit = 1
       
    EndSelect
   
  Until exit
  If IsThread(hThread)
    Debug "Thread läuft"
    KillThread(hThread)
  EndIf
 
EndIf

:wink:

Author:  mk-soft [ Sat Jul 15, 2017 9:49 am ]
Post subject:  Re: Module ThreadToGUI-Update windows,gadgets,... from threa

Update v1.16
- Added second module 'ThreadedGUI' with macro to activate threaded gadget commands

Code:
;-TOP

; Comment: Thread To GUI Help Macro
; Author : mk-soft
; Version: v1.17
; Created: 16.07.2016
; Updated: 14.03.2018
; Link En: http://www.purebasic.fr/english/viewtopic.php?f=12&t=66180
; Link De: http://www.purebasic.fr/german/viewtopic.php?f=8&t=29728

; ***************************************************************************************

;- Begin Module Macros

DeclareModule ThreadedGUI
 
  ; Macros switch to ThreadToGUI
 
  ; Window
  Macro DisableWindow(Window, State)
    ThreadToGUI::DoDisableWindow(Window, State)
  EndMacro
  Macro HideWindow(Window, State, Flags)
    ThreadToGUI::DoHideWindow(Window, State, Flags)
  EndMacro
  Macro SetActiveWindow(Window)
    ThreadToGUI::DoSetActiveWindow(Window)
  EndMacro
  Macro SetWindowColor(Window, Color)
    ThreadToGUI::DoSetWindowColor(Window, Color)
  EndMacro
  Macro SetWindowData(Window, Value)
    ThreadToGUI::DoSetWindowData(Window, Value)
  EndMacro
  Macro SetWindowState(Window, State)
    ThreadToGUI::DoSetWindowState(Window, State)
  EndMacro
  Macro SetWindowTitle(Window, Text)
    ThreadToGUI::DoSetWindowTitle(Window, Text)
  EndMacro
  ; Menus
  Macro DisableMenuItem(Menu, MenuItem, State)
    ThreadToGUI::DoDisableMenuItem(Menu, MenuItem, State)
  EndMacro
  Macro SetMenuItemState(Menu, MenuItem, State)
    ThreadToGUI::DoSetMenuItemState(Menu, MenuItem, State)
  EndMacro
  Macro SetMenuItemText(Menu, MenuItem, Text)
    ThreadToGUI::DoSetMenuItemText(Menu, MenuItem, Text)
  EndMacro
  Macro SetMenuTitleText(Menu, Index, Text)
    ThreadToGUI::DoSetMenuTitleText(Menu, Index, Text)
  EndMacro
  ; Gadgets
  Macro AddGadgetColumn(Gadget, Postion, Text, Width)
    ThreadToGUI::DoAddGadgetColumn(Gadget, Postion, Text, Width)
  EndMacro
  Macro AddGadgetItem(Gadget, Position, Text, ImageID = 0, Flags = #PB_Ignore)
    ThreadToGUI::DoAddGadgetItem(Gadget, Position, Text, ImageID, Flags)
  EndMacro
  Macro ClearGadgetItems(Gadget)
    ThreadToGUI::DoClearGadgetItems(Gadget)
  EndMacro
  Macro ClearGadgetColumns(Gadget) ; Owner Gadget Function
    ThreadToGUI::DoClearGadgetColumns(Gadget) ; Owner Gadget Function
  EndMacro
  Macro DisableGadget(Gadget, State)
    ThreadToGUI::DoDisableGadget(Gadget, State)
  EndMacro
  Macro HideGadget(Gadget, State)
    ThreadToGUI::DoHideGadget(Gadget, State)
  EndMacro
  Macro SetActiveGadget(Gadget)
    ThreadToGUI::DoSetActiveGadget(Gadget)
  EndMacro
  Macro SetGadgetAttribute(Gadget, Attribute, Value)
    ThreadToGUI::DoSetGadgetAttribute(Gadget, Attribute, Value)
  EndMacro
  Macro SetGadgetColor(Gadget, ColorType, Color)
    ThreadToGUI::DoSetGadgetColor(Gadget, ColorType, Color)
  EndMacro
  Macro SetGadgetData(Gadget, Value)
    ThreadToGUI::DoSetGadgetData(Gadget, Value)
  EndMacro
  Macro SetGadgetFont(Gadget, FontID)
    ThreadToGUI::DoSetGadgetFont(Gadget, FontID)
  EndMacro
  Macro SetGadgetItemAttribute(Gadget, Item, Attribute, Value, Column = 0)
    ThreadToGUI::DoSetGadgetItemAttribute(Gadget, Item, Attribute, Value, Column)
  EndMacro
  Macro SetGadgetItemColor(Gadget, Item, ColorType, Color, Column = 0)
    ThreadToGUI::DoSetGadgetItemColor(Gadget, Item, ColorType, Color, Column)
  EndMacro
  Macro SetGadgetItemData(Gadget, Item, Value)
    ThreadToGUI::DoSetGadgetItemData(Gadget, Item, Value)
  EndMacro
  Macro SetGadgetItemImage(Gadget, Item, ImageID)
    ThreadToGUI::DoSetGadgetItemImage(Gadget, Item, ImageID)
  EndMacro
  Macro SetGadgetItemState(Gadget, Postion, State)
    ThreadToGUI::DoSetGadgetItemState(Gadget, Postion, State)
  EndMacro
  Macro SetGadgetItemText(Gadget, Postion, Text, Column = 0)
    ThreadToGUI::DoSetGadgetItemText(Gadget, Postion, Text, Column)
  EndMacro
  Macro SetGadgetState(Gadget, State)
    ThreadToGUI::DoSetGadgetState(Gadget, State)
  EndMacro
  Macro SetGadgetText(Gadget, Text)
    ThreadToGUI::DoSetGadgetText(Gadget, Text)
  EndMacro
  Macro ResizeGadget(Gadget, x, y, Width, Height)
    ThreadToGUI::DoResizeGadget(Gadget, x, y, Width, Height)
  EndMacro
  Macro RemoveGadgetColumn(Gadget, Column)
    ThreadToGUI::DoRemoveGadgetColumn(Gadget, Column)
  EndMacro
  Macro RemoveGadgetItem(Gadget, Position)
    ThreadToGUI::DoRemoveGadgetItem(Gadget, Position)
  EndMacro
  Macro GadgetToolTip(Gadget, Text)
    ThreadToGUI::DoGadgetToolTip(Gadget, Text)
  EndMacro
  ; Statusbar
  Macro StatusBarImage(StatusBar, Field, ImageID, Appearance = 0)
    ThreadToGUI::DoStatusBarImage(StatusBar, Field, ImageID, Appearance)
  EndMacro
  Macro StatusBarProgress(StatusBar, Field, Value, Appearance = 0, Min = #PB_Ignore, Max = #PB_Ignore)
    ThreadToGUI::DoStatusBarProgress(StatusBar, Field, Value, Appearance, Min, Max)
  EndMacro
  Macro StatusBarText(StatusBar, Field, Text, Appearance = 0)
    ThreadToGUI::DoStatusBarText(StatusBar, Field, Text, Appearance)
  EndMacro
  ; Toolbar
  Macro DisableToolBarButton(ToolBar, ButtonID, State)
    ThreadToGUI::DoDisableToolBarButton(ToolBar, ButtonID, State)
  EndMacro
  Macro SetToolBarButtonState(ToolBar, ButtonID, State)
    ThreadToGUI::DoSetToolBarButtonState(ToolBar, ButtonID, State)
  EndMacro
  ; Systray
  Macro ChangeSysTrayIcon(SysTrayIcon, ImageID)
    ThreadToGUI::DoChangeSysTrayIcon(SysTrayIcon, ImageID)
  EndMacro
  Macro SysTrayIconToolTip(SysTrayIcon, Text)
    ThreadToGUI::DoSysTrayIconToolTip(SysTrayIcon, Text)
  EndMacro
  ; Clipboard
  Macro GetClipboardImage(Image, Depth=24)
    ThreadToGUI::DoGetClipboardImage(Image, Depth)
  EndMacro
  Macro GetClipboardText()
    ThreadToGUI::DoGetClipboardText()
  EndMacro
  Macro SetClipboardImage(Image)
    ThreadToGUI::DoSetClipboardImage(Image)
  EndMacro
  Macro SetClipboardText(Texte)
    ThreadToGUI::DoSetClipboardText(Texte)
  EndMacro
  Macro ClearClipboard()
    ThreadToGUI::DoClearClipboard()
  EndMacro
  ; Requester
  Macro MessageRequester(Titel, Text, Flags=0)
    ThreadToGUI::DoMessageRequester(Titel, Text, Flags)
  EndMacro
  Macro OpenFileRequester(Titel, DefaultFile, Pattern, PatterPosition, Flags=0)
    ThreadToGUI::DoOpenFileRequester(Titel, DefaultFile, Pattern, PatterPosition, Flags)
  EndMacro
  Macro NextSelectedFileName()
    ThreadToGUI::DoNextSelectedFileName()
  EndMacro
  Macro SelectedFilePattern()
    ThreadToGUI::DoSelectedFilePattern()
  EndMacro
  Macro SaveFileRequester(Titel, DefaultFile, Pattern, PatterPosition)
    ThreadToGUI::DoSaveFileRequester(Titel, DefaultFile, Pattern, PatterPosition)
  EndMacro
  Macro PathRequester(Titel, InitialPath)
    ThreadToGUI::DoPathRequester(Titel, InitialPath)
  EndMacro
  Macro InputRequester(Titel, Message, DefaultString, Flags=0)
    ThreadToGUI::DoInputRequester(Titel, Message, DefaultString, Flags)
  EndMacro
  Macro ColorRequester(Color = $FFFFFF)
    ThreadToGUI::DoColorRequester(Color)
  EndMacro
  Macro FontRequester(FontName, FontSize, Flags, Color = 0, Style = 0)
    ThreadToGUI::DoFontRequester(FontName, FontSize, Flags, Color, Style)
  EndMacro
  Macro SelectedFontName()
    ThreadToGUI::DoSelectedFontName()
  EndMacro
  Macro SelectedFontSize()
    ThreadToGUI::DoSelectedFontSize()
  EndMacro
  Macro SelectedFontColor()
    ThreadToGUI::DoSelectedFontColor()
  EndMacro
  Macro SelectedFontStyle()
    ThreadToGUI::DoSelectedFontStyle()
  EndMacro
EndDeclareModule

Module ThreadedGUI
  ; Do nothing
EndModule

;- End Module Macros


Example
Code:
...
Procedure thFlash(id)
  Protected count, col
 
  UseModule ThreadedGUI ; Enable macros to switch all gadget commands to ThreadToGUI
 
  DisableGadget(2, #True)
  For count = 0 To 4
    For col = 0 To 3
      StatusBarProgress(0, 1, count * 20 + col * 5)
      Select col
        Case 0 : SetGadgetColor(3, #PB_Gadget_BackColor, RGB(255,0,0))
        Case 1 : SetGadgetColor(3, #PB_Gadget_BackColor, RGB(255,255,0))
        Case 2 : SetGadgetColor(3, #PB_Gadget_BackColor, RGB(0,255,0))
        Case 3 : SetGadgetColor(3, #PB_Gadget_BackColor, RGB(255,255,255))
      EndSelect
      Delay(1000)
    Next
  Next
  StatusBarProgress(0, 1, 100)
  DisableGadget(2, #False)
 
  UnuseModule ThreadedGUI ; Don´t forget to disable the macros
 
EndProcedure
...


P.S. Updated Example 1

Author:  mk-soft [ Mon May 21, 2018 12:03 pm ]
Post subject:  Re: Module ThreadToGUI-Update windows,gadgets,... from threa

Update v1.19
- Added DoDisplayPopupMenu

:wink:

Author:  davido [ Sat Dec 08, 2018 11:23 am ]
Post subject:  Re: Module ThreadToGUI-Update windows,gadgets,... from threa

@mk-soft
I found this very helpful.
Thank you for sharing. :D

Page 2 of 2 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/