/* * This file is a part of TTCalc - a mathematical calculator * and is distributed under the (new) BSD licence. * Author: Tomasz Sowa */ /* * Copyright (c) 2006-2007, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name Tomasz Sowa nor the names of contributors to this * project may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #include "compileconfig.h" #include "winmain.h" namespace MainWindowFunctions { /*! Loop through all the controls and remove the dark border that the previous default push button has. */ void ClearPushButtons(HWND hWnd) { HWND controls = GetWindow(hWnd, GW_CHILD); LRESULT dlg_code; while(controls) { dlg_code = SendMessage(controls, WM_GETDLGCODE, 0, 0); if(dlg_code & DLGC_DEFPUSHBUTTON) SendMessage(controls, BM_SETSTYLE, (WPARAM)BS_PUSHBUTTON, true); controls = GetWindow(controls, GW_HWNDNEXT); } } /*! this method checks whether 'button' is a push button and if so sets it as the default push button (with black border) */ void CheckAndSetDefPushButton(HWND button) { LRESULT dlg_code = SendMessage(button, WM_GETDLGCODE, 0, 0); if(dlg_code & DLGC_UNDEFPUSHBUTTON) SendMessage(button, BM_SETSTYLE, (WPARAM)BS_DEFPUSHBUTTON, true); } HWND GetNextControlInsideTab(HWND input_edit, HWND tab, HWND tab_dialog, HWND focus, HWND first_control, WPARAM wParam, bool direction_next) { LRESULT dlg_code = SendMessage(focus, WM_GETDLGCODE, 0, 0); HWND next = 0; if( focus == first_control ) { if( wParam==VK_TAB || ((dlg_code & DLGC_WANTARROWS)==0 && (dlg_code & DLGC_RADIOBUTTON)==0) ) { if( !direction_next ) next = tab; else { // if we have only radio buttons (precision tab) // the next item from GetNextDlgTabItem (or if we allow // the system to look for it) will be the same as the focus // we must set it on the input_edit HWND nexttmp = GetNextDlgTabItem(tab_dialog, focus, false); if( nexttmp == focus ) next = input_edit; } } } else // the focus is somewhere inside a tab { if( wParam==VK_TAB || ((dlg_code & DLGC_WANTARROWS)==0 && (dlg_code & DLGC_RADIOBUTTON)==0) ) { if( direction_next ) { HWND nexttmp = GetNextDlgTabItem(tab_dialog, focus, false); if( nexttmp == first_control ) { next = input_edit; } } } } return next; } /*! we're trying to get the next or previous control which should have the focus now */ HWND GetNextControl(HWND focus, HWND main_window, HWND tab, WPARAM wParam) { HWND next = 0; HWND output_edit = GetDlgItem(main_window, IDC_OUTPUT_EDIT); HWND input_edit = GetDlgItem(main_window, IDC_INPUT_EDIT); bool shift = (GetKeyState(VK_SHIFT) & 0xffff0000) != 0; bool direction_next = true; int tab_sel = TabCtrl_GetCurSel(tab); HWND tab_dialog = GetPrgRes()->GetTabWindow(tab_sel); HWND first_control = GetNextDlgTabItem(tab_dialog, 0, false); // if an user has pressed 'up' or 'left' or 'tab'+'shift' // that means we're going 'back' if( wParam==VK_UP || wParam==VK_LEFT || (wParam==VK_TAB && shift) ) direction_next = false; if( focus == input_edit ) { if( wParam==VK_TAB || wParam==VK_UP || wParam==VK_DOWN ) { if( direction_next ) { next = output_edit; } else { next = GetNextDlgTabItem(tab_dialog, first_control, true); ClearPushButtons(tab_dialog); } } } else if( focus == output_edit ) { if( wParam==VK_LEFT || wParam==VK_RIGHT ) return 0; if( direction_next ) // if we are on the output edit and the direction is 'forward' // then the next control will be the 'tab' next = tab; else next = input_edit; } else if( focus == tab ) { // if the tab control has the focus and an user has pressed 'left' or 'right' // we allow the system to manipulate the focus if( wParam==VK_LEFT || wParam==VK_RIGHT ) return 0; if( direction_next ) { // we're getting the first control on a specific dialog on the selected tab next = first_control; ClearPushButtons(tab_dialog); } else { // if the direction is 'back' we're setting the focus onto the output edit next = output_edit; } } else next = GetNextControlInsideTab(input_edit, tab, tab_dialog, focus, first_control, wParam, direction_next); return next; } /*! this function sets the focus on a specific control (the standard procedure from the operating system doesn't work well because the tab control would be omitted) remember to set WS_EX_CONTROLPARENT to the tab control and to all dialog boxes on the tab */ bool SetNextFocus(WPARAM wParam) { HWND next = 0; HWND main_window = GetPrgRes()->GetMainWindow(); HWND focus = GetFocus(); HWND tab = GetDlgItem(main_window, IDC_TAB); next = GetNextControl(focus, main_window, tab, wParam); if( next ) { SetFocus(next); CheckAndSetDefPushButton(next); return true; } return false; } void SetActiveTab(unsigned int i) { static int last_shown_dialog = -1; if( last_shown_dialog != -1 ) ShowWindow( GetPrgRes()->GetTabWindow(last_shown_dialog), SW_HIDE); if( i >= GetPrgRes()->HowManyTabWindows() ) { last_shown_dialog = -1; return; } ShowWindow(GetPrgRes()->GetTabWindow(i), SW_SHOWNORMAL); last_shown_dialog = i; } inline void SetMenuLanguageItem(HMENU menu, DWORD menu_id, Languages::GuiMsg msg) { Languages * pLang = GetPrgRes()->GetLanguages(); ModifyMenu(menu, menu_id, MF_BYCOMMAND|MF_STRING, menu_id, pLang->GuiMessage(msg) ); } void SetMenuLanguage(HWND hWnd) { HMENU menu = GetMenu(hWnd); if( !menu ) return; MENUITEMINFO mi; mi.cbSize = sizeof(mi); mi.fMask = MIIM_TYPE; mi.fType = MFT_STRING; // on popup menus we're using indexes because they don't want to work correctly // with the identifiers mi.dwTypeData = const_cast( GetPrgRes()->GetLanguages()->GuiMessage(Languages::menu_view) ); SetMenuItemInfo(menu, IDM_VIEW_INDEX, true, &mi); mi.dwTypeData = const_cast( GetPrgRes()->GetLanguages()->GuiMessage(Languages::menu_edit) ); SetMenuItemInfo(menu, IDM_EDIT_INDEX, true, &mi); mi.dwTypeData = const_cast( GetPrgRes()->GetLanguages()->GuiMessage(Languages::menu_help) ); SetMenuItemInfo(menu, IDM_HELP_INDEX, true, &mi); mi.dwTypeData = const_cast( GetPrgRes()->GetLanguages()->GuiMessage(Languages::menu_language) ); SetMenuItemInfo(GetSubMenu(menu,IDM_VIEW_INDEX), IDM_LANGUAGE_INDEX, true, &mi); SetMenuLanguageItem(menu, IDM_NEW_WINDOW, Languages::menu_view_new_window); SetMenuLanguageItem(menu, IDM_NORMAL_VIEW, Languages::menu_view_normal_view); SetMenuLanguageItem(menu, IDM_COMPACT_VIEW, Languages::menu_view_compact_view); SetMenuLanguageItem(menu, IDM_ALWAYS_ON_TOP, Languages::menu_view_always_on_top); SetMenuLanguageItem(menu, IDM_LANGUAGE_ENGLISH, Languages::menu_view_lang_english); SetMenuLanguageItem(menu, IDM_LANGUAGE_POLISH, Languages::menu_view_lang_polish); SetMenuLanguageItem(menu, IDM_CLOSE_PROGRAM, Languages::menu_view_close_program); SetMenuLanguageItem(menu, IDM_EDIT_UNDO, Languages::menu_edit_undo); SetMenuLanguageItem(menu, IDM_EDIT_PASTE, Languages::menu_edit_paste); SetMenuLanguageItem(menu, IDM_EDIT_COPY_RESULT, Languages::menu_edit_copy_result); SetMenuLanguageItem(menu, IDM_HELP_HELP, Languages::menu_help_help); SetMenuLanguageItem(menu, IDM_HELP_PROJECT_PAGE, Languages::menu_help_project_page); SetMenuLanguageItem(menu, IDM_HELP_ABOUT, Languages::menu_help_about); DrawMenuBar(hWnd); } void WmInitDialogCalculateMainWindowSize(HWND hTab) { RECT r; GetWindowRect(hTab, &r); int cy_caption = GetSystemMetrics(SM_CYCAPTION); int cy_add = 2 * GetSystemMetrics(SM_CYDLGFRAME) + GetSystemMetrics(SM_CYMENU); int cx_add = 2 * GetSystemMetrics(SM_CXDLGFRAME) + r.right - r.left; GetPrgRes()->SetYSizeNormal ( 225 + cy_add + cy_caption * 2); GetPrgRes()->SetYSizeCompact( 59 + cy_add + cy_caption ); GetPrgRes()->SetXSizeMin ( 4 + cx_add ); } HWND WmInitDialogCreateControlsOnTab(HWND hTab, LPCSTR dialog, DLGPROC dialog_proc) { HRSRC tab1res = FindResource(GetPrgRes()->GetInstance(), dialog, RT_DIALOG); HGLOBAL tab1global = LoadResource(GetPrgRes()->GetInstance(), tab1res); DLGTEMPLATE * tab1template = (DLGTEMPLATE*)LockResource(tab1global); if( tab1template ) { HWND hDialog = CreateDialogIndirect(GetPrgRes()->GetInstance(), tab1template, hTab , dialog_proc); // for windres SetWindowLong(hDialog, GWL_STYLE, GetWindowLong(hDialog, GWL_STYLE) & ~(WS_CAPTION | WS_OVERLAPPED) ); // int frame_cx = GetSystemMetrics(SM_CXEDGE); int frame_cy = GetSystemMetrics(SM_CYEDGE); int caption_cy = GetSystemMetrics(SM_CYCAPTION)+10; // the caption plus some white space RECT r; GetWindowRect(hTab, &r); SetWindowPos(hDialog, HWND_TOP, frame_cx, frame_cy + caption_cy, r.right-r.left- 2*frame_cx, r.bottom-r.top- 2*frame_cy - caption_cy, 0); return hDialog; } return 0; } void WmInitDialogCreateTab(HWND hTab, int tab_index, int tab_resource, DLGPROC tab_proc) { HWND dialogh = WmInitDialogCreateControlsOnTab( hTab, MAKEINTRESOURCE(tab_resource), tab_proc ); GetPrgRes()->SetTabWindow( tab_index, dialogh ); } void WmInitDialogCreateAndInitTabs(HWND hTab) { using namespace TabWindowFunctions; TCITEM tab_item; // the name of a tab we'll set when we call the 'SetLanguage()' function tab_item.mask = 0; tab_standard = 0; tab_variables = 1; tab_functions = 2; tab_precision = 3; tab_display = 4; // this insertion must be in the ascending order // (the second parameter of 'TabCtrl_InsertItem') TabCtrl_InsertItem(hTab, tab_standard, &tab_item); TabCtrl_InsertItem(hTab, tab_variables, &tab_item); TabCtrl_InsertItem(hTab, tab_functions, &tab_item); TabCtrl_InsertItem(hTab, tab_precision, &tab_item); TabCtrl_InsertItem(hTab, tab_display, &tab_item); WmInitDialogCreateTab(hTab, tab_standard, IDD_DIALOG_STANDARD, TabWindowProc); WmInitDialogCreateTab(hTab, tab_variables, IDD_DIALOG_VARIABLES, TabWindowProc); WmInitDialogCreateTab(hTab, tab_functions, IDD_DIALOG_FUNCTIONS, TabWindowProc); WmInitDialogCreateTab(hTab, tab_precision, IDD_DIALOG_PRECISION, TabWindowProc); WmInitDialogCreateTab(hTab, tab_display, IDD_DIALOG_DISPLAY, TabWindowProc); SetSizeOfDialogs(); SendMessage(GetPrgRes()->GetTabWindow(tab_variables), WM_INIT_TAB_VARIABLES, 0,0); SendMessage(GetPrgRes()->GetTabWindow(tab_functions), WM_INIT_TAB_FUNCTIONS, 0,0); SendMessage(GetPrgRes()->GetTabWindow(tab_precision), WM_INIT_TAB_PRECISION, 0,0); SendMessage(GetPrgRes()->GetTabWindow(tab_display), WM_INIT_TAB_DISPLAY, 0,0); TabWindowFunctions::SetLanguage(hTab); TabCtrl_SetCurSel(hTab, tab_standard); SetActiveTab( tab_standard ); } BOOL WmInitDialog(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { HWND hTab = GetDlgItem(hWnd,IDC_TAB); GetPrgRes()->SetMainWindow(hWnd); WmInitDialogCalculateMainWindowSize(hTab); WmInitDialogCreateAndInitTabs(hTab); HICON program_icon = LoadIcon(GetPrgRes()->GetInstance(),MAKEINTRESOURCE(IDI_ICON1)); if( program_icon ) SendMessage(hWnd,WM_SETICON,ICON_BIG,(LPARAM)program_icon); GetPrgRes()->SetAlwaysOnTop( GetPrgRes()->GetAlwaysOnTop() ); GetPrgRes()->SetMaximized ( GetPrgRes()->GetMaximized() ); GetPrgRes()->SetView ( GetPrgRes()->GetView() ); ShowWindow(hWnd,SW_SHOW); SendDlgItemMessage(hWnd,IDC_INPUT_EDIT,EM_SETLIMITTEXT,GetPrgRes()->GetBufferSize()-1,0); SetMenuLanguage(hWnd); return true; } /* a function for WM_MOVE */ BOOL WmMove(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { RECT r; GetWindowRect(hWnd,&r); GetPrgRes()->SetXPos( r.left ); GetPrgRes()->SetYPos( r.top ); return 0; } /* a function for WM_CLOSE */ BOOL WmClose(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { GetPrgRes()->GetThreadController()->StopCalculatingAndExitThread(); DestroyWindow(GetPrgRes()->GetMainWindow()); PostQuitMessage(0); return 0; } /* a function for WM_GETMINMAXINFO */ BOOL WmGetMinMaxInfo(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { MINMAXINFO * lpmmi = (LPMINMAXINFO) lParam; lpmmi->ptMinTrackSize.x = GetPrgRes()->GetXSizeMin(); if( GetPrgRes()->GetView() == ProgramResources::view_normal ) { lpmmi->ptMinTrackSize.y = GetPrgRes()->GetYSizeNormal(); //lpmmi->ptMaxTrackSize.y = cy_normalny; } else {// kompaktowy lpmmi->ptMinTrackSize.y = GetPrgRes()->GetYSizeCompact(); lpmmi->ptMaxTrackSize.y = GetPrgRes()->GetYSizeCompact(); } return 0; } /*! a function for WM_SIZING when user drags the border of the main window this method checks if the new boundary is near to the standard width and height, if it is the dragging box is attracted to the standard size */ BOOL WmSizing(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { RECT * pr = (RECT*) lParam; int cx_min = GetPrgRes()->GetXSizeMin(); const int margin = 20; if( GetPrgRes()->GetView() == ProgramResources::view_normal ) { int cy_min = GetPrgRes()->GetYSizeNormal(); if( (pr->bottom - pr->top) < cy_min + margin ) { if( (wParam == WMSZ_BOTTOM) || (wParam == WMSZ_BOTTOMRIGHT) || (wParam == WMSZ_BOTTOMLEFT) ) pr->bottom = pr->top + cy_min; else if( (wParam == WMSZ_TOP) || (wParam == WMSZ_TOPRIGHT) || (wParam == WMSZ_TOPLEFT) ) pr->top = pr->bottom - cy_min; } } if( (pr->right-pr->left) < cx_min + margin ) { if( (wParam == WMSZ_RIGHT) || (wParam == WMSZ_BOTTOMRIGHT) || (wParam == WMSZ_TOPRIGHT) ) pr->right = pr->left + cx_min; else if( (wParam == WMSZ_LEFT) || (wParam == WMSZ_BOTTOMLEFT) || (wParam == WMSZ_TOPLEFT) ) pr->left = pr->right - cx_min; } return 1; } /* a function for WM_SIZE */ BOOL WmSize(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int fwSizeType = int(wParam); RECT r; POINT p; if( fwSizeType == SIZE_MAXIMIZED ) GetPrgRes()->SetMaximized(true); else if( fwSizeType == SIZE_RESTORED ) GetPrgRes()->SetMaximized(false); int cx = LOWORD(lParam); int cy = HIWORD(lParam); GetWindowRect(GetDlgItem(hWnd,IDC_INPUT_EDIT),&r); p.x = r.left; p.y = r.top; ScreenToClient(hWnd,&p); MoveWindow(GetDlgItem(hWnd,IDC_INPUT_EDIT),p.x, p.y, cx, r.bottom-r.top, true); GetWindowRect(GetDlgItem(hWnd,IDC_OUTPUT_EDIT),&r); p.x = r.left; p.y = r.top; ScreenToClient(hWnd,&p); MoveWindow(GetDlgItem(hWnd,IDC_OUTPUT_EDIT),p.x, p.y, cx, r.bottom-r.top, true); GetWindowRect(GetDlgItem(hWnd,IDC_TAB),&r); p.x = r.left; p.y = r.top; ScreenToClient(hWnd,&p); MoveWindow(GetDlgItem(hWnd,IDC_TAB),p.x, p.y, cx, cy-p.y, true); TabWindowFunctions::SetSizeOfVariablesList(); TabWindowFunctions::SetSizeOfFunctionsList(); if( fwSizeType != SIZE_MINIMIZED ) { GetWindowRect(hWnd,&r); GetPrgRes()->SetXSize( r.right - r.left ); GetPrgRes()->SetYSize( r.bottom - r.top ); } return 0; } void WmInitMenuPopUpView(HMENU menu) { if( GetPrgRes()->GetAlwaysOnTop() ) CheckMenuItem(menu,IDM_ALWAYS_ON_TOP,MF_BYCOMMAND|MF_CHECKED); else CheckMenuItem(menu,IDM_ALWAYS_ON_TOP,MF_BYCOMMAND|MF_UNCHECKED); if( GetPrgRes()->GetView() == ProgramResources::view_normal ) { CheckMenuItem(menu,IDM_NORMAL_VIEW,MF_BYCOMMAND|MF_CHECKED); CheckMenuItem(menu,IDM_COMPACT_VIEW,MF_BYCOMMAND|MF_UNCHECKED); } else { CheckMenuItem(menu,IDM_COMPACT_VIEW,MF_BYCOMMAND|MF_CHECKED); CheckMenuItem(menu,IDM_NORMAL_VIEW,MF_BYCOMMAND|MF_UNCHECKED); } if( GetPrgRes()->GetLanguages()->GetCurrentLanguage() == Languages::en ) { CheckMenuItem(menu, IDM_LANGUAGE_ENGLISH, MF_BYCOMMAND|MF_CHECKED); CheckMenuItem(menu, IDM_LANGUAGE_POLISH, MF_BYCOMMAND|MF_UNCHECKED); } else { CheckMenuItem(menu, IDM_LANGUAGE_POLISH, MF_BYCOMMAND|MF_CHECKED); CheckMenuItem(menu, IDM_LANGUAGE_ENGLISH, MF_BYCOMMAND|MF_UNCHECKED); } } void WmInitMenuPopUpEdit(HWND hWnd, HMENU menu) { char buffer[5]; bool can_undo = SendDlgItemMessage(hWnd, IDC_INPUT_EDIT, EM_CANUNDO, 0 ,0); if( can_undo ) EnableMenuItem(menu,IDM_EDIT_UNDO,MF_BYCOMMAND | MF_ENABLED); else EnableMenuItem(menu,IDM_EDIT_UNDO,MF_BYCOMMAND | MF_GRAYED); if( IsClipboardFormatAvailable(CF_TEXT) ) EnableMenuItem(menu,IDM_EDIT_PASTE,MF_BYCOMMAND | MF_ENABLED); else EnableMenuItem(menu,IDM_EDIT_PASTE,MF_BYCOMMAND | MF_GRAYED); GetDlgItemText(hWnd,IDC_OUTPUT_EDIT,buffer,sizeof(buffer)); if( buffer[0] == 0 ) EnableMenuItem(menu,IDM_EDIT_COPY_RESULT,MF_BYCOMMAND | MF_GRAYED); else EnableMenuItem(menu,IDM_EDIT_COPY_RESULT,MF_BYCOMMAND | MF_ENABLED); } BOOL WmInitMenuPopUp(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { if( LOWORD(lParam) == IDM_VIEW_INDEX ) { WmInitMenuPopUpView( (HMENU)wParam ); return true; } else if( LOWORD(lParam) == IDM_EDIT_INDEX ) { WmInitMenuPopUpEdit( hWnd, (HMENU)wParam ); return true; } return false; } /* a function for WM_NOTIFY */ BOOL WmNotify(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { NMHDR * pnmh = (LPNMHDR) lParam; HWND hTab = GetDlgItem(hWnd,IDC_TAB); if( pnmh->hwndFrom==hTab && pnmh->code==TCN_SELCHANGE ) SetActiveTab( TabCtrl_GetCurSel(hTab) ); return true; } /* a group of functions using when the WM_COMMAND message is sent */ BOOL WmCommand_AlwaysOnTop(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { GetPrgRes()->SetAlwaysOnTop( !GetPrgRes()->GetAlwaysOnTop() ); return true; } BOOL WmCommand_NormalView(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { GetPrgRes()->SetView(ProgramResources::view_normal); return true; } BOOL WmCommand_CompactView(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { GetPrgRes()->SetView(ProgramResources::view_compact); return true; } BOOL WmCommand_HelpAbout(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { DialogBox(GetPrgRes()->GetInstance(),MAKEINTRESOURCE(IDD_ABOUT_DIALOG),hWnd,AboutProc); return true; } BOOL WmCommand_NewWindow(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { STARTUPINFO si; PROCESS_INFORMATION pi; GetPrgRes()->SetXPos( GetPrgRes()->GetXPos()+10 ); GetPrgRes()->SetYPos( GetPrgRes()->GetYPos()+10 ); GetPrgRes()->SaveToFile(); GetPrgRes()->SetXPos( GetPrgRes()->GetXPos()-10 ); GetPrgRes()->SetYPos( GetPrgRes()->GetYPos()-10 ); char * buffer = new char[MAX_PATH+1]; GetModuleFileName(GetPrgRes()->GetInstance(),buffer, MAX_PATH); memset(&si,0,sizeof(si)); si.cb = sizeof(si); CreateProcess(buffer,"",0,0,false,NORMAL_PRIORITY_CLASS,0,0,&si,&pi); delete [] buffer; return true; } BOOL WmCommand_CloseProgram(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { GetPrgRes()->GetThreadController()->StopCalculatingAndExitThread(); DestroyWindow(GetPrgRes()->GetMainWindow()); PostQuitMessage(0); return true; } BOOL WmCommand_InputEditNotify(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { if( HIWORD(wParam) != EN_CHANGE ) return false; GetPrgRes()->GetThreadController()->StopCalculating(); GetDlgItemText(hWnd,IDC_INPUT_EDIT, (char*)GetPrgRes()->GetBuffer(), GetPrgRes()->GetBufferSize()); GetPrgRes()->GetThreadController()->StartCalculating(); return true; } BOOL WmCommand_EditUndo(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { SendDlgItemMessage(hWnd, IDC_INPUT_EDIT, EM_UNDO, 0, 0); return true; } BOOL WmCommand_EditPaste(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { SendDlgItemMessage(hWnd, IDC_INPUT_EDIT, WM_PASTE, 0, 0); return true; } BOOL WmCommand_EditCopyResult(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { SendDlgItemMessage(hWnd, IDC_OUTPUT_EDIT, EM_SETSEL, 0, LPARAM(-1)); SendDlgItemMessage(hWnd, IDC_OUTPUT_EDIT, WM_COPY, 0, 0); SendDlgItemMessage(hWnd, IDC_OUTPUT_EDIT, EM_SETSEL, WPARAM(-1), 0); return true; } /*! an user has pressed 'ESC' key in the main window we're setting the entire input edit as being selected */ BOOL WmCommand_IDCANCEL(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { HWND iedit = GetDlgItem(hWnd, IDC_INPUT_EDIT); SendMessage(iedit, EM_SETSEL, 0, -1); SetFocus(iedit); return true; } BOOL WmSetLastError(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { using TabWindowFunctions::last_code; last_code = (ttmath::ErrorCode)wParam; HWND button_err = GetDlgItem( GetPrgRes()->GetTabWindow( TabWindowFunctions::tab_standard), IDC_BUTTON_ERROR ); if( last_code == ttmath::err_ok || last_code == ttmath::err_nothing_has_read ) EnableWindow(button_err, false); else EnableWindow(button_err, true); if( last_code == ttmath::err_unknown_variable || last_code == ttmath::err_division_by_zero || last_code == ttmath::err_overflow || last_code == ttmath::err_unknown_function || last_code == ttmath::err_improper_argument ) TabWindowFunctions::PrintErrorCode(); return true; } void SetOutputEditLanguage(HWND hWnd) { if( TabWindowFunctions::last_code != ttmath::err_ok ) { if( SendDlgItemMessage(hWnd, IDC_OUTPUT_EDIT, WM_GETTEXTLENGTH, 0, 0) > 0 ) { // there should be an error string on the output edit // and we change it into a new language TabWindowFunctions::PrintErrorCode(); } } } BOOL WmCommand_LanguageEnglish(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { GetPrgRes()->GetLanguages()->SetCurrentLanguage(Languages::en); SetMenuLanguage(hWnd); SetOutputEditLanguage(hWnd); TabWindowFunctions::SetLanguage( GetDlgItem(hWnd, IDC_TAB) ); return true; } BOOL WmCommand_LanguagePolish(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { GetPrgRes()->GetLanguages()->SetCurrentLanguage(Languages::pl); SetMenuLanguage(hWnd); SetOutputEditLanguage(hWnd); TabWindowFunctions::SetLanguage( GetDlgItem(hWnd, IDC_TAB) ); return true; } BOOL WmHelp(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { SHELLEXECUTEINFO exec; exec.cbSize = sizeof(exec); exec.fMask = SEE_MASK_FLAG_NO_UI | SEE_MASK_NOCLOSEPROCESS; exec.hwnd = 0; exec.lpVerb = "open"; exec.lpFile = "ttcalc.chm"; exec.lpParameters = 0; exec.lpDirectory = ""; exec.nShow = SW_SHOWNORMAL; if( !ShellExecuteEx(&exec) ) { MessageBox(hWnd, GetPrgRes()->GetLanguages()->GuiMessage(Languages::cant_find_help), GetPrgRes()->GetLanguages()->GuiMessage(Languages::message_box_caption), MB_ICONERROR); } return true; } BOOL WmProjectPage(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { SHELLEXECUTEINFO exec; exec.cbSize = sizeof(exec); exec.fMask = SEE_MASK_FLAG_NO_UI | SEE_MASK_NOCLOSEPROCESS; exec.hwnd = 0; exec.lpVerb = "open"; exec.lpFile = "http://sourceforge.net/projects/ttcalc"; exec.lpParameters = 0; exec.lpDirectory = ""; exec.nShow = SW_SHOWNORMAL; if( !ShellExecuteEx(&exec) ) { MessageBox(hWnd, GetPrgRes()->GetLanguages()->GuiMessage(Languages::cant_open_project_page), GetPrgRes()->GetLanguages()->GuiMessage(Languages::message_box_caption), MB_ICONERROR); } return true; } void CreateCommandMessagesTable(Messages & cmessages) { cmessages.Associate(IDM_ALWAYS_ON_TOP, WmCommand_AlwaysOnTop); cmessages.Associate(IDM_NORMAL_VIEW, WmCommand_NormalView); cmessages.Associate(IDM_COMPACT_VIEW, WmCommand_CompactView); cmessages.Associate(IDM_HELP_ABOUT, WmCommand_HelpAbout); cmessages.Associate(IDM_NEW_WINDOW, WmCommand_NewWindow); cmessages.Associate(IDM_CLOSE_PROGRAM, WmCommand_CloseProgram); cmessages.Associate(IDC_INPUT_EDIT, WmCommand_InputEditNotify); cmessages.Associate(IDM_EDIT_UNDO, WmCommand_EditUndo); cmessages.Associate(IDM_EDIT_PASTE, WmCommand_EditPaste); cmessages.Associate(IDM_EDIT_COPY_RESULT, WmCommand_EditCopyResult); cmessages.Associate(IDCANCEL, WmCommand_IDCANCEL); cmessages.Associate(IDM_LANGUAGE_ENGLISH, WmCommand_LanguageEnglish); cmessages.Associate(IDM_LANGUAGE_POLISH, WmCommand_LanguagePolish); cmessages.Associate(IDM_HELP_HELP, WmHelp); cmessages.Associate(IDM_HELP_PROJECT_PAGE, WmProjectPage); } BOOL WmCommand(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static Messages command_messages; if( command_messages.Empty() ) // initiation CreateCommandMessagesTable(command_messages); if( command_messages.IsMessage(LOWORD(wParam)) ) return command_messages.Call(LOWORD(wParam), hWnd, message, wParam, lParam); else return TabWindowFunctions::WmTabCommand(hWnd, message, wParam, lParam); } void CreateMainMessagesTable(Messages & messages) { messages.Associate(WM_INITDIALOG, WmInitDialog); messages.Associate(WM_MOVE, WmMove); messages.Associate(WM_CLOSE, WmClose); messages.Associate(WM_GETMINMAXINFO, WmGetMinMaxInfo); messages.Associate(WM_SIZE, WmSize); messages.Associate(WM_INITMENUPOPUP, WmInitMenuPopUp); messages.Associate(WM_COMMAND, WmCommand); messages.Associate(WM_NOTIFY, WmNotify); messages.Associate(WM_SIZING, WmSizing); messages.Associate(WM_SET_LAST_ERROR, WmSetLastError); messages.Associate(WM_HELP, WmHelp); } /*! this method prepares a text which is printed on the about dialog box */ void CreateAboutText(char * buffer) { char compiler[30]; #ifdef __GNUC__ #ifdef __VERSION__ sprintf(compiler,"GCC %s", __VERSION__); #else sprintf( compiler, "GCC %d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__ ); #endif #elif _MSC_VER char msvc_ver[20]; int i; sprintf(msvc_ver, "%d", _MSC_VER); /* from msdn: _MSC_VER reports the major and minor versions of the compiler. For example, 1310 for Microsoft Visual C++ .NET 2003. 1310 represents version 13 and a 1.0 point release. The Visual C++ 2005 compiler version is 1400. If there is, for example, version 1400 we rather want to show it as 14.00, so we put a simple dot on the third position */ for(i=0; msvc_ver[i] ; ++i); for( ; i>=2 ; --i) msvc_ver[i+1] = msvc_ver[i]; msvc_ver[2] = '.'; sprintf( compiler, "Microsoft Visual C++ %s", msvc_ver ); #else sprintf(compiler, "unknown"); #endif sprintf( buffer, GetPrgRes()->GetLanguages()->GuiMessage(Languages::about_text), TTCALC_MAJOR_VER, TTCALC_MINOR_VER, TTCALC_REVISION_VER, TTMATH_MAJOR_VER, TTMATH_MINOR_VER, TTMATH_REVISION_VER, compiler); } /*! it displays the about-window */ BOOL CALLBACK AboutProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { char * about_buffer; Languages * lang; switch(message) { case WM_INITDIALOG: lang = GetPrgRes()->GetLanguages(); SetWindowText(hWnd, lang->GuiMessage(Languages::about_box_title) ); SetWindowText(GetDlgItem(hWnd, IDOK), lang->GuiMessage(Languages::about_box_button_close) ); about_buffer = new char[2000]; CreateAboutText(about_buffer); SetDlgItemText(hWnd, IDC_ABOUT_TEXT, about_buffer); delete [] about_buffer; return true; case WM_COMMAND: if(LOWORD(wParam)==IDOK || LOWORD(wParam)==IDCANCEL) { EndDialog(hWnd,0); return true; } break; } return false; } } // namespace MainWindowFunctions