Browse Source

fixed: when 'C' button was pressed, the cursor was not put in the edit window

added: menu to the pad window
       options: open/save as,close,undo,cut,copy,paste,delete,select all
 --This line, and those below, will be ignored--

M    src/winmain.cpp
M    src/pad.cpp
M    src/programresources.h
M    src/resource.rc
M    src/tabs.cpp
M    src/resource.h
M    CHANGELOG


git-svn-id: svn://ttmath.org/publicrep/ttcalc/trunk@221 e52654a7-88a9-db11-a3e9-0013d4bc506e
master
Tomasz Sowa 13 years ago
parent
commit
4346383c57
  1. 1
      CHANGELOG
  2. 344
      src/pad.cpp
  3. 1
      src/programresources.h
  4. 15
      src/resource.h
  5. 25
      src/resource.rc
  6. 3
      src/tabs.cpp
  7. 5
      src/winmain.cpp

1
CHANGELOG

@ -1,4 +1,5 @@
Version 0.9.0 prerelease (2009.10.18):
* fixed: when 'C' button was pressed, the cursor was not put in the edit window
* added: Pad window - a multiline edit window
* added: Checking for update dialog box
* changed: precision:

344
src/pad.cpp

@ -37,6 +37,8 @@
#include <windows.h>
#include <ttmath/ttmath.h>
#include <shlobj.h>
#include <fstream>
#include "programresources.h"
#include "resource.h"
#include "messages.h"
@ -66,6 +68,8 @@ ttmath::ErrorCode code;
bool calculated;
std::string res, res2;
std::string file_name;
#ifndef TTCALC_PORTABLE
ttmath::Parser<TTMathBig1> parser1;
@ -405,6 +409,342 @@ LRESULT PadFocus(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
return 0;
}
bool OpenDialog(HWND hwnd)
{
OPENFILENAME o;
char buf[MAX_PATH];
buf[0] = 0;
o.lStructSize = sizeof(o);
o.hwndOwner = hwnd;
o.hInstance = GetPrgRes()->GetInstance();
o.lpstrFilter = "*.*\0*.*\0";
o.lpstrCustomFilter = 0;
o.nMaxCustFilter = 0;
o.nFilterIndex = 1;
o.lpstrFile = buf;
o.nMaxFile = MAX_PATH;
o.lpstrFileTitle = 0;
o.nMaxFileTitle = 0;
o.lpstrInitialDir = 0;
o.lpstrTitle = 0;
o.Flags = OFN_PATHMUSTEXIST;
o.nFileOffset = 0;
o.nFileExtension = 0;
o.lpstrDefExt = 0;
o.lCustData = 0;
o.lpfnHook = 0;
o.lpTemplateName = 0;
if( GetOpenFileName(&o) )
{
file_name = buf;
return true;
}
return false;
}
// testing files which have only one 10 at the end of a line (unix mode)
void CorrectUnixMode()
{
bool was_r = false;
for(size_t i=0 ; i<res.size() ; ++i)
{
if( res[i]==10 && !was_r )
{
res.insert(res.begin()+i, 13);
++i;
}
else
{
was_r = res[i] == 13;
}
}
}
void LoadFromFile(HWND hwnd, std::ifstream & file)
{
char buf[64];
std::streamsize size;
res.clear();
do
{
file.read(buf, sizeof(buf)-1);
size = file.gcount();
buf[size] = 0;
res += buf;
}
while( !file.eof() );
CorrectUnixMode();
SetWindowText(edit, res.c_str());
}
LRESULT PadOpen(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if( !OpenDialog(hwnd) )
return 0;
std::ifstream file(file_name.c_str(), std::ios_base::in | std::ios_base::binary);
if( !file )
{
MessageBox(hwnd, "I cannot open the file", "TTcalc", MB_ICONERROR);
return 0;
}
file.seekg(0, std::ios::end);
std::ifstream::pos_type pos = file.tellg();
if( pos > 65530 )
{
MessageBox(hwnd, "The file is too long", "TTcalc", MB_ICONERROR);
return 0;
}
file.seekg(0);
LoadFromFile(hwnd, file);
file.close();
return 0;
}
bool SaveDialog(HWND hwnd)
{
OPENFILENAME o;
char buf[MAX_PATH];
sprintf(buf, "ttcalc.txt");
o.lStructSize = sizeof(o);
o.hwndOwner = hwnd;
o.hInstance = GetPrgRes()->GetInstance();
o.lpstrFilter = "*.*\0*.*\0";
o.lpstrCustomFilter = 0;
o.nMaxCustFilter = 0;
o.nFilterIndex = 1;
o.lpstrFile = buf;
o.nMaxFile = MAX_PATH;
o.lpstrFileTitle = 0;
o.nMaxFileTitle = 0;
o.lpstrInitialDir = 0;
o.lpstrTitle = 0;
o.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
o.nFileOffset = 0;
o.nFileExtension = 0;
o.lpstrDefExt = 0;
o.lCustData = 0;
o.lpfnHook = 0;
o.lpTemplateName = 0;
if( GetSaveFileName(&o) )
{
file_name = buf;
return true;
}
return false;
}
void SaveBuffer(HWND hwnd, const char * buf)
{
std::ofstream file(file_name.c_str(), std::ios_base::out | std::ios_base::binary);
if( !file )
{
MessageBox(hwnd, "I cannot open the file for saving", "TTCalc", MB_ICONERROR);
return;
}
for( ; *buf ; ++buf )
file.put(*buf);
file.close();
}
LRESULT PadSave(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if( !SaveDialog(hwnd) )
return 0;
HLOCAL handle = (HLOCAL)SendMessage(edit, EM_GETHANDLE, 0, 0);
if( handle == 0 )
{
MessageBox(hwnd, "There was a problem with saving", "TTCalc", MB_ICONERROR);
return 0;
}
const char * buf = (const char*)LocalLock(handle);
if( buf )
SaveBuffer(hwnd, buf);
else
MessageBox(hwnd, "There was a problem with saving", "TTCalc", MB_ICONERROR);
LocalUnlock(handle);
return 0;
}
LRESULT PadNew(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
SetWindowText(edit, "");
return 0;
}
LRESULT PadUndo(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
SendMessage(edit, WM_UNDO, 0, 0);
return 0;
}
LRESULT PadCut(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
SendMessage(edit, WM_CUT, 0, 0);
return 0;
}
LRESULT PadCopy(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
SendMessage(edit, WM_COPY, 0, 0);
return 0;
}
LRESULT PadPaste(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
SendMessage(edit, WM_PASTE, 0, 0);
return 0;
}
LRESULT PadDel(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
SendMessage(edit, EM_REPLACESEL, true, (LPARAM)"");
return 0;
}
LRESULT PadSelectAll(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
SendMessage(edit, EM_SETSEL, 0, -1);
return 0;
}
void PadInitMenuEdit(HMENU menu)
{
DWORD sel_start, sel_end;
bool can_undo = SendMessage(edit, EM_CANUNDO, 0 ,0);
if( can_undo )
EnableMenuItem(menu,MENUPAD_EDIT_UNDO,MF_BYCOMMAND | MF_ENABLED);
else
EnableMenuItem(menu,MENUPAD_EDIT_UNDO,MF_BYCOMMAND | MF_GRAYED);
SendMessage(edit, EM_GETSEL, (WPARAM)&sel_start, (LPARAM)&sel_end);
if( sel_start == sel_end )
{
EnableMenuItem(menu,MENUPAD_EDIT_CUT, MF_BYCOMMAND | MF_GRAYED);
EnableMenuItem(menu,MENUPAD_EDIT_COPY,MF_BYCOMMAND | MF_GRAYED);
EnableMenuItem(menu,MENUPAD_EDIT_DEL, MF_BYCOMMAND | MF_GRAYED);
}
else
{
EnableMenuItem(menu,MENUPAD_EDIT_CUT, MF_BYCOMMAND | MF_ENABLED);
EnableMenuItem(menu,MENUPAD_EDIT_COPY,MF_BYCOMMAND | MF_ENABLED);
EnableMenuItem(menu,MENUPAD_EDIT_DEL, MF_BYCOMMAND | MF_ENABLED);
}
if( IsClipboardFormatAvailable(CF_TEXT) )
EnableMenuItem(menu,MENUPAD_EDIT_PASTE,MF_BYCOMMAND | MF_ENABLED);
else
EnableMenuItem(menu,MENUPAD_EDIT_PASTE,MF_BYCOMMAND | MF_GRAYED);
}
LRESULT PadInitMenuPopUp(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HMENU menu = (HMENU) wParam;
int index = LOWORD(lParam);
if( index == MENUPAD_EDIT_INDEX )
PadInitMenuEdit(menu);
return 0;
}
void CreatePadCommandTable(Messages<LRESULT> & messages)
{
messages.Associate(MENUPAD_FILE_NEW, PadNew);
messages.Associate(MENUPAD_FILE_OPEN, PadOpen);
messages.Associate(MENUPAD_FILE_SAVE, PadSave);
messages.Associate(MENUPAD_FILE_CLOSE, PadClose);
messages.Associate(MENUPAD_EDIT_UNDO, PadUndo);
messages.Associate(MENUPAD_EDIT_CUT, PadCut);
messages.Associate(MENUPAD_EDIT_COPY, PadCopy);
messages.Associate(MENUPAD_EDIT_PASTE, PadPaste);
messages.Associate(MENUPAD_EDIT_DEL, PadDel);
messages.Associate(MENUPAD_EDIT_SELECTALL, PadSelectAll);
}
LRESULT PadCommand(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static Messages<LRESULT> messages;
bool method_exists;
LRESULT res;
if( messages.Empty() )
// initiation
CreatePadCommandTable(messages);
res = messages.Call(LOWORD(wParam), hwnd, msg, wParam, lParam, &method_exists);
if( !method_exists )
return DefWindowProc(hwnd, msg, wParam, lParam);
return res;
}
void CreatePadMessagesTable(Messages<LRESULT> & messages)
{
@ -413,6 +753,8 @@ void CreatePadMessagesTable(Messages<LRESULT> & messages)
messages.Associate(WM_SIZE, PadSize);
messages.Associate(WM_CLOSE, PadClose);
messages.Associate(WM_SETFOCUS, PadFocus);
messages.Associate(WM_COMMAND, PadCommand);
messages.Associate(WM_INITMENUPOPUP, PadInitMenuPopUp);
}
@ -448,7 +790,7 @@ WNDCLASS w;
w.hIcon = LoadIcon(GetPrgRes()->GetInstance(), MAKEINTRESOURCE(IDI_ICON2));
w.hCursor = 0;
w.hbrBackground = (HBRUSH)(COLOR_BACKGROUND+1);
w.lpszMenuName = 0;
w.lpszMenuName = MAKEINTRESOURCE(IDR_MENU2);
w.lpszClassName = ttcalc_pad_class_name;
return RegisterClass(&w);

1
src/programresources.h

@ -375,6 +375,7 @@ public:
bool IsPadVisible();
private:
bool IsWhiteCharacter(int c);

15
src/resource.h

@ -62,6 +62,7 @@
#define IDD_DIALOG_CONVERT 115
#define IDR_MENU 200
#define IDR_MENU2 201
#define IDC_TAB 1010
#define IDI_ICON2 104
#define IDI_ICON1 105
@ -210,6 +211,20 @@
#define IDM_HELP_CHECK_UPDATE 40042
#define IDM_HELP_ABOUT 40049
// pad menu
#define MENUPAD_EDIT_INDEX 1
#define MENUPAD_FILE_NEW 40500
#define MENUPAD_FILE_OPEN 40501
#define MENUPAD_FILE_SAVE 40502
#define MENUPAD_FILE_CLOSE 40503
#define MENUPAD_EDIT_UNDO 40510
#define MENUPAD_EDIT_CUT 40511
#define MENUPAD_EDIT_COPY 40512
#define MENUPAD_EDIT_PASTE 40513
#define MENUPAD_EDIT_DEL 40514
#define MENUPAD_EDIT_SELECTALL 40515
// about dialog
#define IDC_ABOUT_TEXT 1200
#define IDB_BITMAP_ABOUT 1201

25
src/resource.rc

@ -55,6 +55,31 @@ BEGIN
END
END
IDR_MENU2 MENU DISCARDABLE
BEGIN
POPUP "File"
BEGIN
// MENUITEM "New", MENUPAD_FILE_NEW
// temporarily this option is not available
MENUITEM "Open...", MENUPAD_FILE_OPEN
MENUITEM "Save as...", MENUPAD_FILE_SAVE
MENUITEM SEPARATOR
MENUITEM "Close", MENUPAD_FILE_CLOSE
END
POPUP "Edit"
BEGIN
MENUITEM "Undo", MENUPAD_EDIT_UNDO
MENUITEM SEPARATOR
MENUITEM "Cut", MENUPAD_EDIT_CUT
MENUITEM "Copy", MENUPAD_EDIT_COPY
MENUITEM "Paste", MENUPAD_EDIT_PASTE
MENUITEM "Delete", MENUPAD_EDIT_DEL
MENUITEM SEPARATOR
MENUITEM "Select all", MENUPAD_EDIT_SELECTALL
END
END
101 DIALOG 0, 0, 265, 150
STYLE DS_SETFONT |DS_CENTER |WS_POPUP |WS_SYSMENU |WS_THICKFRAME |WS_MAXIMIZEBOX |WS_MINIMIZEBOX |WS_CAPTION
MENU 200

3
src/tabs.cpp

@ -227,6 +227,9 @@ return true;
BOOL WmTabCommand_Press_clear(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
SetDlgItemText(GetPrgRes()->GetMainWindow(), IDC_INPUT_EDIT, "");
HWND input_edit = GetDlgItem( GetPrgRes()->GetMainWindow(), IDC_INPUT_EDIT );
SetFocus(input_edit);
return true;
}

5
src/winmain.cpp

@ -97,7 +97,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
if( !CreatePadWindow() )
return ShowError( Languages::cant_create_pad );
// there's the main loop of messages here
// the main loop of messages
MainMessagesLoop();
// there can be another variables/functions saved by other instantion of TTCalc
@ -201,6 +201,9 @@ MSG msg;
/*!
if there are some messages then they will be sent
*/
void CheckMessages()
{
MSG msg;

Loading…
Cancel
Save