<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Appendix F. Drawing With GDK</title> <link rel="stylesheet" href="style.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.1"> <link rel="home" href="index.html" title="Programming with gtkmm"> <link rel="up" href="index.html" title="Programming with gtkmm"> <link rel="prev" href="sec-windows-installation.html" title="Appendix E. gtkmm and Win32"> <link rel="next" href="chapter-working-with-svn.html" title="Appendix G. Working with Subversion"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr><th colspan="3" align="center">Appendix F. Drawing With GDK</th></tr> <tr> <td width="20%" align="left"> <a accesskey="p" href="sec-windows-installation.html"><img src="icons/prev.png" alt="Prev"></a> </td> <th width="60%" align="center"> </th> <td width="20%" align="right"> <a accesskey="n" href="chapter-working-with-svn.html"><img src="icons/next.png" alt="Next"></a> </td> </tr> </table> <hr> </div> <div class="appendix" title="Appendix F. Drawing With GDK"> <div class="titlepage"><div><div><h2 class="title"> <a name="chapter-gdk-drawing"></a>Appendix F. Drawing With GDK</h2></div></div></div> <p>The GDK drawing API described here is now deprecated, in favour of Cairo. See the <a class="link" href="chapter-drawingarea.html" title="Chapter 15. The Drawing Area Widget">DrawingArea</a> chapter for information about the Cairo API.</p> <p> Gdk graphics contexts (<code class="classname">Gdk::GC</code>) are a server-side resource. They contain information that describes how drawing is to be done. This provides for fewer arguments to the drawing methods, and less communication between the client and the server. The following example shows how to set up a graphics context with a foreground color of red for drawing. </p> <pre class="programlisting">Gdk::GC some_gc; some_gc.create(get_window()); Gdk::Color some_color; Gdk::Colormap some_colormap(Gdk::Colormap::get_system()); some_color.set_red(65535); some_color.set_green(0); some_color.set_blue(0); some_colormap.alloc(some_color); some_gc.set_foreground(some_color);</pre> <p> The first two lines create the graphics context and assign it to the appropriate widget. The <code class="methodname">get_window()</code> method is a part of the <code class="classname">Gtk::Widget</code> class, so if you put this code into a derived widget's implementation then you can call it just as it is, otherwise you'd use <code class="methodname">some_widget.get_window()</code>. </p> <p> The next two lines create the <code class="classname">Gdk::Color</code> and <code class="classname">Gdk::Colormap</code>. After setting the color values you then need to allocate the color. The system figures out what to do in this case. The colormap contains information about how colors can be displayed on your screen, and is able to allocate the requested color. For example, on a display of only 256 colors the exact color requested may not be available, so the closest color to the one requested will be used instead. The final line sets the color as the foreground color. </p> <p> There are a number of attributes that can be set for a graphics context. There's the foreground and background color. When drawing lines, you can set the thickness of the line with <code class="methodname">set_line_width()</code>. Whether a solid or dashed line is drawn can be set with <code class="methodname">set_line_style()</code>. The size and proportions of the dashes are set with <code class="methodname">set_dashes</code>. How two lines join together, whether round or pointed or beveled off, is set with <code class="methodname">set_join_style()</code>. Other things that can be set within a graphics context include font style, stippling and tiling for the filling of solid polygons. </p> <p> Graphics contexts are central to drawing with Gdk, because nearly all Gdk drawing functions and many Pango functions take a <code class="classname">Gdk::GC</code> object as an argument. So although Cairo has largely superceded many Gdk drawing functions, you're still likely to run into <code class="classname">Gdk::GC</code> objects quite often, so it's important to know what they are and how they're used. </p> </div> <div class="navfooter"> <hr> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"> <a accesskey="p" href="sec-windows-installation.html"><img src="icons/prev.png" alt="Prev"></a> </td> <td width="20%" align="center"> </td> <td width="40%" align="right"> <a accesskey="n" href="chapter-working-with-svn.html"><img src="icons/next.png" alt="Next"></a> </td> </tr> <tr> <td width="40%" align="left" valign="top">Appendix E. <span class="application">gtkmm</span> and Win32 </td> <td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td> <td width="40%" align="right" valign="top"> Appendix G. Working with Subversion</td> </tr> </table> </div> </body> </html>