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