Thursday, 2 October 2014


how to display huge content in a window in #gtk

below is the image,  i have created a window and placed a button in it.i have placed an 
on_ button1_clicked handler in its signal properties. 

 until now you know how to create this, then i will not paste the code for the button. 


now i have created a window and placed a text view in it. for this i have not used glade UI designer.
you should know how to make it without glade. only for the knowledge purpose.

#text view is a widget that is used when we want to display huge content. it only support text files. we can not display .rtf or any other kind of files.


G_MODULE_EXPORT void on_button1_clicked ()
{
    char *contents;
    GtkWidget *window;
    GtkWidget *scrolledwindow;
    GtkWidget *textview; 



g_file_get_contents("buffer.txt", &contents, NULL, NULL);/*pointer contents will point to the                                                                                         buffer which this api will get from owr text file*/

window = gtk_window_new (GTK_WINDOW_TOPLEVEL); // creating a new window
gtk_window_set_default_size (GTK_WINDOW (window), 400, 300); /*setting default size when                                                                                                                         the window is displayed                                                                                                                              you can resize it.*/
gtk_window_set_title(GTK_WINDOW(window), "text view"); //setting the tittle

scrolledwindow = gtk_scrolled_window_new (NULL, NULL);//making window scroll able
gtk_widget_show (scrolledwindow);

gtk_container_add (GTK_CONTAINER (window), scrolledwindow); // adding scroll to window
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

textview = gtk_text_view_new (); creating a text view
gtk_widget_show (textview);
gtk_container_add (GTK_CONTAINER (scrolledwindow), textview); /*adding it in the window                                                                                                                 you can do same when you want                                                                                                    to add buttons,text box,label and etc...*/

gtk_text_buffer_set_text (gtk_text_view_get_buffer(GTK_TEXT_VIEW (textview)),contents,-1); /*passing the pointer of that buffer to this api. so that this api can set it to                                               text view. instead of making pointer for gtk_text_view_get_buffer() and                                             passing pointer to gtk_text_buffer_set_text() api. i am directly passing it so                                           that what gtk_text_view_get_buffer() will return will be taken                                                                 by  gtk_text_buffer_set_text () directly. this technique i have also used in                                                  previous post*/
gtk_widget_show(window); // at last displaying window
g_free(contents);
}

when you click the button window will appear.below is the image of text view window. 


No comments:

Post a Comment