GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Oct 02, 2014 6:24 am

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: displaying a file in same window?
PostPosted: Sat Jun 08, 2013 7:46 am 
Offline

Joined: Fri Jun 07, 2013 8:53 pm
Posts: 4
Hi , How to display a file in a same window. i wrote the below code ,it is giving undefined reference to "file_open". Can any one please give me the correct code.
Code:
GtkWidget *dialog;
dialog = gtk_file_chooser_dialog_new ("Open File",
                  GTK_WINDOW(window),
                  GTK_FILE_CHOOSER_ACTION_OPEN,
                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                  GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                  NULL);
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
  {
    char *filename;
    filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
    open_file (filename);
    g_free (filename);
  }
gtk_widget_destroy (dialog);


Last edited by errol on Sat Jun 08, 2013 7:53 am, edited 1 time in total.
Set the code block on the source code


Top
 Profile  
 
 Post subject: Re: displaying a file in same window?
PostPosted: Sat Jun 08, 2013 7:56 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 750
Location: UK
Hi and welcome,

The very short snippet of code looks OK for opening a file chooser dialog window. With regard to your error there is no reference to "file_open" here. There is a reference to "open_file()". Have you spelt the name of this function wrong else where in your code?

_________________
E.


Top
 Profile  
 
 Post subject: Re: displaying a file in same window?
PostPosted: Sat Jun 08, 2013 4:51 pm 
Offline

Joined: Fri Jun 07, 2013 8:53 pm
Posts: 4
HI
Thanks for giving Reply. Actually that was undefined reference to open_file only. Can you please tell me how can i display a file in a window.


Top
 Profile  
 
 Post subject: Re: displaying a file in same window?
PostPosted: Sat Jun 08, 2013 5:06 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 750
Location: UK
That is far too much of an open question. First what is the file? Is it a XML, HTML, video, text, excel spread sheet, MIDI, ......... etc. and how would you like it to be displayed? The options are enormous.

_________________
E.


Top
 Profile  
 
 Post subject: Re: displaying a file in same window?
PostPosted: Sat Jun 08, 2013 6:16 pm 
Offline

Joined: Fri Jun 07, 2013 8:53 pm
Posts: 4
Hi
this is a text file and i want to display the conent of the file in same window when i click on a button.


Top
 Profile  
 
 Post subject: Re: displaying a file in same window?
PostPosted: Sat Jun 08, 2013 6:34 pm 
Offline

Joined: Fri Jun 07, 2013 8:53 pm
Posts: 4
actually i created one application which is consists of 3 text boxes and 3 buttons. if i enter any data on text boxes that should be saved to a file and when click on open button, the file having data should be displayed in that same window.Please find the below code and help me.
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<glib.h>
#include<gtk/gtk.h>
#include<gdk/gdk.h>

void file_write(char*,char*,char*);
static GtkWidget *window,*entry1,*entry2,*entry3;
char *text1,*text2,*text3;

static void button_pressed3(GtkWidget *widget,gpointer data)
{
     GtkWidget *dialog;
     
     dialog = gtk_file_chooser_dialog_new ("open file",
                       GTK_WINDOW(window),
                       GTK_FILE_CHOOSER_ACTION_OPEN,
                       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                       GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                       NULL);
     
     if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
       {
         char *filename;
     
         filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
         printf("text_fine name%s\n",filename);
         g_open(filename,"r");
         printf("text_fine name%s\n",filename);
       }
     
     gtk_widget_destroy (dialog);
}

static void button_pressed1(GtkWidget *window,gpointer data)      
{
  text1 = gtk_entry_get_text(GTK_ENTRY(entry1));
  text2 = gtk_entry_get_text(GTK_ENTRY(entry2));
  text3 = gtk_entry_get_text(GTK_ENTRY(entry3));
  printf("%s\n",text1);
  printf("%s\n",text2);
  printf("%s\n",text3);
  if(((strlen(text1))>0 && (strlen(text2)>0) && (strlen(text3)>0)))
  file_write(text1,text2,text3);
  else
  printf("please enter all fileds\n");
}
void file_write( char *text1, char *text2,char *text3)
{
   
   FILE *fp;
  fp =fopen("sam1","a+");
  if(fp==NULL)
  {
  printf("file cannt be open");
  exit(1);
  }
  strcat(text1," ");
  fputs(text1,fp);
  strcat(text2," ");
  fputs(text2,fp);
  strcat(text3,"\n");
  fputs(text3,fp);
  fclose(fp);
}   

int kill_window(GtkWidget *window,GdkEvent *event,gpointer data)
{
   gtk_main_quit();
   return FALSE;
}
int main(int argc,char* argv[])
{
  GtkWidget *button1,*button2,*button3;
  GtkWidget *fixed;
  GtkWidget *vbox;
  GtkWidget *hbox1,*hbox2,*hbox3,*label1,*label2,*label3;
 
  gtk_init(&argc,&argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  button1 = gtk_button_new_with_label("save");
  button2 = gtk_button_new_with_label("close");
  button3 = gtk_button_new_with_label("open");

  hbox1 = gtk_hbox_new(TRUE,2);
  hbox2 = gtk_hbox_new(TRUE,2);
  hbox3 = gtk_hbox_new(TRUE,2);

  entry1 = gtk_entry_new();
  entry2 = gtk_entry_new();
  entry3 = gtk_entry_new();
 
  label1 = gtk_label_new("Name");
  label2 = gtk_label_new("Address");
  label3 = gtk_label_new("Mobileno");

  vbox  = gtk_vbox_new(FALSE,2);
  fixed = gtk_fixed_new();

  gtk_window_set_title(GTK_WINDOW(window),"GTK_APPLICATION");
  gtk_window_set_default_size(GTK_WINDOW(window),300,300);

  gtk_fixed_put(GTK_FIXED(fixed), button1, 100, 150);
  gtk_fixed_put(GTK_FIXED(fixed), button2, 160, 150);
  gtk_fixed_put(GTK_FIXED(fixed), button3, 220, 150);

  gtk_fixed_put(GTK_FIXED(fixed), label1, 0, 60);
  gtk_fixed_put(GTK_FIXED(fixed), label2, 0, 90);
  gtk_fixed_put(GTK_FIXED(fixed), label3, 0, 120);

  gtk_fixed_put(GTK_FIXED(fixed), entry1, 90, 50);
  gtk_fixed_put(GTK_FIXED(fixed), entry2, 90, 80);
  gtk_fixed_put(GTK_FIXED(fixed), entry3, 90, 110);

  g_signal_connect(window,"destroy",G_CALLBACK(kill_window),NULL);

  g_signal_connect(button1,"clicked",G_CALLBACK(button_pressed1),NULL);
  g_signal_connect(button2,"clicked",G_CALLBACK(kill_window),NULL);
  g_signal_connect(button3,"clicked",G_CALLBACK(button_pressed3),NULL);
 
  gtk_box_pack_start(GTK_BOX(hbox1),label1,TRUE,TRUE,2);
  gtk_box_pack_start(GTK_BOX(hbox1),entry1,TRUE,TRUE,2);
  gtk_box_pack_start(GTK_BOX(hbox1),fixed,TRUE,TRUE,2);

  gtk_box_pack_start(GTK_BOX(hbox2),label2,TRUE,TRUE,2);
  gtk_box_pack_start(GTK_BOX(hbox2),entry2,TRUE,TRUE,2);
  gtk_box_pack_start(GTK_BOX(hbox2),fixed,TRUE,TRUE,2);

  gtk_box_pack_start(GTK_BOX(hbox3),label3,TRUE,TRUE,2);
  gtk_box_pack_start(GTK_BOX(hbox3),entry3,TRUE,TRUE,2);
  gtk_box_pack_start(GTK_BOX(hbox3),fixed,TRUE,TRUE,2);

  gtk_box_pack_start(GTK_BOX(vbox),button1,FALSE,FALSE,2);
  gtk_box_pack_start(GTK_BOX(vbox),button2,FALSE,FALSE,2);
  gtk_box_pack_start(GTK_BOX(vbox),button3,FALSE,FALSE,2);

  gtk_box_pack_start(GTK_BOX(vbox),hbox1,FALSE,FALSE,2);
  gtk_box_pack_start(GTK_BOX(vbox),hbox2,FALSE,FALSE,2);
  gtk_box_pack_start(GTK_BOX(vbox),hbox3,FALSE,FALSE,2);

  gtk_container_add(GTK_CONTAINER(window),vbox);

  gtk_widget_show_all(window);
 

  gtk_main();
return 0;
}


Top
 Profile  
 
 Post subject: Re: displaying a file in same window?
PostPosted: Sat Jun 08, 2013 6:39 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 750
Location: UK
This is not of any help to answer your question, there is no consistency with the information you are providing and what information you give is very small.

Your first question shows that you want to use a file chooser and now you are taking about using a button. What is it that you really want to do?

Have you read the advise that is given before posting at http://www.gtkforums.com/viewtopic.php?f=3&t=178057?

Also do not quote my replies, this is a threaded forum and we can read previous posts and please use the code tags for you source code.

_________________
E.


Top
 Profile  
 
 Post subject: Re: displaying a file in same window?
PostPosted: Sat Jun 08, 2013 7:38 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 750
Location: UK
Looking at your code there is no possible way to reliably parse your text file. You are not doing any checking of the data input by the user and you are using a Space character as a delimiter.

For example for the line
John London 0123456789

This could be parsed as
Name - John
Address - London
Mobile Number - 0123456789

If the user entered space for a fuller name and address we could have a line of
John Smith Oxford Street London 0123456789

Then this would be parsed as
Name - John
Address - Smith
Mobile Number - Oxford

Your code compiles with many warnings and when executed there are a number of errors output on the console. You do not say which version of GTK+ you are using which can make a difference to how this needs to be compiled and to what the answer needs to be.

Looking at your code there are a large number of errors. Some of the worst are buffer overflow where you are writing past the end of a memory area. Memory leaks. Incorrect allocation and placement of widgets. Poor error checking and error handling.

Your biggest problem is the format of the file and checking the data entered by the user.

_________________
E.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 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