<!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//tr">

<html>
  <head>
    <meta name="generator" content="HTML Tidy, see www.w3.org">
    <meta http-equiv="Content-Type" content=
    "text/html; charset=iso-8859-9">

    <title>GUI Programming with GTK - 3</title>
    <!-- this stylesheet will later on be added by lfparser automatically: -->
<style type="text/css">
<!--
  pre { font-family:monospace,Courier }
 pre.code { font-family:monospace,Courier;background-color:#aedbe8; }
  p.code { width:80%; alignment:center; background-color:#aedbe8; border-style:n
one; border-width:medium; border-color:#aedbe8; padding:0.1cm ; text-align:left
}
-->
</style>
  </head>

  <body>
    <h1>GUI Programming with GTK - 3</h1>

    <h4>ArticleCategory:</h4>
    SoftwareDevelopment 

    <h4>AuthorImage:</h4>
    <img src="../../common/images/OzcanGungor.jpg" alt=
    "Ozcan Gungor" width="114" height="147"> 

    <h4>TranslationInfo:</h4>
    original in tr <a href=
    "mailto:ozcangungor(at)netscape.net">&Ouml;zcan
    G&uuml;ng&ouml;r</a><br>
     en to tr <a href=
    "mailto:ozcangungor(at)netscape.net">&Ouml;zcan
    G&uuml;ng&ouml;r</a> <br>
     <br>
     

    <h4>AboutTheAuthor</h4>
    I'm doing at the moment my military service as Linux, Oracle
    administrator and web programmer. 

    <h4>Abstract</h4>

    <p>In these article series, we will learn how to write
    graphical user interfaces (GUIs) using GTK. I do not have any
    idea how long it will last. In order to understand these
    articles, you should know the following about the C programming
    language:</p>

    <ul>
      <li>Variables</li>

      <li>Functions</li>

      <li>Pointers</li>
    </ul>
    It is recommended to read the previous articles:<br>
     <a href="../../English/May2003/article295.shtml">GUI
    Programming with GTK</a>,<br>
     <a href="../../English/July2003/article303.shtml">GUI
    Programming with GTK - 2</a> .<br> This article
is a little bit shorter than the others as I'm doing my military
    service. <br>
     

    <h4>ArticleIllustration:</h4>
    <img src="../../common/images/illustration295.jpg" alt="GTK"
    width="195" height="172"> 

    <h4>ArticleBody:</h4>

    <h3>Toggle Button</h3>

    <p>This button looks like a normal button but has two states:
    Pressed or not. To create a toogle button one of the following
    function is used:</p>
<pre class="code">
GtkWidget *toggle=gtk_toggle_button_new(void);
GtkWidget *toggle=gtk_toggle_button_new_with_label(const gchar *label);
</pre>

    <p>The first function creates a toggle button without a text label
    in it but the second one creates it with the string <i>label</i> on
    it.<br>
     You can set its state with the following function:<br>
    </p>
<pre class="code">
gtk_toggle_button_set_active (GtkToggleButton *toggle_button, 
                              gboolean is_active);
</pre>

    <p>where <i>toggle_button</i> is the button whose state you
    want to change and <i>is_active</i> is the state (0 or 1). When
    it is 0, then the button is not in 
     "pressed" state; when it is 1, the button is in "pressed" state.</p>

    <p>To get the state of the button, the following function can
    be used:</p>
<pre class="code">
gboolean gtk_toggle_button_get_active(GtkToggleButton *button);
</pre>

    <p>The "toggled" event can be connected to a toggle button.</p>

    <p>Here is an example:</p>
<pre class="code">
#include &lt;gtk/gtk.h&gt;
void togg(GtkWidget *widget, gpointer *data){
   if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data)))
    g_print("State is 1\n");
   else
    g_print("State is 0\n");
}

int main( int argc,char *argv[] )
   {
   
   GtkWidget *window;
   GtkWidget *button;

   gtk_init (&amp;argc, &amp;argv);

   /* Create a new window */
   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   gtk_window_set_title (GTK_WINDOW (window), "Toggle Button");

   /* Connect destroy event to the window. */
   gtk_signal_connect (GTK_OBJECT (window), "destroy",
                       GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
   
   /* Creates a toggle button */ 
   button=gtk_toggle_button_new_with_label("I'm a toggle button");
   
   /* Add the button to window */
   gtk_container_add(GTK_CONTAINER(window),button);

   /* Connect "toggled" event to the button */ 
   gtk_signal_connect (GTK_OBJECT (button), "toggled",
                       GTK_SIGNAL_FUNC(togg),(gpointer *)button);

   gtk_widget_show(button);
   gtk_widget_show (window);
   
   gtk_main ();
   return(0);
}
</pre>

    <h3>Check Button</h3>

    <p>The check button ( it is also known as check box ) is a subclass
    of the toggle button. It can be used to select some options.<br>
     To create a check button, the following fuction is used:</p>
<pre class="code">
GtkWidget* gtk_check_button_new (void);
GtkWidget* gtk_check_button_new_with_label (const gchar *label);
</pre>

    <p>The explanations are the same as for toggle button.</p>

    <p>The example :</p>
<pre class="code">
#include &lt;gtk/gtk.h&gt;
void togg(GtkWidget *widget, gpointer *data){
   if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data)))
       g_print("State is 1\n");
   else
    g_print("State is 0\n");
}

int main( int argc,char *argv[] )
{

    GtkWidget *window;
    GtkWidget *button;

    gtk_init (&amp;argc, &amp;argv);
    
    /* Create a new window */
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "Check Button");

    /* Connect destroy event to the window. */
    gtk_signal_connect (GTK_OBJECT (window), "destroy",
                        GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
   
    /* Creates a check button */ 
    button=gtk_check_button_new_with_label("I'm a check button");
   
    /* Add the button to window */
    gtk_container_add(GTK_CONTAINER(window),button);

    /* Connect "toggled" event to the button */ 
    gtk_signal_connect (GTK_OBJECT (button), "toggled",
                        GTK_SIGNAL_FUNC(togg), (gpointer *)button);
    gtk_widget_show(button);
    gtk_widget_show (window);
   
    gtk_main ();
    return(0);
}
</pre>

    <h3>Label</h3>

    <p>Labels let you put any text anywhere in window.<br>
     To create a label just use the following function:</p>
<pre class="code">
GtkWidget* gtk_label_new(const gchar *text);
</pre>

    <p>With the function</p>
<pre class="code">
gtk_label_set_text(GtkLabel *label, gchar *text);
</pre>

    <p>you can change the string on a label at anytime.</p>
<pre class="code">
gtk_label_set_justify(GtkLabel *label, GtkJustification jtype);
</pre>

    <p>The gtk_label_set_justify funtion is used to justify the text on the label.
    <i>jtype</i> can be</p>

    <ul>
      <li>GTK_JUSTIFY_LEFT to put the text to the left,</li>

      <li>GTK_JUSTIFY_RIGHT to put the text to the rigth,</li>

      <li>GTK_JUSTIFY_CENTER to put the text in the center,</li>

      <li>GTK_JUSTIFY_FILL to make the text cover the whole 
      label.</li>
    </ul>
<pre class="code">
gtk_label_set_line_wrap (GtkLabel *label,gboolean wrap);
</pre>

    <p>is used to make the text splittable into many pieces when
    the text is longer than the place it should fit. When <i>wrap</i> is
    1, then text will <i>wrap</i> to the next line, otherwise not.
</p>
<pre class="code">
gtk_label_get(GtkLabel *label, gchar **str)
</pre>

    <p>is used to get the text on the label into <i>str</i>.</p>

    <h3>ToolTips</h3>

    <p>Tooltip is a text that appears when the mouse is on a
    widget. For example, you can set a tip for a button and the
    text "send the information"
     appears when your mouse is over it.</p>

    <p>To do that a GtkToolTips widget must be created first:</p>
<pre class="code">
GtkToolTips* gtk_tooltips_new();
</pre>

    <p>Then this tooltip is attached to a widget:</p>
<pre class="code">
gtk_tooltips_set_tip(GtkTooltips *tooltips, GtkWidget *widget, 
                     const gchar *tip_text,const gchar *tip_private);
</pre>

    <p>A little example:</p>
<pre class="code">
#include &lt;gtk/gtk.h&gt;
int main( int argc,char *argv[] )
{
    GtkWidget *window;
    GtkWidget *button;
    GtkTooltips *tip;
    
    gtk_init (&amp;argc, &amp;argv);

    /* Create a new window */
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "Tooltips");

    /* Connect destroy event to the window. */
    gtk_signal_connect (GTK_OBJECT (window), "destroy",
                        GTK_SIGNAL_FUNC (gtk_main_quit), NULL);

    /* Creates a button */
    button=gtk_button_new_with_label("I'm a Button");

    /* Add the button to window */
    gtk_container_add(GTK_CONTAINER(window),button);

    /* Creates a tooltips*/
    tip=gtk_tooltips_new();

    /* Attache this tooltips to button with text*/
    gtk_tooltips_set_tip(tip, button, "Click me!",NULL);

    gtk_widget_show(button);
    gtk_widget_show (window);

    gtk_main ();
    return(0);
}
</pre>

    <p>Some other functions:</p>
<pre class="code">
gtk_tooltips_enable (GtkTooltips *tooltips);
</pre>

    <p>Enables the tooltips.</p>
<pre class="code">
gtk_tooltips_disable (GtkTooltips *tooltips);
</pre>

    <p>Disables tooltips.</p>

    <p>To get any tooltip data of a wigdet, we need</p>
<pre class="code">
GtkTooltipsData* gtk_tooltips_get_data(GtkWidget *widget);
</pre>

    <p>GtkTooltipsData is a struct in the following way:
    </p>
<pre class="code">
struct _GtkTooltipsData
{
   GtkTooltips *tooltips;
   GtkWidget *widget;
   gchar *tip_text;
   gchar *tip_private;
   GdkFont *font;
   gint width;
   GList *row;
};
</pre>
    To set the delay of the appearing text, 
<pre class="code">
gtk_tooltips_set_delay (GtkTooltips *tip, guint delay)
</pre>

    <h3><br>
     Combo Box</h3>

    <p>A combo box is an editable text field combined with a pull-down menu.
    You can enter a value or select one of the pull-down entries.</p>

    <p>A combo box can be created with</p>
<pre class="code">
GtkWidget *gtk_combo_new();
</pre>

    <p>And we need a list of options which is a GList struct.</p>
<pre class="code">
GList *glist=NULL;
</pre>

    <p>And the options can be appended to the list with</p>
<pre class="code">
GList *g_list_append(GList *list, gchar *option);
</pre>

    <p>Then this list needs to be added to the combo box</p>
<pre class="code">
gtk_combo_set_popdown_strings(GtkCombo *combo, GList *List);
</pre>

    <p>The combo box is ready. To read the selected option use:</p>
<pre class="code">
gchar *gtk_entry_get_text(GtkEntry *entry); 
</pre>

    <p><i>entry</i> is GTK_ENTRY(GTK_COMBO(combo)-&gt;entry)) in
    this case.</p>
<pre class="code">
gtk_combo_set_use_arrows(GtkCombo *combo,gint val); 
</pre>

    <p>This function is used to enable or disable up/down keys on
    the keyboard to change the value on a combo box. When <i>val</i>
    is 0, these keys do not function, otherwise these keys change
    the value. But when the value on a combo box is different from the
    values in the list, <b>these keys don't function</b>.</p>
<pre class="code">
gtk_combo_set_use_arrows_always(GtkCombo *combo,gint val);
</pre>

    <p>This function is the same as <i>gtk_combo_set_use_arrows</i>
    but when the value on a combo box is different form the values in
    the list, <b>these keys function</b>.</p>
<pre class="code">
gtk_combo_set_value_in_list(GtkCombo *combo, gboolean val, 
                             gboolean ok_if_empty);
</pre>

    <p>When <i>val</i> is 1, you can enter a value in the list. When
    <i>ok_if_empty</i> is 1, the value may be blank.</p>

<pre class="code">
#include &lt;gtk/gtk.h&gt;
void act(GtkWidget *widget, gpointer *data){ 
   g_print((gchar *)data);
}

int main( int argc,char *argv[] ) {
   GtkWidget *window;
   GtkWidget *combo;
   GtkWidget *button;
   GtkWidget *box;
   GList *list=NULL; 

   list=g_list_append(list,"Slackware");
   list=g_list_append(list,"RedHat");
   list=g_list_append(list,"SuSE");

   gtk_init (&amp;argc, &amp;argv); 
   
   /* Create a new window */
   window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 
   gtk_window_set_title (GTK_WINDOW (window), "Combo Box");
   
   /* Connect destroy event to the window. */
   gtk_signal_connect (GTK_OBJECT (window), "destroy",
                       GTK_SIGNAL_FUNC(gtk_main_quit), NULL); 
   
   /* Create a new horizontal box */
   box=gtk_hbox_new(1,0);
   gtk_container_add(GTK_CONTAINER(window),box);
   
   /* Creates a combo box */ 
   combo=gtk_combo_new();

   /* Sets the list */
   gtk_combo_set_popdown_strings(GTK_COMBO(combo),list);

   /* Enables up/down keys change the value. */
   gtk_combo_set_use_arrows_always(GTK_COMBO(combo),1);

   gtk_box_pack_start(GTK_BOX(box), combo,1,1,1); 

   button=gtk_button_new_with_label("Write it");
   gtk_signal_connect(GTK_OBJECT(button),"clicked",GTK_SIGNAL_FUNC(act), 
             gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo)-&gt;entry)));
   gtk_box_pack_start(GTK_BOX(box), button,1,1,1);

   gtk_widget_show(box);
   gtk_widget_show(combo);
   gtk_widget_show(button); 
   gtk_widget_show (window);
   
   gtk_main ();
   return(0);
   }
</pre>
    All comments are welcome.
  </body>
</html>