[Zt] The GTK + Theme Architecture, Version 2

zhaozj2021-02-16  101

http://www.gtk.org/~taylor/gtk/2.0/theme-ENGINES.HTML

The GTK Theme Architecture, Version 2

Theming, the ability for the user to choose different appearances for their applications, was added to GTK several years ago. The theming ability in GTK differs somewhat from that in other toolkits in that a GTK theme defines not the appearance for entire widgets, but instead defines ways of drawing a number of different graphical elements, such as beveled boxes, frames, and check button indicators. This approach allows entirely new widgets that are composed with these same elements to draw correctly with the new theme.

A GTK theme consists of three things: a theme engine, which is a shared object including code to draw the graphical elements, a configuration file to configure the operation of the theme engine and the core parts of GTK , and extra data files for the theme engine such as images. Some variation is possible in the use of a theme engine. A theme can include its own theme engine, it can use an existing theme engine, such as the pixbuf theme which draws all elements in terms of images, or it Can Use GTK 'S Builtin Drawing Code.

One interesting thing about the way themes work in GTK is that the choice of theme engine is not global;. Rather it, and all other aspects of the theme are configured on a per-widget basis Each widget contains a pointer to a GtkStyle object that defines the theme engine and theme options for that particular widget. So, in fact, a theme can apply different theme engines to different widgets. While using two separate external theme engines is not something that makes a lot of sense, it is pretty common to Have aate That Uses A Fancy External Theme Engine for Some Widgets, And The Default Built-in GTK Theme Engine for other widgets That Require Less Intricate Treatment.Styles and RC Styles

The RC file is the basic part of a theme. Declarations in the RC file determine the style that is applied to each widget. The file, however, does not actually define styles directly. Instead, what it defines are known as RC styles, which can be thought of partial styles. It defines RC styles and includes additional declarations to bind the RC styles to classes of widgets. to form the final style for a particular widget, all the RC styles that apply to the widget are merged together.

. The full RC file syntax will not be described here Rather, we'll just give a few examples For a full description, see the GTK reference documentation A simple definition of a a RC style in a RC file looks like..:

Style "My-Button" {

XTHICKNESS = 3

YTHICKNESS = 3

Engine "pixmap" {

Image {

Function = Box

File = "Button1.png"

Border = {3, 3, 3, 3}

stretch = true

}

}

THIS DEFINES A Style Called My-Button. First We set Some Standard GTK Options - The Space Reserved for the Button's Bevel in The x and y Directions.Then, with theEngine

declaration, we say that the theme engine to use is the pixmap engine. If the engine is not already loaded, GTK will look for a shared object called libpixmap.so in the standard engine directory and load it. Then it will call the theme engines Parse Function to Parse The Text Inside The Following Braces. This Defines Options for the Thext Describes The Image File To Use To Draw Boxes.

Once We've Declared An RC Style, We Bind It To a Class of Widgets with a Declaration LIKE:

Class "gtkbutton" style "my-button"

THIS DECLARES That The My-Button RC Style Should Apply To All Widgets in The gtkbutton class and in classes deriving from gtkbutton. Some Other Examples of Binding Statements Are:

# Bind to all label widgets with button widgets

Widget_class "* .gtkbutton.gtklabel" style "my-label"

# Bind to all widgets with the name "gtk-tooltips" set

# via gtk_widget_set_name ()

Widget "* .gtk-Tooltips" style "my-tooltips"

The way that multiple RC styles bound to the same widget are prioritized is that RC styles bound with widget take precedence over RC styles bound with widget_class, which, in turn, take precedence over RC styles bound with class. For multiple declarations of the same type Later Declarations Take Precedence over Earlier Ones.

Theme Engines

Now we've seen the basics of how a theme writer defines a theme, let's look at some details in implementing a theme engine As mentioned above, a theme engine is a shared object The shared object must export three entry points:.. Void theme_init (Gtkthemengine * engine);

Void theme_exit (gtkthemengine * engine);

GtkrcStyle * theme_create_rc_style (gtkthemengine * engine)

The gtkthemeEngine argument this is passed to Each of these Functions IS An Object That GTK CREATES for Each Theme Engine Module That It Loads. It Serves As a Handle for That Particular Module.

The theme_init () and theme_exit () functions are called immediately after the theme engine is loaded and just before it is unloaded. They take care of any initialization and cleanup that the theme engine needs. The theme_create_rc_style () function is the central function of the theme engine. When GTK parses an engine declaration in an RC file, it calls the theme engine's create_rc_style () function to create the GtkRcStyle object for the RC style that is being defined. This will typically not actually be an object of the GtkRcStyle class, but instead a subclass of the GtkRcStyle class that the theme engine defines. Further interaction with the theme engine is done by calling functions in the RC styles class structure. (Virtual function table.)

The gtkrcstyle class Has The Following Virtual Functions:

Gtkrcstyle * (* clone) (gtkrcstyle * rc_style)

Create An Empty Style of The Same Type as this style. The default importation, Which does g_object_new (g_type_from_instance); Should Work In Most Cases.

GUINT (* PARSE) (GTKRCStyle * RC_Style, Gscanner * Scanner)

Fill in engine specific parts of GtkRcStyle by parsing contents of brackets. Returns G_TOKEN_NONE if successful, otherwise returns the token it expected but did not get.void (* merge) (GtkRcStyle * dest, GtkRcStyle * src)

Combine RC style data from src into dest. This will be called under two circumstances. First, it will be called when the RC style is first created in response to a engine declaration in an RC file. In this case, it is called to copy the previously parsed information in the style declaration into the newly created RC style. Second, it is called when merging all RC files together prior to creating a style for a particular widget. If this function is overridden, the implementation in the subclass should chain to The Parent IMplementation.

Gtkstyle * (* create_style) (gtkrcstyle * rc_style)

.

Most theme engines can rely on the default implementation of clone (). Theme engines that do not have any special options can rely on the default implementation of parse () and merge (). However, all theme engines will have their own implementation of create_style (). The create_style () function creates an instance of a subclass of GtkStyle specific to the theme. The virtual functions in GtkStyleClass include both a number of functions to handle various administrative operations for GtkStyle and a large number of functions that take care of drawing The Various DiffERENT Graphical Elements.

The Administrative Functions in gtkstyleClass Are:

Void (* realize) (gtkstyle * style)

Initialize for a Particular ColorMap / Depth Combination. Style-> ColorMap / Style-> Depth Will Have Been Set At this point. Will Typically Chain to Parent.void (* unrealize) (gtkstyle * style)

Clean Up for a Particular ColorMap / Depth Combination. Will Typically Chain TO PARENT.

Void (* copy) (gtkstyle * style, gtkstyle * src)

Make style an exact duplicate of src.

GtkStyle * (* clone) (gtkstyle * style);

Create An Empty Style of The Same Type as this style. The default importation, Which does g_object_new (g_type_from_instance); Should Work In Most Cases.

Void (* init_from_rc) (gtkstyle * style, gtkrcstyle * rc_style);

Initialize the gtkstyle with the value. SHOULD chain to the parent.

The number of functions here may look intimidating, but actually, the typical theme engine can rely on the default implementations of all of these functions. The reason for this is that the rc_style field of the GtkStyle structure contains a pointer to the RC style from which the GtkStyle was created. If no additional state is needed for drawing, then the default implementations of the above functions will suffice. The realize () and unrealize () function are the most likely functions to need to be overridden. These functions would be overridden IF The Theme Engine Needed to Allocate Particular GDK Resources for The Style, Such As Graphics Context In The Base GtkStyle Structure.

The bulk of the functions in GtkStyleClass structure are the drawing functions There are 21 of these -.. Too many to describe in detail here (The For a full description, see the GTK reference documentation describes, or will eventually describe, each function in Detail.) a Typical One Would Be: Void (* Draw_box) (gtkstyle * style,

GDKWINDOW * Window,

GtkStateType State_Type,

Gtkshadowtype shadow_type,

GDKRectangle * Area,

Gtkwidget * widget,

Const gchar * detail,

Gint X,

Gint Y,

Gint Width,

Gint Height);

The Various Parameters Here Specify Extra Information About What Sort of Box To Draw, They Specify How The Box Should Be Positioned, and They Indicate Which GDK Drawable To Draw Onto.

Style

The style parameter specifies the GtkStyle being used to draw this graphics element. It will be a member of the style subclass for the particular theme engine, so if the theme has stored additional parameters in the instance structure, it can cast the style to the theme's Subclass and retrieve the.

Window

This Parameter Specifier The GDK Drawable ONTO Which to Draw The Result. Despite The Name, This Drawable IS Not Necessarily A gtkwindow. Instead, it might be a gdkpixmap.

State_type

.. The state parameter indicates the state for the element It will be one of GTK_STATE_NORMAL, GTK_STATE_ACTIVE, GTK_STATE_PRELIGHT, GTK_STATE_SELECTED, or GTK_STATE_INSENSITIVE The state here will usually be derived from the state of the widget - during mouse-over a button will have the state GTK_STATE_PRELIGHT, when the button is depressed, it has the state GTK_STATE_ACTIVE, and so forth. However, sometimes a different state is used than the state of the widget to indicate `` draw this element as for a widget in the specified state. ' 'area

The area parameter gives a rectangle to which drawing should be clipped;. It may be NULL indicating that no clipping should be done As a special case, if the area parameter is NULL and the window parameter actual is a window (you can check using GDK_IS_WINDOW .

widget

This parameter indicates the widget on which drawing is being done. It may be NULL if the drawing is not associated with a particular widget, so you should not rely on it being set. If it is set, some themes may find it useful for fine -tuning the drawing behavior.

Detail

Like the widget parameter, the detail parameter is used for fine-tuning drawing. It may be NULL. If not NULL, it will be a string such as "entry_bg" or "handle" given by the caller which specifies additional information about the graphical Element Being Drawn

As an example of the usage of the detail field, the default theme engine, when drawing a flat box checks to see if the detail field is "entry_bg", and if so, draws the background using style-> base_gc instead of style-> bg_gc. (style-> base is the color used for the background of entries and text fields. It is, by default white instead of gray.) This is not the only way that this effect could be achieved. A more generic way for a THEME ENGINE TO DO THIS WOULD HAVE A Boolean Parameter Defined in It's Engine Section, Use_base_for_bg, and then, in the Theme's RC File, Have Declaration Like: Style "My-Entry" {

Engine "my-engine" {

Uses_base_for_bg = TRUE

}

}

Class "gtkentry" style "my-entry"

Hard-coding the behavior using the detail field is just a simpler way to achieve the same effect. About the only time the detail field would need to be used, is if single widget drew two elements of the same type which the theme engine needed to DISTINGUISH. This Situation Does Not Currently Occur IN GTK .

X, Y, Width, Height

................ ..

Finally gtkstyleClass Contains a couple of other functions for miscellaneous Operations relative to the appearance of widgets.

Void (* set_background) (gtkstyle * style,

GDKWINDOW * Window,

Gtkstatetype state_type);

Initialize Background Pixmap or Color for the Given WinDow. GDKPIXBUF * (* render_icon) (gtkstyle * style,

Const gtkiconsource * Source,

GtktextDirection Direction,

GtkStateType State,

Gtkiconsizetype size,

Gtkwidget * widget,

Const gchar * detail;

Create an icon image suitable for the given size and state.most Themes CAN SIMPLY USE The Default IMPLEMENTATION OF THESE TWO Functions.

An Example Theme Engine

As an example of a theme engine, we'll show some portions of the Pixbuf theme engine. We'll omit most of the code for actually parsing options from the engine sections of RC files and the actual drawing code, and concentrate on the administrative Parts of the.

The main file of the engine is quite short; it defines four entry points: the theme_init (), theme_exit (), and theme_create_rc () entry points described above, and another entry point g_module_check_init (), that, instead of being specific to theme ENGINES, IS Generic To All Modules Loaded Via The Gmodule Library.

In the theme_init () function, we call functions that register the two types we define in the engine -. The GtkRcStyle and GtkStyle subclasses As we'll see below, while these are simply normal GObject classes, we define them in a somewhat different way From Object Classes Defined in A Static Library

G_module_export void

Theme_init (gtkthemengine * engine)

{

PIXBUF_RC_STYLE_REGISTER_TYPE (ENGINE);

PIXBUF_Style_Register_Type (Engine);

}

..................................

G_module_export void

Theme_exit (Void)

{

}

In the the the_create_rc_style () function, we create an instance of the pixbufrcstyle type registered in the theme_init () function.

G_module_export gtkrcstyle *

Theme_create_rc_style (void)

{

Return gtk_rc_style (g_object_new (pixbuf_type_rc_style);

}

The g_module_check_init () performs a checks to make sure that the version of GTK that we are linking against is compatible with the version for which we were compiledG_MODULE_EXPORT const gchar * g_module_check_init (GModule * module);

Const gchar *

g_module_check_init (gmodule * module)

{

RETURN GTK_CHECK_VERSION (GTK_MAJOR_VERSION,

GTK_MINOR_VERSION,

GTK_MICRO_VERSION - GTK_INTERFACE_AGE);

}

The constants GTK_MAJOR_VERSION, and so forth, come from the header files at compilation time; the function gtk_check_version compares them with versions stored when GTK was compiled If they are compatible, the function returns NULL, otherwise it returns an error message Since this convention.. IS Compatible with what gmodule experts for g_module_check_init (), We Simply Return The value That gtk_check_version () returns directly.

We now turn to the definition of PixbufRcStyle. The header file for PixbufRcStyle looks rather similar to that for a normal statically defined GObject type. First we define structures for the instance and class structures.

Typedef struct _pixbufrcstyle pixbufrcstyle

Typedef struct _pixbufrcstyleClass PixbufrcStyleclass;

Struct _pixbufrcstyle

{

Gtkrcstyle parent_instance;

GLIST * IMG_LIST;

}

Struct _pixbufrcstyleClass

{

GTKRCStyleclass Parent_class;

}

As with a normal type, these structures include the parent class structures as their first element so that inheritance works properly. For the instance structure, we add an extra variable to store the image definitions that are declared in engine section of the RC file. Then WE Define Macros for Casting and Checking Types:

EXTERN GTYPE PIXBUF_TYPE_RC_STYLE;

#define pixbuf_type_rc_style pixbuf_type_rc_style

#define PIXBUF_RC_STYLE (object) (G_TYPE_CHECK_INSTANCE_CAST ((object), PIXBUF_TYPE_RC_STYLE, PixbufRcStyle)) # define PIXBUF_RC_STYLE_CLASS (klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PIXBUF_TYPE_RC_STYLE, PixbufRcStyleClass))

#define pixbuf_is_rc_style (Object) (g_type_check_instance_type ((object), pixbuf_type_rc_style))

#define pixbuf_is_rc_style_class (klass) (g_type_check_class_type (klass), PIXBUF_TYPE_RC_STYLE))

#define pixbuf_rc_style_get_class (obj) (g_type_instance_get_class ((obj), pixbuf_type_rc_style, pixbufrstyleClass))

The main difference to notice here from the normal GObject macros is that instead of calling a function to retrieve the object type, we simply use a global variable. Since we've registered the type from our theme_init () function by calling pixbuf_rc_style_register_type (), We don't need..................

The reason we do the registration with an explicitly called function is that we need to associate the class for PixbufRcStyle with the theme engine module. If we did not take care to make the association, then the engine module might be unloaded while there were still Extent Instances of the Class. Since The Module Includes The Definitions of The Class's Virtual Functions, this Would Have Very Bad Effects. To make the association, we call:

GTYPE GTK_THEME_ENGINE_GET_TYPE (gtkthemengine * Engine,

GTYPE PARENT_TYPE,

Const gchar * type_name,

Const gtypeinfo * type_info);

This function checks to see if a type with the name type_name is already been defined by some module. If this module has registered the type, it returns the type ID, if another module currently loaded has registered the type, then it dies with an error (so, make sure that the type of the classes you define have a unique name). If no module has yet registered the type, it registers the type using parent_type and type_info, and returns the resulting ID. The type is registered using functions in GObject for dealing with dynamically loaded types;. as long instances of the type are extent, GTK will keep a reference to the engine and keep it from being unloaded Once all instances have been finalized, GTK will drop the reference, and the engine can be Unloaded.The Function to Register The Type, Looks Like:

GTYPE PIXBUF_TYPE_RC_STYLE = 0;

Void

PIXBUF_RC_STYLE_REGISTER_TYPE (GTKTHEEENGINE * ENGINE)

{

Static const gtypeinfo object_info =

{

Sizeof (PIXBUFRCSTYLASS),

(Gbaseinitfunc) NULL,

(Gbasefinalizefunc) NULL,

(Gclassinitfunc) pixbuf_rc_style_class_init,

NULL, / * Class_Finalize * /

NULL, / * Class_Data * /

SizeOf (PIXBUFRCSTYLE),

0, / * n_pReallocs * /

(Ginstanceinitfunc) pixbuf_rc_style_init,

}

PIXBUF_TYPE_RC_STYLE = GTK_THEME_ENGINE_GET_TYPE (ENGINE,

GTK_TYPE_RC_STYLE,

"PIXBUFRCSTYLE",

& object_info;

}

The GTYPEINFO STRUCTURE IS The Same as for a Static Type, So The Main Difference Is Ty, So The Main Difference Is That We Are Calling GTK_THEME_ENGINE_GET_TYPE () Rather Than g_type_register_static ().

The class_init and init function for pixbufrcstyle is the Rather Uninteresting.

Static void

Pixbuf_rc_style_init (PIXBUFRCSTYLE * STYLE)

{

}

Static void

PIXBUF_RC_STYLE_CLASS_INIT (PIXBUFRCSTYLASS * KLASS) {

GTKRCStyleClass * rc_style_class = gtk_rc_style_class (klass);

GOBJECTCLASS * OBJECT_CLASS = g_object_class (klass);

PARENT_CLASS = g_type_class_peek_parent (klass);

RC_Style_Class-> Parse = PIXBUF_RC_STYLE_PARSE;

RC_Style_Class-> Merge = PIXBUF_RC_STYLE_MERGE;

RC_Style_Class-> Create_Style = PIXBUF_RC_Style_Create_Style;

Object_class-> finalize = pixbuf_rc_style_finalize;

}

The init () function, in fact, does nothing at all (The default initialization of img_list to NULL suffices. The class_init function saves a pointer to the parent class and then initializes the class structure with pointers to the virtual functions that we override in our IMPLEMENTATION.

Since this document is about theme engines, not about GScanner, we will not explain the parsing code from the theme engine in detail. However, certain aspects that we can see in the top-level parse () function are rather unusual, so we 'll Go through them briefly.

................... ..

Static struct

{

Gchar * name;

Guint token;

}

Theme_symbols [] =

{

{"Image", token_image},

{"Function", token_function},

{"file", token_file},

{"stretch", token_stretch},

[...]

}

Static Guint

PIXBUF_RC_STYLE_PARSE (gtkrcstyle * rc_style,

Gscanner * scanner)

{

Static gquark scope_id = 0;

PIXBUFRCSTYLE * PIXBUF_STYLE = PIXBUF_RC_Style (rc_style);

Guint Old_scope;

Guint token;

GINT I;

ThemeImage * img; / * set up a new scient in this scanner. * /

IF (! scope_id)

Scope_id = g_quark_from_string ("PIXBUF_THEME_ENGINE");

/ * If we bail out due to errors, we * don't * reset the scope, so the

* Error Messaging Code Can make Sense of Our Tokens.

* /

Old_scope = g_scanner_set_scope; scanner, scope_id);

/ * Now Check if we already added our symbols to this scope

* (in some previous call to theme_parse_rc_style for THE

* Same Scanner.

* /

IF (! g_scanner_lookup_symbol (scanner, theme_symbols [0] .name))

{

g_scanner_freeze_symbol_table (scanner);

For (i = 0; I

g_scanner_scope_add_symbol (Scanner, Scope_ID,

Theme_symbols [i] .name,

Gint_to_pointer (theme_symbols [i] .token);

g_scanner_thaw_symbol_table (scanner);

}

Scopes are identified by a particular quark (integer identifier for a string). If the scope_id static variable has not yet been initialized, we call g_quark_from_static () string to get the quark corresponding to the string we use, pixbuf_theme_engine. Since our module could be unloaded later, we are careful to use g_quark_from_string () instead of g_quark_from_static_string (). Then we check to see if we've previously loaded our symbols into this scope. If not, we load them. (The GScanner object is used for a single RC style, SO We'll Load the symbols for the first rc style Using Our Engine in the file

ONCE We've Loaded The Approp, We Go Into The Main Parsing Loop. Note That The Opening '{' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '.

/ * We're Ready to Go, Now Parse The Top Level * / token = g_scanner_peek_next_token (scanner);

While (token! = g_token_right_curly)

{

Switch (token)

{

Case token_image:

IMG = NULL;

Token = theme_parse_image (Scanner, Pixbuf_Style, & IMG);

Break;

DEFAULT:

g_scanner_get_next_token (scanner);

Token = g_token_right_curly;

Break;

}

IF (token! = g_token_none)

Return token;

Else

PIXBUF_Style-> IMG_List = g_list_append (pixbuf_style-> IMG_LIST, IMG);

Token = g_scanner_peek_next_token (scanner);

}

g_scanner_get_next_token (scanner);

The top-level syntax that that the Pixbuf engine expects is quite simple, we can either have an image {...} declaration, which is parsed by calling theme_parse_image (), or we can have the closing '}' for the block. Anything else is an error.

g_scanner_set_scope (Scanner, Old_scope);

Return g_token_none;

}

Finally, in case of success, we set the scope back to the original scope and return G_TOKEN_NONE. Returning a different token value indicates we were expecting some token we did not get, and GTK will output an appropriate diagnostic warning for the parsing error. We Only Reset The Scope In Case of Success And NOT OF Fail The Diagnostic Warning Can Interpret The Token That We Return with respect to the scope where the error occurred.

While parsing, we add all the image declarations we find to pixbuf_style-> img_list. When we merge pixbuf styles together, we need to combine the lists. We do that in the merge () function. In a somewhat idealized form, this looks like :

Static void

PIXBUF_RC_STYLE_MERGE (gtkrcstyle * Dest,

GTKRCStyle * SRC)

{

IF (PIXBUF_IS_RC_STYLE (SRC))

{

PIXBUFRCSTYLE * PIXBUF_DEST = PIXBUF_RC_STYLE (DEST); PIXBUFRCSTYLE * PIXBUF_SRC = PIXBUF_RC_STYLE (SRC);

GList * new_list = g_list_copy (PIXBUF_SRC-> IMG_LIST);

g_list_foreach (new_list, (gfunc) theme_imag_ref, null;

PIXBUF_DEST-> IMG_List = g_list_concat (pixbuf_dest-> img_list, new_list);

}

PARENT_CLASS-> MERGE (DEST, SRC);

}

Since we can either be merging another pixbuf RC style, or a non-pixbuf RC style into the destination RC style (which will always be a pixbuf RC style), we first check to make sure that src is in fact pixbuf RC style using PIXBUF_IS_RC_STYLE (). We then concatenate the images from the source style onto the list in the destination style. Since, when looking up an image for drawing, we examine the images in the list in sequence, concatenating the images in the source at the end of the destination list properly ensures that the information already stored in the destination style takes precedence over the information in the source style. Whether or not the source style was a pixbuf style, we then chain to the parent implementation to merge the core parts of GtkRcStyle.

The final Virtual function We override is create_style (). In this function we simply create a new object of the style subclass we registered in theme_init and return it.

STATIC gtkstyle *

PIXBUF_RC_STYLE_CREATE_STYLE (gtkrcstyle * rc_style)

{

Return gtk_style (g_object_new (pixbuf_type_style);

}

The header file and pixbuf_style_register_type () Functions for Pixbufstyle Follow Identical Lines To Those of PixbufrcStyle So We'll omit theist.

In Our Class Init Function, We Override All The Drawing Functions With Our Own Implementations, While Keeping The Default IMPLEMENTIC VOID

Pixbuf_style_class_init (PIXBUFSTYLASS * KLASS)

{

Gtkstyleclass * style_class = gtk_style_class (klass);

STYLE_CLASS-> DRAW_HLINE = DRAW_HLINE;

STYLE_CLASS-> DRAW_VLINE = DRAW_VLINE;

STYLE_CLASS-> DRAW_SHADOW = DRAW_SHADOW;

STYLE_CLASS-> DRAW_POLYGON = DRAW_POLYGON;

STYLE_CLASS-> DRAW_ARROW = DRAW_ARROW;

STYLE_CLASS-> DRAW_DIAMOND = DRAW_DIAMOND;

STYLE_CLASS-> DRAW_OVAL = DRAW_OVAL;

STYLE_CLASS-> DRAW_STRING = DRAW_STRING;

STYLE_CLASS-> DRAW_BOX = DRAW_BOX;

STYLE_CLASS-> DRAW_FLAT_BOX = DRAW_FLAT_BOX;

STYLE_CLASS-> DRAW_CHECK = DRAW_CHECK;

STYLE_CLASS-> DRAW_OPTION = DRAW_OPTION;

STYLE_CLASS-> DRAW_CROSS = DRAW_CROSS;

STYLE_CLASS-> DRAW_RAMP = DRAW_RAMP;

STYLE_CLASS-> DRAW_TAB = DRAW_TAB;

STYLE_CLASS-> DRAW_SHADOW_GAP = DRAW_SHADOW_GAP;

STYLE_CLASS-> DRAW_BOX_GAP = DRAW_BOX_GAP;

STYLE_CLASS-> DRAW_EXTENSION = DRAW_EXTENSION;

STYLE_CLASS-> DRAW_FOCUS = DRAW_FOCUS;

STYLE_CLASS-> DRAW_SLIDER = DRAW_SLIDER;

Style_class-> DRAW_HANDLE = Draw_Handle;

}

To Give an Example of What Open of these Drawing functions Looks Like, WE CAN Take a Look At Draw_box ().

Static void

Draw_box (gtkstyle * style,

GDKWINDOW * Window,

GtkStateType State,

GtkshadowType Shadow,

GDKRectangle * Area,

Gtkwidget * widget,

Const gchar * detail,

Gint X,

Gint Y,

Gint Width,

Gint Height)

{

ThemematchData match_data;

g_return_if_fail (style! = null);

g_return_if_fail (window! = null); match_data.function = token_d_box;

Match_data.detail = (char *) Detail;

Match_data.flags = theme_match_shadow | theme_match_state;

match_data.shadow = shadow;

Match_data.state = state;

Draw_simple_image (Style, Window, Area, Widget, & Match_Data, True, True,

X, Y, width, height);

}

Since in the pixbuf theme all graphical elements are drawn with pixmaps, the implementations of the various draw_ * functions are rather monotonous. We initialize a structure with information from our arguments and then call a generic function that matches the argument information against the image list for THELE, AND DOES The Actual Drawing. Draw_simple_image Will Eventual Extract The Relevant Image List with code Like:

Image_list = pixbuf_rc_style (style-> rc_style) -> IMG_LIST;

As Noted Above, We don't bother copying the state from the rc style inte the point. INSTEAD, We Simply Use The Poin MAINTAINS from The Style to the Composite RC Style as needed.

We've now gone over the essentials of creating a theme engine. When writing a theme engine, it is often easiest to take an existing theme engine that is similar to what you want to create and modify it to match your needs. You need to be careful when doing this that the theme engine you choose has been updated for GTK -2.0. While the essentials of theme engines in GTK -1.2 were quite similar, the details changed substantially with the introduction of GObject.

Modifying Geometry and Layout

What if you are satisfied with simply changing the way that drawing is done? What if you also want to change the size of various elements, or even move elements around. What if you want to make scrollbars wider, or move both arrows of a scrollbar to the same side of the trough? Well, it is not possible within the official theme engine API. Nonetheless, various themes that are available actually do this. We'll describe what they are doing to achieve these effects, why what they are doing is unreliable, and what would need to be added to GTK to do this the right way.The way that theme engines achieve these effects is by modifying the class structures of the widget classes. In some cases, geometry parameters (such as the scrollbar width) are stored in these structures, so the theme engine can simply replace those parameters with other more congenial parameters. in other cases, the options are not stored in class variables. in these cases, the themes are actually replacing the virtual funct ION POINTERS TO Such Functions as gtk_vscrollbar_size_allocate () with pointers to there out their w version.

There are various problems with this technique. First, as noted above, a theme can specify different theme engines for different widgets. However, changes to a widget class effect all widgets, not just some widgets. Second, there is no guarantee that when switching themes, the first theme will be unloaded before the second theme is loaded. This means that the there is no way a theme can reliably undo its changes to the class structures. In fact, the worst case scenario is that the function pointer that a theme engine saves when it is loaded points to some internal function of the previous theme engine which was then unloaded. If the theme engine tries to restore the function pointer to the previous value, it will then cause the application to segfault. The final problem with modifying the class structure is that the exact details of the way a function like gtk_vscrollbar_size_allocate () is an internal detail of GTK . If you copy the code into your theme engine, your theme will most lik ely break with future versions of GTK .So, do not do that. If you do, despite the above warnings, decide to do this in one of your themes, what you should do after modifying the class structures in your theme_init function is to call gtk_theme_engine_ref (engine) to add an extra reference count so that your theme engine will never be unloaded. This makes sure that at least the application will not crash if one of your function pointers is not properly reset, or if a subsequent theme accidentally RESTORES YOUR POINTER INSTEMES, But That Is Better Than Crashing.)

So, how should it work The plan for future versions of GTK is that widget properties will be modifiable from RC files, so you will be able to do something like [hypothetical syntax]:? Properties "my-scrollbar-props" {

SLIDER_SIZE = 20

TOP_ARROWS = Both

}

Class GtkvscrollBar Properties "My-scrollbar-props"

There are two parts to implementing this. First, the basic mechanism needs to be implemented. Second, all the attributes of widgets that are interesting for themes to configure need to be made configurable through the property system.

转载请注明原文地址:https://www.9cbs.com/read-10496.html

New Post(0)