GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Apr 19, 2014 12:01 pm

All times are UTC




Post new topic Reply to topic  [ 37 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject: Re: Gtk+ Tutorials & Resources
PostPosted: Tue Jul 17, 2012 7:19 pm 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 176
Location: USA
Status Icons part4, individual menu item colors

First let me say that creating a different color for one label in a menu should be very very limited. Maybe your developing the application for work and your boss or the client just wants it that way. Maybe you have accidentally went to a web page that's trying to run some very destructive code on your computer. You could have an application that's a "emergency Internet stop" A menu item that when activated kills the process that is your browser, locks all Internet access and maybe even shuts down the computer. I have seen similar applications in anti-virus software.

The point is you should have a good reason for doing it. So don't create a menu where the background colors and font colors of each menuitem are different for no reason! A little styling goes a very very long way, don't abuse it!

To style just the first menu item label you may think we could add: gtk_widget_set_name(GTK_WIDGET(item1),"mitem1");
Then use: GtkLabel #mitem1
You'd be wrong!
First lets look at the function used to create a stand-alone label. That is, one that's not going in a menu, It's:
GtkWidget *gtk_label_new (const gchar *str);

str: The text of the label
Returns: the new GtkLabel

It returns a pointer to a GtkLabel object. We could then name it just like we did in our C code using:

GtkWidget * label = gtk_label_new ( "somelabel" );
gtk_widget_set_name ( GTKWIDGET(label), "somelabel" );


Then use the name in the CSS to style it using:
#somelabel {
color: red;
}


But #mitem1 is not a GtkLabel, it's a GtkMenuItem. Remember The function we used to create the label for the menu was:
gtk_menu_item_new_with_label ()

It creates a new GtkMenuItem whose child is a GtkLabel. It returns a pointer to the newly created GtkMenuItem. NOT the label, the label object is internal, we don't have access to it. The definition of gtk_menu_item_new_with_label () is in gtkmenuitem.c on lines 514 thru 520.
Code:
GtkWidget*
gtk_menu_item_new_with_label (const gchar *label)
{
  return g_object_new (GTK_TYPE_MENU_ITEM,
                       "label", label,
                       NULL);
}

And g_object_new () is in the GObject reference manual in the section: The Base Object Type
Code:
gpointer  g_object_new (GType object_type,
                        const gchar *first_property_name,
                        ...);

Returns: a new instance of object_type.

And we can see in the definition of gtk_menu_item_new_with_label() that g_object_new was given: GTK_TYPE_MENU_ITEM. So that's the return type, a GtkMenuItem NOT a GtkLabel. That's why we can't access the label through CSS. But there is a way...

We need access to the GtkMenuItem's child which is the GtkLabel we're looking to style. Here's the object hierarchy for GtkMenuItem.
Code:
+----GtkBin
     +----GtkMenuItem

In the callback function cb_right_click() we add the following:
Create pointers to both labels:
GtkWidget *itemlabel1, *itemlabel2;

Use the function: gtk_bin_get_child () It gets the child of the GtkBin, or NULL if the bin contains no child widget.
itemlabel1 = gtk_bin_get_child (GTK_BIN (item1));
itemlabel2 = gtk_bin_get_child (GTK_BIN (item2));


Create two ponters to style contexts'
GtkStyleContext *context1, GtkStyleContext *context2;

Returns the associated style contexts'
context1 = gtk_widget_get_style_context (itemlabel1);
context2 = gtk_widget_get_style_context (itemlabel2);


Add new style classes for each one:
gtk_style_context_add_class(context1, "label1");
gtk_style_context_add_class(context2, "label2");


Save the context states, so all modifications done through gtk_style_context_add_class() can be reverted using gtk_style_context_restore().
gtk_style_context_save (context1);
gtk_style_context_save (context2);


Restore context states' to their previous stage.
gtk_style_context_restore (context1);
gtk_style_context_restore (context2);


All the built in types have predefined CSS selectors. For example when styling a button, we can can use: GtkButton or .button
The . is called a class selector and can be used to style any class which it precedes.
Now we can use label1 and label2 just like the built in type .button in our CSS to style the labels individually. We use:

.label1 {
color: black;
}

.label1:hover {
color: forestgreen;
}

.label2 {
color: cornflowerblue;
}

.label2:hover {
color: yellow;
}


The full code is listed below. I've added two more style contexts' for the GtkMenuItem's. This let's us use different background colors for the hover state of each menu item.
Code:
/* COMPILE WITH:
    gcc -Wall -o icon4 `pkg-config --cflags --libs gtk+-3.0` icon4.c
*/

#include <gtk/gtk.h>
#include <string.h>  /* for CSS */

static void cb_right_click(GtkStatusIcon *icon, int button, int time, gpointer data)
{
   GtkWidget *menu;

   menu = gtk_menu_new ();
   gtk_widget_set_name(GTK_WIDGET(menu),"mymenu");
   GtkWidget *item1 = gtk_menu_item_new_with_label("About");
   gtk_widget_set_name(GTK_WIDGET(item1),"mitem1"); 

   GtkWidget *item2 = gtk_menu_item_new_with_label("Quit");

   gtk_menu_shell_append(GTK_MENU_SHELL(menu), item1);
   gtk_menu_shell_append(GTK_MENU_SHELL(menu), item2);

      gtk_widget_show_all(menu);
      gtk_menu_popup(GTK_MENU(menu),
                     NULL,
                     NULL,
                     gtk_status_icon_position_menu,
                     icon,
                     button,
                     time);

   g_signal_connect(G_OBJECT(item2), "activate", G_CALLBACK(gtk_main_quit), NULL);

  GtkWidget *itemlabel1, *itemlabel2;

  itemlabel1 = gtk_bin_get_child (GTK_BIN (item1));
  itemlabel2 = gtk_bin_get_child (GTK_BIN (item2));

  GtkStyleContext *context1, *context2, *context3, *context4;

  context1 = gtk_widget_get_style_context (itemlabel1);
  context2 = gtk_widget_get_style_context (itemlabel2);
  context3 = gtk_widget_get_style_context (item1);
  context4 = gtk_widget_get_style_context (item2);

  gtk_style_context_add_class(context1, "label1");
  gtk_style_context_add_class(context2, "label2");
  gtk_style_context_add_class(context3, "theitem1");
  gtk_style_context_add_class(context4, "theitem2");

  gtk_style_context_save (context1);
  gtk_style_context_save (context2);
  gtk_style_context_save (context3);
  gtk_style_context_save (context4);

  gtk_style_context_restore (context1);
  gtk_style_context_restore (context2);
  gtk_style_context_restore (context3);
  gtk_style_context_restore (context4);
}
/*----------------------------------------------------------------------------------------------------------------------*/

int main(int argc, char *argv[])
{
   GtkStatusIcon *icon;
/*-- CSS ------------------*/
  GtkCssProvider *provider;
  GdkDisplay *display;
  GdkScreen *screen;
/*---------------------------*/

   gtk_init(&argc, &argv);

   icon = gtk_status_icon_new_from_stock (GTK_STOCK_MEDIA_PLAY);
   
   g_signal_connect (G_OBJECT(icon), "popup-menu", G_CALLBACK(cb_right_click), NULL);

/*---------------- CSS ----------------------------------------------------------------------------------------------------*/
  provider = gtk_css_provider_new ();
  display = gdk_display_get_default ();
  screen = gdk_display_get_default_screen (display);
  gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

  gsize bytes_written, bytes_read;

  const gchar* home = "YourPathHere";  /* for instance /home/zerohour/gtk/programming/GtkStatusIcon/icon4.css     

  GError *error = 0;

  gtk_css_provider_load_from_path (provider,
                                   g_filename_to_utf8(home, strlen(home),
                                   &bytes_read, &bytes_written, &error),
                                   NULL);
  g_object_unref (provider);
/*-------------------------------------------------------------------------------------------------------------------------*/
   
   gtk_main();
   return 0;
}

The style sheet: icon4.css
Code:
#mymenu {   
background-color: white; 
font-weight:bold;
}

.label1 {
color: black;
}

.label1:hover {
color: forestgreen;
}

.label2 {
color: cornflowerblue;
}

.label2:hover {
color: yellow;
}


Top
 Profile  
 
 Post subject: Re: Gtk+ Tutorials & Resources
PostPosted: Thu Jul 19, 2012 2:41 am 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 176
Location: USA
GTK+3 with CSS: GtkProgressBar
Attachment:
progress.png
progress.png [ 115.16 KiB | Viewed 10608 times ]

This is a example of creating and styling a GtkProgressBar with GTK+3 and CSS.
Thanks to Pablo Farias Navarro, (Youtube user thecplusplusguy). The code is his but I've updated it for GTK+3, added CSS, did some slight modifications and made it work without C++ code.
Code:
#include <gtk/gtk.h>
#include <string.h>  /* for CSS */

static float percent = 0.0;
     
static gboolean inc_progress(gpointer data)
{
   percent += 0.05;
   if(percent > 1.01)
      percent = 0.0;
   gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(data), percent);
   char c[3];
   sprintf( c, "%d%%", (int)(percent*100) );
   gtk_progress_bar_set_text(GTK_PROGRESS_BAR(data), c);

   return TRUE;
}
     
int main(int argc, char* argv[])
{
   GtkWidget *window;
   GtkWidget *grid;
   GtkWidget *progress;
/*-- CSS ------------------*/
  GtkCssProvider *provider;
  GdkDisplay *display;
  GdkScreen *screen;
/*---------------------------*/

   gtk_init(&argc, &argv);

   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
   gtk_window_set_has_resize_grip (GTK_WINDOW(window), FALSE);

   progress = gtk_progress_bar_new();
   gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(progress), TRUE) ;

   gtk_widget_set_margin_left (GTK_WIDGET(progress),20);
   gtk_widget_set_margin_right (GTK_WIDGET(progress),20);
   gtk_widget_set_margin_top (GTK_WIDGET(progress),20);
   gtk_widget_set_margin_bottom (GTK_WIDGET(progress),20);
 
   grid = gtk_grid_new();
   gtk_grid_attach (GTK_GRID(grid), progress, 0,0,1,1 );
   
/*---------------- CSS ----------------------------------------------------------------------------------------------------*/
  provider = gtk_css_provider_new ();
  display = gdk_display_get_default ();
  screen = gdk_display_get_default_screen (display);
  gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

  gsize bytes_written, bytes_read;

  const gchar* home = "YourPathHere";   /* for example: "/home/zerohour/gtk/programming/GtkProgressBar/progress1.css" */

  GError *error = 0;

  gtk_css_provider_load_from_path (provider,
                                   g_filename_to_utf8(home, strlen(home), &bytes_read, &bytes_written, &error),
                                   NULL);
  g_object_unref (provider);
/*-------------------------------------------------------------------------------------------------------------------------*/

   g_timeout_add(200, inc_progress, progress);     /*  200 ms  */
     
   gtk_container_add(GTK_CONTAINER(window), grid);
   
   gtk_widget_show_all(window);
   gtk_main();
   return 0;
}

The style sheet: progress1.css
Code:
GtkWindow {
    background-color: aliceblue;
}

GtkProgressBar.progressbar {
background-image: -gtk-gradient (linear,
                                  left top,
                                  left bottom,
                                  color-stop(0.0,rgba(28,82,144,1)),
                                  color-stop(0.50,rgba(37,129,205,1)),
                                  color-stop(0.51,rgba(30,115,190,1)),
                                  color-stop(1.00,rgba(107,176,229,1)));
    border-style: solid;
    border-radius: 3;
    border-width: 0;
    color: white;   /* font color after the progress bar covers the text */

}

GtkProgressBar.trough {
    background-color: grey;
    border-style: solid;
    border-radius: 3;
    border-width: 1;
    border-color: blue;
    color: black;   /* font color before the progress bar covers the text */
}


Top
 Profile  
 
 Post subject: Re: Gtk+ Tutorials & Resources
PostPosted: Fri Oct 19, 2012 9:04 pm 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 176
Location: USA
SIGNALS I
Detecting which button was pushed using labels

Suppose you have two or more buttons and want different actions to take place depending on which button is pressed.
You can write two different callbacks, one for each button but this isn't necessary and it's poor code. Instead you can do this by attaching different labels to your buttons and using those labels to identify which button was pressed in your callback.

This method is shown in the "Guitar tuner" tutorial on the Gnome site but it uses the Anjuta IDE (integrated development enviroment), it's UI designer and Gstreamer. Using a designer is overkill for a interface with just six buttons and I wanted to show a minimal example without Gstreamer or an IDE
http://developer.gnome.org/gnome-devel-demos/stable/guitar-tuner.c.html.en

I've created two buttons in this program and set their labels as "Right" and "Left".
When you create a standalone label using gtk_label_new() you have access to the text used for the label with gtk_label_get_label().
But when you create a button widget with a label using gtk_button_new_with_label() a label widget is created for you and packed into the button widget. The label widget becomes a child of the button widget.

A button is a type GtkBin — A container with just one child. The child in this case is the label widget. To get access to that label widget you can use gtk_bin_get_child() in your callback. Then use gtk_label_get_label() to get the text used for the button's label and g_str_equal() to compare the text returned by gtk_label_get_label(). If the text is "Left" then the left button was pressed and "You pressed the Left button" is printed to the screen. If the text is "Right" then the right button was pressed and "You pressed Right button" is printed.

You could also use: gtk_button_get_label() to get the label directly but this was a good opportunity to introduce gtk_bin_get_child() which works on all type GtkBin. Some GtkBin don't have labels such as GtkOverlay or GtkViewport.

Image

Note for GtkAlignment, the manual says:
Quote:
Note that the desired effect can in most cases be achieved by using the "halign", "valign" and "margin" properties on the child widget, so GtkAlignment should not be used in new code.

Code:
/* COMPILE WITH:
* gcc -Wall -o signals1 `pkg-config --cflags --libs gtk+-3.0` signals1.c
*/

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

/* Function prototype: The "cb" in the name means callback */
static void
cb_button_clicked (GtkButton* button, gpointer user_data);

int main
(int argc, char *argv[])
{
  GtkWidget *window, *grid, *btn_left, *btn_right;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  gtk_window_set_position (GTK_WINDOW(window),
                           GTK_WIN_POS_CENTER);

  gtk_widget_set_size_request (GTK_WIDGET(window),
                               230, 100);

  gtk_window_set_title (GTK_WINDOW(window),
                        "Signals I");

  g_signal_connect (G_OBJECT(window),
                    "delete-event",
                    G_CALLBACK(gtk_main_quit),
                    NULL);

  btn_left = gtk_button_new_with_label ("Left");
  btn_right = gtk_button_new_with_label ("Right");

  gtk_widget_set_size_request (GTK_WIDGET(btn_right),
                               70, 50);
  gtk_widget_set_size_request (GTK_WIDGET(btn_left),
                               70, 50);

  g_signal_connect (GTK_WIDGET(btn_left),
                    "clicked",
                    G_CALLBACK (cb_button_clicked),
                    NULL);

  g_signal_connect (GTK_WIDGET(btn_right),
                    "clicked",
                    G_CALLBACK (cb_button_clicked),
                    NULL);

  grid = gtk_grid_new ();

  gtk_grid_set_column_spacing (GTK_GRID(grid), 30);

  gtk_widget_set_halign (GTK_WIDGET(grid),
                         GTK_ALIGN_CENTER);

  gtk_widget_set_valign (GTK_WIDGET(grid),
                         GTK_ALIGN_CENTER);

  gtk_grid_attach (GTK_GRID(grid),
                   GTK_WIDGET(btn_left),
                   0, 0, 1, 1);

  gtk_grid_attach (GTK_GRID(grid),
                   GTK_WIDGET(btn_right),
                   1, 0, 1, 1);

  gtk_container_add (GTK_CONTAINER(window),
                     GTK_WIDGET(grid));

  gtk_widget_show_all (window);
  gtk_main ();
  return 0;
}

static void
cb_button_clicked (GtkButton* button,
                   gpointer user_data)
{
   GtkWidget* label = gtk_bin_get_child (GTK_BIN(button));
   const gchar* text = gtk_label_get_label (GTK_LABEL(label));

   if (g_str_equal (text, "Left"))
    g_printf ("You pressed the %s button\n", text);
   else if (g_str_equal (text, "Right"))
    g_printf ("You pressed %s button\n", text);
}


Top
 Profile  
 
 Post subject: Re: Gtk+ Tutorials & Resources
PostPosted: Fri Oct 19, 2012 9:21 pm 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 176
Location: USA
SIGNALS II
Detecting which button was pushed without labels

Attachment:
signals2.png
signals2.png [ 6.06 KiB | Viewed 10054 times ]

In the first signals example I showed how to detect which button was pressed by using the labels on the buttons, but what if your buttons don't have any labels? What if they only have images as shown in the image above?
You can use g_object_set_data() from the GObject library to set a unique integer ID for each button. Then use GPOINTER_TO_INT() in your callback to get the ID of the button that was pressed and take the appropriate action.

Code:
/* COMPILE WITH:
* gcc -Wall -o signals2 `pkg-config --cflags --libs gtk+-3.0` signals2.c
*/

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

/* Function prototype */
static void
cb_button_clicked (GtkButton* button,
                   gpointer user_data);

int
main (int argc, char *argv[])
{
  GtkWidget *window, *grid;
  GtkWidget *btn_left, *btn_right, *left_btn_image, *right_btn_image;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  gtk_window_set_position (GTK_WINDOW(window),
                           GTK_WIN_POS_CENTER);

  gtk_widget_set_size_request (GTK_WIDGET(window),
                               230, 100);

  gtk_window_set_title (GTK_WINDOW(window),
                        "Signals II");

  g_signal_connect (G_OBJECT(window),
                    "delete-event",
                    G_CALLBACK (gtk_main_quit),
                    NULL);

/* Just in case you haven't turned on button images in Ubuntu using gconf-editor */
  GtkSettings *default_settings = gtk_settings_get_default ();

  g_object_set (default_settings,
                "gtk-button-images",
                TRUE,
                NULL); 

  left_btn_image  = gtk_image_new_from_stock (GTK_STOCK_GO_BACK,
                                              GTK_ICON_SIZE_BUTTON);

  right_btn_image = gtk_image_new_from_stock (GTK_STOCK_GO_FORWARD,
                                              GTK_ICON_SIZE_BUTTON);

  btn_left = gtk_button_new ();
  btn_right = gtk_button_new ();

  gtk_button_set_image (GTK_BUTTON(btn_left),
                        GTK_WIDGET(left_btn_image));

  gtk_button_set_image (GTK_BUTTON(btn_right),
                        GTK_WIDGET(right_btn_image));

  gtk_widget_set_size_request (GTK_WIDGET(btn_left),
                               70, 50);

  gtk_widget_set_size_request (GTK_WIDGET(btn_right),
                               70, 50);

  gint left = 0, right = 1;

  g_object_set_data (G_OBJECT(btn_left),
                     "btn_number",
                     GINT_TO_POINTER(left));

  g_object_set_data (G_OBJECT(btn_right),
                     "btn_number",
                     GINT_TO_POINTER(right));

  g_signal_connect (GTK_WIDGET(btn_left),
                    "clicked",
                    G_CALLBACK (cb_button_clicked), NULL);

  g_signal_connect (GTK_WIDGET(btn_right),
                    "clicked",
                    G_CALLBACK (cb_button_clicked), NULL);

  grid = gtk_grid_new ();
  gtk_grid_set_column_spacing (GTK_GRID(grid), 30);

  gtk_widget_set_halign (GTK_WIDGET(grid),
                         GTK_ALIGN_CENTER);

  gtk_widget_set_valign (GTK_WIDGET(grid),
                         GTK_ALIGN_CENTER);

  gtk_grid_attach (GTK_GRID(grid),
                   GTK_WIDGET(btn_left),
                   0, 0, 1, 1);

  gtk_grid_attach (GTK_GRID(grid),
                   GTK_WIDGET(btn_right),
                   1, 0, 1, 1);

  gtk_container_add (GTK_CONTAINER(window),
                     GTK_WIDGET(grid));

  gtk_widget_show_all (window);
  gtk_main ();
  return 0;
}

static void
cb_button_clicked (GtkButton* button,
                   gpointer user_data)
{
  gint btn = GPOINTER_TO_INT (g_object_get_data (G_OBJECT(button), "btn_number"));

  if (btn == 0)
    g_printf ("You pressed the left button\n");
  else if (btn == 1)
    g_printf ("You pressed the right button\n");
}

References
https://developer.gnome.org/gobject/stable/gobject-The-Base-Object-Type.html#g-object-set-data
http://developer.gnome.org/glib/stable/glib-Type-Conversion-Macros.html#GPOINTER-TO-INT:CAPS


Top
 Profile  
 
 Post subject: Re: Gtk+ Tutorials & Resources
PostPosted: Mon Nov 12, 2012 6:19 am 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 176
Location: USA
Changing menubar content, by selecting a notebook tab
Attachment:
notemenu.png
notemenu.png [ 16.53 KiB | Viewed 9655 times ]

This example changes the menubar based on the currently selected notebook tab. When the notebook tab called "FirstTab" is selected, the menu that's displayed is "File" When you select the notebook tab called "SecondTab" the "File" menu is replaced with a menu called "Search"

Code:
/*  Compile with:
  gcc -Wall -o notemenu `pkg-config --cflags --libs gtk+-3.0` notemenu.c
*/

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

typedef struct
{
  GtkWidget *main_grid, *notebook;
  GtkWidget *menubar0, *filemenu, *file, *quit,*search;
}WIDGETS;

/*------------ Prototypes --------------------------------*/
static void cb_switch_menu (GtkNotebook *notebook,
                            GtkWidget *page,
                            guint page_num, gpointer data);

static void create_menu0 (gpointer menudata);

static void create_menu1 (gpointer data);
/*--------------------------------------------------------*/

int main(int argc, char *argv[])
{
  WIDGETS *widget = g_slice_new (WIDGETS);
  GtkWidget *window, *grid1, *grid2;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  gtk_window_set_default_size (GTK_WINDOW(window),
                               350, 300);

  g_signal_connect (GTK_WIDGET(window),
                    "destroy",
                    G_CALLBACK (gtk_main_quit),
                    NULL);

/*----------- Notebook -------------------------------------------------*/
  widget->notebook = gtk_notebook_new ();

  gtk_notebook_set_tab_pos (GTK_NOTEBOOK(widget->notebook),
                            GTK_POS_TOP);

  gtk_notebook_set_show_border (GTK_NOTEBOOK(widget->notebook), FALSE);

/*----------- PAGE 1 -------------------------------------------------*/
  grid1 = gtk_grid_new ();

  gtk_container_add (GTK_CONTAINER(widget->notebook),
                     GTK_WIDGET(grid1));

  GtkWidget *first_label = gtk_label_new ("FirstTab");

  gtk_notebook_set_tab_label (GTK_NOTEBOOK(widget->notebook),
                              GTK_WIDGET(grid1),
                              GTK_WIDGET(first_label));
/*
    gtk_widget_set_name (GTK_WIDGET(first_label),
                         "first_tab_label");
*/

/*----------- PAGE 2 -----------------------------------------------------------------------------*/
  grid2 = gtk_grid_new ();

  GtkWidget *second_label = gtk_label_new ("SecondTab");

  gtk_notebook_insert_page (GTK_NOTEBOOK(widget->notebook),
                            GTK_WIDGET(grid2),
                            GTK_WIDGET(second_label),
                            -1);
/*------------------------------------------------------------------------------------------------*/

  widget->main_grid = gtk_grid_new();

  gtk_grid_attach (GTK_GRID(widget->main_grid),
                   GTK_WIDGET(widget->notebook),
                   0, 1, 1, 1);

  gtk_container_add (GTK_CONTAINER(window),
                     GTK_WIDGET(widget->main_grid));

  create_menu0 (widget);

  gtk_widget_show_all (GTK_WIDGET(window));

  g_signal_connect (GTK_WIDGET(widget->notebook),
                    "switch-page",
                    G_CALLBACK (cb_switch_menu),
                    widget);

  gtk_main ();
  g_slice_free (WIDGETS, widget);
  return(0);
}

static void
cb_switch_menu (GtkNotebook *notebook,
                GtkWidget *page, guint page_num,
                gpointer user_data)
{
  WIDGETS *widget = (WIDGETS*) user_data;

  if (page_num == 1)
  {
    g_print ("page_num is %d\n", page_num);

    gtk_widget_destroy (GTK_WIDGET(widget->menubar0));

    create_menu1 (widget);
  }

  if (page_num == 0)
  {   
    g_print ("page_num is %d\n", page_num);

    gtk_widget_destroy (GTK_WIDGET(widget->menubar0));

    create_menu0 (widget);
  }
}
   
static void
create_menu0 (gpointer data)
{
  g_print("create_menu0\n");

  WIDGETS *widget = (WIDGETS*) data;

  widget->menubar0 = gtk_menu_bar_new ();

  gtk_widget_set_hexpand (GTK_WIDGET(widget->menubar0), TRUE);

  widget->filemenu = gtk_menu_new ();
  widget->file = gtk_menu_item_new_with_label ("File");
  widget->quit = gtk_menu_item_new_with_label ("Quit");

  gtk_menu_item_set_submenu (GTK_MENU_ITEM(widget->file),
                             GTK_WIDGET(widget->filemenu));

  gtk_menu_shell_append (GTK_MENU_SHELL(widget->menubar0),
                         GTK_WIDGET(widget->file));

  gtk_menu_shell_append (GTK_MENU_SHELL(widget->filemenu),
                         GTK_WIDGET(widget->quit));

  gtk_grid_attach (GTK_GRID(widget->main_grid),
                   GTK_WIDGET(widget->menubar0),
                   0, 0, 1, 1);

  gtk_widget_show_all (widget->menubar0);
}

static void
create_menu1 (gpointer data)
{
  g_print("create_menu1\n");
  WIDGETS *widget = (WIDGETS*) data;

  widget->menubar0 = gtk_menu_bar_new();

  gtk_widget_set_hexpand (GTK_WIDGET(widget->menubar0), TRUE);

  widget->filemenu = gtk_menu_new ();
  widget->search = gtk_menu_item_new_with_label ("Search");
  widget->quit = gtk_menu_item_new_with_label ("Quit");

  gtk_menu_item_set_submenu (GTK_MENU_ITEM(widget->search),
                             GTK_WIDGET(widget->filemenu));

  gtk_menu_shell_append (GTK_MENU_SHELL(widget->menubar0),
                         GTK_WIDGET(widget->search));

  gtk_menu_shell_append (GTK_MENU_SHELL(widget->filemenu),
                         GTK_WIDGET(widget->quit));

  gtk_grid_attach (GTK_GRID(widget->main_grid),
                   GTK_WIDGET(widget->menubar0),
                   0, 0, 1, 1);

  gtk_widget_show_all (GTK_WIDGET(widget->menubar0));
}


Top
 Profile  
 
 Post subject: Re: Gtk+ Tutorials & Resources
PostPosted: Sun Oct 06, 2013 4:53 am 
Offline
Familiar Face

Joined: Wed Jan 30, 2013 8:28 pm
Posts: 20
I want to know if it is possible to use some css style on a gtkBox, GtkGrid or any container layout it will be very good to set a different background color fir this widget for example

thanks.


Top
 Profile  
 
 Post subject: Re: Gtk+ Tutorials & Resources
PostPosted: Tue Nov 26, 2013 11:43 pm 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 176
Location: USA
If there's no "Style Properties" listed at the top of the GTK manual page, then you can't apply CSS.

As of GTK 3.10 (The current stable version) the containers that have CSS properties are:
GtkButtonBox
GtkPaned
GtkNotebook
GtkExpander

https://developer.gnome.org/gtk3/3.10/LayoutContainers.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 37 posts ]  Go to page Previous  1, 2, 3

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group