GTK + Code Demos - 编译颜色选择器

时间:2012-12-21 05:33:21

标签: gcc gtk gtk3

我发现GTK + Code Demos中的Color Selector是一个非常好的颜色选择器。因此,我想将它作为独立的应用程序使用,而不必每次都运行gtk-demos

我尝试编译示例代码:

#include <gtk/gtk.h>

static GtkWidget *window = NULL;
static GtkWidget *da;
static GdkColor color;
static GtkWidget *frame;

/* Expose callback for the drawing area
 */
static gboolean
expose_event_callback (GtkWidget      *widget, 
                       GdkEventExpose *event, 
                       gpointer        data)
{
  GdkWindow *window = gtk_widget_get_window (widget);

  if (window)
    {
      GtkStyle *style;
      cairo_t *cr;

      style = gtk_widget_get_style (widget);

      cr = gdk_cairo_create (window);

      gdk_cairo_set_source_color (cr, &style->bg[GTK_STATE_NORMAL]);
      gdk_cairo_rectangle (cr, &event->area);
      cairo_fill (cr);

      cairo_destroy (cr);
    }

  return TRUE;
}

static void
change_color_callback (GtkWidget *button,
               gpointer   data)
{
  GtkWidget *dialog;
  GtkColorSelection *colorsel;
  gint response;

  dialog = gtk_color_selection_dialog_new ("Changing color");

  gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (window));

  colorsel = 
    GTK_COLOR_SELECTION (gtk_color_selection_dialog_get_color_selection (GTK_COLOR_SELECTION_DIALOG (dialog)));

  gtk_color_selection_set_previous_color (colorsel, &color);
  gtk_color_selection_set_current_color (colorsel, &color);
  gtk_color_selection_set_has_palette (colorsel, TRUE);

  response = gtk_dialog_run (GTK_DIALOG (dialog));

  if (response == GTK_RESPONSE_OK)
    {
      gtk_color_selection_get_current_color (colorsel,
                         &color);

      gtk_widget_modify_bg (da, GTK_STATE_NORMAL, &color);
    }

  gtk_widget_destroy (dialog);
}

GtkWidget *
do_colorsel (GtkWidget *do_widget)
{
  GtkWidget *vbox;
  GtkWidget *button;
  GtkWidget *alignment;

  if (!window)
    {
      color.red = 0;
      color.blue = 65535;
      color.green = 0;

      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_screen (GTK_WINDOW (window),
                 gtk_widget_get_screen (do_widget));
      gtk_window_set_title (GTK_WINDOW (window), "Color Selection");

      g_signal_connect (window, "destroy",
            G_CALLBACK (gtk_widget_destroyed), &window);

      gtk_container_set_border_width (GTK_CONTAINER (window), 8);

      vbox = gtk_vbox_new (FALSE, 8);
      gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
      gtk_container_add (GTK_CONTAINER (window), vbox);

      /*
       * Create the color swatch area
       */


      frame = gtk_frame_new (NULL);
      gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
      gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);

      da = gtk_drawing_area_new ();

      g_signal_connect (da, "expose_event",
            G_CALLBACK (expose_event_callback), NULL);

      /* set a minimum size */
      gtk_widget_set_size_request (da, 200, 200);
      /* set the color */
      gtk_widget_modify_bg (da, GTK_STATE_NORMAL, &color);

      gtk_container_add (GTK_CONTAINER (frame), da);

      alignment = gtk_alignment_new (1.0, 0.5, 0.0, 0.0);

      button = gtk_button_new_with_mnemonic ("_Change the above color");
      gtk_container_add (GTK_CONTAINER (alignment), button);

      gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);

      g_signal_connect (button, "clicked",
            G_CALLBACK (change_color_callback), NULL);
    }

  if (!gtk_widget_get_visible (window))
    {
      gtk_widget_show_all (window);
    }
  else
    {
      gtk_widget_destroy (window);
      window = NULL;
    }

  return window;
}

错误是:

[orschiro@thinkpad ~]$ gcc `pkg-config --cflags --libs gtk+-3.0` -o colorSelector colorSelector.ccolorSelector.c: In function ‘expose_event_callback’:
colorSelector.c:26:7: warning: ‘gdk_cairo_set_source_color’ is deprecated (declared at /usr/include/gtk-3.0/gdk/gdkcairo.h:58): Use 'gdk_cairo_set_source_rgba' instead [-Wdeprecated-declarations]
colorSelector.c: In function ‘change_color_callback’:
colorSelector.c:44:3: warning: ‘gtk_color_selection_dialog_new’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorseldialog.h:73): Use 'gtk_color_chooser_dialog_new' instead [-Wdeprecated-declarations]
colorSelector.c:49:5: warning: ‘gtk_color_selection_dialog_get_color_selection’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorseldialog.h:75): Use 'GtkColorChooser' instead [-Wdeprecated-declarations]
colorSelector.c:51:3: warning: ‘gtk_color_selection_set_previous_color’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorsel.h:148) [-Wdeprecated-declarations]
colorSelector.c:52:3: warning: ‘gtk_color_selection_set_current_color’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorsel.h:142): Use 'gtk_color_chooser_set_rgba' instead [-Wdeprecated-declarations]
colorSelector.c:53:3: warning: ‘gtk_color_selection_set_has_palette’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorsel.h:100) [-Wdeprecated-declarations]
colorSelector.c:59:7: warning: ‘gtk_color_selection_get_current_color’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorsel.h:145): Use 'gtk_color_chooser_get_rgba' instead [-Wdeprecated-declarations]
colorSelector.c: In function ‘do_colorsel’:
colorSelector.c:91:7: warning: ‘gtk_vbox_new’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkvbox.h:60): Use 'gtk_box_new' instead [-Wdeprecated-declarations]
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

已安装的软件包:

gtk2 2.24.14-1
gtk3 3.6.2-1
gcc-libs-multilib 4.7.2-2
gcc-multilib 4.7.2-2

有人可以解释一下我的错误吗?

1 个答案:

答案 0 :(得分:1)

您的源代码不完整,缺少main函数。 gtk-demo是有条理的,它有一个main调用一些子模块并以相同的方式处理它们,你错过了main

另一个警告来自于您尝试编译的示例代码是针对GTK 2完成的,并且您正在尝试针对GTK 3构建它。

您可以在GTK +存储库的gtk-demo directory找到所有缺失的部分。