force window aspect ratio
[uwplayer.git] / application / window.c
index 9ec4323..e5fc815 100644 (file)
@@ -51,6 +51,10 @@ static void windowKeyEH(Widget widget, XtPointer data, XEvent *event, Boolean *d
     }
 }
 
+static void resizeEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
+    WindowAdjustAspectRatio(data);
+}
+
 MainWindow* WindowCreate(Display *display) {
     Arg args[32];
     int n;
@@ -71,7 +75,7 @@ MainWindow* WindowCreate(Display *display) {
             args,
             n);
     
-    
+    // close handler
     Atom wm_delete_window;
     wm_delete_window = XmInternAtom(
             display,
@@ -83,7 +87,9 @@ MainWindow* WindowCreate(Display *display) {
             window_close_handler,
             window);
     
-    
+    // resize handler
+    XtAddEventHandler(window->window, StructureNotifyMask, False, resizeEH, window);
+       
     n = 0;
     XtSetArg(args[n], XmNwidth, 360); n++;
     XtSetArg(args[n], XmNheight, 220); n++;
@@ -107,6 +113,7 @@ MainWindow* WindowCreate(Display *display) {
     XtSetArg(args[n], XmNbackground, BlackPixelOfScreen(XtScreen(window->window))); n++;
     window->player_widget = XmCreateDrawingArea(container, "player", args, n);
     XtManageChild(window->player_widget);
+    XmProcessTraversal(window->player_widget, XmTRAVERSE_CURRENT);
     
     // get F keycode
     keycodeF = XKeysymToKeycode(XtDisplay(window->window), XStringToKeysym("F"));
@@ -328,3 +335,32 @@ static void ViewFullscreenCB(Widget w, void *udata, void *cdata) {
     }
     
 }
+
+void WindowAdjustAspectRatio(MainWindow *win) {
+    if(!win->player) return;
+    if(!win->player->isactive || win->player->width <= 0 || win->player->height <= 0) return;
+    
+    // we have a running player width video
+    // adjust window aspect ratio (the window aspect ratio is different from
+    // the video, because of window decoration, menubar and other extra controls)
+    
+    Dimension win_width, win_height;
+    XtVaGetValues(win->window, XmNwidth, &win_width, XmNheight, &win_height, NULL);
+    Dimension player_width, player_height;
+    XtVaGetValues(win->player_widget, XmNwidth, &player_width, XmNheight, &player_height, NULL);
+    
+    double r = (double)win->player->width / (double)win->player->height;
+    double p_width = player_width;
+    double p_height = p_width / r;
+    
+    Dimension new_width = p_width + win_width - player_width;
+    Dimension new_height = p_height + win_height - player_height;
+    
+    XSizeHints hints;
+    hints.flags = PAspect;
+    hints.min_aspect.x = new_width;
+    hints.min_aspect.y = new_height;
+    hints.max_aspect.x = new_width;
+    hints.max_aspect.y = new_height;
+    XSetWMNormalHints(XtDisplay(win->window), XtWindow(win->window), &hints);
+}