]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/libwin32/winstat.cpp
Eliminate dependency on man2html.
[bacula/bacula] / bacula / src / win32 / libwin32 / winstat.cpp
1 /*
2  * Bacula File daemon Status Dialog box
3  *
4  *  Inspired from the VNC code by ATT.
5  *
6  * Copyright (2000) Kern E. Sibbald
7  *
8  */
9 /*
10    Copyright (C) 2000-2003 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License
14    as published by the Free Software Foundation; either version 2
15    of the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program; if not, write to the Free Software
24    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #include "bacula.h"
28 #include "winbacula.h"
29 #include "winstat.h"
30 #include "winres.h"
31
32 extern void output_status(void sendit(const char *msg, int len, void *sarg), void *arg);
33
34 bacStatus::bacStatus()
35 {
36    m_bVisible = FALSE;
37    m_hTextDisplay = NULL;
38 }
39
40 bacStatus::~bacStatus()
41 {
42 }
43
44 void
45 bacStatus::DisplayString(const char *msg, int len, void *context)
46 {
47    /* Get class pointer from user data */
48    bacStatus *_this = (bacStatus *)context;
49    const char *pStart;
50    const char *pCurrent;
51
52    for (pStart = msg, pCurrent = msg; ; pCurrent++) {
53       if (*pCurrent == '\n' || *pCurrent == '\0') {
54          int lenSubstring = pCurrent - pStart;
55          if (lenSubstring > 0) {
56             char *pSubString = (char *)alloca(lenSubstring + 1);
57             bstrncpy(pSubString, pStart, lenSubstring + 1);
58
59             SendMessage(_this->m_hTextDisplay, EM_SETSEL, (WPARAM)-1, (LPARAM)-1);
60             SendMessage(_this->m_hTextDisplay, EM_REPLACESEL, 0, (LPARAM)pSubString);
61          }
62          
63          if (*pCurrent == '\n') {
64             SendMessage(_this->m_hTextDisplay, EM_SETSEL, (WPARAM)-1, (LPARAM)-1);
65             SendMessage(_this->m_hTextDisplay, EM_REPLACESEL, 0, (LONG)"\r\n");
66          }
67
68          if (*pCurrent == '\0'){
69             break;
70          }
71          pStart = pCurrent + 1;
72       }
73    }
74 }
75
76 void 
77 bacStatus::UpdateDisplay()
78 {
79    if (m_hTextDisplay != NULL) {
80       long  lHorizontalPos = GetScrollPos(m_hTextDisplay, SB_HORZ);
81       long  lVerticalPos = GetScrollPos(m_hTextDisplay, SB_VERT);
82       long  selStart, selEnd;
83
84       SendMessage(m_hTextDisplay, EM_GETSEL, (WPARAM)&selStart, (LPARAM)&selEnd);
85
86       SetWindowText(m_hTextDisplay, "");
87
88       output_status(DisplayString, this);
89
90       SendMessage(m_hTextDisplay, EM_SETSEL, (WPARAM)selStart, (LPARAM)selEnd);
91       SendMessage(m_hTextDisplay, WM_HSCROLL, MAKEWPARAM(SB_THUMBPOSITION, lHorizontalPos), 0);
92       SendMessage(m_hTextDisplay, WM_VSCROLL, MAKEWPARAM(SB_THUMBPOSITION, lVerticalPos), 0);
93    }
94 }
95
96 /* Dialog box handling functions */
97 void
98 bacStatus::Show(BOOL show)
99 {
100    if (show && !m_bVisible) {
101       DialogBoxParam(hAppInstance, MAKEINTRESOURCE(IDD_STATUS), NULL,
102           (DLGPROC)DialogProc, (LONG)this);
103    }
104 }
105
106 void
107 bacStatus::ResizeChildren(HWND hDlg, WORD wWidth, WORD wHeight)
108 {
109    if (m_hTextDisplay != NULL) {
110       HWND  hwndButton = GetDlgItem(hDlg, IDOK);
111       RECT  rcWindow;
112
113       GetWindowRect(hwndButton, &rcWindow);
114
115       LONG  lButtonWidth = rcWindow.right - rcWindow.left;
116       LONG  lButtonHeight = rcWindow.bottom - rcWindow.top;
117
118       MoveWindow(m_hTextDisplay, 8, 8, wWidth - lButtonWidth - 24, wHeight - 16, TRUE);
119       MoveWindow(hwndButton, wWidth - lButtonWidth - 8, 8, lButtonWidth, lButtonHeight, TRUE);
120    }
121 }
122
123
124 BOOL CALLBACK
125 bacStatus::DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
126 {
127    /* Get class pointer from user data */
128    bacStatus *_this = (bacStatus *)GetWindowLong(hDlg, GWL_USERDATA);
129
130    switch (uMsg) {
131    case WM_INITDIALOG:
132       /* Set class pointer in user data */
133       SetWindowLong(hDlg, GWL_USERDATA, lParam);
134       _this = (bacStatus *)lParam;
135       _this->m_hTextDisplay = GetDlgItem(hDlg, IDC_TEXTDISPLAY);
136
137       /* show the dialog */
138       SetForegroundWindow(hDlg);
139
140       /* Update every 5 seconds */
141       SetTimer(hDlg, 1, 5000, NULL); 
142       _this->m_bVisible = TRUE;
143       _this->UpdateDisplay();
144       return TRUE;
145
146    case WM_TIMER:
147       _this->UpdateDisplay();
148       return TRUE;
149
150    case WM_SIZE:
151       _this->ResizeChildren(hDlg, LOWORD(lParam), HIWORD(lParam));
152       return TRUE;
153
154    case WM_COMMAND:
155       switch (LOWORD(wParam)) {
156       case IDCANCEL:
157       case IDOK:
158          KillTimer(hDlg, 1);
159          EndDialog(hDlg, TRUE);
160          _this->m_bVisible = FALSE;
161          return TRUE;
162       }
163       break;
164
165    case WM_DESTROY:
166       _this->m_hTextDisplay = NULL;
167       KillTimer(hDlg, 1);
168       EndDialog(hDlg, FALSE);
169       _this->m_bVisible = FALSE;
170       return TRUE;
171    }
172    return 0;
173 }