621ada78a118bb3ae30040a43c2441fbb66ca7ee
[uwplayer.git] / application / window.c
1 /*
2  * Copyright 2022 Olaf Wintermann
3  *
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:
10  * 
11  * The above copyright notice and this permission notice shall be included in 
12  * all copies or substantial portions of the Software.
13  *
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.
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "window.h"
28 #include "main.h"
29 #include "player.h"
30 #include "playlist.h"
31 #include "xdnd.h"
32
33 #include "Fsb.h"
34 #include "Sidebar.h"
35
36 static MainWindow *main_window;
37
38 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *args, int nargs);
39
40 static void FileOpenCB(Widget w, void *udata, void *cdata);
41 static void FileQuitCB(Widget w, void *udata, void *cdata);
42 static void PlayRepeatCB(Widget w, void *udata, void *cdata);
43 static void PlayRepeatListCB(Widget w, void *udata, void *cdata);
44 static void PlayAutoPlayCB(Widget w, void *udata, void *cdata);
45 static void PlayRandomCB(Widget w, void *udata, void *cdata);
46 static void ViewFullscreenCB(Widget w, void *udata, void *cdata);
47 static void ViewSidebarCB(Widget w, void *udata, void *cdata);
48 static void ViewAdjustWindowSizeCB(Widget w, void *udata, void *cdata);
49
50 static void WindowRealized(MainWindow *win);
51
52 static int blank_cursor_init = 0;
53 static Pixmap blank_cursor_pixmap;
54 static Cursor blank_cursor;
55
56 static void init_blank_cursor(Widget w) {
57     char data = 0;
58     
59     XColor c;
60     
61     blank_cursor_pixmap = XCreateBitmapFromData(XtDisplay(w), XtWindow(w), &data, 1, 1);
62     if(!blank_cursor_pixmap) return;
63     
64     blank_cursor = XCreatePixmapCursor(XtDisplay(w), blank_cursor_pixmap, blank_cursor_pixmap, &c, &c, 0, 0);
65     
66     XFreePixmap(XtDisplay(w), blank_cursor_pixmap);
67     blank_cursor_init = 1;
68 }
69
70 static void window_close_handler(Widget window, void *udata, void *cdata) {
71     FileQuitCB(window, NULL, NULL);
72 }
73
74 static unsigned int keycodeF;
75
76 static void windowKeyEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
77     MainWindow *win = data;
78     if(win->fullscreen && event->xkey.keycode == keycodeF) {
79         WindowFullscreen(main_window, FALSE);
80         *dispatch = FALSE;
81     }
82 }
83
84 static int main_window_is_realized = 0;
85
86 static void resizeEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {  
87     if(!main_window_is_realized) {
88         if(XtIsRealized(widget)) {
89             main_window_is_realized = 1;
90             WindowRealized(data);
91         }
92     }
93     WindowAdjustAspectRatio(data);
94 }
95
96 static void WindowRealized(MainWindow *win) {
97     char *open_file = GetOpenFileArg();
98     if(open_file) {
99         PlayListAddFile(win, open_file);
100         PlayListPlayNext(win, true);
101         CleanOpenFileArg();
102     }
103     
104     if(!blank_cursor_init) {
105         init_blank_cursor(win->player_widget);
106     }
107     
108     XdndEnable(win->window);
109 }
110
111 static void playerWidgetInputCB(Widget widget, XtPointer u, XtPointer c) {
112     MainWindow *win = u;
113     XmDrawingAreaCallbackStruct *cb = c;
114     
115     if(win->player && win->player->isactive) {
116         PlayerHandleInput(win, win->player, cb);
117     }
118 }
119
120 static void windowGrabButton(MainWindow *win) {
121     //printf("grab\n");
122     XtGrabButton(
123                 win->player_widget,
124                 AnyButton,
125                 AnyModifier,
126                 True,
127                 ButtonPressMask | ButtonReleaseMask | PointerMotionMask | FocusChangeMask | EnterWindowMask | LeaveWindowMask,
128                 GrabModeAsync,
129                 GrabModeAsync,
130                 None,
131                 None);
132     win->buttongrab = True;
133 }
134
135 static void playerEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
136     MainWindow *win = data;
137     int etype = event->type;
138     
139     ///*
140     if(etype == EnterNotify) {
141         //printf("enter\n");
142         windowGrabButton(win);
143         return;
144     }
145     if(etype == LeaveNotify) {
146         //printf("leave\n");
147         //XtUngrabButton(win->player_widget, AnyButton, AnyModifier); 
148         //win->buttongrab = False;
149         return;
150     }
151     
152     int pass = 0;
153     if(etype == ButtonPress || etype == ButtonRelease || etype == KeyPress || etype == KeyRelease) {
154         //printf("button press\n");
155         pass = 1;
156     }
157     
158     if(!win->player || win->player->window == 0) return;
159     
160     WindowHandlePlayerEvent(win, event);
161     
162     if(pass) {
163         // redirect key events to the player window
164         //printf("redirect\n");
165         event->xkey.window = win->player->window;
166         XSendEvent(
167                 XtDisplay(win->player_widget),
168                 win->player->window,
169                 True,
170                 0,
171                 event);
172     }
173 }
174
175 #define IGNORE_MOTION_THRESHOLD_MS 1000
176 #define MOTION_POS_THRESHOLD_PIX   5
177 #define OSD_BOTTOM_THRESHOLD       0.09
178
179 #define DOUBLE_CLICK_TIME_MS       500
180
181 void WindowHandlePlayerEvent(MainWindow *win, XEvent *event) {
182     // event handler for intercepted player mouse events
183     // win->player is not NULL
184     
185     int etype = event->type;
186     
187     if(etype == MotionNotify) {
188         Time cur_motion_time = event->xmotion.time;
189         if(win->player) {
190             win->motion_playback_time = win->player->playback_time;
191         }
192         
193         int x = event->xmotion.x_root;
194         int y = event->xmotion.y_root;
195         if(win->cursorhidden && cur_motion_time - win->player_event_time < IGNORE_MOTION_THRESHOLD_MS) {
196             int diff_x = abs(x - win->mouse_x);
197             int diff_y = abs(y - win->mouse_y);
198             if(diff_x > MOTION_POS_THRESHOLD_PIX || diff_y > MOTION_POS_THRESHOLD_PIX) {
199                 WindowShowPlayerCursor(win);
200             }
201         } else {
202             win->mouse_x = x;
203             win->mouse_y = y;
204         }
205         win->player_event_time = cur_motion_time;
206         win->motion_playback_time = win->player->playback_time;
207         
208         
209         
210         if(win->pwbuttonpressed) {
211             Display *dp = XtDisplay(win->window);
212                 
213             XtUngrabPointer(win->player_widget, CurrentTime);
214
215             XEvent xev;
216             memset(&xev, 0, sizeof(xev));
217             xev.type = ClientMessage;
218             xev.xclient.message_type = XInternAtom(dp, "_NET_WM_MOVERESIZE", False);
219             xev.xclient.window = XtWindow(win->window);
220             xev.xclient.format = 32;
221             xev.xclient.data.l[0] = x;
222             xev.xclient.data.l[1] = y;
223             xev.xclient.data.l[2] = 8; // _NET_WM_MOVERESIZE_MOVE
224             xev.xclient.data.l[3] = 1; // button1
225             xev.xclient.data.l[4] = 1; // source indication
226
227             XSendEvent(dp, DefaultRootWindow(dp), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
228
229             win->pwbuttonpressed = FALSE;  
230         }
231     } else if(etype == ButtonPress) {
232         Time t = event->xbutton.time;
233         
234         int yi = win->player_widget->core.height - event->xbutton.y;
235         if((float)yi/(float)win->player_widget->core.height < OSD_BOTTOM_THRESHOLD) {
236             win->button_press_time = 0;
237         } else {
238             if(t - win->button_press_time < DOUBLE_CLICK_TIME_MS) {
239                 // double click
240                 WindowFullscreen(main_window, !win->fullscreen);
241                 win->button_press_time = 0;
242             } else {
243                 win->button_press_time = t;
244             }
245             win->pwbuttonpressed = 1;
246         }
247     } else if(etype == ButtonRelease) {
248         win->player_event_time = event->xbutton.time;
249         win->pwbuttonpressed = FALSE;
250     }
251 }
252
253
254
255 MainWindow* WindowCreate(Display *display) {
256     Arg args[32];
257     int n;
258      
259     MainWindow *window = malloc(sizeof(MainWindow));
260     memset(window, 0, sizeof(MainWindow));
261     main_window = window;
262       
263     // toplevel window
264     n = 0;
265     XtSetArg(args[n], XmNtitle, APP_NAME); n++;
266     window->window = XtAppCreateShell(
267             APP_NAME,
268             APP_CLASS,
269             applicationShellWidgetClass,
270             //vendorShellWidgetClass,
271             display,
272             args,
273             n);
274     
275     // close handler
276     Atom wm_delete_window;
277     wm_delete_window = XmInternAtom(
278             display,
279             "WM_DELETE_WINDOW",
280             0);
281     XmAddWMProtocolCallback(
282             window->window,
283             wm_delete_window,
284             window_close_handler,
285             window);
286     
287     // resize handler
288     XtAddEventHandler(window->window, StructureNotifyMask, False, resizeEH, window);
289     
290     n = 0;
291     XtSetArg(args[n], XmNwidth, 360); n++;
292     XtSetArg(args[n], XmNheight, 220); n++;
293     XtSetArg(args[n], XmNshadowThickness, 0); n++;
294     Widget container = XmCreateForm(window->window, "form", args, n);
295     XtManageChild(container);
296     XtAddEventHandler(container, KeyPressMask, FALSE, windowKeyEH, window);
297     
298     n = 0;
299     XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
300     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
301     XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
302     WindowCreateMenu(window, container, args, n);
303     
304     n = 0;
305     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
306     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
307     XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
308     XtSetArg(args[n], XmNtopWidget, window->menubar); n++;
309     XtSetArg(args[n], XmNwidth, 300); n++;
310     XtSetArg(args[n], XmNscrollingPolicy, XmAUTOMATIC); n++;
311     XtSetArg(args[n], XmNscrollBarDisplayPolicy, XmAS_NEEDED); n++;
312     XtSetArg(args[n], XmNspacing, 0); n++;
313     XtSetArg(args[n], XmNshadowThickness, 0); n++;
314     window->sidebar_scrolledwindow = XmCreateScrolledWindow(container, "sw_sidebar", args, n);
315     window->sidebar = CreateSidebar(window->sidebar_scrolledwindow, "sidebar", args, 0);
316     SidebarSetWindow(window->sidebar, window);
317     XtManageChild(window->sidebar);
318     //XtManageChild(window->sidebar);
319         
320     n = 0;
321     XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
322     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
323     XtSetArg(args[n], XmNrightWidget, window->sidebar); n++;
324     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
325     XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
326     XtSetArg(args[n], XmNtopWidget, window->menubar); n++;
327     XtSetArg(args[n], XmNbackground, BlackPixelOfScreen(XtScreen(window->window))); n++;
328     window->player_widget = XmCreateDrawingArea(container, "player", args, n);
329     XtManageChild(window->player_widget);
330     XtAddCallback(window->player_widget, XmNinputCallback, playerWidgetInputCB, window);
331     XmProcessTraversal(window->player_widget, XmTRAVERSE_CURRENT);
332     XtAddEventHandler(window->player_widget, PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
333                  EnterWindowMask | KeyPressMask | KeyReleaseMask |
334                   LeaveWindowMask, FALSE, playerEH, window);
335     
336      
337     // get F keycode
338     keycodeF = XKeysymToKeycode(XtDisplay(window->window), XStringToKeysym("F"));
339     
340     
341     PlayListInit(window);
342     
343     window->adjustWindowSize = true; // auto adjust window size by default
344     
345     return window;
346 }
347
348 MainWindow* GetMainWindow(void) {
349     return main_window;
350 }
351
352 void WindowShow(MainWindow *win) {
353     XtRealizeWidget(win->window);
354 }
355
356 /*
357  * Creates a XmPushButton menu item
358  */
359 static Widget createMenuItem(
360         Widget menu,
361         char *name,
362         char *label,
363         char mnemonic,
364         const char *accelerator,
365         char *accelerator_text,
366         XtCallbackProc callback,
367         void *cbData)
368 {
369     Arg args[16];
370     int n = 0;
371     
372     XmString s1 = XmStringCreateSimple(label);
373     XtSetArg(args[n], XmNlabelString, s1); n++;
374     XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
375     XmString at = NULL;
376     if(accelerator && accelerator_text) {
377         at = XmStringCreateSimple(accelerator_text);
378         XtSetArg(args[n], XmNaccelerator, accelerator); n++;
379         XtSetArg(args[n], XmNacceleratorText, at); n++;
380     }
381     
382     Widget menuItem = XmCreatePushButtonGadget(menu, name, args, n);
383     XtManageChild(menuItem);
384     XmStringFree(s1);
385     if(at) XmStringFree(at);
386     
387     if(callback) {
388         XtAddCallback(menuItem, XmNactivateCallback, (XtCallbackProc)callback, cbData);
389     }
390     
391     return menuItem;
392 }
393
394 /*
395  * Creates a XmToggleButton menu item
396  */
397 static Widget createToggleMenuItem(
398         Widget menu,
399         char *name,
400         char *label,
401         char mnemonic,
402         Boolean defaultValue,
403         const char *accelerator,
404         char *accelerator_text,
405         XtCallbackProc callback,
406         void *cbData)
407 {
408     Arg args[16];
409     int n = 0;
410     
411     XmString s1 = XmStringCreateSimple(label);
412     XtSetArg(args[n], XmNlabelString, s1); n++;
413     XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
414     XtSetArg(args[n], XmNset, defaultValue); n++;
415     XmString at = NULL;
416     if(accelerator && accelerator_text) {
417         at = XmStringCreateSimple(accelerator_text);
418         XtSetArg(args[n], XmNaccelerator, accelerator); n++;
419         XtSetArg(args[n], XmNacceleratorText, at); n++;
420     }
421     
422     Widget menuItem = XmCreateToggleButtonGadget(menu, name, args, n);
423     XtManageChild(menuItem);
424     XmStringFree(s1);
425     if(at) XmStringFree(at);
426     
427     if(callback) {
428         XtAddCallback(menuItem, XmNvalueChangedCallback, (XtCallbackProc)callback, cbData);
429     }
430     
431     return menuItem;
432 }
433
434 /*
435  * Creates a menu separator
436  */
437 static Widget createMenuSeparator(Widget menu) {
438     Widget w = XmCreateSeparator(menu, "separator", NULL, 0);
439     XtManageChild(w);
440     return w;
441 }
442
443 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *mbargs, int nmbargs) {
444     Widget menubar = XmCreateMenuBar(parent, "menubar", mbargs, nmbargs);
445     XtManageChild(menubar);
446     win->menubar = menubar;
447     
448     Arg args[16];
449     int n;
450     
451     // menus
452     XmString s = XmStringCreateSimple("File");
453     XtVaCreateManagedWidget(
454             "menuitem",
455             xmCascadeButtonWidgetClass,
456             menubar,
457             XmNlabelString, s,
458             NULL);
459     XmStringFree(s);
460     Widget fileMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 0, NULL, NULL);
461     
462     s = XmStringCreateSimple("Playback");
463     XtVaCreateManagedWidget(
464             "menuitem",
465             xmCascadeButtonWidgetClass,
466             menubar,
467             XmNlabelString, s,
468             NULL);
469     XmStringFree(s);
470     Widget playMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 1, NULL, NULL);
471     
472     s = XmStringCreateSimple("View");
473     Widget viewMenuItem = XtVaCreateManagedWidget(
474             "menuitem",
475             xmCascadeButtonWidgetClass,
476             menubar,
477             XmNlabelString, s,
478             NULL);
479     XmStringFree(s);
480     Widget viewMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 2, NULL, NULL);
481     
482     // file menu
483     createMenuItem(fileMenu, "fileOpen", "Open...", 'O', "Ctrl<Key>O", "Ctrl+O", FileOpenCB, NULL);
484     createMenuItem(fileMenu, "fileQuit", "Exit", 'E', "Ctrl<Key>Q", "Ctrl+Q", FileQuitCB, NULL);
485     
486     // play menu
487     win->playRepeatTrackButton = createToggleMenuItem(playMenu, "playRepeatTrack", "Repeat", 'R', False, NULL, NULL, PlayRepeatCB, win);
488     win->playRepeatListButton = createToggleMenuItem(playMenu, "playRepeatList", "Repeat List", 'L', False, NULL, NULL, PlayRepeatListCB, win);
489     win->playRandom = createToggleMenuItem(playMenu, "playRandom", "Random Playback", 'P', False, NULL, NULL, PlayRandomCB, win);
490     win->playAutoPlayButton = createToggleMenuItem(playMenu, "playAutoNext", "Autoplay Folder", 'A', False, NULL, NULL, PlayAutoPlayCB, win);
491     XtVaSetValues(win->playRepeatTrackButton, XmNindicatorType, XmONE_OF_MANY, NULL);
492     XtVaSetValues(win->playRepeatListButton, XmNindicatorType, XmONE_OF_MANY, NULL);
493     XtVaSetValues(win->playAutoPlayButton, XmNindicatorType, XmONE_OF_MANY, NULL);
494     XtVaSetValues(win->playRandom, XmNindicatorType, XmONE_OF_MANY, NULL);
495     
496     
497     // view menu
498     createMenuItem(viewMenu, "viewFullscreen", "Fullscreen", 'F', "<Key>F", "F", ViewFullscreenCB, NULL);
499     win->viewSidebarButton = createToggleMenuItem(viewMenu, "viewSidebar", "View Sidebar", 'S', False, NULL, NULL, ViewSidebarCB, win);
500     
501     createMenuSeparator(viewMenu);
502     
503     win->viewAdjustWindowSize = createToggleMenuItem(viewMenu, "viewAdjustWindowSize", "Adjust Window Size", 'W', TRUE, NULL, NULL, ViewAdjustWindowSizeCB, win);
504 }
505
506 void go_fullscreen(Display *dsp, Window win)
507 {
508   XEvent xev;
509   Atom wm_state = XInternAtom(dsp, "_NET_WM_STATE", False);
510   Atom fullscreen = XInternAtom(dsp, "_NET_WM_STATE_FULLSCREEN", False);
511   memset(&xev, 0, sizeof(xev));
512   xev.type = ClientMessage;
513   xev.xclient.window = win;
514   xev.xclient.message_type = wm_state;
515   xev.xclient.format = 32;
516   xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
517   xev.xclient.data.l[1] = fullscreen;
518   xev.xclient.data.l[2] = 0;
519   XSendEvent(dsp, DefaultRootWindow(dsp), False,
520     SubstructureNotifyMask, &xev);
521 }
522
523 static Atom net_wm_state;
524 static Atom net_wm_state_fullscreen;
525 static int net_wm_atoms_initialized = 0;
526
527 void WindowFullscreen(MainWindow *win, bool enableFullscreen) {
528     Display *dpy = XtDisplay(win->window);
529     
530     // init net_wm_state atoms
531     if(!net_wm_atoms_initialized) {
532         net_wm_state = XInternAtom(dpy, "_NET_WM_STATE", False);
533         net_wm_state_fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
534         net_wm_atoms_initialized = 1;
535     }
536     
537     WindowMenubarSetVisible(win, !enableFullscreen);
538     if(enableFullscreen && !win->fullscreen) {
539         XtUnmanageChild(main_window->menubar);
540         main_window->fullscreen = TRUE;
541     } else if(!enableFullscreen && win->fullscreen) {
542         XtManageChild(main_window->menubar);
543         main_window->fullscreen = FALSE;
544     }
545     
546     WindowShowSidebar(win, enableFullscreen ? false : win->sidebarvisible);
547     
548     XEvent ev;
549     memset(&ev, 0, sizeof(XEvent));
550     ev.type = ClientMessage;
551     ev.xclient.window = XtWindow(win->window);
552     ev.xclient.message_type = net_wm_state;
553     ev.xclient.format = 32;
554     ev.xclient.data.l[0] = enableFullscreen ? 1 : 0;
555     ev.xclient.data.l[1] = net_wm_state_fullscreen;
556     ev.xclient.data.l[2] = 0;
557     XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureNotifyMask | SubstructureRedirectMask, &ev);
558 }
559
560 void WindowMenubarSetVisible(MainWindow *win, bool visible) {
561     if(visible) {
562         XtManageChild(main_window->menubar);
563         XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, main_window->menubar, NULL);
564     } else {
565         XtUnmanageChild(main_window->menubar);
566         XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_FORM, NULL);
567     }
568 }
569
570 static void filedialog_end(
571         Widget widget,
572         MainWindow *data,
573         XmFileSelectionBoxCallbackStruct *selection)
574 {
575     XtUnmanageChild(widget);
576     XtDestroyWidget(widget);
577 }
578
579 static void filedialog_select(
580         Widget widget,
581         MainWindow *data,
582         XmFileSelectionBoxCallbackStruct *selection)
583 {
584     char *value = NULL;
585     if(selection->value) {
586         XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &value);
587         if(value) {
588             PlayListAddFile(data, value);
589             PlayListPlayNext(data, true);
590             XtFree(value);
591         }
592     }
593     filedialog_end(widget, data, NULL);
594 }
595
596
597
598
599 static void FileOpenCB(Widget w, void *udata, void *cdata) {
600     MainWindow *win = main_window;
601     
602     Arg args[16];
603     int n = 0;
604     
605     XtSetArg(args[n], XnNshowViewMenu, 1); n++;
606     Widget dialog = XnCreateFileSelectionDialog(win->window, "dialog", args, n);
607     XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)filedialog_select, win);
608     XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)filedialog_end, win);
609     
610     Widget dirUp = XnFileSelectionBoxGetChild(dialog, XnFSB_DIR_UP_BUTTON);
611     XtUnmanageChild(dirUp);
612     
613     XtManageChild(dialog);
614 }
615
616 static void FileQuitCB(Widget w, void *udata, void *cdata) {
617     WindowClosePlayer(main_window);
618     ApplicationExit();
619 }
620
621 static void PlayRepeatCB(Widget w, void *udata, void *cdata) {
622     MainWindow *win = udata;
623     win->playlist.repeatTrack = XmToggleButtonGadgetGetState(w);
624     win->playlist.repeatList = 0;
625     win->playlist.autoplayFolder = 0;
626     win->playlist.random = 0;
627     XtVaSetValues(win->playRepeatListButton, XmNset, 0, NULL);
628     XtVaSetValues(win->playAutoPlayButton, XmNset, 0, NULL);
629     XtVaSetValues(win->playRandom, XmNset, 0, NULL);
630 }
631
632 static void PlayRepeatListCB(Widget w, void *udata, void *cdata) {
633     MainWindow *win = udata;
634     win->playlist.repeatList = XmToggleButtonGadgetGetState(w);
635     win->playlist.repeatTrack = 0;
636     win->playlist.autoplayFolder = 0;
637     win->playlist.random = 0;
638     XtVaSetValues(win->playRepeatTrackButton, XmNset, 0, NULL);
639     XtVaSetValues(win->playAutoPlayButton, XmNset, 0, NULL);
640     XtVaSetValues(win->playRandom, XmNset, 0, NULL);
641 }
642
643 static void PlayAutoPlayCB(Widget w, void *udata, void *cdata) {
644     MainWindow *win = udata;
645     win->playlist.autoplayFolder = XmToggleButtonGadgetGetState(w);
646     win->playlist.repeatTrack = 0;
647     win->playlist.repeatList = 0;
648     win->playlist.random = 0;
649     XtVaSetValues(win->playRepeatTrackButton, XmNset, 0, NULL);
650     XtVaSetValues(win->playRepeatListButton, XmNset, 0, NULL);
651     XtVaSetValues(win->playRandom, XmNset, 0, NULL);
652 }
653
654 static void PlayRandomCB(Widget w, void *udata, void *cdata) {
655     MainWindow *win = udata;
656     win->playlist.random = XmToggleButtonGadgetGetState(w);
657     win->playlist.repeatTrack = 0;
658     win->playlist.repeatList = 0;
659     win->playlist.autoplayFolder = 0;
660     XtVaSetValues(win->playRepeatTrackButton, XmNset, 0, NULL);
661     XtVaSetValues(win->playRepeatListButton, XmNset, 0, NULL);
662     XtVaSetValues(win->playAutoPlayButton, XmNset, 0, NULL);
663 }
664
665 static void ViewFullscreenCB(Widget w, void *udata, void *cdata) {
666     if(main_window->fullscreen) {
667         WindowFullscreen(main_window, FALSE);
668     } else {
669         WindowFullscreen(main_window, TRUE);
670     }   
671 }
672
673 static void ViewSidebarCB(Widget w, void *udata, void *cdata) {
674     MainWindow *win = udata;
675     XmToggleButtonCallbackStruct *cb = cdata;
676     win->sidebarvisible = cb->set;
677     WindowShowSidebar(win, cb->set);
678 }
679
680 static void ViewAdjustWindowSizeCB(Widget w, void *udata, void *cdata) {
681     MainWindow *win = udata;
682     win->adjustWindowSize = XmToggleButtonGadgetGetState(w);
683 }
684
685 void WindowAdjustAspectRatio(MainWindow *win) {
686     if(!win->player) return;
687     if(!win->player->isactive || win->player->width <= 0 || win->player->height <= 0) return;
688       
689     // we have a running player width video
690     // adjust window aspect ratio (the window aspect ratio is different from
691     // the video, because of window decoration, menubar and other extra controls)
692     
693     Dimension win_width, win_height;
694     XtVaGetValues(win->window, XmNwidth, &win_width, XmNheight, &win_height, NULL);
695     Dimension player_width, player_height;
696     XtVaGetValues(win->player_widget, XmNwidth, &player_width, XmNheight, &player_height, NULL);
697     
698     double r = (double)win->player->width / (double)win->player->height;
699     double p_width = player_width;
700     double p_height = p_width / r;
701     
702     Dimension new_width = p_width + win_width - player_width;
703     Dimension new_height = p_height + win_height - player_height;
704     
705     XSizeHints hints;
706     hints.flags = PAspect;
707     hints.min_aspect.x = new_width;
708     hints.min_aspect.y = new_height;
709     hints.max_aspect.x = new_width;
710     hints.max_aspect.y = new_height;
711     XSetWMNormalHints(XtDisplay(win->window), XtWindow(win->window), &hints);
712 }
713
714 void WindowClosePlayer(MainWindow *win) {
715     if(win->player) {
716         PlayerDestroy(win->player);
717     }
718     win->player = NULL;
719     WindowShowPlayerCursor(win);
720 }
721
722 void WindowHidePlayerCursor(MainWindow *win) {
723     if(!win->cursorhidden && win->player && win->player->window != 0) {
724         XDefineCursor(XtDisplay(win->player_widget), XtWindow(win->player_widget), blank_cursor);
725         win->cursorhidden = True;
726         XFlush(XtDisplay(win->player_widget));
727     }
728 }
729
730 void WindowShowPlayerCursor(MainWindow *win) {
731     if(win->cursorhidden && win->player && win->player->window != 0) {
732         XDefineCursor(XtDisplay(win->player_widget), XtWindow(win->player_widget), None);
733         XFlush(XtDisplay(win->player_widget));
734     }
735     win->cursorhidden = False;
736 }
737
738 void WindowShowSidebar(MainWindow *win, bool visible) {
739     if(visible) {
740         XtManageChild(win->sidebar_scrolledwindow);
741         XtVaSetValues(win->player_widget, XmNrightAttachment, XmATTACH_WIDGET, XmNrightWidget, win->sidebar_scrolledwindow, NULL);
742     } else {
743         XtUnmanageChild(win->sidebar_scrolledwindow);
744         XtVaSetValues(win->player_widget, XmNrightAttachment, XmATTACH_FORM, NULL);
745     }
746 }
747
748 void WindowUpdate(MainWindow *win) {
749     SidebarRepaint(win->sidebar);
750 }