summaryrefslogtreecommitdiff
path: root/iup/src/mot/iupmot_button.c
diff options
context:
space:
mode:
Diffstat (limited to 'iup/src/mot/iupmot_button.c')
-rwxr-xr-xiup/src/mot/iupmot_button.c301
1 files changed, 301 insertions, 0 deletions
diff --git a/iup/src/mot/iupmot_button.c b/iup/src/mot/iupmot_button.c
new file mode 100755
index 0000000..2d93588
--- /dev/null
+++ b/iup/src/mot/iupmot_button.c
@@ -0,0 +1,301 @@
+/** \file
+ * \brief Button Control
+ *
+ * See Copyright Notice in "iup.h"
+ */
+
+#include <Xm/Xm.h>
+#include <Xm/PushB.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <memory.h>
+#include <stdarg.h>
+
+#include "iup.h"
+#include "iupcbs.h"
+
+#include "iup_object.h"
+#include "iup_childtree.h"
+#include "iup_attrib.h"
+#include "iup_dialog.h"
+#include "iup_str.h"
+#include "iup_button.h"
+#include "iup_drv.h"
+#include "iup_image.h"
+#include "iup_key.h"
+
+#include "iupmot_drv.h"
+
+
+void iupdrvButtonAddBorders(int *x, int *y)
+{
+ int border_size = 2*5;
+ (*x) += border_size;
+ (*y) += border_size;
+}
+
+static int motButtonSetTitleAttrib(Ihandle* ih, const char* value)
+{
+ if (ih->data->type == IUP_BUTTON_TEXT)
+ {
+ iupmotSetMnemonicTitle(ih, value);
+ return 1;
+ }
+
+ return 0;
+}
+
+static int motButtonSetAlignmentAttrib(Ihandle* ih, const char* value)
+{
+ unsigned char align;
+ char value1[30]="", value2[30]="";
+
+ iupStrToStrStr(value, value1, value2, ':'); /* value2 is ignored, NOT supported in Motif */
+
+ if (iupStrEqualNoCase(value1, "ARIGHT"))
+ align = XmALIGNMENT_END;
+ else if (iupStrEqualNoCase(value1, "ACENTER"))
+ align = XmALIGNMENT_CENTER;
+ else /* "ALEFT" */
+ align = XmALIGNMENT_BEGINNING;
+
+ XtVaSetValues (ih->handle, XmNalignment, align, NULL);
+ return 1;
+}
+
+static int motButtonSetImageAttrib(Ihandle* ih, const char* value)
+{
+ if (ih->data->type == IUP_BUTTON_IMAGE)
+ {
+ iupmotSetPixmap(ih, value, XmNlabelPixmap, 0);
+
+ if (!iupAttribGet(ih, "IMINACTIVE"))
+ {
+ /* if not active and IMINACTIVE is not defined
+ then automaticaly create one based on IMAGE */
+ iupmotSetPixmap(ih, value, XmNlabelInsensitivePixmap, 1); /* make_inactive */
+ }
+ return 1;
+ }
+ else
+ return 0;
+}
+
+static int motButtonSetImInactiveAttrib(Ihandle* ih, const char* value)
+{
+ if (ih->data->type == IUP_BUTTON_IMAGE)
+ {
+ iupmotSetPixmap(ih, value, XmNlabelInsensitivePixmap, 0);
+ return 1;
+ }
+ else
+ return 0;
+}
+
+static int motButtonSetImPressAttrib(Ihandle* ih, const char* value)
+{
+ if (ih->data->type == IUP_BUTTON_IMAGE)
+ {
+ iupmotSetPixmap(ih, value, XmNarmPixmap, 0);
+ return 1;
+ }
+ else
+ return 0;
+}
+
+static int motButtonSetPaddingAttrib(Ihandle* ih, const char* value)
+{
+ iupStrToIntInt(value, &ih->data->horiz_padding, &ih->data->vert_padding, 'x');
+ if (ih->handle)
+ {
+ XtVaSetValues(ih->handle, XmNmarginHeight, ih->data->vert_padding,
+ XmNmarginWidth, ih->data->horiz_padding, NULL);
+ }
+ return 0;
+}
+
+static int motButtonSetBgColorAttrib(Ihandle* ih, const char* value)
+{
+ if (iupAttribGet(ih, "IMPRESS") || iupAttribGetBoolean(ih, "FLAT"))
+ {
+ /* ignore given value, must use only from parent */
+ value = iupBaseNativeParentGetBgColor(ih);
+
+ if (iupdrvBaseSetBgColorAttrib(ih, value))
+ return 1;
+ }
+
+ return iupdrvBaseSetBgColorAttrib(ih, value);
+}
+
+static int motButtonSetBackgroundAttrib(Ihandle* ih, const char* value)
+{
+ if (iupAttribGet(ih, "IMPRESS") || iupAttribGetBoolean(ih, "FLAT"))
+ {
+ /* ignore given value, must use only from parent */
+ value = iupAttribGetInheritNativeParent(ih, "BACKGROUND");
+
+ if (iupdrvBaseSetBgColorAttrib(ih, value))
+ return 1;
+ else
+ {
+ Pixmap pixmap = (Pixmap)iupImageGetImage(value, ih, 0);
+ if (pixmap)
+ {
+ XtVaSetValues(ih->handle, XmNbackgroundPixmap, pixmap, NULL);
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+static void motButtonActivateCallback(Widget w, Ihandle* ih, XtPointer call_data)
+{
+ Icallback cb;
+
+ /* Must manually hide the tip if the button is pressed. */
+ iupmotTipLeaveNotify();
+
+ cb = IupGetCallback(ih, "ACTION");
+ if (cb)
+ {
+ if (cb(ih) == IUP_CLOSE)
+ IupExitLoop();
+ }
+ (void)w;
+ (void)call_data;
+}
+
+static void motButtonEnterLeaveWindowEvent(Widget w, Ihandle* ih, XEvent *evt, Boolean *cont)
+{
+ iupmotEnterLeaveWindowEvent(w, ih, evt, cont);
+
+ if (evt->type == EnterNotify)
+ XtVaSetValues(ih->handle, XmNshadowThickness, 2, NULL);
+ else if (evt->type == LeaveNotify)
+ XtVaSetValues(ih->handle, XmNshadowThickness, 0, NULL);
+}
+
+static int motButtonMapMethod(Ihandle* ih)
+{
+ char* value;
+ int num_args = 0;
+ Arg args[30];
+
+ value = iupAttribGet(ih, "IMAGE");
+ if (value)
+ {
+ ih->data->type = IUP_BUTTON_IMAGE;
+ iupmotSetArg(args, num_args, XmNlabelType, XmPIXMAP);
+ }
+ else
+ {
+ ih->data->type = IUP_BUTTON_TEXT;
+ iupmotSetArg(args, num_args, XmNlabelType, XmSTRING);
+ }
+
+ /* Core */
+ iupmotSetArg(args, num_args, XmNmappedWhenManaged, False); /* not visible when managed */
+ iupmotSetArg(args, num_args, XmNx, 0); /* x-position */
+ iupmotSetArg(args, num_args, XmNy, 0); /* y-position */
+ iupmotSetArg(args, num_args, XmNwidth, 10); /* default width to avoid 0 */
+ iupmotSetArg(args, num_args, XmNheight, 10); /* default height to avoid 0 */
+ /* Label */
+ iupmotSetArg(args, num_args, XmNrecomputeSize, False); /* no automatic resize from text */
+ iupmotSetArg(args, num_args, XmNmarginHeight, 0); /* default padding */
+ iupmotSetArg(args, num_args, XmNmarginWidth, 0);
+ iupmotSetArg(args, num_args, XmNmarginTop, 0); /* no extra margins */
+ iupmotSetArg(args, num_args, XmNmarginLeft, 0);
+ iupmotSetArg(args, num_args, XmNmarginBottom, 0);
+ iupmotSetArg(args, num_args, XmNmarginRight, 0);
+ /* PushButton */
+ iupmotSetArg(args, num_args, XmNfillOnArm, False);
+
+ /* Primitive */
+ if (iupAttribGetBoolean(ih, "FOCUSONCLICK"))
+ {
+ if (iupAttribGetBoolean(ih, "CANFOCUS"))
+ iupmotSetArg(args, num_args, XmNtraversalOn, True);
+ else
+ iupmotSetArg(args, num_args, XmNtraversalOn, False);
+ }
+ else
+ iupmotSetArg(args, num_args, XmNtraversalOn, False);
+ iupmotSetArg(args, num_args, XmNhighlightThickness, 2);
+ iupmotSetArg(args, num_args, XmNnavigationType, XmTAB_GROUP);
+
+ ih->handle = XtCreateManagedWidget(
+ iupDialogGetChildIdStr(ih), /* child identifier */
+ xmPushButtonWidgetClass, /* widget class */
+ iupChildTreeGetNativeParentHandle(ih), /* widget parent */
+ args, num_args);
+
+ if (!ih->handle)
+ return IUP_ERROR;
+
+ ih->serial = iupDialogGetChildId(ih); /* must be after using the string */
+
+ XtAddCallback(ih->handle, XmNhelpCallback, (XtCallbackProc)iupmotHelpCallback, (XtPointer)ih);
+
+ value = iupAttribGet(ih, "IMPRESS");
+ if (iupAttribGetBoolean(ih, "FLAT") && !value)
+ {
+ XtVaSetValues(ih->handle, XmNshadowThickness, 0, NULL);
+ XtAddEventHandler(ih->handle, EnterWindowMask, False, (XtEventHandler)motButtonEnterLeaveWindowEvent, (XtPointer)ih);
+ XtAddEventHandler(ih->handle, LeaveWindowMask, False, (XtEventHandler)motButtonEnterLeaveWindowEvent, (XtPointer)ih);
+ }
+ else
+ {
+ if (value && !iupAttribGetStr(ih, "IMPRESSBORDER"))
+ XtVaSetValues(ih->handle, XmNshadowThickness, 0, NULL);
+ else
+ XtVaSetValues(ih->handle, XmNshadowThickness, 2, NULL);
+ XtAddEventHandler(ih->handle, EnterWindowMask, False, (XtEventHandler)iupmotEnterLeaveWindowEvent, (XtPointer)ih);
+ XtAddEventHandler(ih->handle, LeaveWindowMask, False, (XtEventHandler)iupmotEnterLeaveWindowEvent, (XtPointer)ih);
+ }
+
+ XtAddEventHandler(ih->handle, FocusChangeMask, False, (XtEventHandler)iupmotFocusChangeEvent, (XtPointer)ih);
+ XtAddEventHandler(ih->handle, KeyPressMask, False, (XtEventHandler)iupmotKeyPressEvent, (XtPointer)ih);
+
+ XtAddCallback(ih->handle, XmNactivateCallback, (XtCallbackProc)motButtonActivateCallback, (XtPointer)ih);
+ XtAddEventHandler(ih->handle, ButtonPressMask|ButtonReleaseMask, False, (XtEventHandler)iupmotButtonPressReleaseEvent, (XtPointer)ih);
+
+ /* Disable Drag Source */
+ iupmotDisableDragSource(ih->handle);
+
+ /* initialize the widget */
+ XtRealizeWidget(ih->handle);
+
+ if (ih->data->type == IUP_BUTTON_TEXT)
+ iupmotSetString(ih->handle, XmNlabelString, "");
+
+ return IUP_NOERROR;
+}
+
+void iupdrvButtonInitClass(Iclass* ic)
+{
+ /* Driver Dependent Class functions */
+ ic->Map = motButtonMapMethod;
+
+ /* Driver Dependent Attribute functions */
+
+ /* Visual */
+ iupClassRegisterAttribute(ic, "BGCOLOR", iupmotGetBgColorAttrib, motButtonSetBgColorAttrib, IUPAF_SAMEASSYSTEM, "DLGBGCOLOR", IUPAF_DEFAULT);
+ iupClassRegisterAttribute(ic, "BACKGROUND", NULL, motButtonSetBackgroundAttrib, IUPAF_SAMEASSYSTEM, "DLGBGCOLOR", IUPAF_DEFAULT);
+
+ /* Special */
+ iupClassRegisterAttribute(ic, "FGCOLOR", NULL, iupdrvBaseSetFgColorAttrib, IUPAF_SAMEASSYSTEM, "DLGFGCOLOR", IUPAF_DEFAULT);
+ iupClassRegisterAttribute(ic, "TITLE", NULL, motButtonSetTitleAttrib, NULL, NULL, IUPAF_NO_DEFAULTVALUE|IUPAF_NO_INHERIT);
+
+ /* IupButton only */
+ iupClassRegisterAttribute(ic, "ALIGNMENT", NULL, motButtonSetAlignmentAttrib, "ACENTER:ACENTER", NULL, IUPAF_NO_INHERIT); /* force new default value */
+ iupClassRegisterAttribute(ic, "IMAGE", NULL, motButtonSetImageAttrib, NULL, NULL, IUPAF_NO_DEFAULTVALUE|IUPAF_NO_INHERIT);
+ iupClassRegisterAttribute(ic, "IMINACTIVE", NULL, motButtonSetImInactiveAttrib, NULL, NULL, IUPAF_NO_DEFAULTVALUE|IUPAF_NO_INHERIT);
+ iupClassRegisterAttribute(ic, "IMPRESS", NULL, motButtonSetImPressAttrib, NULL, NULL, IUPAF_NO_DEFAULTVALUE|IUPAF_NO_INHERIT);
+ iupClassRegisterAttribute(ic, "FOCUSONCLICK", NULL, NULL, IUPAF_SAMEASSYSTEM, "YES", IUPAF_NO_INHERIT);
+
+ iupClassRegisterAttribute(ic, "PADDING", iupButtonGetPaddingAttrib, motButtonSetPaddingAttrib, IUPAF_SAMEASSYSTEM, "0x0", IUPAF_NOT_MAPPED);
+}