]> git.saurik.com Git - wxWidgets.git/blame - demos/poem/wxpoem.cpp
Moved wxRichTextCtrl to its own library
[wxWidgets.git] / demos / poem / wxpoem.cpp
CommitLineData
3013b6f4
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: wxpoem.cpp
3// Purpose: A small C++ program which displays a random poem on
4// execution. It also allows search for poems containing a
5// string.
6// It requires winpoem.dat and creates winpoem.idx.
7// Original version (WinPoem) written in 1994.
8// This has not been rewritten in a long time so
9// beware, inelegant code!
10// Author: Julian Smart
11// Created: 12/12/98
12// RCS-ID: $Id$
13// Copyright: (c) 1998 Julian Smart
14// Licence: wxWindows licence
15/////////////////////////////////////////////////////////////////////////////
16
3013b6f4
JS
17// For compilers that support precompilation, includes "wx.h".
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
21#pragma hdrstop
22#endif
23
24#ifndef WX_PRECOMP
88ac883a 25#include "wx/wx.h"
3013b6f4
JS
26#endif
27
3013b6f4
JS
28#include "wxpoem.h"
29
bbf73190
RR
30#include "corner1.xpm"
31#include "corner2.xpm"
32#include "corner3.xpm"
33#include "corner4.xpm"
000a8253 34#include "wxpoem.xpm"
bbf73190 35
3013b6f4
JS
36#define buf_size 10000
37#define DEFAULT_POETRY_DAT "wxpoem"
38#define DEFAULT_POETRY_IND "wxpoem"
39#define DEFAULT_CHAR_HEIGHT 18
40#define DEFAULT_FONT "Swiss"
41#define DEFAULT_X_POS 0
42#define DEFAULT_Y_POS 0
43#define BORDER_SIZE 30
44#define THIN_LINE_BORDER 10
45#define THICK_LINE_BORDER 16
46#define THICK_LINE_WIDTH 2
47#define SHADOW_OFFSET 1
48#define X_SIZE 30
49#define Y_SIZE 20
50
6bb934d6
JS
51static wxChar *poem_buffer; // Storage for each poem
52static wxChar line[150]; // Storage for a line
3013b6f4
JS
53static int pages[30]; // For multipage poems -
54 // store the start of each page
55static long last_poem_start = 0; // Start of last found poem
56static long last_find = -1; // Point in file of last found
57 // search string
17d31882
DS
58static bool search_ok = false; // Search was successful
59static bool same_search = false; // Searching on same string
3013b6f4
JS
60
61static long poem_index[600]; // Index of poem starts
62static long nitems = 0; // Number of poems
3013b6f4
JS
63static int char_height = DEFAULT_CHAR_HEIGHT; // Actual height
64static int index_ptr = -1; // Pointer into index
65static int poem_height, poem_width; // Size of poem
66static int XPos; // Startup X position
67static int YPos; // Startup Y position
68static int pointSize = 12; // Font size
69
6bb934d6
JS
70static wxChar *index_filename = NULL; // Index filename
71static wxChar *data_filename = NULL; // Data filename
72static wxChar error_buf[300]; // Error message buffer
17d31882
DS
73static bool loaded_ok = false; // Poem loaded ok
74static bool index_ok = false; // Index loaded ok
3013b6f4 75
17d31882 76static bool paging = false; // Are we paging?
3013b6f4
JS
77static int current_page = 0; // Currently viewed page
78
3013b6f4
JS
79// Backing bitmap
80wxBitmap *backingBitmap = NULL;
81
6bb934d6
JS
82void PoetryError(wxChar *, wxChar *caption=_T("wxPoem Error"));
83void PoetryNotify(wxChar *Msg, wxChar *caption=_T("wxPoem"));
3013b6f4 84void TryLoadIndex();
6bb934d6 85bool LoadPoem(wxChar *, long);
3013b6f4 86int GetIndex();
6bb934d6 87int LoadIndex(wxChar *);
3013b6f4 88bool Compile(void);
3013b6f4 89void FindMax(int *max_thing, int thing);
3013b6f4 90
17d31882
DS
91#if wxUSE_CLIPBOARD
92 #include "wx/dataobj.h"
93 #include "wx/clipbrd.h"
94#endif
3013b6f4 95
a6f28223
JS
96#ifdef __WXWINCE__
97 STDAPI_(__int64) CeGetRandomSeed();
98#endif
99
3013b6f4
JS
100IMPLEMENT_APP(MyApp)
101
102MainWindow *TheMainWindow = NULL;
103
104// Create the fonts
c7bde65d 105void MainWindow::CreateFonts()
3013b6f4 106{
c7bde65d
WS
107 m_normalFont = wxTheFontList->FindOrCreateFont(pointSize, wxSWISS, wxNORMAL, wxNORMAL);
108 m_boldFont = wxTheFontList->FindOrCreateFont(pointSize, wxSWISS, wxNORMAL, wxBOLD);
109 m_italicFont = wxTheFontList->FindOrCreateFont(pointSize, wxSWISS, wxITALIC, wxNORMAL);
3013b6f4
JS
110}
111
112BEGIN_EVENT_TABLE(MainWindow, wxFrame)
113 EVT_CLOSE(MainWindow::OnCloseWindow)
114 EVT_CHAR(MainWindow::OnChar)
17d31882 115 EVT_MENU(wxID_ANY, MainWindow::OnPopup)
3013b6f4
JS
116END_EVENT_TABLE()
117
118MainWindow::MainWindow(wxFrame *frame, wxWindowID id, const wxString& title,
c7bde65d
WS
119 const wxPoint& pos, const wxSize& size, long style):
120 wxFrame(frame, id, title, pos, size, style)
3013b6f4 121{
3baa2662
WS
122 m_corners[0] = m_corners[1] = m_corners[2] = m_corners[3] = NULL;
123
c7bde65d
WS
124 ReadPreferences();
125 CreateFonts();
3baa2662
WS
126
127 SetIcon(wxpoem_xpm);
128
129 m_corners[0] = new wxIcon( corner1_xpm );
130 m_corners[1] = new wxIcon( corner2_xpm );
131 m_corners[2] = new wxIcon( corner3_xpm );
132 m_corners[3] = new wxIcon( corner4_xpm );
133}
134
135MainWindow::~MainWindow()
136{
137 for (int i=0;i<4;i++)
138 {
139 if(m_corners[i])
140 {
141 delete m_corners[i];
142 }
143 }
3013b6f4
JS
144}
145
146// Read the poetry buffer, either for finding the size
147// or for writing to a bitmap (not to the window directly,
148// since that displays messily)
149// If DrawIt is true, we draw, otherwise we just determine the
150// size the window should be.
151void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
152{
153 int i = pages[current_page];
154 int ch = -1;
3013b6f4
JS
155 int y = 0;
156 int j;
6bb934d6 157 wxChar *line_ptr;
3013b6f4 158 int curr_width = 0;
17d31882 159 bool page_break = false;
3013b6f4
JS
160
161 int width = 0;
162 int height = 0;
163
164 if (DrawIt)
165 {
c7bde65d
WS
166 y = (*max_y - poem_height)/2;
167 width = *max_x;
168 height = *max_y;
3013b6f4
JS
169 }
170
171 if (DrawIt && wxColourDisplay())
172 {
c7bde65d 173 dc->SetBrush(*wxLIGHT_GREY_BRUSH);
3baa2662 174 dc->SetPen(*wxGREY_PEN);
c7bde65d
WS
175 dc->DrawRectangle(0, 0, width, height);
176 dc->SetBackgroundMode(wxTRANSPARENT);
3013b6f4
JS
177 }
178
179 // See what ACTUAL char height is
c7bde65d
WS
180 if(m_normalFont)
181 dc->SetFont(*m_normalFont);
3013b6f4
JS
182 long xx;
183 long yy;
f37c24e0 184 dc->GetTextExtent(_T("X"), &xx, &yy);
3013b6f4
JS
185 char_height = (int)yy;
186
187 if (current_page == 0)
3013b6f4 188 {
c7bde65d
WS
189 m_title = wxEmptyString;
190 }
191 else if (!m_title.empty())
192 {
193 dc->SetFont(* m_boldFont);
194 dc->GetTextExtent(m_title, &xx, &yy);
195 FindMax(&curr_width, (int)xx);
196
197 if (DrawIt)
198 {
199 int x = (width - xx)/2;
200 dc->SetFont(* m_boldFont);
201
202 // Change text to BLACK!
203 dc->SetTextForeground(* wxBLACK);
204 dc->DrawText(m_title, x, y);
205 // Change text to WHITE!
206 dc->SetTextForeground(* wxWHITE);
207 dc->DrawText(m_title, x-SHADOW_OFFSET, y-SHADOW_OFFSET);
208 }
209 y += char_height;
210 y += char_height;
3013b6f4
JS
211 }
212
213 while (ch != 0 && !page_break)
214 {
215 j = 0;
685aded8 216#if defined(__WXMSW__) || defined(__WXMAC__)
3013b6f4
JS
217 while (((ch = poem_buffer[i]) != 13) && (ch != 0))
218#else
219 while (((ch = poem_buffer[i]) != 10) && (ch != 0))
220#endif
221 {
254a2129 222 line[j] = (wxChar)ch;
3013b6f4
JS
223 j ++;
224 i ++;
225 }
226
685aded8 227#if defined(__WXMSW__) || defined(__WXMAC__)
3013b6f4
JS
228 if (ch == 13)
229#else
230 if (ch == 10)
231#endif
232 {
233 ch = -1;
234 i ++;
6688f23d 235#if defined(__WXMSW__) || defined(__WXMAC__)
3013b6f4
JS
236 // Add another to skip the linefeed
237 i ++;
238#endif
239 // If a single newline on its own, put a space in
240 if (j == 0)
241 {
c7bde65d
WS
242 line[j] = ' ';
243 j ++;
244 line[j] = 0;
3013b6f4
JS
245 }
246 }
247
248 if (j > 0)
249 {
c7bde65d
WS
250 line[j] = 0;
251 if (line[0] == '@')
3013b6f4 252 {
c7bde65d 253 switch (line[1])
3013b6f4 254 {
c7bde65d
WS
255 case 'P':
256 paging = true;
257 page_break = true;
258 break;
259
260 case 'T':
261 dc->SetFont(* m_boldFont);
262 line_ptr = line+3;
263
264 m_title = line_ptr;
265 m_title << _T(" (cont'd)");
266
267 dc->GetTextExtent(line_ptr, &xx, &yy);
268 FindMax(&curr_width, (int)xx);
269
270 if (DrawIt)
271 {
272 int x = (width - xx)/2;
273 dc->SetFont(* m_boldFont);
274
275 // Change text to BLACK!
276 dc->SetTextForeground(* wxBLACK);
277 dc->DrawText(line_ptr, x, y);
278
279 // Change text to WHITE!
280 dc->SetTextForeground(* wxWHITE);
281 dc->DrawText(line_ptr, x-SHADOW_OFFSET, y-SHADOW_OFFSET);
282 dc->SetTextForeground(* wxWHITE);
283 }
284 break;
285
286 case 'A':
287 line_ptr = line+3;
288 dc->SetFont(* m_italicFont);
289
290 dc->GetTextExtent(line_ptr, &xx, &yy);
291 FindMax(&curr_width, (int)xx);
292
293 if (DrawIt)
294 {
295 int x = (width - xx)/2;
296 dc->SetTextForeground(* wxBLACK);
297 dc->DrawText(line_ptr, x, y);
298 }
299 break;
300
301 // Default: just ignore this line
302 default:
303 y -= char_height;
3013b6f4 304 }
c7bde65d
WS
305 }
306 else
307 {
308 dc->SetFont(* m_normalFont);
3013b6f4 309
c7bde65d 310 dc->GetTextExtent(line, &xx, &yy);
3013b6f4
JS
311 FindMax(&curr_width, (int)xx);
312
313 if (DrawIt)
314 {
c7bde65d
WS
315 int x = (int)((width - xx)/2.0);
316 dc->SetFont(* m_normalFont);
317 dc->SetTextForeground(* wxBLACK);
318 dc->DrawText(line, x, y);
3013b6f4 319 }
3013b6f4 320 }
3013b6f4
JS
321 }
322 y += char_height;
323 }
324
325 // Write (cont'd)
326 if (page_break)
327 {
c7bde65d
WS
328 wxChar *cont = _T("(cont'd)");
329
330 dc->SetFont(* m_normalFont);
331
332 dc->GetTextExtent(cont, &xx, &yy);
333 FindMax(&curr_width, (int)xx);
334 if (DrawIt)
335 {
336 int x = (int)((width - xx)/2.0);
337 dc->SetFont(* m_normalFont);
338 dc->SetTextForeground(* wxBLACK);
339 dc->DrawText(cont, x, y);
340 }
341 y += 2*char_height;
3013b6f4
JS
342 }
343
344 *max_x = (int)curr_width;
345 *max_y = (int)(y-char_height);
346
347 if (page_break)
c7bde65d 348 pages[current_page+1] = i;
3013b6f4 349 else
c7bde65d 350 paging = false;
3013b6f4
JS
351
352 if (DrawIt)
353 {
c7bde65d
WS
354 // Draw dark grey thick border
355 if (wxColourDisplay())
356 {
357 dc->SetBrush(*wxGREY_BRUSH);
358 dc->SetPen(*wxGREY_PEN);
359
360 // Left side
361 dc->DrawRectangle(0, 0, THIN_LINE_BORDER, height);
362 // Top side
363 dc->DrawRectangle(THIN_LINE_BORDER, 0, width-THIN_LINE_BORDER, THIN_LINE_BORDER);
364 // Right side
365 dc->DrawRectangle(width-THIN_LINE_BORDER, THIN_LINE_BORDER, width, height-THIN_LINE_BORDER);
366 // Bottom side
367 dc->DrawRectangle(THIN_LINE_BORDER, height-THIN_LINE_BORDER, width-THIN_LINE_BORDER, height);
368 }
369 // Draw border
370 // Have grey background, plus 3-d border -
371 // One black rectangle.
372 // Inside this, left and top sides - dark grey. Bottom and right -
373 // white.
374
375 // Change pen to black
376 dc->SetPen(*wxBLACK_PEN);
377 dc->DrawLine(THIN_LINE_BORDER, THIN_LINE_BORDER, width-THIN_LINE_BORDER, THIN_LINE_BORDER);
378 dc->DrawLine(width-THIN_LINE_BORDER, THIN_LINE_BORDER, width-THIN_LINE_BORDER, height-THIN_LINE_BORDER);
379 dc->DrawLine(width-THIN_LINE_BORDER, height-THIN_LINE_BORDER, THIN_LINE_BORDER, height-THIN_LINE_BORDER);
380 dc->DrawLine(THIN_LINE_BORDER, height-THIN_LINE_BORDER, THIN_LINE_BORDER, THIN_LINE_BORDER);
381
382 // Right and bottom white lines - 'grey' (black!) if
383 // we're running on a mono display.
384 if (wxColourDisplay())
3baa2662 385 dc->SetPen(*wxWHITE_PEN);
c7bde65d 386 else
3baa2662 387 dc->SetPen(*wxBLACK_PEN);
3013b6f4 388
c7bde65d
WS
389 dc->DrawLine(width-THICK_LINE_BORDER, THICK_LINE_BORDER,
390 width-THICK_LINE_BORDER, height-THICK_LINE_BORDER);
391 dc->DrawLine(width-THICK_LINE_BORDER, height-THICK_LINE_BORDER,
392 THICK_LINE_BORDER, height-THICK_LINE_BORDER);
393
394 // Left and top grey lines
3baa2662 395 dc->SetPen(*wxBLACK_PEN);
c7bde65d
WS
396 dc->DrawLine(THICK_LINE_BORDER, height-THICK_LINE_BORDER,
397 THICK_LINE_BORDER, THICK_LINE_BORDER);
398 dc->DrawLine(THICK_LINE_BORDER, THICK_LINE_BORDER,
399 width-THICK_LINE_BORDER, THICK_LINE_BORDER);
400
401 // Draw icons
3baa2662
WS
402 dc->DrawIcon(* m_corners[0], 0, 0);
403 dc->DrawIcon(* m_corners[1], int(width-32), 0);
c7bde65d
WS
404
405 int y2 = height - 32;
406 int x2 = (width-32);
3baa2662
WS
407 dc->DrawIcon(* m_corners[2], 0, y2);
408 dc->DrawIcon(* m_corners[3], x2, y2);
3013b6f4
JS
409 }
410}
411
412// Get an index (randomly generated) and load the poem
413void MainWindow::GetIndexLoadPoem(void)
414{
415 if (index_ok)
c7bde65d 416 index_ptr = GetIndex();
3013b6f4
JS
417
418 if (index_ptr > -1)
c7bde65d 419 loaded_ok = LoadPoem(data_filename, -1);
3013b6f4
JS
420}
421
422// Find the size of the poem and resize the window accordingly
423void MainWindow::Resize(void)
424{
425 wxClientDC dc(canvas);
426
427 // Get the poem size
17d31882 428 ScanBuffer(& dc, false, &poem_width, &poem_height);
3013b6f4
JS
429 int x = poem_width + (2*BORDER_SIZE);
430 int y = poem_height + (2*BORDER_SIZE);
431
432 SetClientSize(x, y);
433
434 // In case client size isn't what we set it to...
435 int xx, yy;
436 GetClientSize(&xx, &yy);
437
438 wxMemoryDC memDC;
439 if (backingBitmap) delete backingBitmap;
440 backingBitmap = new wxBitmap(x, yy);
441 memDC.SelectObject(* backingBitmap);
442
443 memDC.Clear();
c7bde65d 444 ScanBuffer(&memDC, true, &xx, &yy);
3013b6f4
JS
445}
446
447// Which is more?
448void FindMax(int *max_thing, int thing)
449{
c7bde65d
WS
450 if (thing > *max_thing)
451 *max_thing = thing;
3013b6f4
JS
452}
453
454// Next page/poem
455void MainWindow::NextPage(void)
456{
c7bde65d
WS
457 if (paging)
458 current_page ++;
459 else
460 {
461 current_page = 0;
462 GetIndexLoadPoem();
463 }
464 Resize();
3013b6f4
JS
465}
466
467// Previous page
468void MainWindow::PreviousPage(void)
469{
c7bde65d
WS
470 if (current_page > 0)
471 {
472 current_page --;
473 Resize();
474 }
3013b6f4
JS
475}
476
477// Search for a string
478void MainWindow::Search(bool ask)
479{
c7bde65d 480 long position;
3013b6f4 481
c7bde65d 482 if (ask || m_searchString.empty())
3013b6f4 483 {
c7bde65d 484 wxString s = wxGetTextFromUser( _T("Enter search string"), _T("Search"), m_searchString);
489f6cf7 485 if (!s.empty())
c7bde65d
WS
486 {
487 s.MakeLower();
488 m_searchString = s;
489 search_ok = true;
490 }
491 else
492 {
493 search_ok = false;
494 }
3013b6f4
JS
495 }
496 else
497 {
c7bde65d
WS
498 same_search = true;
499 search_ok = true;
500 }
501
502 if (!m_searchString.empty() && search_ok)
503 {
504 position = DoSearch();
505 if (position > -1)
506 {
507 loaded_ok = LoadPoem(data_filename, position);
508 Resize();
509 }
510 else
511 {
512 last_poem_start = 0;
513 PoetryNotify(_T("Search string not found."));
514 }
3013b6f4 515 }
3013b6f4
JS
516}
517
3013b6f4
JS
518bool MyApp::OnInit()
519{
3baa2662 520 poem_buffer = new wxChar[buf_size];
3013b6f4 521
3baa2662 522 // Seed the random number generator
a6f28223 523#ifdef __WXWINCE__
3baa2662 524 srand((unsigned) CeGetRandomSeed());
a6f28223 525#else
3baa2662 526 time_t current_time;
3013b6f4 527
3baa2662
WS
528 (void)time(&current_time);
529 srand((unsigned int)current_time);
a6f28223 530#endif
3013b6f4
JS
531
532// randomize();
3baa2662 533 pages[0] = 0;
3013b6f4 534
3baa2662
WS
535 TheMainWindow = new MainWindow(NULL,
536 wxID_ANY,
537 _T("wxPoem"),
538 wxPoint(XPos, YPos),
539 wxDefaultSize,
540 wxCAPTION|wxMINIMIZE_BOX|wxSYSTEM_MENU|wxCLOSE_BOX|wxFULL_REPAINT_ON_RESIZE
541 );
3013b6f4 542
3baa2662 543 TheMainWindow->canvas = new MyCanvas(TheMainWindow);
3013b6f4 544
3baa2662
WS
545 if (argc > 1)
546 {
547 index_filename = wxStrcpy(new wxChar[wxStrlen(argv[1]) + 1], argv[1]);
548 data_filename = wxStrcpy(new wxChar[wxStrlen(argv[1]) + 1], argv[1]);
549 }
550 else
551 {
552 index_filename = _T(DEFAULT_POETRY_IND);
553 data_filename = _T(DEFAULT_POETRY_DAT);
554 }
555 TryLoadIndex();
3013b6f4 556
3baa2662
WS
557 TheMainWindow->GetIndexLoadPoem();
558 TheMainWindow->Resize();
559 TheMainWindow->Show(true);
3013b6f4 560
3baa2662 561 return true;
3013b6f4
JS
562}
563
564int MyApp::OnExit()
565{
3baa2662
WS
566 if (backingBitmap)
567 delete backingBitmap;
bbf73190 568
3baa2662 569 delete[] poem_buffer;
3013b6f4 570
3baa2662 571 return 0;
3013b6f4
JS
572}
573
bbf73190 574void MainWindow::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
3013b6f4 575{
3baa2662
WS
576 WritePreferences();
577 this->Destroy();
3013b6f4
JS
578}
579
580void MainWindow::OnChar(wxKeyEvent& event)
581{
582 canvas->OnChar(event);
583}
584
b23386b2 585BEGIN_EVENT_TABLE(MyCanvas, wxWindow)
3013b6f4
JS
586 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
587 EVT_CHAR(MyCanvas::OnChar)
588 EVT_PAINT(MyCanvas::OnPaint)
589END_EVENT_TABLE()
590
591// Define a constructor for my canvas
c7bde65d
WS
592MyCanvas::MyCanvas(wxFrame *frame):
593 wxWindow(frame, wxID_ANY)
3013b6f4 594{
c7bde65d
WS
595 m_popupMenu = new wxMenu;
596 m_popupMenu->Append(POEM_NEXT, _T("Next poem/page"));
597 m_popupMenu->Append(POEM_PREVIOUS, _T("Previous page"));
598 m_popupMenu->AppendSeparator();
599 m_popupMenu->Append(POEM_SEARCH, _T("Search"));
600 m_popupMenu->Append(POEM_NEXT_MATCH, _T("Next match"));
601 m_popupMenu->Append(POEM_COPY, _T("Copy to clipboard"));
602 m_popupMenu->Append(POEM_MINIMIZE, _T("Minimize"));
603 m_popupMenu->AppendSeparator();
604 m_popupMenu->Append(POEM_BIGGER_TEXT, _T("Bigger text"));
605 m_popupMenu->Append(POEM_SMALLER_TEXT, _T("Smaller text"));
606 m_popupMenu->AppendSeparator();
607 m_popupMenu->Append(POEM_ABOUT, _T("About wxPoem"));
608 m_popupMenu->AppendSeparator();
609 m_popupMenu->Append(POEM_EXIT, _T("Exit"));
17d31882
DS
610}
611
612MyCanvas::~MyCanvas()
613{
c7bde65d
WS
614 // Note: this must be done before the main window/canvas are destroyed
615 // or we get an error (no parent window for menu item button)
616 delete m_popupMenu;
617 m_popupMenu = NULL;
3013b6f4
JS
618}
619
620// Define the repainting behaviour
bbf73190 621void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
3013b6f4
JS
622{
623 wxPaintDC dc(this);
624
625 if (backingBitmap)
626 {
627 int xx, yy;
628 TheMainWindow->GetClientSize(&xx, &yy);
f5d01a1c 629
59db9cfa 630 dc.DrawBitmap(* backingBitmap, 0, 0);
254a2129 631#if 0
3013b6f4
JS
632 wxMemoryDC memDC;
633 memDC.SelectObject(* backingBitmap);
634 dc.Blit(0, 0, backingBitmap->GetWidth(), backingBitmap->GetHeight(), &memDC, 0, 0);
59db9cfa 635#endif
3013b6f4
JS
636 }
637}
638
639void MyCanvas::OnMouseEvent(wxMouseEvent& event)
640{
c7bde65d 641 static int startPosX, startPosY, startFrameX, startFrameY;
3013b6f4 642
c7bde65d
WS
643 long x, y;
644 event.GetPosition(&x, &y);
3013b6f4 645
c7bde65d
WS
646 if (event.RightDown())
647 {
648 // Versions from wxWin 1.67 are probably OK
649 PopupMenu(m_popupMenu, (int)x, (int)y );
650 }
651 else if (event.LeftDown())
652 {
653 this->CaptureMouse();
654 int x1 = (int)x;
655 int y1 = (int)y;
656 ClientToScreen(&x1, &y1);
657 startPosX = x1;
658 startPosY = y1;
659 GetParent()->GetPosition(&startFrameX, &startFrameY);
660 }
661 else if (event.LeftUp())
662 {
663 if (GetCapture() == this) this->ReleaseMouse();
664 }
665 else if (event.Dragging() && event.LeftIsDown())
666 {
667 int x1 = (int)x;
668 int y1 = (int)y;
669 ClientToScreen(&x1, &y1);
f5d01a1c 670
c7bde65d
WS
671 int dX = x1 - startPosX;
672 int dY = y1 - startPosY;
673 GetParent()->Move(startFrameX + dX, startFrameY + dY);
674 }
3013b6f4
JS
675}
676
677// Process characters
678void MyCanvas::OnChar(wxKeyEvent& event)
679{
c7bde65d
WS
680 switch (event.GetKeyCode())
681 {
682 case 'n':
683 case 'N':
684 // Next match
685 TheMainWindow->Search(false);
686 break;
687
688 case 's':
689 case 'S':
690 // New search
691 TheMainWindow->Search(true);
692 break;
693
694 case WXK_SPACE:
695 case WXK_RIGHT:
696 case WXK_DOWN:
697 // Another poem
698 TheMainWindow->NextPage();
699 break;
700
701 case WXK_ESCAPE:
702 TheMainWindow->Close(true);
703 default:
704 break;
705 }
706}
3013b6f4
JS
707
708// Load index file
6bb934d6 709int LoadIndex(wxChar *file_name)
3013b6f4
JS
710{
711 long data;
712 FILE *index_file;
713
6bb934d6 714 wxChar buf[100];
3013b6f4 715
6bb934d6 716 if (file_name == NULL)
c7bde65d 717 return 0;
6bb934d6
JS
718
719 wxSprintf(buf, _T("%s.idx"), file_name);
720
994e41cb 721 index_file = wxFopen(buf, _T("r"));
6bb934d6 722 if (index_file == NULL)
c7bde65d 723 return 0;
6bb934d6
JS
724
725 wxFscanf(index_file, _T("%ld"), &nitems);
3013b6f4 726
c7bde65d
WS
727 for (int i = 0; i < nitems; i++)
728 {
729 wxFscanf(index_file, _T("%ld"), &data);
3013b6f4 730 poem_index[i] = data;
c7bde65d 731 }
6bb934d6 732
c7bde65d 733 fclose(index_file);
3013b6f4 734
c7bde65d 735 return 1;
3013b6f4
JS
736}
737
738// Get index
739int GetIndex()
740{
c4b77901 741 int indexn = (int)(rand() % nitems);
3013b6f4
JS
742
743 if ((indexn < 0) || (indexn > nitems))
6bb934d6 744 { PoetryError(_T("No such poem!"));
3013b6f4
JS
745 return -1;
746 }
747 else
748 return indexn;
749}
750
751// Read preferences
c7bde65d 752void MainWindow::ReadPreferences()
3013b6f4 753{
f37c24e0 754#if wxUSE_RESOURCES
c7bde65d
WS
755 wxGetResource(_T("wxPoem"), _T("FontSize"), &pointSize);
756 wxGetResource(_T("wxPoem"), _T("X"), &XPos);
757 wxGetResource(_T("wxPoem"), _T("Y"), &YPos);
f37c24e0 758#endif
3013b6f4
JS
759}
760
761// Write preferences to disk
c7bde65d 762void MainWindow::WritePreferences()
3013b6f4
JS
763{
764#ifdef __WXMSW__
c7bde65d 765 TheMainWindow->GetPosition(&XPos, &YPos);
f37c24e0 766#if wxUSE_RESOURCES
c7bde65d
WS
767 wxWriteResource(_T("wxPoem"), _T("FontSize"), pointSize);
768 wxWriteResource(_T("wxPoem"), _T("X"), XPos);
769 wxWriteResource(_T("wxPoem"), _T("Y"), YPos);
f37c24e0
MB
770#endif
771#endif
3013b6f4
JS
772}
773
774// Load a poem from given file, at given point in file.
775// If position is > -1, use this for the position in the
776// file, otherwise use index[index_ptr] to find the correct position.
6bb934d6 777bool LoadPoem(wxChar *file_name, long position)
3013b6f4 778{
bbf73190
RR
779// int j = 0;
780// int indexn = 0;
6bb934d6 781 wxChar buf[100];
3013b6f4
JS
782 long data;
783 FILE *data_file;
784
17d31882 785 paging = false;
3013b6f4
JS
786 current_page = 0;
787
6bb934d6 788 if (file_name == NULL)
3013b6f4 789 {
6bb934d6 790 wxSprintf(error_buf, _T("Error in Poem loading."));
3013b6f4 791 PoetryError(error_buf);
17d31882 792 return false;
3013b6f4 793 }
6bb934d6
JS
794
795 wxSprintf(buf, _T("%s.dat"), file_name);
994e41cb 796 data_file = wxFopen(buf, _T("r"));
6bb934d6
JS
797
798 if (data_file == NULL)
3013b6f4 799 {
6bb934d6
JS
800 wxSprintf(error_buf, _T("Data file %s not found."), buf);
801 PoetryError(error_buf);
17d31882 802 return false;
6bb934d6
JS
803 }
804
c7bde65d 805 if (position > -1)
3013b6f4 806 data = position;
c7bde65d 807 else
3013b6f4
JS
808 data = poem_index[index_ptr];
809
c7bde65d 810 fseek(data_file, data, SEEK_SET);
3013b6f4 811
c7bde65d
WS
812 int ch = 0;
813 int i = 0;
814 while ((ch != EOF) && (ch != '#'))
815 {
3013b6f4
JS
816 ch = getc(data_file);
817 // Add a linefeed so it will copy to the clipboard ok
818 if (ch == 10)
819 {
c7bde65d
WS
820 poem_buffer[i] = 13;
821 i++;
3013b6f4
JS
822 }
823
254a2129 824 poem_buffer[i] = (wxChar)ch;
3013b6f4
JS
825 i ++;
826
827 if (i == buf_size)
828 {
c7bde65d
WS
829 wxSprintf(error_buf, _T("%s"), _T("Poetry buffer exceeded."));
830 PoetryError(error_buf);
831 return false;
3013b6f4 832 }
c7bde65d
WS
833 }
834 fclose(data_file);
835 poem_buffer[i-1] = 0;
836 return true;
3013b6f4
JS
837}
838
839// Do the search
840long MainWindow::DoSearch(void)
841{
c7bde65d
WS
842 if (m_searchString.empty())
843 return false;
3013b6f4
JS
844
845 FILE *file;
3baa2662 846 size_t i = 0;
3013b6f4 847 int ch = 0;
6bb934d6 848 wxChar buf[100];
3013b6f4
JS
849 long find_start;
850 long previous_poem_start;
851
17d31882 852 bool found = false;
3baa2662 853 size_t search_length = m_searchString.length();
3013b6f4
JS
854
855 if (same_search)
856 {
c7bde65d
WS
857 find_start = last_find + 1;
858 previous_poem_start = last_poem_start;
3013b6f4
JS
859 }
860 else
861 {
c7bde65d
WS
862 find_start = 0;
863 last_poem_start = 0;
864 previous_poem_start = -1;
3013b6f4
JS
865 }
866
867 if (data_filename)
c7bde65d 868 wxSprintf(buf, _T("%s.dat"), data_filename);
3013b6f4 869
994e41cb 870 file = wxFopen(buf, _T("r"));
6bb934d6 871 if (! (data_filename && file))
3013b6f4 872 {
c7bde65d
WS
873 wxSprintf(error_buf, _T("Poetry data file %s not found\n"), buf);
874 PoetryError(error_buf);
875 return false;
3013b6f4
JS
876 }
877
878 fseek(file, find_start, SEEK_SET);
879
880 while ((ch != EOF) && !found)
881 {
882 ch = getc(file);
17d31882 883 ch = wxTolower(ch); // Make lower case
3013b6f4
JS
884
885 // Only match if we're looking at a different poem
886 // (no point in displaying the same poem again)
c7bde65d 887 if ((ch == m_searchString[i]) && (last_poem_start != previous_poem_start))
3013b6f4 888 {
c7bde65d
WS
889 if (i == 0)
890 last_find = ftell(file);
891 if (i == search_length-1)
892 found = true;
893 i ++;
3013b6f4
JS
894 }
895 else
c7bde65d
WS
896 {
897 i = 0;
898 }
3013b6f4
JS
899
900 if (ch == '#')
901 {
902 ch = getc(file);
c7bde65d 903 last_poem_start = ftell(file);
3013b6f4
JS
904 }
905 }
906 fclose(file);
907 if (ch == EOF)
c7bde65d
WS
908 {
909 last_find = -1;
910 }
3013b6f4
JS
911
912 if (found)
913 {
c7bde65d 914 return last_poem_start;
3013b6f4 915 }
c7bde65d
WS
916
917 return -1;
3013b6f4
JS
918}
919
920// Set up poetry filenames, preferences, load the index
921// Load index (or compile it if none found)
922void TryLoadIndex()
923{
c7bde65d
WS
924 index_ok = (LoadIndex(index_filename) != 0);
925 if (!index_ok || (nitems == 0))
926 {
927 PoetryError(_T("Index file not found; will compile new one"), _T("wxPoem"));
928 index_ok = Compile();
929 }
3013b6f4
JS
930}
931
932// Error message
6bb934d6 933void PoetryError(wxChar *msg, wxChar *caption)
3013b6f4 934{
c7bde65d 935 wxMessageBox(msg, caption, wxOK|wxICON_EXCLAMATION);
3013b6f4
JS
936}
937
938// Notification (change icon to something appropriate!)
6bb934d6 939void PoetryNotify(wxChar *Msg, wxChar *caption)
3013b6f4 940{
c7bde65d 941 wxMessageBox(Msg, caption, wxOK | wxICON_INFORMATION);
3013b6f4
JS
942}
943
944// Build up and save an index into the poetry data file, for
945// rapid random access
946bool Compile(void)
947{
948 FILE *file;
3013b6f4 949 int j;
c4b77901 950 int ch;
6bb934d6 951 wxChar buf[100];
3013b6f4
JS
952
953 if (data_filename)
c7bde65d 954 wxSprintf(buf, _T("%s.dat"), data_filename);
3013b6f4 955
994e41cb 956 file = wxFopen(buf, _T("r"));
6bb934d6 957 if (! (data_filename && file))
3013b6f4 958 {
c7bde65d
WS
959 wxSprintf(error_buf, _T("Poetry data file %s not found\n"), buf);
960 PoetryError(error_buf);
961 return false;
3013b6f4
JS
962 }
963
964 nitems = 0;
965
966 // Do first one (?)
967 poem_index[nitems] = 0;
968 nitems ++;
969
970 // Do rest
254a2129 971
c4b77901 972 do {
3013b6f4 973 ch = getc(file);
3013b6f4
JS
974 if (ch == '#')
975 {
976 ch = getc(file);
977 long data;
978 data = ftell(file);
979 poem_index[nitems] = data;
980 nitems ++;
981 }
c4b77901 982 } while (ch != EOF);
3013b6f4
JS
983 fclose(file);
984
985 if (index_filename)
6bb934d6
JS
986 wxSprintf(buf, _T("%s.idx"), index_filename);
987
994e41cb 988 file = wxFopen(buf, _T("w"));
6bb934d6 989 if (! (data_filename && file))
3013b6f4 990 {
c7bde65d
WS
991 wxSprintf(error_buf, _T("Poetry index file %s cannot be created\n"), buf);
992 PoetryError(error_buf);
993 return false;
3013b6f4
JS
994 }
995
6bb934d6 996 wxFprintf(file, _T("%ld\n\n"), nitems);
3013b6f4 997 for (j = 0; j < nitems; j++)
c7bde65d 998 wxFprintf(file, _T("%ld\n"), poem_index[j]);
f5d01a1c 999
3013b6f4 1000 fclose(file);
6bb934d6 1001 PoetryNotify(_T("Poetry index compiled."));
17d31882 1002 return true;
3013b6f4
JS
1003}
1004
508379ee 1005void MainWindow::OnPopup(wxCommandEvent& event)
3013b6f4 1006{
c7bde65d
WS
1007 switch (event.GetId())
1008 {
1009 case POEM_NEXT:
1010 // Another poem/page
1011 TheMainWindow->NextPage();
1012 break;
1013 case POEM_PREVIOUS:
1014 // Previous page
1015 TheMainWindow->PreviousPage();
1016 break;
1017 case POEM_SEARCH:
1018 // Search - with dialog
1019 TheMainWindow->Search(true);
1020 break;
1021 case POEM_NEXT_MATCH:
1022 // Search - without dialog (next match)
1023 TheMainWindow->Search(false);
1024 break;
1025 case POEM_MINIMIZE:
1026 TheMainWindow->Iconize(true);
1027 break;
17d31882 1028#if wxUSE_CLIPBOARD
c7bde65d
WS
1029 case POEM_COPY:
1030 wxTheClipboard->UsePrimarySelection();
1031 if (wxTheClipboard->Open())
1032 {
1033 static wxString s;
1034 s = poem_buffer;
1035 s.Replace( _T("@P"),wxEmptyString);
1036 s.Replace( _T("@A "),wxEmptyString);
1037 s.Replace( _T("@A"),wxEmptyString);
1038 s.Replace( _T("@T "),wxEmptyString);
1039 s.Replace( _T("@T"),wxEmptyString);
1040 wxTextDataObject *data = new wxTextDataObject( s.c_str() );
1041 if (!wxTheClipboard->SetData( data ))
1042 wxMessageBox(_T("Error while copying to the clipboard."));
1043 }
1044 else
1045 {
1046 wxMessageBox(_T("Error opening the clipboard."));
1047 }
1048 wxTheClipboard->Close();
1049 break;
3013b6f4 1050#endif
c7bde65d
WS
1051 case POEM_BIGGER_TEXT:
1052 pointSize ++;
1053 CreateFonts();
1054 TheMainWindow->Resize();
1055 break;
1056 case POEM_SMALLER_TEXT:
1057 if (pointSize > 2)
1058 {
1059 pointSize --;
1060 CreateFonts();
1061 TheMainWindow->Resize();
1062 }
1063 break;
1064 case POEM_ABOUT:
1065 (void)wxMessageBox(_T("wxPoem Version 1.1\nJulian Smart (c) 1995"),
1066 _T("About wxPoem"), wxOK, TheMainWindow);
1067 break;
1068 case POEM_EXIT:
1069 // Exit
1070 TheMainWindow->Close(true);
1071 break;
1072 default:
1073 break;
1074 }
3013b6f4 1075}