2012-08-16 16:02:28 +04:00
|
|
|
|
/* Example1.cpp */
|
|
|
|
|
#include <cairo.h>
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
|
|
#define UI_FILE "mainForm.glade"
|
|
|
|
|
|
|
|
|
|
// описание виджетов
|
|
|
|
|
GtkBuilder *builder;
|
|
|
|
|
GtkWidget *topWindow;
|
|
|
|
|
GtkRadioButton *rbutRectangle, *rbutEllipse, *rbutTriangle;
|
|
|
|
|
GtkDrawingArea *drawingarea;
|
|
|
|
|
|
|
|
|
|
// описание обработчиков сигналов
|
2012-08-16 17:08:24 +04:00
|
|
|
|
extern "C" void topWindow_destroy_cb (GtkWidget *object, gpointer user_data);
|
2012-08-16 16:02:28 +04:00
|
|
|
|
extern "C" gboolean drawingarea_draw_event_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data);
|
2012-08-16 17:08:24 +04:00
|
|
|
|
extern "C" void rbutton_toggled_cb (GtkWidget *object);
|
2012-08-16 16:02:28 +04:00
|
|
|
|
|
|
|
|
|
int main( int argc, char **argv )
|
|
|
|
|
{
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
|
|
// инициализация GTK+
|
|
|
|
|
gtk_init( &argc, &argv );
|
|
|
|
|
|
|
|
|
|
// создание нового GtkBuilder объекта
|
|
|
|
|
builder = gtk_builder_new();
|
|
|
|
|
|
|
|
|
|
// загрузка пользовательского интерфеса из файла, который мы создали в Glade
|
|
|
|
|
if( ! gtk_builder_add_from_file( builder, UI_FILE, &error ) )
|
|
|
|
|
{
|
|
|
|
|
g_warning( "%s", error->message );
|
|
|
|
|
g_free( error );
|
|
|
|
|
return( 1 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// связывание наших виджетов с описаннимем виджетов в GladeXML
|
|
|
|
|
topWindow = GTK_WIDGET(gtk_builder_get_object(builder, "topWindow"));
|
|
|
|
|
rbutRectangle = GTK_RADIO_BUTTON(gtk_builder_get_object(builder, "rbutRectangle"));
|
|
|
|
|
rbutEllipse = GTK_RADIO_BUTTON(gtk_builder_get_object(builder, "rbutEllipse"));
|
|
|
|
|
rbutTriangle = GTK_RADIO_BUTTON(gtk_builder_get_object(builder, "rbutTriangle"));
|
|
|
|
|
drawingarea = GTK_DRAWING_AREA(gtk_builder_get_object(builder, "drawingarea"));
|
|
|
|
|
|
|
|
|
|
// связываем сигналы с объектами графического интерфейса
|
|
|
|
|
gtk_builder_connect_signals (builder, NULL);
|
|
|
|
|
|
|
|
|
|
// освобождение памяти
|
|
|
|
|
g_object_unref( G_OBJECT( builder ) );
|
|
|
|
|
|
|
|
|
|
// Показываем форму и виджеты на ней
|
|
|
|
|
gtk_widget_show( topWindow );
|
|
|
|
|
|
|
|
|
|
// запуск главного цикла приложения
|
|
|
|
|
gtk_main();
|
|
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// закрытие приложения
|
2012-08-16 17:08:24 +04:00
|
|
|
|
void topWindow_destroy_cb (GtkWidget *object, gpointer user_data)
|
2012-08-16 16:02:28 +04:00
|
|
|
|
{
|
|
|
|
|
// завершаем главный цикл приложения
|
|
|
|
|
gtk_main_quit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// перерисовка холста
|
|
|
|
|
gboolean drawingarea_draw_event_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
|
|
|
|
|
{
|
|
|
|
|
cairo_t *cr;
|
|
|
|
|
|
2012-08-16 17:08:24 +04:00
|
|
|
|
cr = gdk_cairo_create (gtk_widget_get_window (widget));
|
2012-08-16 16:02:28 +04:00
|
|
|
|
cairo_set_line_width (cr, 7);
|
|
|
|
|
cairo_set_source_rgb (cr, 0, 0, 0);
|
|
|
|
|
|
|
|
|
|
// переключатель установлен на прямоугольник
|
|
|
|
|
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(rbutRectangle)))
|
|
|
|
|
{
|
|
|
|
|
// рисуем прямоугольник
|
|
|
|
|
cairo_rectangle (cr, 20, 20, 200, 100);
|
|
|
|
|
cairo_stroke_preserve(cr);
|
|
|
|
|
cairo_set_source_rgb(cr, 0, 0.8, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// переключатель установлен на эллипс
|
|
|
|
|
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(rbutEllipse)))
|
|
|
|
|
{
|
|
|
|
|
// рисуем эллипс
|
|
|
|
|
cairo_arc(cr, 150, 100, 90, 0, 2 * 3.14);
|
|
|
|
|
cairo_stroke_preserve(cr);
|
|
|
|
|
cairo_set_source_rgb(cr, 0.8, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// переключатель установлен на треугольник
|
|
|
|
|
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(rbutTriangle)))
|
|
|
|
|
{
|
|
|
|
|
// рисуем треугольник
|
|
|
|
|
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
|
|
|
|
|
cairo_move_to (cr, 40, 40);
|
|
|
|
|
cairo_line_to (cr, 200, 40);
|
|
|
|
|
cairo_line_to (cr, 120, 160);
|
|
|
|
|
cairo_line_to (cr, 40, 40);
|
|
|
|
|
cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
|
|
|
|
|
cairo_stroke_preserve(cr);
|
|
|
|
|
cairo_set_source_rgb(cr, 0.8, 0, 0.8);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cairo_fill(cr);
|
|
|
|
|
|
|
|
|
|
cairo_destroy(cr);
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-16 17:08:24 +04:00
|
|
|
|
void rbutton_toggled_cb (GtkWidget *object)
|
2012-08-16 16:02:28 +04:00
|
|
|
|
{
|
|
|
|
|
// перерисовка drawingarea
|
|
|
|
|
gtk_widget_queue_draw (GTK_WIDGET(drawingarea));
|
|
|
|
|
}
|