+ Window->W = W; Window->H = H;
+
+ if(Window->RenderBuffer) {
+ free(Window->RenderBuffer);
+ Window->RenderBuffer = NULL;
+ }
+ WM_Invalidate(Window);
+
+ {
+ struct sWndMsg_Resize msg;
+ msg.W = W;
+ msg.H = H;
+ WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
+ }
+
+ return 0;
+}
+
+int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, const void *Data)
+{
+ if(Dest == NULL) return -2;
+ if(Length > 0 && Data == NULL) return -1;
+
+ if( Decorator_HandleMessage(Dest, Message, Length, Data) != 1 )
+ {
+ // TODO: Catch errors from ->HandleMessage
+ return 0;
+ }
+
+ // ->HandleMessage returns 1 when the message was not handled
+ if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
+ {
+ // TODO: Catch errors from ->HandleMessage
+ return 0;
+ }
+
+ // TODO: Implement message masking
+
+ if(Dest->Client)
+ {
+ uint32_t src_id;
+ if(!Source)
+ src_id = -1;
+ else if(Source->Client != Dest->Client) {
+ // TODO: Support different client source windows
+ _SysDebug("WM_SendMessage: TODO - Support inter-client messages");
+ return -1;
+ }
+ else {
+ src_id = Source->ID;
+ }
+
+ IPC_SendWMMessage(Dest->Client, src_id, Dest->ID, Message, Length, Data);
+ }
+
+ return 1;
+}
+
+void WM_Invalidate(tWindow *Window)
+{
+ if(!Window) return ;
+ _SysDebug("Invalidating %p", Window);
+ // Don't invalidate twice (speedup)
+// if( !(Window->Flags & WINFLAG_CLEAN) ) return;
+
+ // Mark for re-render
+ Window->Flags &= ~WINFLAG_CLEAN;
+
+ // Mark up the tree that a child window has changed
+ while( (Window = Window->Parent) )
+ Window->Flags &= ~WINFLAG_CHILDCLEAN;
+}
+
+// --- Rendering / Update
+void WM_int_UpdateWindow(tWindow *Window)
+{
+ int bDecoratorRedraw = 0;
+
+ // Ignore hidden windows
+ if( !(Window->Flags & WINFLAG_SHOW) )
+ return ;
+
+ // Render
+ if( !(Window->Flags & WINFLAG_CLEAN) )
+ {
+ // Calculate RealW/RealH
+ if( !(Window->Flags & WINFLAG_NODECORATE) )
+ {
+ _SysDebug("Applying decorations to %p", Window);
+ Decorator_UpdateBorderSize(Window);
+ Window->RealW = Window->BorderL + Window->W + Window->BorderR;
+ Window->RealH = Window->BorderT + Window->H + Window->BorderB;
+ bDecoratorRedraw = 1;
+ }
+ else
+ {
+ Window->BorderL = 0;
+ Window->BorderR = 0;
+ Window->BorderT = 0;
+ Window->BorderB = 0;
+ Window->RealW = Window->W;
+ Window->RealH = Window->H;
+ }
+
+ Window->Renderer->Redraw(Window);
+ Window->Flags |= WINFLAG_CLEAN;
+ }
+
+ // Process children
+ if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
+ {
+ tWindow *child;
+ for( child = Window->FirstChild; child; child = child->NextSibling )
+ {
+ WM_int_UpdateWindow(child);
+ }
+ Window->Flags |= WINFLAG_CHILDCLEAN;
+ }
+
+ if( bDecoratorRedraw )
+ Decorator_Redraw(Window);
+}
+
+void WM_int_BlitWindow(tWindow *Window)
+{
+ tWindow *child;
+
+ // Ignore hidden windows
+ if( !(Window->Flags & WINFLAG_SHOW) )
+ return ;
+
+// _SysDebug("Blit %p to (%i,%i) %ix%i", Window, Window->X, Window->Y, Window->RealW, Window->RealH);
+ Video_Blit(Window->RenderBuffer, Window->X, Window->Y, Window->RealW, Window->RealH);
+
+ if( Window == gpWM_FocusedWindow && Window->CursorW )
+ {
+ Video_FillRect(
+ Window->X + Window->BorderL + Window->CursorX,
+ Window->Y + Window->BorderT + Window->CursorY,
+ Window->CursorW, Window->CursorH,
+ 0x000000
+ );
+
+ }
+
+ for( child = Window->FirstChild; child; child = child->NextSibling )
+ {
+ WM_int_BlitWindow(child);
+ }
+}
+
+void WM_Update(void)
+{
+ // Don't redraw if nothing has changed
+ if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
+ return ;
+
+ // - Iterate through visible windows, updating them as needed
+ WM_int_UpdateWindow(gpWM_RootWindow);
+
+ // - Draw windows from back to front to the render buffer
+ WM_int_BlitWindow(gpWM_RootWindow);