1 #ifndef __CUIWINDOW_H__ 2 #define __CUIWINDOW_H__ 3 4 #include <TCFoundation/TCAlertSender.h> 5 #include <windows.h> 6 #include <CommCtrl.h> 7 //#include <windowsx.h> 8 #include <CUI/CUIDefines.h> 9 #include <TCFoundation/TCTypedPointerArray.h> 10 #include <TCFoundation/TCStlIncludes.h> 11 #include <TCFoundation/mystring.h> 12 13 #ifndef WM_THEMECHANGED 14 #define WM_THEMECHANGED 0x031A 15 #endif 16 #ifndef WM_DPICHANGED 17 #define WM_DPICHANGED 0x02E0 18 #endif 19 20 typedef TCTypedPointerArray<HWND> CUIHwndArray; 21 22 typedef std::map<UINT, UINT> UIntUIntMap; 23 24 #define CUI_MAX_CHILDREN 100 25 #define NUM_SYSTEM_COLORS 25 26 27 class CUIScaler; 28 class TCImage; 29 30 #ifdef TC_NO_UNICODE 31 #define LPNMTTDISPINFOUC LPNMTTDISPINFOA 32 #define LPNMTBGETINFOTIPUC LPNMTBGETINFOTIPA 33 34 #define TTN_GETDISPINFOUC TTN_GETDISPINFOA 35 #define TBN_GETDISPINFOUC TBN_GETDISPINFOA 36 #define TBN_GETINFOTIPUC TBN_GETINFOTIPA 37 #define TBN_GETBUTTONINFOUC TBN_GETBUTTONINFOA 38 #define TVN_ITEMEXPANDINGUC TVN_ITEMEXPANDINGA 39 #define TVN_SELCHANGEDUC TVN_SELCHANGEDA 40 #define TVITEMEXUC TVITEMEXA 41 #define TVINSERTSTRUCTUC TVINSERTSTRUCTA 42 #define TVM_INSERTITEMUC TVM_INSERTITEMA 43 #define MENUITEMINFOUC MENUITEMINFOA 44 45 #define MENUITEMINFOUC MENUITEMINFOA 46 #define OPENFILENAMEUC OPENFILENAMEA 47 #define OPENFILENAME_NT4UC OPENFILENAME_NT4A 48 #define WC_BUTTONUC WC_BUTTONA 49 #define WC_STATICUC WC_STATICA 50 #define WC_EDITUC WC_EDITA 51 #define WC_COMBOBOXUC WC_COMBOBOXA 52 #define TOOLTIPS_CLASSUC TOOLTIPS_CLASSA 53 #define TOOLINFOUC TOOLINFOA 54 #define NMTOOLBARUC NMTOOLBARA 55 #define MAKEINTRESOURCEUC MAKEINTRESOURCEA 56 #define CreateDialogParamUC CreateDialogParamA 57 #define DialogBoxParamUC DialogBoxParamA 58 #define GetWindowLongPtrUC GetWindowLongPtrA 59 #define SetWindowLongPtrUC SetWindowLongPtrA 60 #define GetMenuItemInfoUC GetMenuItemInfoA 61 #define SetMenuItemInfoUC SetMenuItemInfoA 62 #else // TC_NO_UNICODE 63 #define LPNMTTDISPINFOUC LPNMTTDISPINFOW 64 #define LPNMTBGETINFOTIPUC LPNMTBGETINFOTIPW 65 66 #define TTN_GETDISPINFOUC TTN_GETDISPINFOW 67 #define TBN_GETDISPINFOUC TBN_GETDISPINFOW 68 #define TBN_GETINFOTIPUC TBN_GETINFOTIPW 69 #define TBN_GETBUTTONINFOUC TBN_GETBUTTONINFOW 70 #define TVN_ITEMEXPANDINGUC TVN_ITEMEXPANDINGW 71 #define TVN_SELCHANGEDUC TVN_SELCHANGEDW 72 #define TVITEMEXUC TVITEMEXW 73 #define TVINSERTSTRUCTUC TVINSERTSTRUCTW 74 #define TVM_INSERTITEMUC TVM_INSERTITEMW 75 #define MENUITEMINFOUC MENUITEMINFOW 76 77 #define MENUITEMINFOUC MENUITEMINFOW 78 #define OPENFILENAMEUC OPENFILENAMEW 79 #define OPENFILENAME_NT4UC OPENFILENAME_NT4W 80 #define WC_BUTTONUC WC_BUTTONW 81 #define WC_STATICUC WC_STATICW 82 #define WC_EDITUC WC_EDITW 83 #define WC_COMBOBOXUC WC_COMBOBOXW 84 #define TOOLTIPS_CLASSUC TOOLTIPS_CLASSW 85 #define TOOLINFOUC TOOLINFOW 86 #define NMTOOLBARUC NMTOOLBARW 87 #define MAKEINTRESOURCEUC MAKEINTRESOURCEW 88 #define CreateDialogParamUC CreateDialogParamW 89 #define DialogBoxParamUC DialogBoxParamW 90 #define GetWindowLongPtrUC GetWindowLongPtrW 91 #define SetWindowLongPtrUC SetWindowLongPtrW 92 #define GetMenuItemInfoUC GetMenuItemInfoW 93 #define SetMenuItemInfoUC SetMenuItemInfoW 94 #endif // TC_NO_UNICODE 95 96 bool haveWindowsXPOrLater(void); 97 bool haveWindowsVistaOrLater(void); 98 bool haveWindows7OrLater(void); 99 100 class CUIExport CUIWindow : public TCAlertSender 101 { 102 public: 103 CUIWindow(void); 104 CUIWindow(CUCSTR windowTitle, HINSTANCE hInstance, int x, 105 int y, int width, int height); 106 CUIWindow(CUIWindow* parentWindow, int x, int y, int width, 107 int height); 108 CUIWindow(HWND hParentWindow, HINSTANCE, int x, int y, 109 int width, int height); 110 getHWindow(void)111 HWND getHWindow(void) { return hWindow; } getX(void)112 int getX(void) { return x; } getY(void)113 int getY(void) { return y; } getWidth(void)114 int getWidth(void) { return width; } getHeight(void)115 int getHeight(void) { return height; } getHInstance(void)116 HINSTANCE getHInstance(void) { return hInstance; } isInitialized(void)117 BOOL isInitialized(void) { return initialized; } getWindowTitle(void)118 CUCSTR getWindowTitle(void) { return windowTitle; } 119 virtual void showWindow(int nCmdShow); 120 virtual BOOL initWindow(void); 121 virtual void resize(int newWidth, int newHeight); 122 virtual void setRBGFillColor(BYTE r, BYTE g, BYTE b); 123 virtual void setFillColor(DWORD newColor); 124 virtual void setTitle(CUCSTR value); setMinWidth(int value)125 void setMinWidth(int value) { minWidth = value; } setMinHeight(int value)126 void setMinHeight(int value) { minHeight = value; } 127 void setMinSize(int width, int height, bool unscale = false); setMaxWidth(int value)128 void setMaxWidth(int value) { maxWidth = value; } setMaxHeight(int value)129 void setMaxHeight(int value) { maxHeight = value; } 130 virtual SIZE getDecorationSize(HMONITOR hMonitor = NULL); 131 virtual void runDialogModal(HWND hDlg, bool allowMessages = false); 132 virtual bool flushModal(HWND hWnd, bool isDialog, int maxFlush = -1); 133 virtual bool flushDialogModal(HWND hDlg); 134 virtual void closeWindow(void); 135 virtual void maximize(void); 136 virtual void minimize(void); 137 virtual void restore(void); 138 virtual UINT_PTR setTimer(UINT timerID, UINT elapse); 139 virtual BOOL killTimer(UINT timerID); getHParentWindow(void)140 HWND getHParentWindow(void) { return hParentWindow; } 141 virtual HWND createDialog(UCSTR templateName, 142 BOOL asChildWindow = TRUE, DLGPROC dialogProc = staticDialogProc, 143 LPARAM lParam = 0); 144 virtual HWND createDialog(int templateNumber, 145 BOOL asChildWindow = TRUE, DLGPROC dialogProc = staticDialogProc, 146 LPARAM lParam = 0); 147 virtual void doDialogClose(HWND hDlg); 148 virtual LRESULT windowProc(HWND hWnd, UINT message, 149 WPARAM wParam, LPARAM lParam); 150 virtual INT_PTR dialogProc(HWND hDlg, UINT message, 151 WPARAM wParam, LPARAM lParam); 152 virtual void setMenuItemsEnabled(HMENU hMenu, bool enabled); 153 virtual void setMenuEnabled(HMENU hParentMenu, int itemID, 154 bool enabled, BOOL byPosition = FALSE); 155 virtual HBITMAP createDIBSection(HDC hBitmapDC, int bitmapWidth, 156 int bitmapHeight, BYTE **bmBuffer, bool force32 = false); 157 virtual bool getBitmapSize(HBITMAP hBitmap, SIZE& size); 158 virtual int addImageToImageList(HIMAGELIST hImageList, TCImage *image, 159 const SIZE& size); 160 virtual int addImageToImageList(HIMAGELIST hImageList, int resourceId, 161 const SIZE& size, double scaleFactor = 1.0); 162 virtual double getScaleFactor(bool recalculate = false, 163 UINT *dpiX = NULL, UINT *dpiY = NULL); 164 int scalePoints(int points); 165 int unscalePixels(int pixels); 166 167 static void setMenuCheck(HMENU hParentMenu, UINT uItem, bool checked, 168 bool radio = false); 169 static void setMenuRadioCheck(HMENU hParentMenu, UINT uItem, 170 bool checked); 171 static bool getMenuCheck(HMENU hParentMenu, UINT uItem); 172 static void setArrowCursor(void); 173 static void setWaitCursor(void); 174 static HMENU findSubMenu(HMENU hParentMenu, int subMenuIndex, 175 int *index = NULL); 176 static HINSTANCE getLanguageModule(void); 177 static HWND createWindowExUC(DWORD dwExStyle, CUCSTR lpClassName, 178 CUCSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, 179 int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, 180 LPVOID lpParam); 181 static BOOL setWindowTextUC(HWND hWnd, CUCSTR text); 182 static LRESULT sendMessageUC(HWND hWnd, UINT uMsg, WPARAM wParam, 183 LPARAM lParam); 184 static LRESULT sendDlgItemMessageUC(HWND hDlg, int nIDDlgItem, 185 UINT uMsg, WPARAM wParam, LPARAM lParam); 186 static HWND createStatusWindowUC(LONG style, CUCSTR lpszText, 187 HWND hwndParent, UINT wID); 188 static int messageBoxUC(HWND hWnd, CUCSTR lpText, CUCSTR lpCaption, 189 UINT uType); 190 static BOOL insertMenuItemUC(HMENU hmenu, UINT item, BOOL fByPosition, 191 MENUITEMINFOUC *lpmi); 192 static BOOL getOpenFileNameUC(OPENFILENAMEUC *lpofn); 193 static BOOL getTextExtentPoint32UC(HDC hdc, CUCSTR lpString, 194 int cbString, LPSIZE lpSize); 195 static BOOL screenToClient(HWND hWnd, RECT *rect); 196 static BOOL clientToScreen(HWND hWnd, RECT *rect); 197 static LRESULT CALLBACK staticWindowProc(HWND hWnd, 198 UINT message, WPARAM wParam, LPARAM lParam); 199 static INT_PTR CALLBACK staticDialogProc(HWND hDlg, 200 UINT message, WPARAM wParam, LPARAM lParam); 201 202 virtual bool statusBarSetParts(HWND hStatusBar, WPARAM numParts, 203 int *parts, bool scale = true); 204 static bool statusBarSetText(HWND hWnd, TCByte part, CUCSTR text, 205 WORD flags = 0); 206 static bool statusBarSetText(HWND hWnd, TCByte part, 207 const ucstring &text, WORD flags = 0); 208 static WORD statusBarGetText(HWND hWnd, TCByte part, ucstring &text); 209 static void statusBarSetTipText(HWND hWnd, TCByte part, CUCSTR text); 210 static void statusBarSetTipText(HWND hWnd, TCByte part, 211 const ucstring &text); 212 static bool statusBarSetIcon(HWND hWnd, TCByte part, HICON hIcon); 213 214 static void windowGetText(HWND hWnd, ucstring &text); 215 static void windowSetText(HWND hWnd, const ucstring &text); 216 static void windowSetText(HWND hWnd, CUCSTR text); 217 static bool windowGetValue(HWND hWnd, long &value); 218 static bool windowGetValue(HWND hWnd, int &value); 219 static bool windowGetValue(HWND hWnd, float &value); 220 static bool windowGetValue(HWND hWnd, double &value); 221 222 static void listBoxResetContent(HWND hWnd); 223 static void listBoxGetText(HWND hWnd, int index, ucstring &text); 224 static void listBoxSelectString(HWND hWnd, CUCSTR text); 225 static void listBoxSelectString(HWND hWnd, const ucstring &text); 226 static int listBoxFindStringExact(HWND hWnd, CUCSTR text); 227 static int listBoxFindStringExact(HWND hWnd, const ucstring &text); 228 static void listBoxSetCurSel(HWND hWnd, int index); 229 static int listBoxGetCurSel(HWND hWnd); 230 static int listBoxGetCount(HWND hWnd); 231 static int listBoxAddString(HWND hWnd, CUCSTR text); 232 static int listBoxAddString(HWND hWnd, const ucstring &text); 233 static void listBoxInsertString(HWND hWnd, int index, CUCSTR text); 234 static void listBoxInsertString(HWND hWnd, int index, 235 const ucstring &text); 236 static void listBoxDeleteString(HWND hWnd, int index); 237 static bool checkGet(HWND hWnd); 238 static void checkSet(HWND hWnd, bool value); 239 240 static int comboAddString(HWND hWnd, CUCSTR string); 241 static int comboAddString(HWND hWnd, const ucstring &string); 242 static int comboDeleteString(HWND hDlg, int index); 243 static int comboSelectString(HWND hDlg, int startIndex, CUCSTR string); 244 static int comboSelectString(HWND hDlg, int startIndex, 245 const ucstring &string); 246 static void comboResetContent(HWND hDlg); 247 static int comboSetCurSel(HWND hWnd, int index); 248 static int comboGetCurSel(HWND hWnd); 249 static const std::string notificationName(UINT code); 250 static int comboGetCount(HWND hWnd); 251 252 static void buttonSetCheck(HWND hWnd, int state); 253 static void buttonSetChecked(HWND hWnd, bool checked); 254 static int buttonGetCheck(HWND hWnd); 255 static bool buttonIsChecked(HWND hWnd); 256 static HBITMAP buttonSetBitmap(HWND hWnd, HBITMAP hBitmap); 257 static HICON buttonSetIcon(HWND hWnd, HICON hIcon); 258 static HBITMAP buttonGetBitmap(HWND hWnd); 259 static HICON buttonGetIcon(HWND hWnd); 260 static void buttonSetStyle(HWND hWnd, DWORD dwStyle, 261 bool redraw = true); 262 263 static int progressBarSetPos(HWND hWnd, int pos); 264 static int progressBarGetPos(HWND hWnd); 265 266 static int trackBarGetPos(HWND hWnd); 267 268 #ifndef TC_NO_UNICODE 269 static void addFileType(char *fileTypes, const char *description, 270 const char *filter); 271 #endif // TC_NO_UNICODE 272 static void addFileType(UCSTR fileTypes, CUCSTR description, 273 CUCSTR filter); 274 static int getOpenFilenameSize(bool uc); 275 276 static int calcTextHeight(HDC hdc, const ucstring &text, int width, 277 int &optimalWidth); 278 static int calcCheckHeight(HWND hCheck, HDC hdc, int checkBoxWidth, 279 int width, int &optimalWidth); 280 static void fixDialogSizes(HWND hDlg); 281 static void fixControlSize(HWND hWnd); 282 static void fixControlSize(HWND hWnd, HDC hdc, bool force = false); 283 284 protected: 285 ~CUIWindow(void); 286 virtual void dealloc(void); 287 void init(void); 288 virtual void registerWindowClass(void); 289 virtual BOOL createWindow(void); 290 virtual BOOL createMainWindow(void); 291 virtual BOOL createSubWindow(void); 292 virtual const UCCHAR* windowClassName(void); 293 virtual WNDCLASSEX getWindowClass(void); 294 virtual void addChild(CUIWindow* childWindow); 295 virtual void removeChild(CUIWindow* childWindow); 296 virtual void updateSystemColors(void); 297 void initSystemColors(void); 298 void initUcMessages(void); 299 virtual void deleteSystemColorPens(void); 300 virtual void redrawChildren(BOOL recurse); 301 virtual HBRUSH getBackgroundBrush(void); 302 303 virtual LRESULT doSize(WPARAM sizeType, int newWidth, 304 int newHeight); 305 virtual LRESULT doMove(int newX, int newY); 306 virtual LRESULT doEraseBackground(RECT* updateRect = NULL); 307 virtual LRESULT doCreate(HWND hWnd, 308 LPCREATESTRUCT createStruct); 309 virtual LRESULT doClose(void); 310 virtual LRESULT doDestroy(void); 311 virtual LRESULT doNCDestroy(void); 312 virtual void doPaint(void); 313 virtual void doPostPaint(void); 314 virtual void doSystemColorChange(void); 315 virtual LRESULT doLButtonDown(WPARAM keyFlags, int xPos, 316 int yPos); 317 virtual LRESULT doLButtonUp(WPARAM keyFlags, int xPos, 318 int yPos); 319 virtual LRESULT doLButtonDoubleClick(WPARAM keyFlags, 320 int xPos, int yPos); 321 virtual LRESULT doRButtonDown(WPARAM keyFlags, int xPos, 322 int yPos); 323 virtual LRESULT doRButtonUp(WPARAM keyFlags, int xPos, 324 int yPos); 325 virtual LRESULT doRButtonDoubleClick(WPARAM keyFlags, 326 int xPos, int yPos); 327 virtual LRESULT doMButtonDown(WPARAM keyFlags, int xPos, 328 int yPos); 329 virtual LRESULT doMButtonUp(WPARAM keyFlags, int xPos, 330 int yPos); 331 virtual LRESULT doMButtonDoubleClick(WPARAM keyFlags, 332 int xPos, int yPos); 333 virtual LRESULT doMouseMove(WPARAM keyFlags, int xPos, 334 int yPos); 335 virtual LRESULT doMouseWheel(short keyFlags, short zDelta, 336 int xPos, int yPos); 337 virtual LRESULT doCaptureChanged(HWND hNewWnd); 338 virtual LRESULT doKeyDown(int keyCode, LPARAM keyData); 339 virtual LRESULT doKeyUp(int keyCode, LPARAM keyData); 340 virtual LRESULT doDropFiles(HDROP hDrop); 341 virtual LRESULT doChar(UCCHAR characterCode, LPARAM keyData); 342 virtual LRESULT doShowWindow(BOOL showFlag, LPARAM status); 343 virtual LRESULT doActivateApp(BOOL activateFlag, 344 DWORD threadId); 345 virtual LRESULT doActivate(int activateFlag, BOOL minimized, 346 HWND previousHWindow); 347 virtual LRESULT doGetMinMaxInfo(HWND hWnd, 348 LPMINMAXINFO minMaxInfo); 349 virtual LRESULT doCommand(int itemId, int notifyCode, 350 HWND controlHWnd); 351 virtual LRESULT doTimer(UINT_PTR timerID); 352 virtual LRESULT doHelp(LPHELPINFO helpInfo); 353 virtual LRESULT doMenuSelect(UINT menuID, UINT flags, HMENU hMenu); 354 virtual LRESULT doEnterMenuLoop(bool isTrackPopupMenu); 355 virtual LRESULT doExitMenuLoop(bool isTrackPopupMenu); 356 virtual LRESULT doInitMenuPopup(HMENU hPopupMenu, UINT uPos, 357 BOOL fSystemMenu); 358 virtual LRESULT doDrawItem(HWND hControlWnd, 359 LPDRAWITEMSTRUCT drawItemStruct); 360 virtual LRESULT doThemeChanged(void); 361 virtual LRESULT doNotify(int controlId, LPNMHDR notification); 362 virtual LRESULT doDpiChanged(int dpiX, int dpiY, RECT* proposedRect); handleDpiChange(void)363 virtual bool handleDpiChange(void) { return true; } 364 virtual BOOL doDialogThemeChanged(void); 365 virtual BOOL doDialogCtlColorStatic(HDC hdcStatic, HWND hwndStatic); 366 virtual BOOL doDialogCtlColorBtn(HDC hdcStatic, HWND hwndStatic); 367 virtual BOOL doDialogCommand(HWND hDlg, int controlId, 368 int notifyCode, HWND controlHWnd); 369 virtual BOOL doDialogVScroll(HWND hDlg, int scrollCode, 370 int position, HWND hScrollBar); 371 virtual BOOL doDialogHScroll(HWND hDlg, int scrollCode, 372 int position, HWND hScrollBar); 373 virtual BOOL doDialogInit(HWND hDlg, HWND hFocusWindow, LPARAM lParam); 374 virtual BOOL doDialogNotify(HWND hDlg, int controlId, 375 LPNMHDR notification); 376 virtual BOOL doDialogSize(HWND hDlg, WPARAM sizeType, 377 int newWidth, int newHeight); 378 virtual BOOL doDialogGetMinMaxInfo(HWND hDlg, 379 LPMINMAXINFO minMaxInfo); 380 virtual BOOL doDialogChar(HWND hDlg, UCCHAR characterCode, 381 LPARAM keyData); 382 virtual BOOL doDialogHelp(HWND hDlg, LPHELPINFO helpInfo); 383 virtual bool copyToClipboard(const char *value); 384 virtual void processModalMessage(MSG msg); 385 #ifndef TC_NO_UNICODE 386 virtual bool copyToClipboard(const wchar_t *value); 387 #endif // TC_NO_UNICODE 388 389 virtual void positionResizeGrip(HWND hSizeGrip, int parentWidth, 390 int parentHeight); 391 virtual void positionResizeGrip(HWND hSizeGrip); 392 393 virtual void setAutosaveName(const char *value); 394 virtual bool readAutosaveInfo(int &saveX, int &saveY, int &saveWidth, 395 int &saveHeight, int &saveMaximized); 396 virtual void writeAutosaveInfo(int saveX, int saveY, int saveWidth, 397 int saveHeight, int saveMaximized); 398 virtual void initScaler(void); 399 400 static void printMessageName(UINT message); 401 static std::string getMessageName(UINT message); 402 static BOOL CALLBACK disableNonModalWindow(HWND hWnd, 403 LPARAM hModalDialog); 404 static BOOL CALLBACK enableNonModalWindow(HWND hWnd, 405 LPARAM hModalDialog); 406 static void calcSystemSizes(void); 407 static void populateAppVersion(void); 408 409 UCSTR windowTitle; 410 int x; 411 int y; 412 int width; 413 int height; 414 int minWidth; 415 int minHeight; 416 int maxWidth; 417 int maxHeight; 418 HINSTANCE hInstance; 419 HWND hWindow; 420 HMENU hWindowMenu; 421 HDC hdc; 422 CUIWindow* parentWindow; 423 HWND hParentWindow; 424 DWORD exWindowStyle; 425 DWORD windowStyle; 426 UINT windowClassStyle; 427 BOOL initialized; 428 CUIWindow* children[CUI_MAX_CHILDREN]; 429 int numChildren; 430 BOOL created; 431 DWORD fillColor; 432 DWORD* systemColors; 433 HPEN* systemColorPens; 434 HBRUSH hBackgroundBrush; 435 PAINTSTRUCT* paintStruct; 436 char *autosaveName; 437 CUIScaler* scaler; 438 439 static int systemMaxWidth; 440 static int systemMaxHeight; 441 static int systemMinTrackWidth; 442 static int systemMinTrackHeight; 443 static int systemMaxTrackWidth; 444 static int systemMaxTrackHeight; 445 static HCURSOR hArrowCursor; 446 static HCURSOR hWaitCursor; 447 static bool appVersionPopulated; 448 static DWORD appVersionMS; 449 static DWORD appVersionLS; 450 private: 451 static void populateLanguageModule(HINSTANCE hDefaultModule); 452 static bool loadLanguageModule(LCID lcid, bool includeSub = true); 453 454 static HINSTANCE hLanguageModule; // Always use getLanguageModule() 455 static UIntUIntMap ucMessages; 456 }; 457 458 #endif 459