⚝
One Hat Cyber Team
⚝
Your IP:
216.73.216.83
Server IP:
157.245.101.34
Server:
Linux skvinfotech-website 5.4.0-131-generic #147-Ubuntu SMP Fri Oct 14 17:07:22 UTC 2022 x86_64
Server Software:
Apache/2.4.41 (Ubuntu)
PHP Version:
7.4.33
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
usr
/
share
/
doc
/
libgtk-3-dev
/
gobject
/
View File Name :
howto-gobject.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>How to define and implement a new GObject: GObject Reference Manual</title> <meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> <link rel="home" href="index.html" title="GObject Reference Manual"> <link rel="up" href="pt02.html" title="Part IV. Tutorial"> <link rel="prev" href="pt02.html" title="Part IV. Tutorial"> <link rel="next" href="howto-gobject-code.html" title="Boilerplate code"> <meta name="generator" content="GTK-Doc V1.32 (XML mode)"> <link rel="stylesheet" href="style.css" type="text/css"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle"> <td width="100%" align="left" class="shortcuts"></td> <td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td> <td><a accesskey="u" href="pt02.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td> <td><a accesskey="p" href="pt02.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td> <td><a accesskey="n" href="howto-gobject-code.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td> </tr></table> <div class="chapter"> <div class="titlepage"><div><div><h2 class="title"> <a name="howto-gobject"></a>How to define and implement a new GObject</h2></div></div></div> <div class="toc"><dl class="toc"> <dt><span class="sect1"><a href="howto-gobject.html#howto-gobject-header">Boilerplate header code</a></span></dt> <dt><span class="sect1"><a href="howto-gobject-code.html">Boilerplate code</a></span></dt> <dt><span class="sect1"><a href="howto-gobject-construction.html">Object construction</a></span></dt> <dt><span class="sect1"><a href="howto-gobject-destruction.html">Object destruction</a></span></dt> <dt><span class="sect1"><a href="howto-gobject-methods.html">Object methods</a></span></dt> <dd><dl> <dt><span class="sect2"><a href="howto-gobject-methods.html#non-virtual-public-methods">Non-virtual public methods</a></span></dt> <dt><span class="sect2"><a href="howto-gobject-methods.html#virtual-public-methods">Virtual public methods</a></span></dt> <dt><span class="sect2"><a href="howto-gobject-methods.html#virtual-private-methods">Virtual private Methods</a></span></dt> </dl></dd> <dt><span class="sect1"><a href="howto-gobject-chainup.html">Chaining up</a></span></dt> </dl></div> <p> This chapter focuses on the implementation of a subtype of GObject, for example to create a custom class hierarchy, or to subclass a GTK+ widget. </p> <p> Throughout the chapter, a running example of a file viewer program is used, which has a <span class="type">ViewerFile</span> class to represent a single file being viewed, and various derived classes for different types of files with special functionality, such as audio files. The example application also supports editing files (for example, to tweak a photo being viewed), using a <span class="type">ViewerEditable</span> interface. </p> <div class="sect1"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="howto-gobject-header"></a>Boilerplate header code</h2></div></div></div> <p> The first step before writing the code for your GObject is to write the type's header which contains the needed type, function and macro definitions. Each of these elements is nothing but a convention which is followed by almost all users of GObject, and has been refined over multiple years of experience developing GObject-based code. If you are writing a library, it is particularly important for you to adhere closely to these conventions; users of your library will assume that you have. Even if not writing a library, it will help other people who want to work on your project. </p> <p> Pick a name convention for your headers and source code and stick to it: </p> <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> <li class="listitem"><p>use a dash to separate the prefix from the typename: <code class="filename">viewer-file.h</code> and <code class="filename">viewer-file.c</code> (this is the convention used by Nautilus and most GNOME libraries).</p></li> <li class="listitem"><p>use an underscore to separate the prefix from the typename: <code class="filename">viewer_file.h</code> and <code class="filename">viewer_file.c</code>.</p></li> <li class="listitem"><p>Do not separate the prefix from the typename: <code class="filename">viewerfile.h</code> and <code class="filename">viewerfile.c</code>. (this is the convention used by GTK+)</p></li> </ul></div> <p> Some people like the first two solutions better: it makes reading file names easier for those with poor eyesight. </p> <p> The basic conventions for any header which exposes a GType are described in <a class="xref" href="gtype-conventions.html" title="Conventions">the section called “Conventions”</a>. </p> <p> If you want to declare a type named ‘file’ in namespace ‘viewer’, name the type instance <code class="function">ViewerFile</code> and its class <code class="function">ViewerFileClass</code> (names are case sensitive). The recommended method of declaring a type differs based on whether the type is final or derivable. </p> <p> Final types cannot be subclassed further, and should be the default choice for new types — changing a final type to be derivable is always a change that will be compatible with existing uses of the code, but the converse will often cause problems. Final types are declared using <a class="link" href="gobject-Type-Information.html#G-DECLARE-FINAL-TYPE:CAPS" title="G_DECLARE_FINAL_TYPE()"><code class="function">G_DECLARE_FINAL_TYPE</code></a>, and require a structure to hold the instance data to be declared in the source code (not the header file). </p> <div class="informalexample"> <table class="listing_frame" border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td class="listing_lines" align="right"><pre>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 29</pre></td> <td class="listing_code"><pre class="programlisting"><span class="cm">/*</span> <span class="cm"> * Copyright/Licensing information.</span> <span class="cm"> */</span> <span class="cm">/* inclusion guard */</span> <span class="cp">#ifndef __VIEWER_FILE_H__</span> <span class="cp">#define __VIEWER_FILE_H__</span> <span class="cp">#include</span> <span class="cpf"><glib-object.h></span><span class="cp"></span> <span class="cm">/*</span> <span class="cm"> * Potentially, include other headers on which this header depends.</span> <span class="cm"> */</span> <span class="n">G_BEGIN_DECLS</span> <span class="cm">/*</span> <span class="cm"> * Type declaration.</span> <span class="cm"> */</span> <span class="cp">#define VIEWER_TYPE_FILE viewer_file_get_type ()</span> <span class="n">G_DECLARE_FINAL_TYPE</span> <span class="p">(</span><span class="n">ViewerFile</span><span class="p">,</span> <span class="n">viewer_file</span><span class="p">,</span> <span class="n">VIEWER</span><span class="p">,</span> <span class="kt">FILE</span><span class="p">,</span> <span class="n">GObject</span><span class="p">)</span> <span class="cm">/*</span> <span class="cm"> * Method definitions.</span> <span class="cm"> */</span> <span class="n">ViewerFile</span> <span class="o">*</span><span class="n">viewer_file_new</span> <span class="p">(</span><span class="kt">void</span><span class="p">);</span> <span class="n">G_END_DECLS</span> <span class="cp">#endif </span><span class="cm">/* __VIEWER_FILE_H__ */</span><span class="cp"></span></pre></td> </tr> </tbody> </table> </div> <p> </p> <p> Derivable types <span class="emphasis"><em>can</em></span> be subclassed further, and their class and instance structures form part of the public API which must not be changed if API stability is cared about. They are declared using <a class="link" href="gobject-Type-Information.html#G-DECLARE-DERIVABLE-TYPE:CAPS" title="G_DECLARE_DERIVABLE_TYPE()"><code class="function">G_DECLARE_DERIVABLE_TYPE</code></a>: </p> <div class="informalexample"> <table class="listing_frame" border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td class="listing_lines" align="right"><pre>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 29 30 31 32 33 34 35 36 37 38 39 40 41 42</pre></td> <td class="listing_code"><pre class="programlisting"><span class="cm">/*</span> <span class="cm"> * Copyright/Licensing information.</span> <span class="cm"> */</span> <span class="cm">/* inclusion guard */</span> <span class="cp">#ifndef __VIEWER_FILE_H__</span> <span class="cp">#define __VIEWER_FILE_H__</span> <span class="cp">#include</span> <span class="cpf"><glib-object.h></span><span class="cp"></span> <span class="cm">/*</span> <span class="cm"> * Potentially, include other headers on which this header depends.</span> <span class="cm"> */</span> <span class="n">G_BEGIN_DECLS</span> <span class="cm">/*</span> <span class="cm"> * Type declaration.</span> <span class="cm"> */</span> <span class="cp">#define VIEWER_TYPE_FILE viewer_file_get_type ()</span> <span class="n">G_DECLARE_DERIVABLE_TYPE</span> <span class="p">(</span><span class="n">ViewerFile</span><span class="p">,</span> <span class="n">viewer_file</span><span class="p">,</span> <span class="n">VIEWER</span><span class="p">,</span> <span class="kt">FILE</span><span class="p">,</span> <span class="n">GObject</span><span class="p">)</span> <span class="k">struct</span> <span class="n">_ViewerFileClass</span> <span class="p">{</span> <span class="n">GObjectClass</span> <span class="n">parent_class</span><span class="p">;</span> <span class="cm">/* Class virtual function fields. */</span> <span class="kt">void</span> <span class="p">(</span><span class="o">*</span> <span class="n">open</span><span class="p">)</span> <span class="p">(</span><span class="n">ViewerFile</span> <span class="o">*</span><span class="n">file</span><span class="p">,</span> <span class="n">GError</span> <span class="o">**</span><span class="n">error</span><span class="p">);</span> <span class="cm">/* Padding to allow adding up to 12 new virtual functions without</span> <span class="cm"> * breaking ABI. */</span> <span class="n">gpointer</span> <span class="n">padding</span><span class="p">[</span><span class="mi">12</span><span class="p">];</span> <span class="p">};</span> <span class="cm">/*</span> <span class="cm"> * Method definitions.</span> <span class="cm"> */</span> <span class="n">ViewerFile</span> <span class="o">*</span><span class="nf">viewer_file_new</span> <span class="p">(</span><span class="kt">void</span><span class="p">);</span> <span class="n">G_END_DECLS</span> <span class="cp">#endif </span><span class="cm">/* __VIEWER_FILE_H__ */</span><span class="cp"></span></pre></td> </tr> </tbody> </table> </div> <p> </p> <p> The convention for header includes is to add the minimum number of <code class="function">#include</code> directives to the top of your headers needed to compile that header. This allows client code to simply <code class="function">#include "viewer-file.h"</code>, without needing to know the prerequisites for <code class="filename">viewer-file.h</code>. </p> </div> </div> <div class="footer"> <hr>Generated by GTK-Doc V1.32</div> </body> </html>