Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-14-2005, 11:08 AM   #1
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
Structs for a Text Editor in GTK/C

Hey everyone. I'm still learning C/GTK and I'm looking through various text editors (gphpedit, gedit, bluefish) trying to understand how they work.

My C skills are still pretty weak too, so help me out here...

A struct is used to hold various information about each "instance" of a file or editor right? Filename, whether it's been modified, maye the type of file, etc. Now would each of these structs also contain a buffer for the actual text or would that be elsewhere?

Then, let's say the main window has a single variable declared as the above struct to hold the "active" file or editor, how does it swtich to another editor? How are they set global and how are they kept track of?

I know this seems like a vague question, but I'm just trying to understand the theory here. If anyone can just give me a quick, block-like explanation of how a text editor handles multiple open files I'd really appreciate it.


Old 05-15-2005, 07:06 PM   #2
LQ Guru
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
I'm sure there are many different ways to keep track of multiple open files. But let me try to explain what a struct is:

A struct is just a chunk of data. It's the primary way in C to define your own kinds of data. If the built-in data types (int, char, float, etc.) are not enough to satisfy your needs, you can make up your own, composed of existing data types. A struct takes up as much memory as its constituent data, so it's best to be prudent with how much data you put into a single struct.

For example:

struct TextFile
  char filename[20];
  char contents[5000];
Here's a structure representing a text file. The filename contents are kept in static arrays. This is a poor approach, because it limits your file size to 5000 characters (and the filename to 20 characters). Not only that, but every single instance of this structure will consume about 5020 bytes, even if the file only contains 10 bytes of text.

Here's a better way:

struct TextFile
  char * filename;
  char * contents;
Here, pointers are used instead of static arrays. to the actual contents of the file. The actual file contents could be very large, and we don't want to try to cram all of it inside the structure itself. IIRC, pointers use 4 bytes on x86, so the above structure would consume about 8 bytes for each instance, which is pretty small. Then, you'd just allocate memory for filename and contents (using malloc() in C, or 'new' in C++) whenever you are ready to store something in them. This way, there's no built-in restriction on how big the file can be, and you're not wasting unneeded bytes for very small files.

So yes, to answer your question, in most sensible text editors, the actual contents are probably stored elsewhere, and just accessed via a pointer.

As for having multiple files open, you could do it one of several ways. Perhaps there's an array of pointers to open files (an array of pointers to TextFile structures), or a linked list, or another structure that keeps track of what files are open and which one is active. Then, when switching between open files, it just looks in the appropriate TextFile structure, and displays the contents stored therein.

I hope this helps clarify things a bit Modern text editors are undoubtedly more complicated than this, so if you're still learning about how they work, you may want to look at the source code for the simplest text editor you can find. Good luck!

Last edited by wapcaplet; 05-15-2005 at 07:08 PM.
Old 05-16-2005, 09:22 AM   #3
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Original Poster
Rep: Reputation: 31
Thank you!

Yeah, I've been looking through source code on a variety of editors. I myself used a singly linked list to store instances of my custom struct which has the filename pointer, a pointer to a GtkTextBuffer, and some boolean flags. This seems to be working okay-- but again, I hardly know what I'm doing

I thank you for you detailed response.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Which light text editor can copy text from file and paste in browser? davidas Linux - Software 9 03-06-2006 12:28 PM
text editor Mig21 Linux - Software 17 07-07-2005 06:27 PM
text editor dauphinfay Linux - General 2 02-29-2004 10:37 PM
Best Text Editor? Ekim Gram Linux - Software 4 05-24-2003 01:36 PM
vi text editor keevitaja Linux - Newbie 4 08-23-2002 04:13 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:56 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration