[done] RunProgram whis text

Just starting out? Need help? Post your questions and find answers here.
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: RunProgram whis text

Post by ChrisR »

mestnyi wrote: Wed Mar 05, 2025 8:42 am Can you give me the code for how you did it?
Here's a Preview demo example

Code: Select all

EnableExplicit

Global PreviewRunning, PreviewProgramName$

Procedure.s GetTempFilename(BaseName$, NbRamdomLetter.l = 3, NbRamdomDigit.l = 3, Ext$ = ".pb")
  Protected FileName$, I
  FileName$  = GetTemporaryDirectory() + BaseName$
  For I = 1 To NbRamdomLetter
    FileName$ + Chr('a'+Random(25))
  Next
  For I = 1 To NbRamdomDigit
    FileName$ + Str(Random(9))
  Next
  FileName$ + Ext$
  If FileSize(FileName$) = -1   ; If file does not exist, return the generated filename
    ProcedureReturn FileName$
  Else                          ; Else, generate a new filename
    ProcedureReturn GetTempFilename(BaseName$, NbRamdomLetter, NbRamdomDigit, Ext$)
  EndIf
EndProcedure

Procedure CallPreview(SourceCode$)
  If SourceCode$ = "" : ProcedureReturn : EndIf
  Protected TempFileName$, hTempFile
  Protected CompilerPath$, CompilPreview, CompilPreviewOutput$

  CompilerPath$ = #PB_Compiler_Home + "Compilers\pbcompiler.exe"
  If FileSize(CompilerPath$)
    TempFileName$ = GetTempFilename("~Preview_", 3, 3, ".pb")
    hTempFile = CreateFile(#PB_Any, TempFileName$, #PB_UTF8)
    If hTempFile
      WriteStringFormat(hTempFile, #PB_UTF8)
      WriteStringN(hTempFile, SourceCode$)
      CloseFile(hTempFile)
      
      PreviewProgramName$ = GetPathPart(TempFileName$) + GetFilePart(TempFileName$, #PB_FileSystem_NoExtension) + ".exe"
      CompilPreview = RunProgram(CompilerPath$, #DQUOTE$ + TempFileName$ +#DQUOTE$+ " /EXE " +#DQUOTE$+ PreviewProgramName$ +#DQUOTE$ + " /XP /DPIAWARE", "", #PB_Program_Hide | #PB_Program_Open | #PB_Program_Read)
      If CompilPreview
        While ProgramRunning(CompilPreview)
          If AvailableProgramOutput(CompilPreview)
            CompilPreviewOutput$ = ReadProgramString(CompilPreview)
          EndIf
        Wend
        If ProgramExitCode(CompilPreview)
          CloseProgram(CompilPreview)
          MessageRequester("Preview Error", "Fail to compile:" +#CRLF$+ "PBcompiler.exe %Temp%\" + GetFilePart(TempFileName$) + " /EXE %Temp%\" + GetFilePart(PreviewProgramName$) + #CRLF$+#CRLF$+ CompilPreviewOutput$, #PB_MessageRequester_Error | #PB_MessageRequester_Ok)
        Else
          CloseProgram(CompilPreview)
          If FileSize(PreviewProgramName$)
            DeleteFile(TempFileName$)
            PreviewRunning = RunProgram(PreviewProgramName$, "", "", #PB_Program_Open)
          Else
            MessageRequester("Preview Error", "Fail to compile:" +#CRLF$+ "PBcompiler.exe %Temp%\" + GetFilePart(TempFileName$) + " /EXE %Temp%\" + GetFilePart(PreviewProgramName$), #PB_MessageRequester_Error | #PB_MessageRequester_Ok)
          EndIf
        EndIf
      Else
        MessageRequester("Preview Error", "Fail to compile:" +#CRLF$+ "PBcompiler.exe %Temp%\" + GetFilePart(TempFileName$) + " /EXE %Temp%\" + GetFilePart(PreviewProgramName$), #PB_MessageRequester_Error | #PB_MessageRequester_Ok)
      EndIf
    EndIf
  Else
    MessageRequester("Preview Error", "PBcompiler.exe was not found in Compilers folder", #PB_MessageRequester_Error | #PB_MessageRequester_Ok)
  EndIf
EndProcedure

Procedure Resize_Window()
  ResizeGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 80)
  ResizeGadget(1, 10, WindowHeight(0) - 60, WindowWidth(0) - 20, 50)
EndProcedure

Procedure Open_Window(X = 0, Y = 0, Width = 660, Height = 300)
  Protected SourceCode$ = "If OpenWindow(0, 0, 0, 260, 60, " +#DQUOTE$+ "Demo Preview Window" +#DQUOTE$+ ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)" +#CRLF$+
                          "  TextGadget(0, 20, 20, 220, 22, " +#DQUOTE$+ "Preview Window" +#DQUOTE$+ ", #PB_Text_Center)" +#CRLF$+
                          "  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow" +#CRLF$+
                          "EndIf"
  
  If OpenWindow(0, X, Y, Width, Height, "Demo Preview", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
    EditorGadget(0, 10, 10, 640, 220)
    SetGadgetText(0, SourceCode$)
    ButtonGadget(1, 10, 240, 640, 50, "Preview")
    
    BindEvent(#PB_Event_SizeWindow, @Resize_Window(), 0)
    ProcedureReturn #True
  EndIf
EndProcedure

If Open_Window()
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
        
      Case #PB_Event_ActivateWindow
        If PreviewRunning And IsProgram(PreviewRunning) And ProgramRunning(PreviewRunning)
          KillProgram(PreviewRunning)
          CloseProgram(PreviewRunning)
          Delay(50)
        EndIf
        If Not PreviewProgramName$ = "" And FileSize(PreviewProgramName$)
          DeleteFile(PreviewProgramName$)
        EndIf
        PreviewRunning      = 0
        PreviewProgramName$ = ""
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1   ; Preview
            CallPreview(GetGadgetText(0))
        EndSelect
        
    EndSelect
  ForEver
EndIf
Edit: add WriteStringFormat(hTempFile, #PB_UTF8)
Last edited by ChrisR on Thu Mar 06, 2025 5:16 pm, edited 1 time in total.
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: RunProgram whis text

Post by mestnyi »

AZJIO wrote: Wed Mar 05, 2025 10:14 am
with your example, a wheel is spinning over the cursor all the time. Why can it be?
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: RunProgram whis text

Post by ChrisR »

I thought it was for my code, I tried and couldn't understand.
I hadn't seen that AZJIO had published a code snippet and I don't have the wheel!
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: RunProgram whis text

Post by mestnyi »

ChrisR wrote: Wed Mar 05, 2025 7:24 pm I thought it was for my code, I tried and couldn't understand.
I hadn't seen that AZJIO had published a code snippet and I don't have the wheel!
Yours is working, I've already found it on githube SweetyVD :D
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: RunProgram whis text

Post by ChrisR »

Well done :)
Looks old now, it's probably better done in the example here
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: RunProgram whis text

Post by AZJIO »

mestnyi wrote: Wed Mar 05, 2025 6:57 pm
AZJIO wrote: Wed Mar 05, 2025 10:14 am
with your example, a wheel is spinning over the cursor all the time. Why can it be?
Because your code contains an error, it is trying to tell you about it, but since the program is hidden you cannot see this. I assume that the debugging mode could derive an error, but since there is nowhere to display, your process will either fall or will be in an endless cycle trying to understand why the pointer is absent.

I had an idea to read the process output, but I stopped myself. If we go further, you'll need code viewer, code highlighting and we'll come to the point where we'll write an IDE. And it already exists
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: RunProgram whis text

Post by mestnyi »

AZJIO wrote: Wed Mar 05, 2025 11:17 pm
mestnyi wrote: Wed Mar 05, 2025 6:57 pm
AZJIO wrote: Wed Mar 05, 2025 10:14 am
with your example, a wheel is spinning over the cursor all the time. Why can it be?
Because your code contains an error, it is trying to tell you about it, but since the program is hidden you cannot see this. I assume that the debugging mode could derive an error, but since there is nowhere to display, your process will either fall or will be in an endless cycle trying to understand why the pointer is absent.

I had an idea to read the process output, but I stopped myself. If we go further, you'll need code viewer, code highlighting and we'll come to the point where we'll write an IDE. And it already exists
What was that :D :D :D :D :D :D
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: RunProgram whis text

Post by mestnyi »

ChrisR wrote: Wed Mar 05, 2025 7:41 pm Well done :)
Looks old now, it's probably better done in the example here
the only thing without this line is text, hieroglyphs.

Code: Select all

WriteStringFormat(hTempFile, #PB_UTF8)
         
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: [done] RunProgram whis text

Post by ChrisR »

mestnyi wrote: Thu Mar 06, 2025 12:48 pm the only thing without this line is text, hieroglyphs.

Code: Select all

WriteStringFormat(hTempFile, #PB_UTF8)
It wasn't essential for the example here but you're right, it's better with it, I added it.
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: [done] RunProgram whis text

Post by mestnyi »

I would like to do the same for Linux and macos. How do I do this?
Post Reply