fix the check for if a relative is focused on map
[mikachu/openbox.git] / openbox / client.c
index 4d63f05..0bb1d11 100644 (file)
@@ -68,7 +68,7 @@ typedef struct
     gpointer data;
 } ClientCallback;
 
-GList            *client_list           = NULL;
+GList          *client_list             = NULL;
 
 static GSList  *client_destroy_notifies = NULL;
 static RrImage *client_default_icon     = NULL;
@@ -494,6 +494,15 @@ void client_manage(Window window, ObPrompt *prompt)
                   activate ? "yes" : "no");
     if (activate) {
         gboolean raise = FALSE;
+        gboolean relative_focused = FALSE;
+        gboolean parent_focused = FALSE;
+
+        parent_focused = (focus_client != NULL &&
+                          client_search_focus_parent(self));
+        relative_focused = (focus_client != NULL &&
+                            (client_search_focus_tree_full(self) != NULL ||
+                             client_search_focus_group_full(self) != NULL));
+
 
         /* This is focus stealing prevention */
         ob_debug_type(OB_DEBUG_FOCUS,
@@ -524,13 +533,12 @@ void client_manage(Window window, ObPrompt *prompt)
                           "Not focusing the window because its on another "
                           "desktop\n");
         }
-        /* If something is focused, and it's not our relative... */
-        else if (focus_client && client_search_focus_tree_full(self) == NULL &&
-                 client_search_focus_group_full(self) == NULL)
-        {
+        /* If something is focused... */
+        else if (focus_client) {
             /* If the user is working in another window right now, then don't
                steal focus */
-            if (event_last_user_time && launch_time &&
+            if (!parent_focused &&
+                event_last_user_time && launch_time &&
                 event_time_after(event_last_user_time, launch_time) &&
                 event_last_user_time != launch_time &&
                 event_time_after(event_last_user_time,
@@ -539,10 +547,12 @@ void client_manage(Window window, ObPrompt *prompt)
                 activate = FALSE;
                 ob_debug_type(OB_DEBUG_FOCUS,
                               "Not focusing the window because the user is "
-                              "working in another window\n");
+                              "working in another window that is not "
+                              "its parent\n");
             }
-            /* If its a transient (and its parents aren't focused) */
-            else if (client_has_parent(self)) {
+            /* If the new window is a transient (and its relatives aren't
+               focused) */
+            else if (client_has_parent(self) && !relative_focused) {
                 activate = FALSE;
                 ob_debug_type(OB_DEBUG_FOCUS,
                               "Not focusing the window because it is a "
@@ -568,8 +578,11 @@ void client_manage(Window window, ObPrompt *prompt)
                               "Not focusing the window because another window "
                               "would get the focus anyway\n");
             }
+            /* Don't move focus if the window is not visible on the current
+               desktop and none of its relatives are focused */
             else if (!(self->desktop == screen_desktop ||
-                       self->desktop == DESKTOP_ALL))
+                       self->desktop == DESKTOP_ALL) &&
+                     !relative_focused)
             {
                 activate = FALSE;
                 raise = TRUE;
@@ -582,7 +595,7 @@ void client_manage(Window window, ObPrompt *prompt)
         if (!activate) {
             ob_debug_type(OB_DEBUG_FOCUS,
                           "Focus stealing prevention activated for %s at "
-                          "time %u (last user interactioon time %u)\n",
+                          "time %u (last user interaction time %u)\n",
                           self->title, map_time, event_last_user_time);
             /* if the client isn't focused, then hilite it so the user
                knows it is there */
@@ -681,7 +694,7 @@ ObClient *client_fake_manage(Window window)
 
 void client_unmanage_all(void)
 {
-    while (client_list != NULL)
+    while (client_list)
         client_unmanage(client_list->data);
 }
 
@@ -1086,7 +1099,7 @@ gboolean client_find_onscreen(ObClient *self, gint *x, gint *y, gint w, gint h,
 
         /* This makes sure windows aren't entirely outside of the screen so you
            can't see them at all.
-           It makes sure 10% of the window is on the screen at least. At don't
+           It makes sure 10% of the window is on the screen at least. And don't
            let it move itself off the top of the screen, which would hide the
            titlebar on you. (The user can still do this if they want too, it's
            only limiting the application.
@@ -1296,7 +1309,7 @@ static void client_get_state(ObClient *self)
 static void client_get_shaped(ObClient *self)
 {
     self->shaped = FALSE;
-#ifdef   SHAPE
+#ifdef SHAPE
     if (extensions_shape) {
         gint foo;
         guint ufoo;
@@ -1307,7 +1320,7 @@ static void client_get_shaped(ObClient *self)
         XShapeQueryExtents(ob_display, self->window, &s, &foo,
                            &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
                            &ufoo);
-        self->shaped = (s != 0);
+        self->shaped = !!s;
     }
 #endif
 }
@@ -1319,13 +1332,13 @@ void client_update_transient_for(ObClient *self)
     gboolean trangroup = FALSE;
 
     if (XGetTransientForHint(ob_display, self->window, &t)) {
-        if (t != self->window) { /* cant be transient to itself! */
+        if (t != self->window) { /* can't be transient to itself! */
             target = g_hash_table_lookup(window_map, &t);
-            /* if this happens then we need to check for it*/
+            /* if this happens then we need to check for it */
             g_assert(target != self);
             if (target && !WINDOW_IS_CLIENT(target)) {
-                /* this can happen when a dialog is a child of
-                   a dockapp, for example */
+                /* watch out for windows with a parent that is something
+                   different, like a dockapp for example */
                 target = NULL;
             }
         }
@@ -1409,7 +1422,7 @@ static void client_update_transient_tree(ObClient *self,
     /* If we are now transient for a single window we need to add ourselves to
        its children
 
-       WARNING: Cyclical transient ness is possible if two windows are
+       WARNING: Cyclical transient-ness is possible if two windows are
        transient for eachother.
     */
     else if (newparent &&
@@ -1448,7 +1461,7 @@ static void client_update_transient_tree(ObClient *self,
     }
 
     /** If we change our group transient-ness, our children change their
-        effect group transient-ness, which affects how they relate to other
+        effective group transient-ness, which affects how they relate to other
         group windows **/
 
     for (it = self->transients; it; it = g_slist_next(it)) {
@@ -1890,7 +1903,7 @@ void client_update_wmhints(ObClient *self)
 {
     XWMHints *hints;
 
-    /* assume a window takes input if it doesnt specify */
+    /* assume a window takes input if it doesn't specify */
     self->can_focus = TRUE;
 
     if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
@@ -1922,7 +1935,7 @@ void client_update_wmhints(ObClient *self)
             ObGroup *oldgroup = self->group;
 
             /* remove from the old group if there was one */
-            if (self->group != NULL) {
+            if (self->group) {
                 group_remove(self->group, self);
                 self->group = NULL;
             }
@@ -1993,7 +2006,7 @@ void client_update_title(ObClient *self)
     */
                 data = g_strdup("");
             } else
-                data = g_strdup("Unnamed Window");
+                data = g_strdup(_("Unnamed Window"));
         }
     }
     self->original_title = g_strdup(data);
@@ -2607,13 +2620,6 @@ gboolean client_hide(ObClient *self)
 
     if (!client_should_show(self)) {
         if (self == focus_client) {
-            /* if there is a grab going on, then we need to cancel it. if we
-               move focus during the grab, applications will get
-               NotifyWhileGrabbed events and ignore them !
-
-               actions should not rely on being able to move focus during an
-               interactive grab.
-            */
             event_cancel_all_key_grabs();
         }
 
@@ -2826,7 +2832,7 @@ void client_try_configure(ObClient *self, gint *x, gint *y, gint *w, gint *h,
     Rect desired = {*x, *y, *w, *h};
     frame_rect_to_frame(self->frame, &desired);
 
-    /* make the frame recalculate its dimentions n shit without changing
+    /* make the frame recalculate its dimensions n shit without changing
        anything visible for real, this way the constraints below can work with
        the updated frame dimensions. */
     frame_adjust_area(self->frame, FALSE, TRUE, TRUE);
@@ -2881,8 +2887,10 @@ void client_try_configure(ObClient *self, gint *x, gint *y, gint *w, gint *h,
     /* gets the client's position */
     frame_frame_gravity(self->frame, x, y);
 
-    /* work within the prefered sizes given by the window */
-    if (!(*w == self->area.width && *h == self->area.height)) {
+    /* work within the preferred sizes given by the window, these may have
+       changed rather than it's requested width and height, so always run
+       through this code */
+    {
         gint basew, baseh, minw, minh;
         gint incw, inch;
         gfloat minratio, maxratio;
@@ -2911,6 +2919,7 @@ void client_try_configure(ObClient *self, gint *x, gint *y, gint *w, gint *h,
             minh = self->base_size.height;
         }
 
+        /* This comment is no longer true */
         /* if this is a user-requested resize, then check against min/max
            sizes */
 
@@ -3146,7 +3155,7 @@ void client_fullscreen(ObClient *self, gboolean fs)
     if (fs) {
         self->pre_fullscreen_area = self->area;
         /* if the window is maximized, its area isn't all that meaningful.
-           save it's premax area instead. */
+           save its premax area instead. */
         if (self->max_horz) {
             self->pre_fullscreen_area.x = self->pre_max_area.x;
             self->pre_fullscreen_area.width = self->pre_max_area.width;
@@ -3197,7 +3206,6 @@ static void client_iconify_recursive(ObClient *self,
     GSList *it;
     gboolean changed = FALSE;
 
-
     if (self->iconic != iconic) {
         ob_debug("%sconifying window: 0x%lx\n", (iconic ? "I" : "Uni"),
                  self->window);
@@ -3261,7 +3269,7 @@ void client_maximize(ObClient *self, gboolean max, gint dir)
     gint x, y, w, h;
 
     g_assert(dir == 0 || dir == 1 || dir == 2);
-    if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE)) return; /* can't */
+    if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && max) return;/* can't */
 
     /* check if already done */
     if (max) {
@@ -3397,12 +3405,20 @@ void client_close(ObClient *self)
 #define OB_KILL_RESULT_NO 0
 #define OB_KILL_RESULT_YES 1
 
-static void client_kill_requested(ObPrompt *p, gint result, gpointer data)
+static gboolean client_kill_requested(ObPrompt *p, gint result, gpointer data)
 {
     ObClient *self = data;
 
     if (result == OB_KILL_RESULT_YES)
         client_kill(self);
+    return TRUE; /* call the cleanup func */
+}
+
+static void client_kill_cleanup(ObPrompt *p, gpointer data)
+{
+    ObClient *self = data;
+
+    g_assert(p == self->kill_prompt);
 
     prompt_unref(self->kill_prompt);
     self->kill_prompt = NULL;
@@ -3413,10 +3429,18 @@ static void client_prompt_kill(ObClient *self)
     /* check if we're already prompting */
     if (!self->kill_prompt) {
         ObPromptAnswer answers[] = {
-            { _("Cancel"), OB_KILL_RESULT_NO },
-            { _("Force Exit"), OB_KILL_RESULT_YES }
+            { 0, OB_KILL_RESULT_NO },
+            { 0, OB_KILL_RESULT_YES }
         };
         gchar *m;
+        const gchar *y, *title;
+
+        title = self->original_title;
+        if (title[0] == '\0') {
+            /* empty string, so use its parent */
+            ObClient *p = client_search_top_direct_parent(self);
+            if (p) title = p->original_title;
+        }
 
         if (client_on_localhost(self)) {
             const gchar *sig;
@@ -3427,18 +3451,27 @@ static void client_prompt_kill(ObClient *self)
                 sig = "kill";
 
             m = g_strdup_printf
-                (_("The window \"%s\" does not seem to be responding.  Do you want to force it to exit by sending the %s signal?"), self->original_title, sig);
+                (_("The window \"%s\" does not seem to be responding.  Do you want to force it to exit by sending the %s signal?"),
+                 title, sig);
+            y = _("End Process");
         }
-        else
+        else {
             m = g_strdup_printf
-                (_("The window \"%s\" does not seem to be responding.  Do you want to disconnect it from the X server?"), self->original_title);
-
+                (_("The window \"%s\" does not seem to be responding.  Do you want to disconnect it from the X server?"),
+                 title);
+            y = _("Disconnect");
+        }
+        /* set the dialog buttons' text */
+        answers[0].text = _("Cancel");  /* "no" */
+        answers[1].text = y;            /* "yes" */
 
-        self->kill_prompt = prompt_new(m, answers,
+        self->kill_prompt = prompt_new(m, NULL, answers,
                                        sizeof(answers)/sizeof(answers[0]),
                                        OB_KILL_RESULT_NO, /* default = no */
                                        OB_KILL_RESULT_NO, /* cancel = no */
-                                       client_kill_requested, self);
+                                       client_kill_requested,
+                                       client_kill_cleanup,
+                                       self);
         g_free(m);
     }
 
@@ -3812,13 +3845,6 @@ gboolean client_focus(ObClient *self)
        go moving on us */
     event_halt_focus_delay();
 
-    /* if there is a grab going on, then we need to cancel it. if we move
-       focus during the grab, applications will get NotifyWhileGrabbed events
-       and ignore them !
-
-       actions should not rely on being able to move focus during an
-       interactive grab.
-    */
     event_cancel_all_key_grabs();
 
     xerror_set_ignore(TRUE);
@@ -3878,6 +3904,8 @@ static void client_present(ObClient *self, gboolean here, gboolean raise,
     client_focus(self);
 }
 
+/* this function exists to map to the client_activate message in the ewmh,
+   the user arg is unused because nobody uses it correctly anyway. */
 void client_activate(ObClient *self, gboolean here, gboolean raise,
                      gboolean unshade, gboolean user)
 {
@@ -3923,8 +3951,6 @@ gboolean client_focused(ObClient *self)
     return self == focus_client;
 }
 
-
-
 RrImage* client_icon(ObClient *self)
 {
     RrImage *ret = NULL;
@@ -4028,6 +4054,21 @@ ObClient *client_search_focus_parent(ObClient *self)
     return NULL;
 }
 
+ObClient *client_search_focus_parent_full(ObClient *self)
+{
+    GSList *it;
+    ObClient *ret = NULL;
+
+    for (it = self->parents; it; it = g_slist_next(it)) {
+        if (client_focused(it->data))
+            ret = it->data;
+        else
+            ret = client_search_focus_parent_full(it->data);
+        if (ret) break;
+    }
+    return ret;
+}
+
 ObClient *client_search_parent(ObClient *self, ObClient *search)
 {
     GSList *it;
@@ -4111,12 +4152,12 @@ static void detect_edge(Rect area, ObDirection dir,
             /* check if the head of this window is closer than the previously
                chosen edge (take into account that the previously chosen
                edge might have been a tail, not a head) */
-            if (head + (*near_edge ? 0 : my_size) < *dest)
+            if (head + (*near_edge ? 0 : my_size) <= *dest)
                 skip_head = TRUE;
             /* check if the tail of this window is closer than the previously
                chosen edge (take into account that the previously chosen
                edge might have been a head, not a tail) */
-            if (tail - (!*near_edge ? 0 : my_size) < *dest)
+            if (tail - (!*near_edge ? 0 : my_size) <= *dest)
                 skip_tail = TRUE;
             break;
         case OB_DIRECTION_SOUTH:
@@ -4130,12 +4171,12 @@ static void detect_edge(Rect area, ObDirection dir,
             /* check if the head of this window is closer than the previously
                chosen edge (take into account that the previously chosen
                edge might have been a tail, not a head) */
-            if (head - (*near_edge ? 0 : my_size) > *dest)
+            if (head - (*near_edge ? 0 : my_size) >= *dest)
                 skip_head = TRUE;
             /* check if the tail of this window is closer than the previously
                chosen edge (take into account that the previously chosen
                edge might have been a head, not a tail) */
-            if (tail + (!*near_edge ? 0 : my_size) > *dest)
+            if (tail + (!*near_edge ? 0 : my_size) >= *dest)
                 skip_tail = TRUE;
             break;
         default:
@@ -4143,7 +4184,7 @@ static void detect_edge(Rect area, ObDirection dir,
     }
 
     ob_debug("my head %d size %d\n", my_head, my_size);
-    ob_debug("head %d tail %d deest %d\n", head, tail, *dest);
+    ob_debug("head %d tail %d dest %d\n", head, tail, *dest);
     if (!skip_head) {
         ob_debug("using near edge %d\n", head);
         *dest = head;
@@ -4306,28 +4347,28 @@ void client_find_resize_directional(ObClient *self, ObDirection side,
     switch (side) {
     case OB_DIRECTION_EAST:
         head = RECT_RIGHT(self->frame->area) +
-            (self->size_inc.width - 1) * (grow ? 1 : -1);
+            (self->size_inc.width - 1) * (grow ? 1 : 0);
         e_start = RECT_TOP(self->frame->area);
         e_size = self->frame->area.height;
         dir = grow ? OB_DIRECTION_EAST : OB_DIRECTION_WEST;
         break;
     case OB_DIRECTION_WEST:
         head = RECT_LEFT(self->frame->area) -
-            (self->size_inc.width - 1) * (grow ? 1 : -1);
+            (self->size_inc.width - 1) * (grow ? 1 : 0);
         e_start = RECT_TOP(self->frame->area);
         e_size = self->frame->area.height;
         dir = grow ? OB_DIRECTION_WEST : OB_DIRECTION_EAST;
         break;
     case OB_DIRECTION_NORTH:
         head = RECT_TOP(self->frame->area) -
-            (self->size_inc.height - 1) * (grow ? 1 : -1);
+            (self->size_inc.height - 1) * (grow ? 1 : 0);
         e_start = RECT_LEFT(self->frame->area);
         e_size = self->frame->area.width;
         dir = grow ? OB_DIRECTION_NORTH : OB_DIRECTION_SOUTH;
         break;
     case OB_DIRECTION_SOUTH:
         head = RECT_BOTTOM(self->frame->area) +
-            (self->size_inc.height - 1) * (grow ? 1 : -1);
+            (self->size_inc.height - 1) * (grow ? 1 : 0);
         e_start = RECT_LEFT(self->frame->area);
         e_size = self->frame->area.width;
         dir = grow ? OB_DIRECTION_SOUTH : OB_DIRECTION_NORTH;