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. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
05-21-2010, 08:35 AM
|
#1
|
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
|
GTK coding style
I am learning to use GTK+ in C, and as I am making more complex examples a few things are starting to bother me:
The lines of code are VERY long!
There is a huge amount of widgets. How do I name them uniquely in a way that makes sense with the hierarchy of containers without having 30 character long varaibles?
It is best practice for all widgets to have a unique name, or is it a good idea to have some temporary widget variable?
For as much as I know I don't understand how you can manage a small settings dialog, let alone a full GUI app!
Last edited by MTK358; 05-21-2010 at 08:38 AM.
|
|
|
05-21-2010, 08:40 AM
|
#2
|
Member
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684
Rep:
|
I don't deal with GTK very much but in my experience with Qt you'd break individual widgets into their own classes and use pointers to handle the too many variables problem.
Regards,
Alunduil
|
|
|
05-21-2010, 08:46 AM
|
#3
|
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
Original Poster
|
C is not an object-oriented language.
|
|
|
05-21-2010, 08:48 AM
|
#4
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by MTK358
C is not an object-oriented language.
|
Look up gtkmm.
And/or code in Perl which allows to have anonymous subroutines and closures.
|
|
|
05-21-2010, 10:55 AM
|
#5
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379
Rep: 
|
The idea of using a class as mentioned by alunduil is still valid in C. Just think struct rather than class, and remember it's just a container for that dialog. That way you will have a struct for, say a printing dialog box and you can call you cancel button cancel, or btnCancel if you prefer. You can also create a find dialog box again create a struct and the cancel button can be given the same variable name.
|
|
|
05-21-2010, 10:59 AM
|
#6
|
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
Original Poster
|
But don't I still have to include the name of the struct when I want to access the widget?
|
|
|
05-21-2010, 11:56 AM
|
#7
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by MTK358
But don't I still have to include the name of the struct when I want to access the widget?
|
Yes. But C99 has lexical scoping, so names should be unique only in the scope.
|
|
|
05-21-2010, 08:27 PM
|
#8
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379
Rep: 
|
That's the price you pay to make the code maintainable.
To help make the code more readable you can always break your lines up and aligning each variable beneath each other, this is often necessary when passing several widgets to a function. You can then choose an alignment style that suits you aesthetic needs.
Not certain what such liberal usage of whitespace Python would make of that approach, probably awaken the Knights who say Ni from within the interpreter.
|
|
|
All times are GMT -5. The time now is 04:03 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|