Linux-PureBasic-SpinGadget tweak for improved functionallity

Linux specific forum
Oma
Enthusiast
Enthusiast
Posts: 312
Joined: Thu Jun 26, 2014 9:17 am
Location: Germany

Linux-PureBasic-SpinGadget tweak for improved functionallity

Post by Oma »

Hi!
The implementation of the numeric SpinGadget on Linux is a PureBasic-own construct to maintain the compatibility. Unfortunately the functionality is very simplified against the API or Windows. This makes the handling a cumbersome matter.
see:
http://www.purebasic.fr/english/viewtop ... =3&t=63627
http://www.purebasic.fr/english/viewtop ... 13&t=60658

No value change with mousewheel or keyboard up/down-keys ist possible, and the mouseclick-repeat on up/down-arrows in the Gadget does not work either.

A simple tweak to a up/down-arrow-repeat on a permanent click (w/o singleclicks) has not yet succeeded to me!
But for the rest i've found a workaround for the PureBasic-SpinGadget on gtk2/gtk3.

1. Include the marked part in the code example,
2. and finally you only have to make an additional call of SpinGadget_Tweak(Window, Gadget) for each SpinGadget ...

I hope the team could be inspired thereof and implement something similar in the standard SpinGadget :wink: .

Code: Select all

EnableExplicit

; Object constants
#Win_Main  = 0

Enumeration
	#But1
	#SpBG1
	#SpBG2
	#But2
EndEnumeration

Global.i gEvent, gEventType, gQuit

;- Part for tweaked SpinGadgets ...
CompilerIf #PB_Compiler_OS = #PB_OS_Linux
	
	ImportC ""
		g_signal_connect(*instance, detailed_signal.p-utf8, *c_handler, *data, destroy= 0, flags= 0) As "g_signal_connect_data"
	EndImport
	
	;see https://git.gnome.org/browse/gtk+/plain/gdk/gdkkeysyms.h
	#GDK_KEY_Up  = $FF52
	#GDK_KEY_Down= $FF54
	
	Structure CALLBACKDATA
		GadgetNo.i
		WindowNo.i
	EndStructure
	Global NewList GadgetData.CALLBACKDATA() 
	
	ProcedureC Callback_SpinGadgetScroll(*widget.GtkWidget, *Event.GdkEventAny, *user_data.CALLBACKDATA); match callback name with the call in SpinGadget_Tweak
		Protected *ev_scroll.GdkEventScroll
		Protected *ev_keypress.GdkEventKey
		Protected Ret= #False
		
		If *Event\type = #GDK_SCROLL
			*ev_scroll= *Event
		ElseIf *Event\type = #GDK_KEY_PRESS
			*ev_keypress= *Event
		EndIf
		If (*ev_scroll And *ev_scroll\direction = #GDK_SCROLL_UP) Or (*ev_keypress And *ev_keypress\keyval = #GDK_KEY_Up)
			SetGadgetText(*user_data\GadgetNo, Str(Val(GetGadgetText(*user_data\GadgetNo))+ 1))
			PostEvent(#PB_Event_Gadget, *user_data\WindowNo, *user_data\GadgetNo, #PB_EventType_Up)
			PostEvent(#PB_Event_Gadget, *user_data\WindowNo, *user_data\GadgetNo, #PB_EventType_Change)
			Ret= #True;                                                    avoids focus change on up/down key
		ElseIf (*ev_scroll And *ev_scroll\direction = #GDK_SCROLL_DOWN) Or (*ev_keypress And *ev_keypress\keyval = #GDK_KEY_Down)
			SetGadgetText(*user_data\GadgetNo, Str(Val(GetGadgetText(*user_data\GadgetNo))- 1))
			PostEvent(#PB_Event_Gadget, *user_data\WindowNo, *user_data\GadgetNo, #PB_EventType_Down)
			PostEvent(#PB_Event_Gadget, *user_data\WindowNo, *user_data\GadgetNo, #PB_EventType_Change)
			Ret= #True;                                                    avoids focus change on up/down key
		EndIf
		ProcedureReturn Ret
	EndProcedure
	
CompilerEndIf

Procedure SpinGadget_Tweak(Window, Gadget)
	CompilerIf #PB_Compiler_OS = #PB_OS_Linux
		If GadgetType(Gadget) = #PB_GadgetType_Spin
			Protected *entry.GtkEntry = g_list_nth_data_(gtk_container_get_children_(GadgetID(Gadget)), 0)
			
			AddElement(GadgetData())
			GadgetData()\GadgetNo= Gadget
			GadgetData()\WindowNo= Window
			
			gtk_widget_add_events_(*entry, #GDK_SCROLL_MASK | #GDK_KEY_PRESS_MASK)
			g_signal_connect_(*entry, "scroll-event", @Callback_SpinGadgetScroll(), @GadgetData());    event on wheelscrolling over entry
			g_signal_connect_(*entry, "key-press-event", @Callback_SpinGadgetScroll(), @GadgetData()); event on key up/down on focussed entry
		EndIf
	CompilerEndIf
EndProcedure
;- ... End of part

Procedure Create_WinMain()
	If OpenWindow(#Win_Main, 300, 200, 600, 200, "PB-SpinGadget w. scrollwheel + up/down-key support", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
		ButtonGadget  (#But1,     5,   5, 100,  26, "Gadget in front")
		TextGadget    (#PB_Any,   5,  45, 190,  22, "Tweaked PB-StringGadget")
		SpinGadget    (#SpBG1,  200,  40, 100,  26, 0, 10, #PB_Spin_Numeric)
		TextGadget    (#PB_Any,   5,  80, 190,  22, "Standard PB-StringGadget")
		SpinGadget    (#SpBG2,  200,  75, 100,  26, 0, 10, #PB_Spin_Numeric)
		ButtonGadget  (#But2,     5, 110, 100,  26, "Gadget rear")
		
		SetGadgetState(#SpBG1,    5)
		SetGadgetState(#SpBG2,    5)
		
		SpinGadget_Tweak(#Win_Main, #SpBG1);                             add this call for each SpinGadget you wanna tweak
		
		GadgetToolTip(#SpBG1, "Use mousewheel on hover or up/down-keys on focused SpinGadget")
	EndIf
EndProcedure

Create_WinMain()

Repeat
	gEvent= WaitWindowEvent()
	
	Select gEvent
		Case #PB_Event_CloseWindow
			gQuit= #True
			
		Case #PB_Event_Gadget
			If EventGadget() = #SpBG1
				gEventType= EventType()
				If gEventType = #PB_EventType_Up
					Debug "SpinGadget up"
				ElseIf gEventType = #PB_EventType_Down
					Debug "SpinGadget down"
				ElseIf gEventType = #PB_EventType_Change
					Debug "SpinGadget change"
					Debug " ---"
				EndIf
			EndIf
			
	EndSelect
	
Until gQuit
Regards, Charly

ps: 28-01-2018:
Fixed a bug with a LinkedList instead of the static structure, which caused the workaround to only work on the last SpinGadget!
Last edited by Oma on Sun Jan 28, 2018 8:02 am, edited 2 times in total.
PureBasic 5.4-5.7, Linux: (X/L/K)Ubuntus+Mint - Windows XP (32Bit)
PureBasic Linux-API-Library & Viewer: http://www.chabba.de
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Linux-PureBasic-SpinGadget tweak for improved functional

Post by Little John »

Hi Charly,

your code works fine here with PB 5.45 LTS (x86) on Linux Mint 18.2 Cinnamon (tested with GTK 3).
Thank you!

Just for the record: I already made a corresponding feature request.
User avatar
Shardik
Addict
Addict
Posts: 1988
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Linux-PureBasic-SpinGadget tweak for improved functional

Post by Shardik »

You may also take a look into my workarounds from January 2016 which modify PureBasic's numerical SpinGadget to also work with the vertical mouse wheel in GTK+ 2 or 3 (two examples) which also take into account the padding inside the GtkEntry (in order to work equally well with different desktop managers and themes). The last simplified example works with both GTK+ 2 and 3 but doesn't take padding into account.

A further example in the same thread demonstrates how to utilize GTK's native spin button working in both GTK+ 2 and 3.
Oma
Enthusiast
Enthusiast
Posts: 312
Joined: Thu Jun 26, 2014 9:17 am
Location: Germany

Re: Linux-PureBasic-SpinGadget tweak for improved functional

Post by Oma »

@Little John
Good to know. Thanks for the feedback - and your link.
(And thank you for taking the remaining vector icons into your collection.)

@Shardik
I did not expect to find such a code in Requests and Wishlists. Works fine too.
The good thing is (after a short modification on all our codes) ...
With small changes, the mousewheel scrolling works directly on (numeric) StringGadgets too. We no longer need real SpinGadgets :wink:

I'll put all related codes into the API collection.

PS: In the meanwhile the first links to forum threads are integrated in my Linux-API-codes and the Linux-API-Viewer's editor now supports (and opens) Hyperlinks too.
Comes all with the next christmas update (i hope). Am I a good boy?
PureBasic 5.4-5.7, Linux: (X/L/K)Ubuntus+Mint - Windows XP (32Bit)
PureBasic Linux-API-Library & Viewer: http://www.chabba.de
User avatar
Shardik
Addict
Addict
Posts: 1988
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Linux-PureBasic-SpinGadget tweak for improved functional

Post by Shardik »

Oma wrote:I did not expect to find such a code in Requests and Wishlists.
When posting a wish in Requests and Wishlists I often post a workaround because I hope that my wish will be implemented sooner. The developers have less work to find and study the required API functions and it should require less time to implement my proposal... :wink:
mdp
Enthusiast
Enthusiast
Posts: 115
Joined: Mon Apr 18, 2005 8:28 pm

Re: Linux-PureBasic-SpinGadget tweak for improved functional

Post by mdp »

Unfortunately, the code does not work.

Try "tweaking" both the gadges...

Code: Select all

      SpinGadget_Tweak(#Win_Main, #SpBG1);                             add this call for each SpinGadget you wanna tweak
      SpinGadget_Tweak(#Win_Main, #SpBG2);                             add this call for each SpinGadget you wanna tweak
Events intended for the first gadget will work on the second one.

Create n SpinGadgets, apply the enhancer SpinGadget_Tweak() to all, the last one will be affected by actions effected in any - the last one will steal other gadgets' events.
Oma
Enthusiast
Enthusiast
Posts: 312
Joined: Thu Jun 26, 2014 9:17 am
Location: Germany

Re: Linux-PureBasic-SpinGadget tweak for improved functional

Post by Oma »

Hello mdb,
yes you're right, sorry. I didn't notice it in the final version.
It would be necessary to create a new structure for each SpinGadget - but it's not very comfortable and acceptable.

Until i find a better solution to pass individual Gadget- AND Window numbers to callbacks, and
if you don't need the GadgetData für the SpinGadgets, you could try this first fix for more than one used SpinGadget (the structure has been eliminated) ...
Deleted, because the interim solution is now superfluous ...

Code: Select all

;Removed, see the following post
Generally it would be very helpfull to get a PB native function like GetGadgetWindow() which could deliver the PB window number (not the WindowID / API address). It is needed more often.

Regards, Charly
Last edited by Oma on Sun Jan 28, 2018 8:13 am, edited 1 time in total.
PureBasic 5.4-5.7, Linux: (X/L/K)Ubuntus+Mint - Windows XP (32Bit)
PureBasic Linux-API-Library & Viewer: http://www.chabba.de
Oma
Enthusiast
Enthusiast
Posts: 312
Joined: Thu Jun 26, 2014 9:17 am
Location: Germany

Re: Linux-PureBasic-SpinGadget tweak for improved functional

Post by Oma »

Update in the code of the first posting.
I finally fixed the problem with a LinkedList instead of the static structure, which caused the workaround to only work on the last SpinGadget! The GadgetData() will no longer be used for the fix and and it's as easy as before.

Regards, Charly
PureBasic 5.4-5.7, Linux: (X/L/K)Ubuntus+Mint - Windows XP (32Bit)
PureBasic Linux-API-Library & Viewer: http://www.chabba.de
Post Reply