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