*/}}
Browse Source

Win32 progress bar

YimingWu 1 year ago
parent
commit
0f3a91173d
4 changed files with 79 additions and 13 deletions
  1. 9 1
      la_interface.h
  2. 62 8
      la_kernel.c
  3. 1 1
      la_tns.h
  4. 7 3
      la_tns_kernel.c

+ 9 - 1
la_interface.h

@@ -288,10 +288,16 @@ STRUCTURE(laExtraPreferencePage){
 
 STRUCTURE(laProgressDisplay){
     char String[256];
+    SYSWINDOW w;
 #ifdef __linux__
-    SYSWINDOW* w;
     GC gc;
     XGCValues gc_values;
+#endif
+#ifdef _WIN32
+    HBRUSH brush_bg;
+    HBRUSH brush_fg;
+    HPEN pen_fg;
+    HDC hdc;
 #endif
     laTimeRecorder TimeCalled;
     real p1,p2;
@@ -1611,6 +1617,7 @@ void logPrintNew(char* format, ...);
 
 #ifdef _WIN32
 LRESULT CALLBACK LA_WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
+LRESULT CALLBACK LA_ProgressWindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
 #endif
 
 void la_RegisterMainOperators();
@@ -1992,6 +1999,7 @@ void la_RefreshThemeColor(laTheme* th);
 #define LA_VEC2(c) (c)[0], (c)[1]
 #define LA_SET3(c, a1,a2,a3) { (c)[0]=a1; (c)[1]=a2; (c)[2]=a3; }
 #define LA_COLOR3_TO_HEX(c) (((int)((c)[0]*255)<<16)|((int)((c)[1]*255)<<8)|((int)((c)[2]*255)<<0))
+#define LA_COLOR3_TO_RGB(c) (RGB((int)((c)[0]*255),(int)((c)[1]*255),(int)((c)[2]*255)))
 
 int laIsInUiItem(laUiItem *ui, int x, int y);
 int laIsInBound(int x, int y, int l, int r, int u, int b);

+ 62 - 8
la_kernel.c

@@ -466,7 +466,6 @@ void logClear(){
 #define PROGRESSW (LA_RH*15)
 
 void laShowProgress(real p1, real p2){
-#ifdef __linux__
     laBoxedTheme *bt = _LA_THEME_TAB; real* fg=laThemeColor(bt,LA_BT_TEXT); real* bg=laThemeColor(bt,LA_BT_NORMAL);
     if(!MAIN.Progress.Called){
         laRecordTime(&MAIN.Progress.TimeCalled); MAIN.Progress.Called=1;
@@ -475,7 +474,9 @@ void laShowProgress(real p1, real p2){
         real t=laTimeElapsedSecondsf(&tm,&MAIN.Progress.TimeCalled); if(t<0.1) return;
         memcpy(&MAIN.Progress.TimeCalled,&tm,sizeof(laTimeRecorder));
         if(!MAIN.Progress.Shown){
-            int w=XDisplayWidth(MAIN.dpy, 0),h=XDisplayHeight(MAIN.dpy, 0); int ww=PROGRESSW+LA_RH*2;
+             int ww=PROGRESSW+LA_RH*2;
+#ifdef __linux__
+            int w=XDisplayWidth(MAIN.dpy, 0),h=XDisplayHeight(MAIN.dpy, 0);
             XMoveResizeWindow(MAIN.dpy,MAIN.Progress.w,w/2-ww/2,h/2-LA_RH*2/2,ww,LA_RH*2);
             long a=LA_COLOR3_TO_HEX(bg);
             XSetForeground(MAIN.dpy,MAIN.Progress.gc,LA_COLOR3_TO_HEX(fg));
@@ -483,28 +484,58 @@ void laShowProgress(real p1, real p2){
             XSetWindowBackground(MAIN.dpy,MAIN.Progress.w,LA_COLOR3_TO_HEX(bg));
             if(MAIN.CurrentWindow) XSetTransientForHint(MAIN.dpy,MAIN.Progress.w,MAIN.CurrentWindow->win);
             XMapWindow(MAIN.dpy,MAIN.Progress.w);
-            MAIN.Progress.Shown=1;
+#endif
+#ifdef _WIN32
+            int w=GetSystemMetrics(SM_CXFULLSCREEN), h=GetSystemMetrics(SM_CYFULLSCREEN);
+            SetWindowPos(MAIN.Progress.w, MAIN.CurrentWindow->win,w/2-ww/2,h/2-LA_RH*2/2,ww,LA_RH*2,0);
+            ShowWindow(MAIN.Progress.w,SW_SHOW);
+#endif
+            MAIN.Progress.Shown = 1;
         }
     }
-    XClearWindow(MAIN.dpy,MAIN.Progress.w);
     if(p1>=0) MAIN.Progress.p1=p1; if(p2>=0) MAIN.Progress.p2=p2;
+#ifdef __linux__
+    XClearWindow(MAIN.dpy,MAIN.Progress.w);
     XFillRectangle(MAIN.dpy,MAIN.Progress.w,MAIN.Progress.gc,LA_RH*2,0,PROGRESSW*MAIN.Progress.p1,LA_RH);
     XFillRectangle(MAIN.dpy,MAIN.Progress.w,MAIN.Progress.gc,LA_RH*2,LA_RH,PROGRESSW*MAIN.Progress.p2,LA_RH);
-    tnsDrawLCD7_ProgressX11(LA_RH*1.5,0,MAIN.Progress.p1);
-    tnsDrawLCD7_ProgressX11(LA_RH*1.5,LA_RH,MAIN.Progress.p2);
+    tnsDrawLCD7_ProgressSystem(LA_RH*1.5,0,MAIN.Progress.p1);
+    tnsDrawLCD7_ProgressSystem(LA_RH*1.5,LA_RH,MAIN.Progress.p2);
     XSync(MAIN.dpy, 1); XFlush(MAIN.dpy);
 #endif
+#ifdef _WIN32
+    InvalidateRect(MAIN.Progress.w,0,1);
+    if(!MAIN.Progress.brush_bg){ MAIN.Progress.brush_bg=CreateSolidBrush(LA_COLOR3_TO_RGB(bg)); }
+    if(!MAIN.Progress.brush_fg){ MAIN.Progress.brush_fg=CreateSolidBrush(LA_COLOR3_TO_RGB(fg)); }
+    if(!MAIN.Progress.pen_fg){ MAIN.Progress.pen_fg=CreatePen(PS_SOLID, 2, LA_COLOR3_TO_RGB(fg)); }
+    RECT rect; GetClientRect(MAIN.Progress.w, &rect);
+    PAINTSTRUCT ps; BeginPaint(MAIN.Progress.w,&ps);
+    SelectObject(MAIN.Progress.hdc, MAIN.Progress.brush_bg);
+    Rectangle(MAIN.Progress.hdc, 0, 0, rect.right, rect.bottom);
+    SelectObject(MAIN.Progress.hdc, MAIN.Progress.brush_fg);
+    Rectangle(MAIN.Progress.hdc,LA_RH*2,0,LA_RH*2+PROGRESSW*MAIN.Progress.p1,LA_RH);
+    Rectangle(MAIN.Progress.hdc,LA_RH*2,LA_RH,LA_RH*2+PROGRESSW*MAIN.Progress.p2,LA_RH+LA_RH);
+    SelectObject(MAIN.Progress.hdc, MAIN.Progress.pen_fg);
+    tnsDrawLCD7_ProgressSystem(LA_RH*1.5,0,MAIN.Progress.p1);
+    tnsDrawLCD7_ProgressSystem(LA_RH*1.5,LA_RH,MAIN.Progress.p2);
+    EndPaint(MAIN.Progress.w,&ps);
+    MSG msg;
+    while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)){ TranslateMessage(&msg); DispatchMessage(&msg); };
+#endif
 }
 void laHideProgress(){
-#ifdef __linux__
     if(!MAIN.Progress.Shown){
         MAIN.Progress.Called=0; return;
     }
     laTimeRecorder tm; laRecordTime(&tm);
     real t=laTimeElapsedSecondsf(&tm,&MAIN.Progress.TimeCalled);
     if(t<0.2){ usleep((TNS_MIN2(0.2-t,0.2))*1000000); }
-    XUnmapWindow(MAIN.dpy,MAIN.Progress.w); XSync(MAIN.dpy, 1); XFlush(MAIN.dpy);
     MAIN.Progress.Called = MAIN.Progress.Shown = 0;
+#ifdef __linux__
+    XUnmapWindow(MAIN.dpy,MAIN.Progress.w); XSync(MAIN.dpy, 1); XFlush(MAIN.dpy);
+#endif
+#ifdef _WIN32
+    ShowWindow(MAIN.Progress.w,SW_HIDE);
+    if(MAIN.CurrentWindow) UpdateWindow(MAIN.CurrentWindow->win);
 #endif
 }
 
@@ -522,6 +553,26 @@ void la_InitProgressWindow(){
     XSetLineAttributes(MAIN.dpy, MAIN.Progress.gc, 2, LineSolid, CapButt, JoinBevel);
     XSync(MAIN.dpy,0);
 #endif
+#ifdef _WIN32
+    WNDCLASSW wc;
+    wc.hInstance = MAIN.hinstance;
+    wc.lpfnWndProc = LA_ProgressWindowProc;
+    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
+    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
+    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
+    wc.cbWndExtra = 0;
+    wc.cbClsExtra = 0;
+    wc.lpszClassName = L"_LAGUIPROGRESS";
+    wc.lpszMenuName = NULL;
+    wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
+
+    if (!RegisterClassW(&wc)){ return; }
+    
+    MAIN.Progress.w=CreateWindowW(L"_LAGUIPROGRESS",L"Progress", WS_POPUP, 0,0,PROGRESSW+LA_RH*2,LA_RH*2,0,0,MAIN.hinstance,0);
+
+    MAIN.Progress.hdc=GetDC(MAIN.Progress.w);
+
+#endif
 }
 
 void laSetFontFolderPath(char* absolute){
@@ -6676,6 +6727,9 @@ LRESULT CALLBACK LA_WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lp
     }
     return DefWindowProc(hwnd, message, wparam, lparam);
 }
+LRESULT CALLBACK LA_ProgressWindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam){
+    return DefWindowProc(hwnd, message, wparam, lparam);
+}
 #endif
 
 int la_UpdateOperatorHints(laWindow* w){

+ 1 - 1
la_tns.h

@@ -1342,7 +1342,7 @@ int tnsStringGetDimension(char* content, uint32_t* contentU, int Count, int WLim
 int tnsStringGetWidth(char *content, int Count, int UseMono);
 int tnsStringGetWidthU(uint32_t *contentU, int Count, int UseMono);
 void tnsDrawStringM(char *content, uint32_t* contentU, real Color[4], int L, int R, int T, int Flags);
-int tnsDrawLCD7_ProgressX11(real x, real y, real Percent);
+int tnsDrawLCD7_ProgressSystem(real x, real y, real Percent);
 void tnsDrawStringLCD(char *content, uint32_t* contentU, real Color[4], int L, int R, int T, int Flags);
 void tnsDrawStringAutoM(char *content, uint32_t* contentU, real Color[4], int L, int R, int T, int Flags);
 void tnsDrawStringAuto(char *content, real Color[4], int L, int R, int T, int Flags);

+ 7 - 3
la_tns_kernel.c

@@ -2961,8 +2961,7 @@ int tnsStringGetWidthU(uint32_t *contentU, int Count, int UseMono){
     return tnsStringGetDimension(0, contentU, Count, 0, 0, UseMono);
 }
 
-int tnsDrawLCD7_ProgressX11(real x, real y, real Percent){
-#ifdef __linux__
+int tnsDrawLCD7_ProgressSystem(real x, real y, real Percent){
     real hgap=LA_RH/15; real vgap=LA_RH/5;
     real MA=FM->UsingFont->MonoAdvance;
     real w=MA-hgap*2, h=LA_RH-vgap*2; y+=vgap; x+=hgap-MA; real shear=h/12;
@@ -2976,15 +2975,20 @@ int tnsDrawLCD7_ProgressX11(real x, real y, real Percent){
             if(TNS_LCD_MAP_7[uc][i]){
                 real* seg=TNS_LCD_SEG_7[i];
                 real s1=tnsInterpolate(shear,-shear,seg[1]); real s2=tnsInterpolate(shear,-shear,seg[3]);
+#ifdef __linux__
                 XDrawLine(MAIN.dpy,MAIN.Progress.w,MAIN.Progress.gc,
                     tnsInterpolate(x+s1,x+w+s1,seg[0]),tnsInterpolate(y,y+h,seg[1]),
                     tnsInterpolate(x+s2,x+w+s2,seg[2]),tnsInterpolate(y,y+h,seg[3]));
+#endif
+#ifdef _WIN32
+                MoveToEx(MAIN.Progress.hdc,tnsInterpolate(x+s1,x+w+s1,seg[0]),tnsInterpolate(y,y+h,seg[1]),0);
+                LineTo(MAIN.Progress.hdc,tnsInterpolate(x+s2,x+w+s2,seg[2]),tnsInterpolate(y,y+h,seg[3]));
+#endif
             }
         }
         x-=MA;
     }
     return 1;
-#endif
     return 0;
 }
 int tnsMakeLCD7(real x, real y, real w, real h, int ch){