Skip to content

Instantly share code, notes, and snippets.

@micromaomao
Created July 8, 2015 10:31
Show Gist options
  • Save micromaomao/7a5649b8ba245df27f11 to your computer and use it in GitHub Desktop.
Save micromaomao/7a5649b8ba245df27f11 to your computer and use it in GitHub Desktop.
This is my first GUI program in linux!
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkGrid" id="grid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="sayhello">
<property name="label" translatable="yes">Say hello!</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="output">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="editable">False</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="hh">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">24</property>
<property name="margin_right">24</property>
<property name="margin_top">12</property>
<property name="margin_bottom">12</property>
<property name="label" translatable="yes">Hi! this is my first GUI program in linux!</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="name">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="text" translatable="yes">Wtm</property>
<property name="input_hints">GTK_INPUT_HINT_SPELLCHECK | GTK_INPUT_HINT_WORD_COMPLETION | GTK_INPUT_HINT_NONE</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
<object class="GtkTextBuffer" id="textbuffer1"/>
</interface>
#include <gtk/gtk.h>
#include <string>
using namespace std;
GtkWidget *box, *tep;
void say (GtkWidget* app, gpointer user_data)
{
string sr = string(gtk_entry_get_text(GTK_ENTRY(box)));
if(sr.size() == 0){
sr = string("Hello! Input your name below then click the button.");
}else{
sr += string(", hello!");
}
gtk_entry_set_text(GTK_ENTRY(tep), (const gchar*)sr.c_str());
};
void activate (GtkApplication* app, gpointer user_data)
{
GtkWidget *window;
window = gtk_application_window_new(app);
GtkBuilder * gtkb = gtk_builder_new_from_file("hello.glade");
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(gtk_builder_get_object(gtkb, "grid")));
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);
gtk_window_set_resizable(GTK_WINDOW(window), false);
gtk_widget_show_all(window);
box = GTK_WIDGET(gtk_builder_get_object(gtkb, "name"));
tep = GTK_WIDGET(gtk_builder_get_object(gtkb, "output"));
GObject *sayhello = gtk_builder_get_object(gtkb, "sayhello");
g_signal_connect(sayhello, "clicked", G_CALLBACK(say), NULL);
};
int main (int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new("org.websint.mao.hellogtk", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
};
everything:
rm -f a.out
g++ main.cc `pkg-config --cflags --libs gtk+-3.0`
./a.out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment