Files
wmaker/WINGs/Tests/mywidget.c
T

198 lines
3.8 KiB
C
Raw Normal View History

1998-09-29 22:36:29 +00:00
/*
1998-11-03 12:53:26 +00:00
* Demo user widget for WINGs
1998-09-29 22:36:29 +00:00
*
1998-11-03 12:53:26 +00:00
* Author: Alfredo K. Kojima
2004-10-12 21:28:27 +00:00
*
1998-11-03 12:53:26 +00:00
* This file is in the public domain.
2004-10-12 21:28:27 +00:00
*
1998-09-29 22:36:29 +00:00
*/
/*
*
* Include the WINGs private data header.
*
2004-10-12 21:28:27 +00:00
*
1998-09-29 22:36:29 +00:00
*/
#include <WINGs/WINGsP.h>
1998-09-29 22:36:29 +00:00
/*
* Our public header.
*/
#include "mywidget.h"
/*
* Define the widget "class"
*/
typedef struct W_MyWidget {
2009-08-20 00:59:40 +02:00
/* these two fields must be present in all your widgets in this
* exact position */
W_Class widgetClass;
WMView *view;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
/* put your stuff here */
char *text;
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
} _MyWidget;
/* some forward declarations */
2009-08-20 00:59:40 +02:00
static void destroyMyWidget(_MyWidget * mPtr);
static void paintMyWidget(_MyWidget * mPtr);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
static void handleEvents(XEvent * event, void *data);
static void handleActionEvents(XEvent * event, void *data);
1998-09-29 22:36:29 +00:00
/*
2004-10-12 21:28:27 +00:00
* Delegates
1998-09-29 22:36:29 +00:00
* See the source for the other widgets to see how to use.
* You won't need to use this most of the time.
*/
static W_ViewDelegate _MyWidgetDelegate = {
2009-08-20 00:59:40 +02:00
NULL,
NULL,
NULL,
NULL,
NULL
1998-09-29 22:36:29 +00:00
};
/* our widget class ID */
static W_Class myWidgetClass = 0;
/*
* Initializer for our widget. Must be called before creating any
* instances of the widget.
*/
2009-08-20 00:59:40 +02:00
W_Class InitMyWidget(WMScreen * scr)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
/* register our widget with WINGs and get our widget class ID */
if (!myWidgetClass) {
myWidgetClass = W_RegisterUserWidget();
}
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
return myWidgetClass;
1998-09-29 22:36:29 +00:00
}
/*
* Our widget fabrication plant.
*/
2009-08-20 00:59:40 +02:00
MyWidget *CreateMyWidget(WMWidget * parent)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
MyWidget *mPtr;
/* allocate some storage for our new widget instance */
mPtr = wmalloc(sizeof(MyWidget));
/* initialize it */
memset(mPtr, 0, sizeof(MyWidget));
/* set the class ID */
mPtr->widgetClass = myWidgetClass;
/*
* Create the view for our widget.
* Note: the Window for the view is only created after the view is
* realized with W_RealizeView()
*
* Consider the returned view as read-only.
*/
mPtr->view = W_CreateView(W_VIEW(parent));
if (!mPtr->view) {
wfree(mPtr);
return NULL;
}
/* always do this */
mPtr->view->self = mPtr;
/* setup the delegates for the view */
mPtr->view->delegate = &_MyWidgetDelegate;
/*
* Intercept some events for our widget, so that we can handle them.
*/
WMCreateEventHandler(mPtr->view, ExposureMask /* this allows us to know when we should paint */
| StructureNotifyMask, /* this allows us to know things like when we are destroyed */
handleEvents, mPtr);
/*
* Intercept some other events. This could be merged with the above
* call, but we separate for more organization.
*/
WMCreateEventHandler(mPtr->view, ButtonPressMask, handleActionEvents, mPtr);
return mPtr;
1998-09-29 22:36:29 +00:00
}
/*
* Paint our widget contents.
*/
2009-08-20 00:59:40 +02:00
static void paintMyWidget(_MyWidget * mPtr)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
W_Screen *scr = mPtr->view->screen;
WMColor *color;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
if (mPtr->text) {
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
color = WMWhiteColor(scr);
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
W_PaintText(mPtr->view, mPtr->view->window, scr->normalFont, 0, 0,
mPtr->view->size.width, WACenter, color, False, mPtr->text, strlen(mPtr->text));
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
WMReleaseColor(color);
}
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
static void handleEvents(XEvent * event, void *data)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
_MyWidget *mPtr = (_MyWidget *) data;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
switch (event->type) {
case Expose:
if (event->xexpose.count != 0)
break;
paintMyWidget(mPtr);
break;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
case DestroyNotify:
destroyMyWidget(mPtr);
break;
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
}
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
static void handleActionEvents(XEvent * event, void *data)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
_MyWidget *mPtr = (_MyWidget *) data;
switch (event->type) {
case ButtonPress:
XBell(mPtr->view->screen->display, 100);
XBell(mPtr->view->screen->display, 100);
break;
}
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
void SetMyWidgetText(MyWidget * mPtr, char *text)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
CHECK_CLASS(mPtr, myWidgetClass);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
if (mPtr->text)
wfree(mPtr->text);
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
mPtr->text = wstrdup(text);
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
if (W_VIEW_MAPPED(mPtr->view)) {
paintMyWidget(mPtr);
}
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
static void destroyMyWidget(_MyWidget * mPtr)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
/*
* Free all data we allocated for our widget.
*/
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
if (mPtr->text)
wfree(mPtr->text);
2004-10-12 21:28:27 +00:00
2009-08-20 00:59:40 +02:00
wfree(mPtr);
1998-09-29 22:36:29 +00:00
}