GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Jul 25, 2014 3:56 pm

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: newbie needing help in GTK+, cairo in C
PostPosted: Tue Jan 07, 2014 9:27 pm 
Offline
Familiar Face

Joined: Tue Jan 07, 2014 1:15 am
Posts: 7
Maybe it's just me, but the things billed as tutorials are too fragmentary and daunting for me to follow well. Moreover, they mostly do a rotten job of showing how Cairo fits into a widget-based GTK app. Because they don't mention widgets at all. I'm not even clear relationship GTK drawing area has to Cairo, if any. So treat me as dumb as you need to.

I've got a sourceforge project, https://sourceforge.net/projects/qubic/ that I inherited around 2002, unfinished. I didn't know anything at all about GTK and I still know very little.
I've been struggling along well enough for some time, just doing drawing. Now I want to put some text on the drawing. No flow, just a word or two, so I think I want cairo.

I've got a GTK drawing area inside a notebook inside a vbox inside a window. I can draw on it. I want to place two words, "orthogonal" and "diagonal" in different places. Sans Serif bold around 20-point would be nice, but I'll take what I can get. Can Cairo do that? How?

If it's not clear from the above, I've not used Cairo at all. This thing started before Cairo became the way to draw, and I've stuck with it. If I can get this done, I'll probably upgrade the drawing too.


Top
 Profile  
 
 Post subject: Re: newbie needing help in GTK+, cairo in C
PostPosted: Wed Jan 08, 2014 9:33 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 733
Location: UK
Hello,

Which version of GTK+ are you aiming your application for? I had a quick look at your source and it appears to be a version of GTK+ 2. Cairo starting being used with version 2.8. Your code uses various deprecated functions left over from the transition from GTK+ v1 to GTK+ v2.0.

Cairo is a drawing library and not a widget library. It is used by GTK+ to draw the widgets and you can also use it to do your own drawing or if you create your own widgets use Cairo to do the drawing.

Yes you can draw basic text with Cairo although if you want much more effect you would use Pango. Looking at your code you are using the GDK library to do your drawing. You can also use this library to draw text and if you intend to stay with GTK v2 for a while then this may be the best way to start while changing the deprecated stuff before moving on to Cairo.

_________________
E.


Top
 Profile  
 
 Post subject: Re: newbie needing help in GTK+, cairo in C
PostPosted: Wed Jan 08, 2014 9:51 pm 
Offline
Familiar Face

Joined: Tue Jan 07, 2014 1:15 am
Posts: 7
errol wrote:
Hello,

Which version of GTK+ are you aiming your application for? I had a quick look at your source and it appears to be a version of GTK+ 2. Cairo starting being used with version 2.8. Your code uses various deprecated functions left over from the transition from GTK+ v1 to GTK+ v2.0.

Cairo is a drawing library and not a widget library. It is used by GTK+ to draw the widgets and you can also use it to do your own drawing or if you create your own widgets use Cairo to do the drawing.

Yes you can draw basic text with Cairo although if you want much more effect you would use Pango. Looking at your code you are using the GDK library to do your drawing. You can also use this library to draw text and if you intend to stay with GTK v2 for a while then this may be the best way to start while changing the deprecated stuff before moving on to Cairo.


Thanks for the reply. That's all probably accurate. I wasn't there at the beginning of the project, and haven't been in contact with the originator for over 10 years.

What you outline is exactly the way I want to go. I just don't know how, and the documentation I've found is just plain daunting. If you're experienced with the new styles it probably all looks obvious, but from where I am, it's mysterious.

I'm on Xubuntu saucy (13.10), using libgtk2.0 (version 2.24.20-1ubuntu1), and my configuration pulls in (amongst other things) libcairo2 (version 1.12.16-0ubuntu2)
As it stands, my drawing is limited to stroking lines and filling rectangles. Nothing scrolls. As you note, it uses a bunch of deprecated calls. I'd be happy to replace them if I knew how, but the most pressing thing for me at the moment is to place a few words as captions on the drawings. I see no need for pango at present, but when I have some integrated help, I'll probably want it.

My intent is to replace the deprecated stuff, get it all working, and then target GTK 3. I find the documentation on the GTK site a good reference for things I'm already using, but pretty confusing for the stuff I want to do. And the tutorials I've found aren't any better. The way my mind works, I need a simple but working example and all I'm finding is fragments.

If there's better beginner stuff out there, I'd love to know it.


Top
 Profile  
 
 Post subject: Re: newbie needing help in GTK+, cairo in C
PostPosted: Thu Jan 09, 2014 7:41 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 733
Location: UK
To get you going so that you can display text in your drawing area and without having to learn all of Cairo right now I would use the GDK function gdk_draw_text() https://developer.gnome.org/gdk2/stable/gdk2-Drawing-Primitives.html#gdk-draw-text even though it is deprecated. This is to just get you going.

Once you have got that working you can then look at the migration guide https://developer.gnome.org/gtk2/stable/migrating.html as a guide to updating your code to the latest standards in GTK v2. This would make it easier to go to v3.

The obvious deprecated functions that can be replaced now are :-
- gtk_exit() - replace with exit()
- gtk_signal_connect - replace with g_signal_connect()

When you want to move onto GTK+ v3 the migration guide at https://developer.gnome.org/gtk3/stable/migrating.html would be of help.

There are a few examples of using a drawing area and Cairo using both GTK v2 and GTK v3

Using GTK v2
- http://www.gtkforums.com/viewtopic.php?p=9086#9086

Using GTK v3
- http://www.gtkforums.com/viewtopic.php?f=3&t=988&p=195286=Drawing+with+Cairo+in+GTK3#p195286
- http://www.gtkforums.com/viewtopic.php?f=3&t=988&p=195381=Drawing+with+Cairo+in+GTK3#p195381

_________________
E.


Top
 Profile  
 
 Post subject: Re: newbie needing help in GTK+, cairo in C
PostPosted: Thu Jan 09, 2014 9:51 am 
Offline
Familiar Face

Joined: Tue Jan 07, 2014 1:15 am
Posts: 7
Thanks.

It looked good until I tried to do it.

errol wrote:
To get you going so that you can display text in your drawing area and without having to learn all of Cairo right now I would use the GDK function gdk_draw_text() https://developer.gnome.org/gdk2/stable/gdk2-Drawing-Primitives.html#gdk-draw-text even though it is deprecated. This is to just get you going.


I had seen that link before. It's a perfect example of what's unhelpful. It leads directly into the swamp of finding fonts in X. I didn't make up the term "swamp", I got it from one of the more helpful links http://utcc.utoronto.ca/~cks/space/blog/unix/XFontTypes provided by a google search. Well, helpful in the sense of confirming just how confusing things are. And I still don't know how to find a sans-serif font on my own machine, let alone in a portable way.

That business of finding a font appears to be the hardest hurdle I have to jump. And I'm still pretty much clueless. I'm not going to use the example names because they appear to be obsolete, proprietary, and missing from my machine. None of the references I could find give any examples of font names one could expect to be generally useful. Maybe because there aren't any. Is there really no way to find a nice 20-point headline font and have it work portably across systems? Aren't there fonts that come with GTK+, or more generally with Gnome? Are they documented anywhere, along with names the code would use to reference them?

I can't even tell if there's some way to find the names of all the reachable fonts. Perhaps some idiom to select them all and list them?

errol wrote:
Once you have got that working you can then look at the migration guide https://developer.gnome.org/gtk2/stable/migrating.html as a guide to updating your code to the latest standards in GTK v2. This would make it easier to go to v3.

The obvious deprecated functions that can be replaced now are :-
- gtk_exit() - replace with exit()
- gtk_signal_connect - replace with g_signal_connect()

When you want to move onto GTK+ v3 the migration guide at https://developer.gnome.org/gtk3/stable/migrating.html would be of help.

There are a few examples of using a drawing area and Cairo using both GTK v2 and GTK v3

Using GTK v2
- http://www.gtkforums.com/viewtopic.php?p=9086#9086

This doesn't use Cairo. It uses Poppler to display a PDF file, which is not even similar to what I want to do.

errol wrote:

Using GTK v3
- http://www.gtkforums.com/viewtopic.php?f=3&t=988&p=195286=Drawing+with+Cairo+in+GTK3#p195286
- http://www.gtkforums.com/viewtopic.php?f=3&t=988&p=195381=Drawing+with+Cairo+in+GTK3#p195381

Although there is a minor error with the two GTK v3 examples as they do not need to allocate another Cairo context as that is provided.


I hope those GTK v3 links are more useful, but I'm not ready for them yet.


Top
 Profile  
 
 Post subject: Re: newbie needing help in GTK+, cairo in C
PostPosted: Thu Jan 09, 2014 7:25 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 733
Location: UK
The way to select fonts with GDK can be found at https://developer.gnome.org/gdk2/stable/gdk2-Fonts.html. It would be best to be fairly general with what font you want to use as the user will almost certainly have a different set of fonts installed from you. Again GDKFont is deprecated so only use this until you have learnt Cairo and Pango

For example to get a 20 point sans serif font you would use this description. You will get a different font from system to system as people do like to have their own preferred installed fonts.

"-*-sans serif-medium-r-normal--*-200-*-*-*-*-iso8859-1"

The GTK v2 example uses Poppler to render a PDF file using Cairo.

More Cairo and GTK v3 examples can be found at - http://zetcode.com/gfx/cairo/ (Note now GTK v3)

_________________
E.


Last edited by errol on Fri Jan 10, 2014 5:46 am, edited 1 time in total.
Zetcode examples are now GTK v3


Top
 Profile  
 
 Post subject: Re: newbie needing help in GTK+, cairo in C
PostPosted: Thu Jan 09, 2014 9:05 pm 
Offline
Familiar Face

Joined: Tue Jan 07, 2014 1:15 am
Posts: 7
errol wrote:
The way to select fonts with GDK can be found at https://developer.gnome.org/gdk2/stable/gdk2-Fonts.html. It would be best to be fairly general with what font you want to use as the user will almost certainly have a different set of fonts installed from you. Again GDKFont is deprecated so only use this until you have learnt Cairo and Pango

For example to get a 20 point sans serif font you would use this description. You will get a different font from system to system as people do like to have their own preferred installed fonts.

"-*-sans serif-medium-r-normal--*-200-*-*-*-*-iso8859-1"

I can see what you're trying to do. Unfortunately, on my system (Xubuntu 13.10), gdk_font_load() returns NULL on this string, so of course nothing works. So let me repeat a question: is there any way to find out what font names are valid on the system a program is running? For example, how do font chooser dialogs do it?
errol wrote:
The GTK v2 example uses Poppler to render a PDF file using Cairo.

More Cairo and GTK v2 examples can be found at - http://zetcode.com/gfx/cairo/

I'll look at these, hoping for enlightenment.


Top
 Profile  
 
 Post subject: Re: newbie needing help in GTK+, cairo in C
PostPosted: Sun Jan 12, 2014 10:45 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 733
Location: UK
If you want to get lists of fonts that are on the system you will need to learn the Pango API.

After some more thought I think that jumping straight into using Cairo might be the best way forward for your application. It will be hard at first as there will be a fair bit to learn though the benefits when doing the transition from GTK v2 to v3 will be worth it.

Cairo does have a "TOY" font interface so it you want more advanced font work you will still need to use Pango. The good thing is that you can select various system default fonts using CSS names such as "serif", "sans-serif", "cursive" etc. and are likely to work as expected.

If you would like help with your GTK section of code give me a PM. I would be able to update your code to use Cairo and more modern GTK techniques.

_________________
E.


Top
 Profile  
 
 Post subject: Re: newbie needing help in GTK+, cairo in C
PostPosted: Sun Jan 12, 2014 7:41 pm 
Offline
Familiar Face

Joined: Tue Jan 07, 2014 1:15 am
Posts: 7
errol wrote:
If you want to get lists of fonts that are on the system you will need to learn the Pango API.

Actually, I'm exploring finding this out by direct X11 coding. I've got something that compiles, but the linker fails. I'm asking for help with this elsewhere. I've already written Pango code that works, but the naming scheme is so different that it will only work with Pango or Cairo calls. Also, by putting more asterisks in the string, I'm at least getting a font of a useable size, so I'm getting something that at least works.
errol wrote:
After some more thought I think that jumping straight into using Cairo might be the best way forward for your application. It will be hard at first as there will be a fair bit to learn though the benefits when doing the transition from GTK v2 to v3 will be worth it.

Cairo does have a "TOY" font interface so it you want more advanced font work you will still need to use Pango. The good thing is that you can select various system default fonts using CSS names such as "serif", "sans-serif", "cursive" etc. and are likely to work as expected.

I've come to the same conclusion, partly because I knew I was going to go that direction anyway. And that's likely enough for my purposes.
errol wrote:
If you would like help with your GTK section of code give me a PM. I would be able to update your code to use Cairo and more modern GTK techniques.

I haven't been active enough here to PM yet, but maybe I will have by the time I'm ready for that help.

Thanks for all the replies.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 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