GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Nov 28, 2014 2:55 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: RE:gtk_widget_get_preferred_size for a notebook
PostPosted: Sat Jan 12, 2013 1:56 am 
Offline
Familiar Face

Joined: Wed Jan 02, 2013 6:04 pm
Posts: 14
Hi all,

I am trying to set a default size for a notebook opened within a window using the following code. Still the size does'nt seem to change as i intended.
Code:
  GtkWidget *note_book,*page_1,*page_2,*title_1,*title_2;
  note_book=gtk_notebook_new();
  page_1 = gtk_entry_new ();
  title_1 = gtk_label_new ("File_1");
  page_2 = gtk_entry_new ();
  title_2 = gtk_label_new ("File_2");
  gtk_notebook_append_page(GTK_NOTEBOOK(note_book),page_1,title_1);
  gtk_notebook_append_page(GTK_NOTEBOOK(note_book),page_2,title_2);

GtkRequisition *wd_size_1=g_slice_new (GtkRequisition),*wd_size_2=g_slice_new (GtkRequisition);
  wd_size_1->width=100;
  wd_size_1->height=100;
  wd_size_2->width=500;
  wd_size_2->height=500;
  gtk_widget_get_preferred_size(note_book,wd_size_1,wd_size_2);


In addition to above case, i also tried an alternate function,
Code:
gtk_window_set_default_size(GTK_WINDOW(note_book),200,200);

using which, the program compiled without warning, but generated the following comments in the terminal when the program was run.
Code:
(Autoface_2:32428): GLib-GObject-WARNING **: invalid cast from `GtkNotebook' to `GtkWindow'
(Autoface_2:32428): Gtk-CRITICAL **: gtk_window_set_default_size: assertion `GTK_IS_WINDOW (window)' failed


I need some help in understanding the correct usage of the above functions as well as hints on typecasting macros. Thank you.

Best regards,
Jana.K


Top
 Profile  
 
 Post subject: Re: RE:gtk_widget_get_preferred_size for a notebook
PostPosted: Sun Jan 13, 2013 7:11 pm 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 177
Location: USA
You have:
Quote:
GtkWidget *note_book;
note_book = gtk_notebook_new();

Then you try to set the notebook's size using:
Code:
gtk_window_set_default_size(GTK_WINDOW(note_book),200,200);

Your trying to cast a notebook into a window, which isn't possible. The type casting macro GTK_WINDOW() expects a type window. note_book is a GtkNoteBook, not a GtkWindow.

So your trying to create a notebook in a window dynamically?
1) What other widgets are already in the window and what container are you using to hold them?
2) What version of GTK+ are you using?

I'm not familiar with GtkRequisition but I don't think you should be trying to do it this way. You should pack it in a GtkGrid (GTK+3.x) or Gtktable (GTK+2.x) then set the expand properties.

Some good references:
Quote:
You'll notice as you go on that GTK uses a type casting system. This is always done using macros that both test the ability to cast the given item, and perform the cast. Some common ones you will see are
http://www.gtk.org/tutorial1.2/gtk_tut-5.htm

Quote:
1.14
Why are types not registered if I use their GTK_TYPE_BLAH macro ?

The GTK_TYPE_BLAH macros are defined as calls to gtk_blah_get_type(), and the _get_type() i functions are declared as
G_GNUC_CONST which allows the compiler to optimize the call away if it appears that the value is not being used.
http://developer.gnome.org/gtk3/3.0/gtk-question-index.html

The GTK+ Type System:
http://openbooks.sourceforge.net/books/wga/gtk.html#AEN194

--------------------------------------------------
fossies.org:
Line 40:
#define GTK_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WINDOW, GtkWindow))

Line 39:
#define GTK_TYPE_WINDOW (gtk_window_get_type ())

Line 105:
GType gtk_window_get_type (void) G_GNUC_CONST;

http://fossies.org/dox/gtk+-3.7.4/gtkwindow_8h_source.html#l00040

G_GNUC_CONST
http://developer.gnome.org/glib/2.32/glib-Miscellaneous-Macros.html#G-GNUC-CONST:CAPS


Top
 Profile  
 
 Post subject: Re: RE:gtk_widget_get_preferred_size for a notebook
PostPosted: Sun Jan 13, 2013 8:33 pm 
Offline
Familiar Face

Joined: Wed Jan 02, 2013 6:04 pm
Posts: 14
Dear Zerohour,

Thanks for your nice suggestions, nowadays i use only gtkgrid for packing widgets. The program iam writing has a main window which contains a notebook and a menubar. I have pasted my code in the discussion viewtopic.php?f=3&t=178376. In that query, i was sucessful in loading the files that are selected using file->Open menu, in to a textview placed inside a notebook. As soon as the text is displayed, the whole window becomes rigid and becomes very difficult to read through (unscrollable). I wanted to know how the size of textview inside a notebook can be changed and how scrollbar can be attached for a textview in gtk3. I found an example of textview with gtk2, but many of its functions are depreciated not unusable in gtk3.

If i can get the gtk3 version of the below code... it will solve all my problem related to notebook. Thank you,
Code:
#define GTK_ENABLE_BROKEN
#include "config.h"
#include <stdio.h>
#include <gtk/gtk.h>
void text_toggle_editable (GtkWidget *checkbutton,GtkWidget *text)
{ gtk_text_set_editable (GTK_TEXT (text),GTK_TOGGLE_BUTTON (checkbutton)->active);}

void text_toggle_word_wrap (GtkWidget *checkbutton,GtkWidget *text)
{ gtk_text_set_word_wrap (GTK_TEXT (text),GTK_TOGGLE_BUTTON (checkbutton)->active);}

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

int main( int argc,char *argv[] )
{
  GtkWidget *window, *box1, *box2, *hbox, *button, *check, *separator, *table,*vscrollbar, *text;
  GdkColormap *cmap;
  GdkColor color;
  GdkFont *fixed_font;
  FILE *infile;  gtk_init (&argc, &argv);
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_size_request (window, 600, 500);
  gtk_window_set_policy (GTK_WINDOW (window), TRUE, TRUE, FALSE); 
  g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (close_application), NULL);
  gtk_window_set_title (GTK_WINDOW (window), "Text Widget Example");
  gtk_container_set_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_set_border_width (GTK_CONTAINER (box2), 10);
  gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
  gtk_widget_show (box2);
 
  table = gtk_table_new (2, 2, FALSE);
  gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
  gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
  gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
  gtk_widget_show (table);
 
  /* Create the GtkText widget */
  text = gtk_text_new (NULL, NULL);
  gtk_text_set_editable (GTK_TEXT (text), TRUE);
  gtk_table_attach (GTK_TABLE (table), text, 0, 1, 0, 1, GTK_EXPAND | GTK_SHRINK | GTK_FILL,GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
  gtk_widget_show (text);

  /* Add a vertical scrollbar to the GtkText widget */
  vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
  gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1, GTK_FILL, GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
  gtk_widget_show (vscrollbar);

  /* Get the system color map and allocate the color red */
  cmap = gdk_colormap_get_system ();
  color.red = 0xffff;
  color.green = 0;
  color.blue = 0;
  if (!gdk_color_alloc (cmap, &color)) {g_error ("couldn't allocate color"); }

  /* Load a fixed font */
  fixed_font = gdk_font_load ("-misc-fixed-medium-r-*-*-*-140-*-*-*-*-*-*");

  /* Realizing a widget creates a window for it,
   * ready for us to insert some text */
  gtk_widget_realize (text);

  /* Freeze the text widget, ready for multiple updates */
  gtk_text_freeze (GTK_TEXT (text));
 
  /* Insert some colored text */
  gtk_text_insert (GTK_TEXT (text), NULL, &text->style->black, NULL, "Supports ", -1);
  gtk_text_insert (GTK_TEXT (text), NULL, &color, NULL, "colored ", -1);
  gtk_text_insert (GTK_TEXT (text), NULL, &text->style->black, NULL,"text and different ", -1);
  gtk_text_insert (GTK_TEXT (text), fixed_font, &text->style->black, NULL, "fonts\n\n", -1);
 
  /* Load the file text.c into the text window */

  infile = fopen ("text.c", "r");
 
  if (infile) {
    char buffer[1024];
    int nchars;
   
    while (1)
      {
   nchars = fread (buffer, 1, 1024, infile);
   gtk_text_insert (GTK_TEXT (text), fixed_font, NULL, NULL, buffer, nchars);
   if (nchars < 1024)
        break;
      }   
    fclose (infile);
  }

  /* Thaw the text widget, allowing the updates to become visible */ 
  gtk_text_thaw (GTK_TEXT (text));
 
  hbox = gtk_hbutton_box_new ();
  gtk_box_pack_start (GTK_BOX (box2), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

  check = gtk_check_button_new_with_label ("Editable");
  gtk_box_pack_start (GTK_BOX (hbox), check, FALSE, FALSE, 0);
  g_signal_connect (G_OBJECT (check), "toggled", G_CALLBACK (text_toggle_editable), text);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), TRUE);
  gtk_widget_show (check);
  check = gtk_check_button_new_with_label ("Wrap Words");
  gtk_box_pack_start (GTK_BOX (hbox), check, FALSE, TRUE, 0);
  g_signal_connect (G_OBJECT (check), "toggled", G_CALLBACK (text_toggle_word_wrap), text);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), FALSE);
  gtk_widget_show (check);

  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_set_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");
  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (close_application),  NULL);
  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;       
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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