GuiLabel.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* GuiLabel.cpp
  2. *
  3. * Copyright (C) 1993-2008,2010-2018 Paul Boersma, 2007 Stefan de Konink
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "GuiP.h"
  19. Thing_implement (GuiLabel, GuiControl, 0);
  20. #if gtk
  21. #define iam_label GuiLabel me = (GuiLabel) _GuiObject_getUserData (widget)
  22. #elif motif
  23. #define iam_label GuiLabel me = (GuiLabel) widget -> userData
  24. #elif cocoa
  25. #define iam_label GuiLabel me = (GuiLabel) [(GuiCocoaLabel *) widget userData];
  26. #endif
  27. #if gtk
  28. static void _GuiGtkLabel_destroyCallback (GuiObject widget, gpointer void_me) {
  29. (void) widget;
  30. iam (GuiLabel);
  31. forget (me);
  32. }
  33. #elif motif
  34. void _GuiWinLabel_destroy (GuiObject widget) {
  35. iam_label;
  36. _GuiNativeControl_destroy (widget);
  37. forget (me); // NOTE: my widget is not destroyed here
  38. }
  39. #elif cocoa
  40. @implementation GuiCocoaLabel {
  41. GuiLabel d_userData;
  42. }
  43. - (void) dealloc { // override
  44. GuiLabel me = d_userData;
  45. forget (me);
  46. trace (U"deleting a label");
  47. [super dealloc];
  48. }
  49. - (GuiThing) getUserData {
  50. return d_userData;
  51. }
  52. - (void) setUserData: (GuiThing) userData {
  53. Melder_assert (userData == nullptr || Thing_isa (userData, classGuiLabel));
  54. d_userData = static_cast <GuiLabel> (userData);
  55. }
  56. @end
  57. #endif
  58. GuiLabel GuiLabel_create (GuiForm parent, int left, int right, int top, int bottom,
  59. conststring32 labelText, uint32 flags)
  60. {
  61. autoGuiLabel me = Thing_new (GuiLabel);
  62. my d_shell = parent -> d_shell;
  63. my d_parent = parent;
  64. #if gtk
  65. my d_widget = gtk_label_new (Melder_peek32to8 (labelText));
  66. _GuiObject_setUserData (my d_widget, me.get());
  67. my v_positionInForm (my d_widget, left, right, top, bottom, parent);
  68. g_signal_connect (G_OBJECT (my d_widget), "destroy", G_CALLBACK (_GuiGtkLabel_destroyCallback), me.get());
  69. gtk_misc_set_alignment (GTK_MISC (my d_widget), flags & GuiLabel_RIGHT ? 1.0 : flags & GuiLabel_CENTRE ? 0.5 : 0.0, 0.5);
  70. #elif motif
  71. my d_widget = _Gui_initializeWidget (xmLabelWidgetClass, parent -> d_widget, labelText);
  72. _GuiObject_setUserData (my d_widget, me.get());
  73. my d_widget -> window = CreateWindow (L"static", Melder_peek32toW (_GuiWin_expandAmpersands (my d_widget -> name.get())),
  74. WS_CHILD
  75. | ( flags & GuiLabel_RIGHT ? SS_RIGHT : flags & GuiLabel_CENTRE ? SS_CENTER : SS_LEFT )
  76. | SS_CENTERIMAGE,
  77. my d_widget -> x, my d_widget -> y, my d_widget -> width, my d_widget -> height,
  78. my d_widget -> parent -> window, (HMENU) 1, theGui.instance, nullptr);
  79. SetWindowLongPtr (my d_widget -> window, GWLP_USERDATA, (LONG_PTR) my d_widget);
  80. SetWindowFont (my d_widget -> window, GetStockFont (ANSI_VAR_FONT), false);
  81. my v_positionInForm (my d_widget, left, right, top, bottom, parent);
  82. #elif cocoa
  83. trace (U"create");
  84. GuiCocoaLabel *label = [[GuiCocoaLabel alloc] init];
  85. my d_widget = label;
  86. trace (U"position");
  87. my v_positionInForm (my d_widget, left, right, top, bottom, parent);
  88. trace (U"set user data");
  89. [label setUserData: me.get()];
  90. trace (U"set bezel style");
  91. [label setBezelStyle: NSTextFieldRoundedBezel];
  92. trace (U"set bordered");
  93. [label setBordered: NO];
  94. trace (U"set selectable");
  95. [label setSelectable: NO];
  96. trace (U"title");
  97. [label setTitleWithMnemonic: (NSString *) Melder_peek32toCfstring (labelText)];
  98. [label setAlignment:( flags & GuiLabel_RIGHT ? NSRightTextAlignment : flags & GuiLabel_CENTRE ? NSCenterTextAlignment : NSLeftTextAlignment )];
  99. static NSFont *theLabelFont;
  100. if (! theLabelFont) {
  101. theLabelFont = [NSFont systemFontOfSize: 13.0];
  102. }
  103. [label setFont: theLabelFont];
  104. #endif
  105. return me.releaseToAmbiguousOwner();
  106. }
  107. GuiLabel GuiLabel_createShown (GuiForm parent, int left, int right, int top, int bottom,
  108. conststring32 labelText, uint32 flags)
  109. {
  110. GuiLabel me = GuiLabel_create (parent, left, right, top, bottom, labelText, flags);
  111. GuiThing_show (me);
  112. return me;
  113. }
  114. void GuiLabel_setText (GuiLabel me, conststring32 text /* cattable */) {
  115. #if gtk
  116. gtk_label_set_text (GTK_LABEL (my d_widget), Melder_peek32to8 (text));
  117. #elif motif
  118. my d_widget -> name = Melder_dup_f (text);
  119. _GuiNativeControl_setTitle (my d_widget);
  120. #elif cocoa
  121. [(NSTextField *) my d_widget setTitleWithMnemonic: (NSString *) Melder_peek32toCfstring (text)];
  122. #endif
  123. }
  124. /* End of file GuiLabel.cpp */