home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / CLISP / CLISPSRC.TAR / clisp-1995-01-01 / src / winstat.d < prev    next >
Encoding:
Text File  |  1994-09-22  |  4.9 KB  |  140 lines

  1. # A status window, just like Xlisp's one.
  2. # It displays the memory usage.
  3.  
  4. #include <stdio.h> # declares sprintf()
  5.  
  6. local boolean status_active; # TRUE if status window on screen
  7. local mywindow status; # status window, or NULL when not on screen
  8.  
  9. #define status_width  22
  10. #define status_height  2
  11.  
  12. # Update the status window's contents.
  13. local void status_update (void);
  14. local void status_update()
  15.   { var mywindow w = status;
  16.     if (!w) return;
  17.    {var boolean modified = FALSE;
  18.     { var char buf[status_width+1];
  19.       sprintf(buf," Bytes used: %8ld ",used_space());
  20.       if (memcmp(w->text.contents[0],buf,status_width))
  21.         { memcpy(w->text.contents[0],buf,status_width); modified = TRUE; }
  22.     }
  23.     { var char buf[status_width+1];
  24.       sprintf(buf," Bytes free: %8ld ",free_space());
  25.       if (memcmp(w->text.contents[1],buf,status_width))
  26.         { memcpy(w->text.contents[1],buf,status_width); modified = TRUE; }
  27.     }
  28.     # No update is needed if the data to be displayed didn't change.
  29.     if (modified)
  30.       { # Force status window update:
  31.         begin_system_call();
  32.         InvalidateRect(w->hWnd,NULL,TRUE);
  33.         UpdateWindow(w->hWnd);
  34.         end_system_call();
  35.       }
  36.   }}
  37.  
  38. # The status window uses a timer to periodically update its contents.
  39. #define timer_id  1
  40.  
  41. # Switch status window active/inactive.
  42. local void status_on (void);
  43. local void status_off (void);
  44. local void status_on()
  45.   { status_active = TRUE;
  46.     begin_system_call();
  47.     if (!SetTimer(status->hWnd, # window which gets timer messages
  48.                   timer_id,     # timer identifier
  49.                   1000,         # timeout duration / ms
  50.                   NULL          # timer procedure
  51.        )         )
  52.       { status_active = FALSE; end_system_call(); return; } # failure to set the timer
  53.     ShowWindow(status->hWnd,SW_SHOWNOACTIVATE);
  54.     end_system_call();
  55.   }
  56. local void status_off()
  57.   { begin_system_call();
  58.     KillTimer(status->hWnd, # window associated with the timer
  59.               timer_id      # timer identifier
  60.              );
  61.     status_active = FALSE;
  62.     ShowWindow(status->hWnd,SW_HIDE);
  63.     end_system_call();
  64.   }
  65.  
  66. # The event handler for the status window.
  67. local long status_event (mywindow w, UINT message, WPARAM wParam, LPARAM lParam);
  68. local long status_event(w,message,wParam,lParam)
  69.   var mywindow w;
  70.   var UINT message;
  71.   var WPARAM wParam;
  72.   var LPARAM lParam;
  73.   { WINDEBUG( event_out("status_event",w->hWnd,w,message,wParam,lParam); )
  74.     switch (message)
  75.       { case WM_TIMER:
  76.           if (status_active) { status_update(); }
  77.           return 0;
  78.         case WM_CLOSE:
  79.           if (status_active) { status_off(); }
  80.           return 0;
  81.         default:
  82.           break;
  83.       }
  84.     return w->eventhandler3(w,message,wParam,lParam);
  85.   }
  86.  
  87. # Create the status window.
  88. local boolean status_create (HWND parent_hWnd);
  89. local boolean status_create(parent_hWnd)
  90.   var HWND parent_hWnd;
  91.   { var mywindow w;
  92.     status = NULL;
  93.     begin_system_call();
  94.     w = (struct mywindow *) malloc(sizeof(struct mywindow));
  95.     if (!w) { MessageBeep(MB_OK); end_system_call(); return FALSE; }
  96.    {var HWND hWnd =
  97.       CreateWindow(text_class_name,          # registered class name
  98.                    "CLISP (room)",           # window title
  99.                    WS_OVERLAPPED|WS_CAPTION, # window style
  100.                    CW_USEDEFAULT,            # horizontal position
  101.                    CW_USEDEFAULT,            # vertical position
  102.                    status_width*text_char_size.x # window width
  103.                     + 2*GetSystemMetrics(SM_CXBORDER),
  104.                    status_height*text_char_size.y # window height
  105.                     + 2*GetSystemMetrics(SM_CYBORDER)
  106.                     + GetSystemMetrics(SM_CYCAPTION),
  107.                    parent_hWnd,              # parent window
  108.                    NULL,                     # menu oder child window id
  109.                    global_hInstance,         # application instance
  110.                    global_lpszCmdLine        # window-creation data
  111.                   );
  112.     if (!hWnd)
  113.       { free(w); MessageBeep(MB_OK); end_system_call(); return FALSE; }
  114.     SetMenu(hWnd,NULL); # no menu on status window
  115.     # Set the background brush. (See the doc of WNDCLASS for the +1.)
  116.     SetClassWord(hWnd,GCW_HBRBACKGROUND,COLOR_WINDOW+1);
  117.     # Fill in w:
  118.     w->hWnd = hWnd;
  119.     w->in_focus = FALSE;
  120.     w->for_output = w->for_input = FALSE;
  121.     w->eventhandler1 = text_event;
  122.     w->eventhandler2 = status_event;
  123.     w->eventhandler3 = default_event;
  124.     w->text.width = status_width; w->text.height = status_height;
  125.     w->text.contents = malloc(status_height*sizeof(char*)); # check??
  126.     { var uintL y;
  127.       for (y = 0; y < status_height; y++)
  128.         { var uintL x;
  129.           var char* line = (char*)malloc(status_width*sizeof(char)); # check??
  130.           for (x = 0; x < status_width; x++) line[x] = ' ';
  131.           w->text.contents[y] = line;
  132.     }   }
  133.     w->text.in_marking = none;
  134.     end_system_call();
  135.     register_window(w);
  136.     status = w;
  137.     return TRUE;
  138.   }}
  139.  
  140.