2019-10-12 02:04:30 +00:00
|
|
|
/* Benchmark/Scrolling
|
2020-09-13 18:59:33 +00:00
|
|
|
* #Keywords: GtkScrolledWindow
|
2019-10-11 21:35:58 +00:00
|
|
|
*
|
2019-10-12 02:04:30 +00:00
|
|
|
* This demo scrolls a view with various content.
|
2019-10-11 21:35:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
static guint tick_cb;
|
2019-10-23 01:20:36 +00:00
|
|
|
static GtkAdjustment *hadjustment;
|
|
|
|
static GtkAdjustment *vadjustment;
|
2019-10-12 02:04:30 +00:00
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
static GtkWidget *scrolledwindow;
|
|
|
|
static int selected;
|
|
|
|
|
2020-11-10 01:07:09 +00:00
|
|
|
#define N_WIDGET_TYPES 7
|
2019-10-12 02:04:30 +00:00
|
|
|
|
|
|
|
|
2019-10-23 01:20:36 +00:00
|
|
|
static int hincrement = 5;
|
|
|
|
static int vincrement = 5;
|
2019-10-11 21:35:58 +00:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
scroll_cb (GtkWidget *widget,
|
|
|
|
GdkFrameClock *frame_clock,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
double value;
|
|
|
|
|
2019-10-23 01:20:36 +00:00
|
|
|
value = gtk_adjustment_get_value (vadjustment);
|
|
|
|
if (value + vincrement <= gtk_adjustment_get_lower (vadjustment) ||
|
|
|
|
(value + vincrement >= gtk_adjustment_get_upper (vadjustment) - gtk_adjustment_get_page_size (vadjustment)))
|
|
|
|
vincrement = - vincrement;
|
2019-10-11 21:35:58 +00:00
|
|
|
|
2019-10-23 01:20:36 +00:00
|
|
|
gtk_adjustment_set_value (vadjustment, value + vincrement);
|
|
|
|
|
|
|
|
value = gtk_adjustment_get_value (hadjustment);
|
|
|
|
if (value + hincrement <= gtk_adjustment_get_lower (hadjustment) ||
|
|
|
|
(value + hincrement >= gtk_adjustment_get_upper (hadjustment) - gtk_adjustment_get_page_size (hadjustment)))
|
|
|
|
hincrement = - hincrement;
|
|
|
|
|
|
|
|
gtk_adjustment_set_value (hadjustment, value + hincrement);
|
2019-10-11 21:35:58 +00:00
|
|
|
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern GtkWidget *create_icon (void);
|
|
|
|
|
|
|
|
static void
|
2019-10-12 02:04:30 +00:00
|
|
|
populate_icons (void)
|
2019-10-11 21:35:58 +00:00
|
|
|
{
|
2019-10-12 02:04:30 +00:00
|
|
|
GtkWidget *grid;
|
2019-10-11 21:35:58 +00:00
|
|
|
int top, left;
|
|
|
|
|
2019-10-12 02:04:30 +00:00
|
|
|
grid = gtk_grid_new ();
|
|
|
|
gtk_widget_set_halign (grid, GTK_ALIGN_CENTER);
|
2020-02-26 01:57:20 +00:00
|
|
|
gtk_widget_set_margin_start (grid, 10);
|
|
|
|
gtk_widget_set_margin_end (grid, 10);
|
|
|
|
gtk_widget_set_margin_top (grid, 10);
|
|
|
|
gtk_widget_set_margin_bottom (grid, 10);
|
2019-10-12 02:04:30 +00:00
|
|
|
gtk_grid_set_row_spacing (GTK_GRID (grid), 10);
|
|
|
|
gtk_grid_set_column_spacing (GTK_GRID (grid), 10);
|
|
|
|
|
2019-10-11 21:35:58 +00:00
|
|
|
for (top = 0; top < 100; top++)
|
|
|
|
for (left = 0; left < 15; left++)
|
|
|
|
gtk_grid_attach (GTK_GRID (grid), create_icon (), left, top, 1, 1);
|
2019-10-12 02:04:30 +00:00
|
|
|
|
2019-10-23 01:20:36 +00:00
|
|
|
hincrement = 0;
|
2019-12-22 17:27:30 +00:00
|
|
|
vincrement = 5;
|
2019-10-23 01:20:36 +00:00
|
|
|
|
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
|
2020-05-02 04:51:20 +00:00
|
|
|
GTK_POLICY_NEVER,
|
|
|
|
GTK_POLICY_AUTOMATIC);
|
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolledwindow), grid);
|
2019-10-12 02:04:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *content;
|
|
|
|
static gsize content_len;
|
|
|
|
|
2020-08-08 18:38:18 +00:00
|
|
|
extern void fontify (const char *format, GtkTextBuffer *buffer);
|
2019-10-12 02:04:30 +00:00
|
|
|
|
|
|
|
static void
|
2020-08-21 12:41:13 +00:00
|
|
|
populate_text (gboolean highlight)
|
2019-10-12 02:04:30 +00:00
|
|
|
{
|
|
|
|
GtkWidget *textview;
|
|
|
|
GtkTextBuffer *buffer;
|
|
|
|
|
|
|
|
if (!content)
|
|
|
|
{
|
|
|
|
GBytes *bytes;
|
|
|
|
|
|
|
|
bytes = g_resources_lookup_data ("/sources/font_features.c", 0, NULL);
|
|
|
|
content = g_bytes_unref_to_data (bytes, &content_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = gtk_text_buffer_new (NULL);
|
|
|
|
gtk_text_buffer_set_text (buffer, content, (int)content_len);
|
|
|
|
|
2020-08-21 12:41:13 +00:00
|
|
|
if (highlight)
|
2020-08-08 18:38:18 +00:00
|
|
|
fontify ("c", buffer);
|
2019-10-12 02:04:30 +00:00
|
|
|
|
|
|
|
textview = gtk_text_view_new ();
|
|
|
|
gtk_text_view_set_buffer (GTK_TEXT_VIEW (textview), buffer);
|
|
|
|
|
2019-10-23 01:20:36 +00:00
|
|
|
hincrement = 0;
|
2019-12-22 17:27:30 +00:00
|
|
|
vincrement = 5;
|
2019-10-23 01:20:36 +00:00
|
|
|
|
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
|
2020-05-02 04:51:20 +00:00
|
|
|
GTK_POLICY_NEVER,
|
|
|
|
GTK_POLICY_AUTOMATIC);
|
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolledwindow), textview);
|
2019-10-12 02:04:30 +00:00
|
|
|
}
|
|
|
|
|
2019-10-23 01:20:36 +00:00
|
|
|
static void
|
|
|
|
populate_image (void)
|
|
|
|
{
|
|
|
|
GtkWidget *image;
|
|
|
|
|
|
|
|
if (!content)
|
|
|
|
{
|
|
|
|
GBytes *bytes;
|
|
|
|
|
|
|
|
bytes = g_resources_lookup_data ("/sources/font_features.c", 0, NULL);
|
|
|
|
content = g_bytes_unref_to_data (bytes, &content_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
image = gtk_picture_new_for_resource ("/sliding_puzzle/portland-rose.jpg");
|
|
|
|
gtk_picture_set_can_shrink (GTK_PICTURE (image), FALSE);
|
|
|
|
|
|
|
|
hincrement = 5;
|
2019-12-22 17:27:30 +00:00
|
|
|
vincrement = 5;
|
2019-10-23 01:20:36 +00:00
|
|
|
|
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
|
2020-05-02 04:51:20 +00:00
|
|
|
GTK_POLICY_AUTOMATIC,
|
|
|
|
GTK_POLICY_AUTOMATIC);
|
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolledwindow), image);
|
2019-10-23 01:20:36 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:27:30 +00:00
|
|
|
extern GtkWidget *create_weather_view (void);
|
|
|
|
|
|
|
|
static void
|
|
|
|
populate_list (void)
|
|
|
|
{
|
|
|
|
GtkWidget *list;
|
|
|
|
|
|
|
|
list = create_weather_view ();
|
|
|
|
|
|
|
|
hincrement = 5;
|
|
|
|
vincrement = 0;
|
|
|
|
|
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
|
|
|
|
GTK_POLICY_AUTOMATIC,
|
|
|
|
GTK_POLICY_AUTOMATIC);
|
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolledwindow), list);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern GtkWidget *create_color_grid (void);
|
2020-08-08 18:36:16 +00:00
|
|
|
extern GListModel *gtk_color_list_new (guint size);
|
2019-12-22 17:27:30 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
populate_grid (void)
|
|
|
|
{
|
|
|
|
GtkWidget *list;
|
2020-08-08 18:36:16 +00:00
|
|
|
GtkNoSelection *selection;
|
2019-12-22 17:27:30 +00:00
|
|
|
|
|
|
|
list = create_color_grid ();
|
|
|
|
|
2020-08-08 18:36:16 +00:00
|
|
|
selection = gtk_no_selection_new (gtk_color_list_new (2097152));
|
2020-08-31 21:06:48 +00:00
|
|
|
gtk_grid_view_set_model (GTK_GRID_VIEW (list), GTK_SELECTION_MODEL (selection));
|
2020-08-08 18:36:16 +00:00
|
|
|
g_object_unref (selection);
|
|
|
|
|
2019-12-22 17:27:30 +00:00
|
|
|
hincrement = 0;
|
|
|
|
vincrement = 5;
|
|
|
|
|
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
|
|
|
|
GTK_POLICY_AUTOMATIC,
|
|
|
|
GTK_POLICY_AUTOMATIC);
|
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolledwindow), list);
|
|
|
|
}
|
|
|
|
|
2020-11-10 01:07:09 +00:00
|
|
|
extern GtkWidget *create_ucd_view (GtkWidget *label);
|
|
|
|
|
|
|
|
static void
|
|
|
|
populate_list2 (void)
|
|
|
|
{
|
|
|
|
GtkWidget *list;
|
|
|
|
|
|
|
|
list = create_ucd_view (NULL);
|
|
|
|
|
|
|
|
hincrement = 0;
|
|
|
|
vincrement = 5;
|
|
|
|
|
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
|
|
|
|
GTK_POLICY_AUTOMATIC,
|
|
|
|
GTK_POLICY_AUTOMATIC);
|
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolledwindow), list);
|
|
|
|
}
|
|
|
|
|
2019-10-12 02:04:30 +00:00
|
|
|
static void
|
|
|
|
set_widget_type (int type)
|
|
|
|
{
|
|
|
|
if (tick_cb)
|
|
|
|
gtk_widget_remove_tick_callback (window, tick_cb);
|
|
|
|
|
2020-05-02 04:51:20 +00:00
|
|
|
if (gtk_scrolled_window_get_child (GTK_SCROLLED_WINDOW (scrolledwindow)))
|
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolledwindow), NULL);
|
2019-10-12 02:04:30 +00:00
|
|
|
|
|
|
|
selected = type;
|
|
|
|
|
|
|
|
switch (selected)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Scrolling icons");
|
|
|
|
populate_icons ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Scrolling plain text");
|
|
|
|
populate_text (FALSE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Scrolling complex text");
|
|
|
|
populate_text (TRUE);
|
|
|
|
break;
|
|
|
|
|
2019-10-23 01:20:36 +00:00
|
|
|
case 3:
|
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Scrolling a big image");
|
|
|
|
populate_image ();
|
|
|
|
break;
|
|
|
|
|
2019-12-22 17:27:30 +00:00
|
|
|
case 4:
|
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Scrolling a list");
|
|
|
|
populate_list ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
2020-11-10 01:07:09 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Scrolling a list");
|
|
|
|
populate_list2 ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
2019-12-22 17:27:30 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Scrolling a grid");
|
|
|
|
populate_grid ();
|
|
|
|
break;
|
|
|
|
|
2019-10-12 02:04:30 +00:00
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
|
|
|
|
tick_cb = gtk_widget_add_tick_callback (window, scroll_cb, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2020-07-27 09:02:00 +00:00
|
|
|
G_MODULE_EXPORT void
|
2019-11-29 07:48:48 +00:00
|
|
|
iconscroll_next_clicked_cb (GtkButton *source,
|
|
|
|
gpointer user_data)
|
2019-10-12 02:04:30 +00:00
|
|
|
{
|
|
|
|
int new_index;
|
|
|
|
|
|
|
|
if (selected + 1 >= N_WIDGET_TYPES)
|
|
|
|
new_index = 0;
|
|
|
|
else
|
|
|
|
new_index = selected + 1;
|
|
|
|
|
|
|
|
|
|
|
|
set_widget_type (new_index);
|
|
|
|
}
|
|
|
|
|
2020-07-27 09:02:00 +00:00
|
|
|
G_MODULE_EXPORT void
|
2019-11-29 07:48:48 +00:00
|
|
|
iconscroll_prev_clicked_cb (GtkButton *source,
|
|
|
|
gpointer user_data)
|
2019-10-12 02:04:30 +00:00
|
|
|
{
|
|
|
|
int new_index;
|
|
|
|
|
|
|
|
if (selected - 1 < 0)
|
|
|
|
new_index = N_WIDGET_TYPES - 1;
|
|
|
|
else
|
|
|
|
new_index = selected - 1;
|
|
|
|
|
|
|
|
set_widget_type (new_index);
|
2019-10-11 21:35:58 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 03:25:05 +00:00
|
|
|
static gboolean
|
|
|
|
update_fps (gpointer data)
|
|
|
|
{
|
|
|
|
GtkWidget *label = data;
|
|
|
|
GdkFrameClock *frame_clock;
|
|
|
|
double fps;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
frame_clock = gtk_widget_get_frame_clock (label);
|
|
|
|
|
|
|
|
fps = gdk_frame_clock_get_fps (frame_clock);
|
|
|
|
str = g_strdup_printf ("%.2f fps", fps);
|
|
|
|
gtk_label_set_label (GTK_LABEL (label), str);
|
|
|
|
g_free (str);
|
|
|
|
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
remove_timeout (gpointer data)
|
|
|
|
{
|
|
|
|
g_source_remove (GPOINTER_TO_UINT (data));
|
|
|
|
}
|
|
|
|
|
2020-07-27 09:02:00 +00:00
|
|
|
G_MODULE_EXPORT GtkWidget *
|
2019-10-11 21:35:58 +00:00
|
|
|
do_iconscroll (GtkWidget *do_widget)
|
|
|
|
{
|
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
GtkBuilder *builder;
|
2020-09-14 03:25:05 +00:00
|
|
|
GtkWidget *label;
|
|
|
|
guint id;
|
2019-10-11 21:35:58 +00:00
|
|
|
|
2019-11-29 07:48:48 +00:00
|
|
|
builder = gtk_builder_new_from_resource ("/iconscroll/iconscroll.ui");
|
2019-10-11 21:35:58 +00:00
|
|
|
window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
|
2020-05-09 16:03:11 +00:00
|
|
|
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
|
2019-10-11 21:35:58 +00:00
|
|
|
gtk_window_set_display (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_display (do_widget));
|
2020-05-09 16:03:11 +00:00
|
|
|
|
2019-10-12 02:04:30 +00:00
|
|
|
scrolledwindow = GTK_WIDGET (gtk_builder_get_object (builder, "scrolledwindow"));
|
2019-10-11 21:35:58 +00:00
|
|
|
gtk_widget_realize (window);
|
2019-10-23 01:20:36 +00:00
|
|
|
hadjustment = GTK_ADJUSTMENT (gtk_builder_get_object (builder, "hadjustment"));
|
|
|
|
vadjustment = GTK_ADJUSTMENT (gtk_builder_get_object (builder, "vadjustment"));
|
2019-10-12 02:04:30 +00:00
|
|
|
set_widget_type (0);
|
2020-05-13 21:24:57 +00:00
|
|
|
|
2020-09-14 03:25:05 +00:00
|
|
|
label = GTK_WIDGET (gtk_builder_get_object (builder, "fps_label"));
|
|
|
|
id = g_timeout_add (500, update_fps, label);
|
|
|
|
g_object_set_data_full (G_OBJECT (label), "timeout",
|
|
|
|
GUINT_TO_POINTER (id), remove_timeout);
|
|
|
|
|
2020-05-13 21:24:57 +00:00
|
|
|
g_object_unref (builder);
|
2019-10-11 21:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!gtk_widget_get_visible (window))
|
|
|
|
gtk_widget_show (window);
|
|
|
|
else
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2019-10-11 21:35:58 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|