nomadicME
nomadicME

Reputation: 1409

how to save gtk notebook tabs for use in a later session

I am creating a text editor using gtk+-2.0 gtksourceview-2.0. I want to be able to save the tabs within the notebook for use after I close the editor and reopen it at a later date. I don't see anything in devhelp under gtk_notebook_* that looks promising. I could save the paths to these files in a database table, then read the table from the db and create the tabs on application startup, but that seems a little clunky. Many editors have this functionality built in. For one geany, but I know there are others.

Is this possible in gtk? I'm relatively new to C, are there other ways to store these paths (other than in a database)? Thanks.

Upvotes: 0

Views: 316

Answers (2)

nomadicME
nomadicME

Reputation: 1409

I initially thought that writing to/from a config file would be slow, I had similiar thoughts about writing to/from a db. While there were a couple of good suggestions provided: sqlite and config file parsers, ultimately I decided that writing/reading a few lines to/from a text file the old fashioned way wouldn't be that costly.

I put together a demo program before I incorporate these concepts into my editor, which I have provided below. Feel free to critique this program, especially with regards to memory usage. This program demonstrates the following steps:

(if I make a list it seems to mess with my code block)

1) check to see if the config file exists, 2) remove the config file if it does exist, 3) write the paths to the config file, 4) read the paths from the config file

/*
 * Compile Command:
 * gcc ledit_config.c -o ledit_config 
 * 
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>   // system

#define NUM_TABS 10

char paths[NUM_TABS][200];

void write_config();
void read_config();

int main ()
{
  write_config();
  read_config();
}

void write_config()
{
  char *config_file;
  char temp[200];
  int i=0;

  /* change to the user's home directory (for fopen) */
  (int)chdir(getenv("HOME"));
  config_file = ".config/ledit/files";

  /* populate paths array with random paths */
  strcpy(paths[0], "~/Documents/code/glade/tutorial1/scratch_files/scratch.py");
  strcpy(paths[4], "~/Documents/code/glade/tutorial1/scratch_files/scratch.c");
  strcpy(paths[7], "~/Documents/code/glade/tutorial1/scratch_files/scratch.glade");

  if (fopen(config_file, "r") == NULL) /* file does not exist */
  {
    system("mkdir -p $HOME/.config/ledit/");
    FILE *fp;
    fp=fopen(config_file, "w");

    for(i = 0;i < NUM_TABS;++i)
    {
      strcpy(temp,paths[i]);
      strcat(temp, "\n");
      if (strlen(temp) > strlen("\n")) /* check to see if element actually contains more than just "\n" */
      {
        fprintf(fp, "%s",temp);
      }
    }

    fclose(fp);
  }
  else /* file does exist */
  {
    system("rm $HOME/.config/ledit/files");
    FILE *fp;
    fp=fopen(config_file, "w");

    for(i = 0;i < NUM_TABS;++i)
    {
      strcpy(temp,paths[i]);
      strcat(temp, "\n");
      if (strlen(temp) > strlen("\n")) /* check to see if element actually contains more than just "\n" */
      {
        fprintf(fp, "%s",temp);
      }
    }

    fclose(fp);
  }

}

void read_config()
{
  char line[200];
  char *config_file;
  int i=0;

  /* change to the user's home directory (for fopen) */
  (int)chdir(getenv("HOME"));
  config_file = ".config/ledit/files";

  /* empty the paths array */
  for(i = 0;i < NUM_TABS;++i)
    strcpy(paths[i], "");

  /* read the config file and poplulate array */
  i = 0;
  FILE* fp = fopen(config_file,"r");
  while(fgets(line,sizeof(line),fp) != NULL)
  {
    strcpy(paths[i], line);
    i++;
  }
  fclose(fp);

  /* print out paths array for verification */
  for(i = 0;i < NUM_TABS;++i)
    printf("%s",paths[i]);

} 

Upvotes: 0

anthony
anthony

Reputation: 41098

GtkNotebook can't do this for you, you'll have to write some code to store the state of your application, then load it when the application starts. If that's a list of paths of open files, fine. Not sure why you think that's "clunky"? The toolkit doesn't magically know about the details of your app.

Upvotes: 1

Related Questions