Simplify rxvt_temp_buf usage.
[dana/urxvt.git] / src / main.C
index c6cf973..58c4a25 100644 (file)
@@ -229,38 +229,22 @@ rxvt_term::~rxvt_term ()
 #ifdef HAVE_BG_PIXMAP
   bgPixmap.destroy ();
 #endif
+#ifdef HAVE_AFTERIMAGE
+  if (asv)
+    destroy_asvisual (asv, 0);
+  if (asimman)
+    destroy_image_manager (asimman, 0);
+#endif
 
   if (display)
     {
       selection_clear ();
+      selection_clear (true);
 
 #ifdef USE_XIM
       im_destroy ();
 #endif
-#ifdef XTERM_SCROLLBAR
-      if (xscrollbarGC) XFreeGC (dpy, xscrollbarGC);
-      if (ShadowGC)     XFreeGC (dpy, ShadowGC);
-#endif
-#ifdef PLAIN_SCROLLBAR
-      if (pscrollbarGC) XFreeGC (dpy, pscrollbarGC);
-#endif
-#ifdef NEXT_SCROLLBAR
-      if (blackGC)      XFreeGC (dpy, blackGC);
-      if (whiteGC)      XFreeGC (dpy, whiteGC);
-      if (grayGC)       XFreeGC (dpy, grayGC);
-      if (darkGC)       XFreeGC (dpy, darkGC);
-      if (stippleGC)    XFreeGC (dpy, stippleGC);
-      if (dimple)       XFreePixmap (dpy, dimple);
-      if (upArrow)      XFreePixmap (dpy, upArrow);
-      if (downArrow)    XFreePixmap (dpy, downArrow);
-      if (upArrowHi)    XFreePixmap (dpy, upArrowHi);
-      if (downArrowHi)  XFreePixmap (dpy, downArrowHi);
-#endif
-#ifdef RXVT_SCROLLBAR
-      if (topShadowGC)  XFreeGC (dpy, topShadowGC);
-      if (botShadowGC)  XFreeGC (dpy, botShadowGC);
-      if (scrollbarGC)  XFreeGC (dpy, scrollbarGC);
-#endif
+      scrollBar.destroy ();
       if (gc)   XFreeGC (dpy, gc);
 
       delete drawable;
@@ -290,6 +274,7 @@ rxvt_term::~rxvt_term ()
     free (allocated [i]);
 
   free (selection.text);
+  free (selection.clip_text);
   // TODO: manage env vars in child only(!)
   free (env_display);
   free (env_term);
@@ -547,13 +532,19 @@ rxvt_term::window_calc (unsigned int newwidth, unsigned int newheight)
 
       if (flags & WidthValue)
         {
-          ncol = clamp (w, 0, std::numeric_limits<int16_t>::max ());
+          if (!w)
+            rxvt_fatal ("illegal window geometry (width and height must be non-zero), aborting.\n");
+
+          ncol = clamp (w, 1, std::numeric_limits<int16_t>::max ());
           szHint.flags |= USSize;
         }
 
       if (flags & HeightValue)
         {
-          nrow = clamp (h, 0, std::numeric_limits<int16_t>::max ());
+          if (!h)
+            rxvt_fatal ("illegal window geometry (width and height must be non-zero), aborting.\n");
+
+          nrow = clamp (h, 1, std::numeric_limits<int16_t>::max ());
           szHint.flags |= USSize;
         }
 
@@ -638,7 +629,7 @@ rxvt_term::window_calc (unsigned int newwidth, unsigned int newheight)
   if (recalc_y)
     szHint.y += DisplayHeight (dpy, display->screen) - szHint.height - 2 * ext_bwidth;
 
-  ncol = width / fwidth;
+  ncol = width  / fwidth;
   nrow = height / fheight;
 }
 
@@ -659,7 +650,7 @@ rxvt_term::tt_winch ()
   ws.ws_row = nrow;
   ws.ws_xpixel = width;
   ws.ws_ypixel = height;
-  (void)ioctl (pty->pty, TIOCSWINSZ, &ws);
+  ioctl (pty->pty, TIOCSWINSZ, &ws);
 
 #if 0
   // TIOCSWINSZ is supposed to do this automatically and correctly
@@ -701,6 +692,7 @@ rxvt_term::set_fonts ()
 
   prop = (*fs)[1]->properties ();
   prop.height += lineSpace;
+  prop.width += letterSpace;
 
   fs->set_prop (prop, false);
 
@@ -924,8 +916,15 @@ rxvt_term::resize_all_windows (unsigned int newwidth, unsigned int newheight, in
 
   window_calc (newwidth, newheight);
 
-  if (!HOOK_INVOKE ((this, HOOK_RESIZE_ALL_WINDOWS, DT_INT, newwidth, DT_INT, newheight, DT_END)))
-    XSetWMNormalHints (dpy, parent[0], &szHint);
+  bool set_hint = !HOOK_INVOKE ((this, HOOK_RESIZE_ALL_WINDOWS, DT_INT, newwidth, DT_INT, newheight, DT_END));
+
+  // to avoid races between us and the wm, we clear the incremental size hints around the xresizewindow
+  if (set_hint)
+    {
+      szHint.flags &= ~(PBaseSize | PResizeInc);
+      XSetWMNormalHints (dpy, parent[0], &szHint);
+      szHint.flags |= PBaseSize | PResizeInc;
+    }
 
   if (!ignoreparent)
     {
@@ -975,6 +974,9 @@ rxvt_term::resize_all_windows (unsigned int newwidth, unsigned int newheight, in
 #endif
     }
 
+  if (set_hint)
+    XSetWMNormalHints (dpy, parent[0], &szHint);
+
   fix_screen = ncol != prev_ncol || nrow != prev_nrow;
 
   if (fix_screen || newwidth != old_width || newheight != old_height)
@@ -990,8 +992,6 @@ rxvt_term::resize_all_windows (unsigned int newwidth, unsigned int newheight, in
       if (bgPixmap.window_size_sensitive ())
         update_background ();
 #endif
-
-      scr_clear ();
     }
 
   if (fix_screen || old_height == 0)
@@ -1080,18 +1080,17 @@ rxvt_term::im_set_preedit_area (XRectangle &preedit_rect,
 bool
 rxvt_term::IMisRunning ()
 {
-  char *p;
   Atom atom;
   Window win;
   char server[IMBUFSIZ];
 
   /* get current locale modifier */
-  if ((p = XSetLocaleModifiers (NULL)) != NULL)
+  if (char *p = XSetLocaleModifiers (0))
     {
       strcpy (server, "@server=");
-      strncat (server, & (p[4]), IMBUFSIZ - 9); /* skip "@im=" */
+      strncat (server, p + 4, IMBUFSIZ - 9); /* skip "@im=" */
 
-      if ((p = strchr (server + 1, '@')) != NULL)      /* first one only */
+      if (p = strchr (server + 1, '@'))      /* first one only */
         *p = '\0';
 
       atom = XInternAtom (dpy, server, False);
@@ -1168,7 +1167,7 @@ xim_preedit_draw (XIC ic, XPointer client_data, XIMPreeditDrawCallbackStruct *ca
 
   if (text)
     {
-      void *str;
+      wchar_t *str;
 
       if (!text->encoding_is_wchar && text->string.multi_byte)
         {
@@ -1176,14 +1175,14 @@ xim_preedit_draw (XIC ic, XPointer client_data, XIMPreeditDrawCallbackStruct *ca
           if (term->rs[Rs_imLocale])
             SET_LOCALE (term->rs[Rs_imLocale]);
 
-          str = rxvt_temp_buf ((text->length + 1) * sizeof (wchar_t));
-          mbstowcs ((wchar_t *)str, text->string.multi_byte, text->length + 1);
+          str = rxvt_temp_buf<wchar_t> (text->length + 1);
+          mbstowcs (str, text->string.multi_byte, text->length + 1);
 
           if (term->rs[Rs_imLocale])
             SET_LOCALE (term->locale);
         }
       else
-        str = (void *)text->string.wide_char;
+        str = text->string.wide_char;
 
       HOOK_INVOKE ((term, HOOK_XIM_PREEDIT_DRAW,
                     DT_INT, call_data->caret,