Back to index

lightning-sunbird  0.9+nobinonly
gui_main.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "xp.h"
00039 #include <windowsx.h>
00040 
00041 #include "resource.h"
00042 #include "loggerw.h"
00043 #include "profilew.h"
00044 
00045 extern HINSTANCE hInst;
00046 extern char * ActionName[];
00047 extern char szAppName[];
00048 
00049 BOOL CALLBACK GeneralPageProc(HWND, UINT, WPARAM, LPARAM);
00050 BOOL CALLBACK LogPageProc(HWND, UINT, WPARAM, LPARAM);
00051 BOOL CALLBACK FilterPageProc(HWND, UINT, WPARAM, LPARAM);
00052 BOOL CALLBACK AdvancedPageProc(HWND, UINT, WPARAM, LPARAM);
00053 
00054 static void onOptions(HWND hWnd, Logger * logger)
00055 {
00056   if(!logger)
00057     return;
00058 
00059   PROPSHEETPAGE psp[4];
00060 
00061   psp[0].dwSize = sizeof(psp[0]);
00062   psp[0].dwFlags = PSP_DEFAULT;
00063   psp[0].hInstance = hInst;
00064   psp[0].pszTemplate = MAKEINTRESOURCE(IDD_PAGE_GENERAL);
00065   psp[0].pszIcon = 0;
00066   psp[0].pfnDlgProc = GeneralPageProc;
00067   psp[0].pszTitle = 0;
00068   psp[0].lParam = (LPARAM)logger;
00069   psp[0].pfnCallback = NULL;
00070 
00071   psp[1].dwSize = sizeof(psp[1]);
00072   psp[1].dwFlags = PSP_DEFAULT;
00073   psp[1].hInstance = hInst;
00074   psp[1].pszTemplate = MAKEINTRESOURCE(IDD_PAGE_LOG);
00075   psp[1].pszIcon = 0;
00076   psp[1].pfnDlgProc = LogPageProc;
00077   psp[1].pszTitle = 0;
00078   psp[1].lParam = (LPARAM)logger;
00079   psp[1].pfnCallback = NULL;
00080 
00081   psp[2].dwSize = sizeof(psp[2]);
00082   psp[2].dwFlags = PSP_DEFAULT;
00083   psp[2].hInstance = hInst;
00084   psp[2].pszTemplate = MAKEINTRESOURCE(IDD_PAGE_FILTER);
00085   psp[2].pszIcon = 0;
00086   psp[2].pfnDlgProc = FilterPageProc;
00087   psp[2].pszTitle = 0;
00088   psp[2].lParam = (LPARAM)logger;
00089   psp[2].pfnCallback = NULL;
00090 
00091   psp[3].dwSize = sizeof(psp[3]);
00092   psp[3].dwFlags = PSP_DEFAULT;
00093   psp[3].hInstance = hInst;
00094   psp[3].pszTemplate = MAKEINTRESOURCE(IDD_PAGE_ADVANCED);
00095   psp[3].pszIcon = 0;
00096   psp[3].pfnDlgProc = AdvancedPageProc;
00097   psp[3].pszTitle = 0;
00098   psp[3].lParam = (LPARAM)logger;
00099   psp[3].pfnCallback = NULL;
00100 
00101   PROPSHEETHEADER psh;
00102   psh.dwSize = sizeof(PROPSHEETHEADER);
00103   psh.dwFlags = PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
00104   psh.hwndParent = hWnd;
00105   psh.hInstance = hInst;
00106   psh.pszIcon = 0;
00107   psh.pszCaption = "Settings";
00108   psh.nPages = sizeof(psp) / sizeof(psp[0]);
00109   psh.nStartPage = 0;
00110   psh.ppsp = psp;
00111   psh.pfnCallback = NULL;
00112 
00113   logger->bSaveSettings = FALSE;
00114 
00115   int rv = PropertySheet(&psh);
00116   if(rv == -1)
00117     return;
00118 
00119   if(logger->bSaveSettings)
00120   {
00121     ProfileWin profile;
00122 
00123     if(hWnd != NULL)
00124     {
00125       RECT rc;
00126       if(GetWindowRect(hWnd, &rc))
00127         profile.setSizeAndPosition(rc.right - rc.left, rc.bottom - rc.top, rc.left, rc.top);
00128     }
00129 
00130     profile.setBool(NPSPY_REG_KEY_ONTOP, logger->bOnTop);
00131     profile.setBool(NPSPY_REG_KEY_LOGTOWINDOW, logger->bToWindow);
00132     profile.setBool(NPSPY_REG_KEY_LOGTOCONSOLE, logger->bToConsole);
00133     profile.setBool(NPSPY_REG_KEY_LOGTOFILE, logger->bToFile);
00134     profile.setBool(NPSPY_REG_KEY_SPALID, logger->bSPALID);
00135     profile.setString(NPSPY_REG_KEY_LOGFILENAME, logger->szFile);
00136 
00137     for(int i = 1; i < TOTAL_NUMBER_OF_API_CALLS; i++)
00138       profile.setBool(ActionName[i], !logger->bMutedCalls[i]);
00139 
00140     SetWindowPos(hWnd, logger->bOnTop ? HWND_TOPMOST : HWND_NOTOPMOST, 0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
00141   }
00142 
00143   logger->bSaveSettings = FALSE;
00144 }
00145 
00146 static void onCommand(HWND hWnd, int id, HWND hWndCtl, UINT codeNotify)
00147 {
00148   LoggerWin * logger = (LoggerWin *)GetWindowLong(hWnd, DWL_USER);
00149   switch (id)
00150   {
00151     case IDC_CHECK_MUTE:
00152       if(logger)
00153         logger->bMutedAll = (BST_CHECKED == IsDlgButtonChecked(hWnd, IDC_CHECK_MUTE));
00154       break;
00155     case IDC_BUTTON_OPTIONS:
00156       onOptions(hWnd, logger);
00157       break;
00158     case IDC_BUTTON_CLEAR:
00159       if(logger)
00160         logger->onClear();
00161       break;
00162     default:
00163       break;
00164   }
00165 }
00166 
00167 static BOOL onInitDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
00168 {
00169   LoggerWin * logger = (LoggerWin *)lParam;
00170   SetWindowLong(hWnd, DWL_USER, (long)logger);
00171   SetWindowText(hWnd, szAppName);
00172   HFONT hFont = GetStockFont(ANSI_FIXED_FONT);
00173   SetWindowFont(GetDlgItem(hWnd, IDC_MAIN_OUTPUT), hFont, FALSE);
00174 
00175   if(logger)
00176   {
00177     CheckDlgButton(hWnd, IDC_CHECK_MUTE, logger->bMutedAll ? BST_CHECKED : BST_UNCHECKED);
00178     if(logger->width && logger->height)
00179       SetWindowPos(hWnd, NULL, logger->x, logger->y, logger->width, logger->height, SWP_NOZORDER);
00180 
00181     SetWindowPos(hWnd, logger->bOnTop ? HWND_TOPMOST : HWND_NOTOPMOST, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE);
00182   }
00183   return TRUE;
00184 }
00185 
00186 static void onDestroy(HWND hWnd)
00187 {
00188   LoggerWin * logger = (LoggerWin *)GetWindowLong(hWnd, DWL_USER);
00189   if(logger)
00190     logger->onDestroyWindow();
00191 }
00192 
00193 static void onSize(HWND hWnd, UINT state, int cx, int cy)
00194 {
00195   long bu = GetDialogBaseUnits();
00196   int bux = LOWORD(bu);
00197   int buy = HIWORD(bu);
00198   int factorx = bux/4;
00199   int factory = buy/8;
00200 
00201   int marginLeft   = 7 * factorx;
00202   int marginRight  = 0 * factorx;
00203   int marginTop    = 0 * factory;
00204   int marginBottom = 7 * factory;
00205   int spaceHor     = 4 * factorx;
00206   int spaceVer     = 7 * factory;
00207 
00208   HWND hWndOutput = GetDlgItem(hWnd, IDC_MAIN_OUTPUT);
00209   HWND hWndCheckMute = GetDlgItem(hWnd, IDC_CHECK_MUTE);
00210   HWND hWndCheckOntop = GetDlgItem(hWnd, IDC_CHECK_ONTOP);
00211   HWND hWndButtonOptions = GetDlgItem(hWnd, IDC_BUTTON_OPTIONS);
00212   HWND hWndClear = GetDlgItem(hWnd, IDC_BUTTON_CLEAR);
00213 
00214   RECT rcMain;
00215   GetClientRect(hWnd, &rcMain);
00216 
00217   int width = rcMain.right - rcMain.left;
00218   int height = rcMain.bottom - rcMain.top;
00219 
00220   RECT rcButtonOptions;
00221   GetWindowRect(hWndButtonOptions, &rcButtonOptions);
00222   SetWindowPos(hWndButtonOptions, NULL, 
00223                width - marginLeft - rcButtonOptions.right + rcButtonOptions.left, 
00224                height - rcButtonOptions.bottom + rcButtonOptions.top - marginBottom, 
00225                0, 0, SWP_NOZORDER | SWP_NOSIZE);
00226 
00227   RECT rcClear;
00228   GetWindowRect(hWndClear, &rcClear);
00229   SetWindowPos(hWndClear, NULL, 
00230                width - marginLeft - rcClear.right + rcClear.left - rcButtonOptions.right + rcButtonOptions.left - spaceHor, 
00231                height - rcClear.bottom + rcClear.top - marginBottom, 
00232                0, 0, SWP_NOZORDER | SWP_NOSIZE);
00233 
00234   RECT rcCheckMute;
00235   GetWindowRect(hWndCheckMute, &rcCheckMute);
00236   SetWindowPos(hWndCheckMute, NULL, 
00237                marginLeft, 
00238                height - rcCheckMute.bottom + rcCheckMute.top - marginBottom, 
00239                0, 0, SWP_NOZORDER | SWP_NOSIZE);
00240 
00241   RECT rcCheckOntop;
00242   GetWindowRect(hWndCheckOntop, &rcCheckOntop);
00243   SetWindowPos(hWndCheckOntop, NULL, 
00244                marginLeft + rcCheckMute.right - rcCheckMute.left + spaceHor, 
00245                height - rcCheckOntop.bottom + rcCheckOntop.top - marginBottom, 
00246                0, 0, SWP_NOZORDER | SWP_NOSIZE);
00247 
00248   SetWindowPos(hWndOutput, NULL, 
00249                0, 0, 
00250                width, 
00251                height - rcButtonOptions.bottom + rcButtonOptions.top - marginBottom - marginTop - spaceVer, 
00252                SWP_NOZORDER | SWP_NOMOVE);
00253 
00254   // somehow the Clear button doesn't redraw itself well, so force it
00255   InvalidateRect(hWndClear, NULL, TRUE);
00256   UpdateWindow(hWndClear);
00257 }
00258 
00259 BOOL CALLBACK MainDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
00260 {
00261   switch(msg)
00262   {
00263     case WM_INITDIALOG:
00264       return (BOOL)HANDLE_WM_INITDIALOG(hWnd, wParam, lParam, onInitDialog);
00265     case WM_COMMAND:
00266       HANDLE_WM_COMMAND(hWnd, wParam, lParam, onCommand);
00267       break;
00268     case WM_CLOSE:
00269       DestroyWindow(hWnd);
00270       break;
00271     case WM_DESTROY:
00272       HANDLE_WM_DESTROY(hWnd, wParam, lParam, onDestroy);
00273       break;
00274     case WM_SIZE:
00275       HANDLE_WM_SIZE(hWnd, wParam, lParam, onSize);
00276       break;
00277 
00278     default:
00279       return FALSE;
00280   }
00281   return TRUE;
00282 }
00283 
00284 // This is exported function which allows to access Settings GUI from other applications
00285 void WINAPI SPY_Setup()
00286 {
00287   LoggerWin logger;
00288   ProfileWin profile;
00289 
00290   profile.getBool(NPSPY_REG_KEY_ONTOP, &logger.bOnTop);
00291   profile.getBool(NPSPY_REG_KEY_LOGTOWINDOW, &logger.bToWindow);
00292   profile.getBool(NPSPY_REG_KEY_LOGTOCONSOLE, &logger.bToConsole);
00293   profile.getBool(NPSPY_REG_KEY_LOGTOFILE, &logger.bToFile);
00294   profile.getBool(NPSPY_REG_KEY_SPALID, &logger.bSPALID);
00295   profile.getString(NPSPY_REG_KEY_LOGFILENAME, logger.szFile, strlen(logger.szFile));
00296 
00297   for(int i = 1; i < TOTAL_NUMBER_OF_API_CALLS; i++)
00298   {
00299     BOOL selected = TRUE;
00300     if(profile.getBool(ActionName[i], &selected))
00301       logger.bMutedCalls[i] = !selected;
00302   }
00303 
00304   onOptions(NULL, &logger);
00305 }