Reputation: 7
I want to be able to add some colour graphics to my window application, and the only way I can do it is to manually load a bitmap and use Xlib's put pixel routines.
I am currently using Xlib. I tried gtk and many people and I have compilation problems with its headers, and I don't think other additional libraries will work because I want to make something that requires the fewest libraries as possible.
After learning and experimenting, I notice that when I minimize and restore the window with my window manager (xfce), the window appears but the interior contents disappear (only background color is shown) unless I include the event shown in the template code before, and even if I do that, I have to re-execute all of the commands used to produce the items in the window. This can be disastrous if the target computer is older and bitmaps are loaded pixel by pixel (especially large-sized bitmaps).
So rather than doing all of this reloading everytime the window is minimized and then restored, is there some sort of basic X system function that can capture the window contents, and allow me to save the data to a variable (likely a pointer of type char*) then I can call a function with the same pointer to restore all of the contents of the window at once instead of placing each element (including individual bitmap pixels) one at a time?
And here's the template code:
XEvent e;
Window w;
Display* dpy;
//events interested in
long EvCap=ExposureMask|ButtonPressMask|KeyPressMask|StructureNotifyMask;
void endx(){
// Unload stuff so memory is freed
}
void initx(){
// Load window and initial commands that don't need updating
}
void redrawWindowItems(){
// Stuff that needs to go on screen that disappears on minimize
}
int main(){
initx();
redrawWindowItems();
while(1){
if (XCheckWindowEvent(dpy,w,EvCap,&e)){
if (e.type==Expose && e.xexpose.count==0) {redrawWindowItems();} //redraw exposed windows
}
}
endx();
}
Upvotes: 0
Views: 250
Reputation: 18523
... is there some sort of basic X system function that can capture the window contents ...
You can create a pixmap using XCreatePixmap()
. The pixmap shall have the same depth as the window.
Use the XCopyArea()
function to copy the content of the window to the pixmap; when you receive the Expose
event, use XCopyArea()
to copy the content back to the window.
The function
of the GC
used shall be GXcopy
(which is the default when calling XCreateGC()
).
However, this is not the best idea:
If parts of the window are not visible when you "save" the content of the window, the "saved" data will be incomplete.
However, you can perform (nearly) all graphics functions (e.g. drawing) directly to a pixmap instead of a window.
Directly "draw" your image to the pixmap instead of the window.
Then call XCopyArea()
to show the image in the window the first time. (Whenever an Expose
event is received, call XCopyArea()
again.)
Upvotes: 1