33 #ifdef G4UI_BUILD_WIN32_SESSION
52 #define TEXT_MAX_LINES 300
60 int GetNumberOfLines () {
return linei;}
61 void SetHeightOfPage (
int a_height) { heightOfPage = a_height; }
62 void SetEndOfPage (
int a_value);
63 int GetEndOfPage () {
return endOfPage; }
65 void IncrementEndOfPage ();
66 void DecrementEndOfPage ();
67 void JumpDownEndOfPage ();
68 void JumpUpEndOfPage ();
69 G4bool AppendString (
char* a_string);
70 void Draw (HDC a_hdc,RECT* a_rect);
76 int endOfPage,heightOfPage;
80 TextBuffer::TextBuffer()
81 : linei(0),linen(TEXT_MAX_LINES),endOfPage(0),heightOfPage(12)
84 for(
int count=0;count<256;count++) spaces[count] =
' ';
87 TextBuffer::~TextBuffer()
92 void TextBuffer::SetEndOfPage (
int a_value)
94 if( (a_value<0) || (a_value>=linei)) {
101 void TextBuffer::IncrementEndOfPage ()
104 if(endOfPage>=linei) endOfPage = linei-1;
107 void TextBuffer::DecrementEndOfPage ()
110 if(endOfPage<0) endOfPage = 0;
113 void TextBuffer::JumpDownEndOfPage ()
115 endOfPage += heightOfPage;
116 if(endOfPage>=linei) endOfPage = linei-1;
119 void TextBuffer::JumpUpEndOfPage ()
121 endOfPage -= heightOfPage;
122 if(endOfPage<0) endOfPage = 0;
125 G4bool TextBuffer::AppendString (
char* a_string)
128 if( (a_string==NULL) || (a_string[0]==
'\0') )
return value;
129 int length = strlen(a_string);
130 if(a_string[length-1]==
'\n') {
131 lines[linei] += a_string;
136 lines[linei] += a_string;
139 for(
int count=0;count<linen;count++) {
144 if(value==
true) endOfPage = linei-1;
148 void TextBuffer::Draw (HDC a_hdc,RECT* a_rect)
151 GetTextMetrics (a_hdc,&tm);
152 short charWidth = (short)tm.tmAveCharWidth;
153 short charHeight = (
short)(tm.tmHeight + tm.tmExternalLeading);
154 for(
int row=0;row<heightOfPage;row++) {
155 int rowi = endOfPage - row;
156 short y = (short)(a_rect->bottom - charHeight * (row + 1));
157 if((rowi>=0)&&(rowi<linei)) {
158 TextOut (a_hdc,0,y,(
char*)spaces,256);
159 const char*
string = lines[rowi].data();
161 TextOut (a_hdc,0,y,(
char*)
string,strlen((
char*)
string));
168 static char mainClassName[] =
"G4UIWin32";
169 static char textClassName[] =
"G4UIWin32/Text";
170 static G4bool exitSession =
true;
171 static G4bool exitPause =
true;
172 static G4bool exitHelp =
true;
175 static WNDPROC oldEditWindowProc;
176 static G4bool ConvertStringToInt(
const char*,
int&);
178 static int actionIdentifier = 0;
181 G4UIWin32::G4UIWin32 (
199 interactorManager = G4Win32::getInstance ();
203 wc.style = CS_HREDRAW | CS_VREDRAW;
204 wc.lpfnWndProc = (WNDPROC)G4UIWin32::MainWindowProc;
207 wc.hInstance = ::GetModuleHandle(NULL);
208 wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
209 wc.hCursor = LoadCursor(NULL,IDC_ARROW);
210 wc.hbrBackground = GetStockBrush(WHITE_BRUSH);
211 wc.lpszMenuName = mainClassName;
212 wc.lpszClassName = mainClassName;
213 ::RegisterClass (&wc);
215 wc.style = CS_HREDRAW | CS_VREDRAW;
216 wc.lpfnWndProc = (WNDPROC)G4UIWin32::TextWindowProc;
219 wc.hInstance = ::GetModuleHandle(NULL);
220 wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
221 wc.hCursor = LoadCursor(NULL,IDC_ARROW);
222 wc.hbrBackground = GetStockBrush(WHITE_BRUSH);
223 wc.lpszMenuName = textClassName;
224 wc.lpszClassName = textClassName;
225 ::RegisterClass (&wc);
229 menuBar = CreateMenu();
230 defaultMenu = CreatePopupMenu();
231 AppendMenu(menuBar,MF_POPUP,(UINT)defaultMenu,
"Geant4");
233 textBuffer =
new TextBuffer();
236 mainWindow = ::CreateWindow(mainClassName,mainClassName,
238 CW_USEDEFAULT,CW_USEDEFAULT,
241 ::GetModuleHandle(NULL),
244 ::SetWindowLongPtr(mainWindow,GWLP_USERDATA,LONG(
this));
246 ::SetForegroundWindow(mainWindow);
247 ::ShowWindow(mainWindow,SW_SHOWDEFAULT);
248 ::UpdateWindow(mainWindow);
253 G4UIWin32::~G4UIWin32 (
264 if(textWindow!=NULL) ::SetWindowLongPtr(textWindow,GWLP_USERDATA,LONG(NULL));
265 if(mainWindow!=NULL) {
266 ::SetWindowLongPtr(mainWindow,GWLP_USERDATA,LONG(NULL));
267 ::DestroyWindow(mainWindow);
276 if(interactorManager==NULL)
return this;
279 interactorManager->DisableSecondaryLoop ();
281 while((event = interactorManager->GetEvent())!=NULL) {
282 interactorManager->DispatchEvent(event);
283 if(exitSession==
true)
break;
285 interactorManager->EnableSecondaryLoop ();
289 void G4UIWin32::Prompt (
297 void G4UIWin32::SessionTerminate (
304 void G4UIWin32::PauseSessionStart (
310 if(a_state==
"G4_pause> ") {
311 SecondaryLoop (
"Pause, type continue to exit this state");
314 if(a_state==
"EndOfEvent") {
316 SecondaryLoop (
"End of event, type continue to exit this state");
320 void G4UIWin32::SecondaryLoop (
326 if(interactorManager==NULL)
return;
330 while((event = interactorManager->GetEvent())!=NULL) {
331 interactorManager->DispatchEvent(event);
332 if(exitPause==
true)
break;
337 G4int G4UIWin32::ReceiveG4cout (
343 TextAppendString((
char*)a_string.
data());
347 G4int G4UIWin32::ReceiveG4cerr (
353 TextAppendString((
char*)a_string.
data());
357 G4bool G4UIWin32::GetHelpChoice(
365 if(interactorManager==NULL)
return false;
369 while((event = interactorManager->GetEvent())!=NULL) {
370 interactorManager->DispatchEvent(event);
371 if(exitHelp==
true)
break;
375 if(fHelp==
false)
return false;
381 void G4UIWin32::ExitHelp(
388 void G4UIWin32::AddMenu (
395 if(a_name==NULL)
return;
396 if(defaultMenu!=NULL) {
397 DeleteMenu (menuBar,0,MF_BYPOSITION);
400 HMENU hMenu = CreatePopupMenu();
401 AppendMenu(menuBar,MF_POPUP,(UINT)hMenu,a_label);
403 DrawMenuBar(mainWindow);
406 void G4UIWin32::AddButton (
409 ,
const char* a_command
414 if(a_menu==NULL)
return;
415 if(a_label==NULL)
return;
416 if(a_command==NULL)
return;
417 HMENU hMenu = (HMENU)GetInteractor(a_menu);
419 commands[actionIdentifier] = a_command;
420 AppendMenu (hMenu,MF_STRING,actionIdentifier,a_label);
429 return commands[a_id];
434 LRESULT CALLBACK G4UIWin32::MainWindowProc (
443 static short charWidth,charHeight;
450 GetWindowRect (a_window,&rect);
452 hdc = GetDC (a_window);
453 GetTextMetrics (hdc,&tm);
454 charWidth = (short)tm.tmAveCharWidth;
455 charHeight = (
short)(tm.tmHeight + tm.tmExternalLeading);
456 ReleaseDC (a_window,hdc);
458 G4UIWin32* This = (G4UIWin32*)tmpSession;
460 This->textWindow = CreateWindow (textClassName,NULL,
461 WS_CHILD | WS_VISIBLE | WS_VSCROLL,
463 This->textCols * charWidth,
464 This->textRows * charHeight,
466 GetWindowInstance(a_window),
468 ::SetWindowLongPtr (This->textWindow,GWLP_USERDATA,LONG(This));
470 This->editWindow = CreateWindow (
"edit",NULL,
471 WS_CHILD | WS_VISIBLE | WS_BORDER,
472 0,This->textRows * charHeight,
473 This->textCols * charWidth,charHeight,
475 GetWindowInstance(a_window),
477 oldEditWindowProc = (WNDPROC)GetWindowLongPtr(This->editWindow,GWLP_WNDPROC);
478 SetWindowLongPtr (This->editWindow,GWLP_WNDPROC,(LONG)EditWindowProc);
480 MoveWindow (a_window,
482 2 * GetSystemMetrics(SM_CXFRAME) +
483 This->textCols * charWidth,
484 GetSystemMetrics(SM_CYCAPTION) +
485 2 * GetSystemMetrics(SM_CYFRAME) +
486 This->textRows * charHeight + charHeight,
491 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(a_window,GWLP_USERDATA);
494 int width = LOWORD(a_lParam);
495 int height = HIWORD(a_lParam);
496 int editHeight = charHeight;
497 MoveWindow (This->textWindow,
499 width,height - editHeight,
501 MoveWindow (This->editWindow,
502 0,height - editHeight,
505 ((TextBuffer*)This->textBuffer)->SetHeightOfPage(height/charHeight);
509 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(a_window,GWLP_USERDATA);
510 if(This!=NULL) SetFocus (This->editWindow);
513 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(a_window,GWLP_USERDATA);
515 if(This->fHelp==
false) {
516 G4String command = This->GetCommand(a_wParam);
517 This->ApplyShellCommand (command,exitSession,exitPause);
525 return (DefWindowProc(a_window,a_message,a_wParam,a_lParam));
528 LRESULT CALLBACK G4UIWin32::TextWindowProc (
539 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(a_window,GWLP_USERDATA);
541 TextBuffer* textBuffer = (TextBuffer*)This->textBuffer;
543 GetClientRect (a_window,&rect);
545 HDC hdc = BeginPaint(a_window,&ps);
546 textBuffer->Draw(hdc,&rect);
547 EndPaint(a_window,&ps);
551 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(a_window,GWLP_USERDATA);
553 TextBuffer* textBuffer = (TextBuffer*)This->textBuffer;
554 int what = LOWORD(a_wParam);
557 textBuffer->DecrementEndOfPage();
560 textBuffer->IncrementEndOfPage();
563 textBuffer->JumpUpEndOfPage();
566 textBuffer->JumpDownEndOfPage();
568 case SB_THUMBPOSITION:
570 textBuffer->SetEndOfPage(HIWORD(a_wParam));
575 int eop = textBuffer->GetEndOfPage();
576 SetScrollPos(a_window,SB_VERT,eop,
TRUE);
577 InvalidateRect(a_window,NULL,
TRUE);
583 return (DefWindowProc(a_window,a_message,a_wParam,a_lParam));
586 LRESULT CALLBACK G4UIWin32::EditWindowProc (
599 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(
600 GetParent(a_window),GWLP_USERDATA);
602 GetWindowText (a_window,buffer,128);
605 Edit_SetText(a_window,
"");
606 Edit_SetSel(a_window,0,0);
609 if(This->fHelp==
true) {
611 This->fHelp = ConvertStringToInt(command.data(),This->fHelpChoice);
613 This->fHistory.push_back(command);
614 This->fHistoryPos = -1;
615 This->ApplyShellCommand (command,exitSession,exitPause);
621 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(
622 GetParent(a_window),GWLP_USERDATA);
623 if( (This!=NULL) && (This->fHelp==
true) )
break;
625 Edit_GetText(a_window,buffer,128);
630 G4String cmd = This->Complete(command);
631 const char* d = cmd.
data();
633 Edit_SetText(a_window,d);
634 Edit_SetSel(a_window,l,l);
639 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(
640 GetParent(a_window),GWLP_USERDATA);
642 int pos = This->fHistoryPos== -1 ?
643 This->fHistory.size()-1 : This->fHistoryPos-1;
644 if((pos>=0)&&(pos<(
int)This->fHistory.size())) {
646 const char* d = command.
data();
648 Edit_SetText(a_window,d);
649 Edit_SetSel(a_window,l,l);
651 This->fHistoryPos =
pos;
656 G4UIWin32* This = (G4UIWin32*)::GetWindowLongPtr(
657 GetParent(a_window),GWLP_USERDATA);
659 int pos = This->fHistoryPos + 1;
660 if((pos>=0)&&(pos<(int)This->fHistory.size())) {
662 const char* d = command.
data();
664 Edit_SetText(a_window,d);
665 Edit_SetSel(a_window,l,l);
667 This->fHistoryPos =
pos;
668 }
else if(pos>=(
int)This->fHistory.size()) {
669 Edit_SetText(a_window,
"");
670 Edit_SetSel(a_window,0,0);
672 This->fHistoryPos = -1;
678 return CallWindowProc(oldEditWindowProc,
683 void G4UIWin32::TextAppendString (
689 if( (a_string==NULL) || (a_string[0]==
'\0') )
return;
690 if(textWindow==NULL)
return;
691 if(((TextBuffer*)textBuffer)->AppendString(a_string)==
true) {
694 GetClientRect(textWindow,&rect);
695 InvalidateRect(textWindow,NULL,
TRUE);
696 HDC hdc = GetDC(textWindow);
697 ((TextBuffer*)textBuffer)->Draw(hdc,&rect);
698 ReleaseDC (textWindow,hdc);
699 int linen = ((TextBuffer*)textBuffer)->GetNumberOfLines();
700 SetScrollRange(textWindow,SB_VERT,0,linen-1,
TRUE);
701 SetScrollPos(textWindow,SB_VERT,linen-1,
TRUE);
705 G4bool ConvertStringToInt(
713 if(aString==NULL)
return false;
715 long value = strtol(aString,&s,10);
716 if(s==aString)
return false;
void SetSession(G4UIsession *const value)
static G4UImanager * GetUIpointer()
const char * data() const
void SetCoutDestination(G4UIsession *const value)
static const G4double pos