2 * Copyright 2022 Olaf Wintermann
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
33 static MainWindow *main_window;
35 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *args, int nargs);
37 static void FileOpenCB(Widget w, void *udata, void *cdata);
38 static void ViewFullscreenCB(Widget w, void *udata, void *cdata);
40 static void window_close_handler(Widget window, void *udata, void *cdata) {
44 static unsigned int keycodeF;
46 static void windowKeyEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
47 MainWindow *win = data;
48 if(win->fullscreen && event->xkey.keycode == keycodeF) {
49 WindowFullscreen(main_window, FALSE);
54 MainWindow* WindowCreate(Display *display) {
58 MainWindow *window = malloc(sizeof(MainWindow));
59 memset(window, 0, sizeof(MainWindow));
64 XtSetArg(args[n], XmNtitle, APP_NAME); n++;
65 window->window = XtAppCreateShell(
68 applicationShellWidgetClass,
69 //vendorShellWidgetClass,
75 Atom wm_delete_window;
76 wm_delete_window = XmInternAtom(
80 XmAddWMProtocolCallback(
88 XtSetArg(args[n], XmNwidth, 360); n++;
89 XtSetArg(args[n], XmNheight, 220); n++;
90 XtSetArg(args[n], XmNshadowThickness, 0); n++;
91 Widget container = XmCreateForm(window->window, "form", args, n);
92 XtManageChild(container);
93 XtAddEventHandler(container, KeyPressMask, FALSE, windowKeyEH, window);
96 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
97 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
98 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
99 WindowCreateMenu(window, container, args, n);
102 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
103 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
104 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
105 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
106 XtSetArg(args[n], XmNtopWidget, window->menubar); n++;
107 XtSetArg(args[n], XmNbackground, BlackPixelOfScreen(XtScreen(window->window))); n++;
108 window->player_widget = XmCreateDrawingArea(container, "player", args, n);
109 XtManageChild(window->player_widget);
112 keycodeF = XKeysymToKeycode(XtDisplay(window->window), XStringToKeysym("F"));
117 void WindowShow(MainWindow *win) {
118 XtRealizeWidget(win->window);
122 * Creates a XmPushButton menu item
124 static Widget createMenuItem(
129 const char *accelerator,
130 char *accelerator_text,
131 XtCallbackProc callback,
137 XmString s1 = XmStringCreateSimple(label);
138 XtSetArg(args[n], XmNlabelString, s1); n++;
139 XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
141 if(accelerator && accelerator_text) {
142 at = XmStringCreateSimple(accelerator_text);
143 XtSetArg(args[n], XmNaccelerator, accelerator); n++;
144 XtSetArg(args[n], XmNacceleratorText, at); n++;
147 Widget menuItem = XmCreatePushButtonGadget(menu, name, args, n);
148 XtManageChild(menuItem);
150 if(at) XmStringFree(at);
153 XtAddCallback(menuItem, XmNactivateCallback, (XtCallbackProc)callback, cbData);
159 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *mbargs, int nmbargs) {
160 Widget menubar = XmCreateMenuBar(parent, "menubar", mbargs, nmbargs);
161 XtManageChild(menubar);
162 win->menubar = menubar;
168 XmString s = XmStringCreateSimple("File");
169 Widget fileMenuItem = XtVaCreateManagedWidget(
171 xmCascadeButtonWidgetClass,
176 Widget fileMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 0, NULL, NULL);
178 s = XmStringCreateSimple("Playback");
179 Widget playMenuItem = XtVaCreateManagedWidget(
181 xmCascadeButtonWidgetClass,
186 Widget playMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 1, NULL, NULL);
188 s = XmStringCreateSimple("View");
189 Widget viewMenuItem = XtVaCreateManagedWidget(
191 xmCascadeButtonWidgetClass,
196 Widget viewMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 2, NULL, NULL);
199 createMenuItem(fileMenu, "fileOpen", "Open...", 'O', "Ctrl<Key>O", "Ctrl+O", FileOpenCB, NULL);
202 createMenuItem(viewMenu, "viewFullscreen", "Fullscreen", 'F', "<Key>F", "F", ViewFullscreenCB, NULL);
205 void go_fullscreen(Display *dsp, Window win)
208 Atom wm_state = XInternAtom(dsp, "_NET_WM_STATE", False);
209 Atom fullscreen = XInternAtom(dsp, "_NET_WM_STATE_FULLSCREEN", False);
210 memset(&xev, 0, sizeof(xev));
211 xev.type = ClientMessage;
212 xev.xclient.window = win;
213 xev.xclient.message_type = wm_state;
214 xev.xclient.format = 32;
215 xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
216 xev.xclient.data.l[1] = fullscreen;
217 xev.xclient.data.l[2] = 0;
218 XSendEvent(dsp, DefaultRootWindow(dsp), False,
219 SubstructureNotifyMask, &xev);
222 static Atom net_wm_state;
223 static Atom net_wm_state_fullscreen;
224 static int net_wm_atoms_initialized = 0;
226 void WindowFullscreen(MainWindow *win, bool enableFullscreen) {
227 Display *dpy = XtDisplay(win->window);
229 // init net_wm_state atoms
230 if(!net_wm_atoms_initialized) {
231 net_wm_state = XInternAtom(dpy, "_NET_WM_STATE", False);
232 net_wm_state_fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
233 net_wm_atoms_initialized = 1;
236 WindowMenubarSetVisible(win, !enableFullscreen);
237 if(enableFullscreen && !win->fullscreen) {
238 XtUnmanageChild(main_window->menubar);
239 main_window->fullscreen = TRUE;
240 } else if(!enableFullscreen && win->fullscreen) {
241 XtManageChild(main_window->menubar);
242 main_window->fullscreen = FALSE;
246 memset(&ev, 0, sizeof(XEvent));
247 ev.type = ClientMessage;
248 ev.xclient.window = XtWindow(win->window);
249 ev.xclient.message_type = net_wm_state;
250 ev.xclient.format = 32;
251 ev.xclient.data.l[0] = enableFullscreen ? 1 : 0;
252 ev.xclient.data.l[1] = net_wm_state_fullscreen;
253 ev.xclient.data.l[2] = 0;
254 XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureNotifyMask | SubstructureRedirectMask, &ev);
257 void WindowMenubarSetVisible(MainWindow *win, bool visible) {
259 XtManageChild(main_window->menubar);
260 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, main_window->menubar, NULL);
262 XtUnmanageChild(main_window->menubar);
263 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_FORM, NULL);
269 static void filedialog_end(
272 XmFileSelectionBoxCallbackStruct *selection)
274 XtUnmanageChild(widget);
275 XtDestroyWidget(widget);
278 static void filedialog_select(
281 XmFileSelectionBoxCallbackStruct *selection)
284 if(selection->value) {
285 XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &value);
291 // no need to free the value, because it is stored in MainWindow
293 PlayerOpenFile(data);
296 filedialog_end(widget, data, NULL);
302 static void FileOpenCB(Widget w, void *udata, void *cdata) {
303 MainWindow *win = main_window;
308 XtSetArg(args[n], XnNshowViewMenu, 1); n++;
309 Widget dialog = XnCreateFileSelectionDialog(win->window, "dialog", args, n);
310 XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)filedialog_select, win);
311 XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)filedialog_end, win);
313 Widget dirUp = XnFileSelectionBoxGetChild(dialog, XnFSB_DIR_UP_BUTTON);
314 XtUnmanageChild(dirUp);
316 XtManageChild(dialog);
319 static void ViewFullscreenCB(Widget w, void *udata, void *cdata) {
320 if(main_window->fullscreen) {
321 WindowFullscreen(main_window, FALSE);
323 WindowFullscreen(main_window, TRUE);