gtkmm – example 6

This is part of a more general work dedicated to gtkmm accessible here. This example is the uncatchable window. It shows how to move the window, to hide the window title bar and to get the screen size.

capture

Download

Source code


main.cpp

#include 
#include 


int main(int argc, char* argv[])
{
	// Initialize gtkmm
	Gtk::Main app(argc, argv);
	// Create the window
	mainwindow w;
	// Start main loop
	Gtk::Main::run(w);
	return 0;
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 


// The class mainwindow inherits from Gtk::Window
class mainwindow : public Gtk::Window
{
	// Constructor and destructor
public:    
	mainwindow();
	virtual             ~mainwindow();

protected:
	//Signal handlers (run when the button are clicked)
	void                  on_button();

	//Member widgets:
	Gtk::Button           buttonQuit;
	Gtk::Grid             mainGrid;
};


#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include 


// Constructor of the main Window (build ui interface).
mainwindow::mainwindow()
{
	// Initialize the main window and hide the title bar
	this->set_border_width(0);
	this->set_decorated(false);

	// Add the Quit button
	buttonQuit.add_label("Quit");
	buttonQuit.set_size_request(200,100);
	buttonQuit.signal_enter().connect(sigc::mem_fun(*this,&mainwindow::on_button));
	buttonQuit.signal_pressed().connect(sigc::mem_fun(*this,&mainwindow::close));
	mainGrid.attach(buttonQuit,0,1,3,1);

	// Display the main grid in the main window
	mainGrid.set_column_spacing(10);
	mainGrid.set_row_spacing(10);
	mainGrid.show_all();

	// Insert the grid in the main window
	add(mainGrid);
}


// Destructor of the class
mainwindow::~mainwindow()
{}


// Call when the button is clicked
void mainwindow::on_button()
{
	// Get screen dimensions
	Glib::RefPtr screen = Gdk::Screen::get_default();
	// Move window at a new random position
	this->move(rand()%screen->get_width(),rand()%screen->get_height());
}

One thought on “gtkmm – example 6

Leave a Reply

Your email address will not be published. Required fields are marked *