[REVISED] Add Notes Into The IDE

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

[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 thread1 <> thread2 : AttachThreadInput_(thread1,thread2,#True) : EndIf
  BringWindowToTop_(handle)
  SetForegroundWindow_(handle)
  If thread1 <> thread2 : 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 Sun Aug 24, 2025 9:58 pm, edited 3 times in total.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Axolotl
Addict
Addict
Posts: 897
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: 1170
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA
Contact:

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: 1170
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA
Contact:

Re: Add Notes Into The IDE

Post by Randy Walker »

Revised code with goofy block removed. Compile should be smaller now too. << This was a stupid error

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
Last edited by Randy Walker on Sun Aug 24, 2025 10:00 pm, edited 1 time in total.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1170
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA
Contact:

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 right about those two lines. Also needed to add bringWindowToTop line as seen in revised OP. (The OP is properly revised.)
Last edited by Randy Walker on Sun Aug 24, 2025 10:02 pm, edited 1 time in total.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Axolotl
Addict
Addict
Posts: 897
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).
Randy Walker
Addict
Addict
Posts: 1170
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA
Contact:

Re: [REVISED] Add Notes Into The IDE

Post by Randy Walker »

Supercharged revision posted below. First i want to make clear... This is really intended more for general purpose -- not as a tool to be added to the IDE, but could be with some small tweaks. It will create and write to the new %appdata%\MyNotes folder instead of using the PureBasic folder. It provides 4 separate tabs to allow you to categorize your notes. Same auto save feature as before and even remembers the active tab when you closed last:

Code: Select all

Global _mess, fontAri11.i, MyNoteFile$, Kreturn.W, activeGadget
;{- Enumerations / DataSections
;{ Windows
Enumeration
  #Window_0
EndEnumeration
;}
;{ Gadgets
Enumeration
  #Tab_0
  #Tab_1
  #Tab_2
  #Tab_3
  #Panel_1
EndEnumeration
;}
Define.l Event, EventWindow, EventGadget, EventType, EventMenu
;}
buffer$ = Space(512)
fontAri11.i = LoadFont(#PB_Default,"Arial",11)
e$="AppData"
If GetEnvironmentVariable_(e$,@buffer$,512)
  s$ = buffer$
EndIf
s$ = buffer$+"\MyNotes"
If FileSize(s$) = -1
  SetCurrentDirectory(buffer$)
  CreateDirectory("MyNotes")
EndIf
If FileSize(s$) = -2
  MyNoteFile$ = s$ + "\My_SAFE_Note"
EndIf

Procedure.i pseudoWait()
  Protected void.i, bail.i
  Repeat
    void.i = WindowEvent() ; Global 'Event' and other variables listed here
    If void
      Break
    EndIf
    Delay(5)
    bail + 1
    If bail > 100
      Break
    EndIf
  Until void <> 0
  ;GadgetId = EventGadget():EventType = EventType()
  ; \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/
  ProcedureReturn void
EndProcedure

Procedure OpenWindow_Window_0()
  If OpenWindow(#Window_0, 395, 226, 283, 400, "Window_0", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
    PanelGadget(#Panel_1, 5, 0, 240, 395)
      ; Tab #0
    AddGadgetItem(#Panel_1, -1, "To Do")
    EditorGadget(#Tab_0,0,0,240,395)
      ; Tab #1
      AddGadgetItem(#Panel_1, -1, "Home")
    EditorGadget(#Tab_1,0,0,240,395)
      ; Tab #2
      AddGadgetItem(#Panel_1, -1, "Work")
    EditorGadget(#Tab_2,0,0,240,395)
      ; Tab #3
      AddGadgetItem(#Panel_1, -1, "Misc")
    EditorGadget(#Tab_3,0,0,240,395)
    CloseGadgetList()
;     SetGadgetState(#Panel_1,2)
;     SetActiveGadget(#Tab_2)
    For tab = 0 To 3
      file$ = MyNoteFile$+Str(tab)+".txt"
      Debug " LOADing "+file$
      If FileSize(file$) > 0
        Debug File$
        If OpenFile(4,file$)
          activeGadget = Val(ReadString(4))
                Repeat
                  s$ = ReadString(4,#PB_Ascii)
                  ;SetGadgetText(tab,s$)
                  AddGadgetItem(tab,-1,s$)
                Until Eof(4)
                CloseFile(4)
              Else
                MessageRequester("Failed","Cannot open "+Chr(10)+file$)
              EndIf 
            EndIf 
            bottom = Len(GetGadgetText(tab))
            SendMessage_(GadgetID(tab), #EM_SETSEL, bottom, bottom)  ; Set cursor position back.
    Next tab
    SetGadgetState(#Panel_1,activeGadget)
    SetActiveGadget(activeGadget)
  EndIf
EndProcedure
OpenWindow_Window_0()
Procedure savenote(gadget)
;   Debug " saving to "+file$
;   Debug "Get Gadget " +Str(gadget)+" text"
  activeGadget = GetGadgetState(#Panel_1)
  Debug activeGadget
  s$ = Trim(GetGadgetText(gadget))
  file$ = MyNoteFile$+Str(gadget)+".txt"
  t$ = Str(activeGadget)+" is active tab"
  If CreateFile(4,File$) And s$ <> ""
    WriteStringN(4,t$)
    Debug "Write is next"
    WriteStringN(4, s$)
    Debug s$
    CloseFile(4)
  Else
    MessageRequester("Failed","Cannot open "+MyNoteFile$)
  EndIf
EndProcedure  

;{- Event loop
Repeat
  Event = WaitWindowEvent()
  activeGadget = GetGadgetState(#Panel_1)
  Select Event
      Case #PB_Event_CloseWindow
        Quit = 1
        
      Case #WM_CHAR
        key = EventwParam()
        Select key
          Case 13
          tab = GetGadgetState(#Panel_1)
          savenote(tab)
          Case 27
            Repeat
              _mess = pseudoWait():_wParam.w=EventwParam():_lParam.l=EventlParam():GadgetId=EventGadget():MenuId=EventMenu():EventType=EventType();:WindowID=EventWindow()
            Until _mess = #WM_KEYUP
            Quit = 1
        EndSelect
    ; ///////////////////
    Case #PB_Event_Gadget
      EventGadget = EventGadget()
      EventType = EventType()
      Select EventGadget
        Case #Panel_1
          activeGadget = GetGadgetState(#Panel_1)
            SetActiveGadget(activeGadget)
      EndSelect
    ; ////////////////////////
    Case #PB_Event_CloseWindow
      EventWindow = EventWindow()
      If EventWindow = #Window_0
        CloseWindow(#Window_0)
        Break
      EndIf
  EndSelect
Until Event = #PB_Event_CloseWindow Or quit = 1
For tab = 0 To 3
  savenote(tab)
Next tab
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Post Reply