bug 1234158 share code to check and set sWidgetStorage r?stransky
With array-based storage, it is easier to share this code and simply widget
creation functions.
MozReview-Commit-ID: ICWFN57AF8g
--- a/widget/gtk/WidgetStyleCache.cpp
+++ b/widget/gtk/WidgetStyleCache.cpp
@@ -19,77 +19,76 @@ static GtkStyleContext* sStyleStorage[MO
static bool sStyleContextNeedsRestore;
static GtkStyleContext* sCurrentStyleContext;
static GtkStyleContext*
GetStyleInternal(WidgetNodeType aNodeType);
static GtkWidget*
-GetWindowWidget()
+CreateWindowWidget()
{
- static GtkWidget *widget = sWidgetStorage[MOZ_GTK_WINDOW];
- if (!widget) {
- widget = gtk_window_new(GTK_WINDOW_POPUP);
- gtk_widget_realize(widget);
- gtk_widget_set_name(widget, "MozillaGtkWidget");
- sWidgetStorage[MOZ_GTK_WINDOW] = widget;
- }
+ GtkWidget *widget = gtk_window_new(GTK_WINDOW_POPUP);
+ gtk_widget_realize(widget);
+ gtk_widget_set_name(widget, "MozillaGtkWidget");
return widget;
}
static GtkWidget*
-GetWindowContainerWidget()
+CreateWindowContainerWidget()
{
- static GtkWidget *widget = sWidgetStorage[MOZ_GTK_WINDOW_CONTAINER];
- if (!widget) {
- widget = gtk_fixed_new();
- gtk_container_add(GTK_CONTAINER(GetWindowWidget()), widget);
- sWidgetStorage[MOZ_GTK_WINDOW_CONTAINER] = widget;
- }
+ GtkWidget *widget = gtk_fixed_new();
+ gtk_container_add(GTK_CONTAINER(GetWidget(MOZ_GTK_WINDOW)), widget);
return widget;
}
static void
AddToWindowContainer(GtkWidget* widget)
{
- gtk_container_add(GTK_CONTAINER(GetWindowContainerWidget()), widget);
+ gtk_container_add(GTK_CONTAINER(GetWidget(MOZ_GTK_WINDOW_CONTAINER)), widget);
gtk_widget_realize(widget);
}
static GtkWidget*
-GetScrollbarWidget(WidgetNodeType aWidgetType, GtkOrientation aOrientation)
+CreateScrollbarWidget(WidgetNodeType aWidgetType, GtkOrientation aOrientation)
+{
+ GtkWidget* widget = gtk_scrollbar_new(aOrientation, nullptr);
+ AddToWindowContainer(widget);
+ return widget;
+}
+
+static GtkWidget*
+CreateWidget(WidgetNodeType aWidgetType)
{
- GtkWidget* widget = sWidgetStorage[aWidgetType];
- if (!widget) {
- widget = gtk_scrollbar_new(aOrientation, nullptr);
- AddToWindowContainer(widget);
- sWidgetStorage[aWidgetType] = widget;
+ switch (aWidgetType) {
+ case MOZ_GTK_WINDOW:
+ return CreateWindowWidget();
+ case MOZ_GTK_WINDOW_CONTAINER:
+ return CreateWindowContainerWidget();
+ case MOZ_GTK_SCROLLBAR_HORIZONTAL:
+ return CreateScrollbarWidget(aWidgetType,
+ GTK_ORIENTATION_HORIZONTAL);
+ case MOZ_GTK_SCROLLBAR_VERTICAL:
+ return CreateScrollbarWidget(aWidgetType,
+ GTK_ORIENTATION_VERTICAL);
+ default:
+ /* Not implemented */
+ return nullptr;
}
- return widget;
}
GtkWidget*
GetWidget(WidgetNodeType aWidgetType)
{
- switch (aWidgetType) {
- case MOZ_GTK_WINDOW:
- return GetWindowWidget();
- case MOZ_GTK_WINDOW_CONTAINER:
- return GetWindowContainerWidget();
- case MOZ_GTK_SCROLLBAR_HORIZONTAL:
- return GetScrollbarWidget(aWidgetType,
- GTK_ORIENTATION_HORIZONTAL);
- case MOZ_GTK_SCROLLBAR_VERTICAL:
- return GetScrollbarWidget(aWidgetType,
- GTK_ORIENTATION_VERTICAL);
- default:
- /* Not implemented */
- return nullptr;
+ GtkWidget* widget = sWidgetStorage[aWidgetType];
+ if (!widget) {
+ widget = CreateWidget(aWidgetType);
+ sWidgetStorage[aWidgetType] = widget;
}
+ return widget;
}
static GtkStyleContext*
CreateCSSNode(const char* aName, GtkStyleContext *aParentStyle)
{
static auto sGtkWidgetPathIterSetObjectName =
reinterpret_cast<void (*)(GtkWidgetPath *, gint, const char *)>
(dlsym(RTLD_DEFAULT, "gtk_widget_path_iter_set_object_name"));