LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 05-21-2010, 08:35 AM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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.
 
Old 05-21-2010, 08:40 AM   #2
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
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
 
Old 05-21-2010, 08:46 AM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
C is not an object-oriented language.
 
Old 05-21-2010, 08:48 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by MTK358 View Post
C is not an object-oriented language.
Look up gtkmm.

And/or code in Perl which allows to have anonymous subroutines and closures.
 
Old 05-21-2010, 10:55 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 05-21-2010, 10:59 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
But don't I still have to include the name of the struct when I want to access the widget?
 
Old 05-21-2010, 11:56 AM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by MTK358 View Post
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.
 
Old 05-21-2010, 08:27 PM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
  


Reply


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
Preferred coding style? MrCode Programming 26 03-27-2010 02:34 PM
C++ coding style question re: random access iterators spursrule Programming 10 03-03-2008 09:21 PM
gnu style coding...Do u agree? alaios Programming 18 09-01-2005 07:28 AM
Indian Hill C Style and Coding Standands liguorir Programming 0 05-25-2004 09:19 AM
gtk coding... Zerodark Programming 4 08-21-2003 05:49 AM


All times are GMT -5. The time now is 10:15 PM.

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