How to draw a focus indictor on a drawing area

Hello everyone,

I have a custom widget (an image viewer implemented as a GtkDrawingArea subclass) which has some keyboard bindings, so I need a focus indicator.

What’s the best way to do this? Should I use GtkOverlay to float some other widget over my drawing area and rely on that to draw the indicator? What would be the best widget to float on top?

Hi,

gtk3 or gtk4?
Do you want the focus indicator on the whole DrawingArea, or on a smaller element?

Hi @gwllems,

Sorry, gtk4, I should have said.

My image viewer looks like this:

So I’m currently drawing the focus indicator as a thin line just inside the window in my _snapshot() handler. It’d be much better if I could get gtk to draw it for me, of course!

OK, then the easiest way IMHO is to use a custom CSS:

drawingarea:focus {
	outline-color: @theme_selected_bg_color;
	outline-offset: -3px;
	outline-width: 2px;
	outline-style: dotted;
}