[REVISED] Add Notes Into The IDE

Share your advanced PureBasic knowledge/code with the community.
Randy Walker
Addict
Addict
Posts: 1026
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

[REVISED] Add Notes Into The IDE

Post by Randy Walker »

I wanted to have a convenient place to add notes in my IDE so I compiled the following and added it under [Tools] > [Configure Tools].
Very convenient now.
You can of course test it without adding it to your tools list. Either way, it Will create a file in your %appdata%\Purebasic\ folder.

Code: Select all

Global _mess, fontAri11.i, PBNoteFile$, Kreturn.W, PBNoteFile$
fontAri11.i = LoadFont(#PB_Default,"Arial",11)
;fontAri11B.i = LoadFont(#PB_Default,"Arial",11,#PB_Font_Bold)
Procedure AppActivate(handle,title$)
  If title$ <> "" : handle=FindWindow_(0,title$) : EndIf
  thread1=GetWindowThreadProcessId_(GetForegroundWindow_(),0)
  thread2=GetWindowThreadProcessId_(handle,0)
  If thread1thread2 : AttachThreadInput_(thread1,thread2,#True) : EndIf
  BringWindowToTop_(handle)
  SetForegroundWindow_(handle)
  If thread1thread2 : AttachThreadInput_(thread1,thread2,#False) : EndIf
  ProcedureReturn IsWindow_(handle)
EndProcedure
If AppActivate(0,"SAFE NOTE w/autosave")
  End ;Quit ourt here if already ryunning
EndIf
#QuickNote = 1
Procedure savenote()
  If CreateFile(4,PBNoteFile$)
    s$ = Trim(GetGadgetText(#QuickNote))
    WriteString(4, s$)
    CloseFile(4)
  Else
    MessageRequester("Failed","Cannot open "+PBNoteFile$)
  EndIf
EndProcedure  

#KB_Return = #PB_Shortcut_Return + 49152
#KB_Escape = #PB_Shortcut_Escape + 49152
#Window_16 = 1
#QuickNote = 1
s$ = GetEnvironmentVariable("APPDATA") +"\PureBasic\"
Debug s$
;s$ = "%AppData%\Purebasic"
If SetCurrentDirectory(s$)
  PBNoteFile$ = s$ + "PB_QUICK_NOTE.TXT"
Else
  MessageRequester("Bad User Path","Cannot determine path to user directory.",0)
  End ; Cannot determine user so bail launch
EndIf
hWnd16 = OpenWindow(1, 100,100,310,510, "SAFE NOTE w/autosave", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_TitleBar)
If hWnd16
  SetGadgetFont(#PB_Default,FontID(fontAri11.i))
  EditorGadget(#QuickNote, 5, 5, 270, 500) ; AddKeyboardShortcut(1,#PB_Shortcut_Return,2)
  SendMessage_(GadgetID(#QuickNote), #EM_SETTARGETDEVICE, #Null, 0)                 ; 0=wrap , 1up=linewidth , $FFFFFF(effectively)=wrapoff
  ;AddKeyboardShortcut(1, #PB_Shortcut_Escape, #KB_Escape)
  If FileSize(PBNoteFile$) > 0
    ;Debug PBNoteFile$
    If ReadFile(4,PBNoteFile$)
      ;UseFile(4)
      Repeat
        s$ = ReadString(4,#PB_Ascii)
        AddGadgetItem(#QuickNote,-1,s$)
      Until Eof(4)
      CloseFile(4)
    Else
      MessageRequester("Failed","Cannot open "+PBNoteFile$)
    EndIf 
  EndIf 
  If GetFocus_() <> GadgetID(#QuickNote)
    SetActiveGadget(#QuickNote)
  EndIf
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1
        
      Case #WM_CHAR
        key = EventwParam()
        Select key
          Case 13
            savenote()
          Case 27
            Quit = 1
        EndSelect
    EndSelect
  Until Quit = 1
savenote()  
EndIf
I hope someone will find it useful. It saves automatically when you close the window.
Last edited by Randy Walker on Wed Aug 20, 2025 9:08 pm, edited 2 times in total.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Axolotl
Addict
Addict
Posts: 824
Joined: Wed Dec 31, 2008 3:36 pm

Re: Add Notes Into The IDE

Post by Axolotl »

Thanks for sharing.

BTW: I think there is a problem with this lines here ....

Code: Select all

If thread1thread2 : AttachThreadInput_(thread1,thread2,#True) : EndIf 
;....
If thread1thread2 : AttachThreadInput_(thread1,thread2,#False) : EndIf
Probably it should be like this.

Code: Select all

If thread1 <> thread2 : AttachThreadInput_(thread1,thread2,#True) : EndIf
;....
If thread1 <> thread2 : AttachThreadInput_(thread1,thread2,#False) : EndIf


And on that note it is very helpful to use this command at the top of your codes.

Code: Select all

EnableExpicit
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Randy Walker
Addict
Addict
Posts: 1026
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Add Notes Into The IDE

Post by Randy Walker »

Axolotl wrote: Wed Aug 20, 2025 2:51 pm Thanks for sharing.

BTW: I think there is a problem with this lines here ....

Code: Select all

If thread1thread2 : AttachThreadInput_(thread1,thread2,#True) : EndIf 
;....
If thread1thread2 : AttachThreadInput_(thread1,thread2,#False) : EndIf
Probably it should be like this.

Code: Select all

If thread1 <> thread2 : AttachThreadInput_(thread1,thread2,#True) : EndIf
;....
If thread1 <> thread2 : AttachThreadInput_(thread1,thread2,#False) : EndIf


And on that note it is very helpful to use this command at the top of your codes.

Code: Select all

EnableExpicit
Interesting observation, however after new review, this code serves no real purpose. No idea how it got in there, or why it didn't error on that window 0 that had not been initialized.

Code: Select all

Procedure AppActivate(handle,title$)
  If title$ <> "" : handle=FindWindow_(0,title$) : EndIf
  thread1=GetWindowThreadProcessId_(GetForegroundWindow_(),0)
  thread2=GetWindowThreadProcessId_(handle,0)
  If thread1thread2 : AttachThreadInput_(thread1,thread2,#True) : EndIf
  SetForegroundWindow_(handle)
  If thread1thread2 : AttachThreadInput_(thread1,thread2,#False) : EndIf
  ProcedureReturn IsWindow_(handle)
EndProcedure
If AppActivate(0,"SAFE NOTE w/autosave")
  End ;Quit ourt here if already ryunning
EndIf
Just discard that block of code and it still performs the same.
Last edited by Randy Walker on Wed Aug 20, 2025 8:55 pm, edited 1 time in total.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1026
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Add Notes Into The IDE

Post by Randy Walker »

Revised code with goofy block removed. Compile should be smaller now too.

Code: Select all

Global _mess, fontAri11.i, PBNoteFile$, Kreturn.W, PBNoteFile$
fontAri11.i = LoadFont(#PB_Default,"Arial",11)
;fontAri11B.i = LoadFont(#PB_Default,"Arial",11,#PB_Font_Bold)
#QuickNote = 1
Procedure savenote()
  If CreateFile(4,PBNoteFile$)
    s$ = Trim(GetGadgetText(#QuickNote))
    WriteString(4, s$)
    CloseFile(4)
  Else
    MessageRequester("Failed","Cannot open "+PBNoteFile$)
  EndIf
EndProcedure  

#KB_Return = #PB_Shortcut_Return + 49152
#KB_Escape = #PB_Shortcut_Escape + 49152
#Window_16 = 1
#QuickNote = 1
s$ = GetEnvironmentVariable("APPDATA") +"\PureBasic\"
Debug s$
;s$ = "%AppData%\Purebasic"
If SetCurrentDirectory(s$)
  PBNoteFile$ = s$ + "PB_QUICK_NOTE.TXT"
Else
  MessageRequester("Bad User Path","Cannot determine path to user directory.",0)
  End ; Cannot determine user so bail launch
EndIf
hWnd16 = OpenWindow(1, 100,100,310,510, "SAFE NOTE w/autosave", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_TitleBar)
If hWnd16
  SetGadgetFont(#PB_Default,FontID(fontAri11.i))
  EditorGadget(#QuickNote, 5, 5, 270, 500) ; AddKeyboardShortcut(1,#PB_Shortcut_Return,2)
  SendMessage_(GadgetID(#QuickNote), #EM_SETTARGETDEVICE, #Null, 0)                 ; 0=wrap , 1up=linewidth , $FFFFFF(effectively)=wrapoff
  ;AddKeyboardShortcut(1, #PB_Shortcut_Escape, #KB_Escape)
  If FileSize(PBNoteFile$) > 0
    ;Debug PBNoteFile$
    If ReadFile(4,PBNoteFile$)
      ;UseFile(4)
      Repeat
        s$ = ReadString(4,#PB_Ascii)
        AddGadgetItem(#QuickNote,-1,s$)
      Until Eof(4)
      CloseFile(4)
    Else
      MessageRequester("Failed","Cannot open "+PBNoteFile$)
    EndIf 
  EndIf 
  If GetFocus_() <> GadgetID(#QuickNote)
    SetActiveGadget(#QuickNote)
  EndIf
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1
        
      Case #WM_CHAR
        key = EventwParam()
        Select key
          Case 13
            savenote()
          Case 27
            Quit = 1
        EndSelect
    EndSelect
  Until Quit = 1
savenote()  
EndIf
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1026
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: [REVISED] Add Notes Into The IDE

Post by Randy Walker »

OHHhh NOOooo, Wait!!! I see now. That block of code was there to prevent more than one instance. It should be there. And you were rihht about those two lines. Also needed to add bringWindowToTop line at seen in revised OP.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Axolotl
Addict
Addict
Posts: 824
Joined: Wed Dec 31, 2008 3:36 pm

Re: [REVISED] Add Notes Into The IDE

Post by Axolotl »

Well, on my system the AppActivate() is not working as expected.
I change it with some out of my collection.
Then, unfortunately, I got stuck in the programming tunnel. Now the code is so bloated that nothing is the same anymore. :oops:
So here the one procedure (and some needed constants) I replaced the AppActivate() proc with.

Code: Select all

#ProgramCaption$        = "SAFE NOTE w/autosave"    ; ?? 
#ProgramInstance$       = "Mx" + #ProgramCaption$   ; make a very precise mutex name 

Enumeration EWindow 1
  #WND_Main 
EndEnumeration 

#ProgramClassName$ = "WindowClass_" + #WND_Main  ; <= build the correct classname of the Application Main Window !!! 

Procedure SingleApplicationInstance()  ; VOID 
  Protected hwndPrevInst, hMutex 

  ; is a instance already running? 
  ;
  hMutex = CreateMutex_(#Null, 1, #ProgramInstance$)  
  If hMutex <> 0 And GetLastError_() = #ERROR_ALREADY_EXISTS 
    CloseHandle_(hMutex) 

    ; look for the window handle of the related window ..... 
    ;
    hwndPrevInst = FindWindowEx_(GetDesktopWindow_(), 0, #ProgramClassName$, #ProgramCaption$) 

    If hwndPrevInst ; <> 0 
      ; activate the previous running application instance 
      ;
      SendMessage_(hwndPrevInst, #SW_SHOWNORMAL, 0, 0) 
      ShowWindow_(hwndPrevInst,  #SW_RESTORE) 
      ShowWindow_(hwndPrevInst,  #SW_SHOW) 
      SetForegroundWindow_(hwndPrevInst) 

      ; Terminate this application now 
      ;
      End  ; bye, without any comments :) 
    EndIf 
  EndIf 
EndProcedure 

; call it to make sure the previous instance is activated. 
SingleApplicationInstance() 

Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Post Reply