| 1 |
/* GuiList.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1993-2020,2022,2024,2025 Paul Boersma, 2013 Tom Naughton |
| 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 3 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 |
|
| 19 |
#include "GuiP.h" |
| 20 |
|
| 21 |
Thing_implement (GuiList, GuiControl, 0); |
| 22 |
|
| 23 |
#if motif |
| 24 |
#define iam_list \ |
| 25 |
Melder_assert (widget -> widgetClass == xmListWidgetClass); \ |
| 26 |
GuiList me = (GuiList) widget -> userData |
| 27 |
#endif |
| 28 |
|
| 29 |
#if gtk |
| 30 |
static void _GuiGtkList_destroyCallback (gpointer void_me) { |
| 31 |
iam (GuiList); |
| 32 |
forget (me); |
| 33 |
} |
| 34 |
static void _GuiGtkList_selectionChangedCallback (GtkTreeSelection *sel, gpointer void_me) { |
| 35 |
iam (GuiList); |
| 36 |
if (my d_selectionChangedCallback && ! my d_blockValueChangedCallbacks) { |
| 37 |
trace (U"Selection changed."); |
| 38 |
struct structGuiList_SelectionChangedEvent event { me }; |
| 39 |
my d_selectionChangedCallback (my d_selectionChangedBoss, & event); |
| 40 |
} |
| 41 |
} |
| 42 |
#elif motif |
| 43 |
void _GuiWinList_destroy (GuiObject widget) { |
| 44 |
iam_list; |
| 45 |
DestroyWindow (widget -> window); |
| 46 |
forget (me); // NOTE: my widget is not destroyed here |
| 47 |
} |
| 48 |
void _GuiWinList_map (GuiObject widget) { |
| 49 |
iam_list; |
| 50 |
ShowWindow (widget -> window, SW_SHOW); |
| 51 |
} |
| 52 |
void _GuiWinList_handleClick (GuiObject widget) { |
| 53 |
iam_list; |
| 54 |
if (my d_selectionChangedCallback) { |
| 55 |
struct structGuiList_SelectionChangedEvent event { me }; |
| 56 |
my d_selectionChangedCallback (my d_selectionChangedBoss, & event); |
| 57 |
} |
| 58 |
} |
| 59 |
#elif cocoa |
| 60 |
@implementation GuiCocoaList { |
| 61 |
GuiList userData; |
| 62 |
@public NSMutableArray *contents; |
| 63 |
@public NSTableView *tableView; |
| 64 |
} |
| 65 |
|
| 66 |
/* |
| 67 |
* Override NSObject methods. |
| 68 |
*/ |
| 69 |
- (void) dealloc { |
| 70 |
[self -> contents release]; |
| 71 |
GuiThing me = self -> userData; |
| 72 |
forget (me); |
| 73 |
trace (U"deleting a list"); |
| 74 |
[super dealloc]; |
| 75 |
} |
| 76 |
|
| 77 |
/* |
| 78 |
* Override NSView methods. |
| 79 |
*/ |
| 80 |
- (id) initWithFrame: (NSRect) frameRect { |
| 81 |
self = [super initWithFrame: frameRect]; |
| 82 |
if (self) { |
| 83 |
self -> tableView = [[NSTableView alloc] initWithFrame: frameRect]; |
| 84 |
trace (U"Retain count of table view after creation: ", [self -> tableView retainCount]); // probably 1 |
| 85 |
NSTableColumn *tableColumn = [[NSTableColumn alloc] initWithIdentifier: @"list"]; |
| 86 |
tableColumn. width = frameRect. size. width; |
| 87 |
[tableColumn setEditable: NO]; |
| 88 |
[self -> tableView addTableColumn: tableColumn]; |
| 89 |
trace (U"Retain count of table view after adding one column: ", [self -> tableView retainCount]); // probably 1 |
| 90 |
|
| 91 |
self -> tableView. delegate = self; |
| 92 |
self -> tableView. dataSource = self; |
| 93 |
self -> tableView. allowsEmptySelection = YES; |
| 94 |
self -> tableView. headerView = nil; |
| 95 |
self -> tableView. target = self; |
| 96 |
self -> tableView. action = @selector (_GuiCocoaList_clicked:); |
| 97 |
|
| 98 |
NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame: frameRect]; |
| 99 |
[scrollView setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; |
| 100 |
[scrollView setBorderType: NSBezelBorder]; |
| 101 |
trace (U"Retain count of table view before setting as document view: ", [self -> tableView retainCount]); // probably 1 |
| 102 |
[scrollView setDocumentView: self -> tableView]; // this retains the table view |
| 103 |
trace (U"Retain count of table view after setting as document view: ", [self -> tableView retainCount]); // probably 3 |
| 104 |
[scrollView setHasVerticalScroller: YES]; |
| 105 |
//[scrollView setHasHorizontalScroller: YES]; |
| 106 |
|
| 107 |
trace (U"Retain count of scroll view before adding as subview: ", [scrollView retainCount]); // probably 1 |
| 108 |
[self addSubview: scrollView]; // this retains the scroll view |
| 109 |
trace (U"Retain count of scroll view after adding as subview: ", [scrollView retainCount]); // probably 3 |
| 110 |
|
| 111 |
trace (U"Retain count of scroll view before release: ", [scrollView retainCount]); // probably 3 |
| 112 |
[scrollView release]; // compensates for alloc |
| 113 |
trace (U"Retain count of table view before release: ", [self -> tableView retainCount]); // probably 3 |
| 114 |
[self -> tableView release]; // compensates for alloc |
| 115 |
|
| 116 |
self -> contents = [[NSMutableArray alloc] init]; |
| 117 |
} |
| 118 |
return self; |
| 119 |
} |
| 120 |
|
| 121 |
/* |
| 122 |
Implement GuiCocoaAny protocol. |
| 123 |
*/ |
| 124 |
- (GuiThing) getUserData { |
| 125 |
return self -> userData; |
| 126 |
} |
| 127 |
- (void) setUserData: (GuiThing) newUserData { |
| 128 |
Melder_assert (newUserData == nullptr || Thing_isa (newUserData, classGuiList)); |
| 129 |
self -> userData = static_cast <GuiList> (newUserData); |
| 130 |
} |
| 131 |
|
| 132 |
/* |
| 133 |
Implement GuiCocoaList methods. |
| 134 |
*/ |
| 135 |
- (IBAction) _GuiCocoaList_clicked: (id) sender { |
| 136 |
/* |
| 137 |
This method probably shouldn't do anything, |
| 138 |
because tableViewSelectionDidChange will already have been called at this point. |
| 139 |
*/ |
| 140 |
(void) sender; |
| 141 |
trace (U"enter"); |
| 142 |
GuiList me = self -> userData; |
| 143 |
if (me && my d_selectionChangedCallback) { |
| 144 |
//struct structGuiList_SelectionChangedEvent event { me }; |
| 145 |
//my d_selectionChangedCallback (my d_selectionChangedBoss, & event); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/* |
| 150 |
Override TableViewDataSource methods. |
| 151 |
*/ |
| 152 |
- (NSInteger) numberOfRowsInTableView: (NSTableView *) aTableView { |
| 153 |
(void) aTableView; |
| 154 |
return uinteger_to_integer_a ([self -> contents count]); |
| 155 |
} |
| 156 |
- (id) tableView: (NSTableView *) aTableView objectValueForTableColumn: (NSTableColumn *) aTableColumn row: (NSInteger) row { |
| 157 |
(void) aTableColumn; |
| 158 |
(void) aTableView; |
| 159 |
return [self -> contents objectAtIndex: integer_to_uinteger_a (row)]; |
| 160 |
} |
| 161 |
|
| 162 |
/* |
| 163 |
Override TableViewDelegate methods. |
| 164 |
*/ |
| 165 |
- (void) tableViewSelectionDidChange: (NSNotification *) notification { |
| 166 |
/* |
| 167 |
* This is invoked when the user clicks in the table or uses the arrow keys. |
| 168 |
*/ |
| 169 |
(void) notification; |
| 170 |
trace (U"enter"); |
| 171 |
GuiList me = self -> userData; |
| 172 |
if (me && my d_selectionChangedCallback && ! my d_blockValueChangedCallbacks) { |
| 173 |
struct structGuiList_SelectionChangedEvent event { me }; |
| 174 |
my d_selectionChangedCallback (my d_selectionChangedBoss, & event); |
| 175 |
} |
| 176 |
} |
| 177 |
@end |
| 178 |
#endif |
| 179 |
|
| 180 |
#if gtk |
| 181 |
enum { |
| 182 |
COLUMN_STRING, |
| 183 |
N_COLUMNS |
| 184 |
}; |
| 185 |
#endif |
| 186 |
|
| 187 |
GuiList GuiList_create (GuiForm parent, int left, int right, int top, int bottom, bool allowMultipleSelection, conststring32 header) { |
| 188 |
autoGuiList me = Thing_new (GuiList); |
| 189 |
my d_shell = parent -> d_shell; |
| 190 |
my d_parent = parent; |
| 191 |
my d_allowMultipleSelection = allowMultipleSelection; |
| 192 |
#if gtk |
| 193 |
GtkCellRenderer *renderer = nullptr; |
| 194 |
GtkTreeViewColumn *col = nullptr; |
| 195 |
GtkTreeSelection *sel = nullptr; |
| 196 |
GtkListStore *liststore = nullptr; |
| 197 |
|
| 198 |
liststore = gtk_list_store_new (1, G_TYPE_STRING); // 1 column, of type String (this is a vararg list) |
| 199 |
GuiObject scrolled = gtk_scrolled_window_new (nullptr, nullptr); |
| 200 |
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); |
| 201 |
my d_widget = gtk_tree_view_new_with_model (GTK_TREE_MODEL (liststore)); |
| 202 |
gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (my d_widget), false); |
| 203 |
gtk_container_add (GTK_CONTAINER (scrolled), GTK_WIDGET (my d_widget)); |
| 204 |
gtk_widget_show (GTK_WIDGET (scrolled)); // BUG |
| 205 |
gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (my d_widget), allowMultipleSelection ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE); |
| 206 |
g_object_unref (liststore); // Destroys the widget after the list is destroyed |
| 207 |
|
| 208 |
_GuiObject_setUserData (my d_widget, me.get()); |
| 209 |
_GuiObject_setUserData (scrolled, me.get()); // for resizing |
| 210 |
|
| 211 |
renderer = gtk_cell_renderer_text_new (); |
| 212 |
col = gtk_tree_view_column_new (); |
| 213 |
gtk_tree_view_column_pack_start (col, renderer, true); |
| 214 |
gtk_tree_view_column_add_attribute (col, renderer, "text", 0); // zeroeth column |
| 215 |
if (header) { |
| 216 |
//gtk_tree_view_column_set_title (col, Melder_peek32to8 (header)); |
| 217 |
} |
| 218 |
gtk_tree_view_append_column (GTK_TREE_VIEW (my d_widget), col); |
| 219 |
|
| 220 |
g_object_set_data_full (G_OBJECT (my d_widget), "guiList", me.get(), (GDestroyNotify) _GuiGtkList_destroyCallback); |
| 221 |
|
| 222 |
sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (my d_widget)); |
| 223 |
if (allowMultipleSelection) { |
| 224 |
gtk_tree_selection_set_mode (sel, GTK_SELECTION_MULTIPLE); |
| 225 |
} else { |
| 226 |
gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE); |
| 227 |
} |
| 228 |
my v_positionInForm (scrolled, left, right, top, bottom, parent); |
| 229 |
g_signal_connect (sel, "changed", G_CALLBACK (_GuiGtkList_selectionChangedCallback), me.get()); |
| 230 |
#elif motif |
| 231 |
my d_widget = _Gui_initializeWidget (xmListWidgetClass, parent -> d_widget, U"list"); |
| 232 |
_GuiObject_setUserData (my d_widget, me.get()); |
| 233 |
my d_widget -> window = CreateWindowEx (0, L"listbox", L"list", |
| 234 |
WS_CHILD | WS_BORDER | WS_VSCROLL | LBS_NOTIFY | WS_CLIPSIBLINGS | |
| 235 |
( allowMultipleSelection ? LBS_EXTENDEDSEL : 0 ), |
| 236 |
my d_widget -> x, my d_widget -> y, my d_widget -> width, my d_widget -> height, |
| 237 |
my d_widget -> parent -> window, nullptr, theGui.instance, nullptr); |
| 238 |
SetWindowLongPtr (my d_widget -> window, GWLP_USERDATA, (LONG_PTR) my d_widget); |
| 239 |
SetWindowFont (my d_widget -> window, theWinGuiNormalLabelFont (), false); |
| 240 |
/*if (MEMBER (my parent, ScrolledWindow)) { |
| 241 |
XtDestroyWidget (my d_widget -> parent -> motiff.scrolledWindow.horizontalBar); |
| 242 |
my d_widget -> parent -> motiff.scrolledWindow.horizontalBar = nullptr; |
| 243 |
XtDestroyWidget (my d_widget -> parent -> motiff.scrolledWindow.verticalBar); |
| 244 |
my d_widget -> parent -> motiff.scrolledWindow.verticalBar = nullptr; |
| 245 |
}*/ |
| 246 |
my v_positionInForm (my d_widget, left, right, top, bottom, parent); |
| 247 |
#elif cocoa |
| 248 |
(void) header; |
| 249 |
GuiCocoaList *list = [[GuiCocoaList alloc] init]; |
| 250 |
my d_widget = (GuiObject) list; |
| 251 |
my v_positionInForm (my d_widget, left, right, top, bottom, parent); |
| 252 |
[list -> tableView setAllowsMultipleSelection: allowMultipleSelection]; |
| 253 |
[list setUserData: me.get()]; |
| 254 |
#endif |
| 255 |
return me.releaseToAmbiguousOwner(); |
| 256 |
} |
| 257 |
|
| 258 |
GuiList GuiList_createShown (GuiForm parent, int left, int right, int top, int bottom, bool allowMultipleSelection, conststring32 header) { |
| 259 |
GuiList me = GuiList_create (parent, left, right, top, bottom, allowMultipleSelection, header); |
| 260 |
GuiThing_show (me); |
| 261 |
return me; |
| 262 |
} |
| 263 |
|
| 264 |
void GuiList_deleteAllItems (GuiList me) { |
| 265 |
GuiControlBlockValueChangedCallbacks block (me); |
| 266 |
#if gtk |
| 267 |
GtkListStore *list_store = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (my d_widget))); |
| 268 |
gtk_list_store_clear (list_store); |
| 269 |
#elif motif |
| 270 |
ListBox_ResetContent (my d_widget -> window); |
| 271 |
#elif cocoa |
| 272 |
GuiCocoaList *list = (GuiCocoaList *) my d_widget; |
| 273 |
[list -> contents removeAllObjects]; |
| 274 |
[list -> tableView reloadData]; |
| 275 |
#endif |
| 276 |
} |
| 277 |
|
| 278 |
void GuiList_deleteItem (GuiList me, integer position) { |
| 279 |
Melder_assert (position >= 1); // so that we can subtract 1 even if the result has to be unsigned |
| 280 |
GuiControlBlockValueChangedCallbacks block (me); |
| 281 |
#if gtk |
| 282 |
GtkTreeIter iter; |
| 283 |
GtkTreeModel *tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (my d_widget)); |
| 284 |
if (gtk_tree_model_iter_nth_child (tree_model, & iter, nullptr, (gint) (position - 1))) |
| 285 |
gtk_list_store_remove (GTK_LIST_STORE (tree_model), & iter); |
| 286 |
#elif motif |
| 287 |
ListBox_DeleteString (my d_widget -> window, position - 1); |
| 288 |
#elif cocoa |
| 289 |
GuiCocoaList *list = (GuiCocoaList *) my d_widget; |
| 290 |
[list -> contents removeObjectAtIndex: (NSUInteger) (position - 1)]; |
| 291 |
[list -> tableView reloadData]; |
| 292 |
#endif |
| 293 |
} |
| 294 |
|
| 295 |
void GuiList_deselectAllItems (GuiList me) { |
| 296 |
GuiControlBlockValueChangedCallbacks block (me); |
| 297 |
#if gtk |
| 298 |
GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (my d_widget)); |
| 299 |
gtk_tree_selection_unselect_all (selection); |
| 300 |
#elif motif |
| 301 |
ListBox_SetSel (my d_widget -> window, False, -1); |
| 302 |
#elif cocoa |
| 303 |
GuiCocoaList *list = (GuiCocoaList *) my d_widget; |
| 304 |
[list -> tableView deselectAll: nil]; |
| 305 |
#endif |
| 306 |
} |
| 307 |
|
| 308 |
void GuiList_deselectItem (GuiList me, integer position) { |
| 309 |
Melder_assert (position >= 1); // so that we can subtract 1 even if the result has to be unsigned |
| 310 |
GuiControlBlockValueChangedCallbacks block (me); |
| 311 |
#if gtk |
| 312 |
GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (my d_widget)); |
| 313 |
GtkTreeIter iter; |
| 314 |
GtkTreeModel *tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (my d_widget)); |
| 315 |
if (gtk_tree_model_iter_nth_child (tree_model, & iter, nullptr, (gint) (position - 1))) |
| 316 |
gtk_tree_selection_unselect_iter (selection, & iter); |
| 317 |
#elif motif |
| 318 |
ListBox_SetSel (my d_widget -> window, False, position - 1); |
| 319 |
#elif cocoa |
| 320 |
GuiCocoaList *list = (GuiCocoaList *) my d_widget; |
| 321 |
[list -> tableView deselectRow: position - 1]; |
| 322 |
#endif |
| 323 |
} |
| 324 |
|
| 325 |
autoINTVEC GuiList_getSelectedPositions (GuiList me) { |
| 326 |
autoINTVEC selectedPositions; |
| 327 |
#if gtk |
| 328 |
GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (my d_widget)); |
| 329 |
GtkListStore *list_store = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (my d_widget))); |
| 330 |
int n = gtk_tree_selection_count_selected_rows (selection); |
| 331 |
if (n > 0) { |
| 332 |
GList *list = gtk_tree_selection_get_selected_rows (selection, (GtkTreeModel **) & list_store); |
| 333 |
integer ipos = 1; |
| 334 |
selectedPositions = zero_INTVEC (n); |
| 335 |
for (GList *l = g_list_first (list); l != nullptr; l = g_list_next (l)) { |
| 336 |
gint *index = gtk_tree_path_get_indices ((GtkTreePath *) l -> data); |
| 337 |
selectedPositions [ipos] = index [0] + 1; |
| 338 |
ipos ++; |
| 339 |
} |
| 340 |
g_list_foreach (list, (GFunc) gtk_tree_path_free, nullptr); |
| 341 |
g_list_free (list); |
| 342 |
} |
| 343 |
return selectedPositions; |
| 344 |
#elif motif |
| 345 |
int n = ListBox_GetSelCount (my d_widget -> window), *indices; |
| 346 |
if (n == 0) |
| 347 |
return selectedPositions; |
| 348 |
if (n == -1) { // single selection |
| 349 |
int selection = ListBox_GetCurSel (my d_widget -> window); |
| 350 |
if (selection == -1) |
| 351 |
return selectedPositions; |
| 352 |
n = 1; |
| 353 |
indices = Melder_calloc_f (int, n); |
| 354 |
indices [0] = selection; |
| 355 |
} else { |
| 356 |
indices = Melder_calloc_f (int, n); |
| 357 |
ListBox_GetSelItems (my d_widget -> window, n, indices); |
| 358 |
} |
| 359 |
selectedPositions = zero_INTVEC (n); |
| 360 |
for (integer ipos = 1; ipos <= n; ipos ++) |
| 361 |
selectedPositions [ipos] = indices [ipos - 1] + 1; // convert from zero-based list of zero-based indices |
| 362 |
Melder_free (indices); |
| 363 |
#elif cocoa |
| 364 |
GuiCocoaList *list = (GuiCocoaList *) my d_widget; |
| 365 |
NSIndexSet *indexSet = [list -> tableView selectedRowIndexes]; |
| 366 |
selectedPositions = zero_INTVEC (uinteger_to_integer_a ([indexSet count])); |
| 367 |
NSUInteger currentIndex = [indexSet firstIndex]; |
| 368 |
integer ipos = 0; |
| 369 |
while (currentIndex != NSNotFound) { |
| 370 |
selectedPositions [++ ipos] = uinteger_to_integer_a (currentIndex + 1); |
| 371 |
currentIndex = [indexSet indexGreaterThanIndex: currentIndex]; |
| 372 |
} |
| 373 |
Melder_assert (ipos == selectedPositions.size); |
| 374 |
#endif |
| 375 |
return selectedPositions; |
| 376 |
} |
| 377 |
|
| 378 |
integer GuiList_getBottomPosition (GuiList me) { |
| 379 |
#if gtk |
| 380 |
GtkTreePath *path; |
| 381 |
integer position = 1; |
| 382 |
if (gtk_tree_view_get_visible_range (GTK_TREE_VIEW (my d_widget), nullptr, & path)) { |
| 383 |
int *indices = gtk_tree_path_get_indices (path); |
| 384 |
position = indices ? indices[0] + 1 : 1; |
| 385 |
gtk_tree_path_free (path); // also frees indices !! |
| 386 |
} |
| 387 |
trace (U"bottom: ", position); |
| 388 |
return position; |
| 389 |
#elif motif |
| 390 |
integer bottom = ListBox_GetTopIndex (my d_widget -> window) + my d_widget -> height / ListBox_GetItemHeight (my d_widget -> window, 0); |
| 391 |
if (bottom < 1) bottom = 1; |
| 392 |
integer n = ListBox_GetCount (my d_widget -> window); |
| 393 |
if (bottom > n) bottom = n; |
| 394 |
return bottom; |
| 395 |
#elif cocoa |
| 396 |
(void) me; |
| 397 |
return 1; // TODO |
| 398 |
#else |
| 399 |
return 0; |
| 400 |
#endif |
| 401 |
} |
| 402 |
|
| 403 |
integer GuiList_getNumberOfItems (GuiList me) { |
| 404 |
integer numberOfItems = 0; |
| 405 |
#if gtk |
| 406 |
GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (my d_widget)); |
| 407 |
numberOfItems = gtk_tree_model_iter_n_children (model, nullptr); |
| 408 |
#elif motif |
| 409 |
numberOfItems = ListBox_GetCount (my d_widget -> window); |
| 410 |
#elif cocoa |
| 411 |
GuiCocoaList *list = (GuiCocoaList *) my d_widget; |
| 412 |
numberOfItems = uinteger_to_integer_a ([list -> contents count]); |
| 413 |
#endif |
| 414 |
return numberOfItems; |
| 415 |
} |
| 416 |
|
| 417 |
integer GuiList_getTopPosition (GuiList me) { |
| 418 |
#if gtk |
| 419 |
GtkTreePath *path; |
| 420 |
integer position = 1; |
| 421 |
if (gtk_tree_view_get_visible_range (GTK_TREE_VIEW (my d_widget), & path, nullptr)) { |
| 422 |
int *indices = gtk_tree_path_get_indices (path); |
| 423 |
position = indices ? indices[0] + 1 : 1; |
| 424 |
gtk_tree_path_free (path); // also frees indices !! |
| 425 |
} |
| 426 |
trace (U"top: ", position); |
| 427 |
return position; |
| 428 |
#elif motif |
| 429 |
integer top = ListBox_GetTopIndex (my d_widget -> window); |
| 430 |
if (top < 1) top = 1; |
| 431 |
integer n = ListBox_GetCount (my d_widget -> window); |
| 432 |
if (top > n) top = 0; |
| 433 |
return top; |
| 434 |
#elif cocoa |
| 435 |
(void) me; |
| 436 |
return 1; // TODO |
| 437 |
#else |
| 438 |
return 0; |
| 439 |
#endif |
| 440 |
} |
| 441 |
|
| 442 |
void GuiList_insertItem (GuiList me, conststring32 itemText /* cattable */, integer position_base1) { |
| 443 |
bool explicitlyInsertAtEnd = ( position_base1 <= 0 ); |
| 444 |
GuiControlBlockValueChangedCallbacks block (me); |
| 445 |
#if gtk |
| 446 |
GtkListStore *list_store = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (my d_widget))); |
| 447 |
gtk_list_store_insert_with_values (list_store, nullptr, explicitlyInsertAtEnd ? 1000000000 : (gint) position_base1 - 1, COLUMN_STRING, Melder_peek32to8 (itemText), -1); |
| 448 |
#elif motif |
| 449 |
HWND nativeList = my d_widget -> window; |
| 450 |
conststringW nativeItemText = Melder_peek32toW (itemText); |
| 451 |
if (explicitlyInsertAtEnd) { |
| 452 |
ListBox_AddString (nativeList, nativeItemText); |
| 453 |
} else { |
| 454 |
int nativePosition_base0 = position_base1 - 1; |
| 455 |
ListBox_InsertString (nativeList, nativePosition_base0, nativeItemText); |
| 456 |
} |
| 457 |
#elif cocoa |
| 458 |
GuiCocoaList *nativeList = (GuiCocoaList *) my d_widget; |
| 459 |
NSString *nativeItemText = [[NSString alloc] initWithUTF8String: Melder_peek32to8 (itemText)]; |
| 460 |
if (explicitlyInsertAtEnd) { |
| 461 |
[nativeList -> contents addObject: nativeItemText]; |
| 462 |
} else { |
| 463 |
NSUInteger nativePosition_base0 = (uinteger) position_base1 - 1; |
| 464 |
[nativeList -> contents insertObject: nativeItemText atIndex: nativePosition_base0]; |
| 465 |
} |
| 466 |
[nativeItemText release]; |
| 467 |
[nativeList -> tableView reloadData]; |
| 468 |
#endif |
| 469 |
} |
| 470 |
|
| 471 |
void GuiList_replaceItem (GuiList me, conststring32 itemText, integer position) { |
| 472 |
GuiControlBlockValueChangedCallbacks block (me); |
| 473 |
#if gtk |
| 474 |
GtkTreeIter iter; |
| 475 |
GtkTreeModel *tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (my d_widget)); |
| 476 |
if (gtk_tree_model_iter_nth_child (tree_model, & iter, nullptr, (gint) (position - 1))) |
| 477 |
gtk_list_store_set (GTK_LIST_STORE (tree_model), & iter, COLUMN_STRING, Melder_peek32to8 (itemText), -1); |
| 478 |
#elif motif |
| 479 |
integer nativePosition = position - 1; // convert from 1-based to zero-based |
| 480 |
ListBox_DeleteString (my d_widget -> window, nativePosition); |
| 481 |
ListBox_InsertString (my d_widget -> window, nativePosition, Melder_peek32toW (itemText)); |
| 482 |
#elif cocoa |
| 483 |
GuiCocoaList *list = (GuiCocoaList *) my d_widget; |
| 484 |
NSString *nsString = [[NSString alloc] initWithUTF8String: Melder_peek32to8 (itemText)]; |
| 485 |
[list -> contents replaceObjectAtIndex: integer_to_uinteger_a (position - 1) withObject: nsString]; |
| 486 |
[nsString release]; |
| 487 |
[list -> tableView reloadData]; |
| 488 |
#endif |
| 489 |
} |
| 490 |
|
| 491 |
void GuiList_selectItem (GuiList me, integer position) { |
| 492 |
Melder_assert (position >= 1); // so that we can subtract 1 even if the result has to be unsigned |
| 493 |
GuiControlBlockValueChangedCallbacks block (me); |
| 494 |
#if gtk |
| 495 |
GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (my d_widget)); |
| 496 |
GtkTreePath *path = gtk_tree_path_new_from_indices ((gint) position - 1, -1 /* terminator */); |
| 497 |
gtk_tree_selection_select_path (selection, path); |
| 498 |
gtk_tree_path_free (path); |
| 499 |
#elif motif |
| 500 |
if (! my d_allowMultipleSelection) { |
| 501 |
ListBox_SetCurSel (my d_widget -> window, position - 1); |
| 502 |
} else { |
| 503 |
ListBox_SetSel (my d_widget -> window, True, position - 1); |
| 504 |
} |
| 505 |
#elif cocoa |
| 506 |
NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex: NSUInteger (position - 1)]; |
| 507 |
GuiCocoaList *list = (GuiCocoaList *) my d_widget; |
| 508 |
[list -> tableView selectRowIndexes: indexSet byExtendingSelection: my d_allowMultipleSelection]; |
| 509 |
[indexSet release]; |
| 510 |
#endif |
| 511 |
} |
| 512 |
|
| 513 |
void GuiList_setSelectionChangedCallback (GuiList me, GuiList_SelectionChangedCallback callback, Thing boss) { |
| 514 |
my d_selectionChangedCallback = callback; |
| 515 |
my d_selectionChangedBoss = boss; |
| 516 |
} |
| 517 |
|
| 518 |
void GuiList_setDoubleClickCallback (GuiList me, GuiList_DoubleClickCallback callback, Thing boss) { |
| 519 |
my d_doubleClickCallback = callback; |
| 520 |
my d_doubleClickBoss = boss; |
| 521 |
} |
| 522 |
|
| 523 |
void GuiList_setScrollCallback (GuiList me, GuiList_ScrollCallback callback, Thing boss) { |
| 524 |
my d_scrollCallback = callback; |
| 525 |
my d_scrollBoss = boss; |
| 526 |
} |
| 527 |
|
| 528 |
void GuiList_setTopPosition (GuiList me, integer topPosition) { |
| 529 |
trace (U"Set top position ", topPosition); |
| 530 |
#if gtk |
| 531 |
// GtkListStore *list_store = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (my md_widget))); |
| 532 |
GtkTreePath *path = gtk_tree_path_new_from_indices ((gint) topPosition, -1 /* terminator */); // BUG? |
| 533 |
gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (my d_widget), path, nullptr, false, 0.0, 0.0); |
| 534 |
gtk_tree_path_free (path); |
| 535 |
#elif motif |
| 536 |
ListBox_SetTopIndex (my d_widget -> window, topPosition - 1); |
| 537 |
#elif cocoa |
| 538 |
(void) me; |
| 539 |
// TODO: implement |
| 540 |
#endif |
| 541 |
} |
| 542 |
|
| 543 |
/* End of file GuiList.cpp */ |