1998-12-12 13:50:16 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wxpoem.cpp
|
|
|
|
// Purpose: A small C++ program which displays a random poem on
|
|
|
|
// execution. It also allows search for poems containing a
|
|
|
|
// string.
|
|
|
|
// It requires winpoem.dat and creates winpoem.idx.
|
|
|
|
// Original version (WinPoem) written in 1994.
|
|
|
|
// This has not been rewritten in a long time so
|
|
|
|
// beware, inelegant code!
|
|
|
|
// Author: Julian Smart
|
|
|
|
// Created: 12/12/98
|
|
|
|
// Copyright: (c) 1998 Julian Smart
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
1999-06-01 11:32:12 -04:00
|
|
|
#include "wx/wx.h"
|
1998-12-12 13:50:16 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wxpoem.h"
|
|
|
|
|
1998-12-13 06:27:27 -05:00
|
|
|
#include "corner1.xpm"
|
|
|
|
#include "corner2.xpm"
|
|
|
|
#include "corner3.xpm"
|
|
|
|
#include "corner4.xpm"
|
2004-03-01 16:48:05 -05:00
|
|
|
#include "wxpoem.xpm"
|
1998-12-13 06:27:27 -05:00
|
|
|
|
2008-03-24 08:23:31 -04:00
|
|
|
#define BUFFER_SIZE 10000
|
1998-12-12 13:50:16 -05:00
|
|
|
#define DEFAULT_POETRY_DAT "wxpoem"
|
|
|
|
#define DEFAULT_POETRY_IND "wxpoem"
|
|
|
|
#define DEFAULT_CHAR_HEIGHT 18
|
|
|
|
#define DEFAULT_FONT "Swiss"
|
|
|
|
#define DEFAULT_X_POS 0
|
|
|
|
#define DEFAULT_Y_POS 0
|
|
|
|
#define BORDER_SIZE 30
|
|
|
|
#define THIN_LINE_BORDER 10
|
|
|
|
#define THICK_LINE_BORDER 16
|
|
|
|
#define THICK_LINE_WIDTH 2
|
|
|
|
#define SHADOW_OFFSET 1
|
|
|
|
#define X_SIZE 30
|
|
|
|
#define Y_SIZE 20
|
|
|
|
|
2008-03-24 08:23:31 -04:00
|
|
|
static wxChar *poem_buffer; // Storage for each poem
|
2003-09-11 04:50:16 -04:00
|
|
|
static wxChar line[150]; // Storage for a line
|
1998-12-12 13:50:16 -05:00
|
|
|
static int pages[30]; // For multipage poems -
|
|
|
|
// store the start of each page
|
|
|
|
static long last_poem_start = 0; // Start of last found poem
|
|
|
|
static long last_find = -1; // Point in file of last found
|
|
|
|
// search string
|
2004-02-09 15:01:55 -05:00
|
|
|
static bool search_ok = false; // Search was successful
|
|
|
|
static bool same_search = false; // Searching on same string
|
1998-12-12 13:50:16 -05:00
|
|
|
|
|
|
|
static long poem_index[600]; // Index of poem starts
|
|
|
|
static long nitems = 0; // Number of poems
|
|
|
|
static int char_height = DEFAULT_CHAR_HEIGHT; // Actual height
|
|
|
|
static int index_ptr = -1; // Pointer into index
|
|
|
|
static int poem_height, poem_width; // Size of poem
|
|
|
|
static int XPos; // Startup X position
|
|
|
|
static int YPos; // Startup Y position
|
|
|
|
static int pointSize = 12; // Font size
|
|
|
|
|
2009-07-05 09:34:46 -04:00
|
|
|
static const wxChar *index_filename = NULL; // Index filename
|
|
|
|
static const wxChar *data_filename = NULL; // Data filename
|
2003-09-11 04:50:16 -04:00
|
|
|
static wxChar error_buf[300]; // Error message buffer
|
2004-02-09 15:01:55 -05:00
|
|
|
static bool loaded_ok = false; // Poem loaded ok
|
|
|
|
static bool index_ok = false; // Index loaded ok
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2004-02-09 15:01:55 -05:00
|
|
|
static bool paging = false; // Are we paging?
|
1998-12-12 13:50:16 -05:00
|
|
|
static int current_page = 0; // Currently viewed page
|
|
|
|
|
|
|
|
// Backing bitmap
|
|
|
|
wxBitmap *backingBitmap = NULL;
|
|
|
|
|
2009-07-23 16:30:22 -04:00
|
|
|
void PoetryError(const wxChar *, const wxChar *caption=wxT("wxPoem Error"));
|
|
|
|
void PoetryNotify(const wxChar *Msg, const wxChar *caption=wxT("wxPoem"));
|
1998-12-12 13:50:16 -05:00
|
|
|
void TryLoadIndex();
|
2009-07-05 09:34:46 -04:00
|
|
|
bool LoadPoem(const wxChar *, long);
|
1998-12-12 13:50:16 -05:00
|
|
|
int GetIndex();
|
2009-07-05 09:34:46 -04:00
|
|
|
int LoadIndex(const wxChar *);
|
1998-12-12 13:50:16 -05:00
|
|
|
bool Compile(void);
|
|
|
|
void FindMax(int *max_thing, int thing);
|
|
|
|
|
2004-02-09 15:01:55 -05:00
|
|
|
#if wxUSE_CLIPBOARD
|
|
|
|
#include "wx/dataobj.h"
|
|
|
|
#include "wx/clipbrd.h"
|
|
|
|
#endif
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxIMPLEMENT_APP(MyApp);
|
1998-12-12 13:50:16 -05:00
|
|
|
|
|
|
|
MainWindow *TheMainWindow = NULL;
|
|
|
|
|
|
|
|
// Create the fonts
|
2005-03-15 15:33:42 -05:00
|
|
|
void MainWindow::CreateFonts()
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2017-11-25 11:39:44 -05:00
|
|
|
m_normalFont = wxTheFontList->FindOrCreateFont(wxFontInfo(pointSize).Family(wxFONTFAMILY_SWISS));
|
|
|
|
m_boldFont = wxTheFontList->FindOrCreateFont(wxFontInfo(pointSize).Family(wxFONTFAMILY_SWISS).Bold());
|
|
|
|
m_italicFont = wxTheFontList->FindOrCreateFont(wxFontInfo(pointSize).Family(wxFONTFAMILY_SWISS).Italic());
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxBEGIN_EVENT_TABLE(MainWindow, wxFrame)
|
1998-12-12 13:50:16 -05:00
|
|
|
EVT_CLOSE(MainWindow::OnCloseWindow)
|
|
|
|
EVT_CHAR(MainWindow::OnChar)
|
2004-02-09 15:01:55 -05:00
|
|
|
EVT_MENU(wxID_ANY, MainWindow::OnPopup)
|
2015-04-23 07:49:01 -04:00
|
|
|
wxEND_EVENT_TABLE()
|
1998-12-12 13:50:16 -05:00
|
|
|
|
|
|
|
MainWindow::MainWindow(wxFrame *frame, wxWindowID id, const wxString& title,
|
2005-03-15 15:33:42 -05:00
|
|
|
const wxPoint& pos, const wxSize& size, long style):
|
|
|
|
wxFrame(frame, id, title, pos, size, style)
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2005-03-21 15:22:39 -05:00
|
|
|
m_corners[0] = m_corners[1] = m_corners[2] = m_corners[3] = NULL;
|
|
|
|
|
2005-03-15 15:33:42 -05:00
|
|
|
ReadPreferences();
|
|
|
|
CreateFonts();
|
2005-03-21 15:22:39 -05:00
|
|
|
|
2017-11-25 11:46:04 -05:00
|
|
|
SetIcon(wxICON(wxpoem));
|
2005-03-21 15:22:39 -05:00
|
|
|
|
|
|
|
m_corners[0] = new wxIcon( corner1_xpm );
|
|
|
|
m_corners[1] = new wxIcon( corner2_xpm );
|
|
|
|
m_corners[2] = new wxIcon( corner3_xpm );
|
|
|
|
m_corners[3] = new wxIcon( corner4_xpm );
|
|
|
|
}
|
|
|
|
|
|
|
|
MainWindow::~MainWindow()
|
|
|
|
{
|
|
|
|
for (int i=0;i<4;i++)
|
|
|
|
{
|
|
|
|
if(m_corners[i])
|
|
|
|
{
|
|
|
|
delete m_corners[i];
|
|
|
|
}
|
|
|
|
}
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the poetry buffer, either for finding the size
|
|
|
|
// or for writing to a bitmap (not to the window directly,
|
|
|
|
// since that displays messily)
|
|
|
|
// If DrawIt is true, we draw, otherwise we just determine the
|
|
|
|
// size the window should be.
|
|
|
|
void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
|
|
|
|
{
|
|
|
|
int i = pages[current_page];
|
|
|
|
int ch = -1;
|
|
|
|
int y = 0;
|
|
|
|
int j;
|
2003-09-11 04:50:16 -04:00
|
|
|
wxChar *line_ptr;
|
1998-12-12 13:50:16 -05:00
|
|
|
int curr_width = 0;
|
2004-02-09 15:01:55 -05:00
|
|
|
bool page_break = false;
|
1998-12-12 13:50:16 -05:00
|
|
|
|
|
|
|
int width = 0;
|
|
|
|
int height = 0;
|
|
|
|
|
|
|
|
if (DrawIt)
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
y = (*max_y - poem_height)/2;
|
|
|
|
width = *max_x;
|
|
|
|
height = *max_y;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (DrawIt && wxColourDisplay())
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
dc->SetBrush(*wxLIGHT_GREY_BRUSH);
|
2005-03-21 15:22:39 -05:00
|
|
|
dc->SetPen(*wxGREY_PEN);
|
2005-03-15 15:33:42 -05:00
|
|
|
dc->DrawRectangle(0, 0, width, height);
|
|
|
|
dc->SetBackgroundMode(wxTRANSPARENT);
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// See what ACTUAL char height is
|
2005-03-15 15:33:42 -05:00
|
|
|
if(m_normalFont)
|
|
|
|
dc->SetFont(*m_normalFont);
|
2007-05-09 21:46:57 -04:00
|
|
|
wxCoord xx;
|
|
|
|
wxCoord yy;
|
2009-07-23 16:30:22 -04:00
|
|
|
dc->GetTextExtent(wxT("X"), &xx, &yy);
|
1998-12-12 13:50:16 -05:00
|
|
|
char_height = (int)yy;
|
|
|
|
|
|
|
|
if (current_page == 0)
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
m_title = wxEmptyString;
|
|
|
|
}
|
|
|
|
else if (!m_title.empty())
|
|
|
|
{
|
|
|
|
dc->SetFont(* m_boldFont);
|
|
|
|
dc->GetTextExtent(m_title, &xx, &yy);
|
|
|
|
FindMax(&curr_width, (int)xx);
|
|
|
|
|
|
|
|
if (DrawIt)
|
|
|
|
{
|
|
|
|
int x = (width - xx)/2;
|
|
|
|
dc->SetFont(* m_boldFont);
|
|
|
|
|
|
|
|
// Change text to BLACK!
|
|
|
|
dc->SetTextForeground(* wxBLACK);
|
|
|
|
dc->DrawText(m_title, x, y);
|
|
|
|
// Change text to WHITE!
|
|
|
|
dc->SetTextForeground(* wxWHITE);
|
|
|
|
dc->DrawText(m_title, x-SHADOW_OFFSET, y-SHADOW_OFFSET);
|
|
|
|
}
|
|
|
|
y += char_height;
|
|
|
|
y += char_height;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
while (ch != 0 && !page_break)
|
|
|
|
{
|
|
|
|
j = 0;
|
2001-07-22 10:08:55 -04:00
|
|
|
#if defined(__WXMSW__) || defined(__WXMAC__)
|
1998-12-12 13:50:16 -05:00
|
|
|
while (((ch = poem_buffer[i]) != 13) && (ch != 0))
|
|
|
|
#else
|
|
|
|
while (((ch = poem_buffer[i]) != 10) && (ch != 0))
|
|
|
|
#endif
|
|
|
|
{
|
2004-10-05 11:38:40 -04:00
|
|
|
line[j] = (wxChar)ch;
|
1998-12-12 13:50:16 -05:00
|
|
|
j ++;
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
|
2001-07-22 10:08:55 -04:00
|
|
|
#if defined(__WXMSW__) || defined(__WXMAC__)
|
1998-12-12 13:50:16 -05:00
|
|
|
if (ch == 13)
|
|
|
|
#else
|
|
|
|
if (ch == 10)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
ch = -1;
|
|
|
|
i ++;
|
2002-05-14 09:53:48 -04:00
|
|
|
#if defined(__WXMSW__) || defined(__WXMAC__)
|
1998-12-12 13:50:16 -05:00
|
|
|
// Add another to skip the linefeed
|
|
|
|
i ++;
|
|
|
|
#endif
|
|
|
|
// If a single newline on its own, put a space in
|
|
|
|
if (j == 0)
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
line[j] = ' ';
|
|
|
|
j ++;
|
|
|
|
line[j] = 0;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j > 0)
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
line[j] = 0;
|
|
|
|
if (line[0] == '@')
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
switch (line[1])
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
case 'P':
|
|
|
|
paging = true;
|
|
|
|
page_break = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'T':
|
|
|
|
dc->SetFont(* m_boldFont);
|
|
|
|
line_ptr = line+3;
|
|
|
|
|
|
|
|
m_title = line_ptr;
|
2009-07-23 16:30:22 -04:00
|
|
|
m_title << wxT(" (cont'd)");
|
2005-03-15 15:33:42 -05:00
|
|
|
|
|
|
|
dc->GetTextExtent(line_ptr, &xx, &yy);
|
|
|
|
FindMax(&curr_width, (int)xx);
|
|
|
|
|
|
|
|
if (DrawIt)
|
|
|
|
{
|
|
|
|
int x = (width - xx)/2;
|
|
|
|
dc->SetFont(* m_boldFont);
|
|
|
|
|
|
|
|
// Change text to BLACK!
|
|
|
|
dc->SetTextForeground(* wxBLACK);
|
|
|
|
dc->DrawText(line_ptr, x, y);
|
|
|
|
|
|
|
|
// Change text to WHITE!
|
|
|
|
dc->SetTextForeground(* wxWHITE);
|
|
|
|
dc->DrawText(line_ptr, x-SHADOW_OFFSET, y-SHADOW_OFFSET);
|
|
|
|
dc->SetTextForeground(* wxWHITE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'A':
|
|
|
|
line_ptr = line+3;
|
|
|
|
dc->SetFont(* m_italicFont);
|
|
|
|
|
|
|
|
dc->GetTextExtent(line_ptr, &xx, &yy);
|
|
|
|
FindMax(&curr_width, (int)xx);
|
|
|
|
|
|
|
|
if (DrawIt)
|
|
|
|
{
|
|
|
|
int x = (width - xx)/2;
|
|
|
|
dc->SetTextForeground(* wxBLACK);
|
|
|
|
dc->DrawText(line_ptr, x, y);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Default: just ignore this line
|
|
|
|
default:
|
|
|
|
y -= char_height;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
2005-03-15 15:33:42 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dc->SetFont(* m_normalFont);
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2005-03-15 15:33:42 -05:00
|
|
|
dc->GetTextExtent(line, &xx, &yy);
|
1998-12-12 13:50:16 -05:00
|
|
|
FindMax(&curr_width, (int)xx);
|
|
|
|
|
|
|
|
if (DrawIt)
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
int x = (int)((width - xx)/2.0);
|
|
|
|
dc->SetFont(* m_normalFont);
|
|
|
|
dc->SetTextForeground(* wxBLACK);
|
|
|
|
dc->DrawText(line, x, y);
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
y += char_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write (cont'd)
|
|
|
|
if (page_break)
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
const wxChar *cont = wxT("(cont'd)");
|
2005-03-15 15:33:42 -05:00
|
|
|
|
|
|
|
dc->SetFont(* m_normalFont);
|
|
|
|
|
|
|
|
dc->GetTextExtent(cont, &xx, &yy);
|
|
|
|
FindMax(&curr_width, (int)xx);
|
|
|
|
if (DrawIt)
|
|
|
|
{
|
|
|
|
int x = (int)((width - xx)/2.0);
|
|
|
|
dc->SetFont(* m_normalFont);
|
|
|
|
dc->SetTextForeground(* wxBLACK);
|
|
|
|
dc->DrawText(cont, x, y);
|
|
|
|
}
|
|
|
|
y += 2*char_height;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
*max_x = (int)curr_width;
|
|
|
|
*max_y = (int)(y-char_height);
|
|
|
|
|
|
|
|
if (page_break)
|
2005-03-15 15:33:42 -05:00
|
|
|
pages[current_page+1] = i;
|
1998-12-12 13:50:16 -05:00
|
|
|
else
|
2005-03-15 15:33:42 -05:00
|
|
|
paging = false;
|
1998-12-12 13:50:16 -05:00
|
|
|
|
|
|
|
if (DrawIt)
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
// Draw dark grey thick border
|
|
|
|
if (wxColourDisplay())
|
|
|
|
{
|
|
|
|
dc->SetBrush(*wxGREY_BRUSH);
|
|
|
|
dc->SetPen(*wxGREY_PEN);
|
|
|
|
|
|
|
|
// Left side
|
|
|
|
dc->DrawRectangle(0, 0, THIN_LINE_BORDER, height);
|
|
|
|
// Top side
|
|
|
|
dc->DrawRectangle(THIN_LINE_BORDER, 0, width-THIN_LINE_BORDER, THIN_LINE_BORDER);
|
|
|
|
// Right side
|
|
|
|
dc->DrawRectangle(width-THIN_LINE_BORDER, THIN_LINE_BORDER, width, height-THIN_LINE_BORDER);
|
|
|
|
// Bottom side
|
|
|
|
dc->DrawRectangle(THIN_LINE_BORDER, height-THIN_LINE_BORDER, width-THIN_LINE_BORDER, height);
|
|
|
|
}
|
|
|
|
// Draw border
|
|
|
|
// Have grey background, plus 3-d border -
|
|
|
|
// One black rectangle.
|
|
|
|
// Inside this, left and top sides - dark grey. Bottom and right -
|
|
|
|
// white.
|
|
|
|
|
|
|
|
// Change pen to black
|
|
|
|
dc->SetPen(*wxBLACK_PEN);
|
|
|
|
dc->DrawLine(THIN_LINE_BORDER, THIN_LINE_BORDER, width-THIN_LINE_BORDER, THIN_LINE_BORDER);
|
|
|
|
dc->DrawLine(width-THIN_LINE_BORDER, THIN_LINE_BORDER, width-THIN_LINE_BORDER, height-THIN_LINE_BORDER);
|
|
|
|
dc->DrawLine(width-THIN_LINE_BORDER, height-THIN_LINE_BORDER, THIN_LINE_BORDER, height-THIN_LINE_BORDER);
|
|
|
|
dc->DrawLine(THIN_LINE_BORDER, height-THIN_LINE_BORDER, THIN_LINE_BORDER, THIN_LINE_BORDER);
|
|
|
|
|
|
|
|
// Right and bottom white lines - 'grey' (black!) if
|
|
|
|
// we're running on a mono display.
|
|
|
|
if (wxColourDisplay())
|
2005-03-21 15:22:39 -05:00
|
|
|
dc->SetPen(*wxWHITE_PEN);
|
2005-03-15 15:33:42 -05:00
|
|
|
else
|
2005-03-21 15:22:39 -05:00
|
|
|
dc->SetPen(*wxBLACK_PEN);
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2005-03-15 15:33:42 -05:00
|
|
|
dc->DrawLine(width-THICK_LINE_BORDER, THICK_LINE_BORDER,
|
|
|
|
width-THICK_LINE_BORDER, height-THICK_LINE_BORDER);
|
|
|
|
dc->DrawLine(width-THICK_LINE_BORDER, height-THICK_LINE_BORDER,
|
|
|
|
THICK_LINE_BORDER, height-THICK_LINE_BORDER);
|
|
|
|
|
|
|
|
// Left and top grey lines
|
2005-03-21 15:22:39 -05:00
|
|
|
dc->SetPen(*wxBLACK_PEN);
|
2005-03-15 15:33:42 -05:00
|
|
|
dc->DrawLine(THICK_LINE_BORDER, height-THICK_LINE_BORDER,
|
|
|
|
THICK_LINE_BORDER, THICK_LINE_BORDER);
|
|
|
|
dc->DrawLine(THICK_LINE_BORDER, THICK_LINE_BORDER,
|
|
|
|
width-THICK_LINE_BORDER, THICK_LINE_BORDER);
|
|
|
|
|
|
|
|
// Draw icons
|
2005-03-21 15:22:39 -05:00
|
|
|
dc->DrawIcon(* m_corners[0], 0, 0);
|
|
|
|
dc->DrawIcon(* m_corners[1], int(width-32), 0);
|
2005-03-15 15:33:42 -05:00
|
|
|
|
|
|
|
int y2 = height - 32;
|
|
|
|
int x2 = (width-32);
|
2005-03-21 15:22:39 -05:00
|
|
|
dc->DrawIcon(* m_corners[2], 0, y2);
|
|
|
|
dc->DrawIcon(* m_corners[3], x2, y2);
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get an index (randomly generated) and load the poem
|
|
|
|
void MainWindow::GetIndexLoadPoem(void)
|
|
|
|
{
|
|
|
|
if (index_ok)
|
2005-03-15 15:33:42 -05:00
|
|
|
index_ptr = GetIndex();
|
1998-12-12 13:50:16 -05:00
|
|
|
|
|
|
|
if (index_ptr > -1)
|
2005-03-15 15:33:42 -05:00
|
|
|
loaded_ok = LoadPoem(data_filename, -1);
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the size of the poem and resize the window accordingly
|
|
|
|
void MainWindow::Resize(void)
|
|
|
|
{
|
|
|
|
wxClientDC dc(canvas);
|
|
|
|
|
|
|
|
// Get the poem size
|
2004-02-09 15:01:55 -05:00
|
|
|
ScanBuffer(& dc, false, &poem_width, &poem_height);
|
1998-12-12 13:50:16 -05:00
|
|
|
int x = poem_width + (2*BORDER_SIZE);
|
|
|
|
int y = poem_height + (2*BORDER_SIZE);
|
|
|
|
|
|
|
|
SetClientSize(x, y);
|
|
|
|
|
|
|
|
// In case client size isn't what we set it to...
|
|
|
|
int xx, yy;
|
|
|
|
GetClientSize(&xx, &yy);
|
|
|
|
|
|
|
|
wxMemoryDC memDC;
|
|
|
|
if (backingBitmap) delete backingBitmap;
|
|
|
|
backingBitmap = new wxBitmap(x, yy);
|
|
|
|
memDC.SelectObject(* backingBitmap);
|
|
|
|
|
|
|
|
memDC.Clear();
|
2005-03-15 15:33:42 -05:00
|
|
|
ScanBuffer(&memDC, true, &xx, &yy);
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Which is more?
|
|
|
|
void FindMax(int *max_thing, int thing)
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
if (thing > *max_thing)
|
|
|
|
*max_thing = thing;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next page/poem
|
|
|
|
void MainWindow::NextPage(void)
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
if (paging)
|
|
|
|
current_page ++;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
current_page = 0;
|
|
|
|
GetIndexLoadPoem();
|
|
|
|
}
|
|
|
|
Resize();
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Previous page
|
|
|
|
void MainWindow::PreviousPage(void)
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
if (current_page > 0)
|
|
|
|
{
|
|
|
|
current_page --;
|
|
|
|
Resize();
|
|
|
|
}
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Search for a string
|
|
|
|
void MainWindow::Search(bool ask)
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
long position;
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2005-03-15 15:33:42 -05:00
|
|
|
if (ask || m_searchString.empty())
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxString s = wxGetTextFromUser( wxT("Enter search string"), wxT("Search"), m_searchString);
|
2005-03-24 15:01:55 -05:00
|
|
|
if (!s.empty())
|
2005-03-15 15:33:42 -05:00
|
|
|
{
|
|
|
|
s.MakeLower();
|
|
|
|
m_searchString = s;
|
|
|
|
search_ok = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
search_ok = false;
|
|
|
|
}
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
same_search = true;
|
|
|
|
search_ok = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_searchString.empty() && search_ok)
|
|
|
|
{
|
|
|
|
position = DoSearch();
|
|
|
|
if (position > -1)
|
|
|
|
{
|
|
|
|
loaded_ok = LoadPoem(data_filename, position);
|
|
|
|
Resize();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
last_poem_start = 0;
|
2009-07-23 16:30:22 -04:00
|
|
|
PoetryNotify(wxT("Search string not found."));
|
2005-03-15 15:33:42 -05:00
|
|
|
}
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
2008-03-24 08:23:31 -04:00
|
|
|
poem_buffer = new wxChar[BUFFER_SIZE];
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2005-03-21 15:22:39 -05:00
|
|
|
// Seed the random number generator
|
|
|
|
time_t current_time;
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2005-03-21 15:22:39 -05:00
|
|
|
(void)time(¤t_time);
|
|
|
|
srand((unsigned int)current_time);
|
1998-12-12 13:50:16 -05:00
|
|
|
|
|
|
|
// randomize();
|
2005-03-21 15:22:39 -05:00
|
|
|
pages[0] = 0;
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2005-03-21 15:22:39 -05:00
|
|
|
TheMainWindow = new MainWindow(NULL,
|
|
|
|
wxID_ANY,
|
2009-07-23 16:30:22 -04:00
|
|
|
wxT("wxPoem"),
|
2005-03-21 15:22:39 -05:00
|
|
|
wxPoint(XPos, YPos),
|
|
|
|
wxDefaultSize,
|
|
|
|
wxCAPTION|wxMINIMIZE_BOX|wxSYSTEM_MENU|wxCLOSE_BOX|wxFULL_REPAINT_ON_RESIZE
|
|
|
|
);
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2005-03-21 15:22:39 -05:00
|
|
|
TheMainWindow->canvas = new MyCanvas(TheMainWindow);
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2005-03-21 15:22:39 -05:00
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
index_filename = wxStrcpy(new wxChar[wxStrlen(argv[1]) + 1], argv[1]);
|
|
|
|
data_filename = wxStrcpy(new wxChar[wxStrlen(argv[1]) + 1], argv[1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
index_filename = wxT(DEFAULT_POETRY_IND);
|
|
|
|
data_filename = wxT(DEFAULT_POETRY_DAT);
|
2005-03-21 15:22:39 -05:00
|
|
|
}
|
|
|
|
TryLoadIndex();
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2005-03-21 15:22:39 -05:00
|
|
|
TheMainWindow->GetIndexLoadPoem();
|
|
|
|
TheMainWindow->Resize();
|
|
|
|
TheMainWindow->Show(true);
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2005-03-21 15:22:39 -05:00
|
|
|
return true;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int MyApp::OnExit()
|
|
|
|
{
|
2005-03-21 15:22:39 -05:00
|
|
|
if (backingBitmap)
|
|
|
|
delete backingBitmap;
|
1998-12-13 06:27:27 -05:00
|
|
|
|
2005-03-21 15:22:39 -05:00
|
|
|
delete[] poem_buffer;
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2005-03-21 15:22:39 -05:00
|
|
|
return 0;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
1998-12-13 06:27:27 -05:00
|
|
|
void MainWindow::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2005-03-21 15:22:39 -05:00
|
|
|
WritePreferences();
|
|
|
|
this->Destroy();
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::OnChar(wxKeyEvent& event)
|
|
|
|
{
|
|
|
|
canvas->OnChar(event);
|
|
|
|
}
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxBEGIN_EVENT_TABLE(MyCanvas, wxWindow)
|
1998-12-12 13:50:16 -05:00
|
|
|
EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
|
|
|
|
EVT_CHAR(MyCanvas::OnChar)
|
|
|
|
EVT_PAINT(MyCanvas::OnPaint)
|
2015-04-23 07:49:01 -04:00
|
|
|
wxEND_EVENT_TABLE()
|
1998-12-12 13:50:16 -05:00
|
|
|
|
|
|
|
// Define a constructor for my canvas
|
2005-03-15 15:33:42 -05:00
|
|
|
MyCanvas::MyCanvas(wxFrame *frame):
|
|
|
|
wxWindow(frame, wxID_ANY)
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
m_popupMenu = new wxMenu;
|
2009-07-23 16:30:22 -04:00
|
|
|
m_popupMenu->Append(POEM_NEXT, wxT("Next poem/page"));
|
|
|
|
m_popupMenu->Append(POEM_PREVIOUS, wxT("Previous page"));
|
2005-03-15 15:33:42 -05:00
|
|
|
m_popupMenu->AppendSeparator();
|
2009-07-23 16:30:22 -04:00
|
|
|
m_popupMenu->Append(POEM_SEARCH, wxT("Search"));
|
|
|
|
m_popupMenu->Append(POEM_NEXT_MATCH, wxT("Next match"));
|
|
|
|
m_popupMenu->Append(POEM_COPY, wxT("Copy to clipboard"));
|
|
|
|
m_popupMenu->Append(POEM_MINIMIZE, wxT("Minimize"));
|
2005-03-15 15:33:42 -05:00
|
|
|
m_popupMenu->AppendSeparator();
|
2009-07-23 16:30:22 -04:00
|
|
|
m_popupMenu->Append(POEM_BIGGER_TEXT, wxT("Bigger text"));
|
|
|
|
m_popupMenu->Append(POEM_SMALLER_TEXT, wxT("Smaller text"));
|
2005-03-15 15:33:42 -05:00
|
|
|
m_popupMenu->AppendSeparator();
|
2009-07-23 16:30:22 -04:00
|
|
|
m_popupMenu->Append(POEM_ABOUT, wxT("About wxPoem"));
|
2005-03-15 15:33:42 -05:00
|
|
|
m_popupMenu->AppendSeparator();
|
2009-07-23 16:30:22 -04:00
|
|
|
m_popupMenu->Append(POEM_EXIT, wxT("Exit"));
|
2004-02-09 15:01:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
MyCanvas::~MyCanvas()
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
// Note: this must be done before the main window/canvas are destroyed
|
|
|
|
// or we get an error (no parent window for menu item button)
|
|
|
|
delete m_popupMenu;
|
|
|
|
m_popupMenu = NULL;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Define the repainting behaviour
|
1998-12-13 06:27:27 -05:00
|
|
|
void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
|
|
|
wxPaintDC dc(this);
|
|
|
|
|
|
|
|
if (backingBitmap)
|
|
|
|
{
|
|
|
|
int xx, yy;
|
|
|
|
TheMainWindow->GetClientSize(&xx, &yy);
|
1999-02-05 11:44:52 -05:00
|
|
|
|
2002-03-12 05:56:39 -05:00
|
|
|
dc.DrawBitmap(* backingBitmap, 0, 0);
|
2004-10-05 11:38:40 -04:00
|
|
|
#if 0
|
1998-12-12 13:50:16 -05:00
|
|
|
wxMemoryDC memDC;
|
|
|
|
memDC.SelectObject(* backingBitmap);
|
|
|
|
dc.Blit(0, 0, backingBitmap->GetWidth(), backingBitmap->GetHeight(), &memDC, 0, 0);
|
2002-03-12 05:56:39 -05:00
|
|
|
#endif
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyCanvas::OnMouseEvent(wxMouseEvent& event)
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
static int startPosX, startPosY, startFrameX, startFrameY;
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2005-03-15 15:33:42 -05:00
|
|
|
long x, y;
|
|
|
|
event.GetPosition(&x, &y);
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2005-03-15 15:33:42 -05:00
|
|
|
if (event.RightDown())
|
|
|
|
{
|
|
|
|
// Versions from wxWin 1.67 are probably OK
|
|
|
|
PopupMenu(m_popupMenu, (int)x, (int)y );
|
|
|
|
}
|
|
|
|
else if (event.LeftDown())
|
|
|
|
{
|
|
|
|
this->CaptureMouse();
|
|
|
|
int x1 = (int)x;
|
|
|
|
int y1 = (int)y;
|
|
|
|
ClientToScreen(&x1, &y1);
|
|
|
|
startPosX = x1;
|
|
|
|
startPosY = y1;
|
|
|
|
GetParent()->GetPosition(&startFrameX, &startFrameY);
|
|
|
|
}
|
|
|
|
else if (event.LeftUp())
|
|
|
|
{
|
|
|
|
if (GetCapture() == this) this->ReleaseMouse();
|
|
|
|
}
|
|
|
|
else if (event.Dragging() && event.LeftIsDown())
|
|
|
|
{
|
|
|
|
int x1 = (int)x;
|
|
|
|
int y1 = (int)y;
|
|
|
|
ClientToScreen(&x1, &y1);
|
1999-02-05 11:44:52 -05:00
|
|
|
|
2005-03-15 15:33:42 -05:00
|
|
|
int dX = x1 - startPosX;
|
|
|
|
int dY = y1 - startPosY;
|
|
|
|
GetParent()->Move(startFrameX + dX, startFrameY + dY);
|
|
|
|
}
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process characters
|
|
|
|
void MyCanvas::OnChar(wxKeyEvent& event)
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
switch (event.GetKeyCode())
|
|
|
|
{
|
|
|
|
case 'n':
|
|
|
|
case 'N':
|
|
|
|
// Next match
|
|
|
|
TheMainWindow->Search(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
case 'S':
|
|
|
|
// New search
|
|
|
|
TheMainWindow->Search(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WXK_SPACE:
|
|
|
|
case WXK_RIGHT:
|
|
|
|
case WXK_DOWN:
|
|
|
|
// Another poem
|
|
|
|
TheMainWindow->NextPage();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WXK_ESCAPE:
|
|
|
|
TheMainWindow->Close(true);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1998-12-12 13:50:16 -05:00
|
|
|
|
|
|
|
// Load index file
|
2009-07-05 09:34:46 -04:00
|
|
|
int LoadIndex(const wxChar *file_name)
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
|
|
|
long data;
|
|
|
|
FILE *index_file;
|
|
|
|
|
2003-09-11 04:50:16 -04:00
|
|
|
wxChar buf[100];
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2003-09-11 04:50:16 -04:00
|
|
|
if (file_name == NULL)
|
2005-03-15 15:33:42 -05:00
|
|
|
return 0;
|
2003-09-11 04:50:16 -04:00
|
|
|
|
2009-07-23 16:30:22 -04:00
|
|
|
wxSprintf(buf, wxT("%s.idx"), file_name);
|
2003-09-11 04:50:16 -04:00
|
|
|
|
2009-07-23 16:30:22 -04:00
|
|
|
index_file = wxFopen(buf, wxT("r"));
|
2003-09-11 04:50:16 -04:00
|
|
|
if (index_file == NULL)
|
2005-03-15 15:33:42 -05:00
|
|
|
return 0;
|
2003-09-11 04:50:16 -04:00
|
|
|
|
2009-07-23 16:30:22 -04:00
|
|
|
wxFscanf(index_file, wxT("%ld"), &nitems);
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2005-03-15 15:33:42 -05:00
|
|
|
for (int i = 0; i < nitems; i++)
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxFscanf(index_file, wxT("%ld"), &data);
|
1998-12-12 13:50:16 -05:00
|
|
|
poem_index[i] = data;
|
2005-03-15 15:33:42 -05:00
|
|
|
}
|
2003-09-11 04:50:16 -04:00
|
|
|
|
2005-03-15 15:33:42 -05:00
|
|
|
fclose(index_file);
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2005-03-15 15:33:42 -05:00
|
|
|
return 1;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get index
|
|
|
|
int GetIndex()
|
|
|
|
{
|
2004-01-08 06:33:32 -05:00
|
|
|
int indexn = (int)(rand() % nitems);
|
1998-12-12 13:50:16 -05:00
|
|
|
|
|
|
|
if ((indexn < 0) || (indexn > nitems))
|
2009-07-23 16:30:22 -04:00
|
|
|
{ PoetryError(wxT("No such poem!"));
|
1998-12-12 13:50:16 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return indexn;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read preferences
|
2005-03-15 15:33:42 -05:00
|
|
|
void MainWindow::ReadPreferences()
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2007-04-06 17:08:30 -04:00
|
|
|
/* TODO: convert this code to use wxConfig
|
2002-12-22 14:57:17 -05:00
|
|
|
#if wxUSE_RESOURCES
|
2009-07-23 16:30:22 -04:00
|
|
|
wxGetResource(wxT("wxPoem"), wxT("FontSize"), &pointSize);
|
|
|
|
wxGetResource(wxT("wxPoem"), wxT("X"), &XPos);
|
|
|
|
wxGetResource(wxT("wxPoem"), wxT("Y"), &YPos);
|
2002-12-22 14:57:17 -05:00
|
|
|
#endif
|
2007-04-06 17:08:30 -04:00
|
|
|
*/
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write preferences to disk
|
2005-03-15 15:33:42 -05:00
|
|
|
void MainWindow::WritePreferences()
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
|
|
|
#ifdef __WXMSW__
|
2005-03-15 15:33:42 -05:00
|
|
|
TheMainWindow->GetPosition(&XPos, &YPos);
|
2007-04-06 17:08:30 -04:00
|
|
|
/* TODO: convert this code to use wxConfig
|
2002-12-22 14:57:17 -05:00
|
|
|
#if wxUSE_RESOURCES
|
2009-07-23 16:30:22 -04:00
|
|
|
wxWriteResource(wxT("wxPoem"), wxT("FontSize"), pointSize);
|
|
|
|
wxWriteResource(wxT("wxPoem"), wxT("X"), XPos);
|
|
|
|
wxWriteResource(wxT("wxPoem"), wxT("Y"), YPos);
|
2002-12-22 14:57:17 -05:00
|
|
|
#endif
|
2007-04-06 17:08:30 -04:00
|
|
|
*/
|
2002-12-22 14:57:17 -05:00
|
|
|
#endif
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load a poem from given file, at given point in file.
|
|
|
|
// If position is > -1, use this for the position in the
|
|
|
|
// file, otherwise use index[index_ptr] to find the correct position.
|
2009-07-05 09:34:46 -04:00
|
|
|
bool LoadPoem(const wxChar *file_name, long position)
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
1998-12-13 06:27:27 -05:00
|
|
|
// int j = 0;
|
|
|
|
// int indexn = 0;
|
2003-09-11 04:50:16 -04:00
|
|
|
wxChar buf[100];
|
1998-12-12 13:50:16 -05:00
|
|
|
long data;
|
|
|
|
FILE *data_file;
|
|
|
|
|
2004-02-09 15:01:55 -05:00
|
|
|
paging = false;
|
1998-12-12 13:50:16 -05:00
|
|
|
current_page = 0;
|
|
|
|
|
2003-09-11 04:50:16 -04:00
|
|
|
if (file_name == NULL)
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxSprintf(error_buf, wxT("Error in Poem loading."));
|
1998-12-12 13:50:16 -05:00
|
|
|
PoetryError(error_buf);
|
2004-02-09 15:01:55 -05:00
|
|
|
return false;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
2003-09-11 04:50:16 -04:00
|
|
|
|
2009-07-23 16:30:22 -04:00
|
|
|
wxSprintf(buf, wxT("%s.dat"), file_name);
|
2017-11-25 11:46:04 -05:00
|
|
|
data_file = wxFopen(buf, wxT("rb"));
|
2003-09-11 04:50:16 -04:00
|
|
|
|
|
|
|
if (data_file == NULL)
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxSprintf(error_buf, wxT("Data file %s not found."), buf);
|
2003-09-11 04:50:16 -04:00
|
|
|
PoetryError(error_buf);
|
2004-02-09 15:01:55 -05:00
|
|
|
return false;
|
2003-09-11 04:50:16 -04:00
|
|
|
}
|
|
|
|
|
2005-03-15 15:33:42 -05:00
|
|
|
if (position > -1)
|
1998-12-12 13:50:16 -05:00
|
|
|
data = position;
|
2005-03-15 15:33:42 -05:00
|
|
|
else
|
1998-12-12 13:50:16 -05:00
|
|
|
data = poem_index[index_ptr];
|
|
|
|
|
2005-03-15 15:33:42 -05:00
|
|
|
fseek(data_file, data, SEEK_SET);
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2005-03-15 15:33:42 -05:00
|
|
|
int ch = 0;
|
|
|
|
int i = 0;
|
|
|
|
while ((ch != EOF) && (ch != '#'))
|
|
|
|
{
|
1998-12-12 13:50:16 -05:00
|
|
|
ch = getc(data_file);
|
|
|
|
// Add a linefeed so it will copy to the clipboard ok
|
|
|
|
if (ch == 10)
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
poem_buffer[i] = 13;
|
|
|
|
i++;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
2004-10-05 11:38:40 -04:00
|
|
|
poem_buffer[i] = (wxChar)ch;
|
1998-12-12 13:50:16 -05:00
|
|
|
i ++;
|
|
|
|
|
2008-03-24 08:23:31 -04:00
|
|
|
if (i == BUFFER_SIZE)
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxSprintf(error_buf, wxT("%s"), wxT("Poetry buffer exceeded."));
|
2005-03-15 15:33:42 -05:00
|
|
|
PoetryError(error_buf);
|
|
|
|
return false;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
2005-03-15 15:33:42 -05:00
|
|
|
}
|
|
|
|
fclose(data_file);
|
|
|
|
poem_buffer[i-1] = 0;
|
|
|
|
return true;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do the search
|
|
|
|
long MainWindow::DoSearch(void)
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
if (m_searchString.empty())
|
|
|
|
return false;
|
1998-12-12 13:50:16 -05:00
|
|
|
|
|
|
|
FILE *file;
|
2005-03-21 15:22:39 -05:00
|
|
|
size_t i = 0;
|
1998-12-12 13:50:16 -05:00
|
|
|
int ch = 0;
|
2003-09-11 04:50:16 -04:00
|
|
|
wxChar buf[100];
|
1998-12-12 13:50:16 -05:00
|
|
|
long find_start;
|
|
|
|
long previous_poem_start;
|
|
|
|
|
2004-02-09 15:01:55 -05:00
|
|
|
bool found = false;
|
2005-03-21 15:22:39 -05:00
|
|
|
size_t search_length = m_searchString.length();
|
1998-12-12 13:50:16 -05:00
|
|
|
|
|
|
|
if (same_search)
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
find_start = last_find + 1;
|
|
|
|
previous_poem_start = last_poem_start;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
find_start = 0;
|
|
|
|
last_poem_start = 0;
|
|
|
|
previous_poem_start = -1;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data_filename)
|
2009-07-23 16:30:22 -04:00
|
|
|
wxSprintf(buf, wxT("%s.dat"), data_filename);
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2017-11-25 11:46:04 -05:00
|
|
|
file = wxFopen(buf, wxT("rb"));
|
2003-09-11 04:50:16 -04:00
|
|
|
if (! (data_filename && file))
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxSprintf(error_buf, wxT("Poetry data file %s not found\n"), buf);
|
2005-03-15 15:33:42 -05:00
|
|
|
PoetryError(error_buf);
|
|
|
|
return false;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fseek(file, find_start, SEEK_SET);
|
|
|
|
|
|
|
|
while ((ch != EOF) && !found)
|
|
|
|
{
|
|
|
|
ch = getc(file);
|
2004-02-09 15:01:55 -05:00
|
|
|
ch = wxTolower(ch); // Make lower case
|
1998-12-12 13:50:16 -05:00
|
|
|
|
|
|
|
// Only match if we're looking at a different poem
|
|
|
|
// (no point in displaying the same poem again)
|
2007-06-27 16:49:19 -04:00
|
|
|
if ((m_searchString[i] == ch) && (last_poem_start != previous_poem_start))
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
if (i == 0)
|
|
|
|
last_find = ftell(file);
|
|
|
|
if (i == search_length-1)
|
|
|
|
found = true;
|
|
|
|
i ++;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
else
|
2005-03-15 15:33:42 -05:00
|
|
|
{
|
|
|
|
i = 0;
|
|
|
|
}
|
1998-12-12 13:50:16 -05:00
|
|
|
|
|
|
|
if (ch == '#')
|
|
|
|
{
|
|
|
|
ch = getc(file);
|
2005-03-15 15:33:42 -05:00
|
|
|
last_poem_start = ftell(file);
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
if (ch == EOF)
|
2005-03-15 15:33:42 -05:00
|
|
|
{
|
|
|
|
last_find = -1;
|
|
|
|
}
|
1998-12-12 13:50:16 -05:00
|
|
|
|
|
|
|
if (found)
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
return last_poem_start;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
2005-03-15 15:33:42 -05:00
|
|
|
|
|
|
|
return -1;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up poetry filenames, preferences, load the index
|
|
|
|
// Load index (or compile it if none found)
|
|
|
|
void TryLoadIndex()
|
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
index_ok = (LoadIndex(index_filename) != 0);
|
|
|
|
if (!index_ok || (nitems == 0))
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
PoetryError(wxT("Index file not found; will compile new one"), wxT("wxPoem"));
|
2005-03-15 15:33:42 -05:00
|
|
|
index_ok = Compile();
|
|
|
|
}
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error message
|
2008-03-24 08:23:31 -04:00
|
|
|
void PoetryError(const wxChar *msg, const wxChar *caption)
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
wxMessageBox(msg, caption, wxOK|wxICON_EXCLAMATION);
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notification (change icon to something appropriate!)
|
2008-03-24 08:23:31 -04:00
|
|
|
void PoetryNotify(const wxChar *Msg, const wxChar *caption)
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
wxMessageBox(Msg, caption, wxOK | wxICON_INFORMATION);
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build up and save an index into the poetry data file, for
|
|
|
|
// rapid random access
|
|
|
|
bool Compile(void)
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
int j;
|
2004-01-08 06:33:32 -05:00
|
|
|
int ch;
|
2003-09-11 04:50:16 -04:00
|
|
|
wxChar buf[100];
|
1998-12-12 13:50:16 -05:00
|
|
|
|
|
|
|
if (data_filename)
|
2009-07-23 16:30:22 -04:00
|
|
|
wxSprintf(buf, wxT("%s.dat"), data_filename);
|
1998-12-12 13:50:16 -05:00
|
|
|
|
2017-11-25 11:46:04 -05:00
|
|
|
file = wxFopen(buf, wxT("rb"));
|
2003-09-11 04:50:16 -04:00
|
|
|
if (! (data_filename && file))
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxSprintf(error_buf, wxT("Poetry data file %s not found\n"), buf);
|
2005-03-15 15:33:42 -05:00
|
|
|
PoetryError(error_buf);
|
|
|
|
return false;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
nitems = 0;
|
|
|
|
|
|
|
|
// Do first one (?)
|
|
|
|
poem_index[nitems] = 0;
|
|
|
|
nitems ++;
|
|
|
|
|
|
|
|
// Do rest
|
2004-10-05 11:38:40 -04:00
|
|
|
|
2004-01-08 06:33:32 -05:00
|
|
|
do {
|
1998-12-12 13:50:16 -05:00
|
|
|
ch = getc(file);
|
|
|
|
if (ch == '#')
|
|
|
|
{
|
|
|
|
ch = getc(file);
|
|
|
|
long data;
|
|
|
|
data = ftell(file);
|
|
|
|
poem_index[nitems] = data;
|
|
|
|
nitems ++;
|
|
|
|
}
|
2004-01-08 06:33:32 -05:00
|
|
|
} while (ch != EOF);
|
1998-12-12 13:50:16 -05:00
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
if (index_filename)
|
2009-07-23 16:30:22 -04:00
|
|
|
wxSprintf(buf, wxT("%s.idx"), index_filename);
|
2003-09-11 04:50:16 -04:00
|
|
|
|
2009-07-23 16:30:22 -04:00
|
|
|
file = wxFopen(buf, wxT("w"));
|
2003-09-11 04:50:16 -04:00
|
|
|
if (! (data_filename && file))
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxSprintf(error_buf, wxT("Poetry index file %s cannot be created\n"), buf);
|
2005-03-15 15:33:42 -05:00
|
|
|
PoetryError(error_buf);
|
|
|
|
return false;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
2009-07-23 16:30:22 -04:00
|
|
|
wxFprintf(file, wxT("%ld\n\n"), nitems);
|
1998-12-12 13:50:16 -05:00
|
|
|
for (j = 0; j < nitems; j++)
|
2009-07-23 16:30:22 -04:00
|
|
|
wxFprintf(file, wxT("%ld\n"), poem_index[j]);
|
1999-02-05 11:44:52 -05:00
|
|
|
|
1998-12-12 13:50:16 -05:00
|
|
|
fclose(file);
|
2009-07-23 16:30:22 -04:00
|
|
|
PoetryNotify(wxT("Poetry index compiled."));
|
2004-02-09 15:01:55 -05:00
|
|
|
return true;
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|
|
|
|
|
1999-11-04 10:59:39 -05:00
|
|
|
void MainWindow::OnPopup(wxCommandEvent& event)
|
1998-12-12 13:50:16 -05:00
|
|
|
{
|
2005-03-15 15:33:42 -05:00
|
|
|
switch (event.GetId())
|
|
|
|
{
|
|
|
|
case POEM_NEXT:
|
|
|
|
// Another poem/page
|
|
|
|
TheMainWindow->NextPage();
|
|
|
|
break;
|
|
|
|
case POEM_PREVIOUS:
|
|
|
|
// Previous page
|
|
|
|
TheMainWindow->PreviousPage();
|
|
|
|
break;
|
|
|
|
case POEM_SEARCH:
|
|
|
|
// Search - with dialog
|
|
|
|
TheMainWindow->Search(true);
|
|
|
|
break;
|
|
|
|
case POEM_NEXT_MATCH:
|
|
|
|
// Search - without dialog (next match)
|
|
|
|
TheMainWindow->Search(false);
|
|
|
|
break;
|
|
|
|
case POEM_MINIMIZE:
|
|
|
|
TheMainWindow->Iconize(true);
|
|
|
|
break;
|
2004-02-09 15:01:55 -05:00
|
|
|
#if wxUSE_CLIPBOARD
|
2005-03-15 15:33:42 -05:00
|
|
|
case POEM_COPY:
|
|
|
|
wxTheClipboard->UsePrimarySelection();
|
|
|
|
if (wxTheClipboard->Open())
|
|
|
|
{
|
|
|
|
static wxString s;
|
|
|
|
s = poem_buffer;
|
2009-07-23 16:30:22 -04:00
|
|
|
s.Replace( wxT("@P"),wxEmptyString);
|
|
|
|
s.Replace( wxT("@A "),wxEmptyString);
|
|
|
|
s.Replace( wxT("@A"),wxEmptyString);
|
|
|
|
s.Replace( wxT("@T "),wxEmptyString);
|
|
|
|
s.Replace( wxT("@T"),wxEmptyString);
|
2005-03-15 15:33:42 -05:00
|
|
|
wxTextDataObject *data = new wxTextDataObject( s.c_str() );
|
|
|
|
if (!wxTheClipboard->SetData( data ))
|
2009-07-23 16:30:22 -04:00
|
|
|
wxMessageBox(wxT("Error while copying to the clipboard."));
|
2005-03-15 15:33:42 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxMessageBox(wxT("Error opening the clipboard."));
|
2005-03-15 15:33:42 -05:00
|
|
|
}
|
|
|
|
wxTheClipboard->Close();
|
|
|
|
break;
|
1998-12-12 13:50:16 -05:00
|
|
|
#endif
|
2005-03-15 15:33:42 -05:00
|
|
|
case POEM_BIGGER_TEXT:
|
|
|
|
pointSize ++;
|
|
|
|
CreateFonts();
|
|
|
|
TheMainWindow->Resize();
|
|
|
|
break;
|
|
|
|
case POEM_SMALLER_TEXT:
|
|
|
|
if (pointSize > 2)
|
|
|
|
{
|
|
|
|
pointSize --;
|
|
|
|
CreateFonts();
|
|
|
|
TheMainWindow->Resize();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case POEM_ABOUT:
|
2009-07-23 16:30:22 -04:00
|
|
|
(void)wxMessageBox(wxT("wxPoem Version 1.1\nJulian Smart (c) 1995"),
|
|
|
|
wxT("About wxPoem"), wxOK, TheMainWindow);
|
2005-03-15 15:33:42 -05:00
|
|
|
break;
|
|
|
|
case POEM_EXIT:
|
|
|
|
// Exit
|
|
|
|
TheMainWindow->Close(true);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
1998-12-12 13:50:16 -05:00
|
|
|
}
|