]> git.saurik.com Git - wxWidgets.git/blobdiff - demos/poem/wxpoem.cpp
use MSWAlwaysDrawBg() to fix the problem with black background; rewrote/cleaned up...
[wxWidgets.git] / demos / poem / wxpoem.cpp
index 8a883d1543c553377899174921399a0418f45799..1ac305abe921ce5ebbb4f6b06469b1f1f1cf573c 100644 (file)
@@ -29,8 +29,6 @@
 #include "wx/wx.h"
 #endif
 
-#include "wx/help.h"
-
 #include "wxpoem.h"
 
 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXX11__)
 #include "corner2.xpm"
 #include "corner3.xpm"
 #include "corner4.xpm"
-#endif
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-
-#ifdef __WINDOWS__
-#include <windows.h>
-#ifdef DrawText
-#undef DrawText
-#endif
+#include "wxpoem.xpm"
 #endif
 
 #define         buf_size 10000
 #define         X_SIZE 30
 #define         Y_SIZE 20
 
-static char     *poem_buffer;          // Storage for each poem
-static char     line[150];                      // Storage for a line
-static char     title[150];                     // Remember the title
-static char     *search_string = NULL;          // The search string
+static wxChar   *poem_buffer;          // Storage for each poem
+static wxChar   line[150];                      // Storage for a line
+static wxChar   title[150];                     // Remember the title
+static wxChar   *search_string = NULL;          // The search string
 static int      pages[30];                      // For multipage poems -
                                                 // 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
-static bool     search_ok = FALSE;              // Search was successful
-static bool     same_search = FALSE;            // Searching on same string
+static bool     search_ok = false;              // Search was successful
+static bool     same_search = false;            // Searching on same string
 
 static long     poem_index[600];                     // Index of poem starts
 static long     nitems = 0;                     // Number of poems
@@ -88,13 +75,13 @@ static int      XPos;                           // Startup X position
 static int      YPos;                           // Startup Y position
 static int      pointSize = 12;                 // Font size
 
-static char     *index_filename = NULL;            // Index filename
-static char     *data_filename = NULL;             // Data filename
-static char     error_buf[300];                 // Error message buffer
-static bool     loaded_ok = FALSE;              // Poem loaded ok
-static bool     index_ok = FALSE;               // Index loaded ok
+static wxChar   *index_filename = NULL;            // Index filename
+static wxChar   *data_filename = NULL;             // Data filename
+static wxChar   error_buf[300];                 // Error message buffer
+static bool     loaded_ok = false;              // Poem loaded ok
+static bool     index_ok = false;               // Index loaded ok
 
-static bool     paging = FALSE;                 // Are we paging?
+static bool     paging = false;                 // Are we paging?
 static int      current_page = 0;               // Currently viewed page
 
 wxIcon          *Corner1 = NULL;
@@ -115,26 +102,26 @@ wxPen           *WhitePen = NULL;
 // Backing bitmap
 wxBitmap        *backingBitmap = NULL;
 
-void            PoetryError(char *, char *caption="wxPoem Error");
-void            PoetryNotify(char *Msg, char *caption="wxPoem");
+void            PoetryError(wxChar *, wxChar *caption=_T("wxPoem Error"));
+void            PoetryNotify(wxChar *Msg, wxChar *caption=_T("wxPoem"));
 void            TryLoadIndex();
-bool            LoadPoem(char *, long);
+bool            LoadPoem(wxChar *, long);
 int             GetIndex();
-int             LoadIndex(char *);
+int             LoadIndex(wxChar *);
 bool            Compile(void);
 void            WritePreferences();
 void            ReadPreferences();
 void            FindMax(int *max_thing, int thing);
 void            CreateFonts();
-#ifdef __WXMSW__
-void            CopyToClipboard(HWND, char *);
-#endif
 
-wxMenu    *popupMenu = NULL;
+#if wxUSE_CLIPBOARD
+    #include "wx/dataobj.h"
+    #include "wx/clipbrd.h"
+#endif
 
-#if wxUSE_HELP
-    wxHelpController *HelpController = NULL;
-#endif // wxUSE_HELP
+#ifdef __WXWINCE__
+    STDAPI_(__int64) CeGetRandomSeed();
+#endif
 
 IMPLEMENT_APP(MyApp)
 
@@ -151,7 +138,7 @@ void CreateFonts()
 BEGIN_EVENT_TABLE(MainWindow, wxFrame)
     EVT_CLOSE(MainWindow::OnCloseWindow)
     EVT_CHAR(MainWindow::OnChar)
-    EVT_MENU(-1, MainWindow::OnPopup)
+    EVT_MENU(wxID_ANY, MainWindow::OnPopup)
 END_EVENT_TABLE()
 
 MainWindow::MainWindow(wxFrame *frame, wxWindowID id, const wxString& title,
@@ -160,14 +147,6 @@ MainWindow::MainWindow(wxFrame *frame, wxWindowID id, const wxString& title,
 {
 }
 
-MainWindow::~MainWindow()
-{
-  // 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 popupMenu;
-  popupMenu = NULL;
-}
-
 // Read the poetry buffer, either for finding the size
 // or for writing to a bitmap (not to the window directly,
 // since that displays messily)
@@ -177,12 +156,11 @@ void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
 {
     int i = pages[current_page];
     int ch = -1;
-    int x = 10;
     int y = 0;
     int j;
-    char *line_ptr;
+    wxChar *line_ptr;
     int curr_width = 0;
-    bool page_break = FALSE;
+    bool page_break = false;
 
     int width = 0;
     int height = 0;
@@ -206,7 +184,7 @@ void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
     dc->SetFont(* NormalFont);
     long xx;
     long yy;
-    dc->GetTextExtent("X", &xx, &yy);
+    dc->GetTextExtent(_T("X"), &xx, &yy);
     char_height = (int)yy;
 
     if (current_page == 0)
@@ -219,7 +197,7 @@ void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
 
        if (DrawIt)
        {
-         x = (width - xx)/2;
+         int x = (width - xx)/2;
          dc->SetFont(* BoldFont);
 
          // Change text to BLACK!
@@ -242,7 +220,7 @@ void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
         while (((ch = poem_buffer[i]) != 10) && (ch != 0))
 #endif
         {
-            line[j] = ch;
+            line[j] = (wxChar)ch;
             j ++;
             i ++;
         }
@@ -276,23 +254,23 @@ void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
             switch (line[1])
             {
               case 'P':
-                paging = TRUE;
-                page_break = TRUE;
+                paging = true;
+                page_break = true;
                 break;
 
               case 'T':
                 dc->SetFont(* BoldFont);
                 line_ptr = line+3;
 
-                strcpy(title, line_ptr);
-                strcat(title, " (cont'd)");
+                wxStrcpy(title, line_ptr);
+                wxStrcat(title, _T(" (cont'd)"));
 
                 dc->GetTextExtent(line_ptr, &xx, &yy);
                 FindMax(&curr_width, (int)xx);
 
                 if (DrawIt)
                 {
-                  x = (width - xx)/2;
+                  int x = (width - xx)/2;
                   dc->SetFont(* BoldFont);
 
                   // Change text to BLACK!
@@ -315,7 +293,7 @@ void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
 
                 if (DrawIt)
                 {
-                  x = (width - xx)/2;
+                  int x = (width - xx)/2;
                   dc->SetTextForeground(* wxBLACK);
                   dc->DrawText(line_ptr, x, y);
                 }
@@ -348,7 +326,7 @@ void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
     // Write (cont'd)
     if (page_break)
     {
-       char *cont = "(cont'd)";
+       wxChar *cont = _T("(cont'd)");
 
        dc->SetFont(* NormalFont);
 
@@ -370,7 +348,7 @@ void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
     if (page_break)
       pages[current_page+1] = i;
     else
-      paging = FALSE;
+      paging = false;
 
     if (DrawIt)
     {
@@ -450,7 +428,7 @@ void MainWindow::Resize(void)
     wxClientDC dc(canvas);
 
     // Get the poem size
-    ScanBuffer(& dc, FALSE, &poem_width, &poem_height);
+    ScanBuffer(& dc, false, &poem_width, &poem_height);
     int x = poem_width + (2*BORDER_SIZE);
     int y = poem_height + (2*BORDER_SIZE);
 
@@ -466,7 +444,7 @@ void MainWindow::Resize(void)
     memDC.SelectObject(* backingBitmap);
 
     memDC.Clear();
-    TheMainWindow->ScanBuffer(&memDC, TRUE, &xx, &yy);
+    TheMainWindow->ScanBuffer(&memDC, true, &xx, &yy);
 }
 
 // Which is more?
@@ -506,18 +484,19 @@ void MainWindow::Search(bool ask)
 
   if (ask || !search_string)
   {
-    wxString s = wxGetTextFromUser("Enter search string", "Search", (const char*) search_string);
-    if (s != "")
+    wxString s = wxGetTextFromUser( _T("Enter search string"), _T("Search"), (const wxChar*) search_string);
+    if (s != wxEmptyString)
     {
+      s.MakeLower();
       if (search_string) delete[] search_string;
-      search_string = copystring(s);
-      search_ok = TRUE;
-    } else search_ok = FALSE;
+      search_string = wxStrcpy(new wxChar[wxStrlen(s.c_str()) + 1], s.c_str());
+      search_ok = true;
+    } else search_ok = false;
   }
   else
   {
-    same_search = TRUE;
-    search_ok = TRUE;
+    same_search = true;
+    search_ok = true;
   }
 
   if (search_string && search_ok)
@@ -531,124 +510,65 @@ void MainWindow::Search(bool ask)
     else
     {
       last_poem_start = 0;
-      PoetryNotify("Search string not found.");
-    }
-  }
-}
-
-// Copy a string to the clipboard
-#ifdef __WXMSW__
-void CopyToClipboard(HWND handle, char *s)
-{
-  int length = strlen(s);
-  HANDLE hGlobalMemory = GlobalAlloc(GHND, (DWORD) length + 1);
-  if (hGlobalMemory)
-  {
-#ifdef __WINDOWS_386__
-    LPSTR lpGlobalMemory = MK_FP32(GlobalLock(hGlobalMemory));
-#else
-    LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory);
-#endif
-    int i, j = 0;
-    for (i = 0; i < length; i ++)
-    {
-      if (s[i] == '@')
-      {
-        i++;
-        switch (s[i])
-        {
-          case 'P':
-            break;
-          case 'T':
-          case 'A':
-          default:
-            i ++;
-            break;
-        }
-      }
-      else
-      {
-        lpGlobalMemory[j] = s[i];
-        j ++;
-      }
+      PoetryNotify(_T("Search string not found."));
     }
-
-    GlobalUnlock(hGlobalMemory);
-    OpenClipboard(handle);
-    EmptyClipboard();
-    SetClipboardData(CF_TEXT, hGlobalMemory);
-    CloseClipboard();
   }
 }
-#endif
 
 bool MyApp::OnInit()
 {
-  poem_buffer = new char[buf_size];
-
-  GreyPen = new wxPen("LIGHT GREY", THICK_LINE_WIDTH, wxSOLID);
-  DarkGreyPen = new wxPen("GREY", THICK_LINE_WIDTH, wxSOLID);
-  WhitePen = new wxPen("WHITE", THICK_LINE_WIDTH, wxSOLID);
+  poem_buffer = new wxChar[buf_size];
 
-#if wxUSE_HELP
-  HelpController = new wxHelpController();
-  HelpController->Initialize("wxpoem");
-#endif // wxUSE_HELP
+  GreyPen = new wxPen(_T("LIGHT GREY"), THICK_LINE_WIDTH, wxSOLID);
+  DarkGreyPen = new wxPen(_T("GREY"), THICK_LINE_WIDTH, wxSOLID);
+  WhitePen = new wxPen(_T("WHITE"), THICK_LINE_WIDTH, wxSOLID);
 
   CreateFonts();
 
   ReadPreferences();
 
   // Seed the random number generator
+#ifdef __WXWINCE__
+  srand((unsigned) CeGetRandomSeed());
+#else
   time_t current_time;
 
   (void)time(&current_time);
   srand((unsigned int)current_time);
+#endif
 
 //    randomize();
   pages[0] = 0;
 
-  TheMainWindow = new MainWindow(NULL, 500, "wxPoem", wxPoint(XPos, YPos), wxSize(100, 100), wxCAPTION|wxMINIMIZE_BOX|wxSYSTEM_MENU);
+  TheMainWindow = new MainWindow(NULL,
+                                 wxID_ANY,
+                                 _T("wxPoem"),
+                                 wxPoint(XPos, YPos),
+                                 wxDefaultSize,
+                                 wxCAPTION|wxMINIMIZE_BOX|wxSYSTEM_MENU|wxCLOSE_BOX|wxFULL_REPAINT_ON_RESIZE
+                                 );
 
-#ifdef wx_x
-  TheMainWindow->SetIcon(Icon("wxpoem"));
-#endif
+  TheMainWindow->SetIcon(wxICON(wxpoem));
 
   TheMainWindow->canvas = new MyCanvas(TheMainWindow, 501, wxDefaultPosition, wxDefaultSize);
 
-  popupMenu = new wxMenu;
-  popupMenu->Append(POEM_NEXT, "Next poem/page");
-  popupMenu->Append(POEM_PREVIOUS, "Previous page");
-  popupMenu->AppendSeparator();
-  popupMenu->Append(POEM_SEARCH, "Search");
-  popupMenu->Append(POEM_NEXT_MATCH, "Next match");
-  popupMenu->Append(POEM_COPY, "Copy to clipboard");
-  popupMenu->Append(POEM_MINIMIZE, "Minimize");
-  popupMenu->AppendSeparator();
-  popupMenu->Append(POEM_BIGGER_TEXT, "Bigger text");
-  popupMenu->Append(POEM_SMALLER_TEXT, "Smaller text");
-  popupMenu->AppendSeparator();
-  popupMenu->Append(POEM_ABOUT, "About wxPoem");
-  popupMenu->AppendSeparator();
-  popupMenu->Append(POEM_EXIT, "Exit");
-
   if (argc > 1)
   {
-    index_filename = copystring(argv[1]);
-    data_filename = copystring(argv[1]);
+    index_filename = wxStrcpy(new wxChar[wxStrlen(argv[1]) + 1], argv[1]);
+    data_filename = wxStrcpy(new wxChar[wxStrlen(argv[1]) + 1], argv[1]);
   }
   else
   {
-    index_filename = DEFAULT_POETRY_IND;
-    data_filename = DEFAULT_POETRY_DAT;
+    index_filename = _T(DEFAULT_POETRY_IND);
+    data_filename = _T(DEFAULT_POETRY_DAT);
   }
   TryLoadIndex();
 
 #ifdef __WXMSW__
-  Corner1 = new wxIcon("icon_1");
-  Corner2 = new wxIcon("icon_2");
-  Corner3 = new wxIcon("icon_3");
-  Corner4 = new wxIcon("icon_4");
+  Corner1 = new wxIcon(_T("icon_1"));
+  Corner2 = new wxIcon(_T("icon_2"));
+  Corner3 = new wxIcon(_T("icon_3"));
+  Corner4 = new wxIcon(_T("icon_4"));
 #endif
 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXX11__)
   Corner1 = new wxIcon( corner1_xpm );
@@ -659,18 +579,15 @@ bool MyApp::OnInit()
 
   TheMainWindow->GetIndexLoadPoem();
   TheMainWindow->Resize();
-  TheMainWindow->Show(TRUE);
+  TheMainWindow->Show(true);
 
-  return TRUE;
+  return true;
 }
 
 int MyApp::OnExit()
 {
   if (backingBitmap)
     delete backingBitmap;
-#if wxUSE_HELP
-  delete HelpController;
-#endif // wxUSE_HELP
   delete GreyPen;
   delete DarkGreyPen;
   delete WhitePen;
@@ -680,13 +597,6 @@ int MyApp::OnExit()
   delete Corner3;
   delete Corner4;
 
-  // Causes crash since they're deleted by the global font list
-#if 0
-  delete NormalFont;
-  delete BoldFont;
-  delete ItalicFont;
-#endif
-
   delete[] poem_buffer;
   if (search_string)
     delete[] search_string;
@@ -715,6 +625,29 @@ END_EVENT_TABLE()
 MyCanvas::MyCanvas(wxFrame *frame, wxWindowID id, const wxPoint& pos, const wxSize& size):
  wxWindow(frame, id, pos, size)
 {
+  popupMenu = new wxMenu;
+  popupMenu->Append(POEM_NEXT, _T("Next poem/page"));
+  popupMenu->Append(POEM_PREVIOUS, _T("Previous page"));
+  popupMenu->AppendSeparator();
+  popupMenu->Append(POEM_SEARCH, _T("Search"));
+  popupMenu->Append(POEM_NEXT_MATCH, _T("Next match"));
+  popupMenu->Append(POEM_COPY, _T("Copy to clipboard"));
+  popupMenu->Append(POEM_MINIMIZE, _T("Minimize"));
+  popupMenu->AppendSeparator();
+  popupMenu->Append(POEM_BIGGER_TEXT, _T("Bigger text"));
+  popupMenu->Append(POEM_SMALLER_TEXT, _T("Smaller text"));
+  popupMenu->AppendSeparator();
+  popupMenu->Append(POEM_ABOUT, _T("About wxPoem"));
+  popupMenu->AppendSeparator();
+  popupMenu->Append(POEM_EXIT, _T("Exit"));
+}
+
+MyCanvas::~MyCanvas()
+{
+  // 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 popupMenu;
+  popupMenu = NULL;
 }
 
 // Define the repainting behaviour
@@ -728,7 +661,7 @@ void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
         TheMainWindow->GetClientSize(&xx, &yy);
 
         dc.DrawBitmap(* backingBitmap, 0, 0);
-#if 0        
+#if 0
         wxMemoryDC memDC;
         memDC.SelectObject(* backingBitmap);
         dc.Blit(0, 0, backingBitmap->GetWidth(), backingBitmap->GetHeight(), &memDC, 0, 0);
@@ -759,7 +692,9 @@ void MyCanvas::OnMouseEvent(wxMouseEvent& event)
     GetParent()->GetPosition(&startFrameX, &startFrameY);
   }
   else if (event.LeftUp())
-    this->ReleaseMouse();
+  {
+    if (GetCapture() == this) this->ReleaseMouse();
+  }
   else if (event.Dragging() && event.LeftIsDown())
   {
     int x1 = (int)x;
@@ -775,66 +710,68 @@ void MyCanvas::OnMouseEvent(wxMouseEvent& event)
 // Process characters
 void MyCanvas::OnChar(wxKeyEvent& event)
 {
-  switch (event.KeyCode())
+  switch (event.GetKeyCode())
   {
     case 'n':
     case 'N':
       // Next match
-      TheMainWindow->Search(FALSE);
+      TheMainWindow->Search(false);
       break;
     case 's':
     case 'S':
       // New search
-      TheMainWindow->Search(TRUE);
+      TheMainWindow->Search(true);
       break;
     case WXK_SPACE:
+    case WXK_RIGHT:
+    case WXK_DOWN:
       // Another poem
       TheMainWindow->NextPage();
       break;
-    case 27:
-      TheMainWindow->Close(TRUE);
+    case WXK_ESCAPE:
+      TheMainWindow->Close(true);
     default:
        break;
    }
  }
 
 // Load index file
-int LoadIndex(char *file_name)
+int LoadIndex(wxChar *file_name)
 {
     long data;
     FILE *index_file;
 
-    int i = 0;
-    char buf[100];
+    wxChar buf[100];
 
-    if (file_name)
-      sprintf(buf, "%s.idx", file_name);
-    if (! (file_name && (index_file = fopen(buf, "r"))))
+    if (file_name == NULL)
+      return 0;
+
+    wxSprintf(buf, _T("%s.idx"), file_name);
+
+    index_file = wxFopen(buf, _T("r"));
+    if (index_file == NULL)
       return 0;
-    else
-    {
-      fscanf(index_file, "%ld", &nitems);
 
-      for (i = 0; i < nitems; i++)
+    wxFscanf(index_file, _T("%ld"), &nitems);
+
+      for (int i = 0; i < nitems; i++)
       {
-        fscanf(index_file, "%ld", &data);
+      wxFscanf(index_file, _T("%ld"), &data);
         poem_index[i] = data;
       }
+
       fclose(index_file);
 
       return 1;
-    }
 }
 
 // Get index
 int GetIndex()
 {
-    int indexn = 0;
-
-    indexn = (int)(rand() % nitems);
+    int indexn = (int)(rand() % nitems);
 
     if ((indexn < 0) || (indexn > nitems))
-    { PoetryError("No such poem!");
+    { PoetryError(_T("No such poem!"));
       return -1;
     }
     else
@@ -844,9 +781,11 @@ int GetIndex()
 // Read preferences
 void ReadPreferences()
 {
-  wxGetResource("wxPoem", "FontSize", &pointSize);
-  wxGetResource("wxPoem", "X", &XPos);
-  wxGetResource("wxPoem", "Y", &YPos);
+#if wxUSE_RESOURCES
+  wxGetResource(_T("wxPoem"), _T("FontSize"), &pointSize);
+  wxGetResource(_T("wxPoem"), _T("X"), &XPos);
+  wxGetResource(_T("wxPoem"), _T("Y"), &YPos);
+#endif
 }
 
 // Write preferences to disk
@@ -854,39 +793,45 @@ void WritePreferences()
 {
 #ifdef __WXMSW__
   TheMainWindow->GetPosition(&XPos, &YPos);
-  wxWriteResource("wxPoem", "FontSize", pointSize);
-  wxWriteResource("wxPoem", "X", XPos);
-  wxWriteResource("wxPoem", "Y", YPos);
+#if wxUSE_RESOURCES
+  wxWriteResource(_T("wxPoem"), _T("FontSize"), pointSize);
+  wxWriteResource(_T("wxPoem"), _T("X"), XPos);
+  wxWriteResource(_T("wxPoem"), _T("Y"), YPos);
+#endif
 #endif
 }
 
 // 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.
-bool LoadPoem(char *file_name, long position)
+bool LoadPoem(wxChar *file_name, long position)
 {
-    int ch = 0;
-    int i = 0;
 //    int j = 0;
 //    int indexn = 0;
-    char buf[100];
+    wxChar buf[100];
     long data;
     FILE *data_file;
 
-    paging = FALSE;
+    paging = false;
     current_page = 0;
 
-    if (file_name)
-      sprintf(buf, "%s.dat", file_name);
-
-    if (! (file_name && (data_file = fopen(buf, "r"))))
+    if (file_name == NULL)
     {
-      sprintf(error_buf, "Data file %s not found.", buf);
+      wxSprintf(error_buf, _T("Error in Poem loading."));
       PoetryError(error_buf);
-      return FALSE;
+      return false;
     }
-    else
+
+    wxSprintf(buf, _T("%s.dat"), file_name);
+    data_file = wxFopen(buf, _T("r"));
+
+    if (data_file == NULL)
     {
+      wxSprintf(error_buf, _T("Data file %s not found."), buf);
+      PoetryError(error_buf);
+      return false;
+    }
+
       if (position > -1)
         data = position;
       else
@@ -894,8 +839,8 @@ bool LoadPoem(char *file_name, long position)
 
       fseek(data_file, data, SEEK_SET);
 
-      ch = 0;
-      i = 0;
+      int ch = 0;
+      int i = 0;
       while ((ch != EOF) && (ch != '#'))
       {
         ch = getc(data_file);
@@ -906,37 +851,36 @@ bool LoadPoem(char *file_name, long position)
           i++;
         }
 
-        poem_buffer[i] = ch;
+        poem_buffer[i] = (wxChar)ch;
         i ++;
 
         if (i == buf_size)
         {
-           sprintf(error_buf, "%s", "Poetry buffer exceeded.");
+        wxSprintf(error_buf, _T("%s"), _T("Poetry buffer exceeded."));
            PoetryError(error_buf);
-           return FALSE;
+           return false;
         }
       }
       fclose(data_file);
       poem_buffer[i-1] = 0;
-      return TRUE;
-  }
+      return true;
 }
 
 // Do the search
 long MainWindow::DoSearch(void)
 {
     if (!search_string)
-      return FALSE;
+      return false;
 
     FILE *file;
     long i = 0;
     int ch = 0;
-    char buf[100];
+    wxChar buf[100];
     long find_start;
     long previous_poem_start;
 
-    bool found = FALSE;
-    int search_length = strlen(search_string);
+    bool found = false;
+    int search_length = wxStrlen(search_string);
 
     if (same_search)
     {
@@ -951,13 +895,14 @@ long MainWindow::DoSearch(void)
     }
 
     if (data_filename)
-      sprintf(buf, "%s.dat", data_filename);
+      wxSprintf(buf, _T("%s.dat"), data_filename);
 
-    if (! (data_filename && (file = fopen(buf, "r"))))
+    file = wxFopen(buf, _T("r"));
+    if (! (data_filename && file))
     {
-      sprintf(error_buf, "Poetry data file %s not found\n", buf);
+      wxSprintf(error_buf, _T("Poetry data file %s not found\n"), buf);
       PoetryError(error_buf);
-      return FALSE;
+      return false;
     }
 
     fseek(file, find_start, SEEK_SET);
@@ -965,7 +910,7 @@ long MainWindow::DoSearch(void)
     while ((ch != EOF) && !found)
     {
         ch = getc(file);
-        ch |= 0x0020;   // Make lower case
+        ch = wxTolower(ch);   // Make lower case
 
         // Only match if we're looking at a different poem
         // (no point in displaying the same poem again)
@@ -974,7 +919,7 @@ long MainWindow::DoSearch(void)
           if (i == 0)
             last_find = ftell(file);
           if (i == search_length-1)
-            found = TRUE;
+            found = true;
           i ++;
         }
         else
@@ -1005,19 +950,19 @@ void TryLoadIndex()
   index_ok = (LoadIndex(index_filename) != 0);
   if (!index_ok || (nitems == 0))
   {
-      PoetryError("Index file not found; will compile new one", "wxPoem");
+      PoetryError(_T("Index file not found; will compile new one"), _T("wxPoem"));
       index_ok = Compile();
   }
 }
 
 // Error message
-void PoetryError(char *msg, char *caption)
+void PoetryError(wxChar *msg, wxChar *caption)
 {
   wxMessageBox(msg, caption, wxOK|wxICON_EXCLAMATION);
 }
 
 // Notification (change icon to something appropriate!)
-void PoetryNotify(char *Msg, char *caption)
+void PoetryNotify(wxChar *Msg, wxChar *caption)
 {
   wxMessageBox(Msg, caption, wxOK | wxICON_INFORMATION);
 }
@@ -1027,19 +972,19 @@ void PoetryNotify(char *Msg, char *caption)
 bool Compile(void)
 {
     FILE *file;
-    long i = 0;
     int j;
-    int ch = 0;
-    char buf[100];
+    int ch;
+    wxChar buf[100];
 
     if (data_filename)
-      sprintf(buf, "%s.dat", data_filename);
+      wxSprintf(buf, _T("%s.dat"), data_filename);
 
-    if (! (data_filename && (file = fopen(buf, "r"))))
+    file = wxFopen(buf, _T("r"));
+    if (! (data_filename && file))
     {
-      sprintf(error_buf, "Poetry data file %s not found\n", buf);
+      wxSprintf(error_buf, _T("Poetry data file %s not found\n"), buf);
       PoetryError(error_buf);
-      return FALSE;
+      return false;
     }
 
     nitems = 0;
@@ -1049,10 +994,9 @@ bool Compile(void)
     nitems ++;
 
     // Do rest
-    while (ch != EOF)
-    {
+
+    do {
         ch = getc(file);
-        i ++;
         if (ch == '#')
         {
             ch = getc(file);
@@ -1061,25 +1005,27 @@ bool Compile(void)
             poem_index[nitems] = data;
             nitems ++;
         }
-    }
+    } while (ch != EOF);
     fclose(file);
 
     if (index_filename)
-      sprintf(buf, "%s.idx", index_filename);
-    if (! (data_filename && (file = fopen(buf, "w"))))
+      wxSprintf(buf, _T("%s.idx"), index_filename);
+
+    file = wxFopen(buf, _T("w"));
+    if (! (data_filename && file))
     {
-      sprintf(error_buf, "Poetry index file %s cannot be created\n", buf);
+      wxSprintf(error_buf, _T("Poetry index file %s cannot be created\n"), buf);
       PoetryError(error_buf);
-      return FALSE;
+      return false;
     }
 
-    fprintf(file, "%ld\n\n", nitems);
+    wxFprintf(file, _T("%ld\n\n"), nitems);
     for (j = 0; j < nitems; j++)
-      fprintf(file, "%ld\n", poem_index[j]);
+      wxFprintf(file, _T("%ld\n"), poem_index[j]);
 
     fclose(file);
-    PoetryNotify("Poetry index compiled.");
-    return TRUE;
+    PoetryNotify(_T("Poetry index compiled."));
+    return true;
 }
 
 void MainWindow::OnPopup(wxCommandEvent& event)
@@ -1096,19 +1042,36 @@ void MainWindow::OnPopup(wxCommandEvent& event)
        break;
      case POEM_SEARCH:
        // Search - with dialog
-       TheMainWindow->Search(TRUE);
+       TheMainWindow->Search(true);
        break;
      case POEM_NEXT_MATCH:
        // Search - without dialog (next match)
-       TheMainWindow->Search(FALSE);
+       TheMainWindow->Search(false);
        break;
      case POEM_MINIMIZE:
-       TheMainWindow->Iconize(TRUE);
+       TheMainWindow->Iconize(true);
        break;
-#ifdef __WXMSW__
+#if wxUSE_CLIPBOARD
      case POEM_COPY:
-       // Copy current poem to the clipboard
-       CopyToClipboard((HWND) TheMainWindow->GetHWND(), poem_buffer);
+       wxTheClipboard->UsePrimarySelection();
+       if (wxTheClipboard->Open())
+       {
+         static wxString s;
+         s = poem_buffer;
+         s.Replace( _T("@P"),_T(""));
+         s.Replace( _T("@A "),_T(""));
+         s.Replace( _T("@A"),_T(""));
+         s.Replace( _T("@T "),_T(""));
+         s.Replace( _T("@T"),_T(""));
+         wxTextDataObject *data = new wxTextDataObject( s.c_str() );
+         if (!wxTheClipboard->SetData( data ))
+           wxMessageBox(_T("Error while copying to the clipboard."));
+       }
+       else
+       {
+         wxMessageBox(_T("Error opening the clipboard."));
+       }
+       wxTheClipboard->Close();
        break;
 #endif
      case POEM_COMPILE:
@@ -1132,23 +1095,15 @@ void MainWindow::OnPopup(wxCommandEvent& event)
        }
        break;
      }
-     case POEM_HELP_CONTENTS:
-     {
-#if wxUSE_HELP
-       HelpController->LoadFile("wxpoem");
-       HelpController->DisplayContents();
-#endif // wxUSE_HELP
-       break;
-     }
      case POEM_ABOUT:
      {
-       (void)wxMessageBox("wxPoem Version 1.1\nJulian Smart (c) 1995",
-                          "About wxPoem", wxOK, TheMainWindow);
+       (void)wxMessageBox(_T("wxPoem Version 1.1\nJulian Smart (c) 1995"),
+                          _T("About wxPoem"), wxOK, TheMainWindow);
        break;
      }
      case POEM_EXIT:
        // Exit
-       TheMainWindow->Close(TRUE);
+       TheMainWindow->Close(true);
        break;
      default:
        break;