Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: looking for a _sane_ way to use GTK with C++

  1. #1
    Join Date
    Jan 2007
    Beans
    69

    looking for a _sane_ way to use GTK with C++

    I've been learning SDL with C++ lately, but wanted to give some other graphic libraries a try. So i used GTK (specifically gtkmm).

    take a look at this example code:

    Code:
    #include <gtkmm/main.h>
    #include <iostream>
    
    #include <gtkmm/button.h>
    #include <gtkmm/window.h>
    
    using namespace std;
    
    class HelloWorld : public Gtk::Window
    {
    public:
      HelloWorld();
      virtual ~HelloWorld();
    
    protected:
      //Signal handlers:
      virtual void on_button_clicked();
    
      //Member widgets:
      Gtk::Button m_button;
    };
    
    HelloWorld::HelloWorld()
    : m_button("Hello World")   // creates a new button with label "Hello World".
    {
      // Sets the border width of the window.
      set_border_width(10);
    
      // When the button receives the "clicked" signal, it will call the
      // on_button_clicked() method defined below.
      m_button.signal_clicked().connect(sigc::mem_fun(*this,
                  &HelloWorld::on_button_clicked));
    
      // This packs the button into the Window (a container).
      add(m_button);
    
      // The final step is to display this newly created widget...
      m_button.show();
    }
    
    HelloWorld::~HelloWorld()
    {
    }
    
    void HelloWorld::on_button_clicked()
    {
      cout << "Hello World" << endl;
    }
    
    
    int main (int argc, char *argv[])
    {
      Gtk::Main kit(argc, argv);
    
      HelloWorld helloworld;
      //Shows the window and returns when it is closed.
      Gtk::Main::run(helloworld);
    
      return 0;
    }
    and guess what it does? it draws one button on one window and executes one line of code when the button is clicked. that's all. For 55-60 lines of code!

    Why so complex? seriously, reading the gtkmm manual, half of it is completely confusing. I'm halfway through the Lazyfoo SDL tutorials. I've used SDL_image, mixer, ttf. I have classes to spawn enemies drawn on the screen.

    Seriously, just to draw a button, you have to use advanced classes? Why so much? I hate dealing with huge amounts of code. It should work like this:

    Code:
    #include <gtksomething.h>
    #include <iostream>
    
    int main()
    {
      NewWindow (windowname)
      windowname.TitleText = "Hello World in GTK";
    
      NewButton (buttonname, x, y, w, h)	//x,y, width and height of button
      buttonname.ButtonText = "Hello World";
    
      bool quit=false;
    
      while(quit == false)
      {
        windowname.show();
        buttonname.show();
        if (buttonname.clicked==true)
        {
          cout << "hello world" << endl;
          quit = true;
        }
      }
      return 0;
    }
    20 lines of code. Much better. As I'm still learning C++, I'd like to know what each line of code does, which with SDL, I do. I'm still grasping pointers, etc. Is there a library like the above I can use, or should i fumble about trying to learn GTKmm?

  2. #2
    Join Date
    Apr 2007
    Beans
    14,781

    Re: looking for a _sane_ way to use GTK with C++

    C++ is like that across the board from what I have seen...

    Here is an example in C: http://bo.majewski.name/bluear/gnu/GTK/plain/index.htm

    That C++ example is using classes. It doesn't have to. I am sure you can do it shorter (use the C code for starting)

  3. #3
    Join Date
    Feb 2008
    Beans
    785
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: looking for a _sane_ way to use GTK with C++

    its the same for wxwidgets , and I use event talbes instead of connect ,and make my code a bit longer(!), but I dont care as long as it does what I want to , its the C++ way.
    if it bothers you , try to make a more sensible(?) C++ wrapper for gtk+.

  4. #4
    Join Date
    Apr 2006
    Location
    Vancouver, BC, Canada
    Beans
    146
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: looking for a _sane_ way to use GTK with C++

    Don't take this as an insult, but the problem is just that you don't know what you're doing. The reason examples aren't demonstrated as such in the tutorials is because it's Bad Design(TM). Not because it's incapable.

    PHP Code:
    #include <gtkmm/main.h> 
    #include <gtkmm/window.h>
    #include <gtkmm/button.h>

    int main (int argcchar** argv
    {
        
    Gtk::Main kit (argcargv); 

        
    Gtk::Window window
        
    Gtk::Button button

        
    window.set_title ("huh?"); 
        
    window.set_default_size (20050);
        
    button.set_label ("oh snap.."); 
        
    button.show (); 

        
    window.add (button); 
        
    kit.run (window);

        return 
    0;  

    Quote Originally Posted by Pimientito
    NEVER EVER give a gypsy player an unprotected guitar to play. It will end in tears!

  5. #5
    Join Date
    Feb 2008
    Beans
    785
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: looking for a _sane_ way to use GTK with C++

    I wouldnt call it a bad design... its OOP.
    deriving your own frame (window) class from the base gtk's one , is good , that way you can customize it as you wish.

  6. #6
    Join Date
    Apr 2006
    Location
    Vancouver, BC, Canada
    Beans
    146
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: looking for a _sane_ way to use GTK with C++

    Quote Originally Posted by StOoZ View Post
    I wouldnt call it a bad design... its OOP.
    deriving your own frame (window) class from the base gtk's one , is good , that way you can customize it as you wish.
    No, I agree, the model in my code and his example is what I was refferring to as Bad Design.
    Quote Originally Posted by Pimientito
    NEVER EVER give a gypsy player an unprotected guitar to play. It will end in tears!

  7. #7
    Join Date
    Apr 2005
    Location
    Glasgow, Scotland
    Beans
    1,642

    Re: looking for a _sane_ way to use GTK with C++

    It's interesting to note that the equivalent in Vala is a good bit shorter:

    Code:
    using Gtk;
    
    public class Example : Window
    {
    	construct
    	{
      		set_border_width (10);
    
    		var button = new Button.with_label ("Hello World");
    		button.clicked += (btn) =>
    		{
    			print ("Hello World\n");
    		};
    
    		add (button);
    	}
    
    	static void main (string[] args)
    	{
    		Gtk.init (ref args);
    
    		var example = new Example ();
    		example.show_all ();
    
    		Gtk.main ();
    	}
    }
    A Fedora user

  8. #8
    Join Date
    Aug 2007
    Location
    Novocastria, Australia
    Beans
    751
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: looking for a _sane_ way to use GTK with C++

    You might want to check out libglademm and then use glade to create you interface. You design the interface, export it as an XML document which is then loaded into you C++ programme. You can then just worry about event calls rather than having to put all the widgets in manually.

  9. #9
    Join Date
    Dec 2006
    Location
    Australia
    Beans
    1,097
    Distro
    Xubuntu 15.10 Wily Werewolf

    Re: looking for a _sane_ way to use GTK with C++

    Quote Originally Posted by NovaAesa View Post
    You might want to check out libglademm and then use glade to create you interface. You design the interface, export it as an XML document which is then loaded into you C++ programme. You can then just worry about event calls rather than having to put all the widgets in manually.
    Seconded.

    Seriously, hand-coding a GUI is a pain-in-the-posterior in almost any library and language. GTK+ and its various incarnations are no exceptions to this general observation.

    Glade works fantastically, and it's much simpler to use than hand-coding.
    Last edited by samjh; August 19th, 2008 at 12:00 PM.

  10. #10
    Join Date
    Oct 2005
    Location
    Kansas City
    Beans
    122
    Distro
    Xubuntu 8.04 Hardy Heron

    Re: looking for a _sane_ way to use GTK with C++

    I also really like Glade, although I am somewhat annoyed that they removed the ability to generate C code from version 3. I might sound like a nut for worrying about something so negligible, but I really liked having the C code for the UI to be compiled in for performance reasons.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •