1 /*------------------------------------------------------------------------------
4 Description: Demonstrate how to embed a libgerbv render window into a new
5 application to create a custom viewer
7 Instructions: Make sure you are in the example-code directory, and compile
8 this program with the following command (assumes you are using a
9 newer version of gtk which uses cairo):
11 gcc -Wall -g `pkg-config --cflags libgerbv` `pkg-config --libs libgerbv` example6.c -o example6
13 Run with the following command:
17 Common compiling problems:
18 1. If you are compiling gerbv from source, make sure you run
19 "make install" before trying to compile this example. This
20 ensures libgerbv is correctly installed and can be found.
21 2. If you installed gerbv to "/usr/local" (the default), many
22 distributions don't correctly point pkgconfig to this target.
23 To fix this, add the following to your ~/.bashrc file:
25 export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/:/usr/lib/pkgconfig/
26 ------------------------------------------------------------------------------*/
28 /* gerbv.h pulls in all glib and gtk headers for you */
32 /* this holds our rendering info like window size, scale factor, and translation */
33 gerbv_render_info_t screenRenderInfo
;
34 /* this holds all our layers */
35 gerbv_project_t
*mainProject
;
36 /* store the drawing area widget globally to simplify the key event handling, to eliminate
37 the need for an event box */
38 GtkWidget
*drawingarea
;
41 example_render_project_to_screen (GdkDrawable
*drawable
) {
42 cairo_t
*cr
= gdk_cairo_create (drawable
);
44 /* this is by far the simplest method of rendering everything */
45 gerbv_render_all_layers_to_cairo_target (mainProject
, cr
, &screenRenderInfo
);
47 /* if you know cairo well, feel free to incorporate your own method here,
48 but this method shows you one possible idea. With it, you have more flexibilty over
51 // paint the background white before we draw anything
52 cairo_set_source_rgba (cr, 1,1,1, 1);
55 // step through all the files
56 for(i = mainProject->max_files-1; i >= 0; i--) {
57 if (mainProject->file[i]) {
58 cairo_push_group (cr);
59 gerbv_render_layer_to_cairo_target (cr, mainProject->file[i], &screenRenderInfo);
60 cairo_pop_group_to_source (cr);
61 cairo_paint_with_alpha (cr, 0.70);
67 /* this is called when the window size changes, and also during startup */
69 example_callbacks_drawingarea_configure_event (GtkWidget
*widget
, GdkEventConfigure
*event
)
71 GdkDrawable
*drawable
= widget
->window
;
73 /* figure out how large the window is, and then fit the rendered images inside
74 the specified window */
75 gdk_drawable_get_size (drawable
, &screenRenderInfo
.displayWidth
, &screenRenderInfo
.displayHeight
);
76 gerbv_render_zoom_to_fit_display (mainProject
, &screenRenderInfo
);
78 /* GTK should now automatically expose the window, so no need to do it manually */
82 /* this is called any time the window needs to redraw (another window moved in front of
83 it, the window was un-minimized, etc) */
85 example_callbacks_drawingarea_expose_event (GtkWidget
*widget
, GdkEventExpose
*event
)
87 /* render all the layers */
88 example_render_project_to_screen(widget
->window
);
92 /* do some simple translation based on the arrow keys and "Z" keys */
94 example_callbacks_drawingarea_key_press_event (GtkWidget
*widget
, GdkEventKey
*event
)
96 switch(event
->keyval
) {
98 /* cairo renders positive Y as down, so keep the sign in mind */
99 screenRenderInfo
.lowerLeftY
-= 0.1;
102 screenRenderInfo
.lowerLeftY
+= 0.1;
105 screenRenderInfo
.lowerLeftX
+= 0.1;
108 screenRenderInfo
.lowerLeftX
-= 0.1;
111 /* notice the lower left corner doesn't move with this method...
112 to do a "true" zoom in, refer to render.c and see how Gerber Viewer
114 screenRenderInfo
.scaleFactorX
+= screenRenderInfo
.scaleFactorX
/3;
115 screenRenderInfo
.scaleFactorY
+= screenRenderInfo
.scaleFactorY
/3;
118 screenRenderInfo
.scaleFactorX
-= screenRenderInfo
.scaleFactorX
/3;
119 screenRenderInfo
.scaleFactorY
-= screenRenderInfo
.scaleFactorY
/3;
124 /* render everything again by forcing an expose event */
125 GdkRectangle update_rect
;
129 update_rect
.width
= screenRenderInfo
.displayWidth
;
130 update_rect
.height
= screenRenderInfo
.displayHeight
;
132 /* force the drawing area to have an expose_event, thus redrawing the window */
133 gdk_window_invalidate_rect (drawingarea
->window
, &update_rect
, FALSE
);
138 example_create_GUI (void){
139 GtkWidget
*mainWindow
;
141 mainWindow
= gtk_window_new (GTK_WINDOW_TOPLEVEL
);
142 gtk_window_set_default_size((GtkWindow
*)mainWindow
, 400, 400);
144 gtk_window_set_title (GTK_WINDOW (mainWindow
), "Example 6");
146 /* a drawing area is the easiest way to make a custom cairo renderer */
147 drawingarea
= gtk_drawing_area_new();
148 gtk_container_add (GTK_CONTAINER (mainWindow
), drawingarea
);
150 /* hook up the signals we need to connect to */
151 gtk_signal_connect(GTK_OBJECT(drawingarea
), "expose_event",
152 GTK_SIGNAL_FUNC(example_callbacks_drawingarea_expose_event
), NULL
);
153 gtk_signal_connect(GTK_OBJECT(drawingarea
),"configure_event",
154 GTK_SIGNAL_FUNC(example_callbacks_drawingarea_configure_event
), NULL
);
155 gtk_signal_connect(GTK_OBJECT(mainWindow
), "key_press_event",
156 GTK_SIGNAL_FUNC(example_callbacks_drawingarea_key_press_event
), NULL
);
157 gtk_signal_connect_after(GTK_OBJECT(mainWindow
), "delete_event",
158 GTK_SIGNAL_FUNC(gtk_main_quit
), NULL
);
160 gtk_widget_show_all (mainWindow
);
164 main(int argc
, char *argv
[]) {
165 /* create the top level libgerbv structure */
166 mainProject
= gerbv_create_project();
168 /* make sure we change the render type to "cairo" instead of the GDK alternative */
169 screenRenderInfo
.renderType
= GERBV_RENDER_TYPE_CAIRO_HIGH_QUALITY
;
171 /* parse 2 Gerber files */
172 gerbv_open_layer_from_filename (mainProject
, "example1-input.gbx");
173 gerbv_open_layer_from_filename (mainProject
, "example2-input.gbx");
175 /* make sure we parsed the files */
176 if ((mainProject
->file
[0] == NULL
) || (mainProject
->file
[1] == NULL
))
177 g_error ("There was an error parsing the files.");
179 /* start up the gtk engine and create our GUI */
180 gtk_init (&argc
, &argv
);
181 example_create_GUI ();
183 /* start the main GUI loop...it will stay in this function call until you exit */
186 /* destroy the project, which will in turn destroy all child images */
187 gerbv_destroy_project (mainProject
);