Add WINGs tutorial
[whome.git] / WINGs_tutorial / NinthWindow.c
blob56802098bd398ca7f4a9f7995485f8e694d45393
1 #include "editmenu.h" /* This must be the MODIFIED .h file */
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <signal.h>
6 #define WINWIDTH 300
7 #define WINHEIGHT 400
8 #define MENUWIDTH 85
9 #define MENITEMHT 21
10 #define LOGPROGRAM "xconsole"
11 #define ERRMSGFIFO "/tmp/WINGsWindowfifo"
12 #define FIFONAMELEN 20
13 #define NOLOGWINDOW (-2) /* value when there is no console window */
14 #define FIFOERROR (-1) /* value when there is a problem w/ console */
15 #define FIFOLOWESTPOSS 0
18 int windowCounter=0;
19 int fifonr;
20 int sibpid;
21 char fifofilename[FIFONAMELEN+5];
23 struct dataStruct{
24 WMWindow *window;
25 WEditMenu *menu;
26 } dataStruct;
29 /* functions for the message window part: */
31 void redirectmsg(int sig){
33 // clean up after SIGCHLD, and set fifonr to flag it
34 fifonr=NOLOGWINDOW;
35 if (!access(fifofilename,F_OK|W_OK))
36 unlink(fifofilename);
37 return;
41 int showMessageWindow(){
43 sprintf(fifofilename,"%s%i",ERRMSGFIFO,(unsigned short)getpid());
45 (void) signal(SIGCHLD,redirectmsg); // clean up if message console is killed
47 if(access(fifofilename,F_OK)==-1)
48 fifonr=mknod(fifofilename,0640|O_EXCL|S_IFIFO,(dev_t)0);
49 else {fifonr=FIFOERROR;
50 wwarning("Fifo file already exists\n");
52 /* fifonr == FIFOERROR if mknod/mkfifo or access failed, mknod returns -1 on failure */
54 if(fifonr!=FIFOERROR){
56 sibpid=fork();
57 if(sibpid==0){
58 execlp(LOGPROGRAM , LOGPROGRAM, "-file",fifofilename,"-geometry","250x400", "-title","Window Messages",(char *)0);
59 exit(1);
60 }else
61 fifonr=open(fifofilename,O_WRONLY);
63 return fifonr;
66 /* general and menu handling functions */
68 void closeAll(WMWidget *self,void *data){
70 WMDestroyWidget(self);
71 if(--windowCounter<1){
72 if (fifonr>=FIFOLOWESTPOSS)
73 kill(sibpid,SIGTERM);
74 if (!access(fifofilename,F_OK|W_OK))
75 unlink(fifofilename);
76 exit(0);
81 void menuItemAction(void *self, void *data){
83 if (fifonr<FIFOLOWESTPOSS)fifonr=showMessageWindow(); // try again in case FIFOERROR
84 if (fifonr==FIFOERROR) // give up and print to stderr
85 fprintf(stderr,"%i: %s selected\n", getpid(), WGetEditMenuItemTitle(self));
86 else{
87 char textbuffer[100];
88 snprintf(textbuffer,100, "%i: %s selected\n", getpid(), WGetEditMenuItemTitle(self));
89 write(fifonr, textbuffer,strlen(textbuffer));
94 void menuItemCloseAction(void *self, void *data){
96 WMPostNotificationName("WMWindowClose", self, NULL);
100 void getMenu(WMWidget *self, void *data){
102 WMPoint position;
103 struct dataStruct *tmp=(struct dataStruct *)data;
105 if(WMGetButtonSelected(self)){
106 position=WMGetViewScreenPosition(WMWidgetView(tmp->window));
107 WEditMenuShowAt(tmp->menu,(position.x>MENUWIDTH)?position.x-MENUWIDTH:0, position.y+MENITEMHT,tmp->window);
108 }else{
109 WEditMenuHide(tmp->menu);
110 WDeselectItem(tmp->menu); // remove selection before next pop up
115 static void notificationHandler(void *self, WMNotification *notif){
117 if(!strcmp("WMWindowClose",WMGetNotificationName(notif)))
118 closeAll(self,NULL);
119 if(!strcmp(WMViewSizeDidChangeNotification,WMGetNotificationName(notif))){
120 //resize actions
121 WMSize size = WMGetViewSize(WMWidgetView(self));
125 /* main widget creating functions */
127 WMWindow * makeMainwindow(Display *display, WMScreen *screen){
128 WMWindow *window;
130 window = WMCreateWindow(screen, "Menu");
131 WMResizeWidget(window, WINWIDTH, WINHEIGHT);
132 WMSetWindowCloseAction(window, closeAll, NULL);
133 WMAddNotificationObserver(notificationHandler, window, "WMWindowClose", WMWidgetView(window));
134 WMSetViewNotifySizeChanges(WMWidgetView(window), True);
135 WMAddNotificationObserver(notificationHandler, window, WMViewSizeDidChangeNotification, WMWidgetView(window));
136 WMAddNotificationObserver(notificationHandler, window, "WMWindowClose", NULL);
137 WMRealizeWidget(window);
138 return window;
142 WEditMenu * makeMenus(WMScreen *screen,WEditMenu *menu, WEditMenu *submenu){
143 WEditMenuItem * menuitem;
145 submenu=WCreateEditMenu(screen,"Submenu");
146 menuitem =WAddMenuItemWithTitle(submenu,"Submenu item");
147 WSetEditMenuItemAction( menuitem, menuItemAction);
148 menuitem =WAddMenuItemWithTitle(submenu,"2nd submenu item");
149 WSetEditMenuItemAction( menuitem, menuItemAction);
150 menuitem =WAddMenuItemWithTitle(submenu,"3d submenu item");
151 WSetEditMenuItemAction( menuitem, menuItemAction);
152 menu=WCreateEditMenu(screen,"Main menu");
153 menuitem = WAddMenuItemWithTitle(menu,"1st main item");
154 WSetEditMenuItemAction( menuitem, menuItemAction);
155 menuitem = WAddMenuItemWithTitle(menu,"2nd main item");
156 WSetEditMenuItemAction( menuitem, menuItemAction);
157 menuitem = WAddMenuItemWithTitle(menu,"To submenu");
158 WSetEditMenuSubmenu(menu, menuitem , submenu);
159 menuitem = WAddMenuItemWithTitle(menu,"Quit");
160 WSetEditMenuItemAction( menuitem, menuItemCloseAction);
161 WMRealizeWidget(submenu);WMRealizeWidget(menu);
162 return menu;
166 WMButton * makeButtonsTop( WMWidget *window, void *AppData){
167 WMButton *Button;
169 Button =WMCreateButton(window,WBTPushOnPushOff);
170 WMSetButtonText (Button, "Menu");
171 WMSetButtonAction (Button, getMenu, AppData);
172 WMMoveWidget(Button, 4,2);
173 WMRealizeWidget(Button);
174 return Button;
178 int main (int argc, char **argv){
180 Display *display;
181 WMScreen *screen;
182 WMWindow *mainwindow;
183 WEditMenu *submenu, *menu;
184 WEditMenuItem * menuitem;
185 struct dataStruct Mainmenu;
186 WMButton *menubutton;
188 fifonr=NOLOGWINDOW;
190 WMInitializeApplication("MenuWindow", &argc, argv);
191 display = XOpenDisplay("");
192 screen = WMCreateScreen(display, DefaultScreen(display));
193 mainwindow= makeMainwindow(display, screen) ;
195 menu=makeMenus(screen,menu,submenu);
197 Mainmenu.window=mainwindow;
198 Mainmenu.menu=menu;
199 menubutton=makeButtonsTop(mainwindow, &Mainmenu);
201 WMMapSubwidgets(mainwindow);
202 WMMapWidget(mainwindow);
204 WMScreenMainLoop(screen);
205 return 0;