´ÙÀ½ ÀÌÀü Â÷·Ê

7. ¹öÆ° widget

7.1 º¸ÅëÀÇ ¹öÆ°

¿ì¸®´Â ¹öÆ°widget¿¡ ´ëÇؼ­´Â °ÅÀÇ º¸¾Æ¿Ô´Ù. ±×°ÍÀº »ó´çÈ÷ °£´ÜÇÏ´Ù. ±×·±µ¥ ¹öÆ°À» ¸¸µå´Âµ¥´Â µÎ°¡Áö ¹æ¹ýÀÌ ÀÖ´Ù. ¿ì¸®´Â ¶óº§ÀÌ ÀÖ´Â ¹öÆ°À» ¸¸µé±â À§ÇØ gtk_button_new_with_labe()À» ÀÌ¿ëÇÒ ¼ö ÀÖ°í, ºó ¹öÆ°À» ¸¸µé±â À§ÇØ gtk_button_new()¸¦ ÀÌ¿ëÇÒ ¼öµµ ÀÖ´Ù. ±×·± ´ÙÀ½ ±×°Í¿¡ ¶óº§À» ºÙÀ̵çÁö ÇȽº¸ÊÀ» ºÙÀ̵çÁö ÇÏ´Â °ÍÀº ¿©·¯ºÐ¿¡°Ô ´Þ·ÁÀÖ´Ù. ±×·¸°Ô ÇÏ·Á¸é »õ·Î¿î ¹Ú½º¸¦ ¸¸µé°í gtk_box_pack_start·Î½á ÀÌ ¹Ú½º ¾È¿¡ ¿ì¸®ÀÇ object¸¦ ÆÐÅ·Çϸç, ±× ´ÙÀ½¿£ gtk_container_add·Î½á ±× ¹Ú½º¸¦ ¹öÆ° ¾ÈÀ¸·Î ÆÐÅ·ÇÏ¸é µÈ´Ù.

ÀÌ ¿¹Á¦´Â gtk_button_new¸¦ ÀÌ¿ëÇÏ¿© ±×¸²°ú ¶óº§ÀÌ ÀÖ´Â ¹öÆ°À» ¸¸µç´Ù. ¹Ú½º¸¦ ¸¸µå´Â Äڵ尡 ´Ù¸¥ °Íµé·ÎºÎÅÍ ¶³¾îÁ® ³ª¿Í ÀÖÀ¸¸ç ¿©·¯ºÐÀÇ ÇÁ·Î±×·¥ ¿¡¼­ ±×°ÍÀ» ÀÌ¿ëÇÒ ¼ö ÀÖÀ» °ÍÀÌ´Ù.

/* buttons.c */

#include <gtk/gtk.h>

/* À̹ÌÁö¿Í ±×°Í¿¡ ÆÐÅ·µÈ ¶óº§À» °¡Áö°í ÀÖ´Â hbox¸¦ Çϳª ¸¸µç´Ù.
 * ±×¸®°í ±× ¹Ú½º¸¦ ¸®ÅÏÇÑ´Ù. */

GtkWidget *xpm_label_box (GtkWidget *parent, gchar *xpm_filename, gchar *label_text)
{
                GtkWidget *box1;
                GtkWidget *label;
                GtkWidget *pixmapwid;
                GdkPixmap *pixmap;
                GdkBitmap *mask;
                GtkStyle *style;

                /* xpm°ú ¶óº§À» À§ÇÑ ¹Ú½º¸¦ ¸¸µç´Ù. */
                box1 = gtk_hbox_new (FALSE, 0);
                gtk_container_border_width (GTK_CONTAINER (box1), 2);

                /* ¹öÆ°ÀÇ ½ºÅ¸ÀÏÀ» ÃëÇÑ´Ù.. background »ö±òÀ» ÃëÇÏ´Â °Í °°Àºµ¥,
                 * ¾Æ´Ï¶ó¸é ´©±º°¡ ³ª¿¡°Ô Á¤Á¤ÇØ ÁÖ±æ ¹Ù¶õ´Ù. */
                style = gtk_widget_get_style(parent);

                /* xpm ÆÄÀϷκÎÅÍ ÇȽº¸ÊÀ» ¸¸µç´Ù. */
                pixmap = gdk_pixmap_create_from_xpm (parent->window, &mask,
                                             &style->bg[GTK_STATE_NORMAL],
                                             xpm_filename);
                pixmapwid = gtk_pixmap_new (pixmap, mask);

                /* ¹öÆ°À» À§ÇÑ ¶óº§À» ¸¸µç´Ù. */
                label = gtk_label_new (label_text);

                /* ¹Ú½º ¾ÈÀ¸·Î ÇȽº¸Ê°ú ¶óº§À» ÆÐÅ·ÇØ ³Ö´Â´Ù. */
                gtk_box_pack_start (GTK_BOX (box1),
                                                        pixmapwid, FALSE, FALSE, 3);

                gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 3);

                gtk_widget_show(pixmapwid);
                gtk_widget_show(label);

                return (box1);
}

/* ¿ì¸®ÀÇ ÀüÇüÀûÀÎ callback ÇÔ¼ö´Ù. */
void callback (GtkWidget *widget, gpointer data)
{
                g_print ("Hello again - %s was pressed\n", (char *) data);
}

int main (int argc, char *argv[])
{
                /* GtkWidgetÀº widgetµéÀ» À§ÇÑ ±â¾ïÀå¼Ò Á¾·ù´Ù. */
                GtkWidget *window;
                GtkWidget *button;
                GtkWidget *box1;

                gtk_init (&argc, &argv);

                /* À©µµ¸¦ Çϳª ¸¸µç´Ù. */
                window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

                gtk_window_set_title (GTK_WINDOW (window), "Pixmap'd Buttons!");

                /* ¸ðµç À©µµµé¿¡ ´ëÇØ ÀÌ·¸°Ô ÇØÁÖ´Â °ÍÀÌ ÁÁÀ» °ÍÀÌ´Ù. */
                gtk_signal_connect (GTK_OBJECT (window), "destroy",
                            GTK_SIGNAL_FUNC (gtk_exit), NULL);

                /* À©µµÀÇ border width¸¦ ¼¼ÆÃÇÑ´Ù. */
                gtk_container_border_width (GTK_CONTAINER (window), 10);

                /* »õ·Î¿î ¹öÆ°À» Çϳª ¸¸µç´Ù. */
                button = gtk_button_new ();

                /* ÀÌÂë¿¡¼­ ÀÌ ÇÔ¼ö¸¦ ¾²´Â °ÍÀ» ±â¾ïÇ϶ó. */
                gtk_signal_connect (GTK_OBJECT (button), "clicked",
                            GTK_SIGNAL_FUNC (callback), (gpointer) "cool button");

                /* ¹Ú½º¸¦ ¸¸µå´Â ÇÔ¼ö´Ù. */
                box1 = xpm_label_box(window, "info.xpm", "cool button");

                /* ¿ì¸® widgetµéÀ» ÆÐÅ·ÇÏ°í, º¸¿©ÁØ´Ù. */
                gtk_widget_show(box1);

                gtk_container_add (GTK_CONTAINER (button), box1);

                gtk_widget_show(button);

                gtk_container_add (GTK_CONTAINER (window), button);

                gtk_widget_show (window);

                /* ¿©±â¼­ºÎÅÍ´Â ¹º°¡ Àç¹ÌÀÖ´Â ÀÏÀÌ ½ÃÀÛµÉ ¶§±îÁö ¹«ÀÛÁ¤ ±â´Ù¸°´Ù. */
                gtk_main ();

                return 0;
}

xpm_label_box ÇÔ¼ö´Â ÄÁÅ×À̳ʰ¡ µÉ ¼ö ÀÖ´Â ¾î¶² widget¿¡¶óµµ ÇȽº¸Ê°ú ¶óº§À» ÆÐÅ·Çϱâ À§ÇÏ¿© ¾²¿©Áú ¼ö ÀÖÀ» °ÍÀÌ´Ù.

7.2 Åä±Û ¹öÆ°

Åä±Û¹öÆ°Àº Ŭ¸¯¿¡ ÀÇÇØ ÅÃÀϵǴ µÎ°¡Áö Áß ¾î´À ÇÑ »óÅ¿¡ ÀÖ¾î¾ß ÇÑ´Ù´Â Á¡¸¸ »«´Ù¸é º¸Åë ¹öÆ°°ú ¸Å¿ì À¯»çÇÏ´Ù. ±×°ÍÀº ´­·ÁÁø »óÅ·ΠÀÖ´Ù°¡µµ, ¿ì¸®°¡ ´Ù½Ã Ŭ¸¯ÇØ ÁÖ¸é ´Ù½Ã Æ¢¾î³ª¿À°Ô µÉ ¼ö ÀÖ´Ù. ¶Ç Ŭ¸¯ÇÏ¸é ±×°ÍÀº ´Ù½Ã ´­·¯Á® µé¾î°¥ °ÍÀÌ´Ù.

Åä±Û¹öÆ°¿¡ ¾²ÀÌ´Â ¸¹Àº ÇÔ¼öµéÀÌ ¶óµð¿À¿Í üũ ¹öÆ°¿¡ ÀÇÇØ »ó¼ÓµÇ¾î ¾²À̵íÀÌ, Åä±Û¹öÆ°Àº üũ¹öÆ°°ú ¶óµð¿À¹öÆ°ÀÇ ±â¹ÝÀÌ µÈ´Ù. À̰͵éÀ» Á¢ÇÏ°Ô µÇ¸é ÀÌ Á¡À» ´Ù½Ã ÁöÀûÇÒ °ÍÀÌ´Ù.

»õ·Î¿î Åä±Û¹öÆ°À» ¸¸µé·Á¸é ÀÌ°ÍÀ» ÀÌ¿ëÇÑ´Ù.

GtkWidget* gtk_toggle_button_new (void);

GtkWidget* gtk_toggle_button_new_with_label (gchar *label);

ÃßÃøÇÒ ¼ö ÀÖ°ÚÁö¸¸, ÀÌ°ÍÀº º¸Åë¹öÆ° widgetÀÇ È£Ãâ°ú ¶È°°ÀÌ ÀÛ¿ëÇÑ´Ù. ù¹ø° °ÍÀº ºó Åä±Û¹öÆ°À», ±×¸®°í µÎ¹ø° °ÍÀº ÀÌ¹Ì ¶óº§widgetÀÌ ÆÐÅ·µÇ¾î ÀÖ´Â ¹öÆ°À» ¸¸µç´Ù.

Åä±Û¹öÆ°°ú ¶óµð¿À¹öÆ°, üũ¹öÆ°ÀÇ »óŸ¦ µÇµ¹¸®±â À§ÇØ, ¿ì¸®´Â ¾Æ·¡ÀÇ ¿¹Á¦¿¡¼­ º¸¿©Áú ¸ÅÅ©·Î¸¦ ÀÌ¿ëÇÑ´Ù. ÀÌ°ÍÀº ¾î¶² callback¿¡¼­ Åä±ÛÀÇ »óŸ¦ Å×½ºÆ®ÇÑ´Ù. Åä±Û¹öÆ°¿¡ ÀÇÇØ ¹ß»ýÇÏ´Â ½Ã±×³ÎÀº (Åä±Û/üũ/¶óµð¿À ¹öÆ° widgetµé) "toggled" ½Ã±×³ÎÀÌ´Ù. ÀÌ ¹öÆ°µéÀÇ »óŸ¦ üũÇÏ·Á¸é ÀÌ toggled ½Ã±×³ÎÀ» Àâ¾Æ³»µµ·Ï ½Ã±×³Î Çڵ鷯¸¦ ¼Â¾÷ÇÏ°í, ±×°ÍÀÇ »óŸ¦ °áÁ¤Çϱâ À§ÇÑ ¸ÅÅ©·Î¸¦ ÀÌ¿ëÇÑ´Ù. ±× callbackÀº ÀÌ·± ¸ð¾çÀÇ °ÍÀÌ´Ù.

void toggle_button_callback (GtkWidget *widget, gpointer   data)
{
    if (GTK_TOGGLE_BUTTON (widget)->active)
    {
        /* ÄÁÆ®·ÑÀÌ ¿©±â·Î ¿À¸é, Åä±Û¹öÆ°ÀÇ »óÅ´ upÀÌ´Ù. */

    } else {

        /* Åä±Û¹öÆ°Àº downÀÌ´Ù. */
    }
}

void gtk_toggle_button_set_state (GtkToggleButton *toggle_button,
                                  gint state);

À§ÀÇ È£ÃâÀº Åä±Û¹öÆ°°ú, ±×°Í¿¡¼­ ÆÄ»ýµÇ´Â ¶óµð¿À¿Í üũ¹öÆ°ÀÇ »óŸ¦ ¼¼ÆÃÇϱâ À§ÇØ ¾²ÀÏ ¼ö ÀÖ´Ù. ù¹ø° ÀÎÀÚ·Î ¿ì¸®°¡ ¸¸µç ¹öÆ°À» ³Ñ°ÜÁÖ°í, ±×¸®°í ±×°ÍÀÌ ´­¸° »óÅÂÀÎÁö ¾Æ´ÑÁö¸¦ ±¸º°Çϱâ À§ÇØ µÎ¹ø° ÀÎÀÚ¸¦ TRUE ¶Ç´Â FALSE·Î ³Ñ°ÜÁØ´Ù. µðÆúÆ®´Â ¾È ´­·¯Áø »óÅÂ, Áï FALSEÀÌ´Ù.

¿ì¸®°¡ gtk_toggle_button_set_state()ÇÔ¼ö¸¦ ¾²¸é ¹öÆ°¿¡¼­ "clicked"½Ã±×³ÎÀÌ ¹ß»ýÇؼ­ ¹öÆ°ÀÇ »óÅ°¡ ½ÇÁ¦·Î º¯ÇÏ°Ô µÊÀ» ±â¾ïÇÏÀÚ.

void    gtk_toggle_button_toggled (GtkToggleButton *toggle_button);

ÀÌ°ÍÀº °£´ÜÈ÷ ¹öÆ°À» Åä±ÛÇÏ°í, "toggled"½Ã±×³ÎÀ» ¹ß»ý½ÃŲ´Ù.

7.3 üũ¹öÆ°

üũ¹öÆ°Àº À§¿¡ ÀÖ´Â Åä±Û¹öÆ°¿¡¼­ ¸¹Àº Ư¼º°ú ÇÔ¼öµéÀ» »ó¼Ó¹Þ¾ÒÁö¸¸ ´Ù¼Ò ´Ù¸£°Ô º¸ÀδÙ. ÀÌ°ÍÀº ¹öÆ°°ú ±× ¾ÈÀÇ ÅؽºÆ®·Î Àִٱ⺸´Ù´Â, ÅؽºÆ® ¿·¿¡ ÀÖ´Â ÀÛÀº »ç°¢ÇüÀ̶ó°í ÇÒ ¼ö ÀÖ´Ù. À̵éÀº ¾î¶² ¾îÇÿ¡¼­ Åä±ÛµÇ´Â ¿É¼ÇÀ¸·Î¼­ ¸¹ÀÌ ºÃÀ» °ÍÀÌ´Ù.

ÀÌ°ÍÀ» ¸¸µå´Â µÎ°¡Áö ÇÔ¼ö´Â º¸Åë¹öÆ°¿¡¼­¿Í ¸¶Âù°¡Áö´Ù.

GtkWidget* gtk_check_button_new (void);

GtkWidget* gtk_check_button_new_with_label (gchar *label);

new_with_labelÇÔ¼ö´Â ¿·¿¡ ÅؽºÆ® ¶óº§À» °¡Áö°í Àִ üũ¹öÆ°À» ¸¸µç´Ù.

üũ¹öÆ°ÀÇ »óŸ¦ üũÇÏ´Â °ÍÀº Åä±Û¹öÆ°¿¡¼­¿Í °°´Ù.

7.4 ¶óµð¿À¹öÆ°

¶óµð¿À¹öÆ°Àº ±×µéÀÌ ±×·ìÈ­µÇ¾î À־ ÇÑ ¹ø¿¡ ¿ÀÁ÷ Çϳª¾¿¸¸ ¼±ÅÃ/º¹±ÍµÉ ¼ö ÀÖ´Ù´Â Á¡¸¸ »©°í´Â üũ¹öÆ°°ú À¯»çÇÏ´Ù. ÀÌ°ÍÀº ¾îÇÃÀÌ ¿É¼ÇÀÇ ¸®½ºÆ®¿¡¼­ Çϳª¸¦ ¼±ÅÃÇϵµ·Ï ÇÏ´Â °æ¿ì¿¡ ¾²À̸é ÁÁÀ» °ÍÀÌ´Ù.

»õ·Î¿î ¶óµð¿À¹öÆ°À» ¸¸µå´Â µ¥ ¾²´Â ÇÔ¼öµéÀÌ´Ù.

GtkWidget* gtk_radio_button_new (GSList *group);

GtkWidget* gtk_radio_button_new_with_label (GSList *group,
                                            gchar *label);

ÀÌ È£Ãâµé¿¡´Â ´Ù¼Ò ´Ù¸¥ ÀÎÀÚ°¡ ÀÖ´Ù´Â °ÍÀ» ´«¿©°Ü º¸ÀÚ. À̵éÀº ÀûÀýÈ÷ ÀÓ¹«¸¦ ¼öÇàÇÒ ±×·ìÀ» ÇÊ¿ä·Î ÇÑ´Ù. ù¹ø° ÇÔ¼ö´Â ù¹ø° ÀÎÀÚ·Î NULLÀ» ³Ñ°Ü Áà¾ß ÇÑ´Ù. ±×¸®°í ¿ì¸®´Â ÀÌ°ÍÀ» ÀÌ¿ëÇÏ¿© ±×·ìÀ» ¸¸µç´Ù.

GSList* gtk_radio_button_group (GtkRadioButton *radio_button);

±×¸®°í´Â ÀÌ ±×·ìÀ» gtk_radio_button_new ¶Ç´Â gtk_radio_button_new_with_ label¿¡ ù¹ø° ÀÎÀÚ·Î ³Ñ°ÜÁØ´Ù. ´ÙÀ½À» ÀÌ¿ëÇؼ­ ¾î¶² ¹öÆ°ÀÌ µðÆúÆ®·Î ´­·¯Áö°Ô µÇ´ÂÁö¸¦ ºÐ¸íÈ÷ Çصδ °Íµµ ÁÁÀº »ý°¢ÀÌ´Ù.

void gtk_toggle_button_set_state (GtkToggleButton *toggle_button,
                                  gint state);

ÀÌ°ÍÀº Åä±Û¹öÆ°¿¡ ´ëÇÑ ºÎºÐ¿¡¼­ ¼³¸íµÇ¾úÀ¸¸ç, Á¤È®È÷ °°Àº ¹æ¹ýÀ» µû¸¥´Ù.

´ÙÀ½ ¿¹´Â ¼¼°³ÀÇ ¹öÆ°À¸·Î ÀÌ·ç¾îÁø ±×·ìÀ» ¸¸µç´Ù.

/* radiobuttons.c */

#include <gtk/gtk.h>
#include <glib.h>

void close_application( GtkWidget *widget, gpointer data ) {
  gtk_main_quit();
}

main(int argc,char *argv[])
{
  static GtkWidget *window = NULL;
  GtkWidget *box1;
  GtkWidget *box2;
  GtkWidget *button;
  GtkWidget *separator;
  GSList *group;
  
  gtk_init(&argc,&argv);          
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  
  gtk_signal_connect (GTK_OBJECT (window), "delete_event",
                      GTK_SIGNAL_FUNC(close_application),
                      NULL);

  gtk_window_set_title (GTK_WINDOW (window), "radio buttons");
  gtk_container_border_width (GTK_CONTAINER (window), 0);

  box1 = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), box1);
  gtk_widget_show (box1);

  box2 = gtk_vbox_new (FALSE, 10);
  gtk_container_border_width (GTK_CONTAINER (box2), 10);
  gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
  gtk_widget_show (box2);

  button = gtk_radio_button_new_with_label (NULL, "button1");
  gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
  gtk_widget_show (button);

  group = gtk_radio_button_group (GTK_RADIO_BUTTON (button));
  button = gtk_radio_button_new_with_label(group, "button2");
  gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (button), TRUE);
  gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
  gtk_widget_show (button);

  group = gtk_radio_button_group (GTK_RADIO_BUTTON (button));
  button = gtk_radio_button_new_with_label(group, "button3");
  gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
  gtk_widget_show (button);

  separator = gtk_hseparator_new ();
  gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
  gtk_widget_show (separator);

  box2 = gtk_vbox_new (FALSE, 10);
  gtk_container_border_width (GTK_CONTAINER (box2), 10);
  gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
  gtk_widget_show (box2);

  button = gtk_button_new_with_label ("close");
  gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
                             GTK_SIGNAL_FUNC(close_application),
                             GTK_OBJECT (window));
  gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_widget_grab_default (button);
  gtk_widget_show (button);
  gtk_widget_show (window);
     
  gtk_main();
  return(0);
}

´ÙÀ½ ¹®Àå°ú °°Àº ½ÄÀ¸·Î ¾î¶² º¯¼ö Çϳª°¡ ¹öÆ°µéÀÇ ¸®½ºÆ®¸¦ °¡Áö°í ÀÖ¾î¾ßÇÒ ÇÊ¿ä°¡ ¾øµµ·Ï ÁÙ¿© ¾µ ¼öµµ ÀÖ´Ù.

     button2 = gtk_radio_button_new_with_label(
                 gtk_radio_button_group (GTK_RADIO_BUTTON (button1)),
                 "button2");


´ÙÀ½ ÀÌÀü Â÷·Ê