GTK-EventCallback and Unicode

Linux specific forum
gerd
User
User
Posts: 94
Joined: Tue Feb 23, 2010 7:50 pm
Location: Germany

GTK-EventCallback and Unicode

Post by gerd »

Hope somebody can help. The following code works fine in ascii mode, but not with unicode.
What do I have to do, to make it work in unicode?

Code: Select all

; Wrapper to call the correct function for Gtk.

Procedure GTKSignalConnect(*Widget, Signal$, Function, user_data)
    g_signal_connect_data_(*Widget, @Signal$, Function, user_data, 0, 0)
EndProcedure

; This is a signal callback. Note the CDLL declaration. that is important.
; the user_data field helps to pass additional data.
;
ProcedureCDLL EventCallback(*Widget, *Event.GdkEventCrossing, user_data)
  If user_data = 1
    Debug "Enter Event"
  Else
    Debug "Leave Event"
  EndIf
EndProcedure

If OpenWindow(0,0,0,345,105,"Event Test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  myButton = ButtonGadget(#PB_Any, 200, 10, 100, 30, "Test")

; connect the signals...
GTKSignalConnect(GadgetID(myButton), "enter-notify-event", @EventCallback(), 1)
GTKSignalConnect(GadgetID(myButton), "leave-notify-event", @EventCallback(), 2)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow : EndIf 
Thanks
gerd
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: GTK-EventCallback and Unicode

Post by ts-soft »

You can import the api with prototypes or use this:

Code: Select all

; Wrapper to call the correct function for Gtk.

Procedure GTKSignalConnect(*Widget, Signal$, Function, user_data)
  Protected *buffer = AllocateMemory(StringByteLength(Signal$, #PB_Ascii) + 1)
  If *buffer
    PokeS(*buffer, Signal$, -1, #PB_Ascii)
    g_signal_connect_data_(*Widget, *buffer, Function, user_data, 0, 0)
    FreeMemory(*buffer)
  EndIf
EndProcedure

; This is a signal callback. Note the CDLL declaration. that is important.
; the user_data field helps to pass additional data.
;
ProcedureCDLL EventCallback(*Widget, *Event.GdkEventCrossing, user_data)
  If user_data = 1
    Debug "Enter Event"
  Else
    Debug "Leave Event"
  EndIf
EndProcedure

If OpenWindow(0,0,0,345,105,"Event Test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  myButton = ButtonGadget(#PB_Any, 200, 10, 100, 30, "Test")

; connect the signals...
GTKSignalConnect(GadgetID(myButton), "enter-notify-event", @EventCallback(), 1)
GTKSignalConnect(GadgetID(myButton), "leave-notify-event", @EventCallback(), 2)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow : EndIf 
Greetings - Thomas
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
gerd
User
User
Posts: 94
Joined: Tue Feb 23, 2010 7:50 pm
Location: Germany

Re: GTK-EventCallback and Unicode

Post by gerd »

Thank you Thomas
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Re: GTK-EventCallback and Unicode

Post by uwekel »

Hi Gerd,

you do not need a wrapper routine and you must not convert the event name string. Just use the pseudo-type ".p-utf8" as in the code below. This sample works in Ascii and Unicode mode :-)

Code: Select all

; Wrapper to call the correct function for Gtk.

ImportC ""
  g_signal_connect_data.i(*instance, detailed_signal.p-utf8, *c_handler, *data_=0, *destroy_data=0, *connect_flags=0)
EndImport

ProcedureC EventCallback(*Widget, *Event.GdkEventCrossing, user_data)
  If user_data = 1
    Debug "Enter Event"
  Else
    Debug "Leave Event"
  EndIf
EndProcedure

OpenWindow(0, 0, 0, 345, 105, "Event Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
*b = GadgetID(ButtonGadget(#PB_Any, 200, 10, 100, 30, "Test"))
g_signal_connect_data(*b, "enter-notify-event", @EventCallback(), 1)
g_signal_connect_data(*b, "leave-notify-event", @EventCallback(), 2)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Best Regards
Uwe
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
gerd
User
User
Posts: 94
Joined: Tue Feb 23, 2010 7:50 pm
Location: Germany

Re: GTK-EventCallback and Unicode

Post by gerd »

Hi Uwe,

thanks for your reply. Your code works fine too.

Thanks
gerd
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Re: GTK-EventCallback and Unicode

Post by Poshu »

Out of sheer curiosity, where could I find a list of available signal? The Gnome Dev Center isn't of much help here...
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Re: GTK-EventCallback and Unicode

Post by uwekel »

For the PB gadgets you have to lookup the events fro the GTK widgets.
A good entry point is this: http://developer.gnome.org/gtk2/stable/
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
Post Reply