Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
945 views
in Technique[技术] by (71.8m points)

gtk3 - gtk image-button scaling of image and handling size requests

I am trying to use some buttons containing images (pixbuf) with the following properties:

  • The width of a button is determined by its parent, in my case it takes the full width of a column in an evenly sized grid
  • A button displays a pixbuf
  • Such a pixbuf is evenly scaled such that the whole button width is being used.

As a result, the height of a button has to be chosen according to its width determined by its parent and the contained pixbuf's aspect ratio. Unfortunately, I am unable to find a clean solution to achieve such behaviour.

My current approach is to use the size-allocate signal of the button to scale the pixbuf. While this approach would work if I only used the allocation for the button, in the case stated above I only want to use the width of the allocation to determine the height to use by the given pixbuf's aspect-ratio. Thus, the signal handler is able to set the image's height to a height violating the button's current height. This is not legit as the aspect-ratio should be used while determining the required space for the button and not when it already is allocated. Using my unlegit approach, the button is too small to display the image and only resizes to the desired size if it is somehow updated by user interaction (queueing redraw or allocate do not fix this).

I would like to use an approach where I use the width which will be assigned to the button to determine its height before allocation. I cannot find useful signals to do so as preferred size etc. are only set to specific values while offering no possibilities to be linked to some signal as far as I see.

Another approach could be to use an AspectFrame. I do not really want to use that as it adds additional complexity by having to set the ratio of it each time an image is loaded just to enable a dynamic height of a button. Update: It also does not work as it shrinks to fit instead of extending.

Using hard size requests does not seem like a good idea as the width depends on the parent widget.

I tried to subclass the button but found out that for the Rust binding gtk-rs buttons currently are not subclassable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...