Functions in TfeTextView

TfeTextView functions are described in this section.

tfetextview.h

The header file tfetextview.h provides:

Therefore, Any programs use TfeTextView needs to include tfetextview.h.

@@@include tfetextview//tfetextview.h @@@

Instance creation Functions

A TfeTextView instance is created with tfe_text_view_new or tfe_text_view_new_with_file.

GtkWidget *tfe_text_view_new (void);

tfe_text_view_new just creates a new TfeTextView instance and returns the pointer to the new instance.

GtkWidget *tfe_text_view_new_with_file (GFile *file);

tfe_text_view_new_with_file is given a Gfile object as an argument and it loads the file into the GtkTextBuffer instance, then returns the pointer to the new instance. If an error occurs during the creation process, NULL is returned.

Each function is defined as follows.

@@@include tfetextview/tfetextview.c tfe_text_view_new_with_file tfe_text_view_new @@@

Save and saveas functions write the contents in the GtkTextBuffer to a file.

void tfe_text_view_save (TfeTextView *tv)

The function tfe_text_view_save writes the contents in the GtkTextBuffer to a file specified by tv->file. If tv->file is NULL, then it shows GtkFileChooserDialog and prompts the user to choose a file to save. Then it saves the contents to the file and sets tv->file to point the GFile instance for the file.

void tfe_text_view_saveas (TfeTextView *tv)

The function saveas uses GtkFileChooserDialog and prompts the user to select a existed file or specify a new file to save. Then, the function changes tv->file and save the contents to the specified file. If an error occurs, it is shown to the user through the message dialog. The error is managed only in the TfeTextView and no information is notified to the caller.

save_file function

@@@include tfetextview/tfetextview.c save_file @@@

saveas_dialog_response function

@@@include tfetextview/tfetextview.c saveas_dialog_response @@@

tfe_text_view_save function

@@@include tfetextview/tfetextview.c tfe_text_view_save @@@

tfe_text_view_saveas function

@@@include tfetextview/tfetextview.c tfe_text_view_saveas @@@

When you use GtkFileChooserDialog, you need to divide the program into two parts. One is a function which creates GtkFileChooserDialog and the other is a signal handler. The function just creates and shows GtkFileChooserDialog. The rest is done by the handler. It gets Gfile from GtkFileChooserDialog and saves the buffer to the file by calling save_file.

Saveas process

Open function shows GtkFileChooserDialog to a user and prompts them to choose a file. Then it reads the file and puts the text into GtkTextBuffer.

void tfe_text_view_open (TfeTextView *tv, GtkWindow *win);

The parameter win is the top-level window. It will be a transient parent window of GtkFileChooserDialog when the dialog is created.

This function may be called just after tv has been created. In that case, tv has not been incorporated into the widget hierarchy. Therefore it is impossible to get the top-level window from tv. That’s why the function needs win parameter.

This function is usually called when the buffer of tv is empty. However, even if the buffer is not empty, tfe_text_view_open doesn’t treat it as an error. If you want to revert the buffer, calling this function is appropriate.

Open and read process is divided into two phases. One is showing GtkFileChooserDialog and the other is its response handler. The response handler gets the filename, reads the contents of the file and puts it into the GtkTextBuffer.

open_dialog_response function

@@@include tfetextview/tfetextview.c open_dialog_response @@@

tfe_text_view_open function

@@@include tfetextview/tfetextview.c tfe_text_view_open @@@

The whole process between the caller and TfeTextView is shown in the following diagram. It is really complicated. Because signal is the only way for GtkFileChooserDialog to communicate with others.

Caller and TfeTextView
  1. A caller gets a pointer tv to a TfeTextView instance by calling tfe_text_view_new.
  2. The caller connects the handler (left bottom in the diagram) and the signal “open-response”.
  3. It calls tfe_text_view_open to prompt the user to select a file from GtkFileChooserDialog.
  4. The dialog emits a signal and it invokes the handler open_dialog_response.
  5. The handler reads the file and inserts the text into GtkTextBuffer and emits a signal to inform the status as a response code.
  6. The handler out of the TfeTextView receives the signal.

Getting GFile in TfeTextView

You can get the GFile in a TfeTextView instance with tfe_text_view_get_file. It is very simple.

@@@include tfetextview/tfetextview.c tfe_text_view_get_file @@@

The important thing is to duplicate tv->file. Otherwise, if the caller frees the GFile object, tv->file is no more guaranteed to point the GFile. Another reason to use g_file_dup is that GFile isn’t thread-safe. If you use GFile in the different thread, the duplication is necessary. See Gio API Reference – g_file_dup.

The API document and source file of tfetextview.c

Refer API document of TfeTextView. The markdown file is under the directory src/tfetextview.

You can find all the TfeTextView source codes under src/tfetextview directories.