Commit 8087bb7a authored by Matthias Clasen's avatar Matthias Clasen

Add a new example app

Add a new example to the getting started part of the docs. The focus
of this example is on 'new stuff': GtkApplication, templates, settings,
gmenu, gaction, GtkStack, GtkHeaderBar, GtkSearchBar, GtkRevealer,
GtkListBox, GtkMenuButton, etc.

It is being developed in several steps. Each step is put in a separate
directory below examples/: application1, ..., application8. This is a
little repetitive, but lets us use the code of all examples in the
documentation.
parent 68025551
......@@ -1796,6 +1796,14 @@ demos/gtk-demo/geninclude.pl
demos/pixbuf-demo/Makefile
demos/widget-factory/Makefile
examples/Makefile
examples/application1/Makefile
examples/application2/Makefile
examples/application3/Makefile
examples/application4/Makefile
examples/application5/Makefile
examples/application6/Makefile
examples/application7/Makefile
examples/application8/Makefile
tests/Makefile
tests/visuals/Makefile
testsuite/Makefile
......
......@@ -412,7 +412,14 @@ HTML_IMAGES = \
$(srcdir)/images/up-center.png \
$(srcdir)/images/up-end.png \
$(srcdir)/images/up-start.png \
$(srcdir)/images/list-box.png
$(srcdir)/images/list-box.png \
$(srcdir)/images/getting-started-app1.png \
$(srcdir)/images/getting-started-app2.png \
$(srcdir)/images/getting-started-app3.png \
$(srcdir)/images/getting-started-app4.png \
$(srcdir)/images/getting-started-app6.png \
$(srcdir)/images/getting-started-app7.png \
$(srcdir)/images/getting-started-app8.png
# Extra options to supply to gtkdoc-fixref
FIXXREF_OPTIONS=--extra-dir=../gdk/html \
......
This diff is collapsed.
# these have a handrolled Makefile that gets in the way of distchecking
# so they will not be included in tarballs until they get integrated
# in the new 'getting started' section of the docs
OLD_EXAMPLES = \
arrow \
aspectframe \
base \
buttonbox \
buttons \
calendar \
entry \
eventbox \
fixed \
frame \
gtkdial \
helloworld \
helloworld2 \
label \
menu \
notebook \
packbox \
paned \
progressbar \
radiobuttons \
rangewidgets \
rulers \
scribble-simple \
scribble-xinput \
scrolledwin \
selection \
spinbutton \
statusbar \
table \
tictactoe \
wheelbarrow
SUBDIRS = \
application1 application2 application3 \
application4 application5 application6 \
application7 application8
AM_CPPFLAGS = \
-I$(top_srcdir) \
......
This diff is collapsed.
AM_CPPFLAGS = \
-I$(top_srcdir) \
-I$(top_srcdir)/gdk \
-I$(top_builddir)/gdk \
$(GTK_DEBUG_FLAGS) \
$(GTK_DEP_CFLAGS)
LDADD = \
$(top_builddir)/gtk/libgtk-3.la \
$(top_builddir)/gdk/libgdk-3.la \
$(GTK_DEP_LIBS)
noinst_PROGRAMS = exampleapp
exampleapp_SOURCES = \
main.c \
exampleapp.c exampleapp.h \
exampleappwin.c exampleappwin.h
#include <gtk/gtk.h>
#include "exampleapp.h"
#include "exampleappwin.h"
struct ExampleApp {
GtkApplication parent;
};
struct ExampleAppClass {
GtkApplicationClass parent_class;
};
G_DEFINE_TYPE(ExampleApp, example_app, GTK_TYPE_APPLICATION);
static void
example_app_init (ExampleApp *app)
{
}
static void
example_app_activate (GApplication *app)
{
ExampleAppWindow *win;
win = example_app_window_new (EXAMPLE_APP (app));
gtk_window_present (GTK_WINDOW (win));
}
static void
example_app_open (GApplication *app,
GFile **files,
gint n_files,
const gchar *hint)
{
GList *windows;
ExampleAppWindow *win;
int i;
windows = gtk_application_get_windows (GTK_APPLICATION (app));
if (windows)
win = EXAMPLE_APP_WINDOW (windows->data);
else
win = example_app_window_new (EXAMPLE_APP (app));
for (i = 0; i < n_files; i++)
example_app_window_open (win, files[i]);
gtk_window_present (GTK_WINDOW (win));
}
static void
example_app_class_init (ExampleAppClass *class)
{
G_APPLICATION_CLASS (class)->activate = example_app_activate;
G_APPLICATION_CLASS (class)->open = example_app_open;
}
ExampleApp *
example_app_new (void)
{
return g_object_new (EXAMPLE_APP_TYPE,
"application-id", "org.gtk.exampleapp",
"flags", G_APPLICATION_HANDLES_OPEN,
NULL);
}
#ifndef __EXAMPLEAPP_H
#define __EXAMPLEAPP_H
#include <gtk/gtk.h>
#define EXAMPLE_APP_TYPE (example_app_get_type ())
#define EXAMPLE_APP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EXAMPLE_APP_TYPE, ExampleApp))
typedef struct ExampleApp ExampleApp;
typedef struct ExampleAppClass ExampleAppClass;
GType example_app_get_type (void);
ExampleApp *example_app_new (void);
#endif /* __EXAMPLEAPP_H */
#include "exampleapp.h"
#include "exampleappwin.h"
#include <gtk/gtk.h>
struct ExampleAppWindow {
GtkApplicationWindow parent;
};
struct ExampleAppWindowClass {
GtkApplicationWindowClass parent_class;
};
G_DEFINE_TYPE(ExampleAppWindow, example_app_window, GTK_TYPE_APPLICATION_WINDOW);
static void
example_app_window_init (ExampleAppWindow *app)
{
}
static void
example_app_window_class_init (ExampleAppWindowClass *class)
{
}
ExampleAppWindow *
example_app_window_new (ExampleApp *app)
{
return g_object_new (EXAMPLE_APP_WINDOW_TYPE, "application", app, NULL);
}
void
example_app_window_open (ExampleAppWindow *win,
GFile *file)
{
}
#ifndef __EXAMPLEAPPWIN_H
#define __EXAMPLEAPPWIN_H
#include <gtk/gtk.h>
#include "exampleapp.h"
#define EXAMPLE_APP_WINDOW_TYPE (example_app_window_get_type ())
#define EXAMPLE_APP_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EXAMPLE_APP_WINDOW_TYPE, ExampleAppWindow))
typedef struct ExampleAppWindow ExampleAppWindow;
typedef struct ExampleAppWindowClass ExampleAppWindowClass;
GType example_app_window_get_type (void);
ExampleAppWindow *example_app_window_new (ExampleApp *app);
void example_app_window_open (ExampleAppWindow *win,
GFile *file);
#endif /* __EXAMPLEAPPWIN_H */
#include <gtk/gtk.h>
#include <exampleapp.h>
int
main (int argc, char *argv[])
{
return g_application_run (G_APPLICATION (example_app_new ()), argc, argv);
}
AM_CPPFLAGS = \
-I$(top_srcdir) \
-I$(top_srcdir)/gdk \
-I$(top_builddir)/gdk \
$(GTK_DEBUG_FLAGS) \
$(GTK_DEP_CFLAGS)
LDADD = \
$(top_builddir)/gtk/libgtk-3.la \
$(top_builddir)/gdk/libgdk-3.la \
$(GTK_DEP_LIBS)
noinst_PROGRAMS = exampleapp
exampleapp_SOURCES = \
main.c \
exampleapp.c exampleapp.h \
exampleappwin.c exampleappwin.h \
resources.c
BUILT_SOURCES = resources.c
resources.c: exampleapp.gresource.xml window.ui
$(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) $(srcdir)/exampleapp.gresource.xml \
--target=$@ --sourcedir=$(srcdir) --generate-source
EXTRA_DIST = \
window.ui \
exampleapp.gresource.xml
#include <gtk/gtk.h>
#include "exampleapp.h"
#include "exampleappwin.h"
struct ExampleApp {
GtkApplication parent;
};
struct ExampleAppClass {
GtkApplicationClass parent_class;
};
G_DEFINE_TYPE(ExampleApp, example_app, GTK_TYPE_APPLICATION);
static void
example_app_init (ExampleApp *app)
{
}
static void
example_app_activate (GApplication *app)
{
ExampleAppWindow *win;
win = example_app_window_new (EXAMPLE_APP (app));
gtk_window_present (GTK_WINDOW (win));
}
static void
example_app_open (GApplication *app,
GFile **files,
gint n_files,
const gchar *hint)
{
GList *windows;
ExampleAppWindow *win;
int i;
windows = gtk_application_get_windows (GTK_APPLICATION (app));
if (windows)
win = EXAMPLE_APP_WINDOW (windows->data);
else
win = example_app_window_new (EXAMPLE_APP (app));
for (i = 0; i < n_files; i++)
example_app_window_open (win, files[i]);
gtk_window_present (GTK_WINDOW (win));
}
static void
example_app_class_init (ExampleAppClass *class)
{
G_APPLICATION_CLASS (class)->activate = example_app_activate;
G_APPLICATION_CLASS (class)->open = example_app_open;
}
ExampleApp *
example_app_new (void)
{
return g_object_new (EXAMPLE_APP_TYPE,
"application-id", "org.gtk.exampleapp",
"flags", G_APPLICATION_HANDLES_OPEN,
NULL);
}
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org/gtk/exampleapp">
<file preprocess="xml-stripblanks">window.ui</file>
</gresource>
</gresources>
#ifndef __EXAMPLEAPP_H
#define __EXAMPLEAPP_H
#include <gtk/gtk.h>
#define EXAMPLE_APP_TYPE (example_app_get_type ())
#define EXAMPLE_APP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EXAMPLE_APP_TYPE, ExampleApp))
typedef struct ExampleApp ExampleApp;
typedef struct ExampleAppClass ExampleAppClass;
GType example_app_get_type (void);
ExampleApp *example_app_new (void);
#endif /* __EXAMPLEAPP_H */
#include "exampleapp.h"
#include "exampleappwin.h"
#include <gtk/gtk.h>
struct ExampleAppWindow {
GtkApplicationWindow parent;
};
struct ExampleAppWindowClass {
GtkApplicationWindowClass parent_class;
};
G_DEFINE_TYPE(ExampleAppWindow, example_app_window, GTK_TYPE_APPLICATION_WINDOW);
static void
example_app_window_init (ExampleAppWindow *win)
{
gtk_widget_init_template (GTK_WIDGET (win));
}
static void
example_app_window_class_init (ExampleAppWindowClass *class)
{
gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class),
"/org/gtk/exampleapp/window.ui");
}
ExampleAppWindow *
example_app_window_new (ExampleApp *app)
{
return g_object_new (EXAMPLE_APP_WINDOW_TYPE, "application", app, NULL);
}
void
example_app_window_open (ExampleAppWindow *win,
GFile *file)
{
}
#ifndef __EXAMPLEAPPWIN_H
#define __EXAMPLEAPPWIN_H
#include <gtk/gtk.h>
#include "exampleapp.h"
#define EXAMPLE_APP_WINDOW_TYPE (example_app_window_get_type ())
#define EXAMPLE_APP_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EXAMPLE_APP_WINDOW_TYPE, ExampleAppWindow))
typedef struct ExampleAppWindow ExampleAppWindow;
typedef struct ExampleAppWindowClass ExampleAppWindowClass;
GType example_app_window_get_type (void);
ExampleAppWindow *example_app_window_new (ExampleApp *app);
void example_app_window_open (ExampleAppWindow *win,
GFile *file);
#endif /* __EXAMPLEAPPWIN_H */
#include <gtk/gtk.h>
#include <exampleapp.h>
int
main (int argc, char *argv[])
{
return g_application_run (G_APPLICATION (example_app_new ()), argc, argv);
}
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.8 -->
<template class="ExampleAppWindow" parent="GtkApplicationWindow">
<property name="title" translatable="yes">Example Application</property>
<property name="default-width">600</property>
<property name="default-height">400</property>
<child>
<object class="GtkBox" id="content_box">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkHeaderBar" id="header">
<property name="visible">True</property>
<child type="title">
<object class="GtkStackSwitcher" id="tabs">
<property name="visible">True</property>
<property name="stack">stack</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkStack" id="stack">
<property name="visible">True</property>
</object>
</child>
</object>
</child>
</template>
</interface>
AM_CPPFLAGS = \
-I$(top_srcdir) \
-I$(top_srcdir)/gdk \
-I$(top_builddir)/gdk \
$(GTK_DEBUG_FLAGS) \
$(GTK_DEP_CFLAGS)
LDADD = \
$(top_builddir)/gtk/libgtk-3.la \
$(top_builddir)/gdk/libgdk-3.la \
$(GTK_DEP_LIBS)
noinst_PROGRAMS = exampleapp
exampleapp_SOURCES = \
main.c \
exampleapp.c exampleapp.h \
exampleappwin.c exampleappwin.h \
resources.c
BUILT_SOURCES = resources.c
resources.c: exampleapp.gresource.xml window.ui
$(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) $(srcdir)/exampleapp.gresource.xml \
--target=$@ --sourcedir=$(srcdir) --generate-source
EXTRA_DIST = \
window.ui \
exampleapp.gresource.xml
#include <gtk/gtk.h>
#include "exampleapp.h"
#include "exampleappwin.h"
struct ExampleApp {
GtkApplication parent;
};
struct ExampleAppClass {
GtkApplicationClass parent_class;
};
G_DEFINE_TYPE(ExampleApp, example_app, GTK_TYPE_APPLICATION);
static void
example_app_init (ExampleApp *app)
{
}
static void
example_app_activate (GApplication *app)
{
ExampleAppWindow *win;
win = example_app_window_new (EXAMPLE_APP (app));
gtk_window_present (GTK_WINDOW (win));
}
static void
example_app_open (GApplication *app,
GFile **files,
gint n_files,
const gchar *hint)
{
GList *windows;
ExampleAppWindow *win;
int i;
windows = gtk_application_get_windows (GTK_APPLICATION (app));
if (windows)
win = EXAMPLE_APP_WINDOW (windows->data);
else
win = example_app_window_new (EXAMPLE_APP (app));
for (i = 0; i < n_files; i++)
example_app_window_open (win, files[i]);
gtk_window_present (GTK_WINDOW (win));
}
static void
example_app_class_init (ExampleAppClass *class)
{
G_APPLICATION_CLASS (class)->activate = example_app_activate;
G_APPLICATION_CLASS (class)->open = example_app_open;
}
ExampleApp *
example_app_new (void)
{
return g_object_new (EXAMPLE_APP_TYPE,
"application-id", "org.gtk.exampleapp",
"flags", G_APPLICATION_HANDLES_OPEN,
NULL);
}
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org/gtk/exampleapp">
<file preprocess="xml-stripblanks">window.ui</file>
</gresource>
</gresources>
#ifndef __EXAMPLEAPP_H
#define __EXAMPLEAPP_H
#include <gtk/gtk.h>
#define EXAMPLE_APP_TYPE (example_app_get_type ())
#define EXAMPLE_APP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EXAMPLE_APP_TYPE, ExampleApp))
typedef struct ExampleApp ExampleApp;
typedef struct ExampleAppClass ExampleAppClass;
GType example_app_get_type (void);
ExampleApp *example_app_new (void);
#endif /* __EXAMPLEAPP_H */
#include "exampleapp.h"
#include "exampleappwin.h"
#include <gtk/gtk.h>
struct ExampleAppWindow {
GtkApplicationWindow parent;
};
struct ExampleAppWindowClass {
GtkApplicationWindowClass parent_class;
};
typedef struct ExampleAppWindowPrivate ExampleAppWindowPrivate;
struct ExampleAppWindowPrivate {
GtkWidget *stack;
};
G_DEFINE_TYPE_WITH_PRIVATE(ExampleAppWindow, example_app_window, GTK_TYPE_APPLICATION_WINDOW);
static void
example_app_window_init (ExampleAppWindow *win)
{
gtk_widget_init_template (GTK_WIDGET (win));
}
static void
example_app_window_class_init (ExampleAppWindowClass *class)
{
gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class),
"/org/gtk/exampleapp/window.ui");
gtk_widget_class_bind_child (GTK_WIDGET_CLASS (class), ExampleAppWindowPrivate, stack);
}
ExampleAppWindow *
example_app_window_new (ExampleApp *app)
{
return g_object_new (EXAMPLE_APP_WINDOW_TYPE, "application", app, NULL);
}
void
example_app_window_open (ExampleAppWindow *win,
GFile *file)
{
ExampleAppWindowPrivate *priv = example_app_window_get_instance_private (win);
gchar *basename;
GtkWidget *scrolled, *view;
gchar *contents;
gsize length;
basename = g_file_get_basename (file);
scrolled = gtk_scrolled_window_new (NULL, NULL);
gtk_widget_show (scrolled);
gtk_widget_set_hexpand (scrolled, TRUE);
gtk_widget_set_vexpand (scrolled, TRUE);
view = gtk_text_view_new ();
gtk_text_view_set_editable (GTK_TEXT_VIEW (view), FALSE);
gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (view), FALSE);
gtk_widget_show (view);
gtk_container_add (GTK_CONTAINER (scrolled), view);
gtk_stack_add_titled (GTK_STACK (priv->stack), scrolled, basename, basename);
if (g_file_load_contents (file, NULL, &contents, &length, NULL, NULL)) {
GtkTextBuffer *buffer;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
gtk_text_buffer_set_text (buffer, contents, length);
g_free (contents);
}
g_free (basename);
}
#ifndef __EXAMPLEAPPWIN_H
#define __EXAMPLEAPPWIN_H
#include <gtk/gtk.h>
#include "exampleapp.h"