]> git.saurik.com Git - wxWidgets.git/blob - utils/nplugin/samples/simple/simple.cpp
SetTextColor --> SetTextColour, to be consistent with the rest of the lib
[wxWidgets.git] / utils / nplugin / samples / simple / simple.cpp
1 /*
2 * File: simple.cpp
3 * Purpose: Minimal wxWidgets plugin
4 * Author: Julian Smart
5 * Created: 1997
6 * Updated:
7 * Copyright: (c) Julian Smart
8 */
9
10 /* static const char sccsid[] = "%W% %G%"; */
11
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #ifndef WX_PRECOMP
19 #include "wx/wx.h"
20 #endif
21
22 #include <fstream.h>
23
24 #include "NPApp.h"
25 #include "NPFrame.h"
26
27 // Define a new application type
28 class MyApp: public wxPluginApp
29 { public:
30 virtual bool OnInit(void);
31 virtual wxPluginFrame* OnNewInstance(const wxPluginData& data);
32 };
33
34 // Define a new frame type
35 class MyFrame: public wxPluginFrame
36 { public:
37 MyFrame(const wxPluginData& data);
38
39 public:
40 // Let's paint directly onto the 'frame'; we don't need a subwindow
41 void OnPaint(wxPaintEvent& event);
42 void OnDraw(wxDC& dc);
43 void OnMouseEvent(wxMouseEvent& event);
44
45 // Called when the file has been downloaded
46 virtual void OnNPNewFile(NPStream *stream, const wxString& fname);
47
48 void CentreStrings(wxDC& dc);
49
50 DECLARE_EVENT_TABLE()
51
52 protected:
53 wxStringList m_strings;
54 long m_xpos;
55 long m_ypos;
56 };
57
58 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
59 EVT_SIZE(MyFrame::OnSize)
60 EVT_PAINT(MyFrame::OnPaint)
61 EVT_MOUSE_EVENTS(MyFrame::OnMouseEvent)
62 END_EVENT_TABLE()
63
64 IMPLEMENT_APP(MyApp)
65
66 // No app initialisation necessary, and for a plugin there is no
67 // top frame.
68 bool MyApp::OnInit(void)
69 {
70 return TRUE;
71 }
72
73 // Called whenever a new plugin instance is called. We could check
74 // various things here in 'data' but we won't bother.
75 wxPluginFrame* MyApp::OnNewInstance(const wxPluginData& data)
76 {
77 // Implicitly added to list of plugin frames
78 return new MyFrame(data);
79 }
80
81 // My frame constructor
82 MyFrame::MyFrame(const wxPluginData& data):
83 wxPluginFrame(data)
84 {
85 m_xpos = -1;
86 m_ypos = -1;
87 }
88
89 void MyFrame::OnPaint(wxPaintEvent& event)
90 {
91 wxPaintDC dc(this);
92
93 OnDraw(dc);
94 }
95
96 void MyFrame::OnDraw(wxDC& dc)
97 {
98 dc.SetBrush(*wxCYAN_BRUSH);
99 dc.SetPen(*wxRED_PEN);
100
101 int w, h;
102 GetClientSize(&w, &h);
103
104 dc.DrawRectangle(0, 0, w, h);
105
106 wxFont swissFont(10, wxSWISS, wxNORMAL, wxNORMAL);
107 dc.SetFont(swissFont);
108 dc.SetBackgroundMode(wxTRANSPARENT);
109
110 CentreStrings(dc);
111 }
112
113 // Called when the file has been downloaded
114 void MyFrame::OnNPNewFile(NPStream *stream, const wxString& fname)
115 {
116 ifstream str(fname);
117 char buf[201];
118
119 while ( !str.eof() )
120 {
121 buf[0] = 0;
122 str.getline(buf, 200);
123
124 if ( buf[0] != 0 )
125 m_strings.Add(buf);
126 }
127 Refresh();
128 }
129
130 void MyFrame::CentreStrings(wxDC& dc)
131 {
132 int y = 5;
133 int cw, ch;
134 GetClientSize(&cw, &ch);
135
136 wxNode *node = m_strings.First();
137 while ( node )
138 {
139 char *s = (char *)node->Data();
140 long w, h;
141 dc.GetTextExtent(s, &w, &h);
142
143 int x = wxMax(0, (cw - w)/2);
144 dc.DrawText(s, x, y);
145
146 y += h + (h/2);
147
148 node = node->Next();
149 }
150 }
151
152 // This implements a tiny doodling program. Drag the mouse using
153 // the left button.
154 void MyFrame::OnMouseEvent(wxMouseEvent& event)
155 {
156 long x, y;
157 event.Position(&x, &y);
158 wxClientDC dc(this);
159
160 if (m_xpos > -1 && m_ypos > -1 && event.Dragging() && event.LeftIsDown())
161 {
162 dc.SetPen(wxBLACK_PEN);
163 dc.SetBrush(wxTRANSPARENT_BRUSH);
164 dc.DrawLine(m_xpos, m_ypos, x, y);
165 }
166 m_xpos = x;
167 m_ypos = y;
168 }
169