Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifdef AIX
21 : #define _LINUX_SOURCE_COMPAT
22 : #include <sys/timer.h>
23 : #undef _LINUX_SOURCE_COMPAT
24 : #endif
25 :
26 : #include <config_vclplug.h>
27 :
28 : #include <com/sun/star/awt/Toolkit.hpp>
29 : #include <com/sun/star/frame/Desktop.hpp>
30 : #include <com/sun/star/uno/XComponentContext.hpp>
31 : #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
32 : #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
33 : #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
34 : #include <osl/diagnose.h>
35 : #include <osl/process.h>
36 : #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
37 : #include <com/sun/star/ui/dialogs/ControlActions.hpp>
38 : #include <com/sun/star/uno/Any.hxx>
39 : #include <osl/mutex.hxx>
40 : #include "unx/gtk/gtkinst.hxx"
41 :
42 : #include <vcl/svapp.hxx>
43 :
44 : #include <tools/urlobj.hxx>
45 :
46 : #include <algorithm>
47 : #include <set>
48 : #include <string.h>
49 :
50 : #include "gtk/fpicker/SalGtkFilePicker.hxx"
51 :
52 : //------------------------------------------------------------------------
53 : // namespace directives
54 : //------------------------------------------------------------------------
55 :
56 : using namespace ::com::sun::star;
57 : using namespace ::com::sun::star::ui::dialogs;
58 : using namespace ::com::sun::star::ui::dialogs::TemplateDescription;
59 : using namespace ::com::sun::star::ui::dialogs::ExtendedFilePickerElementIds;
60 : using namespace ::com::sun::star::ui::dialogs::CommonFilePickerElementIds;
61 : using namespace ::com::sun::star::lang;
62 : using namespace ::com::sun::star::beans;
63 : using namespace ::com::sun::star::uno;
64 :
65 0 : static void expandexpanders(GtkContainer *pWidget)
66 : {
67 0 : GList *pChildren = gtk_container_get_children(pWidget);
68 0 : for( GList *p = pChildren; p; p = p->next )
69 : {
70 0 : if (GTK_IS_CONTAINER(GTK_WIDGET(p->data)))
71 0 : expandexpanders(GTK_CONTAINER(GTK_WIDGET(p->data)));
72 0 : if (GTK_IS_EXPANDER(GTK_WIDGET(p->data)))
73 0 : gtk_expander_set_expanded(GTK_EXPANDER(GTK_WIDGET(p->data)), sal_True);
74 : }
75 0 : g_list_free(pChildren);
76 0 : }
77 :
78 0 : void SalGtkFilePicker::dialog_mapped_cb(GtkWidget *, SalGtkFilePicker *pobjFP)
79 : {
80 0 : pobjFP->InitialMapping();
81 0 : }
82 :
83 0 : void SalGtkFilePicker::InitialMapping()
84 : {
85 0 : if (!mbPreviewState )
86 : {
87 0 : gtk_widget_hide( m_pPreview );
88 0 : gtk_file_chooser_set_preview_widget_active( GTK_FILE_CHOOSER( m_pDialog ), false);
89 : }
90 0 : gtk_widget_set_size_request (m_pPreview, -1, -1);
91 0 : }
92 :
93 0 : SalGtkFilePicker::SalGtkFilePicker( const uno::Reference< uno::XComponentContext >& xContext ) :
94 : SalGtkPicker( xContext ),
95 : SalGtkFilePicker_Base( m_rbHelperMtx ),
96 : m_pFilterList( NULL ),
97 : m_pVBox ( NULL ),
98 : mnHID_FolderChange( 0 ),
99 : mnHID_SelectionChange( 0 ),
100 : bVersionWidthUnset( false ),
101 : mbPreviewState( sal_False ),
102 : mHID_Preview( 0 ),
103 : m_pPreview( NULL ),
104 : m_pPseudoFilter( NULL ),
105 : m_PreviewImageWidth( 256 ),
106 0 : m_PreviewImageHeight( 256 )
107 : {
108 : int i;
109 :
110 0 : for( i = 0; i < TOGGLE_LAST; i++ )
111 : {
112 0 : m_pToggles[i] = NULL;
113 0 : mbToggleVisibility[i] = false;
114 : }
115 :
116 0 : for( i = 0; i < BUTTON_LAST; i++ )
117 : {
118 0 : m_pButtons[i] = NULL;
119 0 : mbButtonVisibility[i] = false;
120 : }
121 :
122 0 : for( i = 0; i < LIST_LAST; i++ )
123 : {
124 0 : m_pHBoxs[i] = NULL;
125 0 : m_pAligns[i] = NULL;
126 0 : m_pLists[i] = NULL;
127 0 : m_pListLabels[i] = NULL;
128 0 : mbListVisibility[i] = false;
129 : }
130 :
131 0 : OUString aFilePickerTitle = getResString( FILE_PICKER_TITLE_OPEN );
132 :
133 : m_pDialog = gtk_file_chooser_dialog_new(
134 : OUStringToOString( aFilePickerTitle, RTL_TEXTENCODING_UTF8 ).getStr(),
135 : NULL,
136 : GTK_FILE_CHOOSER_ACTION_OPEN,
137 : GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
138 : GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
139 0 : (char *)NULL );
140 :
141 0 : gtk_dialog_set_default_response( GTK_DIALOG (m_pDialog), GTK_RESPONSE_ACCEPT );
142 :
143 : #if ENABLE_GNOME_VFS || ENABLE_GIO
144 0 : gtk_file_chooser_set_local_only( GTK_FILE_CHOOSER( m_pDialog ), sal_False );
145 : #endif
146 0 : gtk_file_chooser_set_select_multiple( GTK_FILE_CHOOSER( m_pDialog ), sal_False );
147 :
148 0 : m_pVBox = gtk_vbox_new( sal_False, 0 );
149 :
150 : // We don't want clickable items to have a huge hit-area
151 0 : GtkWidget *pHBox = gtk_hbox_new( sal_False, 0 );
152 0 : GtkWidget *pThinVBox = gtk_vbox_new( sal_False, 0 );
153 :
154 0 : gtk_box_pack_end (GTK_BOX( m_pVBox ), pHBox, sal_False, sal_False, 0);
155 0 : gtk_box_pack_start (GTK_BOX( pHBox ), pThinVBox, sal_False, sal_False, 0);
156 0 : gtk_widget_show( pHBox );
157 0 : gtk_widget_show( pThinVBox );
158 :
159 0 : OUString aLabel;
160 :
161 0 : for( i = 0; i < TOGGLE_LAST; i++ )
162 : {
163 0 : m_pToggles[i] = gtk_check_button_new();
164 :
165 : #define LABEL_TOGGLE( elem ) \
166 : case elem : \
167 : aLabel = getResString( CHECKBOX_##elem ); \
168 : setLabel( CHECKBOX_##elem, aLabel ); \
169 : break
170 :
171 0 : switch( i ) {
172 :
173 0 : LABEL_TOGGLE( AUTOEXTENSION );
174 0 : LABEL_TOGGLE( PASSWORD );
175 0 : LABEL_TOGGLE( FILTEROPTIONS );
176 0 : LABEL_TOGGLE( READONLY );
177 0 : LABEL_TOGGLE( LINK );
178 0 : LABEL_TOGGLE( PREVIEW );
179 0 : LABEL_TOGGLE( SELECTION );
180 : default:
181 : OSL_TRACE("Handle unknown control %d", i);
182 0 : break;
183 : }
184 :
185 0 : gtk_box_pack_end( GTK_BOX( pThinVBox ), m_pToggles[i], sal_False, sal_False, 0 );
186 : }
187 :
188 0 : for( i = 0; i < LIST_LAST; i++ )
189 : {
190 0 : m_pHBoxs[i] = gtk_hbox_new( sal_False, 0 );
191 :
192 0 : m_pAligns[i] = gtk_alignment_new(0, 0, 0, 1);
193 :
194 0 : m_pListStores[i] = gtk_list_store_new (1, G_TYPE_STRING);
195 0 : m_pLists[i] = gtk_combo_box_new_with_model(GTK_TREE_MODEL(m_pListStores[i]));
196 0 : g_object_unref (m_pListStores[i]); // owned by the widget.
197 0 : GtkCellRenderer *pCell = gtk_cell_renderer_text_new ();
198 : gtk_cell_layout_pack_start(
199 0 : GTK_CELL_LAYOUT(m_pLists[i]), pCell, TRUE);
200 : gtk_cell_layout_set_attributes(
201 0 : GTK_CELL_LAYOUT (m_pLists[i]), pCell, "text", 0, NULL);
202 :
203 0 : m_pListLabels[i] = gtk_label_new( "" );
204 :
205 : #define LABEL_LIST( elem ) \
206 : case elem : \
207 : aLabel = getResString( LISTBOX_##elem##_LABEL ); \
208 : setLabel( LISTBOX_##elem##_LABEL, aLabel ); \
209 : break
210 :
211 0 : switch( i )
212 : {
213 0 : LABEL_LIST( VERSION );
214 0 : LABEL_LIST( TEMPLATE );
215 0 : LABEL_LIST( IMAGE_TEMPLATE );
216 : default:
217 : OSL_TRACE("Handle unknown control %d", i);
218 0 : break;
219 : }
220 :
221 0 : gtk_container_add( GTK_CONTAINER( m_pAligns[i]), m_pLists[i] );
222 0 : gtk_box_pack_end( GTK_BOX( m_pHBoxs[i] ), m_pAligns[i], sal_False, sal_False, 0 );
223 :
224 0 : gtk_box_pack_end( GTK_BOX( m_pHBoxs[i] ), m_pListLabels[i], sal_False, sal_False, 0 );
225 :
226 0 : gtk_box_pack_end( GTK_BOX( m_pVBox ), m_pHBoxs[i], sal_False, sal_False, 0 );
227 : }
228 :
229 0 : aLabel = getResString( FILE_PICKER_FILE_TYPE );
230 : m_pFilterExpander = gtk_expander_new_with_mnemonic(
231 0 : OUStringToOString( aLabel, RTL_TEXTENCODING_UTF8 ).getStr());
232 :
233 0 : gtk_box_pack_end( GTK_BOX( m_pVBox ), m_pFilterExpander, sal_False, sal_True, 0 );
234 :
235 0 : GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
236 0 : gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
237 0 : GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
238 0 : gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
239 0 : GTK_SHADOW_IN);
240 0 : gtk_container_add (GTK_CONTAINER (m_pFilterExpander), scrolled_window);
241 0 : gtk_widget_show (scrolled_window);
242 :
243 0 : OString sExpand(getenv("SAL_EXPANDFPICKER"));
244 0 : sal_Int32 nExpand = sExpand.toInt32();
245 0 : switch (nExpand)
246 : {
247 : default:
248 : case 0:
249 0 : break;
250 : case 1:
251 0 : gtk_expander_set_expanded(GTK_EXPANDER(m_pFilterExpander), sal_True);
252 0 : break;
253 : case 2:
254 0 : expandexpanders(GTK_CONTAINER(m_pDialog));
255 0 : gtk_expander_set_expanded(GTK_EXPANDER(m_pFilterExpander), sal_True);
256 0 : break;
257 : }
258 :
259 : m_pFilterStore = gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_STRING,
260 0 : G_TYPE_STRING, G_TYPE_STRING);
261 0 : m_pFilterView = gtk_tree_view_new_with_model (GTK_TREE_MODEL(m_pFilterStore));
262 0 : gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(m_pFilterView), false);
263 0 : gtk_tree_view_set_rules_hint (GTK_TREE_VIEW(m_pFilterView), true);
264 :
265 : GtkTreeViewColumn *column;
266 : GtkCellRenderer *cell;
267 :
268 0 : for (i = 0; i < 2; ++i)
269 : {
270 0 : column = gtk_tree_view_column_new ();
271 0 : cell = gtk_cell_renderer_text_new ();
272 0 : gtk_tree_view_column_set_expand (column, sal_True);
273 0 : gtk_tree_view_column_pack_start (column, cell, sal_False);
274 0 : gtk_tree_view_column_set_attributes (column, cell, "text", i, (char *)NULL);
275 0 : gtk_tree_view_append_column (GTK_TREE_VIEW(m_pFilterView), column);
276 : }
277 :
278 0 : gtk_container_add (GTK_CONTAINER (scrolled_window), m_pFilterView);
279 0 : gtk_widget_show (m_pFilterView);
280 :
281 0 : gtk_file_chooser_set_extra_widget( GTK_FILE_CHOOSER( m_pDialog ), m_pVBox );
282 :
283 0 : m_pPreview = gtk_image_new();
284 0 : gtk_file_chooser_set_preview_widget( GTK_FILE_CHOOSER( m_pDialog ), m_pPreview );
285 :
286 0 : g_signal_connect( G_OBJECT( m_pToggles[PREVIEW] ), "toggled",
287 0 : G_CALLBACK( preview_toggled_cb ), this );
288 0 : g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW(m_pFilterView)), "changed",
289 0 : G_CALLBACK ( type_changed_cb ), this);
290 0 : g_signal_connect( G_OBJECT( m_pDialog ), "notify::filter",
291 0 : G_CALLBACK( filter_changed_cb ), this);
292 0 : g_signal_connect( G_OBJECT( m_pFilterExpander ), "activate",
293 0 : G_CALLBACK( expander_changed_cb ), this);
294 0 : g_signal_connect (G_OBJECT( m_pDialog ), "map",
295 0 : G_CALLBACK (dialog_mapped_cb), this);
296 :
297 0 : gtk_widget_show( m_pVBox );
298 :
299 0 : PangoLayout *layout = gtk_widget_create_pango_layout (m_pFilterView, NULL);
300 : guint ypad;
301 : PangoRectangle row_height;
302 0 : pango_layout_set_markup (layout, "All Files", -1);
303 0 : pango_layout_get_pixel_extents (layout, NULL, &row_height);
304 0 : g_object_unref (layout);
305 :
306 0 : g_object_get (cell, "ypad", &ypad, (char *)NULL);
307 0 : guint height = (row_height.height + 2*ypad) * 5;
308 0 : gtk_widget_set_size_request (m_pFilterView, -1, height);
309 0 : gtk_widget_set_size_request (m_pPreview, 1, height);
310 :
311 0 : gtk_file_chooser_set_preview_widget_active( GTK_FILE_CHOOSER( m_pDialog ), true);
312 0 : }
313 :
314 : //------------------------------------------------------------------------------------
315 : // XFilePickerNotifier
316 : //------------------------------------------------------------------------------------
317 :
318 0 : void SAL_CALL SalGtkFilePicker::addFilePickerListener( const uno::Reference<XFilePickerListener>& xListener )
319 : throw( uno::RuntimeException )
320 : {
321 0 : SolarMutexGuard g;
322 :
323 : OSL_ENSURE(!m_xListener.is(),
324 : "SalGtkFilePicker only talks with one listener at a time...");
325 0 : m_xListener = xListener;
326 0 : }
327 :
328 0 : void SAL_CALL SalGtkFilePicker::removeFilePickerListener( const uno::Reference<XFilePickerListener>& )
329 : throw( uno::RuntimeException )
330 : {
331 0 : SolarMutexGuard g;
332 :
333 0 : m_xListener.clear();
334 0 : }
335 :
336 : // -------------------------------------------------
337 : // XEventListener
338 : // -------------------------------------------------
339 :
340 0 : void SAL_CALL SalGtkFilePicker::disposing( const lang::EventObject& aEvent ) throw( uno::RuntimeException )
341 : {
342 : // no member access => no mutex needed
343 :
344 0 : uno::Reference<XFilePickerListener> xFilePickerListener( aEvent.Source, ::com::sun::star::uno::UNO_QUERY );
345 :
346 0 : if( xFilePickerListener.is() )
347 0 : removeFilePickerListener( xFilePickerListener );
348 0 : }
349 :
350 : //-----------------------------------------------------------------------------------------
351 : // FilePicker Event functions
352 : //-----------------------------------------------------------------------------------------
353 :
354 0 : void SalGtkFilePicker::impl_fileSelectionChanged( FilePickerEvent aEvent )
355 : {
356 : OSL_TRACE( "file selection changed");
357 0 : if (m_xListener.is()) m_xListener->fileSelectionChanged( aEvent );
358 0 : }
359 :
360 0 : void SalGtkFilePicker::impl_directoryChanged( FilePickerEvent aEvent )
361 : {
362 : OSL_TRACE("directory changed");
363 0 : if (m_xListener.is()) m_xListener->directoryChanged( aEvent );
364 0 : }
365 :
366 0 : void SalGtkFilePicker::impl_controlStateChanged( FilePickerEvent aEvent )
367 : {
368 : OSL_TRACE("control state changed");
369 0 : if (m_xListener.is()) m_xListener->controlStateChanged( aEvent );
370 0 : }
371 :
372 : //=====================================================================
373 :
374 0 : struct FilterEntry
375 : {
376 : protected:
377 : OUString m_sTitle;
378 : OUString m_sFilter;
379 :
380 : UnoFilterList m_aSubFilters;
381 :
382 : public:
383 0 : FilterEntry( const OUString& _rTitle, const OUString& _rFilter )
384 : :m_sTitle( _rTitle )
385 0 : ,m_sFilter( _rFilter )
386 : {
387 0 : }
388 :
389 : FilterEntry( const OUString& _rTitle, const UnoFilterList& _rSubFilters );
390 :
391 0 : OUString getTitle() const { return m_sTitle; }
392 0 : OUString getFilter() const { return m_sFilter; }
393 :
394 : /// determines if the filter has sub filter (i.e., the filter is a filter group in real)
395 : sal_Bool hasSubFilters( ) const;
396 :
397 : /** retrieves the filters belonging to the entry
398 : @return
399 : the number of sub filters
400 : */
401 : sal_Int32 getSubFilters( UnoFilterList& _rSubFilterList );
402 :
403 : // helpers for iterating the sub filters
404 0 : const UnoFilterEntry* beginSubFilters() const { return m_aSubFilters.getConstArray(); }
405 0 : const UnoFilterEntry* endSubFilters() const { return m_aSubFilters.getConstArray() + m_aSubFilters.getLength(); }
406 : };
407 :
408 : //=====================================================================
409 :
410 : //---------------------------------------------------------------------
411 0 : FilterEntry::FilterEntry( const OUString& _rTitle, const UnoFilterList& _rSubFilters )
412 : :m_sTitle( _rTitle )
413 0 : ,m_aSubFilters( _rSubFilters )
414 : {
415 0 : }
416 :
417 : //---------------------------------------------------------------------
418 0 : sal_Bool FilterEntry::hasSubFilters() const
419 : {
420 0 : return( 0 < m_aSubFilters.getLength() );
421 : }
422 :
423 : //---------------------------------------------------------------------
424 0 : sal_Int32 FilterEntry::getSubFilters( UnoFilterList& _rSubFilterList )
425 : {
426 0 : _rSubFilterList = m_aSubFilters;
427 0 : return m_aSubFilters.getLength();
428 : }
429 :
430 : static bool
431 0 : isFilterString( const OUString &rFilterString, const char *pMatch )
432 : {
433 0 : sal_Int32 nIndex = 0;
434 0 : OUString aToken;
435 0 : bool bIsFilter = true;
436 :
437 0 : OUString aMatch(OUString::createFromAscii(pMatch));
438 :
439 0 : do
440 : {
441 0 : aToken = rFilterString.getToken( 0, ';', nIndex );
442 0 : if( !aToken.match( aMatch ) )
443 : {
444 0 : bIsFilter = false;
445 0 : break;
446 : }
447 : }
448 0 : while( nIndex >= 0 );
449 :
450 0 : return bIsFilter;
451 : }
452 :
453 : static OUString
454 0 : shrinkFilterName( const OUString &rFilterName, bool bAllowNoStar = false )
455 : {
456 : int i;
457 0 : int nBracketLen = -1;
458 0 : int nBracketEnd = -1;
459 0 : const sal_Unicode *pStr = rFilterName.getStr();
460 0 : OUString aRealName = rFilterName;
461 :
462 0 : for( i = aRealName.getLength() - 1; i > 0; i-- )
463 : {
464 0 : if( pStr[i] == ')' )
465 0 : nBracketEnd = i;
466 0 : else if( pStr[i] == '(' )
467 : {
468 0 : nBracketLen = nBracketEnd - i;
469 0 : if( nBracketEnd <= 0 )
470 0 : continue;
471 0 : if( isFilterString( rFilterName.copy( i + 1, nBracketLen - 1 ), "*." ) )
472 0 : aRealName = aRealName.replaceAt( i, nBracketLen + 1, OUString() );
473 0 : else if (bAllowNoStar)
474 : {
475 0 : if( isFilterString( rFilterName.copy( i + 1, nBracketLen - 1 ), ".") )
476 0 : aRealName = aRealName.replaceAt( i, nBracketLen + 1, OUString() );
477 : }
478 : }
479 : }
480 :
481 0 : return aRealName;
482 : }
483 :
484 : static void
485 0 : dialog_remove_buttons( GtkDialog *pDialog )
486 : {
487 0 : g_return_if_fail( GTK_IS_DIALOG( pDialog ) );
488 :
489 : GtkWidget *pActionArea;
490 :
491 : #if GTK_CHECK_VERSION(3,0,0)
492 : pActionArea = gtk_dialog_get_action_area( pDialog );
493 : #else
494 0 : pActionArea = pDialog->action_area;
495 : #endif
496 :
497 : GList *pChildren =
498 0 : gtk_container_get_children( GTK_CONTAINER( pActionArea ) );
499 :
500 0 : for( GList *p = pChildren; p; p = p->next )
501 0 : gtk_widget_destroy( GTK_WIDGET( p->data ) );
502 :
503 0 : g_list_free( pChildren );
504 : }
505 :
506 : //------------------------------------------------------------------------------------
507 : namespace {
508 : //................................................................................
509 : struct FilterTitleMatch : public ::std::unary_function< FilterEntry, bool >
510 : {
511 : protected:
512 : const OUString& rTitle;
513 :
514 : public:
515 0 : FilterTitleMatch( const OUString& _rTitle ) : rTitle( _rTitle ) { }
516 :
517 : //............................................................................
518 0 : bool operator () ( const FilterEntry& _rEntry )
519 : {
520 : sal_Bool bMatch;
521 0 : if( !_rEntry.hasSubFilters() )
522 : // a real filter
523 0 : bMatch = ( _rEntry.getTitle() == rTitle );
524 : else
525 : // a filter group -> search the sub filters
526 : bMatch =
527 0 : _rEntry.endSubFilters() != ::std::find_if(
528 : _rEntry.beginSubFilters(),
529 : _rEntry.endSubFilters(),
530 : *this
531 0 : );
532 :
533 0 : return bMatch ? true : false;
534 : }
535 0 : bool operator () ( const UnoFilterEntry& _rEntry )
536 : {
537 0 : OUString aShrunkName = shrinkFilterName( _rEntry.First );
538 0 : return aShrunkName == rTitle ? true : false;
539 : }
540 : };
541 : }
542 :
543 :
544 : //------------------------------------------------------------------------------------
545 0 : sal_Bool SalGtkFilePicker::FilterNameExists( const OUString& rTitle )
546 : {
547 0 : sal_Bool bRet = sal_False;
548 :
549 0 : if( m_pFilterList )
550 : bRet =
551 0 : m_pFilterList->end() != ::std::find_if(
552 : m_pFilterList->begin(),
553 : m_pFilterList->end(),
554 : FilterTitleMatch( rTitle )
555 0 : );
556 :
557 0 : return bRet;
558 : }
559 :
560 : //------------------------------------------------------------------------------------
561 0 : sal_Bool SalGtkFilePicker::FilterNameExists( const UnoFilterList& _rGroupedFilters )
562 : {
563 0 : sal_Bool bRet = sal_False;
564 :
565 0 : if( m_pFilterList )
566 : {
567 0 : const UnoFilterEntry* pStart = _rGroupedFilters.getConstArray();
568 0 : const UnoFilterEntry* pEnd = pStart + _rGroupedFilters.getLength();
569 0 : for( ; pStart != pEnd; ++pStart )
570 0 : if( m_pFilterList->end() != ::std::find_if(
571 : m_pFilterList->begin(),
572 : m_pFilterList->end(),
573 0 : FilterTitleMatch( pStart->First ) ) )
574 0 : break;
575 :
576 0 : bRet = pStart != pEnd;
577 : }
578 :
579 0 : return bRet;
580 : }
581 :
582 : //------------------------------------------------------------------------------------
583 0 : void SalGtkFilePicker::ensureFilterList( const OUString& _rInitialCurrentFilter )
584 : {
585 0 : if( !m_pFilterList )
586 : {
587 0 : m_pFilterList = new FilterList;
588 :
589 : // set the first filter to the current filter
590 0 : if ( m_aCurrentFilter.isEmpty() )
591 0 : m_aCurrentFilter = _rInitialCurrentFilter;
592 : }
593 0 : }
594 :
595 :
596 : //-----------------------------------------------------------------------------------------
597 : //
598 : //-----------------------------------------------------------------------------------------
599 :
600 0 : void SAL_CALL SalGtkFilePicker::appendFilter( const OUString& aTitle, const OUString& aFilter )
601 : throw( lang::IllegalArgumentException, uno::RuntimeException )
602 : {
603 0 : SolarMutexGuard g;
604 :
605 : OSL_ASSERT( m_pDialog != NULL );
606 :
607 0 : if( FilterNameExists( aTitle ) )
608 0 : throw IllegalArgumentException();
609 :
610 : // ensure that we have a filter list
611 0 : ensureFilterList( aTitle );
612 :
613 : // append the filter
614 0 : m_pFilterList->insert( m_pFilterList->end(), FilterEntry( aTitle, aFilter ) );
615 0 : }
616 :
617 : //-----------------------------------------------------------------------------------------
618 : //
619 : //-----------------------------------------------------------------------------------------
620 :
621 0 : void SAL_CALL SalGtkFilePicker::setCurrentFilter( const OUString& aTitle )
622 : throw( lang::IllegalArgumentException, uno::RuntimeException )
623 : {
624 0 : SolarMutexGuard g;
625 :
626 : OSL_ASSERT( m_pDialog != NULL );
627 :
628 : OSL_TRACE( "Setting current filter to %s\n",
629 : OUStringToOString( aTitle, RTL_TEXTENCODING_UTF8 ).getStr() );
630 :
631 0 : if( aTitle != m_aCurrentFilter )
632 : {
633 0 : m_aCurrentFilter = aTitle;
634 0 : SetCurFilter( m_aCurrentFilter );
635 : OSL_TRACE( "REALLY Setting current filter to %s\n",
636 : OUStringToOString( aTitle, RTL_TEXTENCODING_UTF8 ).getStr() );
637 :
638 0 : }
639 :
640 : // TODO m_pImpl->setCurrentFilter( aTitle );
641 0 : }
642 :
643 : //-----------------------------------------------------------------------------------------
644 : //
645 : //-----------------------------------------------------------------------------------------
646 :
647 0 : void SalGtkFilePicker::updateCurrentFilterFromName(const gchar* filtername)
648 : {
649 0 : OUString aFilterName(filtername, strlen(filtername), RTL_TEXTENCODING_UTF8);
650 0 : FilterList::iterator aEnd = m_pFilterList->end();
651 0 : for (FilterList::iterator aIter = m_pFilterList->begin(); aIter != aEnd; ++aIter)
652 : {
653 0 : if (aFilterName == shrinkFilterName( aIter->getTitle()))
654 : {
655 0 : m_aCurrentFilter = aIter->getTitle();
656 0 : break;
657 : }
658 0 : }
659 0 : }
660 :
661 0 : void SalGtkFilePicker::UpdateFilterfromUI()
662 : {
663 : // Update the filtername from the users selection if they have had a chance to do so.
664 : // If the user explicitly sets a type then use that, if not then take the implicit type
665 : // from the filter of the files glob on which he is currently searching
666 0 : if (!mnHID_FolderChange || !mnHID_SelectionChange)
667 0 : return;
668 :
669 0 : GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(m_pFilterView));
670 : GtkTreeIter iter;
671 : GtkTreeModel *model;
672 0 : if (gtk_tree_selection_get_selected (selection, &model, &iter))
673 : {
674 : gchar *title;
675 0 : gtk_tree_model_get (model, &iter, 2, &title, -1);
676 0 : updateCurrentFilterFromName(title);
677 0 : g_free (title);
678 : }
679 0 : else if( GtkFileFilter *filter = gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(m_pDialog)))
680 : {
681 0 : if (m_pPseudoFilter != filter)
682 0 : updateCurrentFilterFromName(gtk_file_filter_get_name( filter ));
683 : else
684 0 : updateCurrentFilterFromName(OUStringToOString( m_aInitialFilter, RTL_TEXTENCODING_UTF8 ).getStr());
685 : }
686 : }
687 :
688 0 : OUString SAL_CALL SalGtkFilePicker::getCurrentFilter() throw( uno::RuntimeException )
689 : {
690 0 : SolarMutexGuard g;
691 :
692 : OSL_ASSERT( m_pDialog != NULL );
693 :
694 : OSL_TRACE( "GetCURRENTfilter" );
695 :
696 0 : UpdateFilterfromUI();
697 :
698 : OSL_TRACE( "Returning current filter of %s\n",
699 : OUStringToOString( m_aCurrentFilter, RTL_TEXTENCODING_UTF8 ).getStr() );
700 :
701 0 : return m_aCurrentFilter;
702 : }
703 :
704 : //-----------------------------------------------------------------------------------------
705 : // XFilterGroupManager functions
706 : //-----------------------------------------------------------------------------------------
707 :
708 0 : void SAL_CALL SalGtkFilePicker::appendFilterGroup( const OUString& /*sGroupTitle*/, const uno::Sequence<beans::StringPair>& aFilters )
709 : throw( lang::IllegalArgumentException, uno::RuntimeException )
710 : {
711 0 : SolarMutexGuard g;
712 :
713 : OSL_ASSERT( m_pDialog != NULL );
714 :
715 : // TODO m_pImpl->appendFilterGroup( sGroupTitle, aFilters );
716 : // check the names
717 0 : if( FilterNameExists( aFilters ) )
718 : // TODO: a more precise exception message
719 0 : throw IllegalArgumentException();
720 :
721 : // ensure that we have a filter list
722 0 : OUString sInitialCurrentFilter;
723 0 : if( aFilters.getLength() )
724 0 : sInitialCurrentFilter = aFilters[0].First;
725 :
726 0 : ensureFilterList( sInitialCurrentFilter );
727 :
728 : // append the filter
729 0 : const StringPair* pSubFilters = aFilters.getConstArray();
730 0 : const StringPair* pSubFiltersEnd = pSubFilters + aFilters.getLength();
731 0 : for( ; pSubFilters != pSubFiltersEnd; ++pSubFilters )
732 0 : m_pFilterList->insert( m_pFilterList->end(), FilterEntry( pSubFilters->First, pSubFilters->Second ) );
733 :
734 0 : }
735 :
736 : //-----------------------------------------------------------------------------------------
737 : // XFilePicker functions
738 : //-----------------------------------------------------------------------------------------
739 :
740 0 : void SAL_CALL SalGtkFilePicker::setMultiSelectionMode( sal_Bool bMode ) throw( uno::RuntimeException )
741 : {
742 0 : SolarMutexGuard g;
743 :
744 : OSL_ASSERT( m_pDialog != NULL );
745 :
746 0 : gtk_file_chooser_set_select_multiple( GTK_FILE_CHOOSER(m_pDialog), bMode );
747 0 : }
748 :
749 0 : void SAL_CALL SalGtkFilePicker::setDefaultName( const OUString& aName )
750 : throw( uno::RuntimeException )
751 : {
752 0 : SolarMutexGuard g;
753 :
754 : OSL_ASSERT( m_pDialog != NULL );
755 :
756 0 : OString aStr = OUStringToOString( aName, RTL_TEXTENCODING_UTF8 );
757 0 : GtkFileChooserAction eAction = gtk_file_chooser_get_action( GTK_FILE_CHOOSER( m_pDialog ) );
758 :
759 : // set_current_name launches a Gtk critical error if called for other than save
760 0 : if( GTK_FILE_CHOOSER_ACTION_SAVE == eAction )
761 0 : gtk_file_chooser_set_current_name( GTK_FILE_CHOOSER( m_pDialog ), aStr.getStr() );
762 0 : }
763 :
764 0 : void SAL_CALL SalGtkFilePicker::setDisplayDirectory( const OUString& rDirectory )
765 : throw( lang::IllegalArgumentException, uno::RuntimeException )
766 : {
767 0 : SolarMutexGuard g;
768 :
769 0 : implsetDisplayDirectory(rDirectory);
770 0 : }
771 :
772 0 : OUString SAL_CALL SalGtkFilePicker::getDisplayDirectory() throw( uno::RuntimeException )
773 : {
774 0 : SolarMutexGuard g;
775 :
776 0 : return implgetDisplayDirectory();
777 : }
778 :
779 0 : uno::Sequence<OUString> SAL_CALL SalGtkFilePicker::getFiles() throw( uno::RuntimeException )
780 : {
781 : // no member access => no mutex needed
782 :
783 0 : uno::Sequence< OUString > aFiles = getSelectedFiles();
784 : /*
785 : The previous multiselection API design was completely broken
786 : and unimplementable for some hetrogenous pseudo-URIs eg. search://
787 : Thus crop unconditionally to a single selection.
788 : */
789 0 : aFiles.realloc (1);
790 0 : return aFiles;
791 : }
792 :
793 0 : uno::Sequence<OUString> SAL_CALL SalGtkFilePicker::getSelectedFiles() throw( uno::RuntimeException )
794 : {
795 0 : SolarMutexGuard g;
796 :
797 : OSL_ASSERT( m_pDialog != NULL );
798 :
799 0 : GSList* pPathList = gtk_file_chooser_get_uris( GTK_FILE_CHOOSER(m_pDialog) );
800 :
801 0 : int nCount = g_slist_length( pPathList );
802 0 : int nIndex = 0;
803 : OSL_TRACE( "GETFILES called %d files", nCount );
804 :
805 : // get the current action setting
806 : GtkFileChooserAction eAction = gtk_file_chooser_get_action(
807 0 : GTK_FILE_CHOOSER( m_pDialog ));
808 :
809 0 : uno::Sequence< OUString > aSelectedFiles(nCount);
810 :
811 : // Convert to OOo
812 0 : for( GSList *pElem = pPathList; pElem; pElem = pElem->next)
813 : {
814 0 : gchar *pURI = reinterpret_cast<gchar*>(pElem->data);
815 0 : aSelectedFiles[ nIndex ] = uritounicode(pURI);
816 :
817 0 : if( GTK_FILE_CHOOSER_ACTION_SAVE == eAction )
818 : {
819 0 : OUString sFilterName;
820 0 : sal_Int32 nTokenIndex = 0;
821 0 : bool bExtensionTypedIn = false;
822 :
823 0 : GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(m_pFilterView));
824 : GtkTreeIter iter;
825 : GtkTreeModel *model;
826 0 : if (gtk_tree_selection_get_selected (selection, &model, &iter))
827 : {
828 : gchar *title;
829 0 : gtk_tree_model_get (model, &iter, 2, &title, -1);
830 0 : sFilterName = OUString( title, strlen( title), RTL_TEXTENCODING_UTF8 );
831 0 : g_free (title);
832 : }
833 : else
834 : {
835 0 : if( aSelectedFiles[nIndex].indexOf('.') > 0 )
836 : {
837 0 : OUString sExtension;
838 0 : nTokenIndex = 0;
839 0 : do
840 0 : sExtension = aSelectedFiles[nIndex].getToken( 0, '.', nTokenIndex );
841 0 : while( nTokenIndex >= 0 );
842 :
843 0 : if( sExtension.getLength() >= 3 ) // 3 = typical/minimum extension length
844 : {
845 0 : static const OUString aStarDot( "*." );
846 :
847 0 : OUString aNewFilter;
848 0 : OUString aOldFilter = getCurrentFilter();
849 0 : sal_Bool bChangeFilter = sal_True;
850 0 : for ( FilterList::iterator aListIter = m_pFilterList->begin();
851 0 : aListIter != m_pFilterList->end();
852 : ++aListIter
853 : )
854 : {
855 0 : if( aListIter->getFilter().equalsIgnoreAsciiCase( aStarDot+sExtension ) )
856 : {
857 0 : if( aNewFilter.isEmpty() )
858 0 : aNewFilter = aListIter->getTitle();
859 :
860 0 : if( aOldFilter == aListIter->getTitle() )
861 0 : bChangeFilter = sal_False;
862 :
863 0 : bExtensionTypedIn = true;
864 : }
865 : }
866 0 : if( bChangeFilter && bExtensionTypedIn )
867 0 : setCurrentFilter( aNewFilter );
868 0 : }
869 : }
870 :
871 0 : GtkFileFilter *filter = gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(m_pDialog));
872 0 : if (m_pPseudoFilter != filter)
873 : {
874 0 : const gchar* filtername = gtk_file_filter_get_name( filter );
875 0 : sFilterName = OUString( filtername, strlen( filtername ), RTL_TEXTENCODING_UTF8 );
876 : }
877 : else
878 0 : sFilterName = m_aInitialFilter;
879 : }
880 :
881 : OSL_TRACE( "2: current filter is %s\n",
882 : OUStringToOString( sFilterName, RTL_TEXTENCODING_UTF8 ).getStr() );
883 :
884 : FilterList::iterator aListIter = ::std::find_if(
885 0 : m_pFilterList->begin(), m_pFilterList->end(), FilterTitleMatch(sFilterName) );
886 :
887 0 : OUString aFilter;
888 0 : if (aListIter != m_pFilterList->end())
889 0 : aFilter = aListIter->getFilter();
890 :
891 : OSL_TRACE( "turned into %s\n",
892 : OUStringToOString( aFilter, RTL_TEXTENCODING_UTF8 ).getStr() );
893 :
894 0 : nTokenIndex = 0;
895 0 : OUString sToken;
896 : // OUString strExt;
897 0 : do
898 : {
899 0 : sToken = aFilter.getToken( 0, '.', nTokenIndex );
900 :
901 0 : if ( sToken.lastIndexOf( ';' ) != -1 )
902 : {
903 0 : sal_Int32 nZero = 0;
904 0 : OUString aCurrentToken = sToken.getToken( 0, ';', nZero);
905 :
906 0 : sToken = aCurrentToken;
907 0 : break;
908 : }
909 : }
910 0 : while( nTokenIndex >= 0 );
911 :
912 0 : if( !bExtensionTypedIn && ( sToken != "*" ) )
913 : {
914 : //if the filename does not already have the auto extension, stick it on
915 0 : OUString sExtension = OUString( "." ) + sToken;
916 0 : OUString &rBase = aSelectedFiles[nIndex];
917 0 : sal_Int32 nExtensionIdx = rBase.getLength() - sExtension.getLength();
918 : OSL_TRACE( "idx are %d %d", rBase.lastIndexOf( sExtension ), nExtensionIdx );
919 :
920 0 : if( rBase.lastIndexOf( sExtension ) != nExtensionIdx )
921 0 : rBase += sExtension;
922 0 : }
923 :
924 : }
925 :
926 0 : nIndex++;
927 0 : g_free( pURI );
928 : }
929 :
930 0 : g_slist_free( pPathList );
931 :
932 0 : return aSelectedFiles;
933 : }
934 :
935 : //-----------------------------------------------------------------------------------------
936 : // XExecutableDialog functions
937 : //-----------------------------------------------------------------------------------------
938 :
939 0 : void SAL_CALL SalGtkFilePicker::setTitle( const OUString& rTitle ) throw( uno::RuntimeException )
940 : {
941 0 : SolarMutexGuard g;
942 :
943 0 : implsetTitle(rTitle);
944 0 : }
945 :
946 0 : sal_Int16 SAL_CALL SalGtkFilePicker::execute() throw( uno::RuntimeException )
947 : {
948 0 : SolarMutexGuard g;
949 :
950 : OSL_TRACE( "1: HERE WE ARE");
951 : OSL_ASSERT( m_pDialog != NULL );
952 :
953 0 : sal_Int16 retVal = 0;
954 :
955 0 : SetFilters();
956 :
957 : mnHID_FolderChange =
958 0 : g_signal_connect( GTK_FILE_CHOOSER( m_pDialog ), "current-folder-changed",
959 0 : G_CALLBACK( folder_changed_cb ), ( gpointer )this );
960 :
961 : mnHID_SelectionChange =
962 0 : g_signal_connect( GTK_FILE_CHOOSER( m_pDialog ), "selection-changed",
963 0 : G_CALLBACK( selection_changed_cb ), ( gpointer )this );
964 :
965 0 : int btn = GTK_RESPONSE_NO;
966 :
967 : uno::Reference< awt::XExtendedToolkit > xToolkit(
968 : awt::Toolkit::create(m_xContext),
969 0 : UNO_QUERY_THROW );
970 :
971 : uno::Reference< frame::XDesktop > xDesktop(
972 : frame::Desktop::create(m_xContext),
973 0 : UNO_QUERY_THROW );
974 :
975 0 : RunDialog* pRunDialog = new RunDialog(m_pDialog, xToolkit, xDesktop);
976 0 : uno::Reference < awt::XTopWindowListener > xLifeCycle(pRunDialog);
977 0 : while( GTK_RESPONSE_NO == btn )
978 : {
979 0 : btn = GTK_RESPONSE_YES; // we dont want to repeat unless user clicks NO for file save.
980 :
981 0 : gint nStatus = pRunDialog->run();
982 0 : switch( nStatus )
983 : {
984 : case GTK_RESPONSE_ACCEPT:
985 0 : if( GTK_FILE_CHOOSER_ACTION_SAVE == gtk_file_chooser_get_action( GTK_FILE_CHOOSER( m_pDialog ) ) )
986 : {
987 0 : Sequence < OUString > aPathSeq = getFiles();
988 0 : if( aPathSeq.getLength() == 1 )
989 : {
990 0 : OString sFileName = unicodetouri( aPathSeq[0] );
991 0 : gchar *gFileName = g_filename_from_uri ( sFileName.getStr(), NULL, NULL );
992 0 : if( g_file_test( gFileName, G_FILE_TEST_IS_REGULAR ) )
993 : {
994 : GtkWidget *dlg;
995 0 : INetURLObject aFileObj( sFileName );
996 :
997 : OString baseName(
998 : OUStringToOString(
999 : aFileObj.getName(
1000 : INetURLObject::LAST_SEGMENT,
1001 : true,
1002 : INetURLObject::DECODE_WITH_CHARSET
1003 : ),
1004 : RTL_TEXTENCODING_UTF8
1005 : )
1006 0 : );
1007 : OString aMsg(
1008 : OUStringToOString(
1009 : getResString( FILE_PICKER_OVERWRITE ),
1010 : RTL_TEXTENCODING_UTF8
1011 : )
1012 0 : );
1013 0 : OString toReplace( RTL_CONSTASCII_STRINGPARAM( "$filename$" ));
1014 :
1015 0 : aMsg = aMsg.replaceAt(
1016 : aMsg.indexOf( toReplace ),
1017 : toReplace.getLength(),
1018 : baseName
1019 0 : );
1020 :
1021 : dlg = gtk_message_dialog_new( NULL,
1022 : GTK_DIALOG_MODAL,
1023 : GTK_MESSAGE_QUESTION,
1024 : GTK_BUTTONS_YES_NO,
1025 : "%s",
1026 : aMsg.getStr()
1027 0 : );
1028 :
1029 0 : gtk_window_set_title( GTK_WINDOW( dlg ),
1030 : OUStringToOString(getResString(FILE_PICKER_TITLE_SAVE ),
1031 0 : RTL_TEXTENCODING_UTF8 ).getStr() );
1032 :
1033 0 : RunDialog* pAnotherDialog = new RunDialog(dlg, xToolkit, xDesktop);
1034 0 : uno::Reference < awt::XTopWindowListener > xAnotherLifeCycle(pAnotherDialog);
1035 0 : btn = pAnotherDialog->run();
1036 :
1037 0 : gtk_widget_destroy( dlg );
1038 : }
1039 0 : g_free (gFileName);
1040 :
1041 0 : if( btn == GTK_RESPONSE_YES )
1042 0 : retVal = ExecutableDialogResults::OK;
1043 0 : }
1044 : }
1045 : else
1046 0 : retVal = ExecutableDialogResults::OK;
1047 0 : break;
1048 :
1049 : case GTK_RESPONSE_CANCEL:
1050 0 : retVal = ExecutableDialogResults::CANCEL;
1051 0 : break;
1052 :
1053 : case 1: //PLAY
1054 : {
1055 0 : FilePickerEvent evt;
1056 0 : evt.ElementId = PUSHBUTTON_PLAY;
1057 : OSL_TRACE( "filter_changed, isn't it great %x", this);
1058 0 : impl_controlStateChanged( evt );
1059 0 : btn = GTK_RESPONSE_NO;
1060 : }
1061 0 : break;
1062 :
1063 : default:
1064 0 : retVal = 0;
1065 0 : break;
1066 : }
1067 : }
1068 :
1069 0 : if (mnHID_FolderChange)
1070 0 : g_signal_handler_disconnect(GTK_FILE_CHOOSER( m_pDialog ), mnHID_FolderChange);
1071 0 : if (mnHID_SelectionChange)
1072 0 : g_signal_handler_disconnect(GTK_FILE_CHOOSER( m_pDialog ), mnHID_SelectionChange);
1073 :
1074 0 : return retVal;
1075 : }
1076 :
1077 : //------------------------------------------------------------------------------------
1078 :
1079 : // cf. offapi/com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.idl
1080 0 : GtkWidget *SalGtkFilePicker::getWidget( sal_Int16 nControlId, GType *pType )
1081 : {
1082 : OSL_TRACE("control id is %d", nControlId);
1083 0 : GType tType = GTK_TYPE_TOGGLE_BUTTON; //prevent waring by initializing
1084 0 : GtkWidget *pWidget = 0;
1085 :
1086 : #define MAP_TOGGLE( elem ) \
1087 : case ExtendedFilePickerElementIds::CHECKBOX_##elem: \
1088 : pWidget = m_pToggles[elem]; tType = GTK_TYPE_TOGGLE_BUTTON; \
1089 : break
1090 : #define MAP_BUTTON( elem ) \
1091 : case ExtendedFilePickerElementIds::PUSHBUTTON_##elem: \
1092 : pWidget = m_pButtons[elem]; tType = GTK_TYPE_BUTTON; \
1093 : break
1094 : #define MAP_LIST( elem ) \
1095 : case ExtendedFilePickerElementIds::LISTBOX_##elem: \
1096 : pWidget = m_pLists[elem]; tType = GTK_TYPE_COMBO_BOX; \
1097 : break
1098 : #define MAP_LIST_LABEL( elem ) \
1099 : case ExtendedFilePickerElementIds::LISTBOX_##elem##_LABEL: \
1100 : pWidget = m_pListLabels[elem]; tType = GTK_TYPE_LABEL; \
1101 : break
1102 :
1103 0 : switch( nControlId )
1104 : {
1105 0 : MAP_TOGGLE( AUTOEXTENSION );
1106 0 : MAP_TOGGLE( PASSWORD );
1107 0 : MAP_TOGGLE( FILTEROPTIONS );
1108 0 : MAP_TOGGLE( READONLY );
1109 0 : MAP_TOGGLE( LINK );
1110 0 : MAP_TOGGLE( PREVIEW );
1111 0 : MAP_TOGGLE( SELECTION );
1112 0 : MAP_BUTTON( PLAY );
1113 0 : MAP_LIST( VERSION );
1114 0 : MAP_LIST( TEMPLATE );
1115 0 : MAP_LIST( IMAGE_TEMPLATE );
1116 0 : MAP_LIST_LABEL( VERSION );
1117 0 : MAP_LIST_LABEL( TEMPLATE );
1118 0 : MAP_LIST_LABEL( IMAGE_TEMPLATE );
1119 : default:
1120 : OSL_TRACE("Handle unknown control %d", nControlId);
1121 0 : break;
1122 : }
1123 : #undef MAP
1124 :
1125 0 : if( pType )
1126 0 : *pType = tType;
1127 0 : return pWidget;
1128 : }
1129 :
1130 :
1131 :
1132 : //------------------------------------------------------------------------------------
1133 : // XFilePickerControlAccess functions
1134 : //------------------------------------------------------------------------------------
1135 0 : static void HackWidthToFirst(GtkComboBox *pWidget)
1136 : {
1137 : GtkRequisition requisition;
1138 0 : gtk_widget_size_request(GTK_WIDGET(pWidget), &requisition);
1139 0 : gtk_widget_set_size_request(GTK_WIDGET(pWidget), requisition.width, -1);
1140 0 : }
1141 :
1142 0 : static void ComboBoxAppendText(GtkComboBox *pCombo, const OUString &rStr)
1143 : {
1144 : GtkTreeIter aIter;
1145 0 : GtkListStore *pStore = GTK_LIST_STORE(gtk_combo_box_get_model(pCombo));
1146 0 : OString aStr = OUStringToOString(rStr, RTL_TEXTENCODING_UTF8);
1147 0 : gtk_list_store_append(pStore, &aIter);
1148 0 : gtk_list_store_set(pStore, &aIter, 0, aStr.getStr(), -1);
1149 0 : }
1150 :
1151 0 : void SalGtkFilePicker::HandleSetListValue(GtkComboBox *pWidget, sal_Int16 nControlAction, const uno::Any& rValue)
1152 : {
1153 0 : switch (nControlAction)
1154 : {
1155 : case ControlActions::ADD_ITEM:
1156 : {
1157 0 : OUString sItem;
1158 0 : rValue >>= sItem;
1159 0 : ComboBoxAppendText(pWidget, sItem);
1160 0 : if (!bVersionWidthUnset)
1161 : {
1162 0 : HackWidthToFirst(pWidget);
1163 0 : bVersionWidthUnset = true;
1164 0 : }
1165 : }
1166 0 : break;
1167 : case ControlActions::ADD_ITEMS:
1168 : {
1169 0 : Sequence< OUString > aStringList;
1170 0 : rValue >>= aStringList;
1171 0 : sal_Int32 nItemCount = aStringList.getLength();
1172 0 : for (sal_Int32 i = 0; i < nItemCount; ++i)
1173 : {
1174 0 : ComboBoxAppendText(pWidget,aStringList[i]);
1175 0 : if (!bVersionWidthUnset)
1176 : {
1177 0 : HackWidthToFirst(pWidget);
1178 0 : bVersionWidthUnset = true;
1179 : }
1180 0 : }
1181 : }
1182 0 : break;
1183 : case ControlActions::DELETE_ITEM:
1184 : {
1185 0 : sal_Int32 nPos=0;
1186 0 : rValue >>= nPos;
1187 :
1188 : GtkTreeIter aIter;
1189 0 : GtkListStore *pStore = GTK_LIST_STORE(
1190 : gtk_combo_box_get_model(GTK_COMBO_BOX(pWidget)));
1191 0 : if(gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(pStore), &aIter, NULL, nPos))
1192 0 : gtk_list_store_remove(pStore, &aIter);
1193 : }
1194 0 : break;
1195 : case ControlActions::DELETE_ITEMS:
1196 : {
1197 0 : gtk_combo_box_set_active(pWidget, -1);
1198 0 : GtkListStore *pStore = GTK_LIST_STORE(
1199 : gtk_combo_box_get_model(GTK_COMBO_BOX(pWidget)));
1200 0 : gtk_list_store_clear(pStore);
1201 : }
1202 0 : break;
1203 : case ControlActions::SET_SELECT_ITEM:
1204 : {
1205 0 : sal_Int32 nPos=0;
1206 0 : rValue >>= nPos;
1207 0 : gtk_combo_box_set_active(pWidget, nPos);
1208 : }
1209 0 : break;
1210 : default:
1211 : OSL_TRACE("undocumented/unimplemented ControlAction for a list");
1212 0 : break;
1213 : }
1214 :
1215 : //I think its best to make it insensitive unless there is the chance to
1216 : //actually select something from the list.
1217 : gint nItems = gtk_tree_model_iter_n_children(
1218 0 : gtk_combo_box_get_model(pWidget), NULL);
1219 0 : gtk_widget_set_sensitive(GTK_WIDGET(pWidget), nItems > 1 ? true : false);
1220 0 : }
1221 :
1222 0 : uno::Any SalGtkFilePicker::HandleGetListValue(GtkComboBox *pWidget, sal_Int16 nControlAction) const
1223 : {
1224 0 : uno::Any aAny;
1225 0 : switch (nControlAction)
1226 : {
1227 : case ControlActions::GET_ITEMS:
1228 : {
1229 0 : Sequence< OUString > aItemList;
1230 :
1231 0 : GtkTreeModel *pTree = gtk_combo_box_get_model(pWidget);
1232 : GtkTreeIter iter;
1233 0 : if (gtk_tree_model_get_iter_first(pTree, &iter))
1234 : {
1235 : sal_Int32 nSize = gtk_tree_model_iter_n_children(
1236 0 : pTree, NULL);
1237 :
1238 0 : aItemList.realloc(nSize);
1239 0 : for (sal_Int32 i=0; i < nSize; ++i)
1240 : {
1241 : gchar *item;
1242 : gtk_tree_model_get(gtk_combo_box_get_model(pWidget),
1243 0 : &iter, 0, &item, -1);
1244 0 : aItemList[i] = OUString(item, strlen(item), RTL_TEXTENCODING_UTF8);
1245 0 : g_free(item);
1246 0 : gtk_tree_model_iter_next(pTree, &iter);
1247 : }
1248 : }
1249 0 : aAny <<= aItemList;
1250 : }
1251 0 : break;
1252 : case ControlActions::GET_SELECTED_ITEM:
1253 : {
1254 : GtkTreeIter iter;
1255 0 : if (gtk_combo_box_get_active_iter(pWidget, &iter))
1256 : {
1257 : gchar *item;
1258 : gtk_tree_model_get(gtk_combo_box_get_model(pWidget),
1259 0 : &iter, 0, &item, -1);
1260 0 : OUString sItem(item, strlen(item), RTL_TEXTENCODING_UTF8);
1261 0 : aAny <<= sItem;
1262 0 : g_free(item);
1263 : }
1264 : }
1265 0 : break;
1266 : case ControlActions::GET_SELECTED_ITEM_INDEX:
1267 : {
1268 0 : gint nActive = gtk_combo_box_get_active(pWidget);
1269 0 : aAny <<= static_cast< sal_Int32 >(nActive);
1270 : }
1271 0 : break;
1272 : default:
1273 : OSL_TRACE("undocumented/unimplemented ControlAction for a list");
1274 0 : break;
1275 : }
1276 0 : return aAny;
1277 : }
1278 :
1279 0 : void SAL_CALL SalGtkFilePicker::setValue( sal_Int16 nControlId, sal_Int16 nControlAction, const uno::Any& rValue )
1280 : throw( uno::RuntimeException )
1281 : {
1282 0 : SolarMutexGuard g;
1283 :
1284 : OSL_ASSERT( m_pDialog != NULL );
1285 :
1286 : OSL_TRACE( "SETTING VALUE %d", nControlAction );
1287 : GType tType;
1288 : GtkWidget *pWidget;
1289 :
1290 0 : if( !( pWidget = getWidget( nControlId, &tType ) ) )
1291 : OSL_TRACE("enable unknown control %d", nControlId);
1292 0 : else if( tType == GTK_TYPE_TOGGLE_BUTTON )
1293 : {
1294 0 : sal_Bool bChecked = false;
1295 0 : rValue >>= bChecked;
1296 0 : gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( pWidget ), bChecked );
1297 : }
1298 0 : else if( tType == GTK_TYPE_COMBO_BOX )
1299 0 : HandleSetListValue(GTK_COMBO_BOX(pWidget), nControlAction, rValue);
1300 : else
1301 : {
1302 : OSL_TRACE("Can't set value on button / list %d %d\n",
1303 : nControlId, nControlAction);
1304 0 : }
1305 0 : }
1306 :
1307 0 : uno::Any SAL_CALL SalGtkFilePicker::getValue( sal_Int16 nControlId, sal_Int16 nControlAction )
1308 : throw( uno::RuntimeException )
1309 : {
1310 0 : SolarMutexGuard g;
1311 :
1312 : OSL_ASSERT( m_pDialog != NULL );
1313 :
1314 0 : uno::Any aRetval;
1315 :
1316 : GType tType;
1317 : GtkWidget *pWidget;
1318 :
1319 0 : if( !( pWidget = getWidget( nControlId, &tType ) ) )
1320 : OSL_TRACE("enable unknown control %d", nControlId);
1321 0 : else if( tType == GTK_TYPE_TOGGLE_BUTTON )
1322 0 : aRetval <<= (sal_Bool) gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( pWidget ) );
1323 0 : else if( tType == GTK_TYPE_COMBO_BOX )
1324 0 : aRetval = HandleGetListValue(GTK_COMBO_BOX(pWidget), nControlAction);
1325 : else
1326 : OSL_TRACE("Can't get value on button / list %d %d\n",
1327 : nControlId, nControlAction );
1328 :
1329 0 : return aRetval;
1330 : }
1331 :
1332 0 : void SAL_CALL SalGtkFilePicker::enableControl( sal_Int16 nControlId, sal_Bool bEnable )
1333 : throw( uno::RuntimeException )
1334 : {
1335 0 : SolarMutexGuard g;
1336 :
1337 : OSL_ASSERT( m_pDialog != NULL );
1338 :
1339 : GtkWidget *pWidget;
1340 :
1341 0 : if ( nControlId == ExtendedFilePickerElementIds::LISTBOX_FILTER_SELECTOR )
1342 0 : gtk_expander_set_expanded( GTK_EXPANDER( m_pFilterExpander ), bEnable );
1343 0 : else if( ( pWidget = getWidget( nControlId ) ) )
1344 : {
1345 0 : if( bEnable )
1346 : {
1347 : OSL_TRACE( "enable" );
1348 0 : gtk_widget_set_sensitive( pWidget, sal_True );
1349 : }
1350 : else
1351 : {
1352 : OSL_TRACE( "disable" );
1353 0 : gtk_widget_set_sensitive( pWidget, sal_False );
1354 : }
1355 : }
1356 : else
1357 0 : OSL_TRACE("enable unknown control %d", nControlId );
1358 0 : }
1359 :
1360 0 : void SAL_CALL SalGtkFilePicker::setLabel( sal_Int16 nControlId, const OUString& rLabel )
1361 : throw( uno::RuntimeException )
1362 : {
1363 0 : SolarMutexGuard g;
1364 :
1365 : OSL_ASSERT( m_pDialog != NULL );
1366 :
1367 : GType tType;
1368 : GtkWidget *pWidget;
1369 :
1370 0 : if( !( pWidget = getWidget( nControlId, &tType ) ) )
1371 : {
1372 : OSL_TRACE("Set label on unknown control %d", nControlId);
1373 0 : return;
1374 : }
1375 :
1376 0 : OString aTxt = OUStringToOString( rLabel.replace('~', '_'), RTL_TEXTENCODING_UTF8 );
1377 0 : if (nControlId == ExtendedFilePickerElementIds::PUSHBUTTON_PLAY)
1378 : {
1379 : #ifdef GTK_STOCK_MEDIA_PLAY
1380 0 : if (msPlayLabel.isEmpty())
1381 0 : msPlayLabel = rLabel;
1382 0 : if (msPlayLabel == rLabel)
1383 0 : gtk_button_set_label(GTK_BUTTON(pWidget), GTK_STOCK_MEDIA_PLAY);
1384 : else
1385 0 : gtk_button_set_label(GTK_BUTTON(pWidget), GTK_STOCK_MEDIA_STOP);
1386 : #else
1387 : gtk_button_set_label(GTK_BUTTON(pWidget), aTxt.getStr());
1388 : #endif
1389 : }
1390 0 : else if( tType == GTK_TYPE_TOGGLE_BUTTON || tType == GTK_TYPE_BUTTON || tType == GTK_TYPE_LABEL )
1391 : g_object_set( pWidget, "label", aTxt.getStr(),
1392 0 : "use_underline", sal_True, (char *)NULL );
1393 : else
1394 0 : OSL_TRACE("Can't set label on list");
1395 : }
1396 :
1397 0 : OUString SAL_CALL SalGtkFilePicker::getLabel( sal_Int16 nControlId )
1398 : throw( uno::RuntimeException )
1399 : {
1400 0 : SolarMutexGuard g;
1401 :
1402 : OSL_ASSERT( m_pDialog != NULL );
1403 :
1404 : GType tType;
1405 0 : OString aTxt;
1406 : GtkWidget *pWidget;
1407 :
1408 0 : if( !( pWidget = getWidget( nControlId, &tType ) ) )
1409 : OSL_TRACE("Get label on unknown control %d", nControlId);
1410 0 : else if( tType == GTK_TYPE_TOGGLE_BUTTON || tType == GTK_TYPE_BUTTON || tType == GTK_TYPE_LABEL )
1411 0 : aTxt = gtk_button_get_label( GTK_BUTTON( pWidget ) );
1412 : else
1413 : OSL_TRACE("Can't get label on list");
1414 :
1415 0 : return OStringToOUString( aTxt, RTL_TEXTENCODING_UTF8 );
1416 : }
1417 :
1418 : //------------------------------------------------------------------------------------
1419 : // XFilePreview functions
1420 : //------------------------------------------------------------------------------------
1421 :
1422 0 : uno::Sequence<sal_Int16> SAL_CALL SalGtkFilePicker::getSupportedImageFormats() throw( uno::RuntimeException )
1423 : {
1424 0 : SolarMutexGuard g;
1425 :
1426 : OSL_ASSERT( m_pDialog != NULL );
1427 :
1428 : // TODO return m_pImpl->getSupportedImageFormats();
1429 0 : return uno::Sequence<sal_Int16>();
1430 : }
1431 :
1432 0 : sal_Int32 SAL_CALL SalGtkFilePicker::getTargetColorDepth() throw( uno::RuntimeException )
1433 : {
1434 0 : SolarMutexGuard g;
1435 :
1436 : OSL_ASSERT( m_pDialog != NULL );
1437 :
1438 : // TODO return m_pImpl->getTargetColorDepth();
1439 0 : return 0;
1440 : }
1441 :
1442 0 : sal_Int32 SAL_CALL SalGtkFilePicker::getAvailableWidth() throw( uno::RuntimeException )
1443 : {
1444 0 : SolarMutexGuard g;
1445 :
1446 : OSL_ASSERT( m_pDialog != NULL );
1447 :
1448 0 : return m_PreviewImageWidth;
1449 : }
1450 :
1451 0 : sal_Int32 SAL_CALL SalGtkFilePicker::getAvailableHeight() throw( uno::RuntimeException )
1452 : {
1453 0 : SolarMutexGuard g;
1454 :
1455 : OSL_ASSERT( m_pDialog != NULL );
1456 :
1457 0 : return m_PreviewImageHeight;
1458 : }
1459 :
1460 0 : void SAL_CALL SalGtkFilePicker::setImage( sal_Int16 /*aImageFormat*/, const uno::Any& /*aImage*/ )
1461 : throw( lang::IllegalArgumentException, uno::RuntimeException )
1462 : {
1463 0 : SolarMutexGuard g;
1464 :
1465 0 : OSL_ASSERT( m_pDialog != NULL );
1466 :
1467 : // TODO m_pImpl->setImage( aImageFormat, aImage );
1468 0 : }
1469 :
1470 0 : void SalGtkFilePicker::implChangeType( GtkTreeSelection *selection )
1471 : {
1472 0 : OUString aLabel = getResString( FILE_PICKER_FILE_TYPE );
1473 :
1474 : GtkTreeIter iter;
1475 : GtkTreeModel *model;
1476 0 : if (gtk_tree_selection_get_selected (selection, &model, &iter))
1477 : {
1478 : gchar *title;
1479 0 : gtk_tree_model_get (model, &iter, 2, &title, -1);
1480 0 : aLabel += OUString( ": " );
1481 0 : aLabel += OUString( title, strlen(title), RTL_TEXTENCODING_UTF8 );
1482 0 : g_free (title);
1483 : }
1484 0 : gtk_expander_set_label (GTK_EXPANDER (m_pFilterExpander),
1485 0 : OUStringToOString( aLabel, RTL_TEXTENCODING_UTF8 ).getStr());
1486 0 : FilePickerEvent evt;
1487 0 : evt.ElementId = LISTBOX_FILTER;
1488 0 : impl_controlStateChanged( evt );
1489 0 : }
1490 :
1491 0 : void SalGtkFilePicker::type_changed_cb( GtkTreeSelection *selection, SalGtkFilePicker *pobjFP )
1492 : {
1493 0 : pobjFP->implChangeType(selection);
1494 0 : }
1495 :
1496 0 : void SalGtkFilePicker::unselect_type()
1497 : {
1498 0 : gtk_tree_selection_unselect_all(gtk_tree_view_get_selection(GTK_TREE_VIEW(m_pFilterView)));
1499 0 : }
1500 :
1501 0 : void SalGtkFilePicker::expander_changed_cb( GtkExpander *expander, SalGtkFilePicker *pobjFP )
1502 : {
1503 0 : if (gtk_expander_get_expanded(expander))
1504 0 : pobjFP->unselect_type();
1505 0 : }
1506 :
1507 0 : void SalGtkFilePicker::filter_changed_cb( GtkFileChooser *, GParamSpec *,
1508 : SalGtkFilePicker *pobjFP )
1509 : {
1510 0 : FilePickerEvent evt;
1511 0 : evt.ElementId = LISTBOX_FILTER;
1512 : OSL_TRACE( "filter_changed, isn't it great %x", pobjFP );
1513 0 : pobjFP->impl_controlStateChanged( evt );
1514 0 : }
1515 :
1516 0 : void SalGtkFilePicker::folder_changed_cb( GtkFileChooser *, SalGtkFilePicker *pobjFP )
1517 : {
1518 0 : FilePickerEvent evt;
1519 : OSL_TRACE( "folder_changed, isn't it great %x", pobjFP );
1520 0 : pobjFP->impl_directoryChanged( evt );
1521 0 : }
1522 :
1523 0 : void SalGtkFilePicker::selection_changed_cb( GtkFileChooser *, SalGtkFilePicker *pobjFP )
1524 : {
1525 0 : FilePickerEvent evt;
1526 : OSL_TRACE( "selection_changed, isn't it great %x", pobjFP );
1527 0 : pobjFP->impl_fileSelectionChanged( evt );
1528 0 : }
1529 :
1530 0 : void SalGtkFilePicker::update_preview_cb( GtkFileChooser *file_chooser, SalGtkFilePicker* pobjFP )
1531 : {
1532 : GtkWidget *preview;
1533 : char *filename;
1534 : GdkPixbuf *pixbuf;
1535 0 : gboolean have_preview = sal_False;
1536 :
1537 0 : preview = pobjFP->m_pPreview;
1538 0 : filename = gtk_file_chooser_get_preview_filename( file_chooser );
1539 :
1540 0 : if( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( pobjFP->m_pToggles[PREVIEW] ) ) && g_file_test( filename, G_FILE_TEST_IS_REGULAR ) )
1541 : {
1542 : pixbuf = gdk_pixbuf_new_from_file_at_size(
1543 : filename,
1544 : pobjFP->m_PreviewImageWidth,
1545 0 : pobjFP->m_PreviewImageHeight, NULL );
1546 :
1547 0 : have_preview = ( pixbuf != NULL );
1548 :
1549 0 : gtk_image_set_from_pixbuf( GTK_IMAGE( preview ), pixbuf );
1550 0 : if( pixbuf )
1551 0 : g_object_unref( G_OBJECT( pixbuf ) );
1552 :
1553 : }
1554 :
1555 0 : gtk_file_chooser_set_preview_widget_active( file_chooser, have_preview );
1556 :
1557 0 : if( filename )
1558 0 : g_free( filename );
1559 0 : }
1560 :
1561 0 : sal_Bool SAL_CALL SalGtkFilePicker::setShowState( sal_Bool bShowState ) throw( uno::RuntimeException )
1562 : {
1563 0 : SolarMutexGuard g;
1564 :
1565 : OSL_ASSERT( m_pDialog != NULL );
1566 :
1567 : // TODO return m_pImpl->setShowState( bShowState );
1568 0 : if( bShowState != mbPreviewState )
1569 : {
1570 0 : if( bShowState )
1571 : {
1572 : // Show
1573 0 : if( !mHID_Preview )
1574 : {
1575 0 : mHID_Preview = g_signal_connect(
1576 : GTK_FILE_CHOOSER( m_pDialog ), "update-preview",
1577 0 : G_CALLBACK( update_preview_cb ), ( gpointer )this );
1578 : }
1579 0 : gtk_widget_show( m_pPreview );
1580 : }
1581 : else
1582 : {
1583 : // Hide
1584 0 : gtk_widget_hide( m_pPreview );
1585 : }
1586 :
1587 : // also emit the signal
1588 0 : g_signal_emit_by_name( G_OBJECT( m_pDialog ), "update-preview" );
1589 :
1590 0 : mbPreviewState = bShowState;
1591 : }
1592 0 : return true;
1593 : }
1594 :
1595 0 : sal_Bool SAL_CALL SalGtkFilePicker::getShowState() throw( uno::RuntimeException )
1596 : {
1597 0 : SolarMutexGuard g;
1598 :
1599 : OSL_ASSERT( m_pDialog != NULL );
1600 :
1601 0 : return mbPreviewState;
1602 : }
1603 :
1604 : //------------------------------------------------------------------------------------
1605 : // XInitialization
1606 : //------------------------------------------------------------------------------------
1607 :
1608 0 : void SAL_CALL SalGtkFilePicker::initialize( const uno::Sequence<uno::Any>& aArguments )
1609 : throw( uno::Exception, uno::RuntimeException )
1610 : {
1611 : // parameter checking
1612 0 : uno::Any aAny;
1613 0 : if( 0 == aArguments.getLength() )
1614 : throw lang::IllegalArgumentException(
1615 : OUString( "no arguments" ),
1616 0 : static_cast<XFilePicker2*>( this ), 1 );
1617 :
1618 0 : aAny = aArguments[0];
1619 :
1620 0 : if( ( aAny.getValueType() != ::getCppuType( ( sal_Int16* )0 ) ) &&
1621 0 : (aAny.getValueType() != ::getCppuType( ( sal_Int8* )0 ) ) )
1622 : throw lang::IllegalArgumentException(
1623 : OUString( "invalid argument type" ),
1624 0 : static_cast<XFilePicker2*>( this ), 1 );
1625 :
1626 0 : sal_Int16 templateId = -1;
1627 0 : aAny >>= templateId;
1628 :
1629 0 : GtkFileChooserAction eAction = GTK_FILE_CHOOSER_ACTION_OPEN;
1630 0 : const gchar *first_button_text = GTK_STOCK_OPEN;
1631 :
1632 0 : SolarMutexGuard g;
1633 :
1634 : // TODO: extract full semantic from
1635 : // svtools/source/filepicker/filepicker.cxx (getWinBits)
1636 0 : switch( templateId )
1637 : {
1638 : case FILEOPEN_SIMPLE:
1639 0 : eAction = GTK_FILE_CHOOSER_ACTION_OPEN;
1640 0 : first_button_text = GTK_STOCK_OPEN;
1641 : OSL_TRACE( "3all true" );
1642 0 : break;
1643 : case FILESAVE_SIMPLE:
1644 0 : eAction = GTK_FILE_CHOOSER_ACTION_SAVE;
1645 0 : first_button_text = GTK_STOCK_SAVE;
1646 : OSL_TRACE( "2all true" );
1647 0 : break;
1648 : case FILESAVE_AUTOEXTENSION_PASSWORD:
1649 0 : eAction = GTK_FILE_CHOOSER_ACTION_SAVE;
1650 0 : first_button_text = GTK_STOCK_SAVE;
1651 0 : mbToggleVisibility[PASSWORD] = true;
1652 : OSL_TRACE( "1all true" );
1653 : // TODO
1654 0 : break;
1655 : case FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS:
1656 0 : eAction = GTK_FILE_CHOOSER_ACTION_SAVE;
1657 0 : first_button_text = GTK_STOCK_SAVE;
1658 0 : mbToggleVisibility[PASSWORD] = true;
1659 0 : mbToggleVisibility[FILTEROPTIONS] = true;
1660 : OSL_TRACE( "4all true" );
1661 : // TODO
1662 0 : break;
1663 : case FILESAVE_AUTOEXTENSION_SELECTION:
1664 0 : eAction = GTK_FILE_CHOOSER_ACTION_SAVE; // SELECT_FOLDER ?
1665 0 : first_button_text = GTK_STOCK_SAVE;
1666 0 : mbToggleVisibility[SELECTION] = true;
1667 : OSL_TRACE( "5all true" );
1668 : // TODO
1669 0 : break;
1670 : case FILESAVE_AUTOEXTENSION_TEMPLATE:
1671 0 : eAction = GTK_FILE_CHOOSER_ACTION_SAVE;
1672 0 : first_button_text = GTK_STOCK_SAVE;
1673 0 : mbListVisibility[TEMPLATE] = true;
1674 : OSL_TRACE( "6all true" );
1675 : // TODO
1676 0 : break;
1677 : case FILEOPEN_LINK_PREVIEW_IMAGE_TEMPLATE:
1678 0 : eAction = GTK_FILE_CHOOSER_ACTION_OPEN;
1679 0 : first_button_text = GTK_STOCK_OPEN;
1680 0 : mbToggleVisibility[LINK] = true;
1681 0 : mbToggleVisibility[PREVIEW] = true;
1682 0 : mbListVisibility[IMAGE_TEMPLATE] = true;
1683 : // TODO
1684 0 : break;
1685 : case FILEOPEN_PLAY:
1686 0 : eAction = GTK_FILE_CHOOSER_ACTION_OPEN;
1687 0 : first_button_text = GTK_STOCK_OPEN;
1688 0 : mbButtonVisibility[PLAY] = true;
1689 : // TODO
1690 0 : break;
1691 : case FILEOPEN_READONLY_VERSION:
1692 0 : eAction = GTK_FILE_CHOOSER_ACTION_OPEN;
1693 0 : first_button_text = GTK_STOCK_OPEN;
1694 0 : mbToggleVisibility[READONLY] = true;
1695 0 : mbListVisibility[VERSION] = true;
1696 0 : break;
1697 : case FILEOPEN_LINK_PREVIEW:
1698 0 : eAction = GTK_FILE_CHOOSER_ACTION_OPEN;
1699 0 : first_button_text = GTK_STOCK_OPEN;
1700 0 : mbToggleVisibility[LINK] = true;
1701 0 : mbToggleVisibility[PREVIEW] = true;
1702 : // TODO
1703 0 : break;
1704 : case FILESAVE_AUTOEXTENSION:
1705 0 : eAction = GTK_FILE_CHOOSER_ACTION_SAVE;
1706 0 : first_button_text = GTK_STOCK_SAVE;
1707 : OSL_TRACE( "7all true" );
1708 : // TODO
1709 0 : break;
1710 : default:
1711 : throw lang::IllegalArgumentException(
1712 : OUString( "Unknown template" ),
1713 : static_cast< XFilePicker2* >( this ),
1714 0 : 1 );
1715 : }
1716 :
1717 0 : if( GTK_FILE_CHOOSER_ACTION_SAVE == eAction )
1718 : {
1719 0 : OUString aFilePickerTitle(getResString( FILE_PICKER_TITLE_SAVE ));
1720 0 : gtk_window_set_title ( GTK_WINDOW( m_pDialog ),
1721 0 : OUStringToOString( aFilePickerTitle, RTL_TEXTENCODING_UTF8 ).getStr() );
1722 : }
1723 :
1724 0 : gtk_file_chooser_set_action( GTK_FILE_CHOOSER( m_pDialog ), eAction);
1725 0 : dialog_remove_buttons( GTK_DIALOG( m_pDialog ) );
1726 0 : gtk_dialog_add_button( GTK_DIALOG( m_pDialog ), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL );
1727 0 : for( int nTVIndex = 0; nTVIndex < BUTTON_LAST; nTVIndex++ )
1728 : {
1729 0 : if( mbButtonVisibility[nTVIndex] )
1730 : {
1731 : #ifdef GTK_STOCK_MEDIA_PLAY
1732 0 : m_pButtons[ nTVIndex ] = gtk_dialog_add_button( GTK_DIALOG( m_pDialog ), GTK_STOCK_MEDIA_PLAY, 1 );
1733 : #else
1734 : OString aPlay = OUStringToOString( getResString( PUSHBUTTON_PLAY ), RTL_TEXTENCODING_UTF8 );
1735 : m_pButtons[ nTVIndex ] = gtk_dialog_add_button( GTK_DIALOG( m_pDialog ), aPlay.getStr(), 1 );
1736 : #endif
1737 : }
1738 : }
1739 0 : gtk_dialog_add_button( GTK_DIALOG( m_pDialog ), first_button_text, GTK_RESPONSE_ACCEPT );
1740 :
1741 0 : gtk_dialog_set_default_response( GTK_DIALOG (m_pDialog), GTK_RESPONSE_ACCEPT );
1742 :
1743 : // Setup special flags
1744 0 : for( int nTVIndex = 0; nTVIndex < TOGGLE_LAST; nTVIndex++ )
1745 : {
1746 0 : if( mbToggleVisibility[nTVIndex] )
1747 0 : gtk_widget_show( m_pToggles[ nTVIndex ] );
1748 : }
1749 :
1750 0 : for( int nTVIndex = 0; nTVIndex < LIST_LAST; nTVIndex++ )
1751 : {
1752 0 : if( mbListVisibility[nTVIndex] )
1753 : {
1754 0 : gtk_widget_set_sensitive( m_pLists[ nTVIndex ], false );
1755 0 : gtk_widget_show( m_pLists[ nTVIndex ] );
1756 0 : gtk_widget_show( m_pListLabels[ nTVIndex ] );
1757 0 : gtk_widget_show( m_pAligns[ nTVIndex ] );
1758 0 : gtk_widget_show( m_pHBoxs[ nTVIndex ] );
1759 : }
1760 0 : }
1761 0 : }
1762 :
1763 0 : void SalGtkFilePicker::preview_toggled_cb( GObject *cb, SalGtkFilePicker* pobjFP )
1764 : {
1765 0 : if( pobjFP->mbToggleVisibility[PREVIEW] )
1766 0 : pobjFP->setShowState( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( cb ) ) );
1767 0 : }
1768 :
1769 : //------------------------------------------------------------------------------------
1770 : // XCancellable
1771 : //------------------------------------------------------------------------------------
1772 :
1773 0 : void SAL_CALL SalGtkFilePicker::cancel() throw( uno::RuntimeException )
1774 : {
1775 0 : SolarMutexGuard g;
1776 :
1777 0 : OSL_ASSERT( m_pDialog != NULL );
1778 :
1779 : // TODO m_pImpl->cancel();
1780 0 : }
1781 :
1782 : //--------------------------------------------------
1783 : // Misc
1784 : //-------------------------------------------------
1785 0 : void SalGtkFilePicker::SetCurFilter( const OUString& rFilter )
1786 : {
1787 : // Get all the filters already added
1788 0 : GSList *filters = gtk_file_chooser_list_filters ( GTK_FILE_CHOOSER( m_pDialog ) );
1789 0 : bool bFound = false;
1790 :
1791 0 : for( GSList *iter = filters; !bFound && iter; iter = iter->next )
1792 : {
1793 0 : GtkFileFilter* pFilter = reinterpret_cast<GtkFileFilter *>( iter->data );
1794 0 : G_CONST_RETURN gchar * filtername = gtk_file_filter_get_name( pFilter );
1795 0 : OUString sFilterName( filtername, strlen( filtername ), RTL_TEXTENCODING_UTF8 );
1796 :
1797 0 : OUString aShrunkName = shrinkFilterName( rFilter );
1798 0 : if( aShrunkName.equals( sFilterName) )
1799 : {
1800 : OSL_TRACE( "actually setting %s", filtername );
1801 0 : gtk_file_chooser_set_filter( GTK_FILE_CHOOSER( m_pDialog ), pFilter );
1802 0 : bFound = true;
1803 : }
1804 0 : }
1805 :
1806 0 : g_slist_free( filters );
1807 0 : }
1808 :
1809 : extern "C"
1810 : {
1811 : static gboolean
1812 0 : case_insensitive_filter (const GtkFileFilterInfo *filter_info, gpointer data)
1813 : {
1814 0 : gboolean bRetval = sal_False;
1815 0 : const char *pFilter = (const char *) data;
1816 :
1817 0 : g_return_val_if_fail( data != NULL, sal_False );
1818 0 : g_return_val_if_fail( filter_info != NULL, sal_False );
1819 :
1820 0 : if( !filter_info->uri )
1821 0 : return sal_False;
1822 :
1823 0 : const char *pExtn = strrchr( filter_info->uri, '.' );
1824 0 : if( !pExtn )
1825 0 : return sal_False;
1826 0 : pExtn++;
1827 :
1828 0 : if( !g_ascii_strcasecmp( pFilter, pExtn ) )
1829 0 : bRetval = sal_True;
1830 :
1831 : SAL_INFO( "vcl.gtk", "'" << filter_info->uri << "' match extn '" << pExtn << "' vs '" << pFilter << "' yeilds " << bRetval );
1832 :
1833 0 : return bRetval;
1834 : }
1835 : }
1836 :
1837 0 : GtkFileFilter* SalGtkFilePicker::implAddFilter( const OUString& rFilter, const OUString& rType )
1838 : {
1839 0 : GtkFileFilter *filter = gtk_file_filter_new();
1840 :
1841 0 : OUString aShrunkName = shrinkFilterName( rFilter );
1842 0 : OString aFilterName = OUStringToOString( aShrunkName, RTL_TEXTENCODING_UTF8 );
1843 0 : gtk_file_filter_set_name( filter, aFilterName.getStr() );
1844 :
1845 0 : static const OUString aStarDot( "*." );
1846 0 : OUString aTokens;
1847 :
1848 0 : bool bAllGlob = !rType.compareToAscii( "*.*" ) || !rType.compareToAscii( "*" );
1849 0 : if (bAllGlob)
1850 0 : gtk_file_filter_add_pattern( filter, "*" );
1851 : else
1852 : {
1853 0 : sal_Int32 nIndex = 0;
1854 0 : OUString aToken;
1855 0 : do
1856 : {
1857 0 : aToken = rType.getToken( 0, ';', nIndex );
1858 : // Assume all have the "*.<extn>" syntax
1859 0 : sal_Int32 nStarDot = aToken.lastIndexOf( aStarDot );
1860 0 : if (nStarDot >= 0)
1861 0 : aToken = aToken.copy( nStarDot + 2 );
1862 0 : if (!aToken.isEmpty())
1863 : {
1864 0 : if (!aTokens.isEmpty())
1865 0 : aTokens += ",";
1866 0 : aTokens = aTokens += aToken;
1867 : gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_URI,
1868 : case_insensitive_filter,
1869 0 : g_strdup( OUStringToOString(aToken, RTL_TEXTENCODING_UTF8).getStr() ),
1870 0 : (GDestroyNotify) g_free );
1871 :
1872 : OSL_TRACE( "fustering with %s", OUStringToOString( aToken, RTL_TEXTENCODING_UTF8 ).getStr());
1873 : }
1874 : #if OSL_DEBUG_LEVEL > 0
1875 : else
1876 : {
1877 : g_warning( "Duff filter token '%s'\n",
1878 : OUStringToOString(
1879 : rType.getToken( 0, ';', nIndex ), RTL_TEXTENCODING_UTF8 ).getStr() );
1880 : }
1881 : #endif
1882 : }
1883 0 : while( nIndex >= 0 );
1884 : }
1885 :
1886 0 : gtk_file_chooser_add_filter( GTK_FILE_CHOOSER( m_pDialog ), filter );
1887 :
1888 0 : if (!bAllGlob)
1889 : {
1890 : GtkTreeIter iter;
1891 0 : gtk_list_store_append (m_pFilterStore, &iter);
1892 : gtk_list_store_set (m_pFilterStore, &iter,
1893 : 0, OUStringToOString(shrinkFilterName( rFilter, true ), RTL_TEXTENCODING_UTF8).getStr(),
1894 : 1, OUStringToOString(aTokens, RTL_TEXTENCODING_UTF8).getStr(),
1895 : 2, aFilterName.getStr(),
1896 : 3, OUStringToOString(rType, RTL_TEXTENCODING_UTF8).getStr(),
1897 0 : -1);
1898 : }
1899 0 : return filter;
1900 : }
1901 :
1902 0 : void SalGtkFilePicker::implAddFilterGroup( const OUString& /*_rFilter*/, const Sequence< StringPair >& _rFilters )
1903 : {
1904 : // Gtk+ has no filter group concept I think so ...
1905 : // implAddFilter( _rFilter, String() );
1906 0 : const StringPair* pSubFilters = _rFilters.getConstArray();
1907 0 : const StringPair* pSubFiltersEnd = pSubFilters + _rFilters.getLength();
1908 0 : for( ; pSubFilters != pSubFiltersEnd; ++pSubFilters )
1909 0 : implAddFilter( pSubFilters->First, pSubFilters->Second );
1910 0 : }
1911 :
1912 0 : void SalGtkFilePicker::SetFilters()
1913 : {
1914 0 : if (m_aInitialFilter.isEmpty())
1915 0 : m_aInitialFilter = m_aCurrentFilter;
1916 :
1917 0 : OUString sPseudoFilter;
1918 0 : if( GTK_FILE_CHOOSER_ACTION_SAVE == gtk_file_chooser_get_action( GTK_FILE_CHOOSER( m_pDialog ) ) )
1919 : {
1920 0 : std::set<OUString> aAllFormats;
1921 0 : if( m_pFilterList && !m_pFilterList->empty() )
1922 : {
1923 0 : for ( FilterList::iterator aListIter = m_pFilterList->begin();
1924 0 : aListIter != m_pFilterList->end();
1925 : ++aListIter
1926 : )
1927 : {
1928 0 : if( aListIter->hasSubFilters() )
1929 : { // it's a filter group
1930 0 : UnoFilterList aSubFilters;
1931 0 : aListIter->getSubFilters( aSubFilters );
1932 0 : const StringPair* pSubFilters = aSubFilters.getConstArray();
1933 0 : const StringPair* pSubFiltersEnd = pSubFilters + aSubFilters.getLength();
1934 0 : for( ; pSubFilters != pSubFiltersEnd; ++pSubFilters )
1935 0 : aAllFormats.insert(pSubFilters->Second);
1936 : }
1937 : else
1938 0 : aAllFormats.insert(aListIter->getFilter());
1939 : }
1940 : }
1941 0 : if (aAllFormats.size() > 1)
1942 : {
1943 0 : OUString sAllFilter;
1944 0 : std::set<OUString>::const_iterator aEnd = aAllFormats.end();
1945 0 : for (std::set<OUString>::const_iterator aIter = aAllFormats.begin(); aIter != aEnd; ++aIter)
1946 : {
1947 0 : if (!sAllFilter.isEmpty())
1948 0 : sAllFilter += OUString(sal_Unicode(';'));
1949 0 : sAllFilter += *aIter;
1950 : }
1951 0 : sPseudoFilter = getResString(FILE_PICKER_ALLFORMATS);
1952 0 : m_pPseudoFilter = implAddFilter( sPseudoFilter, sAllFilter );
1953 0 : }
1954 : }
1955 :
1956 0 : if( m_pFilterList && !m_pFilterList->empty() )
1957 : {
1958 0 : for ( FilterList::iterator aListIter = m_pFilterList->begin();
1959 0 : aListIter != m_pFilterList->end();
1960 : ++aListIter
1961 : )
1962 : {
1963 0 : if( aListIter->hasSubFilters() )
1964 : { // it's a filter group
1965 :
1966 0 : UnoFilterList aSubFilters;
1967 0 : aListIter->getSubFilters( aSubFilters );
1968 :
1969 0 : implAddFilterGroup( aListIter->getTitle(), aSubFilters );
1970 : }
1971 : else
1972 : {
1973 : // it's a single filter
1974 :
1975 0 : implAddFilter( aListIter->getTitle(), aListIter->getFilter() );
1976 : }
1977 : }
1978 : }
1979 :
1980 0 : if (gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_pFilterStore), NULL)) //If m_pFilterStore is not empty
1981 0 : gtk_widget_show( m_pFilterExpander );
1982 : else
1983 0 : gtk_widget_hide( m_pFilterExpander );
1984 :
1985 : // set the default filter
1986 0 : if (!sPseudoFilter.isEmpty())
1987 0 : SetCurFilter( sPseudoFilter );
1988 0 : else if(!m_aCurrentFilter.isEmpty())
1989 0 : SetCurFilter( m_aCurrentFilter );
1990 :
1991 0 : OSL_TRACE( "end setting filters");
1992 0 : }
1993 :
1994 0 : SalGtkFilePicker::~SalGtkFilePicker()
1995 : {
1996 0 : SolarMutexGuard g;
1997 :
1998 : int i;
1999 :
2000 0 : for( i = 0; i < TOGGLE_LAST; i++ )
2001 0 : gtk_widget_destroy( m_pToggles[i] );
2002 :
2003 0 : for( i = 0; i < LIST_LAST; i++ )
2004 : {
2005 0 : gtk_widget_destroy( m_pListLabels[i] );
2006 0 : gtk_widget_destroy( m_pAligns[i] ); //m_pAligns[i] owns m_pLists[i]
2007 0 : gtk_widget_destroy( m_pHBoxs[i] );
2008 : }
2009 :
2010 0 : delete m_pFilterList;
2011 :
2012 0 : gtk_widget_destroy( m_pVBox );
2013 0 : }
2014 :
2015 : using namespace ::com::sun::star;
2016 :
2017 : uno::Reference< ui::dialogs::XFilePicker2 >
2018 0 : GtkInstance::createFilePicker( const com::sun::star::uno::Reference<
2019 : com::sun::star::uno::XComponentContext > &xMSF )
2020 : {
2021 : return uno::Reference< ui::dialogs::XFilePicker2 >(
2022 0 : new SalGtkFilePicker( xMSF ) );
2023 0 : }
2024 :
2025 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|