mirror of
git://git.geda-project.org/gerbvhtdocs
synced 2025-05-09 18:01:02 +08:00
208 lines
15 KiB
HTML
208 lines
15 KiB
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>gerbv: example6.c</title>
|
|
<link href="doxygen.css" rel="stylesheet" type="text/css">
|
|
<link href="tabs.css" rel="stylesheet" type="text/css">
|
|
</head><body>
|
|
<!-- Generated by Doxygen 1.5.6 -->
|
|
<div class="navigation" id="top">
|
|
<div class="tabs">
|
|
<ul>
|
|
<li><a href="main.html"><span>Main Page</span></a></li>
|
|
<li><a href="modules.html"><span>Modules</span></a></li>
|
|
<li><a href="classes.html"><span>Data Structures</span></a></li>
|
|
<li><a href="files.html"><span>Files</span></a></li>
|
|
<li><a href="dirs.html"><span>Directories</span></a></li>
|
|
<li><a href="examples.html"><span>Examples</span></a></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<div class="contents">
|
|
<h1>example6.c</h1><div class="fragment"><pre class="fragment"><span class="comment">/*------------------------------------------------------------------------------</span>
|
|
<span class="comment"> Filename: example6.c</span>
|
|
<span class="comment"> </span>
|
|
<span class="comment"> Description: Demonstrate how to embed a libgerbv render window into a new</span>
|
|
<span class="comment"> application to create a custom viewer</span>
|
|
<span class="comment"></span>
|
|
<span class="comment"> Instructions: Make sure you are in the example-code directory, and compile</span>
|
|
<span class="comment"> this program with the following command (assumes you are using a</span>
|
|
<span class="comment"> newer version of gtk which uses cairo):</span>
|
|
<span class="comment"></span>
|
|
<span class="comment"> gcc -Wall -g `pkg-config --cflags gtk+-2.0 glib-2.0 libgerbv` `pkg-config \</span>
|
|
<span class="comment">--libs gtk+-2.0 glib-2.0 libgerbv` example6.c -o example6</span>
|
|
<span class="comment"></span>
|
|
<span class="comment"> Run with the following command:</span>
|
|
<span class="comment"> </span>
|
|
<span class="comment"> ./example6</span>
|
|
<span class="comment"></span>
|
|
<span class="comment">------------------------------------------------------------------------------*/</span>
|
|
|
|
<span class="comment">/* gerbv.h pulls in all glib and gtk headers for you */</span>
|
|
<span class="preprocessor">#include "<a class="code" href="gerbv_8h.html" title="The main header file for the libgerbv library.">gerbv.h</a>"</span>
|
|
<span class="preprocessor">#include <cairo.h></span>
|
|
|
|
<span class="comment">/* this holds our rendering info like window size, scale factor, and translation */</span>
|
|
<a name="_a0"></a><a class="code" href="structgerbv__render__info__t.html">gerbv_render_info_t</a> screenRenderInfo;
|
|
<span class="comment">/* this holds all our layers */</span>
|
|
<a name="_a1"></a><a class="code" href="structgerbv__project__t.html">gerbv_project_t</a> *<a name="a2"></a><a class="code" href="main_8c.html#8d078d012e6d279c43966b2121226c43" title="Global state variable to keep track of what&#39;s happening on the screen.">mainProject</a>;
|
|
<span class="comment">/* store the drawing area widget globally to simplify the key event handling, to eliminate</span>
|
|
<span class="comment"> the need for an event box */</span>
|
|
GtkWidget *drawingarea;
|
|
|
|
<span class="keywordtype">void</span>
|
|
example_render_project_to_screen (GdkDrawable *drawable) {
|
|
cairo_t *cr = gdk_cairo_create (drawable);
|
|
|
|
<span class="comment">/* this is by far the simplest method of rendering everything */</span>
|
|
gerbv_render_all_layers_to_cairo_target (mainProject, cr, &screenRenderInfo);
|
|
|
|
<span class="comment">/* if you know cairo well, feel free to incorporate your own method here,</span>
|
|
<span class="comment"> but this method shows you one possible idea. With it, you have more flexibilty over</span>
|
|
<span class="comment"> the rendering</span>
|
|
<span class="comment"> int i; </span>
|
|
<span class="comment"> // paint the background white before we draw anything</span>
|
|
<span class="comment"> cairo_set_source_rgba (cr, 1,1,1, 1);</span>
|
|
<span class="comment"> cairo_paint (cr);</span>
|
|
<span class="comment"> </span>
|
|
<span class="comment"> // step through all the files</span>
|
|
<span class="comment"> for(i = mainProject->max_files-1; i >= 0; i--) {</span>
|
|
<span class="comment"> if (mainProject->file[i]) {</span>
|
|
<span class="comment"> cairo_push_group (cr);</span>
|
|
<span class="comment"> gerbv_render_layer_to_cairo_target (cr, mainProject->file[i], &screenRenderInfo);</span>
|
|
<span class="comment"> cairo_pop_group_to_source (cr);</span>
|
|
<span class="comment"> cairo_paint_with_alpha (cr, 0.70);</span>
|
|
<span class="comment"> }</span>
|
|
<span class="comment"> } */</span>
|
|
cairo_destroy (cr);
|
|
}
|
|
|
|
<span class="comment">/* this is called when the window size changes, and also during startup */</span>
|
|
gboolean
|
|
example_callbacks_drawingarea_configure_event (GtkWidget *widget, GdkEventConfigure *event)
|
|
{
|
|
GdkDrawable *drawable = widget->window;
|
|
|
|
<span class="comment">/* figure out how large the window is, and then fit the rendered images inside</span>
|
|
<span class="comment"> the specified window */</span>
|
|
gdk_drawable_get_size (drawable, &screenRenderInfo.<a name="a3"></a><a class="code" href="structgerbv__render__info__t.html#c1f647d0380e07c39a350f21a37e7a5c">displayWidth</a>, &screenRenderInfo.<a name="a4"></a><a class="code" href="structgerbv__render__info__t.html#465fcc1f9e903179b0f44ac028ad7dbc">displayHeight</a>);
|
|
<a name="a5"></a><a class="code" href="gerbv_8c.html#c4a1b93b36ae9b6cbda1da4441bf087d" title="Calculate the zoom and translations to fit the rendered scene inside the given scene...">gerbv_render_zoom_to_fit_display</a> (mainProject, &screenRenderInfo);
|
|
|
|
<span class="comment">/* GTK should now automatically expose the window, so no need to do it manually */</span>
|
|
<span class="keywordflow">return</span> TRUE;
|
|
}
|
|
|
|
<span class="comment">/* this is called any time the window needs to redraw (another window moved in front of</span>
|
|
<span class="comment"> it, the window was un-minimized, etc) */</span>
|
|
gboolean
|
|
example_callbacks_drawingarea_expose_event (GtkWidget *widget, GdkEventExpose *event)
|
|
{
|
|
<span class="comment">/* render all the layers */</span>
|
|
example_render_project_to_screen(widget->window);
|
|
<span class="keywordflow">return</span> TRUE;
|
|
}
|
|
|
|
<span class="comment">/* do some simple translation based on the arrow keys and "Z" keys */</span>
|
|
gboolean
|
|
example_callbacks_drawingarea_key_press_event (GtkWidget *widget, GdkEventKey *event)
|
|
{
|
|
<span class="keywordflow">switch</span>(event->keyval) {
|
|
<span class="keywordflow">case</span> GDK_Up:
|
|
<span class="comment">/* cairo renders positive Y as down, so keep the sign in mind */</span>
|
|
screenRenderInfo.<a name="a6"></a><a class="code" href="structgerbv__render__info__t.html#0f7fb11737241af0fdc2e6e5b8c95327">lowerLeftY</a> -= 0.1;
|
|
<span class="keywordflow">break</span>;
|
|
<span class="keywordflow">case</span> GDK_Down:
|
|
screenRenderInfo.<a class="code" href="structgerbv__render__info__t.html#0f7fb11737241af0fdc2e6e5b8c95327">lowerLeftY</a> += 0.1;
|
|
<span class="keywordflow">break</span>;
|
|
<span class="keywordflow">case</span> GDK_Left:
|
|
screenRenderInfo.<a name="a7"></a><a class="code" href="structgerbv__render__info__t.html#71983cbd44f96fba693f200e7dbd5416">lowerLeftX</a> += 0.1;
|
|
<span class="keywordflow">break</span>;
|
|
<span class="keywordflow">case</span> GDK_Right:
|
|
screenRenderInfo.<a class="code" href="structgerbv__render__info__t.html#71983cbd44f96fba693f200e7dbd5416">lowerLeftX</a> -= 0.1;
|
|
<span class="keywordflow">break</span>;
|
|
<span class="keywordflow">case</span> GDK_z:
|
|
<span class="comment">/* notice the lower left corner doesn't move with this method...</span>
|
|
<span class="comment"> to do a "true" zoom in, refer to render.c and see how Gerber Viewer</span>
|
|
<span class="comment"> does it */</span>
|
|
screenRenderInfo.<a name="a8"></a><a class="code" href="structgerbv__render__info__t.html#2323896b8bb588ad34fb404cd808bfa1">scaleFactorX</a> += screenRenderInfo.<a class="code" href="structgerbv__render__info__t.html#2323896b8bb588ad34fb404cd808bfa1">scaleFactorX</a>/3;
|
|
screenRenderInfo.<a name="a9"></a><a class="code" href="structgerbv__render__info__t.html#f55b7a0baaf4614a54d67858e48e9347">scaleFactorY</a> += screenRenderInfo.<a class="code" href="structgerbv__render__info__t.html#f55b7a0baaf4614a54d67858e48e9347">scaleFactorY</a>/3;
|
|
<span class="keywordflow">break</span>;
|
|
<span class="keywordflow">case</span> GDK_Z:
|
|
screenRenderInfo.<a class="code" href="structgerbv__render__info__t.html#2323896b8bb588ad34fb404cd808bfa1">scaleFactorX</a> -= screenRenderInfo.<a class="code" href="structgerbv__render__info__t.html#2323896b8bb588ad34fb404cd808bfa1">scaleFactorX</a>/3;
|
|
screenRenderInfo.<a class="code" href="structgerbv__render__info__t.html#f55b7a0baaf4614a54d67858e48e9347">scaleFactorY</a> -= screenRenderInfo.<a class="code" href="structgerbv__render__info__t.html#f55b7a0baaf4614a54d67858e48e9347">scaleFactorY</a>/3;
|
|
<span class="keywordflow">break</span>;
|
|
<span class="keywordflow">default</span>:
|
|
<span class="keywordflow">break</span>;
|
|
}
|
|
<span class="comment">/* render everything again by forcing an expose event */</span>
|
|
GdkRectangle update_rect;
|
|
|
|
update_rect.x = 0;
|
|
update_rect.y = 0;
|
|
update_rect.width = screenRenderInfo.<a class="code" href="structgerbv__render__info__t.html#c1f647d0380e07c39a350f21a37e7a5c">displayWidth</a>;
|
|
update_rect.height = screenRenderInfo.<a class="code" href="structgerbv__render__info__t.html#465fcc1f9e903179b0f44ac028ad7dbc">displayHeight</a>;
|
|
|
|
<span class="comment">/* force the drawing area to have an expose_event, thus redrawing the window */</span>
|
|
gdk_window_invalidate_rect (drawingarea->window, &update_rect, FALSE);
|
|
<span class="keywordflow">return</span> TRUE;
|
|
}
|
|
|
|
<span class="keywordtype">void</span>
|
|
example_create_GUI (<span class="keywordtype">void</span>){
|
|
GtkWidget *mainWindow;
|
|
|
|
mainWindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
gtk_window_set_default_size((GtkWindow *)mainWindow, 400, 400);
|
|
|
|
gtk_window_set_title (GTK_WINDOW (mainWindow), <span class="stringliteral">"Example 6"</span>);
|
|
|
|
<span class="comment">/* a drawing area is the easiest way to make a custom cairo renderer */</span>
|
|
drawingarea = gtk_drawing_area_new();
|
|
gtk_container_add (GTK_CONTAINER (mainWindow), drawingarea);
|
|
|
|
<span class="comment">/* hook up the signals we need to connect to */</span>
|
|
gtk_signal_connect(GTK_OBJECT(drawingarea), <span class="stringliteral">"expose_event"</span>,
|
|
GTK_SIGNAL_FUNC(example_callbacks_drawingarea_expose_event), NULL);
|
|
gtk_signal_connect(GTK_OBJECT(drawingarea),<span class="stringliteral">"configure_event"</span>,
|
|
GTK_SIGNAL_FUNC(example_callbacks_drawingarea_configure_event), NULL);
|
|
gtk_signal_connect(GTK_OBJECT(mainWindow), <span class="stringliteral">"key_press_event"</span>,
|
|
GTK_SIGNAL_FUNC(example_callbacks_drawingarea_key_press_event), NULL);
|
|
gtk_signal_connect_after(GTK_OBJECT(mainWindow), <span class="stringliteral">"delete_event"</span>,
|
|
GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
|
|
|
|
gtk_widget_show_all (mainWindow);
|
|
}
|
|
|
|
<span class="keywordtype">int</span>
|
|
main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[]) {
|
|
<span class="comment">/* create the top level libgerbv structure */</span>
|
|
mainProject = <a name="a10"></a><a class="code" href="gerbv_8c.html#5320e4f59b98078e635681ef0c5454c8" title="Create a new project structure and initialize some important variables.">gerbv_create_project</a>();
|
|
|
|
<span class="comment">/* make sure we change the render type to "cairo" instead of the GDK alternative */</span>
|
|
screenRenderInfo.<a name="a11"></a><a class="code" href="structgerbv__render__info__t.html#902b5d841ab7a246a3ffa4fd2a6a9d26">renderType</a> = <a name="a12"></a><a class="code" href="gerbv_8h.html#62df0d6c1541994f63747056f7e1415f3c88b1f8a6f9201b7dfe8e4f91b412df">GERBV_RENDER_TYPE_CAIRO_HIGH_QUALITY</a>;
|
|
|
|
<span class="comment">/* parse 2 Gerber files */</span>
|
|
<a name="a13"></a><a class="code" href="gerbv_8c.html#99cc1512fb3e47976604fde92ae3ce8c" title="Open a file, parse the contents, and add a new layer to an existing project.">gerbv_open_layer_from_filename</a> (mainProject, <span class="stringliteral">"example1-input.gbx"</span>);
|
|
<a class="code" href="gerbv_8c.html#99cc1512fb3e47976604fde92ae3ce8c" title="Open a file, parse the contents, and add a new layer to an existing project.">gerbv_open_layer_from_filename</a> (mainProject, <span class="stringliteral">"example2-input.gbx"</span>);
|
|
|
|
<span class="comment">/* make sure we parsed the files */</span>
|
|
<span class="keywordflow">if</span> ((mainProject-><a name="a14"></a><a class="code" href="structgerbv__project__t.html#3ecaa11fbc1cef45f458e2b88445ac35">file</a>[0] == NULL) || (mainProject-><a class="code" href="structgerbv__project__t.html#3ecaa11fbc1cef45f458e2b88445ac35">file</a>[1] == NULL))
|
|
g_error (<span class="stringliteral">"There was an error parsing the files."</span>);
|
|
|
|
<span class="comment">/* start up the gtk engine and create our GUI */</span>
|
|
gtk_init (&argc, &argv);
|
|
example_create_GUI ();
|
|
|
|
<span class="comment">/* start the main GUI loop...it will stay in this function call until you exit */</span>
|
|
gtk_main();
|
|
|
|
<span class="comment">/* destroy the project, which will in turn destroy all child images */</span>
|
|
<a name="a15"></a><a class="code" href="gerbv_8c.html#2e09480d52ed08f73975eec160946b0c" title="Free a project and all related variables.">gerbv_destroy_project</a> (mainProject);
|
|
<span class="keywordflow">return</span> 0;
|
|
}
|
|
</pre></div> </div>
|
|
<hr size="1"><address style="text-align: right;"><small>Generated on Tue Aug 19 00:14:47 2008 for gerbv by
|
|
<a href="http://www.doxygen.org/index.html">
|
|
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
|
|
</body>
|
|
</html>
|