GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sun Oct 26, 2014 7:46 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: GTK+ MVC~esque capabilities?
PostPosted: Mon Feb 27, 2012 8:54 pm 
Offline
Familiar Face

Joined: Mon Feb 27, 2012 8:40 pm
Posts: 6
Hello! Coming from Java and Web app based development I decided to pick up C/C++ About 2 months ago and begin learning whats behind the unix OS and Desktop environments i love so much. I am clearly pleased at what I have found, but I find myself running into the same issue as I always have with Java and other widget toolkits such as ExtJS.

When i first picked up C one of the things I wanted to teach myself first was how to create my own classes, impliment them in their own special files, and incorporate those classes into my main application file modular..ly. Basically, I hate having all of my code in 1 file, and orginization is key, for myself at least, for being able to take a small program and develop it into something large.

One of the (I think maybe the only) ways that other frameworks deal with this is through the use of MVC architecture (Model, View, Controller). Models are basically structs for your data , for example a model named record which kept various data for each 'instance' of the record model. The Controllers listen in the background and is where most of your applications 'logic' goes, as in all of the calculations.. for Example a 'logger' controller which handles logging everything or a 'layoutManager' controller which handles resizing and moving/placing widgets when something changes in the application. The Views are anything you see on the screen, the window is a view, each panel you use to lay things out are views, each menu is a view, and each view, model, and controller gets its own special file (making for a very clean, readable main application file).


I am currently looking not to jump right on into an MVC implimentation of GTK+ as if it even exists I assume learning the basics first as always would be the best way to go, however I do desperately need to find some way to organize my code. I have 1 basic window with a full menu bar (file, edit, view, tools, help), a fool toolbar (about 8 icons), and a statusbar in my first GTK application im doing for practice. This window and all its contents are all defined in 1 main method, in 1 file, and take up about 200 lines. Now, already i feel as though I should be learning how to do this the PROPER way. I do not believe that with how awesome C/C++ is (im loving these pointers :) ) with the ability to access external data via its memory address using pointers or references there has not been an efficient way of orginizing applications standardized using GTK, however my searching through beginner/intermediate GTK tutorials hasnt really shown me anything.

Does anyone have any recommendations for where to start with this? If there is an MVC equivalent method of designing GTK applications that would be spectacular, however if not is there at least a standard way of orginizing ones GTK application that most programmers have adopted, and if so can someone share that with me?

Thank you all!
Aaron


Top
 Profile  
 
 Post subject: Re: GTK+ MVC~esque capabilities?
PostPosted: Mon Feb 27, 2012 10:54 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Splitting up c code into multiple files is quite easy and yes does make the code much neater. You just have to set up header files to describe what functions are in each file, declare external variables and then write a makefile/configure script that will compile and link them all together. It sounds like you are approaching c from an Object Oriented (OO) perspective which it doesn't have and there can be a number of pitfalls for the unwary. You might find c++ a more natural entry. Gtk has bindings for a number of different languages and for c you can even get OO functionality using GObject. The gtk site has a tutorial on making a custom widget using cairo and GObject that goes through all the tedium of how to set up classes.
As to the MVC model, if you had a file for each view then for all but your siple GUIs you would be in for a lot of files... Your Ms and Cs however could be located in different files with no problem at all. With gtk there is a main function for setting up the gui. The signal system listens in the background and then calls functions that you connect to buttons etc. on the display (these callback functions will be your controllers). Personally I prefer to group these into files in terms of functionality (e.g. IO, data analysis, GUI alteration) rather than having individual files for each controller.
Another thing to look into is an app called glade which can speed up your GUI design.
The GNOME site has a few guidelines for coding practices though most of it concerns presentation. For examples in structuring try downloading the source code for a gtk app (e.g. gedit) and have a look at the way they organise files. It can also guide you in setting up the autotools scripts (configure, makefiles, translations and documentation).


Top
 Profile  
 
 Post subject: Re: GTK+ MVC~esque capabilities?
PostPosted: Tue Feb 28, 2012 6:28 pm 
Offline
Familiar Face

Joined: Mon Feb 27, 2012 8:40 pm
Posts: 6
I seem to have found a happy medium. I have been using my own class called gtktools to do things like hide/reveal my statusbar, etc. Now I can use my gtool for anything I want for example constructing a window, etc using some recommendations from here in your answer, and elsewhere, and thought I would share my strategy.


What I figured out was that I can make a kind of tree out of my files. For a basic example say I just want to put a window on the screen with a statusbar and a menubar.

FILE:
windowcomponents.h

struct windowcomponents {
GtkWidget *main_window;
GtkWidget *vbox_layout;
GtkWindow *status_bar;
guint msgid*;
};


FILE:
menucomponents.h

struct menucomponents {
GtkWidget *menu_bar;
GtkWidget *filemenu;
GtkWidget *file;
GtkWidget *quit;
};


FILE:
gtktools.h

#include "windowcomponents.h"
#include "menucomponents.h"

struct gtktools {
void makeAndSizeWindow(windowcomponents *wc);
};


FILE:
gtktools.cc

void gtktools::makeAndSizeWindow(windowcomponents *wc) {
wc->main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(wc->main_window), 400, 400);
gtk_window_set_position(GTK_WINDOW(wc->main_window), GTK_WIN_POS_CENTER);
gtk_widget_show_all(wc->main_window);
}


FILE:
app.cc

#include <gtk/gtk.h>
#include "gtktools.h" //which in turn includes my component structs

gtktools *gtool = new gtktools();

int main(int argc, char *argv) {
windowcomponents *w_comps = g_slice_new(windowcomponents);


gtool->makeAndSizeWindow(w_comps); //Now my 1 gtool function will take care of creating, setting up, sizing, placing, and
//displaying my window and my app file is clean!


gtk_main();

g_slice_free(windowcomponents, w_comps);
delete gtool;
gtool = 0;
return 0;
}




This is probably common knowledge for most of you, and maybe even bad practice for all i know, but I figured I owed it to you all to show what you (and other posts) have helped me figure out! :)


Top
 Profile  
 
 Post subject: Re: GTK+ MVC~esque capabilities?
PostPosted: Wed Feb 29, 2012 9:20 am 
Offline
Never Seen the Sunlight

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

Just to note your code has a number of errors in it. Mainly code related, but you have also missed out the function call to gtk_init() to initialise GTK+. You are also using C++ in an odd way. Try using "class" instead of "struct", use more of the memory management facilities as that can ease your coding and be less error prone. I would also suggest using gtkmm which is a C++ binding for the GTK+ API if you continue to use C++ as they interface very well. It is based on GObject and is very object orientated.

Below is your code rewritten using classes and gtkmm
gtktools.h
Code:
#include <gtkmm.h>

class gtktools : public Gtk::Window {
public:
    gtktools();
    virtual ~gtktools();
};

app.cc
Code:
#include <gtkmm.h>
#include "gtktools.h" //which in turn includes my component structs

int main(int argc, char **argv)
{
    Gtk::Main kit(argc, argv);

    gtktools window;

    kit.run(window);

    return 0;
}

gtktools.cc
Code:
#include "gtktools.h"

gtktools::gtktools()
{
    set_default_size(400, 400);
    set_position(Gtk::WIN_POS_CENTER);
    show_all();
}

gtktools::~gtktools()
{
}


I used the command line
g++ `pkg-config --cflags --libs gtkmm-3.0` app.cc gtktools.cc
to build.

If you have used Java before some of the concepts should be familiar.

The web site for gtkmm is http://www.gtkmm.org/ and the documentation is at http://www.gtkmm.org/en/documentation.html.

There are many examples which show how to use gtkmm, but the main thing to is learn from the examples.

_________________
E.


Top
 Profile  
 
 Post subject: Re: GTK+ MVC~esque capabilities?
PostPosted: Mon Mar 05, 2012 1:26 am 
Offline

Joined: Mon Mar 05, 2012 1:03 am
Posts: 3
Location: California, US
Bakery http://bakery.sourceforge.net/ is an implementation of the MVC pattern; haven't used it yet, but I think its the only one around, and Murry Cumming has a hand in its design, I dont think it gets any better as far as gtkmm expertise.

As a side note, if you're doing any development with gtkmm 3.0 on a debian-based platform there's some kind of issue with gtkmm/application.h; that means you're not going to do anything that uses an application window on Ubuntu, Linux Mint, or afew other distros. I'm not sure but i don't think the red hat fork (including centos) is a problem as its been updated. Bit of a show stopper. Near as I can firgue out they're working on the issue and Its possible patches have already been issued, but not made it into the current build.

I munged up my pkg-config screwing with trying to use gnome 3.0 on my ubuntu lucid machine (you can read all about that adventure here: http://wp.me/2bkNs), so I went for an update, which was about time really, but immediately discovered the problem after trying to build a gtkmm example right after the install.

My observation is based on that experience plus http://permalink.gmane.org/gmane.comp.gnome.svn/563406 and a few other posts I've noticed from the gtkmm team around the web, and the fact that I spent nearly all this afternoon trying to understand why I couldn't build that example.

I breathed a heavy sigh of relief when I was able to at least build other examples, let me tell you.

_________________
"I got two tool stacks and batch of PERL, where its at."


Top
 Profile  
 
 Post subject: Re: GTK+ MVC~esque capabilities?
PostPosted: Mon Mar 05, 2012 7:28 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 765
Location: UK
Hi,

I am not sure that Bakery is being kept up to date and almost certainly will not work out of a distro's box with gtkmm v3.xx with the last release in February 2009.As far as I know it has been removed from the next release of Debian (Wheezy). But many of the features can be found in GTK and Glib as well as the standard libraries.

As for Gtk::Application this is a problem with many distributions and not just Debian based ones as it is very new and will not have filtered down yet.

_________________
E.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group