From f9e4735510c55b3aa19d7a04d4f7cfe7cde445ff Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Thu, 27 Aug 2015 15:30:55 -0400 Subject: [PATCH] Started migrating GTK+ controls. --- redo/unix/box.c | 58 +++++++++++++++----------------------------- redo/unix/button.c | 47 ++++++++++++----------------------- redo/unix/checkbox.c | 52 ++++++++++++--------------------------- 3 files changed, 52 insertions(+), 105 deletions(-) diff --git a/redo/unix/box.c b/redo/unix/box.c index 876d88b0..fd1a848f 100644 --- a/redo/unix/box.c +++ b/redo/unix/box.c @@ -3,13 +3,12 @@ // TODO clean this up -struct box { - uiBox b; +struct uiBox { + uiUnixControl c; GtkWidget *widget; GtkContainer *container; GtkBox *box; - void (*baseCommitDestroy)(uiControl *); - struct ptrArray *controls; // TODO switch to GArray + struct ptrArray *controls; // TODO switch to GArray int vertical; int padded; GtkSizeGroup *stretchygroup; // ensures all stretchy controls have the same size @@ -20,11 +19,16 @@ struct boxControl { int stretchy; }; -uiDefineControlType(uiBox, uiTypeBox, struct box) +static void onDestroy(uiBox *b); -static void boxCommitDestroy(uiControl *c) +uiUnixDefineControlWithOnDestroy( + uiBox, // type name + uiBoxType, // type function + onDestroy(this); // on destroy +) + +static void onDestroy(uiBox *b) { - struct box *b = (struct box *) c; struct boxControl *bc; // don't chain up to base here; we need to destroy children ourselves first @@ -38,20 +42,11 @@ static void boxCommitDestroy(uiControl *c) ptrArrayDestroy(b->controls); // kill the size group g_object_unref(b->stretchygroup); - // NOW we can chain up to base - (*(b->baseCommitDestroy))(uiControl(b)); -} - -static uintptr_t boxHandle(uiControl *c) -{ - struct box *b = (struct box *) c; - - return (uintptr_t) (b->widget); } static void boxContainerUpdateState(uiControl *c) { - struct box *b = (struct box *) c; + uiBox *b = uiBox(c); struct boxControl *bc; uintmax_t i; @@ -61,9 +56,8 @@ static void boxContainerUpdateState(uiControl *c) } } -static void boxAppend(uiBox *ss, uiControl *c, int stretchy) +void uiBoxAppend(uiBox *b, uiControl *c, int stretchy) { - struct box *b = (struct box *) ss; struct boxControl *bc; GtkWidget *widget; @@ -91,9 +85,8 @@ static void boxAppend(uiBox *ss, uiControl *c, int stretchy) uiControlQueueResize(uiControl(b)); } -static void boxDelete(uiBox *ss, uintmax_t index) +void uiBoxDelete(uiBox *b, uintmax_t index) { - struct box *b = (struct box *) ss; struct boxControl *bc; bc = ptrArrayIndex(b->controls, struct boxControl *, index); @@ -105,17 +98,13 @@ static void boxDelete(uiBox *ss, uintmax_t index) uiControlQueueResize(uiControl(b)); } -static int boxPadded(uiBox *ss) +int uiBoxPadded(uiBox *b) { - struct box *b = (struct box *) ss; - return b->padded; } -static void boxSetPadded(uiBox *ss, int padded) +void uiBoxSetPadded(uiBox *b, int padded) { - struct box *b = (struct box *) ss; - b->padded = padded; if (b->padded) if (b->vertical) @@ -129,9 +118,9 @@ static void boxSetPadded(uiBox *ss, int padded) static uiBox *finishNewBox(GtkOrientation orientation) { - struct box *b; + uiBox *b; - b = (struct box *) uiNewControl(uiTypeBox()); + b = (uiBox *) uiNewControl(uiTypeBox()); b->widget = gtk_box_new(orientation, 0); b->container = GTK_CONTAINER(b->widget); @@ -148,17 +137,10 @@ static uiBox *finishNewBox(GtkOrientation orientation) uiUnixMakeSingleWidgetControl(uiControl(b), b->widget); - uiControl(b)->Handle = boxHandle; - b->baseCommitDestroy = uiControl(b)->CommitDestroy; - uiControl(b)->CommitDestroy = boxCommitDestroy; + uiUnixFinishNewControl(b, uiBox); uiControl(b)->ContainerUpdateState = boxContainerUpdateState; - uiBox(b)->Append = boxAppend; - uiBox(b)->Delete = boxDelete; - uiBox(b)->Padded = boxPadded; - uiBox(b)->SetPadded = boxSetPadded; - - return uiBox(b); + return b; } uiBox *uiNewHorizontalBox(void) diff --git a/redo/unix/button.c b/redo/unix/button.c index df356ed5..af003caa 100644 --- a/redo/unix/button.c +++ b/redo/unix/button.c @@ -1,28 +1,24 @@ // 10 june 2015 #include "uipriv_unix.h" -struct button { - uiButton b; +struct uiButton { + uiUnixControl c; GtkWidget *widget; GtkButton *button; void (*onClicked)(uiButton *, void *); void *onClickedData; }; -uiDefineControlType(uiButton, uiTypeButton, struct button) +uiUnixDefineControl( + uiButton, // type name + uiButtonType // type function +) static void onClicked(GtkButton *button, gpointer data) { - struct button *b = (struct button *) data; + uiButton *b = uiButton(data); - (*(b->onClicked))(uiButton(b), b->onClickedData); -} - -static uintptr_t buttonHandle(uiControl *c) -{ - struct button *b = (struct button *) c; - - return (uintptr_t) (b->widget); + (*(b->onClicked))(b, b->onClickedData); } static void defaultOnClicked(uiButton *b, void *data) @@ -30,48 +26,37 @@ static void defaultOnClicked(uiButton *b, void *data) // do nothing } -static char *buttonText(uiButton *bb) +char *uiButtonText(uiButton *b) { - struct button *b = (struct button *) bb; - return uiUnixStrdupText(gtk_button_get_label(b->button)); } -static void buttonSetText(uiButton *bb, const char *text) +void uiButtonSetText(uiButton *b, const char *text) { - struct button *b = (struct button *) bb; - gtk_button_set_label(b->button, text); // changing the text might necessitate a change in the button's size uiControlQueueResize(uiControl(b)); } -static void buttonOnClicked(uiButton *bb, void (*f)(uiButton *, void *), void *data) +void uiButtonOnClicked(uiButton *b, void (*f)(uiButton *, void *), void *data) { - struct button *b = (struct button *) bb; - b->onClicked = f; b->onClickedData = data; } uiButton *uiNewButton(const char *text) { - struct button *b; + uiButton *b; - b = (struct button *) uiNewControl(uiTypeButton()); + b = (uiButton *) uiNewControl(uiTypeButton()); b->widget = gtk_button_new_with_label(text); b->button = GTK_BUTTON(b->widget); - uiUnixMakeSingleWidgetControl(uiControl(b), b->widget); g_signal_connect(b->widget, "clicked", G_CALLBACK(onClicked), b); - b->onClicked = defaultOnClicked; + uiButtonOnClicked(b, defaultOnClicked, NULL); - uiControl(b)->Handle = buttonHandle; + uiUnixFinishNewControl(b, uiButton); - uiButton(b)->Text = buttonText; - uiButton(b)->SetText = buttonSetText; - uiButton(b)->OnClicked = buttonOnClicked; - - return uiButton(b); + return b; } diff --git a/redo/unix/checkbox.c b/redo/unix/checkbox.c index 9b2f2a86..b1509414 100644 --- a/redo/unix/checkbox.c +++ b/redo/unix/checkbox.c @@ -1,8 +1,8 @@ // 10 june 2015 #include "uipriv_unix.h" -struct checkbox { - uiCheckbox c; +struct uiCheckbox { + uiUnixControl c; GtkWidget *widget; GtkButton *button; GtkToggleButton *toggleButton; @@ -12,7 +12,10 @@ struct checkbox { gulong onToggledSignal; }; -uiDefineControlType(uiCheckbox, uiTypeCheckbox, struct checkbox) +uiUnixDefineControl( + uiCheckbox, // type name + uiCheckboxType // type function +) static void onToggled(GtkToggleButton *b, gpointer data) { @@ -21,52 +24,36 @@ static void onToggled(GtkToggleButton *b, gpointer data) (*(c->onToggled))(uiCheckbox(c), c->onToggledData); } -static uintptr_t checkboxHandle(uiControl *cc) -{ - struct checkbox *c = (struct checkbox *) cc; - - return (uintptr_t) (c->widget); -} - static void defaultOnToggled(uiCheckbox *c, void *data) { // do nothing } -static char *checkboxText(uiCheckbox *cc) +char *uiCheckboxText(uiCheckbox *c) { - struct checkbox *c = (struct checkbox *) cc; - return uiUnixStrdupText(gtk_button_get_label(c->button)); } -static void checkboxSetText(uiCheckbox *cc, const char *text) +void uiCheckboxSetText(uiCheckbox *c, const char *text) { - struct checkbox *c = (struct checkbox *) cc; - gtk_button_set_label(GTK_BUTTON(c->button), text); // changing the text might necessitate a change in the checkbox's size uiControlQueueResize(uiControl(c)); } -static void checkboxOnToggled(uiCheckbox *cc, void (*f)(uiCheckbox *, void *), void *data) +void uiCheckboxOnToggled(uiCheckbox *c, void (*f)(uiCheckbox *, void *), void *data) { - struct checkbox *c = (struct checkbox *) cc; - c->onToggled = f; c->onToggledData = data; } -static int checkboxChecked(uiCheckbox *cc) +int uiCheckboxChecked(uiCheckbox *c) { - struct checkbox *c = (struct checkbox *) cc; - return gtk_toggle_button_get_active(c->toggleButton) != FALSE; } -static void checkboxSetChecked(uiCheckbox *cc, int checked) +void uiCheckboxSetChecked(uiCheckbox *c, int checked) { - struct checkbox *c = (struct checkbox *) cc; gboolean active; active = FALSE; @@ -80,26 +67,19 @@ static void checkboxSetChecked(uiCheckbox *cc, int checked) uiCheckbox *uiNewCheckbox(const char *text) { - struct checkbox *c; + uiCheckbox *c; - c = (struct checkbox *) uiNewControl(uiTypeCheckbox()); + c = (uiCheckbox *) uiNewControl(uiTypeCheckbox()); c->widget = gtk_check_button_new_with_label(text); c->button = GTK_BUTTON(c->widget); c->toggleButton = GTK_TOGGLE_BUTTON(c->widget); c->checkButton = GTK_CHECK_BUTTON(c->widget); - uiUnixMakeSingleWidgetControl(uiControl(c), c->widget); c->onToggledSignal = g_signal_connect(c->widget, "toggled", G_CALLBACK(onToggled), c); - c->onToggled = defaultOnToggled; + uiCheckboxOnToggled(c, defaultOnToggled, NULL); - uiControl(c)->Handle = checkboxHandle; + uiUnixFinishNewControl(c, uiCheckbox); - uiCheckbox(c)->Text = checkboxText; - uiCheckbox(c)->SetText = checkboxSetText; - uiCheckbox(c)->OnToggled = checkboxOnToggled; - uiCheckbox(c)->Checked = checkboxChecked; - uiCheckbox(c)->SetChecked = checkboxSetChecked; - - return uiCheckbox(c); + return c; }