00001
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <gtk/gtk.h>
00026 #include <glib.h>
00027 #include "preview_dialog.h"
00028
00029
00033 static void
00034 preview_close_cb
00035 (
00036 GtkWidget * widget,
00038 GtkWidget *preview_window
00040 )
00041 {
00042 gtk_widget_destroy (preview_window);
00043 }
00044
00045
00051 static gboolean
00052 preview_configure_event
00053 (
00054 GtkWidget *widget,
00056 GdkEventConfigure *event
00058 )
00059 {
00060 gint depth = -1;
00061 if (pixmap)
00062 g_object_unref (pixmap);
00063 pixmap = gdk_pixmap_new (
00064 widget->window,
00065 widget->allocation.width,
00066 widget->allocation.height,
00067 depth);
00068 gdk_draw_rectangle (
00069 pixmap,
00070 widget->style->white_gc,
00071 TRUE,
00072 0,
00073 0,
00074 widget->allocation.width,
00075 widget->allocation.height);
00076 return TRUE;
00077 }
00078
00079
00083 void
00084 preview_delete_event
00085 (
00086 GtkWidget *widget,
00088 GdkEvent *event
00090 )
00091 {
00092 gtk_widget_destroy (widget);
00093 }
00094
00095
00102 int
00103 preview_set_fg_color
00104 (
00105 GdkGC *gc,
00107 const char *color_name
00109 )
00110 {
00111 if (!gc)
00112 return (EXIT_FAILURE);
00113 if (!color_name)
00114 {
00115 fprintf (stderr, "WARNING in %s(): color_name = NULL, setting color to magenta.\n",
00116 __FUNCTION__);
00117 color_name = "magenta";
00118 return (EXIT_FAILURE);
00119 }
00120 GdkColor color;
00121 if (!gdk_color_parse (color_name, &color))
00122 {
00123 fprintf (stderr, "WARNING in %s(): could not parse color %s.\n",
00124 __FUNCTION__, color_name);
00125 return (EXIT_FAILURE);
00126 }
00127 gdk_gc_set_foreground (gc, &color);
00128 return (EXIT_SUCCESS);
00129 }
00130
00131
00138 int
00139 preview_set_fill_mode
00140 (
00141 GdkGC *gc,
00143 GdkFill fill_mode
00162 )
00163 {
00164 if (!gc)
00165 return (EXIT_FAILURE);
00166 if (!fill_mode)
00167 {
00168 fprintf (stderr, "WARNING in %s(): fill mode = NULL, setting fill mode to SOLID.\n",
00169 __FUNCTION__);
00170 fill_mode = GDK_SOLID;
00171 return (EXIT_FAILURE);
00172 }
00173 gdk_gc_set_fill
00174 (
00175 gc,
00176 fill_mode
00177 );
00178 return (EXIT_SUCCESS);
00179 }
00180
00181
00188 int
00189 preview_set_line_cap
00190 (
00191 GdkGC *gc,
00193 GdkCapStyle line_cap
00212 )
00213 {
00214 if (!gc)
00215 return (EXIT_FAILURE);
00216 if (!line_cap)
00217 {
00218 fprintf (stderr, "WARNING in %s(): line cap = NULL, setting line cap to ROUND.\n",
00219 __FUNCTION__);
00220 line_cap = GDK_CAP_ROUND;
00221 return (EXIT_FAILURE);
00222 }
00223 GdkGCValues gc_values;
00224 gdk_gc_get_values (gc, &gc_values);
00225 gdk_gc_set_line_attributes
00226 (
00227 gc,
00228 gc_values.line_width,
00229 gc_values.line_style,
00230 line_cap,
00231 gc_values.join_style
00232 );
00233 return (EXIT_SUCCESS);
00234 }
00235
00236
00243 int
00244 preview_set_line_style
00245 (
00246 GdkGC *gc,
00248 GdkLineStyle line_style
00264 )
00265 {
00266 if (!gc)
00267 return (EXIT_FAILURE);
00268 if (!line_style)
00269 {
00270 fprintf (stderr, "WARNING in %s(): line style = NULL, setting line style to SOLID.\n",
00271 __FUNCTION__);
00272 line_style = GDK_LINE_SOLID;
00273 return (EXIT_FAILURE);
00274 }
00275 GdkGCValues gc_values;
00276 gdk_gc_get_values (gc, &gc_values);
00277 gdk_gc_set_line_attributes
00278 (
00279 gc,
00280 gc_values.line_width,
00281 line_style,
00282 gc_values.cap_style,
00283 gc_values.join_style
00284 );
00285 return (EXIT_SUCCESS);
00286 }
00287
00288
00295 int
00296 preview_set_line_width
00297 (
00298 GdkGC *gc,
00300 gint line_width
00302 )
00303 {
00304 if ((!gc) || (line_width == 0))
00305 {
00306 return (EXIT_FAILURE);
00307 }
00308 GdkGCValues gc_values;
00309 gdk_gc_get_values (gc, &gc_values);
00310 if (line_width < 0)
00311 {
00312 gdk_gc_set_line_attributes
00313 (
00314 gc,
00315 abs (line_width),
00316 GDK_LINE_DOUBLE_DASH,
00317 gc_values.cap_style,
00318 gc_values.join_style
00319 );
00320 }
00321 else
00322 {
00323 gdk_gc_set_line_attributes
00324 (
00325 gc,
00326 line_width,
00327 gc_values.line_style,
00328 gc_values.cap_style,
00329 gc_values.join_style
00330 );
00331 }
00332 return (EXIT_SUCCESS);
00333 }
00334
00335
00344 int
00345 preview_use_gc
00346 (
00347 GdkDrawable *drawable,
00352 GdkGC *gc,
00354 const char * color_name,
00356 gint line_width,
00358 GdkCapStyle line_cap,
00377 GdkLineStyle line_style,
00393 GdkFill fill_mode
00412 )
00413 {
00414 if (!drawable)
00415 {
00416 fprintf (stderr, "WARNING: couldn't allocate a gc, invalid drawable was passed.\n");
00417 return (EXIT_FAILURE);
00418 }
00419 if (!gc)
00420 {
00421 GdkGC *gc = gdk_gc_new (drawable);
00422 preview_set_fg_color (gc, color_name);
00423 preview_set_line_width (gc, line_width);
00424 preview_set_line_cap (gc, line_cap);
00425 preview_set_line_style (gc, line_style);
00426 preview_set_fill_mode (gc, fill_mode);
00427
00428 return (EXIT_SUCCESS);
00429 }
00430 }
00431
00432
00440 static void
00441 preview_draw_arc
00442 (
00443 GtkWidget *widget,
00445 preview_arc arc
00447 )
00448 {
00449 if (!arc)
00450 {
00451 fprintf (stderr, "WARNING: passed arc was invalid.\n");
00452 return;
00453 }
00454
00455 arc->angle1 = 64.0 * arc->angle1;
00456 gdk_draw_arc
00457 (
00458 arc->drawable,
00459 arc->gc,
00460 arc->filled,
00461 arc->x,
00462 arc->y,
00463 arc->width,
00464 arc->height,
00465 arc->angle1,
00466 arc->angle2
00467 );
00468 }
00469
00470
00474 static void
00475 preview_draw_background
00476 (
00477 GtkWidget *widget,
00479 gdouble x,
00481 gdouble y
00483 )
00484 {
00485 GdkRectangle update_rect;
00486 update_rect.x = x - 5;
00487 update_rect.y = y - 5;
00488 update_rect.width = 10;
00489 update_rect.height = 10;
00490 gdk_draw_rectangle
00491 (
00492 pixmap,
00493 widget->style->black_gc,
00494 TRUE,
00495 update_rect.x,
00496 update_rect.y,
00497 update_rect.width,
00498 update_rect.height
00499 );
00500 gtk_widget_queue_draw_area
00501 (
00502 widget,
00503 update_rect.x,
00504 update_rect.y,
00505 update_rect.width,
00506 update_rect.height
00507 );
00508 }
00509
00510
00516 static void
00517 preview_draw_line
00518 (
00519 GtkWidget *widget,
00521 preview_line line
00523 )
00524 {
00525 if (!line)
00526 {
00527 fprintf (stderr, "WARNING: passed line was invalid.\n");
00528 return;
00529 }
00530 gdk_draw_line
00531 (
00532 line->drawable,
00533 line->gc,
00534 line->x1,
00535 line->y1,
00536 line->x2,
00537 line->y2
00538 );
00539 }
00540
00541
00545 static void
00546 preview_draw_pad
00547 (
00548 GtkWidget *widget
00550 )
00551 {
00553 }
00554
00555
00559 static void
00560 preview_draw_pin
00561 (
00562 GtkWidget *widget
00564 )
00565 {
00567 }
00568
00569
00573 static void
00574 preview_draw_polygon
00575 (
00576 GtkWidget *widget,
00578 preview_polygon polygon
00580 )
00581 {
00582 if (!polygon)
00583 {
00584 fprintf (stderr, "WARNING: passed polygon was invalid.\n");
00585 return;
00586 }
00587 gdk_draw_polygon
00588 (
00589 polygon->drawable,
00590 polygon->gc,
00591 polygon->filled,
00592 polygon->points,
00593 polygon->npoints
00594 );
00595 }
00596
00597
00611 static void
00612 preview_draw_rectangle
00613 (
00614 GtkWidget *widget,
00616 preview_rectangle rectangle
00618 )
00619 {
00620 if (!rectangle)
00621 {
00622 fprintf (stderr, "WARNING: passed rectangle was invalid.\n");
00623 return;
00624 }
00625 gdk_draw_rectangle
00626 (
00627 rectangle->drawable,
00628 rectangle->gc,
00629 rectangle->filled,
00630 rectangle->x,
00631 rectangle->y,
00632 rectangle->width,
00633 rectangle->height
00634 );
00635 }
00636
00637
00643 static gboolean
00644 preview_expose_event
00645 (
00646 GtkWidget *widget,
00648 GdkEventExpose *event
00650 )
00651 {
00652 gdk_draw_drawable (widget->window,
00653 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
00654 pixmap,
00655 event->area.x,
00656 event->area.y,
00657 event->area.x,
00658 event->area.y,
00659 event->area.width,
00660 event->area.height);
00661
00665 return FALSE;
00666 }
00667
00668
00677 int
00678 preview_create_window
00679 (
00680 gchar *footprint_name,
00682 gint width,
00684 gint height
00686 )
00687 {
00688
00689 GtkWidget *preview_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
00690
00691
00692 gtk_window_set_destroy_with_parent (GTK_WINDOW (preview_window), TRUE);
00693
00694 gchar *preview_window_title = g_strdup_printf ("pcb-fpw preview: %s",
00695 footprint_name);
00696 gtk_window_set_title (GTK_WINDOW (preview_window),
00697 preview_window_title);
00698 g_free (preview_window_title);
00699 gtk_container_set_border_width (GTK_CONTAINER (preview_window), 10);
00700
00701 g_signal_connect
00702 (
00703 GTK_OBJECT (preview_window),
00704 "delete_event",
00705 (GtkSignalFunc) preview_delete_event,
00706 NULL
00707 );
00708
00709 GtkWidget *vbox = gtk_vbox_new (FALSE, 10);
00710 gtk_container_add (GTK_CONTAINER (preview_window), vbox);
00711
00712 GtkWidget *preview_drawing_area = gtk_drawing_area_new ();
00713 gtk_widget_set_size_request (preview_drawing_area, width, height);
00714
00715 g_signal_connect
00716 (
00717 GTK_OBJECT (preview_drawing_area),
00718 "expose_event",
00719 (GtkSignalFunc) preview_expose_event,
00720 NULL
00721 );
00722 g_signal_connect
00723 (
00724 GTK_OBJECT (preview_drawing_area),
00725 "configure_event",
00726 (GtkSignalFunc) preview_configure_event,
00727 NULL
00728 );
00729 gtk_widget_set_events
00730 (
00731 preview_drawing_area,
00732 GDK_EXPOSURE_MASK | GDK_LEAVE_NOTIFY_MASK
00733 );
00734
00735 GtkWidget *preview_viewport = gtk_viewport_new (NULL, NULL);
00736 gtk_viewport_set_shadow_type (GTK_VIEWPORT (preview_viewport), GTK_SHADOW_IN);
00737
00738 gtk_box_pack_start (GTK_BOX (vbox), preview_viewport, TRUE, TRUE, 0);
00739
00740 gtk_container_add (GTK_CONTAINER (preview_viewport), preview_drawing_area);
00741
00742 GtkWidget *hbox = gtk_hbutton_box_new ();
00743 gtk_button_box_set_layout (GTK_BUTTON_BOX (hbox), GTK_BUTTONBOX_END);
00744
00745 GtkWidget *button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
00746 g_signal_connect
00747 (
00748 G_OBJECT (button),
00749 "clicked",
00750 G_CALLBACK (preview_close_cb),
00751 preview_window
00752 );
00753
00754 gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
00755
00756 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
00757
00758 gtk_window_set_default_size
00759 (
00760 GTK_WINDOW (preview_window),
00761 width + 50,
00762 height + 50
00763 );
00764
00765 gtk_widget_realize (preview_window);
00766 gtk_widget_show_all (preview_window);
00767
00768 gtk_main ();
00769 return 0;
00770 }
00771
00772