2021-02-09 17:37:26 +02:00
|
|
|
/* koto-expander.c
|
|
|
|
*
|
|
|
|
* Copyright 2021 Joshua Strobl
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <glib/gi18n.h>
|
2021-02-24 20:17:18 +02:00
|
|
|
#include <gtk-4.0/gtk/gtk.h>
|
2021-02-09 17:37:26 +02:00
|
|
|
#include "koto-config.h"
|
|
|
|
#include "koto-button.h"
|
|
|
|
#include "koto-expander.h"
|
|
|
|
|
|
|
|
enum {
|
2021-02-12 12:56:41 +02:00
|
|
|
PROP_EXP_0,
|
2021-02-09 17:37:26 +02:00
|
|
|
PROP_HEADER_ICON_NAME,
|
|
|
|
PROP_HEADER_LABEL,
|
|
|
|
PROP_HEADER_SECONDARY_BUTTON,
|
|
|
|
PROP_CONTENT,
|
2021-02-12 12:56:41 +02:00
|
|
|
N_EXP_PROPERTIES
|
2021-02-09 17:37:26 +02:00
|
|
|
};
|
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
static GParamSpec * expander_props[N_EXP_PROPERTIES] = {
|
|
|
|
NULL,
|
|
|
|
};
|
2021-02-09 17:37:26 +02:00
|
|
|
|
|
|
|
struct _KotoExpander {
|
|
|
|
GtkBox parent_instance;
|
|
|
|
gboolean constructed;
|
2021-05-11 20:05:04 +03:00
|
|
|
GtkWidget * header;
|
|
|
|
KotoButton * header_button;
|
2021-02-09 17:37:26 +02:00
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
gchar * icon_name;
|
|
|
|
gchar * label;
|
2021-02-09 17:37:26 +02:00
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
KotoButton * header_secondary_button;
|
|
|
|
KotoButton * header_expand_button;
|
2021-02-09 17:37:26 +02:00
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
GtkWidget * revealer;
|
|
|
|
GtkWidget * content;
|
2021-02-09 17:37:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _KotoExpanderClass {
|
|
|
|
GtkBoxClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE(KotoExpander, koto_expander, GTK_TYPE_BOX);
|
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
static void koto_expander_get_property(
|
|
|
|
GObject * obj,
|
|
|
|
guint prop_id,
|
|
|
|
GValue * val,
|
|
|
|
GParamSpec * spec
|
|
|
|
);
|
|
|
|
|
|
|
|
static void koto_expander_set_property(
|
|
|
|
GObject * obj,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue * val,
|
|
|
|
GParamSpec * spec
|
|
|
|
);
|
|
|
|
|
|
|
|
static void koto_expander_class_init(KotoExpanderClass * c) {
|
|
|
|
GObjectClass * gobject_class = G_OBJECT_CLASS(c);
|
|
|
|
|
2021-02-09 17:37:26 +02:00
|
|
|
|
|
|
|
gobject_class->set_property = koto_expander_set_property;
|
|
|
|
gobject_class->get_property = koto_expander_get_property;
|
|
|
|
|
2021-02-12 12:56:41 +02:00
|
|
|
expander_props[PROP_HEADER_ICON_NAME] = g_param_spec_string(
|
2021-02-09 17:37:26 +02:00
|
|
|
"icon-name",
|
|
|
|
"Icon Name",
|
|
|
|
"Name of the icon to use in the Expander",
|
|
|
|
"emblem-favorite-symbolic",
|
2021-05-11 20:05:04 +03:00
|
|
|
G_PARAM_CONSTRUCT | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_READWRITE
|
2021-02-09 17:37:26 +02:00
|
|
|
);
|
|
|
|
|
2021-02-12 12:56:41 +02:00
|
|
|
expander_props[PROP_HEADER_LABEL] = g_param_spec_string(
|
2021-02-09 17:37:26 +02:00
|
|
|
"label",
|
|
|
|
"Label",
|
|
|
|
"Label for the Expander",
|
|
|
|
NULL,
|
2021-05-11 20:05:04 +03:00
|
|
|
G_PARAM_CONSTRUCT | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_READWRITE
|
2021-02-09 17:37:26 +02:00
|
|
|
);
|
|
|
|
|
2021-02-12 12:56:41 +02:00
|
|
|
expander_props[PROP_HEADER_SECONDARY_BUTTON] = g_param_spec_object(
|
2021-02-09 17:37:26 +02:00
|
|
|
"secondary-button",
|
|
|
|
"Secondary Button",
|
|
|
|
"Secondary Button to be placed next to Expander button",
|
2021-02-12 12:56:41 +02:00
|
|
|
KOTO_TYPE_BUTTON,
|
2021-05-11 20:05:04 +03:00
|
|
|
G_PARAM_CONSTRUCT | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_READWRITE
|
2021-02-09 17:37:26 +02:00
|
|
|
);
|
|
|
|
|
2021-02-12 12:56:41 +02:00
|
|
|
expander_props[PROP_CONTENT] = g_param_spec_object(
|
2021-02-09 17:37:26 +02:00
|
|
|
"content",
|
|
|
|
"Content",
|
|
|
|
"Content inside the Expander",
|
|
|
|
GTK_TYPE_WIDGET,
|
2021-05-11 20:05:04 +03:00
|
|
|
G_PARAM_EXPLICIT_NOTIFY | G_PARAM_READWRITE
|
2021-02-09 17:37:26 +02:00
|
|
|
);
|
|
|
|
|
2021-02-12 12:56:41 +02:00
|
|
|
g_object_class_install_properties(gobject_class, N_EXP_PROPERTIES, expander_props);
|
2021-02-09 17:37:26 +02:00
|
|
|
}
|
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
static void koto_expander_get_property(
|
|
|
|
GObject * obj,
|
|
|
|
guint prop_id,
|
|
|
|
GValue * val,
|
|
|
|
GParamSpec * spec
|
|
|
|
) {
|
|
|
|
KotoExpander * self = KOTO_EXPANDER(obj);
|
|
|
|
|
2021-02-09 17:37:26 +02:00
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_HEADER_ICON_NAME:
|
|
|
|
g_value_set_string(val, self->icon_name);
|
|
|
|
break;
|
|
|
|
case PROP_HEADER_LABEL:
|
|
|
|
g_value_set_string(val, self->label);
|
|
|
|
break;
|
|
|
|
case PROP_HEADER_SECONDARY_BUTTON:
|
2021-05-11 20:05:04 +03:00
|
|
|
g_value_set_object(val, (GObject*) self->header_secondary_button);
|
2021-02-09 17:37:26 +02:00
|
|
|
break;
|
|
|
|
case PROP_CONTENT:
|
2021-05-11 20:05:04 +03:00
|
|
|
g_value_set_object(val, (GObject*) self->content);
|
2021-02-09 17:37:26 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, spec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
static void koto_expander_set_property(
|
|
|
|
GObject * obj,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue * val,
|
|
|
|
GParamSpec * spec
|
|
|
|
) {
|
|
|
|
KotoExpander * self = KOTO_EXPANDER(obj);
|
2021-02-09 17:37:26 +02:00
|
|
|
|
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
if (!GTK_IS_WIDGET(self->header_button)) { // Header Button is not a widget
|
|
|
|
KotoButton * new_button = koto_button_new_with_icon(NULL, "emblem-favorite-symbolic", NULL, KOTO_BUTTON_PIXBUF_SIZE_SMALL);
|
2021-02-09 17:37:26 +02:00
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
if (GTK_IS_WIDGET(new_button)) { // Created our widget successfully
|
|
|
|
self->header_button = new_button;
|
|
|
|
gtk_widget_set_hexpand(GTK_WIDGET(self->header_button), TRUE);
|
|
|
|
gtk_box_prepend(GTK_BOX(self->header), GTK_WIDGET(self->header_button));
|
2021-02-09 17:37:26 +02:00
|
|
|
}
|
2021-05-11 20:05:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_HEADER_ICON_NAME:
|
|
|
|
g_return_if_fail(GTK_IS_WIDGET(self->header_button));
|
|
|
|
koto_button_set_icon_name(self->header_button, g_strdup(g_value_get_string(val)), FALSE);
|
|
|
|
break;
|
|
|
|
case PROP_HEADER_LABEL:
|
|
|
|
g_return_if_fail(GTK_IS_WIDGET(self->header_button));
|
|
|
|
koto_button_set_text(self->header_button, g_strdup(g_value_get_string(val)));
|
|
|
|
break;
|
|
|
|
case PROP_HEADER_SECONDARY_BUTTON:
|
|
|
|
koto_expander_set_secondary_button(self, (KotoButton*) g_value_get_object(val));
|
|
|
|
break;
|
|
|
|
case PROP_CONTENT:
|
|
|
|
koto_expander_set_content(self, (GtkWidget*) g_value_get_object(val));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, spec);
|
|
|
|
break;
|
|
|
|
}
|
2021-02-09 17:37:26 +02:00
|
|
|
}
|
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
static void koto_expander_init(KotoExpander * self) {
|
|
|
|
GtkStyleContext * style = gtk_widget_get_style_context(GTK_WIDGET(self));
|
|
|
|
|
|
|
|
|
2021-02-09 17:37:26 +02:00
|
|
|
gtk_style_context_add_class(style, "expander");
|
2021-02-24 20:17:18 +02:00
|
|
|
gtk_widget_set_hexpand((GTK_WIDGET(self)), TRUE);
|
2021-02-09 17:37:26 +02:00
|
|
|
|
|
|
|
self->header = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
|
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
GtkStyleContext * header_style = gtk_widget_get_style_context(self->header);
|
|
|
|
|
|
|
|
|
2021-02-09 17:37:26 +02:00
|
|
|
gtk_style_context_add_class(header_style, "expander-header");
|
|
|
|
|
|
|
|
self->revealer = gtk_revealer_new();
|
|
|
|
gtk_revealer_set_reveal_child(GTK_REVEALER(self->revealer), TRUE); // Set to be revealed by default
|
|
|
|
gtk_revealer_set_transition_type(GTK_REVEALER(self->revealer), GTK_REVEALER_TRANSITION_TYPE_SLIDE_DOWN);
|
|
|
|
|
2021-02-12 12:56:41 +02:00
|
|
|
self->header_expand_button = koto_button_new_with_icon("", "pan-down-symbolic", "pan-up-symbolic", KOTO_BUTTON_PIXBUF_SIZE_SMALL);
|
2021-02-24 20:17:18 +02:00
|
|
|
gtk_box_append(GTK_BOX(self->header), GTK_WIDGET(self->header_expand_button));
|
2021-02-09 17:37:26 +02:00
|
|
|
|
2021-02-24 20:17:18 +02:00
|
|
|
gtk_box_prepend(GTK_BOX(self), self->header);
|
|
|
|
gtk_box_append(GTK_BOX(self), self->revealer);
|
2021-02-09 17:37:26 +02:00
|
|
|
|
|
|
|
self->constructed = TRUE;
|
|
|
|
|
2021-05-07 16:45:57 +03:00
|
|
|
koto_button_add_click_handler(self->header_expand_button, KOTO_BUTTON_CLICK_TYPE_PRIMARY, G_CALLBACK(koto_expander_toggle_content), self);
|
2021-02-09 17:37:26 +02:00
|
|
|
}
|
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
void koto_expander_set_secondary_button(
|
|
|
|
KotoExpander * self,
|
|
|
|
KotoButton * new_button
|
|
|
|
) {
|
2021-02-09 17:37:26 +02:00
|
|
|
if (!self->constructed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-12 12:56:41 +02:00
|
|
|
if (!GTK_IS_WIDGET(new_button)) {
|
2021-02-09 17:37:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-12 12:56:41 +02:00
|
|
|
if (GTK_IS_WIDGET(self->header_secondary_button)) { // Already have a button
|
2021-02-24 20:17:18 +02:00
|
|
|
gtk_box_remove(GTK_BOX(self->header), GTK_WIDGET(self->header_secondary_button));
|
2021-02-09 17:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self->header_secondary_button = new_button;
|
2021-02-24 20:17:18 +02:00
|
|
|
gtk_box_append(GTK_BOX(self->header), GTK_WIDGET(self->header_secondary_button));
|
2021-02-09 17:37:26 +02:00
|
|
|
|
2021-02-12 12:56:41 +02:00
|
|
|
g_object_notify_by_pspec(G_OBJECT(self), expander_props[PROP_HEADER_SECONDARY_BUTTON]);
|
2021-02-09 17:37:26 +02:00
|
|
|
}
|
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
void koto_expander_set_content(
|
|
|
|
KotoExpander * self,
|
|
|
|
GtkWidget * new_content
|
|
|
|
) {
|
2021-02-09 17:37:26 +02:00
|
|
|
if (!self->constructed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-24 20:17:18 +02:00
|
|
|
if (GTK_IS_WIDGET(self->content)) { // Already have content
|
|
|
|
gtk_revealer_set_child(GTK_REVEALER(self->revealer), NULL);
|
|
|
|
g_object_unref(self->content);
|
2021-02-09 17:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self->content = new_content;
|
2021-02-24 20:17:18 +02:00
|
|
|
gtk_revealer_set_child(GTK_REVEALER(self->revealer), self->content);
|
2021-02-09 17:37:26 +02:00
|
|
|
|
2021-02-12 12:56:41 +02:00
|
|
|
g_object_notify_by_pspec(G_OBJECT(self), expander_props[PROP_CONTENT]);
|
2021-02-09 17:37:26 +02:00
|
|
|
}
|
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
GtkWidget * koto_expander_get_content(KotoExpander * self) {
|
2021-05-07 16:45:57 +03:00
|
|
|
return self->content;
|
|
|
|
}
|
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
void koto_expander_toggle_content(
|
|
|
|
GtkGestureClick * gesture,
|
|
|
|
int n_press,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
gpointer data
|
|
|
|
) {
|
|
|
|
(void) gesture;
|
|
|
|
(void) n_press;
|
|
|
|
(void) x;
|
|
|
|
(void) y;
|
2021-02-09 17:37:26 +02:00
|
|
|
KotoExpander* self = data;
|
2021-02-24 20:17:18 +02:00
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
|
2021-02-24 20:17:18 +02:00
|
|
|
koto_button_flip(KOTO_BUTTON(self->header_expand_button));
|
2021-02-09 17:37:26 +02:00
|
|
|
GtkRevealer* rev = GTK_REVEALER(self->revealer);
|
2021-05-11 20:05:04 +03:00
|
|
|
|
|
|
|
|
2021-02-09 17:37:26 +02:00
|
|
|
gtk_revealer_set_reveal_child(rev, !gtk_revealer_get_reveal_child(rev)); // Invert our values
|
|
|
|
}
|
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
KotoExpander * koto_expander_new(
|
|
|
|
gchar * primary_icon_name,
|
|
|
|
gchar * primary_label_text
|
|
|
|
) {
|
|
|
|
return g_object_new(
|
|
|
|
KOTO_TYPE_EXPANDER,
|
|
|
|
"orientation",
|
|
|
|
GTK_ORIENTATION_VERTICAL,
|
|
|
|
"icon-name",
|
|
|
|
primary_icon_name,
|
|
|
|
"label",
|
|
|
|
primary_label_text,
|
2021-02-09 17:37:26 +02:00
|
|
|
NULL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-11 20:05:04 +03:00
|
|
|
KotoExpander * koto_expander_new_with_button(
|
|
|
|
gchar * primary_icon_name,
|
|
|
|
gchar * primary_label_text,
|
|
|
|
KotoButton * secondary_button
|
|
|
|
) {
|
|
|
|
return g_object_new(
|
|
|
|
KOTO_TYPE_EXPANDER,
|
|
|
|
"orientation",
|
|
|
|
GTK_ORIENTATION_VERTICAL,
|
|
|
|
"icon-name",
|
|
|
|
primary_icon_name,
|
|
|
|
"label",
|
|
|
|
primary_label_text,
|
|
|
|
"secondary-button",
|
|
|
|
secondary_button,
|
2021-02-09 17:37:26 +02:00
|
|
|
NULL
|
|
|
|
);
|
|
|
|
}
|