summaryrefslogtreecommitdiff
path: root/support/dktools/Dk4WxFrame.cpt
blob: ad5ed4e6d8187543579324aaff4c1738a2afeb40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2015-xxxx
license		=	bsd


%%	header

/**	@file	Dk4WxFrame.h	Base class for top level frames.
*/

#ifndef DK4CONF_H_INCLUDED
#include "dk4conf.h"
#endif

#ifndef DK4TYPES_H_INCLUDED
#include "dk4types.h"
#endif

#ifndef WX_WXPREC_H_INCLUDED
#include <wx/wxprec.h>
#define	WX_WXPREC_H_INCLUDED 1
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#ifndef WX_WX_H_INCLUDED
#include <wx/wx.h>
#define	WX_WX_H_INCLUDED 1
#endif
#endif

#ifndef DK4WXAPPLICATIONHELPER_H_INCLUDED
#include "Dk4WxApplicationHelper.h"
#endif

#ifndef	DK4WXHELPCONTROLLER_H_INCLUDED
#include "Dk4WxHelpController.h"
#endif



/**	Frame (top level window).
*/
class Dk4WxFrame : public wxFrame
{
  protected:

    /**	Application helper.
    */
    Dk4WxApplicationHelper	*pAppHelp;

    /**	Help controller for online help.
    */
    Dk4WxHelpController		*pHelp;

    /**	Application name.
    */
    wxString			 sAppName;


    /**	Synchronize access to iInstances.
    */
    static wxCriticalSection	 csInstances;

    /**	Number of instances.
    */
    static int			 iInstances;

  public:

    /**	Constructor.
	@param	appName		Application name.
	@param	appHelper	Application helper.
	@param	helpController	Help controller.
	@param	wxid		Window ID.
    */
    Dk4WxFrame(
      const wxString	&	 appName,
      Dk4WxApplicationHelper	*appHelper,
      Dk4WxHelpController	*helpController,
      int			 wxid
    );

    /**	Restore previously saved window position.
    */
    void
    RestorePosition(void);

    /**	Handler for close event.
	@param	event	Event to process.
    */
    void
    OnClose(wxCloseEvent & event);

    /**	Decide whether we can close the frame.
	@param	isFinal	Flag: Final frame.
	@return	True to close the frame, false to keep it.
    */
    virtual
    bool
    CanClose(bool isFinal);


    /**	Open help system.
    */
    void
    DisplayContents(void);
    
    /**	Open help section specified by name.
	@param	name	Section name.
    */
    void
    DisplaySection(wxString const & name);

    /**	Open a help section specified by number.
	@param	number	Section number (context ID).
    */
    void
    DisplaySection(int number);

  protected:

    /**	Save current window position.
    */
    void
    SavePosition(void);

};



%%	module


#include "Dk4WxFrame.h"



$!trace-include




/**	Protection for number of instances.
*/
wxCriticalSection	Dk4WxFrame::csInstances;



/**	Number of instances.
*/
int			Dk4WxFrame::iInstances = 0;



/**	Keywords to save and restore window size and position.
*/
static const wxChar * const	dk4wxframe_kw_size[] = {
$!string-table	macro=wxT
window.x
window.y
window.w
window.h
window.maximized
window.iconized
$!end
};



/**	Keywords to restore maximized and iconized state.
*/
static const wxChar * const	dk4wxframe_kw_restore[] = {
$!string-table	macro=wxT
window.restore-maximized
window.restore-iconized
window.restore-size
$!end
};



Dk4WxFrame::Dk4WxFrame(
  const wxString	&	 appName,
  Dk4WxApplicationHelper	*appHelper,
  Dk4WxHelpController		*helpController,
  int				 wxid
)
: wxFrame(NULL, wxid, appName)
{
  pAppHelp = appHelper;
  pHelp    = helpController;
  sAppName = appName;
  {
    wxCriticalSectionLocker	lock(csInstances);
    iInstances++;
  }
  Connect(wxid, wxEVT_CLOSE_WINDOW, wxCloseEventHandler(Dk4WxFrame::OnClose));
}



void
Dk4WxFrame::OnClose(wxCloseEvent & event)
{
  bool		doClose = true;
  bool		isFinal	= false;
  bool		savePos	= false;

  {
    wxCriticalSectionLocker	lock(csInstances);
    if (1 == iInstances) {
      isFinal = true;
    }
  }
  if (event.CanVeto()) {
    doClose = CanClose(isFinal);
  } else {
    (void)CanClose(isFinal);
  }
  if (doClose) {
    {
      wxCriticalSectionLocker	lock(csInstances);
      if (1 == iInstances--) { savePos = true; }
    }
    if (savePos) {
      SavePosition();
    }
    event.Skip();
  } else {
    event.Veto();
  }
}



bool
Dk4WxFrame::CanClose(bool isFinal)
{
  return true;
}


void
Dk4WxFrame::SavePosition(void)
{
  int	iv[6];
  if (NULL != pAppHelp) {
    iv[4] = iv[5] = 0;
    if (IsIconized()) {
      iv[5] = 1;
      Iconize(false);
    }
    if (IsMaximized()) {
      iv[4] = 1;
      Maximize(false);
    }
    GetPosition(&(iv[0]), &(iv[1]));
    GetSize(&(iv[2]), &(iv[3]));
    $? ". saving current size"
    $? ". x = %d", iv[0]
    $? ". y = %d", iv[1]
    $? ". w = %d", iv[2]
    $? ". h = %d", iv[3]
    pAppHelp->SetMultiple(dk4wxframe_kw_size, iv, 6);
  }
}



void
Dk4WxFrame::RestorePosition(void)
{
  int	iv[6];		/* Values stored in config */
  bool	bv[3];		/* Feature restore */
  int	x;		/* X position */
  int	y;		/* Y position */
  int	w;		/* Width */
  int	h;		/* Height */

  $? "+ Dk4WxFrame::RestorePosition"

#if wxCHECK_VERSION(3,0,0)
  bv[2] = false;
#else
  bv[2] = true;
#endif

  /* Initialize variables.
  */
  iv[0] = iv[1] = -1;
  iv[2] = iv[3] = iv[4] = iv[5] = 0;
  bv[0] = bv[1] = false;

  /*	Retrieve stored values if found.
  */
  if (NULL != pAppHelp) {
    pAppHelp->GetMultiple(dk4wxframe_kw_size, iv, 6);
    pAppHelp->GetMultiple(dk4wxframe_kw_restore, bv, 3);
  }

  /*	Find size if not stored.
  */
  GetSize(&w, &h);
  x = iv[0];
  y = iv[1];

  /*	Correct size if size restoration wanted, increase if necessary.
  */
  if (bv[2]) {	$? ". bv[2]"
    if (iv[2] > w) { w = iv[2]; }
    if (iv[3] > h) { h = iv[3]; }
  }

  /*	Correct values, no parts of window should be outside screen.
  */
  Dk4WxApplicationHelper::CorrectPosition(x, y, w, h);

  /*	Set position and optionally size.
  */
  if (bv[2]) {	$? ". bv[2]"
    SetSize(x, y, w, h);
  } else {
    SetPosition( wxPoint(x,y) );
  }

  /*	Restore maximized or iconized state if required.
  */
  if (bv[0]) {	$? ". bv[0]"
    Maximize(0 != iv[4]);
  }
  if (bv[1]) {	$? ". bv[1]"
    Iconize(0 != iv[5]);
  }

  $? "- Dk4WxFrame::RestorePosition"
}



void
Dk4WxFrame::DisplayContents(void)
{
  if (NULL != pHelp) {
    pHelp->DisplayContents();
  }
}


    
void
Dk4WxFrame::DisplaySection(wxString const & name)
{
  if (NULL != pHelp) {
    pHelp->DisplaySection(name);
  }
}



void
Dk4WxFrame::DisplaySection(int number)
{
  if (NULL != pHelp) {
    pHelp->DisplaySection(number);
  }
}