summaryrefslogtreecommitdiff
path: root/iup/src/iup_toggle.c
diff options
context:
space:
mode:
authorPixel <pixel@nobis-crew.org>2009-11-04 11:56:41 -0800
committerPixel <pixel@nobis-crew.org>2009-11-04 11:59:33 -0800
commitd577d991b97ae2b5ee1af23641bcffc3f83af5b2 (patch)
tree590639d50205d1bcfaff2a7d2dc6ebf3f373c7ed /iup/src/iup_toggle.c
Initial import. Contains the im, cd and iup librairies, and a "working" Makefile for them under linux.
Diffstat (limited to 'iup/src/iup_toggle.c')
-rwxr-xr-xiup/src/iup_toggle.c140
1 files changed, 140 insertions, 0 deletions
diff --git a/iup/src/iup_toggle.c b/iup/src/iup_toggle.c
new file mode 100755
index 0000000..9a09f52
--- /dev/null
+++ b/iup/src/iup_toggle.c
@@ -0,0 +1,140 @@
+/** \file
+ * \brief Toggle Control.
+ *
+ * See Copyright Notice in "iup.h"
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "iup.h"
+#include "iupcbs.h"
+
+#include "iup_object.h"
+#include "iup_attrib.h"
+#include "iup_str.h"
+#include "iup_drv.h"
+#include "iup_drvfont.h"
+#include "iup_stdcontrols.h"
+#include "iup_layout.h"
+#include "iup_toggle.h"
+#include "iup_image.h"
+
+
+static char* iToggleGetRadioAttrib(Ihandle* ih)
+{
+ if (ih->data->radio)
+ return "YES";
+ else
+ return "NO";
+}
+
+char* iupToggleGetPaddingAttrib(Ihandle* ih)
+{
+ char *str = iupStrGetMemory(50);
+ sprintf(str, "%dx%d", ih->data->horiz_padding, ih->data->vert_padding);
+ return str;
+}
+
+static int iToggleCreateMethod(Ihandle* ih, void** params)
+{
+ if (params)
+ {
+ if (params[0]) iupAttribStoreStr(ih, "TITLE", (char*)(params[0]));
+ if (params[1]) iupAttribStoreStr(ih, "ACTION", (char*)(params[1]));
+ }
+ ih->data = iupALLOCCTRLDATA();
+ return IUP_NOERROR;
+}
+
+static void iToggleComputeNaturalSizeMethod(Ihandle* ih, int *w, int *h, int *expand)
+{
+ int natural_w = 0,
+ natural_h = 0,
+ type = ih->data->type;
+ (void)expand; /* unset if not a container */
+
+ if (!ih->handle)
+ {
+ /* if not mapped must initialize the internal values */
+ char* value = iupAttribGet(ih, "IMAGE");
+ if (value)
+ type = IUP_TOGGLE_IMAGE;
+ else
+ type = IUP_TOGGLE_TEXT;
+ }
+
+ if (type == IUP_TOGGLE_IMAGE)
+ {
+ iupImageGetInfo(iupAttribGet(ih, "IMAGE"), &natural_w, &natural_h, NULL);
+
+ /* even when IMPRESS is set, must compute the borders space */
+ iupdrvButtonAddBorders(&natural_w, &natural_h);
+
+ natural_w += 2*ih->data->horiz_padding;
+ natural_h += 2*ih->data->vert_padding;
+ }
+ else /* IUP_TOGGLE_TEXT */
+ {
+ /* must use IupGetAttribute to check from the native implementation */
+ char* title = IupGetAttribute(ih, "TITLE");
+ char* str = iupStrProcessMnemonic(title, NULL, 0); /* remove & */
+ iupdrvFontGetMultiLineStringSize(ih, str, &natural_w, &natural_h);
+ if (str && str!=title) free(str);
+
+ iupdrvToggleAddCheckBox(&natural_w, &natural_h);
+ }
+
+ *w = natural_w;
+ *h = natural_h;
+}
+
+
+/******************************************************************************/
+
+
+Ihandle* IupToggle(const char* title, const char* action)
+{
+ void *params[3];
+ params[0] = (void*)title;
+ params[1] = (void*)action;
+ params[2] = NULL;
+ return IupCreatev("toggle", params);
+}
+
+Iclass* iupToggleGetClass(void)
+{
+ Iclass* ic = iupClassNew(NULL);
+
+ ic->name = "toggle";
+ ic->format = "SA"; /* one optional string and one optional callback name */
+ ic->nativetype = IUP_TYPECONTROL;
+ ic->childtype = IUP_CHILDNONE;
+ ic->is_interactive = 1;
+
+ /* Class functions */
+ ic->Create = iToggleCreateMethod;
+ ic->ComputeNaturalSize = iToggleComputeNaturalSizeMethod;
+ ic->LayoutUpdate = iupdrvBaseLayoutUpdateMethod;
+ ic->UnMap = iupdrvBaseUnMapMethod;
+
+ /* Callbacks */
+ iupClassRegisterCallback(ic, "ACTION", "i");
+ iupClassRegisterCallback(ic, "VALUECHANGED_CB", "");
+
+ /* Common Callbacks */
+ iupBaseRegisterCommonCallbacks(ic);
+
+ /* Common */
+ iupBaseRegisterCommonAttrib(ic);
+
+ /* Visual */
+ iupBaseRegisterVisualAttrib(ic);
+
+ iupClassRegisterAttribute(ic, "RADIO", iToggleGetRadioAttrib, NULL, NULL, NULL, IUPAF_READONLY|IUPAF_NO_INHERIT);
+ iupClassRegisterAttribute(ic, "3STATE", NULL, NULL, NULL, NULL, IUPAF_NO_INHERIT);
+
+ iupdrvToggleInitClass(ic);
+
+ return ic;
+}