Vinzenz Feenstra’s WebLog

July 4, 2006

GTKmm Tutorial Part 2

by @ 3:48 pm. Filed under Code, Articles, GTKmm, C++, Programming

Hi,

Part two of my german GTKmm Tutorial can be found here

Regards,

Vinzenz



Tags: , , , , ,

GTKmm Tutorial Part 1

by @ 3:47 pm. Filed under Code, Articles, GTKmm, C++

Hi,

I’ve written a german GTKmm Tutorial. It is the first of a series and can be viewed here

If you’re looking for an english tutorial please take a look here

Regards,

Vinzenz :)



Tags: , , , ,

July 1, 2006

C++ Container Clean Help Function

by @ 1:39 am. Filed under Code, C++, Programming

#include <cstdlib>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <set>
namespace container_utils
{
    namespace helper
    {
        template < typename T >
        struct isPointer
        {
            enum { Result = 0 };
            typedef T Type;
            typedef T* Pointer;
        };
 
        template< typename T>
        struct isPointer<T*>
        {
            enum { Result = 1 };
            typedef T Type;
            typedef T* Pointer;
        };
 
        template< class Container ,
        class Type = typename Container::value_type,
            bool Ptr = isPointer<Type>::Result
        >
        struct container_cleanup
        {
            container_cleanup(Container & cont)
            {
                cont.clear(); // erasing all elements
            }
        };
 
        template< class Container ,
        class Type
        >
        struct container_cleanup<Container,Type,true>
        {
            container_cleanup( Container & cont)
            {
                typename Container::iterator it;
                for (it = cont.begin(); it != cont.end(); ++it)
                    delete *it;
                cont.clear(); // erasing all elements
            }
        };
    } // namespace container_utils::helper
 
    /** This is the function will be called by us
    * It will be decided at compile time which clear type will be used
    */
    template < class Container >
        void clean( Container & cont )
    {
        helper::container_cleanup<Container> tmp(cont);
    }
} //namespace container_utils
 
 
struct A
{
    A()
    {
        std::cout << “A Constructed” << std::endl;
    }
    ~A()
    {
        std::cout << “A Destructed “ << std::endl;
    }
};
struct B
{
    static int num;
    int value;
    B()
    {
        std::cout << “B Constructed” << std::endl;
        value = ++num;
    }
    ~B()
    {
        std::cout << “B Destructed “ << std::endl;
    }
    bool operator< ( B const & rhs ) const
    {
        return this->value < rhs.value;
    }
};
int B::num = 0;
 
int main()
{
    // Test mit std::list<>
    std::list<B> a_list;
 
    for(unsigned i = 0; i < 10 ; ++i)
        a_list.push_back(B());
 
    container_utils::clean(a_list);
 
    std::cout << “############################################” << std::endl;
    // Test mit std::set<>
    std::set<B> a_set;
    for(unsigned i = 0; i < 10 ; ++i)
        a_set.insert(B());
 
    container_utils::clean(a_set);
 
    std::cout << “############################################” << std::endl;
    // Test mit std::vector<>
    std::vector<A*> vec_ptr;
    for(unsigned i = 0; i < 10 ; ++i)
        vec_ptr.push_back(new A);
 
    container_utils::clean(vec_ptr);
 
    std::cout << “############################################” << std::endl;
}
 
 

Download this code: container_clean.cpp



C++ Container Choice Image

by @ 12:41 am. Filed under C++, Programming

C++ Container Choice helper

Thanks to http://www.linuxsoftware.co.nz for providing such a helpful image for all C++ programmers :)



June 30, 2006

GTKmm Example Sources

by @ 10:53 pm. Filed under Downloads, Code, GTKmm, C++

The sources are commented in german but I think you guys are abled to understand the code by itself. Please see the GTKmm Section for the english versions.

GTKmm Example1 tar.gz | tar.bz2 | zip
GTKmm Example2 tar.gz | tar.bz2 | zip
GTKmm Example3 tar.gz | tar.bz2 | zip



Tags: , , , ,

GTKmm Example 2

by @ 10:39 pm. Filed under Code, GTKmm, C++

And here is the second example translated into english. :)

#include <gtkmm.h>
struct MyWindow : public Gtk::Window
{
    MyWindow();
    Gtk::Button m_ok_button;
    Gtk::Entry   m_edit;
    Gtk::Label  m_labels[2];
    enum { MW_Label1 , MW_Label2 };
 
    void on_button_ok_clicked();
};
 
int main(int argc, char**argv)
{
    Gtk::Main main_obj(argc,argv);
 
    MyWindow window_obj;
 
    main_obj.run(window_obj);
 
    return EXIT_SUCCESS;
}
 
MyWindow::MyWindow()
: m_ok_button(Gtk::Stock::OK)
{
    set_title(“GTKmm example 2″);
    Gtk::VBox * vbox = new Gtk::VBox;
    add(*Gtk::manage(vbox));
    vbox->set_homogeneous(true);
    vbox->set_spacing( 10 );
    vbox->set_border_width( 10 );
    vbox->pack_start( m_labels[ MW_Label1 ],true,true);
    Gtk::HBox * hbox = new Gtk::HBox;
    vbox->pack_start( *Gtk::manage( hbox ));
    hbox->pack_start( m_labels[ MW_Label2 ] );
    hbox->pack_start( m_edit );
    hbox->set_spacing( 15 );
    vbox->pack_start( m_ok_button );
    m_labels[MW_Label1].set_text(“Enter a text and click ‘OK’”);
    m_labels[MW_Label2].set_text(“Enter text: “);
    m_ok_button.signal_clicked()
	    .connect( sigc::mem_fun(*this,&MyWindow::on_button_ok_clicked));
    show_all();
}
 
void MyWindow::on_button_ok_clicked()
{
    m_labels[MW_Label1].set_text( m_edit.get_text());
}

Download this code: gtkmm_example2.cpp



GTKmm Example 1

by @ 10:37 pm. Filed under Code, GTKmm, C++

I’ve just translated the GTKmm Examples into english and publish them here now ;)

// Needed for each application to initialize GTK+ and GTKmm
#include <gtkmm/Main.h>   
// Needed Buttons
#include <gtkmm/Button.h> 
// Needed for the Window ;)
#include <gtkmm/Window.h> 
 
// Just inherit from Gtk::Window
struct MyWindow : public Gtk::Window
{
    MyWindow();
    Gtk::Button m_quit_button;     // We just want a button
    void on_button_quit_clicked(); // this should be executed on a button click
};
 
MyWindow::MyWindow()
: m_quit_button(“Quit”) // Setting the caption of the button
{
    set_size_request( 200 , 100 ); // Setting the window size
 
    set_title(“Hello World”); // Setting the window caption 
 
    add(m_quit_button);  // add the button to the window
 
    // set the button click signal handler
    m_quit_button.signal_clicked()
	    .connect(sigc::mem_fun(*this,&MyWindow::on_button_quit_clicked));
 
    show_all(); // Shows all widgets 
}
 
// Signal
void MyWindow::on_button_quit_clicked()
{
    hide();// closes the window and quits
}
 
 
int main(int argc, char**argv)
{
    // To guarantee standard GTK+ commandline parameters the 
    // argument variables are passed for the intialization
    Gtk::Main main_obj(argc,argv);
 
    MyWindow window_obj;      // An instance of our inherited window
 
    main_obj.run(window_obj); // starting the application
 
    return EXIT_SUCCESS;      // return 0
}

Download this code: gtkmm_example1.cpp



archives:

January 2009
M T W T F S S
« Mar    
 1234
567891011
12131415161718
19202122232425
262728293031  

internal links:

categories:

Search

other:

Advertisement