It is currently Fri Aug 23, 2019 12:52 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Linux - Fixed Width Combobox
PostPosted: Wed Aug 14, 2019 4:51 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Apr 26, 2003 2:49 pm
Posts: 609
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:
;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.

Top
 Profile  
Reply with quote  
 Post subject: Re: Linux - Fixed Width Combobox
PostPosted: Thu Aug 15, 2019 6:27 am 
Offline
Enthusiast
Enthusiast

Joined: Thu Jun 26, 2014 9:17 am
Posts: 306
Location: Germany
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Linux - Fixed Width Combobox
PostPosted: Thu Aug 15, 2019 10:18 am 
Offline
Enthusiast
Enthusiast

Joined: Sat Apr 26, 2003 2:49 pm
Posts: 609
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Linux - Fixed Width Combobox
PostPosted: Thu Aug 15, 2019 10:41 am 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 1643
Location: Germany
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:
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:
      *cells = gtk_cell_layout_get_cells_(wdCB)
      pxRdr = *cells\data
     *cells = *cells\next
     cellRdr = *cells\data
to
Code:
      *cells = gtk_cell_layout_get_cells_(wdCB)
      cellRdr = *cells\next\data
      g_list_free_(*cells)


Top
 Profile  
Reply with quote  
 Post subject: Re: Linux - Fixed Width Combobox
PostPosted: Thu Aug 15, 2019 7:33 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Apr 26, 2003 2:49 pm
Posts: 609
Hi Shardik, i left the pxBuf for clearness, i have removed it and commented the code.
thanks for the free stuff, i missed it


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: mk-soft and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye