The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
5.1KB

  1. /*
  2. ==============================================================================
  3. OSCLogListBox.h
  4. Created: 13 Jun 2015 9:48:28pm
  5. Author: Timur Doumler
  6. ==============================================================================
  7. */
  8. #ifndef OSCLOGLISTBOX_H_INCLUDED
  9. #define OSCLOGLISTBOX_H_INCLUDED
  10. #include "../JuceLibraryCode/JuceHeader.h"
  11. //==============================================================================
  12. class OSCLogListBox : public ListBox, private ListBoxModel, private AsyncUpdater
  13. {
  14. public:
  15. //==============================================================================
  16. OSCLogListBox()
  17. {
  18. setModel (this);
  19. }
  20. //==============================================================================
  21. ~OSCLogListBox()
  22. {
  23. }
  24. //==============================================================================
  25. int getNumRows() override
  26. {
  27. return oscLogList.size();
  28. }
  29. //==============================================================================
  30. void paintListBoxItem (int row, Graphics& g, int width, int height, bool rowIsSelected) override
  31. {
  32. if (isPositiveAndBelow (row, oscLogList.size()))
  33. {
  34. g.setColour (Colours::black);
  35. g.drawText (oscLogList[row],
  36. Rectangle<int> (width, height).reduced (4, 0),
  37. Justification::centredLeft, true);
  38. }
  39. }
  40. //==============================================================================
  41. void addOSCMessage (const OSCMessage& message, int level = 0)
  42. {
  43. oscLogList.add (getIndentationString (level)
  44. + "- osc message, address = '"
  45. + message.getAddressPattern().toString()
  46. + "', "
  47. + String (message.size())
  48. + " argument(s)");
  49. if (! message.empty())
  50. {
  51. for (OSCArgument* arg = message.begin(); arg != message.end(); ++arg)
  52. addOSCMessageArgument (*arg, level + 1);
  53. }
  54. triggerAsyncUpdate();
  55. }
  56. //==============================================================================
  57. void addOSCBundle (const OSCBundle& bundle, int level = 0)
  58. {
  59. OSCTimeTag timeTag = bundle.getTimeTag();
  60. oscLogList.add (getIndentationString (level)
  61. + "- osc bundle, time tag = "
  62. + timeTag.toTime().toString (true, true, true, true));
  63. for (OSCBundle::Element* element = bundle.begin(); element != bundle.end(); ++element)
  64. {
  65. if (element->isMessage())
  66. addOSCMessage (element->getMessage(), level + 1);
  67. else if (element->isBundle())
  68. addOSCBundle (element->getBundle(), level + 1);
  69. }
  70. triggerAsyncUpdate();
  71. }
  72. //==============================================================================
  73. void addOSCMessageArgument (const OSCArgument& arg, int level)
  74. {
  75. String typeAsString;
  76. String valueAsString;
  77. if (arg.isFloat32())
  78. {
  79. typeAsString = "float32";
  80. valueAsString = String (arg.getFloat32());
  81. }
  82. else if (arg.isInt32())
  83. {
  84. typeAsString = "int32";
  85. valueAsString = String (arg.getInt32());
  86. }
  87. else if (arg.isString())
  88. {
  89. typeAsString = "string";
  90. valueAsString = arg.getString();
  91. }
  92. else if (arg.isBlob())
  93. {
  94. typeAsString = "blob";
  95. const MemoryBlock& blob = arg.getBlob();
  96. valueAsString = String::fromUTF8( (const char*)blob.getData(), blob.getSize());
  97. }
  98. else
  99. {
  100. typeAsString = "(unknown)";
  101. valueAsString = "";
  102. }
  103. oscLogList.add (getIndentationString (level + 1) + "- " + typeAsString.paddedRight(' ', 12) + valueAsString);
  104. }
  105. //==============================================================================
  106. void addInvalidOSCPacket (const char* /* data */, int dataSize)
  107. {
  108. oscLogList.add ("- (" + String(dataSize) + "bytes with invalid format)");
  109. }
  110. //==============================================================================
  111. void clear()
  112. {
  113. oscLogList.clear();
  114. triggerAsyncUpdate();
  115. }
  116. //==============================================================================
  117. void handleAsyncUpdate() override
  118. {
  119. updateContent();
  120. scrollToEnsureRowIsOnscreen (oscLogList.size() - 1);
  121. repaint();
  122. }
  123. private:
  124. //==============================================================================
  125. String getIndentationString (int level)
  126. {
  127. return String().paddedRight (' ', 2 * level);
  128. }
  129. //==============================================================================
  130. StringArray oscLogList;
  131. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OSCLogListBox)
  132. };
  133. #endif // OSCLOGLISTBOX_H_INCLUDED