diff options
author | Walter Harms <wharms@bfs.de> | 2023-05-12 23:03:32 +0200 |
---|---|---|
committer | Walter Harms <wharms@bfs.de> | 2023-05-12 23:03:32 +0200 |
commit | 2f99a415913b6be4ed2955a3e30694c2216aafbd (patch) | |
tree | f7edfa31d631eaa30bd5258d40c1b3902c13239f | |
parent | 324358180ddeaae482c5f66bb5647a4918710296 (diff) | |
download | xorg-lib-libXaw-2f99a415913b6be4ed2955a3e30694c2216aafbd.tar.gz |
add examples
provide some examples to support the documentation
-rw-r--r-- | examples/list.c | 52 | ||||
-rw-r--r-- | examples/makefile | 11 | ||||
-rw-r--r-- | examples/menu.c | 62 | ||||
-rw-r--r-- | examples/pane.c | 84 | ||||
-rw-r--r-- | examples/readme.md | 51 | ||||
-rw-r--r-- | examples/repeater.c | 77 | ||||
-rw-r--r-- | examples/scrollbar.c | 72 | ||||
-rw-r--r-- | examples/strip.c | 51 | ||||
-rw-r--r-- | examples/toggle.c | 103 | ||||
-rw-r--r-- | examples/viewport.c | 45 |
10 files changed, 608 insertions, 0 deletions
diff --git a/examples/list.c b/examples/list.c new file mode 100644 index 0000000..8296059 --- /dev/null +++ b/examples/list.c @@ -0,0 +1,52 @@ +#include <stdlib.h> +#include <stdio.h> +#include <StringDefs.h> +#include <Xaw/Box.h> +#include <Xaw/List.h> +#include <Xaw/Viewport.h> +#include <Xaw/Command.h> + +void quit_CB(Widget w, XtPointer closure, XtPointer call_data) +{ + Widget lst=closure; + XawListReturnStruct *ret; + + ret=XawListShowCurrent(lst); + + if (ret->string[0] != 0) + printf("selected:%s\n",ret->string); + + XtAppSetExitFlag(XtWidgetToApplicationContext(w)); +} + +int main(int argc, char **argv) +{ + Widget toplevel,box,list,cmd,view; + XtAppContext app_con; + toplevel = XtAppInitialize(&app_con, "Hello", NULL, 0, + &argc, argv, NULL, + NULL, 0); + char *items[]={ "the ","quick","brown", "fox ", + "jumps","Over","the","lazy","dog", + NULL}; + + box = XtCreateManagedWidget("box", boxWidgetClass, toplevel, NULL, 0); + cmd=XtCreateManagedWidget("exit", commandWidgetClass, box, NULL, 0); + + view=XtVaCreateManagedWidget("viewport", viewportWidgetClass, box, + XtNallowVert, 1, + XtNforceBars,1, + NULL); + + list= XtVaCreateManagedWidget("listbox", listWidgetClass,view, + XtNlist,items, + XtNdefaultColumns,1, + XtNnumberStrings,4, + XtNforceColumns,1, + NULL); + XawListChange(list,items,0,0,0); + XtAddCallback(cmd, XtNcallback, quit_CB , list); + XtRealizeWidget(toplevel); + XtAppMainLoop(app_con); + exit(0); +} diff --git a/examples/makefile b/examples/makefile new file mode 100644 index 0000000..b65abd1 --- /dev/null +++ b/examples/makefile @@ -0,0 +1,11 @@ +#future version should use autoconf +CC=gcc +CFLAGS=-Wall -g -I/usr/include/X11 +LDFLAGS=-L. +LOADLIBES= -lX11 -lXt -lXaw +FILES=viewport toggle strip scrollbar repeater pane menu list + +all: $(FILES) + +clean: + $(RM) *.o $(FILES) diff --git a/examples/menu.c b/examples/menu.c new file mode 100644 index 0000000..a9126bf --- /dev/null +++ b/examples/menu.c @@ -0,0 +1,62 @@ +#include <stdlib.h> +#include <stdio.h> +#include <X11/StringDefs.h> +#include <Xaw/Box.h> +#include <Xaw/Command.h> +#include <Xaw/SimpleMenu.h> +#include <Xaw/MenuButton.h> +#include <Xaw/Sme.h> +#include <Xaw/SmeLine.h> +#include <Xaw/SmeBSB.h> + +static void +quit_cb(Widget w, XtPointer client_data, XtPointer call_data) +{ + XtAppSetExitFlag(XtWidgetToApplicationContext(w)); +} + +void setupmenu(Widget top) +{ + Widget button, menu; + char buf[256]; + int i; + + button = XtVaCreateManagedWidget("menuButton", menuButtonWidgetClass, top, + NULL); + + menu = XtVaCreatePopupShell("menu", simpleMenuWidgetClass, + button, NULL); + + for (i = 1; i < 6; i++) { + sprintf(buf, "menuEntry%d", i ); + (void) XtVaCreateManagedWidget(buf, smeBSBObjectClass, menu, NULL); + if (i == 2) + (void) XtVaCreateManagedWidget("menuButton", smeLineObjectClass, + menu, NULL); + } + +} + +int main(int argc, char **argv) +{ + Widget toplevel,box,command; + XtAppContext app_con; + toplevel = XtAppInitialize(&app_con, "demo", NULL, 0, + &argc, argv, NULL, + NULL, 0); + + box = XtCreateManagedWidget("box", boxWidgetClass, toplevel, NULL, 0); + + command = XtVaCreateManagedWidget("cmd", + commandWidgetClass, box, + XtNlabel, "EXIT", + NULL); + + XtAddCallback(command, XtNcallback, quit_cb, NULL); + + setupmenu(box); + + XtRealizeWidget(toplevel); + XtAppMainLoop(app_con); + exit(0); +} diff --git a/examples/pane.c b/examples/pane.c new file mode 100644 index 0000000..4ec01de --- /dev/null +++ b/examples/pane.c @@ -0,0 +1,84 @@ +#include <stdlib.h> +#include <stdio.h> +#include <X11/StringDefs.h> +#include <Xaw/Box.h> +#include <Xaw/Label.h> +#include <Xaw/Paned.h> +#include <Xaw/Command.h> + +static void +quit_cb(Widget w, XtPointer client_data, XtPointer call_data) +{ + XtAppSetExitFlag(XtWidgetToApplicationContext(w)); +} + +static void +getnumsub_cb(Widget w, XtPointer client_data, XtPointer call_data) +{ + Widget p=XtParent(w); + char buf[80]; + int min,max; + XawPanedGetMinMax(w,&min,&max); + + sprintf(buf,"%d number\n%d min %d max\n",XawPanedGetNumSub(p),min,max); + XtVaSetValues(w,XtNlabel,buf,NULL); + +} +static void +refigure_cb(Widget w, XtPointer client_data, XtPointer call_data) +{ + Widget p=XtParent(w); + XawPanedSetRefigureMode(p,False); +} + +int main(int argc, char **argv) +{ + Widget toplevel,pane,label,command; + XtAppContext app_con; + toplevel = XtAppInitialize(&app_con, "demo", NULL, 0, + &argc, argv, NULL, + NULL, 0); + + pane=XtVaCreateManagedWidget( "paned", panedWidgetClass, toplevel, + NULL); + + label = XtVaCreateManagedWidget("Label", + labelWidgetClass, + pane, XtNlabel, "quick", NULL); + + XawPanedAllowResize(label,False); + + label = XtVaCreateManagedWidget("Label", + labelWidgetClass, + pane, XtNlabel, "brown", NULL); + + label = XtVaCreateManagedWidget("Label", + labelWidgetClass, + pane, XtNlabel, "fox", NULL); + + command = XtVaCreateManagedWidget("cmd", + commandWidgetClass, pane, + XtNlabel, "stop resize", + NULL); + + XtAddCallback(command, XtNcallback,refigure_cb, NULL); + + command = XtVaCreateManagedWidget("cmd", + commandWidgetClass, pane, + XtNlabel, "getnumsub", + NULL); + + XtAddCallback(command, XtNcallback,getnumsub_cb, NULL); + XawPanedSetMinMax(command,50,100); + + command = XtVaCreateManagedWidget("cmd", + commandWidgetClass, pane, + XtNlabel, "EXIT", + NULL); + XtAddCallback(command, XtNcallback, quit_cb, NULL); + + XtRealizeWidget(toplevel); + + XtAppMainLoop(app_con); + exit(0); +} diff --git a/examples/readme.md b/examples/readme.md new file mode 100644 index 0000000..06ae9ad --- /dev/null +++ b/examples/readme.md @@ -0,0 +1,51 @@ + + Copyright (c) 2023 by The XFree86 Project, Inc. + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF + OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + +The examples are intended to test the diffent xaw widgets. +Every program should provide an example for one widget and if possible +also support functions. Everyone is invited to add more. +The progras make use of label and/or command widget so there is no +extra program to show it use. All programs support the standard libXt +command line options. + +list + select one or none from the list + +menu + press the menubutton and see the menu + +pane + see the use of a pane widget + +repeater + the repeater widget, press the button and keep it pressed + +scrollbar + see the scrollbar moving relativ and absolut + +strip + a dynamic widget showing a simple graphic + +toggle + select, unselect one of many buttons + +viewport + shrink the window, select any of the buttons diff --git a/examples/repeater.c b/examples/repeater.c new file mode 100644 index 0000000..f6b1f59 --- /dev/null +++ b/examples/repeater.c @@ -0,0 +1,77 @@ +#include <stdlib.h> +#include <stdio.h> +#include <X11/StringDefs.h> +#include <Xaw/Box.h> +#include <Xaw/Command.h> +#include <Xaw/Repeater.h> + +static int counter; + +static void +quit_cb(Widget w, XtPointer client_data, XtPointer call_data) +{ + XtAppSetExitFlag(XtWidgetToApplicationContext(w)); +} + +static void +start_cb(Widget w, XtPointer ptr, XtPointer arg) +{ +#ifdef DEBUG + puts("Start"); +#endif +} + +static void +do_cb(Widget w, XtPointer ptr, XtPointer arg) +{ + char buf[10]; + Widget show= (Widget)ptr; + counter++; + snprintf(buf,10,"%d",counter); + XtVaSetValues(show,XtNlabel,buf,NULL); +#ifdef DEBUG + puts("counter"); +#endif +} + + +static void +stop_cb(Widget w, XtPointer ptr, XtPointer arg) +{ + counter=0; +#ifdef DEBUG + puts("reset counter"); +#endif +} + +int main(int argc, char **argv) +{ + Widget toplevel,box,command,rep; + XtAppContext app_con; + toplevel = XtAppInitialize(&app_con, "demo", NULL, 0, + &argc, argv, NULL, + NULL, 0); + + box = XtCreateManagedWidget("box", boxWidgetClass, toplevel, NULL, 0); + + command = XtVaCreateManagedWidget("cmd", + commandWidgetClass, box, + XtNlabel, "EXIT", + NULL); + XtAddCallback(command, XtNcallback, quit_cb, NULL); + + rep=XtVaCreateManagedWidget("Repeater", + repeaterWidgetClass, + box, XtNlabel,"hitme",NULL); + + XtAddCallback(rep,XtNstartCallback,start_cb,command); + + XtAddCallback(rep,XtNcallback,do_cb,command); + + XtAddCallback(rep,XtNstopCallback,stop_cb,command); + + XtRealizeWidget(toplevel); + + XtAppMainLoop(app_con); + exit(0); +} diff --git a/examples/scrollbar.c b/examples/scrollbar.c new file mode 100644 index 0000000..c0b3536 --- /dev/null +++ b/examples/scrollbar.c @@ -0,0 +1,72 @@ +#define _GNU_SOURCE +#include <stdlib.h> +#include <stdio.h> +#include <X11/StringDefs.h> +#include <Xaw/Box.h> +#include <Xaw/Label.h> +#include <Xaw/Scrollbar.h> +#include <Xaw/Command.h> + +static void +quit_cb(Widget w, XtPointer client_data, XtPointer call_data) +{ + XtAppSetExitFlag(XtWidgetToApplicationContext(w)); +} + +static void +scroll_cb(Widget widget,XtPointer label_ptr, XtPointer value_ptr) +{ + long value = (long)value_ptr; + Widget label = (Widget) label_ptr; + char *msg=NULL; + asprintf(&msg, "%ld", value); + XtVaSetValues( label, XtNlabel, msg, NULL); + free(msg); +} +static void +jump_cb(Widget widget,XtPointer label_ptr, XtPointer value_ptr) +{ + float value = *(float *)value_ptr; + Widget label = (Widget) label_ptr; + char *msg=NULL; + asprintf(&msg, "%lf", value); + XtVaSetValues( label, XtNlabel, msg, NULL); + free(msg); +} + +int main(int argc, char **argv) +{ + Widget toplevel,box,label,scroll,command; + XtAppContext app_con; + toplevel = XtAppInitialize(&app_con, "demo", NULL, 0, + &argc, argv, NULL, + NULL, 0); + + box = XtCreateManagedWidget("box", boxWidgetClass, toplevel, NULL, 0); + + label = XtVaCreateManagedWidget("Label", + labelWidgetClass, + box, XtNlabel, "hello", NULL); + + command = XtVaCreateManagedWidget("cmd", + commandWidgetClass, box, + XtNlabel, "EXIT", + NULL); + + XtAddCallback(command, XtNcallback, quit_cb, NULL); + + scroll = XtVaCreateManagedWidget("scroller", + scrollbarWidgetClass, box, + XtNorientation,XtorientHorizontal, + XtNlength,500, + XtNwidth,100, + NULL); + + XtAddCallback(scroll, XtNscrollProc, scroll_cb, label); + XtAddCallback(scroll,XtNjumpProc,jump_cb,label); + + XtRealizeWidget(toplevel); + + XtAppMainLoop(app_con); + exit(0); +} diff --git a/examples/strip.c b/examples/strip.c new file mode 100644 index 0000000..1cf5efe --- /dev/null +++ b/examples/strip.c @@ -0,0 +1,51 @@ +#include <stdio.h> +#include <stdlib.h> +#include <X11/StringDefs.h> +#include <Xaw/Box.h> +#include <Xaw/Command.h> +#include <Xaw/StripChart.h> + +static void +quit_cb(Widget w, XtPointer client_data, XtPointer call_data) +{ + XtAppSetExitFlag(XtWidgetToApplicationContext(w)); +} + +static void +getvalue_cb(Widget widget,XtPointer label_ptr, XtPointer value_ptr) +{ + static double dbl=0,delta=0.1; + double *ret = (double *)value_ptr; + + dbl=dbl+delta; + if (dbl >= 1.0 || dbl <= 0 ) { delta = delta*-1.0 ; } + *ret=dbl; +} + +int main(int argc, char **argv) +{ + Widget toplevel,box,chart,command; + XtAppContext app_con; + toplevel = XtAppInitialize(&app_con, "demo", NULL, 0, + &argc, argv, NULL, + NULL, 0); + + box = XtCreateManagedWidget("box", boxWidgetClass, toplevel, NULL, 0); + + command = XtVaCreateManagedWidget("cmd", + commandWidgetClass, box, + XtNlabel, "EXIT", + NULL); + + chart = XtVaCreateManagedWidget("chart", + stripChartWidgetClass, box, + XtNupdate, 1, + NULL); + + XtAddCallback(chart,XtNgetValue,getvalue_cb,NULL); + XtAddCallback(command, XtNcallback, quit_cb, NULL); + + XtRealizeWidget(toplevel); + XtAppMainLoop(app_con); + exit(0); +} diff --git a/examples/toggle.c b/examples/toggle.c new file mode 100644 index 0000000..2d7e88b --- /dev/null +++ b/examples/toggle.c @@ -0,0 +1,103 @@ +#include <stdio.h> +#include <stdlib.h> +#include <X11/StringDefs.h> +#include <Xaw/Box.h> +#include <Xaw/Command.h> +#include <Xaw/Toggle.h> + +static Widget toggle[3]; +static Widget radio_group; + +static void +quit_cb(Widget w, XtPointer client_data, XtPointer call_data) +{ + XtAppSetExitFlag(XtWidgetToApplicationContext(w)); +} + +static void +getdata_cb(Widget w, XtPointer client_data, XtPointer call_data) +{ + char *radio_data=XawToggleGetCurrent(radio_group); + if (radio_data) + printf("radio_data=%s\n",radio_data); + else + printf("radio_data=%s\n","nodata"); +} + +static void +unset_cb(Widget w, XtPointer client_data, XtPointer call_data) +{ + XawToggleUnsetCurrent(radio_group); +} + + +static void +reset_cb(Widget w, XtPointer client_data, XtPointer call_data) +{ + XawToggleSetCurrent(radio_group,"3397"); +} + + +int main(int argc, char **argv) +{ + Widget toplevel,box,command; + XtAppContext app_con; + + toplevel = XtAppInitialize(&app_con, "demo", NULL, 0, + &argc, argv, NULL, + NULL, 0); + + box = XtCreateManagedWidget("box", boxWidgetClass, toplevel, NULL, 0); + + command = XtVaCreateManagedWidget("cmd", + commandWidgetClass, box, + XtNlabel, "EXIT", + NULL); + + toggle[0]=XtVaCreateManagedWidget("toggle", + toggleWidgetClass, box, +// XtNradioData,radioname, + XtNradioGroup,radio_group, + XtNlabel, "track", + NULL); + radio_group=toggle[0]; + toggle[1]=XtVaCreateManagedWidget("toggle", + toggleWidgetClass, box, +// XtNradioData,radioname, + XtNradioGroup,radio_group, + XtNlabel, "trick", + NULL); + + toggle[2]=XtVaCreateManagedWidget("toggle", + toggleWidgetClass, box, + XtNradioData,"3397", + XtNradioGroup,radio_group, + XtNlabel, "tick", + NULL); + + XtAddCallback(command, XtNcallback, quit_cb, NULL); + + command = XtVaCreateManagedWidget("getcurrent", + commandWidgetClass, box, + XtNlabel, "say ", + NULL); + XtAddCallback(command, XtNcallback, getdata_cb, NULL); + + command = XtVaCreateManagedWidget("unsetcurrent", + commandWidgetClass, box, + XtNlabel, "unset ", + NULL); + XtAddCallback(command, XtNcallback, unset_cb, NULL); + + + command = XtVaCreateManagedWidget("setnew", + commandWidgetClass, box, + XtNlabel, "setnew", + NULL); + + XtAddCallback(command, XtNcallback, reset_cb, NULL); + + XtRealizeWidget(toplevel); + XtAppMainLoop(app_con); + exit(0); +} diff --git a/examples/viewport.c b/examples/viewport.c new file mode 100644 index 0000000..890e9f9 --- /dev/null +++ b/examples/viewport.c @@ -0,0 +1,45 @@ +#include <stdlib.h> +#include <stdio.h> +#include <X11/StringDefs.h> +#include <Xaw/Viewport.h> +#include <Xaw/Command.h> +#include <Xaw/Box.h> +static void +quit_cb(Widget w, XtPointer client_data, XtPointer call_data) +{ + XtAppSetExitFlag(XtWidgetToApplicationContext(w)); +} + +int main(int argc, char **argv) +{ + Widget toplevel,viewport,box,cmd; + XtAppContext app_con; + int i; + char buf[80]; + toplevel = XtAppInitialize(&app_con, "demo", NULL, 0, + &argc, argv, NULL, + NULL, 0); + + viewport = XtVaCreateManagedWidget("view", viewportWidgetClass, toplevel, + XtNforceBars,True, + XtNallowVert,True, + NULL); + + box = XtVaCreateManagedWidget("box", boxWidgetClass, viewport, + XtNvSpace,10, + NULL); + + for (i=0;i<10;i++){ + sprintf(buf,"label%02d",i); + cmd=XtVaCreateManagedWidget("cmd", + commandWidgetClass, + box, XtNlabel, buf, NULL); + XtAddCallback(cmd, XtNcallback, quit_cb, NULL); + } + + XtRealizeWidget(toplevel); + + + XtAppMainLoop(app_con); + exit(0); +} |