More wxWidgets in poem demo.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@32836 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Włodzimierz Skiba
2005-03-15 20:33:42 +00:00
parent fc3bb38313
commit c7bde65d2b
2 changed files with 469 additions and 467 deletions

View File

@@ -56,8 +56,6 @@
static wxChar *poem_buffer; // Storage for each poem static wxChar *poem_buffer; // Storage for each poem
static wxChar line[150]; // Storage for a line static wxChar line[150]; // Storage for a line
static wxChar title[150]; // Remember the title
static wxChar *search_string = NULL; // The search string
static int pages[30]; // For multipage poems - static int pages[30]; // For multipage poems -
// store the start of each page // store the start of each page
static long last_poem_start = 0; // Start of last found poem static long last_poem_start = 0; // Start of last found poem
@@ -89,11 +87,6 @@ wxIcon *Corner2 = NULL;
wxIcon *Corner3 = NULL; wxIcon *Corner3 = NULL;
wxIcon *Corner4 = NULL; wxIcon *Corner4 = NULL;
// Fonts
wxFont *NormalFont = NULL;
wxFont *BoldFont = NULL;
wxFont *ItalicFont = NULL;
// Pens // Pens
wxPen *GreyPen = NULL; wxPen *GreyPen = NULL;
wxPen *DarkGreyPen = NULL; wxPen *DarkGreyPen = NULL;
@@ -109,10 +102,7 @@ bool LoadPoem(wxChar *, long);
int GetIndex(); int GetIndex();
int LoadIndex(wxChar *); int LoadIndex(wxChar *);
bool Compile(void); bool Compile(void);
void WritePreferences();
void ReadPreferences();
void FindMax(int *max_thing, int thing); void FindMax(int *max_thing, int thing);
void CreateFonts();
#if wxUSE_CLIPBOARD #if wxUSE_CLIPBOARD
#include "wx/dataobj.h" #include "wx/dataobj.h"
@@ -128,11 +118,11 @@ IMPLEMENT_APP(MyApp)
MainWindow *TheMainWindow = NULL; MainWindow *TheMainWindow = NULL;
// Create the fonts // Create the fonts
void CreateFonts() void MainWindow::CreateFonts()
{ {
NormalFont = wxTheFontList->FindOrCreateFont(pointSize, wxSWISS, wxNORMAL, wxNORMAL); m_normalFont = wxTheFontList->FindOrCreateFont(pointSize, wxSWISS, wxNORMAL, wxNORMAL);
BoldFont = wxTheFontList->FindOrCreateFont(pointSize, wxSWISS, wxNORMAL, wxBOLD); m_boldFont = wxTheFontList->FindOrCreateFont(pointSize, wxSWISS, wxNORMAL, wxBOLD);
ItalicFont = wxTheFontList->FindOrCreateFont(pointSize, wxSWISS, wxITALIC, wxNORMAL); m_italicFont = wxTheFontList->FindOrCreateFont(pointSize, wxSWISS, wxITALIC, wxNORMAL);
} }
BEGIN_EVENT_TABLE(MainWindow, wxFrame) BEGIN_EVENT_TABLE(MainWindow, wxFrame)
@@ -145,6 +135,8 @@ MainWindow::MainWindow(wxFrame *frame, wxWindowID id, const wxString& title,
const wxPoint& pos, const wxSize& size, long style): const wxPoint& pos, const wxSize& size, long style):
wxFrame(frame, id, title, pos, size, style) wxFrame(frame, id, title, pos, size, style)
{ {
ReadPreferences();
CreateFonts();
} }
// Read the poetry buffer, either for finding the size // Read the poetry buffer, either for finding the size
@@ -181,31 +173,34 @@ void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
} }
// See what ACTUAL char height is // See what ACTUAL char height is
dc->SetFont(* NormalFont); if(m_normalFont)
dc->SetFont(*m_normalFont);
long xx; long xx;
long yy; long yy;
dc->GetTextExtent(_T("X"), &xx, &yy); dc->GetTextExtent(_T("X"), &xx, &yy);
char_height = (int)yy; char_height = (int)yy;
if (current_page == 0) if (current_page == 0)
title[0] = 0;
else if (title[0] != 0)
{ {
dc->SetFont(* BoldFont); m_title = wxEmptyString;
dc->GetTextExtent(title, &xx, &yy); }
else if (!m_title.empty())
{
dc->SetFont(* m_boldFont);
dc->GetTextExtent(m_title, &xx, &yy);
FindMax(&curr_width, (int)xx); FindMax(&curr_width, (int)xx);
if (DrawIt) if (DrawIt)
{ {
int x = (width - xx)/2; int x = (width - xx)/2;
dc->SetFont(* BoldFont); dc->SetFont(* m_boldFont);
// Change text to BLACK! // Change text to BLACK!
dc->SetTextForeground(* wxBLACK); dc->SetTextForeground(* wxBLACK);
dc->DrawText(title, x, y); dc->DrawText(m_title, x, y);
// Change text to WHITE! // Change text to WHITE!
dc->SetTextForeground(* wxWHITE); dc->SetTextForeground(* wxWHITE);
dc->DrawText(title, x-SHADOW_OFFSET, y-SHADOW_OFFSET); dc->DrawText(m_title, x-SHADOW_OFFSET, y-SHADOW_OFFSET);
} }
y += char_height; y += char_height;
y += char_height; y += char_height;
@@ -259,11 +254,11 @@ void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
break; break;
case 'T': case 'T':
dc->SetFont(* BoldFont); dc->SetFont(* m_boldFont);
line_ptr = line+3; line_ptr = line+3;
wxStrcpy(title, line_ptr); m_title = line_ptr;
wxStrcat(title, _T(" (cont'd)")); m_title << _T(" (cont'd)");
dc->GetTextExtent(line_ptr, &xx, &yy); dc->GetTextExtent(line_ptr, &xx, &yy);
FindMax(&curr_width, (int)xx); FindMax(&curr_width, (int)xx);
@@ -271,7 +266,7 @@ void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
if (DrawIt) if (DrawIt)
{ {
int x = (width - xx)/2; int x = (width - xx)/2;
dc->SetFont(* BoldFont); dc->SetFont(* m_boldFont);
// Change text to BLACK! // Change text to BLACK!
dc->SetTextForeground(* wxBLACK); dc->SetTextForeground(* wxBLACK);
@@ -286,7 +281,7 @@ void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
case 'A': case 'A':
line_ptr = line+3; line_ptr = line+3;
dc->SetFont(* ItalicFont); dc->SetFont(* m_italicFont);
dc->GetTextExtent(line_ptr, &xx, &yy); dc->GetTextExtent(line_ptr, &xx, &yy);
FindMax(&curr_width, (int)xx); FindMax(&curr_width, (int)xx);
@@ -306,7 +301,7 @@ void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
} }
else else
{ {
dc->SetFont(* NormalFont); dc->SetFont(* m_normalFont);
dc->GetTextExtent(line, &xx, &yy); dc->GetTextExtent(line, &xx, &yy);
FindMax(&curr_width, (int)xx); FindMax(&curr_width, (int)xx);
@@ -314,7 +309,7 @@ void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
if (DrawIt) if (DrawIt)
{ {
int x = (int)((width - xx)/2.0); int x = (int)((width - xx)/2.0);
dc->SetFont(* NormalFont); dc->SetFont(* m_normalFont);
dc->SetTextForeground(* wxBLACK); dc->SetTextForeground(* wxBLACK);
dc->DrawText(line, x, y); dc->DrawText(line, x, y);
} }
@@ -328,14 +323,14 @@ void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
{ {
wxChar *cont = _T("(cont'd)"); wxChar *cont = _T("(cont'd)");
dc->SetFont(* NormalFont); dc->SetFont(* m_normalFont);
dc->GetTextExtent(cont, &xx, &yy); dc->GetTextExtent(cont, &xx, &yy);
FindMax(&curr_width, (int)xx); FindMax(&curr_width, (int)xx);
if (DrawIt) if (DrawIt)
{ {
int x = (int)((width - xx)/2.0); int x = (int)((width - xx)/2.0);
dc->SetFont(* NormalFont); dc->SetFont(* m_normalFont);
dc->SetTextForeground(* wxBLACK); dc->SetTextForeground(* wxBLACK);
dc->DrawText(cont, x, y); dc->DrawText(cont, x, y);
} }
@@ -399,7 +394,6 @@ void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
dc->DrawLine(THICK_LINE_BORDER, THICK_LINE_BORDER, dc->DrawLine(THICK_LINE_BORDER, THICK_LINE_BORDER,
width-THICK_LINE_BORDER, THICK_LINE_BORDER); width-THICK_LINE_BORDER, THICK_LINE_BORDER);
//#ifdef __WXMSW__
// Draw icons // Draw icons
dc->DrawIcon(* Corner1, 0, 0); dc->DrawIcon(* Corner1, 0, 0);
dc->DrawIcon(* Corner2, int(width-32), 0); dc->DrawIcon(* Corner2, int(width-32), 0);
@@ -408,7 +402,6 @@ void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
int x2 = (width-32); int x2 = (width-32);
dc->DrawIcon(* Corner3, 0, y2); dc->DrawIcon(* Corner3, 0, y2);
dc->DrawIcon(* Corner4, x2, y2); dc->DrawIcon(* Corner4, x2, y2);
//#endif
} }
} }
@@ -444,7 +437,7 @@ void MainWindow::Resize(void)
memDC.SelectObject(* backingBitmap); memDC.SelectObject(* backingBitmap);
memDC.Clear(); memDC.Clear();
TheMainWindow->ScanBuffer(&memDC, true, &xx, &yy); ScanBuffer(&memDC, true, &xx, &yy);
} }
// Which is more? // Which is more?
@@ -482,16 +475,19 @@ void MainWindow::Search(bool ask)
{ {
long position; long position;
if (ask || !search_string) if (ask || m_searchString.empty())
{ {
wxString s = wxGetTextFromUser( _T("Enter search string"), _T("Search"), (const wxChar*) search_string); wxString s = wxGetTextFromUser( _T("Enter search string"), _T("Search"), m_searchString);
if (s != wxEmptyString) if (s != wxEmptyString)
{ {
s.MakeLower(); s.MakeLower();
if (search_string) delete[] search_string; m_searchString = s;
search_string = wxStrcpy(new wxChar[wxStrlen(s.c_str()) + 1], s.c_str());
search_ok = true; search_ok = true;
} else search_ok = false; }
else
{
search_ok = false;
}
} }
else else
{ {
@@ -499,7 +495,7 @@ void MainWindow::Search(bool ask)
search_ok = true; search_ok = true;
} }
if (search_string && search_ok) if (!m_searchString.empty() && search_ok)
{ {
position = DoSearch(); position = DoSearch();
if (position > -1) if (position > -1)
@@ -523,10 +519,6 @@ bool MyApp::OnInit()
DarkGreyPen = new wxPen(_T("GREY"), THICK_LINE_WIDTH, wxSOLID); DarkGreyPen = new wxPen(_T("GREY"), THICK_LINE_WIDTH, wxSOLID);
WhitePen = new wxPen(_T("WHITE"), THICK_LINE_WIDTH, wxSOLID); WhitePen = new wxPen(_T("WHITE"), THICK_LINE_WIDTH, wxSOLID);
CreateFonts();
ReadPreferences();
// Seed the random number generator // Seed the random number generator
#ifdef __WXWINCE__ #ifdef __WXWINCE__
srand((unsigned) CeGetRandomSeed()); srand((unsigned) CeGetRandomSeed());
@@ -550,7 +542,7 @@ bool MyApp::OnInit()
TheMainWindow->SetIcon(wxICON(wxpoem)); TheMainWindow->SetIcon(wxICON(wxpoem));
TheMainWindow->canvas = new MyCanvas(TheMainWindow, 501, wxDefaultPosition, wxDefaultSize); TheMainWindow->canvas = new MyCanvas(TheMainWindow);
if (argc > 1) if (argc > 1)
{ {
@@ -598,8 +590,6 @@ int MyApp::OnExit()
delete Corner4; delete Corner4;
delete[] poem_buffer; delete[] poem_buffer;
if (search_string)
delete[] search_string;
return 0; return 0;
} }
@@ -622,32 +612,32 @@ BEGIN_EVENT_TABLE(MyCanvas, wxWindow)
END_EVENT_TABLE() END_EVENT_TABLE()
// Define a constructor for my canvas // Define a constructor for my canvas
MyCanvas::MyCanvas(wxFrame *frame, wxWindowID id, const wxPoint& pos, const wxSize& size): MyCanvas::MyCanvas(wxFrame *frame):
wxWindow(frame, id, pos, size) wxWindow(frame, wxID_ANY)
{ {
popupMenu = new wxMenu; m_popupMenu = new wxMenu;
popupMenu->Append(POEM_NEXT, _T("Next poem/page")); m_popupMenu->Append(POEM_NEXT, _T("Next poem/page"));
popupMenu->Append(POEM_PREVIOUS, _T("Previous page")); m_popupMenu->Append(POEM_PREVIOUS, _T("Previous page"));
popupMenu->AppendSeparator(); m_popupMenu->AppendSeparator();
popupMenu->Append(POEM_SEARCH, _T("Search")); m_popupMenu->Append(POEM_SEARCH, _T("Search"));
popupMenu->Append(POEM_NEXT_MATCH, _T("Next match")); m_popupMenu->Append(POEM_NEXT_MATCH, _T("Next match"));
popupMenu->Append(POEM_COPY, _T("Copy to clipboard")); m_popupMenu->Append(POEM_COPY, _T("Copy to clipboard"));
popupMenu->Append(POEM_MINIMIZE, _T("Minimize")); m_popupMenu->Append(POEM_MINIMIZE, _T("Minimize"));
popupMenu->AppendSeparator(); m_popupMenu->AppendSeparator();
popupMenu->Append(POEM_BIGGER_TEXT, _T("Bigger text")); m_popupMenu->Append(POEM_BIGGER_TEXT, _T("Bigger text"));
popupMenu->Append(POEM_SMALLER_TEXT, _T("Smaller text")); m_popupMenu->Append(POEM_SMALLER_TEXT, _T("Smaller text"));
popupMenu->AppendSeparator(); m_popupMenu->AppendSeparator();
popupMenu->Append(POEM_ABOUT, _T("About wxPoem")); m_popupMenu->Append(POEM_ABOUT, _T("About wxPoem"));
popupMenu->AppendSeparator(); m_popupMenu->AppendSeparator();
popupMenu->Append(POEM_EXIT, _T("Exit")); m_popupMenu->Append(POEM_EXIT, _T("Exit"));
} }
MyCanvas::~MyCanvas() MyCanvas::~MyCanvas()
{ {
// Note: this must be done before the main window/canvas are destroyed // Note: this must be done before the main window/canvas are destroyed
// or we get an error (no parent window for menu item button) // or we get an error (no parent window for menu item button)
delete popupMenu; delete m_popupMenu;
popupMenu = NULL; m_popupMenu = NULL;
} }
// Define the repainting behaviour // Define the repainting behaviour
@@ -679,7 +669,7 @@ void MyCanvas::OnMouseEvent(wxMouseEvent& event)
if (event.RightDown()) if (event.RightDown())
{ {
// Versions from wxWin 1.67 are probably OK // Versions from wxWin 1.67 are probably OK
PopupMenu(popupMenu, (int)x, (int)y ); PopupMenu(m_popupMenu, (int)x, (int)y );
} }
else if (event.LeftDown()) else if (event.LeftDown())
{ {
@@ -717,17 +707,20 @@ void MyCanvas::OnChar(wxKeyEvent& event)
// Next match // Next match
TheMainWindow->Search(false); TheMainWindow->Search(false);
break; break;
case 's': case 's':
case 'S': case 'S':
// New search // New search
TheMainWindow->Search(true); TheMainWindow->Search(true);
break; break;
case WXK_SPACE: case WXK_SPACE:
case WXK_RIGHT: case WXK_RIGHT:
case WXK_DOWN: case WXK_DOWN:
// Another poem // Another poem
TheMainWindow->NextPage(); TheMainWindow->NextPage();
break; break;
case WXK_ESCAPE: case WXK_ESCAPE:
TheMainWindow->Close(true); TheMainWindow->Close(true);
default: default:
@@ -779,7 +772,7 @@ int GetIndex()
} }
// Read preferences // Read preferences
void ReadPreferences() void MainWindow::ReadPreferences()
{ {
#if wxUSE_RESOURCES #if wxUSE_RESOURCES
wxGetResource(_T("wxPoem"), _T("FontSize"), &pointSize); wxGetResource(_T("wxPoem"), _T("FontSize"), &pointSize);
@@ -789,7 +782,7 @@ void ReadPreferences()
} }
// Write preferences to disk // Write preferences to disk
void WritePreferences() void MainWindow::WritePreferences()
{ {
#ifdef __WXMSW__ #ifdef __WXMSW__
TheMainWindow->GetPosition(&XPos, &YPos); TheMainWindow->GetPosition(&XPos, &YPos);
@@ -869,7 +862,7 @@ bool LoadPoem(wxChar *file_name, long position)
// Do the search // Do the search
long MainWindow::DoSearch(void) long MainWindow::DoSearch(void)
{ {
if (!search_string) if (m_searchString.empty())
return false; return false;
FILE *file; FILE *file;
@@ -880,7 +873,7 @@ long MainWindow::DoSearch(void)
long previous_poem_start; long previous_poem_start;
bool found = false; bool found = false;
int search_length = wxStrlen(search_string); int search_length = m_searchString.length();
if (same_search) if (same_search)
{ {
@@ -914,7 +907,7 @@ long MainWindow::DoSearch(void)
// Only match if we're looking at a different poem // Only match if we're looking at a different poem
// (no point in displaying the same poem again) // (no point in displaying the same poem again)
if ((ch == search_string[i]) && (last_poem_start != previous_poem_start)) if ((ch == m_searchString[i]) && (last_poem_start != previous_poem_start))
{ {
if (i == 0) if (i == 0)
last_find = ftell(file); last_find = ftell(file);
@@ -923,7 +916,9 @@ long MainWindow::DoSearch(void)
i ++; i ++;
} }
else else
{
i = 0; i = 0;
}
if (ch == '#') if (ch == '#')
{ {
@@ -933,13 +928,15 @@ long MainWindow::DoSearch(void)
} }
fclose(file); fclose(file);
if (ch == EOF) if (ch == EOF)
{
last_find = -1; last_find = -1;
}
if (found) if (found)
{ {
return last_poem_start; return last_poem_start;
} }
else
return -1; return -1;
} }
@@ -1058,11 +1055,11 @@ void MainWindow::OnPopup(wxCommandEvent& event)
{ {
static wxString s; static wxString s;
s = poem_buffer; s = poem_buffer;
s.Replace( _T("@P"),_T("")); s.Replace( _T("@P"),wxEmptyString);
s.Replace( _T("@A "),_T("")); s.Replace( _T("@A "),wxEmptyString);
s.Replace( _T("@A"),_T("")); s.Replace( _T("@A"),wxEmptyString);
s.Replace( _T("@T "),_T("")); s.Replace( _T("@T "),wxEmptyString);
s.Replace( _T("@T"),_T("")); s.Replace( _T("@T"),wxEmptyString);
wxTextDataObject *data = new wxTextDataObject( s.c_str() ); wxTextDataObject *data = new wxTextDataObject( s.c_str() );
if (!wxTheClipboard->SetData( data )) if (!wxTheClipboard->SetData( data ))
wxMessageBox(_T("Error while copying to the clipboard.")); wxMessageBox(_T("Error while copying to the clipboard."));
@@ -1074,19 +1071,12 @@ void MainWindow::OnPopup(wxCommandEvent& event)
wxTheClipboard->Close(); wxTheClipboard->Close();
break; break;
#endif #endif
case POEM_COMPILE:
// Compile index
Compile();
break;
case POEM_BIGGER_TEXT: case POEM_BIGGER_TEXT:
{
pointSize ++; pointSize ++;
CreateFonts(); CreateFonts();
TheMainWindow->Resize(); TheMainWindow->Resize();
break; break;
}
case POEM_SMALLER_TEXT: case POEM_SMALLER_TEXT:
{
if (pointSize > 2) if (pointSize > 2)
{ {
pointSize --; pointSize --;
@@ -1094,13 +1084,10 @@ void MainWindow::OnPopup(wxCommandEvent& event)
TheMainWindow->Resize(); TheMainWindow->Resize();
} }
break; break;
}
case POEM_ABOUT: case POEM_ABOUT:
{
(void)wxMessageBox(_T("wxPoem Version 1.1\nJulian Smart (c) 1995"), (void)wxMessageBox(_T("wxPoem Version 1.1\nJulian Smart (c) 1995"),
_T("About wxPoem"), wxOK, TheMainWindow); _T("About wxPoem"), wxOK, TheMainWindow);
break; break;
}
case POEM_EXIT: case POEM_EXIT:
// Exit // Exit
TheMainWindow->Close(true); TheMainWindow->Close(true);

View File

@@ -32,16 +32,17 @@ DECLARE_APP(MyApp)
class MyCanvas: public wxWindow class MyCanvas: public wxWindow
{ {
public: public:
MyCanvas(wxFrame *frame, wxWindowID id, const wxPoint& pos, const wxSize& size); MyCanvas(wxFrame *frame);
~MyCanvas(); ~MyCanvas();
void OnPaint(wxPaintEvent& event); void OnPaint(wxPaintEvent& event);
void OnMouseEvent(wxMouseEvent& event); void OnMouseEvent(wxMouseEvent& event);
void OnChar(wxKeyEvent& event); void OnChar(wxKeyEvent& event);
DECLARE_EVENT_TABLE()
private: private:
wxMenu *popupMenu; wxMenu *m_popupMenu;
DECLARE_EVENT_TABLE()
}; };
// Define a new frame // Define a new frame
@@ -74,21 +75,35 @@ class MainWindow: public wxFrame
void GetIndexLoadPoem(void); void GetIndexLoadPoem(void);
void Resize(void); void Resize(void);
private:
wxString m_searchString;
wxString m_title;
// Preferences
void WritePreferences();
void ReadPreferences();
// Fonts
void CreateFonts();
wxFont *m_normalFont;
wxFont *m_boldFont;
wxFont *m_italicFont;
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };
// Menu items // Menu items
enum enum
{ {
POEM_NEXT = wxID_HIGHEST, POEM_ABOUT = wxID_ABOUT,
POEM_PREVIOUS, POEM_EXIT = wxID_EXIT,
POEM_COPY, POEM_PREVIOUS = wxID_BACKWARD,
POEM_SEARCH, POEM_COPY = wxID_COPY,
POEM_NEXT_MATCH, POEM_NEXT = wxID_FORWARD,
POEM_ABOUT, POEM_NEXT_MATCH = wxID_MORE,
POEM_EXIT, POEM_BIGGER_TEXT = wxID_ZOOM_IN,
POEM_COMPILE, POEM_SMALLER_TEXT = wxID_ZOOM_OUT,
POEM_BIGGER_TEXT, POEM_SEARCH = wxID_FIND,
POEM_SMALLER_TEXT, POEM_MINIMIZE = wxID_ICONIZE_FRAME
POEM_MINIMIZE
}; };