[PATCH 2/4] Condense string formatting in a couple custom widgets.

Martin Sivak msivak at redhat.com
Tue Jan 29 14:43:24 UTC 2013


Ack.

----- Original Message -----
> No reason to have this kind of stuff in two different places.
> ---
>  widgets/src/MountpointSelector.c | 50
>  ++++++++++++++++++++++++----------------
>  widgets/src/SpokeSelector.c      | 16 +++++++------
>  2 files changed, 39 insertions(+), 27 deletions(-)
> 
> diff --git a/widgets/src/MountpointSelector.c
> b/widgets/src/MountpointSelector.c
> index 2597e44..160c43f 100644
> --- a/widgets/src/MountpointSelector.c
> +++ b/widgets/src/MountpointSelector.c
> @@ -164,9 +164,32 @@ static gchar *find_pixmap(const gchar *file) {
>      return NULL;
>  }
>  
> +static void format_name_label(AnacondaMountpointSelector *widget,
> const char *value) {
> +    char *markup;
> +
> +    markup = g_markup_printf_escaped("<span fgcolor='black'
> size='large' weight='bold'>%s</span>", value);
> +    gtk_label_set_markup(GTK_LABEL(widget->priv->name_label),
> markup);
> +    g_free(markup);
> +}
> +
> +static void format_size_label(AnacondaMountpointSelector *widget,
> const char *value) {
> +    char *markup;
> +
> +    markup = g_markup_printf_escaped("<span fgcolor='black'
> size='large' weight='bold'>%s</span>", value);
> +    gtk_label_set_markup(GTK_LABEL(widget->priv->size_label),
> markup);
> +    g_free(markup);
> +}
> +
> +static void format_mountpoint_label(AnacondaMountpointSelector
> *widget, const char *value) {
> +    char *markup;
> +
> +    markup = g_markup_printf_escaped("<span fgcolor='grey'
> size='small'>%s</span>", value);
> +    gtk_label_set_markup(GTK_LABEL(widget->priv->mountpoint_label),
> markup);
> +    g_free(markup);
> +}
> +
>  static void
>  anaconda_mountpoint_selector_init(AnacondaMountpointSelector
>  *mountpoint) {
>      gchar *pixmap_path;
> -    char *markup;
>  
>      mountpoint->priv = G_TYPE_INSTANCE_GET_PRIVATE(mountpoint,
>                                                     ANACONDA_TYPE_MOUNTPOINT_SELECTOR,
> @@ -203,26 +226,20 @@ static void
> anaconda_mountpoint_selector_init(AnacondaMountpointSelector *mountp
>  
>      /* Create the name label. */
>      mountpoint->priv->name_label = gtk_label_new(NULL);
> -    markup = g_markup_printf_escaped("<span fgcolor='black'
> size='large' weight='bold'>%s</span>", _(DEFAULT_NAME));
> -    gtk_label_set_markup(GTK_LABEL(mountpoint->priv->name_label),
> markup);
> +    format_name_label(mountpoint, _(DEFAULT_NAME));
>      gtk_misc_set_alignment(GTK_MISC(mountpoint->priv->name_label),
>      0, 0);
>      gtk_widget_set_hexpand(GTK_WIDGET(mountpoint->priv->name_label),
>      TRUE);
> -    g_free(markup);
>  
>      /* Create the size label. */
>      mountpoint->priv->size_label = gtk_label_new(NULL);
> -    markup = g_markup_printf_escaped("<span fgcolor='black'
> size='large' weight='bold'>%s</span>", _(DEFAULT_SIZE));
> -    gtk_label_set_markup(GTK_LABEL(mountpoint->priv->size_label),
> markup);
> +    format_size_label(mountpoint, _(DEFAULT_SIZE));
>      gtk_misc_set_alignment(GTK_MISC(mountpoint->priv->size_label),
>      0, 0.5);
> -    g_free(markup);
>  
>      /* Create the mountpoint label. */
>      mountpoint->priv->mountpoint_label = gtk_label_new(NULL);
> -    markup = g_markup_printf_escaped("<span fgcolor='grey'
> size='small'>%s</span>", DEFAULT_MOUNTPOINT);
> -
>    gtk_label_set_markup(GTK_LABEL(mountpoint->priv->mountpoint_label),
> markup);
> +    format_mountpoint_label(mountpoint, DEFAULT_MOUNTPOINT);
>      gtk_misc_set_alignment(GTK_MISC(mountpoint->priv->mountpoint_label),
>      0, 0);
>      gtk_widget_set_hexpand(GTK_WIDGET(mountpoint->priv->mountpoint_label),
>      TRUE);
> -    g_free(markup);
>  
>      /* Add everything to the grid, add the grid to the widget. */
>      gtk_grid_attach(GTK_GRID(mountpoint->priv->grid),
>      mountpoint->priv->name_label, 0, 0, 1, 1);
> @@ -265,27 +282,20 @@ static void
> anaconda_mountpoint_selector_get_property(GObject *object, guint pro
>  
>  static void anaconda_mountpoint_selector_set_property(GObject
>  *object, guint prop_id, const GValue *value, GParamSpec *pspec) {
>      AnacondaMountpointSelector *widget =
>      ANACONDA_MOUNTPOINT_SELECTOR(object);
> -    AnacondaMountpointSelectorPrivate *priv = widget->priv;
>  
>      switch(prop_id) {
>          case PROP_NAME: {
> -            char *markup = g_markup_printf_escaped("<span
> size='large' weight='bold'>%s</span>", g_value_get_string(value));
> -            gtk_label_set_markup(GTK_LABEL(priv->name_label),
> markup);
> -            g_free(markup);
> +            format_name_label(widget, g_value_get_string(value));
>              break;
>          }
>  
>          case PROP_SIZE: {
> -            char *markup = g_markup_printf_escaped("<span
> size='large' weight='bold'>%s</span>", g_value_get_string(value));
> -            gtk_label_set_markup(GTK_LABEL(priv->size_label),
> markup);
> -            g_free(markup);
> +            format_size_label(widget, g_value_get_string(value));
>              break;
>          }
>  
>          case PROP_MOUNTPOINT: {
> -            char *markup = g_markup_printf_escaped("<span
> size='small'>%s</span>", g_value_get_string(value));
> -            gtk_label_set_markup(GTK_LABEL(priv->mountpoint_label),
> markup);
> -            g_free(markup);
> +            format_mountpoint_label(widget,
> g_value_get_string(value));
>              break;
>          }
>      }
> diff --git a/widgets/src/SpokeSelector.c
> b/widgets/src/SpokeSelector.c
> index 55412f6..e6c23cc 100644
> --- a/widgets/src/SpokeSelector.c
> +++ b/widgets/src/SpokeSelector.c
> @@ -170,9 +170,15 @@ static void
> format_status_label(AnacondaSpokeSelector *spoke, const char *markup
>      g_free(escaped);
>  }
>  
> -static void anaconda_spoke_selector_init(AnacondaSpokeSelector
> *spoke) {
> +static void format_title_label(AnacondaSpokeSelector *widget, const
> char *label) {
>      char *markup;
>  
> +    markup = g_markup_printf_escaped("<span weight='bold'
> size='large'>%s</span>", label);
> +
>    gtk_label_set_markup_with_mnemonic(GTK_LABEL(widget->priv->title_label),
> markup);
> +    g_free(markup);
> +}
> +
> +static void anaconda_spoke_selector_init(AnacondaSpokeSelector
> *spoke) {
>      spoke->priv = G_TYPE_INSTANCE_GET_PRIVATE(spoke,
>                                                ANACONDA_TYPE_SPOKE_SELECTOR,
>                                                AnacondaSpokeSelectorPrivate);
> @@ -211,13 +217,11 @@ static void
> anaconda_spoke_selector_init(AnacondaSpokeSelector *spoke) {
>  
>      /* Create the title label. */
>      spoke->priv->title_label = gtk_label_new(NULL);
> -    markup = g_markup_printf_escaped("<span weight='bold'
> size='large'>%s</span>", _(DEFAULT_TITLE));
> +    format_title_label(spoke, _(DEFAULT_TITLE));
>      gtk_label_set_justify(GTK_LABEL(spoke->priv->title_label),
>      GTK_JUSTIFY_LEFT);
> -
>    gtk_label_set_markup_with_mnemonic(GTK_LABEL(spoke->priv->title_label),
> markup);
>      gtk_label_set_mnemonic_widget(GTK_LABEL(spoke->priv->title_label),
>      GTK_WIDGET(spoke));
>      gtk_misc_set_alignment(GTK_MISC(spoke->priv->title_label), 0,
>      1);
>      gtk_widget_set_hexpand(GTK_WIDGET(spoke->priv->title_label),
>      FALSE);
> -    g_free(markup);
>  
>      /* Create the status label. */
>      spoke->priv->status_label = gtk_label_new(NULL);
> @@ -277,9 +281,7 @@ static void
> anaconda_spoke_selector_set_property(GObject *object, guint prop_id,
>          }
>  
>          case PROP_TITLE: {
> -            char *markup = g_markup_printf_escaped("<span
> weight='bold' size='large'>%s</span>", g_value_get_string(value));
> -
>            gtk_label_set_markup_with_mnemonic(GTK_LABEL(priv->title_label),
> markup);
> -            g_free(markup);
> +            format_title_label(widget, g_value_get_string(value));
>              break;
>          }
>      }
> --
> 1.7.11.2
> 
> _______________________________________________
> anaconda-patches mailing list
> anaconda-patches at lists.fedorahosted.org
> https://lists.fedorahosted.org/mailman/listinfo/anaconda-patches
> 


More information about the anaconda-patches mailing list