1 #include "editmenu.h" /* This must be the MODIFIED .h file */
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;
21 char fifofilename
[FIFONAMELEN
+ 5];
29 /* functions for the message window part: */
31 void redirectmsg(int sig
)
33 // clean up after SIGCHLD, and set fifonr to flag it
35 if (!access(fifofilename
, F_OK
| W_OK
))
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);
51 wwarning("Fifo file already exists\n");
53 /* fifonr == FIFOERROR if mknod/mkfifo or access failed, mknod returns -1 on failure */
55 if (fifonr
!= FIFOERROR
) {
58 execlp(LOGPROGRAM
, LOGPROGRAM
, "-file", fifofilename
, "-geometry", "250x400", "-title", "Window Messages", (char *)0);
61 fifonr
= open(fifofilename
, O_WRONLY
);
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
))
81 void menuItemAction(void *self
, void *data
)
83 if (fifonr
< FIFOLOWESTPOSS
)
84 fifonr
= showMessageWindow(); // try again in case FIFOERROR
86 if (fifonr
== FIFOERROR
) // give up and print to stderr
87 fprintf(stderr
, "%i: %s selected\n", getpid(), WGetEditMenuItemTitle(self
));
91 snprintf(textbuffer
, sizeof(textbuffer
), "%i: %s selected\n", getpid(), WGetEditMenuItemTitle(self
));
92 write(fifonr
, textbuffer
, strlen(textbuffer
));
97 void menuItemCloseAction(void *self
, void *data
)
99 WMPostNotificationName("WMWindowClose", self
, NULL
);
103 void getMenu(WMWidget
*self
, void *data
)
106 struct dataStruct
*tmp
= (struct dataStruct
*) data
;
108 if (WMGetButtonSelected(self
)) {
109 position
= WMGetViewScreenPosition(WMWidgetView(tmp
->window
));
110 WEditMenuShowAt(tmp
->menu
,
111 (position
.x
> MENUWIDTH
)?(position
.x
- MENUWIDTH
):0,
112 position
.y
+ MENITEMHT
, tmp
->window
);
114 WEditMenuHide(tmp
->menu
);
115 WDeselectItem(tmp
->menu
); // remove selection before next pop up
120 static void notificationHandler(void *self
, WMNotification
*notif
)
122 if(!strcmp("WMWindowClose", WMGetNotificationName(notif
)))
123 closeAll(self
, NULL
);
124 if(!strcmp(WMViewSizeDidChangeNotification
, WMGetNotificationName(notif
))) {
126 WMSize size
= WMGetViewSize(WMWidgetView(self
));
130 /* main widget creating functions */
132 WMWindow
* makeMainwindow(Display
*display
, WMScreen
*screen
)
136 window
= WMCreateWindow(screen
, "Menu");
137 WMResizeWidget(window
, WINWIDTH
, WINHEIGHT
);
138 WMSetWindowCloseAction(window
, closeAll
, NULL
);
139 WMAddNotificationObserver(notificationHandler
, window
, "WMWindowClose", WMWidgetView(window
));
140 WMSetViewNotifySizeChanges(WMWidgetView(window
), True
);
141 WMAddNotificationObserver(notificationHandler
, window
, WMViewSizeDidChangeNotification
, WMWidgetView(window
));
142 WMAddNotificationObserver(notificationHandler
, window
, "WMWindowClose", NULL
);
143 WMRealizeWidget(window
);
148 WEditMenu
* makeMenus(WMScreen
*screen
, WEditMenu
*menu
, WEditMenu
*submenu
)
150 WEditMenuItem
*menuitem
;
152 submenu
= WCreateEditMenu(screen
, "Submenu");
153 menuitem
= WAddMenuItemWithTitle(submenu
, "Submenu item");
154 WSetEditMenuItemAction(menuitem
, menuItemAction
);
155 menuitem
= WAddMenuItemWithTitle(submenu
, "2nd submenu item");
156 WSetEditMenuItemAction(menuitem
, menuItemAction
);
157 menuitem
= WAddMenuItemWithTitle(submenu
, "3rd submenu item");
158 WSetEditMenuItemAction(menuitem
, menuItemAction
);
159 menu
= WCreateEditMenu(screen
, "Main menu");
160 menuitem
= WAddMenuItemWithTitle(menu
, "1st main item");
161 WSetEditMenuItemAction(menuitem
, menuItemAction
);
162 menuitem
= WAddMenuItemWithTitle(menu
, "2nd main item");
163 WSetEditMenuItemAction(menuitem
, menuItemAction
);
164 menuitem
= WAddMenuItemWithTitle(menu
, "To submenu");
165 WSetEditMenuSubmenu(menu
, menuitem
, submenu
);
166 menuitem
= WAddMenuItemWithTitle(menu
, "Quit");
167 WSetEditMenuItemAction(menuitem
, menuItemCloseAction
);
168 WMRealizeWidget(submenu
);
169 WMRealizeWidget(menu
);
174 WMButton
* makeButtonsTop(WMWidget
*window
, void *AppData
)
178 Button
= WMCreateButton(window
, WBTPushOnPushOff
);
179 WMSetButtonText(Button
, "Menu");
180 WMSetButtonAction(Button
, getMenu
, AppData
);
181 WMMoveWidget(Button
, 4, 2);
182 WMRealizeWidget(Button
);
187 int main(int argc
, char **argv
)
191 WMWindow
*mainwindow
;
192 WEditMenu
*submenu
, *menu
;
193 WEditMenuItem
*menuitem
;
194 struct dataStruct Mainmenu
;
195 WMButton
*menubutton
;
197 fifonr
= NOLOGWINDOW
;
199 WMInitializeApplication("MenuWindow", &argc
, argv
);
200 display
= XOpenDisplay("");
201 screen
= WMCreateScreen(display
, DefaultScreen(display
));
202 mainwindow
= makeMainwindow(display
, screen
);
204 menu
= makeMenus(screen
, menu
, submenu
);
206 Mainmenu
.window
= mainwindow
;
207 Mainmenu
.menu
= menu
;
208 menubutton
= makeButtonsTop(mainwindow
, &Mainmenu
);
210 WMMapSubwidgets(mainwindow
);
211 WMMapWidget(mainwindow
);
213 WMScreenMainLoop(screen
);