GTK and EVENTS

General FreeBASIC programming questions.
VANYA
Posts: 1309
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

GTK and EVENTS

Postby VANYA » Dec 27, 2018 7:12

Hi All!

My task is to capture events from GTK in the procedure and to determine their type and properties. For this action the gtk_main function is not suitable.
I'm trying to capture events using the functions:
1) gtk_events_pending
2) gtk_main_iteration | gtk_main_iteration_do(0)
3) gtk_get_current_event

Gtk_get_current_event function should return a pointer to the GDKEvent structure , but it always returns null. WHY!!!!!!

Code: Select all

#INCLUDE "gtk/gtk.bi"

dim shared as Long exitcode

sub closeApp cdecl(Gwindow as GtkWidget ptr,  data_ as gpointer)
   
   exitcode = 1
   
   sleep(50)
   
   gtk_main_quit()
   
End Sub

Sub EVENTS_BUTTON()
   
   Beep()
   
End Sub

function allevent() as Long
   
   if gtk_events_pending() then
      
      dim as GDKEvent ptr ev
      
      ev  = gtk_get_current_event ()
      
      ?  ev '''''''''''''''''''' why is it always returns  NULL ??????

      gtk_main_iteration_do(0)
      
      
   endif      
   
End Function


Dim As GtkWidget Ptr window_
Dim As GtkWidget Ptr vbox
Dim As GtkWidget Ptr button1
gtk_init (0, 0)
window_ = gtk_window_new (GTK_WINDOW_TOPLEVEL)
gtk_window_set_title(GTK_WINDOW(window_),"")
gtk_window_set_default_size(window_, 100, 60)
gtk_window_set_position(GTK_WINDOW(window_), GTK_WIN_POS_CENTER)
vbox = gtk_fixed_new()
gtk_container_add (GTK_CONTAINER(window_), vbox)
button1 = gtk_button_new_with_label(g_locale_to_utf8("Сделать BEEP",Len("Сделать BEEP"),0,0,0))
gtk_widget_set_size_request(button1, 90, 25)
gtk_fixed_put(GTK_FIXED(vbox), button1, 15, 10)
g_signal_connect(G_OBJECT(window_), "destroy", G_CALLBACK (@closeApp), 0)
g_signal_connect(G_OBJECT(button1), "clicked", G_CALLBACK (@EVENTS_BUTTON), window_)
gtk_widget_show_all (window_)


do
   

   sleep(15)
   
   allevent
   
   
Loop until exitcode = 1
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: GTK and EVENTS

Postby sancho3 » Dec 28, 2018 0:05

I know very little to nothing about GTK, so take this as a stab in the dark.
I think it is because the event is already over before it gets to your sub.
If you move the code into the button handler then it returns a value.

Code: Select all

Sub EVENTS_BUTTON()
   
      dim as GDKEvent ptr ev
     
      ev  = gtk_get_current_event ()
   ?"mooooooo";ev
   
End Sub
VANYA
Posts: 1309
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: GTK and EVENTS

Postby VANYA » Dec 28, 2018 4:27

Yes, in the callback function events are caught, but I need to catch them separately (not dependent ) from callback function . That is, I'm looking for an analogue win32 api GetMessage | PeekMessage .
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Re: GTK and EVENTS

Postby D.J.Peters » Dec 28, 2018 6:21

deleted I misunderstood the question :-)

Joshy
Last edited by D.J.Peters on Dec 28, 2018 10:55, edited 1 time in total.
VANYA
Posts: 1309
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: GTK and EVENTS

Postby VANYA » Dec 28, 2018 10:09

In principle I understand. Thanks to everyone who posted in the topic. I asked to try this:

Code: Select all

#INCLUDE "gtk/gtk.bi"

#define EventGadget &h401

#define EventClose &h10

type TLikeEvent
   
   w as GtkWidget ptr ' widget
   
   EventNumber as Long ' number
   
   event_ as long ' event   
   
End Type

dim shared as Long index , EventNumber  , flaginit

dim shared as GtkWidget ptr EventHwnd , CurrentHwnd , mainBox

dim shared as TLikeEvent DIMSTOREEVENT(1000)

function closeApp cdecl(Gwindow as GtkWidget ptr, event_ as GdkEvent, data_ as gpointer) as gBoolean
   
   DIMSTOREEVENT(index).w = Gwindow
   
   DIMSTOREEVENT(index).event_ = EventClose
   
   index+=1   
   
   return TRUE
   
End function

Sub EVENTS_BUTTON cdecl(Gwindow as GtkWidget ptr,  data_ as gpointer)
   
   DIMSTOREEVENT(index).w = Gwindow
   
   DIMSTOREEVENT(index).EventNumber = *cast(Long ptr, data_)
   
   DIMSTOREEVENT(index).event_ = EventGadget
   
   index+=1
   
End Sub

function WindowEvent() as Long
   
   if gtk_events_pending() then      
      
      if index > 0 then                   
         
         if DIMSTOREEVENT(index-1).event_ = EventGadget then
            
            EventNumber = DIMSTOREEVENT(index -1).EventNumber
            
            index-=1   
            
            gtk_main_iteration_do(0)   
            
            return EventGadget   
            
         elseif    DIMSTOREEVENT(index-1).event_ = EventClose then
            
            EventHwnd = DIMSTOREEVENT(index-1).w
            
            index-=1   
            
            gtk_main_iteration_do(0)
            
            return EventClose            
            
         EndIf
         
      EndIf      
      
      gtk_main_iteration_do(0)
      
      
   endif     
   
End Function

function OpenWindow (sName as string , x as Long , y as Long , width_ as Long , height_ as Long) as GtkWidget Ptr
   
   if flaginit = 0 then
      
      gtk_init (0, 0)
      
      flaginit = 1
      
   EndIf
   
   CurrentHwnd = gtk_window_new (GTK_WINDOW_TOPLEVEL)
   
   gtk_window_set_title(GTK_WINDOW(CurrentHwnd),strptr(sName))
   
   gtk_window_set_default_size(GTK_WINDOW(CurrentHwnd), width_, height_)
   
   gtk_window_set_position(GTK_WINDOW(CurrentHwnd), GTK_WIN_POS_NONE)
   
   gtk_window_move(GTK_WINDOW(CurrentHwnd), x , y )
   
   g_signal_connect(G_OBJECT(CurrentHwnd), "delete-event", G_CALLBACK (@closeApp), 0)
   
   mainBox = gtk_fixed_new()
   
   gtk_container_add (GTK_CONTAINER(CurrentHwnd), mainBox)
   
   return CurrentHwnd
   
End Function

function ButtonGadget(Byref number as Long , x as Long , y as Long , width_ as Long , height_ as Long , sText as string) as GtkWidget Ptr
   
   Dim As GtkWidget Ptr button , vBox
   
   vbox = gtk_fixed_new()
   
   gtk_container_add (GTK_CONTAINER(mainBox), vbox)
   
   button = gtk_button_new_with_label(g_locale_to_utf8(sText,Len(sText),0,0,0))
   
   gtk_widget_set_size_request(button, width_, height_)
   
   gtk_fixed_put(GTK_FIXED(vbox), button, x , y)
   
   g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK (@EVENTS_BUTTON), @number)
   
   gtk_widget_show_all (CurrentHwnd)
   
   return button
   
End Function

sub MessBox(sCaption as String , sText as String)   

   if flaginit = 0 then
      
      gtk_init (0, 0)
      
      flaginit = 1
      
   EndIf
   
   Dim Box As GtkWidget Ptr
   
   Box = gtk_message_dialog_new(0, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, g_locale_to_utf8(sText,Len(sText) ,0,0,0) )
   
   gtk_window_set_title(GTK_WINDOW(Box), g_locale_to_utf8(sCaption,Len(sCaption),0,0,0))
   
   gtk_dialog_run(GTK_DIALOG(Box))
   
   gtk_widget_destroy(Box)
   
end sub

'/////////////////////////////////////////////////////////////////////////////////////////////
'////////////////////////////////////////////////////////////////////////////////////////////
'/////////////////////////////////////////////////////////////////////////////////////////////
'////////////////////////////////////////////////////////////////////////////////////////////

' EXAMPLE



' 1 window
OpenWindow("window" , 100, 300, 400, 200)
ButtonGadget(1 , 10 , 10, 70, 25, "Button 1")
ButtonGadget(2 , 120 , 90, 70, 25, "Button 2")
' 2 window
OpenWindow("window 2" , 150, 350, 400, 200)
ButtonGadget(3 , 10 , 10, 70, 25, "Button 3")
ButtonGadget(4 , 10 , 40, 70, 25, "Button 4")


do
   
   
   var ev = WindowEvent
   
   if ev = EventGadget then
      
      MessBox ( "Message!!!" , "Button number " & EventNumber   )
      
   elseif  ev = EventClose then

      MessBox ( "Message!!!" , "GOODBYE" )
      
      exit do
      
   EndIf
   
   sleep(15)
   
Loop
 


Mischief of course :)))))

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest