tags:

views:

79

answers:

2

I'm starting to make my first C program in awhile using GTK+. I've learned C to some extent and I've worked with PyGTK, so I have a decent understanding of both. But, I've never created GUI program with C. Though it worked, my last GUI program was a bit of a mess because the program logic was all mixed in with the GUI stuff. I've read that it's best to write the GUI and program logic in a decoupled way so that if you switch GUI libraries, it'd be rather painless.

Is this right? Let's say I'm adding an item to a visual list. Do I have a function that adds the item to a logical list and run that in a function that looks at that list and then updates the gui?

For example,

void new_item_button_handler()
{
  add_item_to_array() /* Code dealing with program logic*/
  /*
   * Code here to look at array and update visual list using GUI commands
   */
}

What's the general process for doing this?

Any advice would be appreciated!

EDIT:

Thanks for the advice regarding MVC. Could you perhaps explain that in the context of the example I gave? There's a lot of info on MVC out there, and I'm having a hard time trying to figure out how it applies to my code example here.

+2  A: 

I think you're looking for the Model-View-Controller design pattern.

Matt Kane
+2  A: 

traditionally this is solved with an MVC pattern. however if you are not disciplined, you will see business logic creeping into your view layer.

design your package structure as best you can so that everything fits neatly into a model, view, and controller packages. make sure you have well define interfaces for sharing data between packages. make every effort to design the packages in a way that each individual package will be testable and mostly usable without depending on the other 2.

i would also encourage you to look at the IOC pattern. this will help when connecting all your packages together. it will also help during testing when you need to mock up some stub classes to test 1 package independently of the other 2. IOC is the "wiring" of your application, it lets you mix and match objects.

bostonBob