A checklist for widgets

When porting your widgets to use CtkStyleContext, this checklist might be useful.

  1. Replace “style-set” handlers with “style-updated” handlers.
  2. Try to identify the role of what you're rendering with any number of classes. This will replace the detail string. There is a predefined set of CSS classes which you can reuse where appropriate. Doing so will give you theming 'for free', whereas custom classes will require extra work in the theme. Note that complex widgets are likely to need different styles when rendering different parts, and style classes are one way to achieve this. This could result in code like the following (simplified) examples:

    Example 46. Setting a permanent CSS class

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    static void
    ctk_button_init (CtkButton *button)
    {
      CtkStyleContext *context;
    
      ...
    
      context = ctk_widget_get_style_context (CTK_WIDGET (button));
    
      /* Set the "button" class */
      ctk_style_context_add_class (context, CTK_STYLE_CLASS_BUTTON);
    }

    Or

    Example 47. Using dynamic CSS classes for different elements

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    static gboolean
    ctk_spin_button_draw (CtkSpinButton *spin,
                          cairo_t       *cr)
    {
      CtkStyleContext *context;
    
      ...
    
      context = ctk_widget_get_style_context (CTK_WIDGET (spin));
    
      ctk_style_context_save (context);
      ctk_style_context_add_class (context, CTK_STYLE_CLASS_ENTRY);
    
      /* Call to entry draw impl with "entry" class */
      parent_class->draw (spin, cr);
    
      ctk_style_context_restore (context);
      ctk_style_context_save (context);
    
      /* Render up/down buttons with the "button" class */
      ctk_style_context_add_class (context, CTK_STYLE_CLASS_BUTTON);
      draw_up_button (spin, cr);
      draw_down_button (spin, cr);
    
      ctk_style_context_restore (context);
    
      ...
    }

    Note that CtkStyleContext only provides fg/bg colors, so text/base is done through distinctive theming of the different classes. For example, an entry would usually be black on white while a button would usually be black on light grey.

  3. Replace all ctk_paint_*() calls with corresponding ctk_render_*() calls.

    The most distinctive changes are the use of CtkStateFlags to represent the widget state and the lack of CtkShadowType. Note that widget state is now passed implicitly via the context, so to render in a certain state, you have to temporarily set the state on the context, as in the following example:

    Example 48. Rendering with a specific state

    1
    2
    3
    4
    ctk_style_context_save (context);
    ctk_style_context_set_state (context, CTK_STATE_FLAG_ACTIVE);
    ctk_render_check (context, cr, x, y, width, height);
    ctk_style_context_restore (context);

    For ctk_render_check() and ctk_render_option(), the shadow_type parameter is replaced by the CTK_STATE_FLAG_ACTIVE and CTK_STATE_FLAG_INCONSISTENT state flags. For things such as pressed/unpressed button states, CTK_STATE_FLAG_ACTIVE is used, and the CSS may style normal/active states differently to render outset/inset borders, respectively.

  4. The various ctk_widget_modify_*() functions to override colors or fonts for individual widgets have been replaced by similar ctk_widget_override_*() functions.
  5. It is no longer necessary to call ctk_widget_style_attach(), ctk_style_attach(), ctk_style_detach() or ctk_widget_ensure_style().
  6. Replace all uses of xthickness/ythickness. CtkStyleContext uses the CSS box model, and there are border-width/padding/margin properties to replace the different applications of X and Y thickness. Note that all of this is merely a guideline. Widgets may choose to follow it or not.