Back to index

lightning-sunbird  0.9+nobinonly
utils.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 Communicator client 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 <windows.h>
00039 #include <windowsx.h>
00040 
00041 #include "plugin.h"
00042 
00043 // open the registry, create if necessary
00044 HKEY openRegistry()      
00045 {
00046   HKEY phkResult;
00047 
00048   if(RegCreateKey(HKEY_CURRENT_USER, REGISTRY_PLACE, &phkResult) != ERROR_SUCCESS)
00049     MessageBox(0, "Error creating Default Plugin registry key", "Default Plugin", MB_OK);
00050 
00051   return phkResult;
00052 }
00053 
00054 // return TRUE if we've never seen this MIME type before
00055 BOOL IsNewMimeType(LPSTR mime)   
00056 {
00057   HKEY hkey = openRegistry();
00058   DWORD dwType, keysize = 512;
00059   char keybuf[512];
00060 
00061   if(RegQueryValueEx(hkey, mime, 0, &dwType, (LPBYTE) &keybuf, &keysize) == ERROR_SUCCESS)
00062   {
00063     // key exists, must have already been here...
00064     return FALSE;
00065   }
00066   else 
00067   {
00068     if(RegSetValueEx(hkey, mime, 0,  REG_SZ, (LPBYTE) "(none)", 7) != ERROR_SUCCESS)
00069       MessageBox(0, "Error adding MIME type value", "Default Plugin", MB_OK);
00070 
00071     return TRUE;
00072   }
00073 }
00074 
00075 // string length in pixels for the specific window (selected font)
00076 static int getWindowStringLength(HWND hWnd, LPSTR lpsz)
00077 {
00078   SIZE sz;
00079   HDC hDC = GetDC(hWnd);
00080   HFONT hWindowFont = GetWindowFont(hWnd);
00081   HFONT hFontOld = SelectFont(hDC, hWindowFont);
00082   GetTextExtentPoint32(hDC, lpsz, lstrlen(lpsz), &sz);
00083   POINT pt;
00084   pt.x = sz.cx;
00085   pt.y = sz.cy;
00086   LPtoDP(hDC, &pt, 1);
00087   SelectFont(hDC, hFontOld);
00088   ReleaseDC(hWnd, hDC);
00089   return (int)pt.x;
00090 }
00091 
00092 /****************************************************************/
00093 /*                                                              */
00094 /* void SetDlgItemTextWrapped(HWND hWnd, int iID, LPSTR szText) */
00095 /*                                                              */
00096 /* helper to wrap long lines in a static control, which do not  */
00097 /* wrap automatically if they do not have space characters      */
00098 /*                                                              */
00099 /****************************************************************/
00100 void SetDlgItemTextWrapped(HWND hWnd, int iID, LPSTR szText)
00101 {
00102   HWND hWndStatic = GetDlgItem(hWnd, iID);
00103   if((szText == NULL) || (lstrlen(szText) == 0))
00104   {
00105     SetDlgItemText(hWnd, iID, "");
00106     return;
00107   }
00108 
00109   RECT rc;
00110   GetClientRect(hWndStatic, &rc);
00111 
00112   int iStaticLength = rc.right - rc.left;
00113   int iStringLength = getWindowStringLength(hWndStatic, szText);
00114 
00115   if(iStringLength <= iStaticLength)
00116   {
00117     SetDlgItemText(hWnd, iID, szText);
00118     return;
00119   }
00120 
00121   int iBreaks = iStringLength/iStaticLength;
00122   if(iBreaks <= 0)
00123     return;
00124 
00125   char * pBuf = new char[iStringLength + iBreaks + 1];
00126   if(pBuf == NULL)
00127     return;
00128 
00129   lstrcpy(pBuf, "");
00130 
00131   int iStart = 0;
00132   int iLines = 0;
00133   for(int i = 0; i < iStringLength; i++)
00134   {
00135     char * sz = &szText[iStart];
00136     int iIndex = i - iStart;
00137     char ch = sz[iIndex + 1];
00138 
00139     sz[iIndex + 1] = '\0';
00140 
00141     int iLength = getWindowStringLength(hWndStatic, sz);
00142 
00143     if(iLength < iStaticLength)
00144     {
00145       sz[iIndex + 1] = ch;
00146       if(iLines == iBreaks)
00147       {
00148         lstrcat(pBuf, sz);
00149         break;
00150       }
00151       continue;
00152     }
00153 
00154     sz[iIndex + 1] = ch;  // restore zeroed element
00155     i--;                  // go one step back
00156 
00157     ch = sz[iIndex];
00158     sz[iIndex] = '\0';    // terminate string one char shorter
00159 
00160     lstrcat(pBuf, sz);    // append the string
00161     lstrcat(pBuf, " ");   // append space character for successful wrapping
00162 
00163     iStart += lstrlen(sz);// shift new start position
00164     sz[iIndex] = ch;      // restore zeroed element
00165     iLines++;             // count lines
00166   }
00167 
00168   SetDlgItemText(hWnd, iID, pBuf);
00169 
00170   delete [] pBuf;
00171 }