test-resizing.c

Go to the documentation of this file.
00001 #include <X11/Xlib.h>
00002 #include <X11/Xutil.h>
00003 #include <stdlib.h>
00004 #include <glib.h>
00005 
00006 static void
00007 calc_rects (XRectangle *rects, int width, int height)
00008 {
00009   int w = (width - 21) / 3;
00010   int h = (height - 21) / 3;
00011   int i;
00012 
00013   i = 0;
00014   while (i < 9)
00015     {
00016       rects[i].width = w;
00017       rects[i].height = h;
00018       ++i;
00019     }
00020   
00021   /* NW */
00022   rects[0].x = 0;
00023   rects[0].y = 0;
00024 
00025   /* N */
00026   rects[1].x = width / 2 - w / 2;
00027   rects[1].y = 0;
00028 
00029   /* NE */
00030   rects[2].x = width - w;
00031   rects[2].y = 0;
00032 
00033   /* E */
00034   rects[3].x = width - w;
00035   rects[3].y = height / 2 - h / 2;
00036 
00037   /* SE */
00038   rects[4].x = width - w;
00039   rects[4].y = height - h;
00040 
00041   /* S */
00042   rects[5].x = width / 2 - w / 2;
00043   rects[5].y = height - h;
00044 
00045   /* SW */
00046   rects[6].x = 0;
00047   rects[6].y = height - h;
00048 
00049   /* W */
00050   rects[7].x = 0;
00051   rects[7].y = height / 2 - h / 2;
00052 
00053   /* Center */
00054   rects[8].x = width / 2 - w / 2;
00055   rects[8].y = height / 2 - h / 2;
00056 }
00057 
00058 static Bool
00059 all_events (Display  *display,
00060             XEvent   *event,
00061             XPointer  arg)
00062 {
00063   return True;
00064 }
00065 
00066 static void
00067 get_size (Display *d, Drawable draw,
00068           int *xp, int *yp, int *widthp, int *heightp)
00069 {
00070   int x, y;
00071   unsigned int width, height, border, depth;
00072   Window root;
00073   
00074   XGetGeometry (d, draw, &root, &x, &y, &width, &height, &border, &depth);
00075 
00076   if (xp)
00077     *xp = x;
00078   if (yp)
00079     *yp = y;
00080   if (widthp)
00081     *widthp = width;
00082   if (*heightp)
00083     *heightp = height;
00084 }
00085 
00086 int
00087 main (int argc, char **argv)
00088 {
00089   Display *d;
00090   Window w, cw;
00091   XSizeHints hints;
00092   int screen;
00093   XEvent ev;
00094   int x, y, width, height;
00095   Pixmap pix;
00096   GC gc;  
00097   XGCValues gc_vals;
00098   XSetWindowAttributes set_attrs;
00099   XWindowChanges changes;
00100   XRectangle rects[9];
00101   gboolean redraw_pending;
00102   unsigned int mask;
00103   
00104   d = XOpenDisplay (NULL);
00105 
00106   screen = DefaultScreen (d);
00107 
00108   /* Print some debug spew to show how StaticGravity works */     
00109   w = XCreateSimpleWindow (d, RootWindow (d, screen), 
00110                            0, 0, 100, 100, 0,
00111                            WhitePixel (d, screen),
00112                            WhitePixel (d, screen));
00113   cw = XCreateSimpleWindow (d, w,
00114                             0, 0, 100, 100, 0,
00115                             WhitePixel (d, screen),
00116                             WhitePixel (d, screen));
00117   set_attrs.win_gravity = StaticGravity;
00118       
00119   XChangeWindowAttributes (d, cw,
00120                            CWWinGravity,
00121                            &set_attrs);
00122   
00123   get_size (d, w, &x, &y, &width, &height);
00124   
00125   g_print ("Parent is %d,%d  %d x %d before configuring parent\n",
00126            x, y, width, height);
00127   
00128   get_size (d, cw, &x, &y, &width, &height);
00129 
00130   g_print ("Child is %d,%d  %d x %d before configuring parent\n",
00131            x, y, width, height);
00132   
00133   changes.x = 10;
00134   changes.y = 10;
00135   changes.width = 110;
00136   changes.height = 110;
00137   /* last mask wins */
00138   mask = CWX | CWY;
00139   mask = CWWidth | CWHeight;
00140   mask = CWX | CWY | CWWidth | CWHeight;
00141   
00142   XConfigureWindow (d, w, mask, &changes);
00143   XSync (d, False);
00144 
00145   get_size (d, w, &x, &y, &width, &height);
00146 
00147   g_print ("Parent is %d,%d  %d x %d after configuring parent\n",
00148            x, y, width, height);
00149   
00150   get_size (d, cw, &x, &y, &width, &height);
00151 
00152   g_print ("Child is %d,%d  %d x %d after configuring parent\n",
00153            x, y, width, height);  
00154 
00155   XDestroyWindow (d, w);
00156   
00157   /* The window that gets displayed */
00158   
00159   x = 20;
00160   y = 20;
00161   width = 100;
00162   height = 100;
00163 
00164   calc_rects (rects, width, height);
00165   
00166   w = XCreateSimpleWindow (d, RootWindow (d, screen), 
00167                            x, y, width, height, 0,
00168                            WhitePixel (d, screen),
00169                            WhitePixel (d, screen));
00170   
00171   set_attrs.bit_gravity = StaticGravity;
00172       
00173   XChangeWindowAttributes (d, w,
00174                            CWBitGravity,
00175                            &set_attrs);
00176   
00177   XSelectInput (d, w,
00178                 ButtonPressMask | ExposureMask | StructureNotifyMask);
00179   
00180   hints.flags = PMinSize;
00181   
00182   hints.min_width = 100;
00183   hints.min_height = 100;
00184   
00185   XSetWMNormalHints (d, w, &hints);
00186   XMapWindow (d, w);
00187 
00188   redraw_pending = FALSE;
00189   while (1)
00190     {
00191       XNextEvent (d, &ev);
00192       
00193       switch (ev.xany.type)
00194         {
00195         case ButtonPress:
00196           if (ev.xbutton.button == 3)
00197             {
00198               g_print ("Exiting on button 3 press\n");
00199               exit (0);
00200             }
00201           break;
00202 
00203         case ConfigureNotify:
00204           x = ev.xconfigure.x;
00205           y = ev.xconfigure.y;
00206           width = ev.xconfigure.width;
00207           height = ev.xconfigure.height;
00208 
00209           redraw_pending = TRUE;
00210           break;
00211           
00212         case Expose:
00213           redraw_pending = TRUE;
00214           break;
00215           
00216         default:
00217           break;
00218         }
00219 
00220       /* Primitive event compression */
00221       if (XCheckIfEvent (d, &ev, all_events, NULL))
00222         {
00223           XPutBackEvent (d, &ev);
00224         }
00225       else if (redraw_pending)
00226         {
00227           calc_rects (rects, width, height);
00228           
00229           pix = XCreatePixmap (d, w, width, height,
00230                                DefaultDepth (d, screen));
00231           
00232           gc_vals.foreground = WhitePixel (d, screen);
00233           
00234           gc = XCreateGC (d, pix, GCForeground, &gc_vals);
00235           
00236           XFillRectangle (d, pix, gc, 0, 0, width, height);
00237               
00238           /* Draw rectangles at each gravity point */
00239           gc_vals.foreground = BlackPixel (d, screen);
00240           XChangeGC (d, gc, GCForeground, &gc_vals);
00241           
00242           XFillRectangles (d, pix, gc, rects, G_N_ELEMENTS (rects));
00243           
00244           XCopyArea (d, pix, w, gc, 0, 0, width, height, 0, 0);
00245           
00246           XFreePixmap (d, pix);
00247           XFreeGC (d, gc);
00248 
00249           redraw_pending = FALSE;
00250         }
00251     }
00252 
00253   /* This program has an infinite loop above so a return statement would
00254    * just cause compiler warnings.
00255    */
00256 }
00257 

Generated on Sat Aug 23 22:04:19 2008 for metacity by  doxygen 1.5.5