Foreground Fileview [SOLVED]

New to Gambas? Post your questions here. No question is too silly or too simple.
toto96
Posts: 8
Joined: Monday 24th April 2023 5:23pm

Foreground Fileview [SOLVED]

Post by toto96 »

Hello,

I am using a fileview and I would need to have a different foreground color, for example, if the file name contains "Picture056".
Is this possible? :?:
exemple.jpg
exemple.jpg (35.5 KiB) Viewed 1494 times
Last edited by toto96 on Thursday 27th April 2023 7:04am, edited 1 time in total.
User avatar
cogier
Site Admin
Posts: 1125
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Foreground Fileview

Post by cogier »

Hi toto96 and welcome to the forum.

Can you tell us exactly what you are trying to achieve. I can't find a setting to change a background of a single FileView icon, but there may well be another way.
toto96
Posts: 8
Joined: Monday 24th April 2023 5:23pm

Re: Foreground Fileview

Post by toto96 »

Hi :)

I would like to change the font color of one or several items in the fileview.

For my example:

In my application, I save a "note" on an image and I would like the text color of the element to change. I can do it with the foreground parameter, but it modifies everything (here in red).
Sorry for my English - I am French.


 With fileview
    .MaxPreviewSize = -1
    .ShowPreview = True
    .IconSize = 192
    .filter = extension_fichier_image
    .Drop = True
  End With


 FILEVIEW.Foreground = Color.Red
Attachments
application.jpg
application.jpg (171.6 KiB) Viewed 1433 times
User avatar
BruceSteers
Posts: 1565
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Foreground Fileview

Post by BruceSteers »

You can do it with IconView

IconView has Keys and the items have a .RichText property so you can set <font color=red>.

FileView uses an IconView to show it's contents so you can access it like this...

Public Sub Make_b_Red()

  Dim s As String

  ' get the IconView  control inside the FileView
  Dim p As Panel = FileView1.Children[0]
  Dim IView As IconView = p.Children[0]

' if file name has a b in it then set its text red
  For Each s In IView.Keys
    If  IView[s].Text Like "*b*" Then
      IView[s].RichText = "<font color=red>" & IView[s].Text & "</font>"
    Endif
  Next

End


That will show every file with a b in it as red text.
If at first you don't succeed , try doing something differently.
BruceS
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Foreground Fileview

Post by vuott »

BruceSteers wrote: Wednesday 26th April 2023 12:11amFileView uses an IconView to show it's contents so you can access it like this...

' get the IconView  control inside the FileView
  Dim p As Panel = FileView1.Children[0]
  Dim IView As IconView = p.Children[0]
Bravo ! 👍
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
BruceSteers
Posts: 1565
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Foreground Fileview

Post by BruceSteers »

vuott wrote: Wednesday 26th April 2023 4:44am
BruceSteers wrote: Wednesday 26th April 2023 12:11amFileView uses an IconView to show it's contents so you can access it like this...

' get the IconView  control inside the FileView
  Dim p As Panel = FileView1.Children[0]
  Dim IView As IconView = p.Children[0]
Bravo ! 👍
Why thank you kind sir :)

So let's take it a step further...
The above code only works if the FileView is not in "Detailed" view mode as detailed mode uses a ColumnView not an IconView.

So below is the test code I just made
The Form has a FileView a TextBox and a ComboBox
The combobox changes the View mode
With the TextBox you can provide a pcre text pattern and names matching the pattern will show red. Ie. IMG_*
it will work in all FileView View modes


Public Sub Form_Open()

  FileView1.Dir = User.Home ' load Home dir in FileView
  ComboBox1.List = ["Normal", "Compact", "Detailed", "Preview"]
  ComboBox1.Index = FileView1.View

End

Public Sub ComboBox1_Click()
  
  If FileView1.View = Last.Index Then Return

  FileView1.View = Last.Index
  MakeFileViewTextRed(TextBox1.Text)

End

Public Sub TextBox1_Activate()

  MakeFileViewTextRed(TextBox1.Text)

End


' Make FileView1 text red or not depending on the pcre Pattern
Public Sub MakeFileViewTextRed(Pattern As String)

  Dim s As String

  Dim p As Panel = FileView1.Children[0]
  Dim IView As IconView = p.Children[0]
  Dim CView As ColumnView = p.Children[1]

  If FileView1.View <> FileView.Detailed Then

    For Each s In IView.Keys
      If IView[s].Text Like Pattern Then
        IView[s].RichText = "<font color=red>" & IView[s].Text & "</font>"
      Else
        IView[s].RichText = IView[s].Text
      Endif
    Next
    IView.Refresh

  Else

    For Each s In CView.Keys
      If CView[s].Text Like Pattern Then
        CView[s].Foreground = Color.Red
      Else
        CView[s].Foreground = Color.Default
      Endif
    Next
    CView.Refresh

  Endif

End

If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1565
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Foreground Fileview

Post by BruceSteers »

cogier wrote: Tuesday 25th April 2023 3:23pm Hi toto96 and welcome to the forum.

Can you tell us exactly what you are trying to achieve. I can't find a setting to change a background of a single FileView icon, but there may well be another way.
With Gambas there is very often another way :)

Let me explain how i figured out the above posted method...

1. I added this simple test code to my Form_Open
  
  Print FileView1.Children[0]  ' this was Panel 0x56283a42d348)
  Print FileView1.Children[1]  ' this was TextLabel 0x55a579d76898)
  Print FileView1.Children[2]  ' this gave a Null object error

So then from that i knew i wanted the Panel at FileView1.Children[0]

2. So I then tried this code..
  Dim p As Panel = FileView1.Children[0]
  Print p.Children[0]  '  bingo, that was (IconView 0x562b09c0a9f8)
  Print p.Children[1]  '  that was (ColumnView 0x55f1134bcb38) 

That was all I needed to know.
I have access to the IconView and the ColumnView of the FileView control and can use their properties for customization.

I use this method a lot to find the way to private controls within controls for tweaks.

Happy coding :)
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1565
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Foreground Fileview

Post by BruceSteers »

toto96 wrote: Tuesday 25th April 2023 5:29pm Hi :)

I would like to change the font color of one or several items in the fileview.

For my example:

In my application, I save a "note" on an image and I would like the text color of the element to change. I can do it with the foreground parameter, but it modifies everything (here in red).
Sorry for my English - I am French.


 With fileview
    .MaxPreviewSize = -1
    .ShowPreview = True
    .IconSize = 192
    .filter = extension_fichier_image
    .Drop = True
  End With


 FILEVIEW.Foreground = Color.Red
You should be able to use the above method to access the IconView inside the FileView and set individual items of choice.

But watch out for the Key names with the IconView not quite matching the text. (mine were prefixed with 1)
Ie.
If the file name is IMG_4591.jpg
The ColumView Key name was CView["IMG_4591.jpg"]
The IconView Key name was IView["1IMG_4591.jpg"]

Best of luck :)
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1565
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Foreground Fileview

Post by BruceSteers »

So finally..

Here is code that sets individual items.
it uses a String[] array called $aMarkedFiles
Any items added to this list show red while others show default colour

I've attached the project so you can test it out.
it has 2 buttons "Mark file" and "Unmark file" that toggle items having red text for you.

Hopefully you can use it for your needs :)
FileViewRedItems-0.0.tar.gz
(12.94 KiB) Downloaded 100 times

' Gambas class file

Private $aMarkedFiles As New String[]

Public Sub Form_Open()

  FileView1.Dir = User.Home
  ComboBox1.List = ["Normal", "Compact", "Detailed", "Preview"]
  ComboBox1.Index = FileView1.View

End

Public Sub ComboBox1_Click()
  
  If FileView1.View = Last.Index Then Return
  FileView1.View = Last.Index
  Wait 0.1  ' give the FileView a moment to refresh it's contents
  MakeFileViewTextRed

End

' go through the FileView contents setting any items that are in the $aMarkedFiles array to red
Public Sub MakeFileViewTextRed()

  Dim s As String

  Dim p As Panel = FileView1.Children[0]
  Dim IView As IconView = p.Children[0]
  Dim CView As ColumnView = p.Children[1]

  If FileView1.View <> FileView.Detailed Then

    For Each s In IView.Keys
      If $aMarkedFiles.Exist(IView[s].Text) Then
        IView[s].RichText = "<font color=red>" & IView[s].Text & "</font>"
      Else
        IView[s].RichText = IView[s].Text
      Endif
    Next
    IView.Refresh

  Else

    For Each s In CView.Keys
      If $aMarkedFiles.Exist(CView[s].Text) Then
        CView[s].Foreground = Color.Red
      Else
        CView[s].Foreground = Color.Default
      Endif
    Next
    CView.Refresh
  Endif
  FileView1_Click
  
End

Public Sub btnMark_Click()

  If Not FileView1.Current Then Return
  $aMarkedFiles.Add(FileView1.Current)
  MakeFileViewTextRed
  
End

Public Sub btnUnmark_Click()

  If Not FileView1.Current Then Return
  $aMarkedFiles.Remove($aMarkedFiles.Find(FileView1.Current))
  MakeFileViewTextRed

End

Public Sub FileView1_Click() ' enable/disable the Mark/Unmark buttons depending if item is marked

  If Not FileView1.Current Then Return
  Dim bMarked As Boolean = $aMarkedFiles.Exist(FileView1.Current)
  btnMark.Enabled = Not bMarked
  btnUnmark.Enabled = bMarked
  
End

If at first you don't succeed , try doing something differently.
BruceS
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Foreground Fileview

Post by vuott »

No files appear in the FileView Control.
Why ?
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
Post Reply