glade_habr added

This commit is contained in:
Kolan Sh 2012-08-16 16:02:28 +04:00
parent 85bdaae967
commit 7c8dd9bc77
4 changed files with 490 additions and 0 deletions

117
c/glade_habr/Example1.cpp Normal file
View File

@ -0,0 +1,117 @@
/* Example1.cpp */
#include <cairo.h>
#include <gtk/gtk.h>
#define UI_FILE "mainForm.glade"
// описание виджетов
GtkBuilder *builder;
GtkWidget *topWindow;
GtkRadioButton *rbutRectangle, *rbutEllipse, *rbutTriangle;
GtkDrawingArea *drawingarea;
// описание обработчиков сигналов
extern "C" void topWindow_destroy_cb (GtkObject *object, gpointer user_data);
extern "C" gboolean drawingarea_draw_event_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data);
extern "C" void rbutton_toggled_cb (GtkObject *object);
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 );
}
// закрытие приложения
void topWindow_destroy_cb (GtkObject *object, gpointer user_data)
{
// завершаем главный цикл приложения
gtk_main_quit();
}
// перерисовка холста
gboolean drawingarea_draw_event_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
cairo_t *cr;
cr = gdk_cairo_create (widget->window);
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;
}
void rbutton_toggled_cb (GtkObject *object)
{
// перерисовка drawingarea
gtk_widget_queue_draw (GTK_WIDGET(drawingarea));
}

13
c/glade_habr/Makefile Normal file
View File

@ -0,0 +1,13 @@
CC=g++
LDLIBS=`pkg-config --libs gtk+-3.0 gmodule-2.0`
CFLAGS=-Wall -g `pkg-config --cflags gtk+-2.0 gmodule-2.0`
Example1: Example1.o
$(CC) $(LDLIBS) Example1.o -o Example1
Example1.o: Example1.cpp
$(CC) $(CFLAGS) -c Example1.cpp
clean:
rm -f Example1
rm -f *.o

1
c/glade_habr/README Normal file
View File

@ -0,0 +1 @@
http://habrahabr.ru/post/107403/

359
c/glade_habr/mainForm.glade Normal file
View File

@ -0,0 +1,359 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<object class="GtkWindow" id="topWindow">
<property name="can_focus">False</property>
<property name="default_width">500</property>
<property name="default_height">300</property>
<signal name="destroy" handler="topWindow_destroy_cb" swapped="no"/>
<child>
<object class="GtkBox" id="vbox1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkMenuBar" id="menubar1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuItem" id="menuitem1">
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Файл</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="menu1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem1">
<property name="label">gtk-new</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem2">
<property name="label">gtk-open</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem3">
<property name="label">gtk-save</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem4">
<property name="label">gtk-save-as</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkSeparatorMenuItem" id="separatormenuitem1">
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem5">
<property name="label">gtk-quit</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
<signal name="activate" handler="topWindow_destroy_cb" swapped="no"/>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem2">
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Правка</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="menu2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem6">
<property name="label">gtk-cut</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem7">
<property name="label">gtk-copy</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem8">
<property name="label">gtk-paste</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem9">
<property name="label">gtk-delete</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem3">
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Вид</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem4">
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Справка</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="menu3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem10">
<property name="label">gtk-about</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frame1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label_xalign">0</property>
<property name="shadow_type">none</property>
<child>
<object class="GtkPaned" id="hpaned1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<child>
<object class="GtkScrolledWindow" id="scrolledwindow1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<property name="min_content_width">145</property>
<property name="min_content_height">145</property>
<child>
<object class="GtkViewport" id="viewport1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkRadioButton" id="rbutRectangle">
<property name="label" translatable="yes">radiobutton</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_action_appearance">False</property>
<property name="xalign">0</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
<signal name="toggled" handler="rbutton_toggled_cb" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="rbutEllipse">
<property name="label" translatable="yes">radiobutton</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_action_appearance">False</property>
<property name="xalign">0</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
<property name="group">rbutRectangle</property>
<signal name="toggled" handler="rbutton_toggled_cb" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="rbutTriangle">
<property name="label" translatable="yes">radiobutton</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_action_appearance">False</property>
<property name="xalign">0</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
<property name="group">rbutRectangle</property>
<signal name="toggled" handler="rbutton_toggled_cb" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="resize">False</property>
<property name="shrink">True</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow" id="scrolledwindow2">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkViewport" id="viewport2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkBox" id="box2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Холст размером 300 x 200</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkDrawingArea" id="drawingarea">
<property name="width_request">300</property>
<property name="height_request">200</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="events">GDK_EXPOSURE_MASK | GDK_STRUCTURE_MASK</property>
<property name="halign">start</property>
<signal name="draw" handler="drawingarea_draw_event_cp" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="resize">True</property>
<property name="shrink">True</property>
</packing>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkStatusbar" id="statusbar1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
</interface>