Linux - Fixed Width Combobox

Share your advanced PureBasic knowledge/code with the community.
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Linux - Fixed Width Combobox

Post by Justin »

The PB Linux combobox resizes itself to fit its contents, it can be a problem, this version used a fixed width like the windows version.
The code is crossplatform just use ComboEx_Create() and the regular combobox PB functions.

Code: Select all

;Fixed width combobox
;Justin 2019

EnableExplicit

	
CompilerIf #PB_Compiler_OS = #PB_OS_Linux
#G_TYPE_INT = 24

Enumeration
	#PANGO_ELLIPSIZE_NONE
	#PANGO_ELLIPSIZE_START
	#PANGO_ELLIPSIZE_MIDDLE
	#PANGO_ELLIPSIZE_END
EndEnumeration

;- IMPORTS
ImportC ""
	g_object_set_property(obj.i, prop.p-utf8, v.i)
	gtk_cell_layout_get_cells_(cl.i) As "gtk_cell_layout_get_cells"
EndImport
CompilerEndIf

Procedure ComboEx_Create(id.i, x.l, y.l, width.l, height.l, flags.l = 0)
	CompilerIf #PB_Compiler_OS = #PB_OS_Linux
		Define.i gdCB, wdCB, cellRdr
		Define.GList *cells
		Define.GValue gvWidth, gvEllipMode
		
		g_value_init_(@gvWidth, #G_TYPE_INT)
		g_value_init_(@gvEllipMode, #G_TYPE_INT)
		
		;Combo gadget
		gdCB = ComboBoxGadget(id, x, y, width, height, flags)
		If id = #PB_Any
			wdCB = GadgetID(gdCB)
			
		Else
			wdCB = GadgetID(id)
		EndIf

		;Get cell renderers, first is GtkCellRendererPixbuf, second GtkCellRendererText
		*cells = gtk_cell_layout_get_cells_(wdCB)
		;Get GtkCellRendererText
		cellRdr = *cells\next\data
		g_list_free_(*cells)
		
	  ;Setting the GtkCellRendererText width to 1 takes the combobox width 
		g_value_set_int_(gvWidth, 1)
		g_object_set_property(cellRdr, "width", gvWidth)
		;Ellipsize mode
		g_value_set_int_(gvEllipMode, #PANGO_ELLIPSIZE_END)
		g_object_set_property(cellRdr, "ellipsize", gvEllipMode)
	
		ProcedureReturn gdCB
	
	CompilerElse
		ProcedureReturn ComboBoxGadget(id, x, y, width, height, flags)
	CompilerEndIf
EndProcedure

;- TEST
Define.i ev, cb, fnt, fnt2, cb2, btn1, cb3

OpenWindow(#PB_Any, 0, 0, 600, 400, "", #PB_Window_SystemMenu)
btn1 = ButtonGadget(#PB_Any, 10, 80, 150, 30, "Test")

cb3 = ComboEx_Create(#PB_Any, 10, 150, 150, 30)
AddGadgetItem(cb3, -1, "ComboEx")
AddGadgetItem(cb3, -1, "yyyyyy")
SetGadgetState(cb3, 0)

cb2 = ComboBoxGadget(#PB_Any, 10, 10, 150, 30)
AddGadgetItem(cb2, -1, "PB Combo")
AddGadgetItem(cb2, -1, "yyyyyy")
SetGadgetState(cb2, 0)

Repeat
	ev = WaitWindowEvent()
	Select ev
		Case #PB_Event_Gadget
			Select EventGadget()
				Case btn1
					AddGadgetItem(cb3, -1, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
					AddGadgetItem(cb2, -1, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
					SetGadgetState(cb2, 2)
					SetGadgetState(cb3, 2)

			EndSelect
	EndSelect
Until ev = #PB_Event_CloseWindow
Last edited by Justin on Thu Aug 15, 2019 7:31 pm, edited 1 time in total.
Oma
Enthusiast
Enthusiast
Posts: 312
Joined: Thu Jun 26, 2014 9:17 am
Location: Germany

Re: Linux - Fixed Width Combobox

Post by Oma »

Thank you Justin,

also an unconventional solution, but seems to be working.

Charly
PureBasic 5.4-5.7, Linux: (X/L/K)Ubuntus+Mint - Windows XP (32Bit)
PureBasic Linux-API-Library & Viewer: http://www.chabba.de
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: Linux - Fixed Width Combobox

Post by Justin »

Yes, I spent some time with that, setting the GtkCellRendererText width to anything smaller than the combo width minus the arrow width takes the whole combo width, getting the arrow button width is very tricky so 1 always works.

Tested on Mint 32/64.
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Linux - Fixed Width Combobox

Post by Shardik »

You have found a pretty nice solution to the ComboBox's variable width problem in GTK3. Your example even works in GTK2 even though it's not necessary because in GTK2 the variable width problem doesn't occur.

Two small improvements: the variable PixRdr is not needed and may be deleted, but more important you should free your GList *cells which is causing a memory leak:
GTK3 reference for gtk-cell-layout-get-cells wrote:The list, but not the renderers has been newly allocated and should be freed with g_list_free() when no longer needed.
So you should change

Code: Select all

      *cells = gtk_cell_layout_get_cells_(wdCB)
      pxRdr = *cells\data
     *cells = *cells\next
     cellRdr = *cells\data
to

Code: Select all

      *cells = gtk_cell_layout_get_cells_(wdCB)
      cellRdr = *cells\next\data
      g_list_free_(*cells)
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: Linux - Fixed Width Combobox

Post by Justin »

Hi Shardik, i left the pxBuf for clearness, i have removed it and commented the code.
thanks for the free stuff, i missed it
Post Reply