29 #ifdef G4UI_BUILD_WIN32_SESSION
45 #define TEXT_MAX_LINES 300
53 int GetNumberOfLines () {
return linei;}
54 void SetHeightOfPage (
int a_height) { heightOfPage = a_height; }
55 void SetEndOfPage (
int a_value);
56 int GetEndOfPage () {
return endOfPage; }
58 void IncrementEndOfPage ();
59 void DecrementEndOfPage ();
60 void JumpDownEndOfPage ();
61 void JumpUpEndOfPage ();
62 G4bool AppendString (
char* a_string);
63 void Draw (HDC a_hdc,RECT* a_rect);
69 int endOfPage,heightOfPage;
73 TextBuffer::TextBuffer()
74 : linei(0),linen(TEXT_MAX_LINES),endOfPage(0),heightOfPage(12)
77 for(
int count=0;count<256;count++) spaces[count] =
' ';
80 TextBuffer::~TextBuffer()
85 void TextBuffer::SetEndOfPage (
int a_value)
87 if( (a_value<0) || (a_value>=linei)) {
94 void TextBuffer::IncrementEndOfPage ()
97 if(endOfPage>=linei) endOfPage = linei-1;
100 void TextBuffer::DecrementEndOfPage ()
103 if(endOfPage<0) endOfPage = 0;
106 void TextBuffer::JumpDownEndOfPage ()
108 endOfPage += heightOfPage;
109 if(endOfPage>=linei) endOfPage = linei-1;
112 void TextBuffer::JumpUpEndOfPage ()
114 endOfPage -= heightOfPage;
115 if(endOfPage<0) endOfPage = 0;
118 G4bool TextBuffer::AppendString (
char* a_string)
121 if( (a_string==NULL) || (a_string[0]==
'\0') )
return value;
122 int length = strlen(a_string);
123 if(a_string[length-1]==
'\n') {
124 lines[linei] += a_string;
129 lines[linei] += a_string;
132 for(
int count=0;count<linen;count++) {
137 if(value==
true) endOfPage = linei-1;
144 GetTextMetrics (a_hdc,&tm);
145 short charWidth = (short)tm.tmAveCharWidth;
146 short charHeight = (
short)(tm.tmHeight + tm.tmExternalLeading);
147 for(
int row=0;row<heightOfPage;row++) {
148 int rowi = endOfPage - row;
149 short y = (short)(a_rect->bottom - charHeight * (row + 1));
150 if((rowi>=0)&&(rowi<linei)) {
151 TextOut (a_hdc,0,y,(
char*)spaces,256);
152 const char*
string = lines[rowi].data();
154 TextOut (a_hdc,0,y,(
char*)
string,strlen((
char*)
string));
161 static char mainClassName[] =
"G4UIWin32";
162 static char textClassName[] =
"G4UIWin32/Text";
163 static G4bool exitSession =
true;
164 static G4bool exitPause =
true;
165 static G4bool exitHelp =
true;
168 static WNDPROC oldEditWindowProc;
169 static G4bool ConvertStringToInt(
const char*,
int&);
171 static int actionIdentifier = 0;
174 G4UIWin32::G4UIWin32 (
192 interactorManager = G4Win32::getInstance ();
196 wc.style = CS_HREDRAW | CS_VREDRAW;
197 wc.lpfnWndProc = (WNDPROC)G4UIWin32::MainWindowProc;
200 wc.hInstance = ::GetModuleHandle(NULL);
201 wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
202 wc.hCursor = LoadCursor(NULL,IDC_ARROW);
203 wc.hbrBackground = GetStockBrush(WHITE_BRUSH);
204 wc.lpszMenuName = mainClassName;
205 wc.lpszClassName = mainClassName;
206 ::RegisterClass (&wc);
208 wc.style = CS_HREDRAW | CS_VREDRAW;
209 wc.lpfnWndProc = (WNDPROC)G4UIWin32::TextWindowProc;
212 wc.hInstance = ::GetModuleHandle(NULL);
213 wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
214 wc.hCursor = LoadCursor(NULL,IDC_ARROW);
215 wc.hbrBackground = GetStockBrush(WHITE_BRUSH);
216 wc.lpszMenuName = textClassName;
217 wc.lpszClassName = textClassName;
218 ::RegisterClass (&wc);
222 menuBar = CreateMenu();
223 defaultMenu = CreatePopupMenu();
224 AppendMenu(menuBar,MF_POPUP,(UINT_PTR)defaultMenu,
"Geant4");
226 textBuffer =
new TextBuffer();
229 mainWindow = ::CreateWindow(mainClassName,mainClassName,
231 CW_USEDEFAULT,CW_USEDEFAULT,
234 ::GetModuleHandle(NULL),
237 ::SetWindowLongPtr(mainWindow,GWLP_USERDATA,(LONG_PTR)
this);
239 ::SetForegroundWindow(mainWindow);
240 ::ShowWindow(mainWindow,SW_SHOWDEFAULT);
241 ::UpdateWindow(mainWindow);
246 G4UIWin32::~G4UIWin32 (
257 if(textWindow!=NULL) ::SetWindowLongPtr(textWindow,GWLP_USERDATA,LONG(NULL));
258 if(mainWindow!=NULL) {
259 ::SetWindowLongPtr(mainWindow,GWLP_USERDATA,LONG(NULL));
260 ::DestroyWindow(mainWindow);
269 if(interactorManager==NULL)
return this;
272 interactorManager->DisableSecondaryLoop ();
274 while((event = interactorManager->GetEvent())!=NULL) {
275 interactorManager->DispatchEvent(event);
276 if(exitSession==
true)
break;
278 interactorManager->EnableSecondaryLoop ();
282 void G4UIWin32::Prompt (
290 void G4UIWin32::SessionTerminate (
297 void G4UIWin32::PauseSessionStart (
303 if(a_state==
"G4_pause> ") {
304 SecondaryLoop (
"Pause, type continue to exit this state");
307 if(a_state==
"EndOfEvent") {
309 SecondaryLoop (
"End of event, type continue to exit this state");
313 void G4UIWin32::SecondaryLoop (
319 if(interactorManager==NULL)
return;
323 while((event = interactorManager->GetEvent())!=NULL) {
324 interactorManager->DispatchEvent(event);
325 if(exitPause==
true)
break;
330 G4int G4UIWin32::ReceiveG4cout (
336 TextAppendString((
char*)a_string.
data());
340 G4int G4UIWin32::ReceiveG4cerr (
346 TextAppendString((
char*)a_string.
data());
350 G4bool G4UIWin32::GetHelpChoice(
358 if(interactorManager==NULL)
return false;
362 while((event = interactorManager->GetEvent())!=NULL) {
363 interactorManager->DispatchEvent(event);
364 if(exitHelp==
true)
break;
368 if(fHelp==
false)
return false;
374 void G4UIWin32::ExitHelp(
381 void G4UIWin32::AddMenu (
388 if(a_name==NULL)
return;
389 if(defaultMenu!=NULL) {
390 DeleteMenu (menuBar,0,MF_BYPOSITION);
393 HMENU hMenu = CreatePopupMenu();
394 AppendMenu(menuBar,MF_POPUP,(UINT_PTR)hMenu,a_label);
396 DrawMenuBar(mainWindow);
399 void G4UIWin32::AddButton (
402 ,
const char* a_command
407 if(a_menu==NULL)
return;
408 if(a_label==NULL)
return;
409 if(a_command==NULL)
return;
410 HMENU hMenu = (HMENU)GetInteractor(a_menu);
412 commands[actionIdentifier] = a_command;
413 AppendMenu (hMenu,MF_STRING,actionIdentifier,a_label);
422 return commands[a_id];
427 LRESULT CALLBACK G4UIWin32::MainWindowProc (
436 static short charWidth,charHeight;
443 GetWindowRect (a_window,&rect);
445 hdc = GetDC (a_window);
446 GetTextMetrics (hdc,&tm);
447 charWidth = (short)tm.tmAveCharWidth;
448 charHeight = (
short)(tm.tmHeight + tm.tmExternalLeading);
449 ReleaseDC (a_window,hdc);
451 G4UIWin32* This = (G4UIWin32*)tmpSession;
453 This->textWindow = CreateWindow (textClassName,NULL,
454 WS_CHILD | WS_VISIBLE | WS_VSCROLL,
456 This->textCols * charWidth,
457 This->textRows * charHeight,
459 GetWindowInstance(a_window),
461 ::SetWindowLongPtr (This->textWindow,GWLP_USERDATA,(LONG_PTR)This);
463 This->editWindow = CreateWindow (
"edit",NULL,
464 WS_CHILD | WS_VISIBLE | WS_BORDER,
465 0,This->textRows * charHeight,
466 This->textCols * charWidth,charHeight,
468 GetWindowInstance(a_window),
470 oldEditWindowProc = (WNDPROC)GetWindowLongPtr(This->editWindow,GWLP_WNDPROC);
471 SetWindowLongPtr (This->editWindow,GWLP_WNDPROC,(LONG_PTR)EditWindowProc);
473 MoveWindow (a_window,
475 2 * GetSystemMetrics(SM_CXFRAME) +
476 This->textCols * charWidth,
477 GetSystemMetrics(SM_CYCAPTION) +
478 2 * GetSystemMetrics(SM_CYFRAME) +
479 This->textRows * charHeight + charHeight,
484 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(a_window,GWLP_USERDATA);
487 int width = LOWORD(a_lParam);
488 int height = HIWORD(a_lParam);
489 int editHeight = charHeight;
490 MoveWindow (This->textWindow,
492 width,height - editHeight,
494 MoveWindow (This->editWindow,
495 0,height - editHeight,
498 ((TextBuffer*)This->textBuffer)->SetHeightOfPage(height/charHeight);
502 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(a_window,GWLP_USERDATA);
503 if(This!=NULL) SetFocus (This->editWindow);
506 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(a_window,GWLP_USERDATA);
508 if(This->fHelp==
false) {
509 G4String command = This->GetCommand(a_wParam);
510 This->ApplyShellCommand (command,exitSession,exitPause);
518 return (DefWindowProc(a_window,a_message,a_wParam,a_lParam));
521 LRESULT CALLBACK G4UIWin32::TextWindowProc (
532 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(a_window,GWLP_USERDATA);
534 TextBuffer* textBuffer = (TextBuffer*)This->textBuffer;
536 GetClientRect (a_window,&rect);
538 HDC hdc = BeginPaint(a_window,&ps);
539 textBuffer->Draw(hdc,&rect);
540 EndPaint(a_window,&ps);
544 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(a_window,GWLP_USERDATA);
546 TextBuffer* textBuffer = (TextBuffer*)This->textBuffer;
547 int what = LOWORD(a_wParam);
550 textBuffer->DecrementEndOfPage();
553 textBuffer->IncrementEndOfPage();
556 textBuffer->JumpUpEndOfPage();
559 textBuffer->JumpDownEndOfPage();
561 case SB_THUMBPOSITION:
563 textBuffer->SetEndOfPage(HIWORD(a_wParam));
568 int eop = textBuffer->GetEndOfPage();
569 SetScrollPos(a_window,SB_VERT,eop,
TRUE);
570 InvalidateRect(a_window,NULL,
TRUE);
576 return (DefWindowProc(a_window,a_message,a_wParam,a_lParam));
579 LRESULT CALLBACK G4UIWin32::EditWindowProc (
592 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(
593 GetParent(a_window),GWLP_USERDATA);
595 GetWindowText (a_window,buffer,128);
598 Edit_SetText(a_window,
"");
599 Edit_SetSel(a_window,0,0);
602 if(This->fHelp==
true) {
604 This->fHelp = ConvertStringToInt(command.data(),This->fHelpChoice);
606 This->fHistory.push_back(command);
607 This->fHistoryPos = -1;
608 This->ApplyShellCommand (command,exitSession,exitPause);
614 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(
615 GetParent(a_window),GWLP_USERDATA);
616 if( (This!=NULL) && (This->fHelp==
true) )
break;
618 Edit_GetText(a_window,buffer,128);
623 G4String cmd = This->Complete(command);
624 const char*
d = cmd.
data();
626 Edit_SetText(a_window,d);
627 Edit_SetSel(a_window,l,l);
632 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(
633 GetParent(a_window),GWLP_USERDATA);
635 int pos = This->fHistoryPos== -1 ?
636 This->fHistory.size()-1 : This->fHistoryPos-1;
637 if((pos>=0)&&(pos<(
int)This->fHistory.size())) {
639 const char*
d = command.
data();
641 Edit_SetText(a_window,d);
642 Edit_SetSel(a_window,l,l);
644 This->fHistoryPos =
pos;
649 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(
650 GetParent(a_window),GWLP_USERDATA);
652 int pos = This->fHistoryPos + 1;
653 if((pos>=0)&&(pos<(
int)This->fHistory.size())) {
655 const char*
d = command.
data();
657 Edit_SetText(a_window,d);
658 Edit_SetSel(a_window,l,l);
660 This->fHistoryPos =
pos;
661 }
else if(pos>=(
int)This->fHistory.size()) {
662 Edit_SetText(a_window,
"");
663 Edit_SetSel(a_window,0,0);
665 This->fHistoryPos = -1;
671 return CallWindowProc(oldEditWindowProc,
676 void G4UIWin32::TextAppendString (
682 if( (a_string==NULL) || (a_string[0]==
'\0') )
return;
683 if(textWindow==NULL)
return;
684 if(((TextBuffer*)textBuffer)->AppendString(a_string)==
true) {
687 GetClientRect(textWindow,&rect);
688 InvalidateRect(textWindow,NULL,
TRUE);
689 HDC hdc = GetDC(textWindow);
690 ((TextBuffer*)textBuffer)->Draw(hdc,&rect);
691 ReleaseDC (textWindow,hdc);
692 int linen = ((TextBuffer*)textBuffer)->GetNumberOfLines();
693 SetScrollRange(textWindow,SB_VERT,0,linen-1,
TRUE);
694 SetScrollPos(textWindow,SB_VERT,linen-1,
TRUE);
698 G4bool ConvertStringToInt(
706 if(aString==NULL)
return false;
708 long value = strtol(aString,&s,10);
709 if(s==aString)
return false;