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 #define INCL_GPI
00039 #define INCL_WIN
00040 #include <os2.h>
00041 #include <string.h>
00042 
00043 #include "plugin.h"
00044 
00045 // return TRUE if we've never seen this MIME type before
00046 BOOL IsNewMimeType(PSZ mime)   
00047 {
00048   ULONG keysize = 512;
00049   char keybuf[512];
00050 
00051   PrfQueryProfileString(HINI_USERPROFILE, OS2INI_PLACE, mime, "", keybuf, keysize);
00052   if (keybuf[0] != '\0') {
00053     return FALSE;
00054   }
00055   else 
00056   {
00057     if (!(PrfWriteProfileString(HINI_USERPROFILE, OS2INI_PLACE, mime, "(none)")))
00058       WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, "Error adding MIME type value", "Default Plugin", 0, MB_OK);
00059     return TRUE;
00060   }
00061 }
00062 
00063 // string length in pixels for the specific window (selected font)
00064 static int getWindowStringLength(HWND hWnd, PSZ lpsz)
00065 {
00066   HPS hPS = WinGetPS(hWnd);
00067 
00068   POINTL ptls[5];
00069   GpiQueryTextBox(hPS, strlen(lpsz), lpsz, 5, ptls);
00070   POINTL pt;
00071   pt.x = ptls[TXTBOX_CONCAT].x;
00072   pt.y = ptls[TXTBOX_TOPLEFT].y - ptls[TXTBOX_BOTTOMLEFT].y;
00073   WinReleasePS(hPS);
00074   return (int)pt.x;
00075 }
00076 
00077 /****************************************************************/
00078 /*                                                              */
00079 /* void SetDlgItemTextWrapped(HWND hWnd, int iID, PSZ szText)   */
00080 /*                                                              */
00081 /* helper to wrap long lines in a static control, which do not  */
00082 /* wrap automatically if they do not have space characters      */
00083 /*                                                              */
00084 /****************************************************************/
00085 void SetDlgItemTextWrapped(HWND hWnd, int iID, PSZ szText)
00086 {
00087   HWND hWndStatic = WinWindowFromID(hWnd, iID);
00088   if(!szText || !*szText)
00089   {
00090     WinSetDlgItemText(hWnd, iID, "");
00091     return;
00092   }
00093 
00094   RECTL rc;
00095   WinQueryWindowRect(hWndStatic, &rc);
00096 
00097   int iStaticLength = rc.xRight - rc.xLeft;
00098   int iStringLength = getWindowStringLength(hWndStatic, szText);
00099 
00100   if(iStringLength <= iStaticLength)
00101   {
00102     WinSetDlgItemText(hWnd, iID, szText);
00103     return;
00104   }
00105 
00106   int iBreaks = iStringLength/iStaticLength;
00107   if(iBreaks <= 0)
00108     return;
00109 
00110   char * pBuf = new char[iStringLength + iBreaks + 1];
00111   if(pBuf == NULL)
00112     return;
00113 
00114   strcpy(pBuf, "");
00115 
00116   int iStart = 0;
00117   int iLines = 0;
00118   for(int i = 0; i < iStringLength; i++)
00119   {
00120     char * sz = &szText[iStart];
00121     int iIndex = i - iStart;
00122     char ch = sz[iIndex + 1];
00123 
00124     sz[iIndex + 1] = '\0';
00125 
00126     int iLength = getWindowStringLength(hWndStatic, sz);
00127 
00128     if(iLength < iStaticLength)
00129     {
00130       sz[iIndex + 1] = ch;
00131       if(iLines == iBreaks)
00132       {
00133         strcat(pBuf, sz);
00134         break;
00135       }
00136       continue;
00137     }
00138 
00139     sz[iIndex + 1] = ch;  // restore zeroed element
00140     i--;                  // go one step back
00141 
00142     ch = sz[iIndex];
00143     sz[iIndex] = '\0';    // terminate string one char shorter
00144 
00145     strcat(pBuf, sz);    // append the string
00146     strcat(pBuf, " ");   // append space character for successful wrapping
00147 
00148     iStart += strlen(sz);// shift new start position
00149     sz[iIndex] = ch;      // restore zeroed element
00150     iLines++;             // count lines
00151   }
00152 
00153   WinSetDlgItemText(hWnd, iID, pBuf);
00154 
00155   delete [] pBuf;
00156 }