summaryrefslogtreecommitdiffstats
path: root/src/libs/7zip/unix/CPP/Windows/Control/Dialog.cpp
blob: 464ae302c549100c3aa1df4eaec4c1201249ff32 (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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// Dialog.cpp

#include "StdAfx.h"

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif 

#include  <wx/filename.h>


#undef _WIN32
 
#include "Windows/Control/DialogImpl.h"
#include "Windows/Synchronization.h"


// FIXME
class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};

DECLARE_APP(MyApp)

// #include "../GUI/p7zip_32.xpm"
extern const char * p7zip_32_xpm[];

const TCHAR * nameWindowToUnix(const TCHAR * lpFileName) {
  if ((lpFileName[0] == wxT('c')) && (lpFileName[1] == wxT(':'))) return lpFileName+2;
  return lpFileName;
}


extern time_t g_T0; // FIXME

#define DIALOG_ID_MESSAGEBOX  8100
#define DIALOG_ID_DIR_DIALOG  8101
#define DIALOG_ID_FILE_DIALOG 8102
#define DIALOG_ID_POST_DIALOG 8190
#define DIALOG_ID_END_DIALOG  8199

static struct
{
	bool busy;

	int id;
	wxWindow *parentWindow;

	// CreateDialog
	NWindows::NControl::CModalDialog * dialog;

	// EndModal
	int value;
	NWindows::NControl::CModalDialogImpl * window;

	// MessageBox
	const TCHAR * msg;
	const TCHAR * title;
	int flag;

	// 
	LPCWSTR initialFolderOrFile;

	wxSemaphore * sem;
	int ret;

	UString resultPath;
	
#define MAX_CREATE 16
} g_tabCreate[MAX_CREATE];

static int myCreateHandle2(int n);

static int findFreeInd()
{
static NWindows::NSynchronization::CCriticalSection g_CriticalSection;

	g_CriticalSection.Enter();
	int ind = 0;
	while (ind < MAX_CREATE)
	{
		if (g_tabCreate[ind].busy == false)
		{
			g_tabCreate[ind].busy = true;
			break;
		}
		ind++;
	}
	g_CriticalSection.Leave();

	return ind;
}

static int WaitInd(wxWindow * destWindow, int ind,int id,wxWindow * parent,UString &resultPath)
{
	int ret = 0;

	g_tabCreate[ind].id           = id;
	g_tabCreate[ind].parentWindow = parent;
	g_tabCreate[ind].ret          = 0;
	g_tabCreate[ind].resultPath   = wxEmptyString;

	if (wxThread::IsMain())
	{
		ret = myCreateHandle2(ind);
		resultPath = g_tabCreate[ind].resultPath;
	}
	else
	{
		if (destWindow == 0) {
			extern wxWindow * g_window;
        		if (g_window == 0)
			{
				printf("INTERNAL ERROR : g_window and destWindow == NULL\n"); abort();
			}
			destWindow = g_window;
		}
		g_tabCreate[ind].sem = new wxSemaphore(0);

		// create any type of command event here
		wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, WORKER_EVENT );
		event.SetInt( ind );

		// send in a thread-safe way
		// DEBUG printf("T=0x%lx - %d : WaitInd(%d,%p): BEGIN\n", wxThread::GetCurrentId(),time(0)-g_T0,g_tabCreate[ind].id,g_tabCreate[ind].parentWindow);
		wxPostEvent( destWindow, event );

		g_tabCreate[ind].sem->Wait();

		ret = g_tabCreate[ind].ret;
		resultPath = g_tabCreate[ind].resultPath;
		// DEBUG printf("T=0x%lx - %d : WaitInd(%d,%p): ret=%d\n", wxThread::GetCurrentId(),time(0)-g_T0,g_tabCreate[ind].id,g_tabCreate[ind].parentWindow,ret);
		delete g_tabCreate[ind].sem;
		g_tabCreate[ind].sem = 0;
	}

	g_tabCreate[ind].busy = false;

	return ret;
}

static int WaitInd(wxWindow * destWindow,int ind,int id,wxWindow * parent)
{
	UString resultPath;
	return WaitInd(destWindow,ind,id,parent,resultPath);
}

void verify_main_thread(void);

class LockGUI
{
	bool _IsMain;
	public:
		LockGUI() {
			
			verify_main_thread();
			
			_IsMain = wxThread::IsMain();
			if (!_IsMain) {
				// DEBUG
				printf("GuiEnter-Dialog(0x%lx)\n",wxThread::GetCurrentId());
				abort(); // FIXME wxMutexGuiEnter();
			}
	       	}
		~LockGUI() { 
			if (!_IsMain) {
				wxMutexGuiLeave();
				// DEBUG printf("GuiLeave(0x%lx)\n",wxThread::GetCurrentId());
			}
	       	}
};

static const unsigned int kNumDialogsMax = 32;
static unsigned int g_NumDialogs = 0;
static const CDialogInfo *g_Dialogs[kNumDialogsMax]; 

void RegisterDialog(const CDialogInfo *dialogInfo) 
{ 
  // DEBUG printf("RegisterDialog : %d\n",dialogInfo->id);
  if (g_NumDialogs < kNumDialogsMax)
    g_Dialogs[g_NumDialogs++] = dialogInfo; 
}

namespace NWindows {

	CSysString MyLoadString(unsigned int resourceID)
	{
		for(unsigned i=0; i < g_NumDialogs; i++) {
			if (g_Dialogs[i]->stringTable) {
				int j = 0;
				while(g_Dialogs[i]->stringTable[j].str) {
					if (resourceID == g_Dialogs[i]->stringTable[j].id) {
						return g_Dialogs[i]->stringTable[j].str;
					}

					j++;
				}
			}
		}
		return L"FIXME-MyLoadStringW-";
	}

	namespace NControl {

/////////////////////////////////////////// CModalDialog //////////////////////////////////

			bool CModalDialog::CheckButton(int buttonID, UINT checkState)
			{
				LockGUI lock;
				wxCheckBox* w = (wxCheckBox*)_window->FindWindow(buttonID);
				if (w)
				{
					w->SetValue(checkState == BST_CHECKED);
					return true;
				}
				return false;
			}

			UINT CModalDialog::IsButtonChecked(int buttonID) const
			{ 
				LockGUI lock;
				wxCheckBox* w = (wxCheckBox*)_window->FindWindow(buttonID);
				if (w)
				{
					bool bret = w->GetValue();
					if (bret) return BST_CHECKED;
				}
				return BST_UNCHECKED;
			}

			void CModalDialog::EnableItem(int id, bool state)
			{
				LockGUI lock;
				wxWindow* w = _window->FindWindow(id);
				if (w) w->Enable(state);
			}

			void CModalDialog::SetItemText(int id, const TCHAR *txt)
			{
				LockGUI lock;
				wxWindow* w = _window->FindWindow(id);
				if (w)
				{
					wxString label(txt);
					w->SetLabel(label);
				}
			}

			wxWindow * CModalDialog::GetItem(long id) const
			{
				LockGUI lock;
				return _window->FindWindow(id);
			}

			void CModalDialog::ShowItem(int itemID, int cmdShow) const
			{
				LockGUI lock;
				// cmdShow = SW_HIDE or SW_SHOW (sometimes false or true !)
				wxWindow* w = _window->FindWindow(itemID);
				if (w)
				{
// FIXME					w->Show(cmdShow != SW_HIDE);
					w->Enable(cmdShow != SW_HIDE);
				}
			}

			UINT_PTR CModalDialog::SetTimer(UINT_PTR idEvent , unsigned milliseconds)
			{
				LockGUI lock;
				return _window->SetTimer(idEvent , milliseconds);
			}

			void CModalDialog::KillTimer(UINT_PTR idEvent)
			{
				LockGUI lock;
				_window->KillTimer(idEvent);
			}

			void CModalDialog::SetText(const TCHAR *_title) {
				LockGUI lock;
			      	_window->SetTitle(_title);
		       	}


			bool CModalDialog::GetText(CSysString &s) {
				wxString str;
				{
					LockGUI lock;
	  				str = _window->GetTitle();
				}
	  			s = str;
	  			return true;
		       	}

			INT_PTR CModalDialog::Create(int id , HWND parentWindow)
			{
				int ind = findFreeInd();

				g_tabCreate[ind].dialog = this;

				return WaitInd(0,  ind,id,parentWindow);
			}

			void CModalDialog::End(int result)
			{ 
				int ind = findFreeInd();

				g_tabCreate[ind].window  = _window;
				g_tabCreate[ind].value   = result;

				WaitInd(this->_window,ind,DIALOG_ID_END_DIALOG,0);
			}

			void CModalDialog::PostMessage(UINT message)
			{
				int ind = findFreeInd();

				g_tabCreate[ind].dialog  = this;
				g_tabCreate[ind].value   = message;

				WaitInd(this->_window,ind,DIALOG_ID_POST_DIALOG,0);
			}

/////////////////////////////////////////// CModalDialogImpl ///////////////////////////////////////

			CModalDialogImpl::CModalDialogImpl(CDialog *dialog, wxWindow* parent, wxWindowID id, 
					 const wxString& title, const wxPoint& pos,
					 const wxSize& size, long style) :
			   		wxDialog(parent, id, title , pos , size, style /* | wxDIALOG_NO_PARENT */ ) ,
				       	_timer(this, TIMER_ID_IMPL), _dialog(dialog)
			{
				// set the frame icon
				this->SetIcon(wxICON(p7zip_32));
			}

			void CModalDialogImpl::OnAnyButton(wxCommandEvent& event)
			{
				int id = event.GetId();
				if (id == wxID_OK)
				{
					if (_dialog) _dialog->OnOK();
					// event.Skip(true);
				}
				else if (id == wxID_CANCEL)
				{
					if (_dialog) _dialog->OnCancel();
					// event.Skip(true);
				}
				else if (id == wxID_HELP)
				{
					if (_dialog) _dialog->OnHelp();
				}
				else
				{
					if (_dialog)
					{
						/* bool bret = */ _dialog->OnButtonClicked(id, FindWindow(id) );
					}
				}
			}

			void CModalDialogImpl::OnAnyChoice(wxCommandEvent &event)
			{
				int itemID =  event.GetId();
				if (_dialog) _dialog->OnCommand(CBN_SELCHANGE, itemID, 0);
			}

			void CModalDialogImpl::OnAnyTimer(wxTimerEvent &event)
			{
				int timerID =  event.GetId();
				if (_dialog) _dialog->OnTimer(timerID , 0);
			}
	}
}

///////////////////////// myCreateHandle


static int myCreateHandle2(int n)
{ 
	unsigned int                           id           = g_tabCreate[n].id;
	wxWindow *                             parentWindow = g_tabCreate[n].parentWindow;
	NWindows::NControl::CModalDialogImpl * window       = 0;

	// DEBUG printf("T=0x%lx - %d : myCreateHandle(%d): BEGIN\n", wxThread::GetCurrentId(),time(0)-g_T0,n);

	if (id == DIALOG_ID_END_DIALOG)
	{
		/* FIXME : the dialog must be shown before ending it ?
		while (!g_tabCreate[n].window->IsShownOnScreen()) Sleep(200);
		Sleep(200);
		*/
		g_tabCreate[n].window->EndModal(g_tabCreate[n].value);
		return 0;
	}

	if (id == DIALOG_ID_POST_DIALOG)
	{
		g_tabCreate[n].dialog->OnMessage(g_tabCreate[n].value, 0, 0);
		return 0;
	}

	if (id == DIALOG_ID_MESSAGEBOX)
	{
		long style = g_tabCreate[n].flag;
		long decorated_style = style;
		if ( ( style & ( wxICON_EXCLAMATION | wxICON_HAND | wxICON_INFORMATION |
				wxICON_QUESTION ) ) == 0 )
		{
			decorated_style |= ( style & wxYES ) ? wxICON_QUESTION : wxICON_INFORMATION ;
		}
		wxMessageDialog dialog(parentWindow, g_tabCreate[n].msg, g_tabCreate[n].title, decorated_style);
		dialog.SetIcon(wxICON(p7zip_32));
		int ret = dialog.ShowModal();

		return ret;
	}

	if (id == DIALOG_ID_DIR_DIALOG)
	{
		wxString defaultDir = g_tabCreate[n].initialFolderOrFile;
		wxDirDialog dirDialog(g_tabCreate[n].parentWindow,
			       	g_tabCreate[n].title, defaultDir);
		dirDialog.SetIcon(wxICON(p7zip_32));
		int ret = dirDialog.ShowModal();
		if (ret == wxID_OK) g_tabCreate[n].resultPath = dirDialog.GetPath();
		return ret;
	}

	if (id == DIALOG_ID_FILE_DIALOG)
	{
		wxString defaultFilename = g_tabCreate[n].initialFolderOrFile;
		
		wxFileName filename(defaultFilename);
		
		wxString dir = filename.GetPath();
		wxString name = filename.GetFullName();
		
		
		// printf("DIALOG_ID_FILE_DIALOG = '%ls' => '%ls'  '%ls'\n",&defaultFilename[0],&dir[0],&name[0]);
		
		
		wxFileDialog fileDialog(g_tabCreate[n].parentWindow, g_tabCreate[n].title,
				dir, name, wxT("All Files (*.*)|*.*"), wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
		fileDialog.SetIcon(wxICON(p7zip_32));
		int ret = fileDialog.ShowModal();
		if (ret == wxID_OK) g_tabCreate[n].resultPath = fileDialog.GetPath();
		return ret;
	}

	for(unsigned  i=0; i < g_NumDialogs; i++) {
		if (id == g_Dialogs[i]->id) {
			// DEBUG printf("%d : Create(%d,%p): CreateDialog-1\n",time(0)-g_T0,id,parentWindow);
			window = (g_Dialogs[i]->createDialog)(g_tabCreate[n].dialog,g_tabCreate[n].parentWindow);
			// DEBUG printf("%d : Create(%d,%p): CreateDialog-2\n",time(0)-g_T0,id,parentWindow);
			break;
		}
	}

	if (window) {

		// DEBUG printf("%d : Create(%d,%p): %p->ShowModal()\n",time(0)-g_T0,id,parentWindow,window);

		// window->Show(true);
		// wxGetApp().ProcessPendingEvents();

		INT_PTR ret = window->ShowModal();

		// DEBUG printf("%d : Create(%d,%p): %p->ShowModal() - ret=%d\n",time(0)-g_T0,id,parentWindow,window,ret);
		window->Detach();
		window->Destroy();

		// DEBUG printf("%d : Create(%d,%p): END\n",time(0)-g_T0,id,parentWindow,window);

		return ret;
	}

	// FIXME
	printf("INTERNAL ERROR : cannot find dialog %d\n",id);

	return 0;
}

void myCreateHandle(int n)
{
	int ret = myCreateHandle2(n);
	g_tabCreate[n].ret = ret;
	g_tabCreate[n].sem->Post();
}

int MessageBoxW(wxWindow * parent, const TCHAR * msg, const TCHAR * title,int flag)
{
	int ind = findFreeInd();

	g_tabCreate[ind].msg          = msg;
	g_tabCreate[ind].title        = title;
	g_tabCreate[ind].flag         = flag;
	
	return WaitInd(parent,ind,DIALOG_ID_MESSAGEBOX,parent); // FIXME
}



// FIXME : should be in Windows/Shell.cpp

namespace NWindows{
namespace NShell{

bool BrowseForFolder(HWND owner, LPCWSTR title, LPCWSTR initialFolder, UString &resultPath)
{
	int ind = findFreeInd();

	g_tabCreate[ind].title               = title;
	g_tabCreate[ind].initialFolderOrFile = nameWindowToUnix(initialFolder);
	
	UString resTmp;
	int ret = WaitInd(0,ind,DIALOG_ID_DIR_DIALOG,owner,resTmp); // FIXME
	if(ret == wxID_OK)
	{
		resultPath = resTmp;
		return true;
	}
	return false;
}

}}

/////////////////////////// CPP/Windows/CommonDialog.cpp
namespace NWindows
{

	bool MyGetOpenFileName(HWND hwnd, LPCWSTR title, LPCWSTR fullFileName, LPCWSTR s, UString &resPath)
	{
		int ind = findFreeInd();

		g_tabCreate[ind].title               = title;
		g_tabCreate[ind].initialFolderOrFile = nameWindowToUnix(fullFileName);
	
		UString resTmp;
		int ret = WaitInd(0,ind,DIALOG_ID_FILE_DIALOG,hwnd,resTmp); // FIXME
		if(ret == wxID_OK)
		{
			resPath = resTmp;
			return true;
		}
		return false;
	}
}