在自定义小部件周围绘制焦点框

Draw a focus frame around a custom widget

GtkEntry 聚焦时周围有一个漂亮的圆角框。我如何让 GTK 在我自己的小部件(构建在 GtkDrawArea 之上)获得焦点时围绕它绘制这样的框架?

我浏览了 GtkContainer 的所有后代,但没有这样做。有 GtkFrame 但它有另一个用途并且看起来不同。

先阅读 documentation of GTK3. Download its source code,然后研究它以获得灵感。

GtkWidget abstract superclass has some “focus-in-event” GTK signal and some “focus-out-event” signal and some “draw” GTK signal;你可以明确地处理它们。

您的绘图例程将使用 GDK 库。您可以让自己的小部件成为 GtkWidgetGtkBin

的子类

注意 GTK 是开源的,

这样你就可以学习(获得灵感)gtk/gtkentry.c的源代码。

您的 GTK 信号处理程序可能会使用 g_idle_add to postpone the drawing when nothing needs to run in the GTK event loop

如果你用GCC, be sure to enable all warnings and debug info, so use gcc -Wall -Wextra -g (and perhaps -O). You many need to use the GDB debugger编译。

在你的GtkDrawingArea的draw信号中,使用gtk_render_focus()在合适的地方绘制焦点框。