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