Skip to content

Commit

Permalink
Removed all the GUI stuff from the GTK+ date/time picker for now. Ins…
Browse files Browse the repository at this point in the history
…talled the date properties instead.
  • Loading branch information
andlabs committed Jan 11, 2015
1 parent 2867965 commit 2ce4ae1
Showing 1 changed file with 92 additions and 46 deletions.
138 changes: 92 additions & 46 deletions gtkdtp/dtp.c
Expand Up @@ -10,18 +10,15 @@
#define GDTPC(x) ((goDateTimePickerClass *) x)

struct goDateTimePickerPrivate {
GtkWidget *openbutton;

GtkWidget *popup;
GtkWidget *calendar;
GtkWidget *spinHours;
GtkWidget *spinMinutes;
GtkWidget *spinSeconds;
gint year;
gint month;
gint day;
};

G_DEFINE_TYPE_WITH_CODE(goDateTimePicker, goDateTimePicker, GTK_TYPE_BOX,
G_ADD_PRIVATE(goDateTimePicker))

// TODO figure out how to share these between C and Go
enum {
gtkMargin = 12,
gtkXPadding = 12,
Expand All @@ -30,53 +27,14 @@ enum {

static void goDateTimePicker_init(goDateTimePicker *dtp)
{
goDateTimePickerPrivate *d;
GtkWidget *arrow;
GtkWidget *vbox;
GtkWidget *hbox;

dtp->priv = goDateTimePicker_get_instance_private(dtp);
d = dtp->priv;

// create the actual bar elements
// TODO the entry field
// just make a dummy one for testing
hbox = gtk_entry_new();
gtk_style_context_add_class(gtk_widget_get_style_context(hbox), GTK_STYLE_CLASS_COMBOBOX_ENTRY);
gtk_widget_set_hexpand(hbox, TRUE);
gtk_widget_set_halign(hbox, GTK_ALIGN_FILL);
gtk_container_add(GTK_CONTAINER(dtp), hbox);
// the open button
d->openbutton = gtk_toggle_button_new();
arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
gtk_container_add(GTK_CONTAINER(d->openbutton), arrow);
// and make them look linked
// TODO sufficient?
gtk_style_context_add_class(gtk_widget_get_style_context(GTK_WIDGET(dtp)), "linked");
// and mark them as visible
gtk_widget_show_all(d->openbutton);
// and add them to the bar
gtk_container_add(GTK_CONTAINER(dtp), d->openbutton);

// now create the popup that will hold everything
d->popup = gtk_window_new(GTK_WINDOW_POPUP);
gtk_window_set_type_hint(GTK_WINDOW(d->popup), GDK_WINDOW_TYPE_HINT_COMBO);
gtk_window_set_resizable(GTK_WINDOW(d->popup), FALSE);
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, gtkYPadding);
gtk_container_set_border_width(GTK_CONTAINER(vbox), gtkMargin);
d->calendar = gtk_calendar_new();
gtk_container_add(GTK_CONTAINER(vbox), d->calendar);
gtk_container_add(GTK_CONTAINER(d->popup), vbox);
}

static void goDateTimePicker_dispose(GObject *obj)
{
goDateTimePickerPrivate *d = PRIV(obj);

// TODO really with g_clear_object()?
g_clear_object(&(d->openbutton));
g_clear_object(&(d->popup));
// TODO g_object_clear() the children?
G_OBJECT_CLASS(goDateTimePicker_parent_class)->dispose(obj);
}

Expand All @@ -85,8 +43,96 @@ static void goDateTimePicker_finalize(GObject *obj)
G_OBJECT_CLASS(goDateTimePicker_parent_class)->finalize(obj);
}

enum {
pYear = 1,
pMonth,
pDay,
nParams,
};

static GParamSpec *gdtpParams[] = {
NULL, // always null
NULL, // year
NULL, // month
NULL, // day
};

static void goDateTimePicker_set_property(GObject *obj, guint prop, const GValue *value, GParamSpec *spec)
{
goDateTimePickerPrivate *d = PRIV(obj);

switch (prop) {
case pYear:
d->year = g_value_get_int(value);
break;
case pMonth:
d->month = g_value_get_int(value);
break;
case pDay:
d->day = g_value_get_int(value);
// see note on GtkCalendar comaptibility below
if (d->day == 0)
; // TODO
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop, spec);
return;
}
// TODO refresh everything here
}

static void goDateTimePicker_get_property(GObject *obj, guint prop, GValue *value, GParamSpec *spec)
{
goDateTimePickerPrivate *d = PRIV(obj);

switch (prop) {
case pYear:
g_value_set_int(value, d->year);
break;
case pMonth:
g_value_set_int(value, d->month);
break;
case pDay:
g_value_set_int(value, d->day);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop, spec);
return;
}
}

static void goDateTimePicker_class_init(goDateTimePickerClass *class)
{
G_OBJECT_CLASS(class)->dispose = goDateTimePicker_dispose;
G_OBJECT_CLASS(class)->finalize = goDateTimePicker_finalize;
G_OBJECT_CLASS(class)->set_property = goDateTimePicker_set_property;
G_OBJECT_CLASS(class)->get_property = goDateTimePicker_get_property;

// types and values are to be compatible with the 3.4 GtkCalendar parameters
gdtpParams[pYear] = g_param_spec_int("year",
"current year",
"Current year",
0,
G_MAXINT >> 9,
0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
gdtpParams[pMonth] = g_param_spec_uint("month",
"current month",
"Current month",
0,
11,
0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
// because of the requirement to be compatible with GtkCalendar, we have to follow its rules about dates
// values are 1..31 with 0 meaning no date selected
// we will not allow no date to be selected, so we will set the default to 1 instead of 0
// TODO is this an issue for binding?
gdtpParams[pDay] = g_param_spec_uint("day",
"current day",
"Current day",
0,
31,
1,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties(G_OBJECT_CLASS(class), nParams, gdtpParams);
}

0 comments on commit 2ce4ae1

Please sign in to comment.