]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/applet/monitorapplet.cpp
fixed typo, used word 'assigned' instead of 'allotted'
[wxWidgets.git] / contrib / samples / applet / monitorapplet.cpp
1 /****************************************************************************
2 *
3 * wxWindows HTML Applet Package
4 *
5 * Copyright (C) 1991-2001 SciTech Software, Inc.
6 * All rights reserved.
7 *
8 * ========================================================================
9 *
10 * The contents of this file are subject to the wxWindows License
11 * Version 3.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.wxwindows.org/licence3.txt
14 *
15 * Software distributed under the License is distributed on an
16 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17 * implied. See the License for the specific language governing
18 * rights and limitations under the License.
19 *
20 * ========================================================================
21 *
22 * Language: ANSI C++
23 * Environment: Any
24 *
25 * Description: Main wxApplet class implementation
26 *
27 ****************************************************************************/
28
29 // For compilers that support precompilation
30 #include "wx/wxprec.h"
31
32 // Include private headers
33 #include "wx/wx.h"
34 #include "monitorapplet.h"
35
36 /*---------------------------- Global variables ---------------------------*/
37
38 // Implement the dynamic class so it can be constructed dynamically
39 IMPLEMENT_DYNAMIC_CLASS(MonitorApplet, wxApplet);
40
41 // Event handler table.
42 BEGIN_EVENT_TABLE(MonitorApplet, wxApplet)
43 EVT_LISTBOX(ID_LISTBOX_MFTR, MonitorApplet::OnChange)
44 EVT_LISTBOX(ID_LISTBOX_MDL, MonitorApplet::OnChange)
45 END_EVENT_TABLE()
46
47 // Include database of known monitors. Normally this would come from a
48 // real database on disk, but for this simple example we hard code all
49 // the values into a table.
50 #include "monitors.c"
51
52 /*------------------------- Implementation --------------------------------*/
53
54 /****************************************************************************
55 REMARKS:
56 Constructor called during dynamic creation. Simple initialise all
57 internal values for the class so that it can be properly created later
58 via the virtual Create member function.
59 ****************************************************************************/
60 MonitorApplet::MonitorApplet()
61 {
62 m_Mfr = NULL;
63 m_Model = NULL;
64 m_Data = NULL;
65 }
66
67 /****************************************************************************
68 REMARKS:
69 Psuedo virtual constructor for the MonitorApplet class.
70 ****************************************************************************/
71 bool MonitorApplet::Create(
72 wxHtmlAppletWindow *parent,
73 const wxSize& size,
74 long style)
75 {
76 bool ret = wxApplet::Create(parent, size, style);
77 if (ret) {
78 // Read our cookie or create it if it does not exist
79 if ((m_Data = (MonitorData*)parent->FindCookie(MONITOR_COOKIE_NAME)) == NULL) {
80 m_Data = new MonitorData;
81 memset(&m_Data->m_Monitor,0,sizeof(m_Data->m_Monitor));
82 parent->RegisterCookie(MONITOR_COOKIE_NAME,m_Data);
83 }
84
85 // Create all the controls and initialise them
86 MonitorDialogFunc(this,true,true);
87 if ((m_Mfr = new ComboBox(this , ID_LISTBOX_MFTR, ID_TEXTCTRL_MFTR)) == NULL)
88 return false;
89 if ((m_Model = new ComboBox(this , ID_LISTBOX_MDL, ID_TEXTCTRL_MDL)) == NULL)
90 return false;
91 ReadMfrList();
92 ReadModelList(true);
93 }
94 return ret;
95 }
96
97 /****************************************************************************
98 REMARKS:
99 Destructor for the MonitorApplet class.
100 ****************************************************************************/
101 MonitorApplet::~MonitorApplet()
102 {
103 delete m_Mfr;
104 delete m_Model;
105 }
106
107 /****************************************************************************
108 REMARKS:
109 Save the current state for the applet to our cookie
110 ****************************************************************************/
111 void MonitorApplet::SaveCurrentState()
112 {
113 // Read currently selected strings into cookie
114 strcpy(m_Data->m_Monitor.m_Mfr,m_Mfr->GetStringSelection());
115 strcpy(m_Data->m_Monitor.m_Model,m_Model->GetStringSelection());
116 }
117
118 /****************************************************************************
119 REMARKS:
120 Handles user navigation away from the applet via an HTML link
121 ****************************************************************************/
122 void MonitorApplet::OnLinkClicked(
123 const wxHtmlLinkInfo&)
124 {
125 SaveCurrentState();
126 }
127
128 /****************************************************************************
129 REMARKS:
130 Handles user navigation away from the applet via the history forward command
131 ****************************************************************************/
132 void MonitorApplet::OnHistoryForward()
133 {
134 SaveCurrentState();
135 }
136
137 /****************************************************************************
138 REMARKS:
139 Handles user navigation away from the applet via the history back command
140 ****************************************************************************/
141 void MonitorApplet::OnHistoryBack()
142 {
143 SaveCurrentState();
144 }
145
146 /****************************************************************************
147 REMARKS:
148 Handles inter applet communication messages
149 ****************************************************************************/
150 void MonitorApplet::OnMessage(
151 wxEvent& msg)
152 {
153 msg.Skip(true);
154 }
155
156 /****************************************************************************
157 REMARKS:
158 ****************************************************************************/
159 void MonitorApplet::OnChange(
160 wxCommandEvent &evt)
161 {
162 if (evt.GetId() == m_Mfr->GetListBoxId()) {
163 m_Mfr->OnChange(evt);
164 ReadModelList(true);
165 }
166 else if (evt.GetId() == m_Model->GetListBoxId()) {
167 m_Model->OnChange(evt);
168 }
169 }
170
171 /****************************************************************************
172 REMARKS:
173 Updates the manufacturer list for the dialog box from the database.
174 ****************************************************************************/
175 void MonitorApplet::ReadMfrList()
176 {
177 char buf[80] = "";
178 int i,selected = 0;
179 MonitorEntry *m;
180
181 m_Mfr->Clear();
182 for (m = m_Monitors,i = 0; m->m_Mfr[0] != 0; m++) {
183 if (wxStricmp(buf,m->m_Mfr) != 0) {
184 m_Mfr->Append(m->m_Mfr);
185 if (wxStricmp(m_Data->m_Monitor.m_Mfr,m->m_Mfr) == 0)
186 selected = i;
187 strcpy(buf,m->m_Mfr);
188 i++;
189 }
190 }
191 m_Mfr->Select(selected);
192 }
193
194 /****************************************************************************
195 REMARKS:
196 Updates the model list for the dialog box for the currently selected
197 manufacturer type.
198 ****************************************************************************/
199 void MonitorApplet::ReadModelList(
200 bool selectCurrent)
201 {
202 int i,selected = 0;
203 MonitorEntry *m;
204 wxString mfrStr;
205
206 mfrStr = m_Mfr->GetStringSelection();
207 m_Model->Clear();
208 for (m = m_Monitors,i = 0; m->m_Mfr[0] != 0; m++) {
209 if (wxStricmp(mfrStr,m->m_Mfr) == 0) {
210 m_Model->Append(m->m_Model);
211 if (selectCurrent && wxStricmp(m_Data->m_Monitor.m_Model,m->m_Model) == 0)
212 selected = i;
213 i++;
214 }
215 }
216 m_Model->Select(selected);
217 }
218